Skip to content

Commit

Permalink
Add tests for actions.0.dart API example. (#148678)
Browse files Browse the repository at this point in the history
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
ksokolovskyi authored May 22, 2024
1 parent ea7cf54 commit 112e8b5
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 20 deletions.
1 change: 0 additions & 1 deletion dev/bots/check_code_samples.dart
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ final Set<String> _knownMissingTests = <String>{
'examples/api/test/widgets/single_child_scroll_view/single_child_scroll_view.1_test.dart',
'examples/api/test/widgets/single_child_scroll_view/single_child_scroll_view.0_test.dart',
'examples/api/test/widgets/restoration/restoration_mixin.0_test.dart',
'examples/api/test/widgets/actions/actions.0_test.dart',
'examples/api/test/widgets/actions/action_listener.0_test.dart',
'examples/api/test/widgets/actions/focusable_action_detector.0_test.dart',
'examples/api/test/widgets/color_filter/color_filtered.0_test.dart',
Expand Down
53 changes: 34 additions & 19 deletions examples/api/lib/widgets/actions/actions.0.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class ActionsExampleApp extends StatelessWidget {

// A simple model class that notifies listeners when it changes.
class Model {
ValueNotifier<bool> isDirty = ValueNotifier<bool>(false);
ValueNotifier<int> data = ValueNotifier<int>(0);
final ValueNotifier<bool> isDirty = ValueNotifier<bool>(false);
final ValueNotifier<int> data = ValueNotifier<int>(0);

int save() {
if (isDirty.value) {
Expand All @@ -41,6 +41,11 @@ class Model {
isDirty.value = data.value != newValue;
data.value = newValue;
}

void dispose() {
isDirty.dispose();
data.dispose();
}
}

class ModifyIntent extends Intent {
Expand Down Expand Up @@ -87,7 +92,7 @@ class SaveButton extends StatefulWidget {
}

class _SaveButtonState extends State<SaveButton> {
int savedValue = 0;
int _savedValue = 0;

@override
Widget build(BuildContext context) {
Expand All @@ -96,15 +101,15 @@ class _SaveButtonState extends State<SaveButton> {
builder: (BuildContext context, Widget? child) {
return TextButton.icon(
icon: const Icon(Icons.save),
label: Text('$savedValue'),
label: Text('$_savedValue'),
style: ButtonStyle(
foregroundColor: MaterialStatePropertyAll<Color>(
widget.valueNotifier.value ? Colors.red : Colors.green,
),
),
onPressed: () {
setState(() {
savedValue = Actions.invoke(context, const SaveIntent())! as int;
_savedValue = Actions.invoke(context, const SaveIntent())! as int;
});
},
);
Expand All @@ -121,15 +126,21 @@ class ActionsExample extends StatefulWidget {
}

class _ActionsExampleState extends State<ActionsExample> {
Model model = Model();
int count = 0;
final Model _model = Model();
int _count = 0;

@override
void dispose() {
_model.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Actions(
actions: <Type, Action<Intent>>{
ModifyIntent: ModifyAction(model),
SaveIntent: SaveAction(model),
ModifyIntent: ModifyAction(_model),
SaveIntent: SaveAction(_model),
},
child: Builder(
builder: (BuildContext context) {
Expand All @@ -143,26 +154,30 @@ class _ActionsExampleState extends State<ActionsExample> {
IconButton(
icon: const Icon(Icons.exposure_plus_1),
onPressed: () {
Actions.invoke(context, ModifyIntent(++count));
Actions.invoke(context, ModifyIntent(++_count));
},
),
ListenableBuilder(
listenable: model.data,
builder: (BuildContext context, Widget? child) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text('${model.data.value}', style: Theme.of(context).textTheme.headlineMedium),
);
}),
listenable: _model.data,
builder: (BuildContext context, Widget? child) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Value: ${_model.data.value}',
style: Theme.of(context).textTheme.headlineMedium,
),
);
},
),
IconButton(
icon: const Icon(Icons.exposure_minus_1),
onPressed: () {
Actions.invoke(context, ModifyIntent(--count));
Actions.invoke(context, ModifyIntent(--_count));
},
),
],
),
SaveButton(model.isDirty),
SaveButton(_model.isDirty),
const Spacer(),
],
);
Expand Down
140 changes: 140 additions & 0 deletions examples/api/test/widgets/actions/actions.0_test.dart
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;
});
}

0 comments on commit 112e8b5

Please sign in to comment.