-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add restore default to color picker (#84)
- Loading branch information
Showing
6 changed files
with
194 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
test/widgets/dialog_widgets/dialog_color_picker_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
|
||
import 'package:elastic_dashboard/widgets/dialog_widgets/dialog_color_picker.dart'; | ||
import '../../test_util.dart'; | ||
|
||
class MockColorCallback extends Mock { | ||
void onColorChanged(Color? color); | ||
} | ||
|
||
void main() { | ||
TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
testWidgets('Color picker select', (widgetTester) async { | ||
FlutterError.onError = ignoreOverflowErrors; | ||
|
||
MockColorCallback mockCallback = MockColorCallback(); | ||
|
||
Color? calledBackColor; | ||
|
||
when(mockCallback.onColorChanged(any)).thenAnswer((realInvocation) { | ||
calledBackColor = realInvocation.positionalArguments[0]; | ||
}); | ||
|
||
await widgetTester.pumpWidget( | ||
MaterialApp( | ||
home: Scaffold( | ||
body: DialogColorPicker( | ||
onColorPicked: mockCallback.onColorChanged, | ||
label: 'Color Picker', | ||
initialColor: Colors.green, | ||
), | ||
), | ||
), | ||
); | ||
|
||
await widgetTester.pumpAndSettle(); | ||
|
||
expect(find.text('Color Picker'), findsOneWidget); | ||
expect(find.byType(ElevatedButton), findsOneWidget); | ||
|
||
await widgetTester.tap(find.byType(ElevatedButton)); | ||
await widgetTester.pumpAndSettle(); | ||
|
||
expect(find.text('Cancel'), findsOneWidget); | ||
expect(find.text('Restore Default'), findsNothing); | ||
expect(find.text('Save'), findsOneWidget); | ||
|
||
final hexInput = find.widgetWithText(TextField, 'Hex Code'); | ||
|
||
expect(hexInput, findsOneWidget); | ||
|
||
await widgetTester.enterText(hexInput, '0000FF'); | ||
await widgetTester.testTextInput.receiveAction(TextInputAction.done); | ||
await widgetTester.pumpAndSettle(); | ||
|
||
expect(calledBackColor, isNull); | ||
|
||
await widgetTester.tap(find.text('Save')); | ||
await widgetTester.pumpAndSettle(); | ||
|
||
expect(calledBackColor, isNotNull); | ||
expect(calledBackColor!.value, 0xFF0000FF); | ||
}); | ||
|
||
testWidgets('Color picker cancel', (widgetTester) async { | ||
FlutterError.onError = ignoreOverflowErrors; | ||
|
||
MockColorCallback mockCallback = MockColorCallback(); | ||
|
||
Color? calledBackColor; | ||
|
||
when(mockCallback.onColorChanged(any)).thenAnswer((realInvocation) { | ||
calledBackColor = realInvocation.positionalArguments[0]; | ||
}); | ||
|
||
await widgetTester.pumpWidget( | ||
MaterialApp( | ||
home: Scaffold( | ||
body: DialogColorPicker( | ||
onColorPicked: mockCallback.onColorChanged, | ||
label: 'Color Picker', | ||
initialColor: Colors.green, | ||
), | ||
), | ||
), | ||
); | ||
|
||
await widgetTester.pumpAndSettle(); | ||
|
||
expect(find.text('Color Picker'), findsOneWidget); | ||
expect(find.byType(ElevatedButton), findsOneWidget); | ||
|
||
await widgetTester.tap(find.byType(ElevatedButton)); | ||
await widgetTester.pumpAndSettle(); | ||
|
||
expect(find.text('Cancel'), findsOneWidget); | ||
expect(find.text('Restore Default'), findsNothing); | ||
expect(find.text('Save'), findsOneWidget); | ||
|
||
final hexInput = find.widgetWithText(TextField, 'Hex Code'); | ||
|
||
expect(hexInput, findsOneWidget); | ||
|
||
await widgetTester.enterText(hexInput, '0000FF'); | ||
await widgetTester.testTextInput.receiveAction(TextInputAction.done); | ||
await widgetTester.pumpAndSettle(); | ||
|
||
expect(calledBackColor, isNull); | ||
|
||
await widgetTester.tap(find.text('Cancel')); | ||
await widgetTester.pumpAndSettle(); | ||
|
||
expect(calledBackColor, isNotNull); | ||
expect(calledBackColor!.value, Colors.green.value); | ||
}); | ||
|
||
testWidgets('Color picker restore default', (widgetTester) async { | ||
FlutterError.onError = ignoreOverflowErrors; | ||
|
||
MockColorCallback mockCallback = MockColorCallback(); | ||
|
||
Color? calledBackColor; | ||
|
||
when(mockCallback.onColorChanged(any)).thenAnswer((realInvocation) { | ||
calledBackColor = realInvocation.positionalArguments[0]; | ||
}); | ||
|
||
await widgetTester.pumpWidget( | ||
MaterialApp( | ||
home: Scaffold( | ||
body: DialogColorPicker( | ||
onColorPicked: mockCallback.onColorChanged, | ||
label: 'Color Picker', | ||
initialColor: Colors.green, | ||
defaultColor: Colors.red, | ||
), | ||
), | ||
), | ||
); | ||
|
||
await widgetTester.pumpAndSettle(); | ||
|
||
expect(find.text('Color Picker'), findsOneWidget); | ||
expect(find.byType(ElevatedButton), findsOneWidget); | ||
|
||
await widgetTester.tap(find.byType(ElevatedButton)); | ||
await widgetTester.pumpAndSettle(); | ||
|
||
expect(find.text('Cancel'), findsOneWidget); | ||
expect(find.text('Restore Default'), findsOneWidget); | ||
expect(find.text('Save'), findsOneWidget); | ||
|
||
await widgetTester.tap(find.text('Restore Default')); | ||
await widgetTester.pumpAndSettle(); | ||
|
||
expect(calledBackColor, isNotNull); | ||
expect(calledBackColor!.value, Colors.red.value); | ||
}); | ||
} |