|
| 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/action_listener.0.dart' as example; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + testWidgets('ActionListener can be enabled, triggered, and disabled', (WidgetTester tester) async { |
| 12 | + final List<String?> log = <String?>[]; |
| 13 | + |
| 14 | + final DebugPrintCallback originalDebugPrint = debugPrint; |
| 15 | + debugPrint = (String? message, {int? wrapWidth}) { |
| 16 | + log.add(message); |
| 17 | + }; |
| 18 | + try { |
| 19 | + await tester.pumpWidget( |
| 20 | + const example.ActionListenerExampleApp(), |
| 21 | + ); |
| 22 | + |
| 23 | + expect(find.widgetWithText(AppBar, 'ActionListener Sample'), findsOne); |
| 24 | + expect(find.widgetWithText(OutlinedButton, 'Enable'), findsOne); |
| 25 | + |
| 26 | + await tester.tap(find.byType(OutlinedButton)); |
| 27 | + await tester.pump(); |
| 28 | + |
| 29 | + expect(find.widgetWithText(OutlinedButton, 'Disable'), findsOne); |
| 30 | + expect(find.widgetWithText(ElevatedButton, 'Call Action Listener'), findsOne); |
| 31 | + expect(log, const <String?>['Action Listener was added']); |
| 32 | + |
| 33 | + await tester.tap(find.text('Call Action Listener')); |
| 34 | + await tester.pumpAndSettle(); |
| 35 | + |
| 36 | + expect(find.widgetWithText(SnackBar, 'Action Listener Called'), findsOne); |
| 37 | + |
| 38 | + await tester.tap(find.text('Disable')); |
| 39 | + await tester.pump(); |
| 40 | + |
| 41 | + expect(find.widgetWithText(OutlinedButton, 'Enable'), findsOne); |
| 42 | + expect( |
| 43 | + log, |
| 44 | + const <String?>['Action Listener was added', 'Action Listener was removed'], |
| 45 | + ); |
| 46 | + } finally { |
| 47 | + debugPrint = originalDebugPrint; |
| 48 | + } |
| 49 | + }); |
| 50 | +} |
0 commit comments