forked from flutter/packages
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for actions.0.dart API example. (#148678)
This PR contributes to flutter/flutter#130459 ### Description - Updates `examples/api/lib/widgets/actions/actions.0.dart` to meet the latest API examples structure - Adds tests for `examples/api/lib/widgets/actions/actions.0.dart`
- Loading branch information
1 parent
ea7cf54
commit 112e8b5
Showing
3 changed files
with
174 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_api_samples/widgets/actions/actions.0.dart' as example; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
Color? getSaveButtonColor(WidgetTester tester) { | ||
final ButtonStyleButton button = tester.widget<ButtonStyleButton>( | ||
find.descendant( | ||
of: find.byType(example.SaveButton), | ||
matching: find.byWidgetPredicate( | ||
(Widget widget) => widget is TextButton, | ||
), | ||
), | ||
); | ||
|
||
return button.style?.foregroundColor?.resolve(<WidgetState>{}); | ||
} | ||
|
||
testWidgets('increments and decrements value', (WidgetTester tester) async { | ||
await tester.pumpWidget( | ||
const example.ActionsExampleApp(), | ||
); | ||
|
||
int value = 0; | ||
|
||
while (value < 10) { | ||
expect(find.text('Value: $value'), findsOneWidget); | ||
|
||
// Increment the value. | ||
await tester.tap(find.byIcon(Icons.exposure_plus_1)); | ||
await tester.pump(); | ||
|
||
value++; | ||
} | ||
|
||
while (value >= 0) { | ||
expect(find.text('Value: $value'), findsOneWidget); | ||
|
||
// Decrement the value. | ||
await tester.tap(find.byIcon(Icons.exposure_minus_1)); | ||
await tester.pump(); | ||
|
||
value--; | ||
} | ||
}); | ||
|
||
testWidgets('SaveButton indicates dirty status', (WidgetTester tester) async { | ||
await tester.pumpWidget( | ||
const example.ActionsExampleApp(), | ||
); | ||
|
||
// Verify that initial color is green, as the value is not marked as dirty. | ||
Color? saveButtonColor = getSaveButtonColor(tester); | ||
expect(saveButtonColor, equals(Colors.green)); | ||
|
||
// Decrement the value, which marks it as dirty. | ||
await tester.tap(find.byIcon(Icons.exposure_minus_1)); | ||
await tester.pump(); | ||
expect(find.text('Value: -1'), findsOneWidget); | ||
|
||
// Verify that the color is red, as the value is marked as dirty. | ||
saveButtonColor = getSaveButtonColor(tester); | ||
expect(saveButtonColor, equals(Colors.red)); | ||
|
||
// Increment the value. | ||
await tester.tap(find.byIcon(Icons.exposure_plus_1)); | ||
await tester.pump(); | ||
expect(find.text('Value: 0'), findsOneWidget); | ||
|
||
// Verify that the color is red, as the value is still marked as dirty. | ||
saveButtonColor = getSaveButtonColor(tester); | ||
expect(saveButtonColor, equals(Colors.red)); | ||
}); | ||
|
||
testWidgets('SaveButton tap resets dirty status and adds log', (WidgetTester tester) async { | ||
final List<String?> log = <String?>[]; | ||
|
||
final DebugPrintCallback originalDebugPrint = debugPrint; | ||
debugPrint = (String? message, {int? wrapWidth}) { | ||
log.add(message); | ||
}; | ||
|
||
await tester.pumpWidget( | ||
const example.ActionsExampleApp(), | ||
); | ||
|
||
// Verify that value is not marked as dirty. | ||
Color? saveButtonColor = getSaveButtonColor(tester); | ||
expect(saveButtonColor, equals(Colors.green)); | ||
expect( | ||
find.descendant( | ||
of: find.byType(example.SaveButton), | ||
matching: find.text('0'), | ||
), | ||
findsOneWidget, | ||
); | ||
|
||
// Decrement the value, which marks it as dirty. | ||
await tester.tap(find.byIcon(Icons.exposure_minus_1)); | ||
await tester.pump(); | ||
expect(find.text('Value: -1'), findsOneWidget); | ||
|
||
// Verify that value is marked as dirty. | ||
saveButtonColor = getSaveButtonColor(tester); | ||
expect(saveButtonColor, equals(Colors.red)); | ||
expect( | ||
find.descendant( | ||
of: find.byType(example.SaveButton), | ||
matching: find.text('0'), | ||
), | ||
findsOneWidget, | ||
); | ||
|
||
// Tap SaveButton to reset dirty status. | ||
await tester.tap(find.byType(example.SaveButton)); | ||
await tester.pump(); | ||
|
||
// Verify log record. | ||
expect(log.length, equals(1)); | ||
expect(log.last, equals('Saved Data: -1')); | ||
|
||
// Verify that value is no more marked as dirty. | ||
saveButtonColor = getSaveButtonColor(tester); | ||
expect(saveButtonColor, equals(Colors.green)); | ||
expect( | ||
find.descendant( | ||
of: find.byType(example.SaveButton), | ||
matching: find.text('-1'), | ||
), | ||
findsOneWidget, | ||
); | ||
|
||
debugPrint = originalDebugPrint; | ||
}); | ||
} |