|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/foundation.dart'; |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | +import 'package:flutter_api_samples/widgets/actions/actions.0.dart' as example; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + Color? getSaveButtonColor(WidgetTester tester) { |
| 12 | + final ButtonStyleButton button = tester.widget<ButtonStyleButton>( |
| 13 | + find.descendant( |
| 14 | + of: find.byType(example.SaveButton), |
| 15 | + matching: find.byWidgetPredicate( |
| 16 | + (Widget widget) => widget is TextButton, |
| 17 | + ), |
| 18 | + ), |
| 19 | + ); |
| 20 | + |
| 21 | + return button.style?.foregroundColor?.resolve(<WidgetState>{}); |
| 22 | + } |
| 23 | + |
| 24 | + testWidgets('increments and decrements value', (WidgetTester tester) async { |
| 25 | + await tester.pumpWidget( |
| 26 | + const example.ActionsExampleApp(), |
| 27 | + ); |
| 28 | + |
| 29 | + int value = 0; |
| 30 | + |
| 31 | + while (value < 10) { |
| 32 | + expect(find.text('Value: $value'), findsOneWidget); |
| 33 | + |
| 34 | + // Increment the value. |
| 35 | + await tester.tap(find.byIcon(Icons.exposure_plus_1)); |
| 36 | + await tester.pump(); |
| 37 | + |
| 38 | + value++; |
| 39 | + } |
| 40 | + |
| 41 | + while (value >= 0) { |
| 42 | + expect(find.text('Value: $value'), findsOneWidget); |
| 43 | + |
| 44 | + // Decrement the value. |
| 45 | + await tester.tap(find.byIcon(Icons.exposure_minus_1)); |
| 46 | + await tester.pump(); |
| 47 | + |
| 48 | + value--; |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + testWidgets('SaveButton indicates dirty status', (WidgetTester tester) async { |
| 53 | + await tester.pumpWidget( |
| 54 | + const example.ActionsExampleApp(), |
| 55 | + ); |
| 56 | + |
| 57 | + // Verify that initial color is green, as the value is not marked as dirty. |
| 58 | + Color? saveButtonColor = getSaveButtonColor(tester); |
| 59 | + expect(saveButtonColor, equals(Colors.green)); |
| 60 | + |
| 61 | + // Decrement the value, which marks it as dirty. |
| 62 | + await tester.tap(find.byIcon(Icons.exposure_minus_1)); |
| 63 | + await tester.pump(); |
| 64 | + expect(find.text('Value: -1'), findsOneWidget); |
| 65 | + |
| 66 | + // Verify that the color is red, as the value is marked as dirty. |
| 67 | + saveButtonColor = getSaveButtonColor(tester); |
| 68 | + expect(saveButtonColor, equals(Colors.red)); |
| 69 | + |
| 70 | + // Increment the value. |
| 71 | + await tester.tap(find.byIcon(Icons.exposure_plus_1)); |
| 72 | + await tester.pump(); |
| 73 | + expect(find.text('Value: 0'), findsOneWidget); |
| 74 | + |
| 75 | + // Verify that the color is red, as the value is still marked as dirty. |
| 76 | + saveButtonColor = getSaveButtonColor(tester); |
| 77 | + expect(saveButtonColor, equals(Colors.red)); |
| 78 | + }); |
| 79 | + |
| 80 | + testWidgets('SaveButton tap resets dirty status and adds log', (WidgetTester tester) async { |
| 81 | + final List<String?> log = <String?>[]; |
| 82 | + |
| 83 | + final DebugPrintCallback originalDebugPrint = debugPrint; |
| 84 | + debugPrint = (String? message, {int? wrapWidth}) { |
| 85 | + log.add(message); |
| 86 | + }; |
| 87 | + |
| 88 | + await tester.pumpWidget( |
| 89 | + const example.ActionsExampleApp(), |
| 90 | + ); |
| 91 | + |
| 92 | + // Verify that value is not marked as dirty. |
| 93 | + Color? saveButtonColor = getSaveButtonColor(tester); |
| 94 | + expect(saveButtonColor, equals(Colors.green)); |
| 95 | + expect( |
| 96 | + find.descendant( |
| 97 | + of: find.byType(example.SaveButton), |
| 98 | + matching: find.text('0'), |
| 99 | + ), |
| 100 | + findsOneWidget, |
| 101 | + ); |
| 102 | + |
| 103 | + // Decrement the value, which marks it as dirty. |
| 104 | + await tester.tap(find.byIcon(Icons.exposure_minus_1)); |
| 105 | + await tester.pump(); |
| 106 | + expect(find.text('Value: -1'), findsOneWidget); |
| 107 | + |
| 108 | + // Verify that value is marked as dirty. |
| 109 | + saveButtonColor = getSaveButtonColor(tester); |
| 110 | + expect(saveButtonColor, equals(Colors.red)); |
| 111 | + expect( |
| 112 | + find.descendant( |
| 113 | + of: find.byType(example.SaveButton), |
| 114 | + matching: find.text('0'), |
| 115 | + ), |
| 116 | + findsOneWidget, |
| 117 | + ); |
| 118 | + |
| 119 | + // Tap SaveButton to reset dirty status. |
| 120 | + await tester.tap(find.byType(example.SaveButton)); |
| 121 | + await tester.pump(); |
| 122 | + |
| 123 | + // Verify log record. |
| 124 | + expect(log.length, equals(1)); |
| 125 | + expect(log.last, equals('Saved Data: -1')); |
| 126 | + |
| 127 | + // Verify that value is no more marked as dirty. |
| 128 | + saveButtonColor = getSaveButtonColor(tester); |
| 129 | + expect(saveButtonColor, equals(Colors.green)); |
| 130 | + expect( |
| 131 | + find.descendant( |
| 132 | + of: find.byType(example.SaveButton), |
| 133 | + matching: find.text('-1'), |
| 134 | + ), |
| 135 | + findsOneWidget, |
| 136 | + ); |
| 137 | + |
| 138 | + debugPrint = originalDebugPrint; |
| 139 | + }); |
| 140 | +} |
0 commit comments