|
| 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/material.dart'; |
| 6 | +import 'package:flutter_api_samples/widgets/animated_switcher/animated_switcher.0.dart' |
| 7 | + as example; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + testWidgets('Increments counter on button tap', (WidgetTester tester) async { |
| 12 | + await tester.pumpWidget( |
| 13 | + const example.AnimatedSwitcherExampleApp(), |
| 14 | + ); |
| 15 | + |
| 16 | + int counter = 0; |
| 17 | + |
| 18 | + expect(find.text('$counter'), findsOneWidget); |
| 19 | + |
| 20 | + while (counter < 10) { |
| 21 | + // Tap on the button to increment the counter. |
| 22 | + await tester.tap( |
| 23 | + find.ancestor( |
| 24 | + of: find.text('Increment'), |
| 25 | + matching: find.byType(ElevatedButton), |
| 26 | + ), |
| 27 | + ); |
| 28 | + await tester.pumpAndSettle(); |
| 29 | + |
| 30 | + counter += 1; |
| 31 | + |
| 32 | + expect(find.text('${counter - 1}'), findsNothing); |
| 33 | + expect(find.text('$counter'), findsOneWidget); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + testWidgets('Animates counter change', (WidgetTester tester) async { |
| 38 | + await tester.pumpWidget( |
| 39 | + const example.AnimatedSwitcherExampleApp(), |
| 40 | + ); |
| 41 | + |
| 42 | + // The animation duration defined in the example app. |
| 43 | + const Duration animationDuration = Duration(milliseconds: 500); |
| 44 | + |
| 45 | + final Finder zeroTransitionFinder = find.ancestor( |
| 46 | + of: find.text('0'), |
| 47 | + matching: find.byType(ScaleTransition), |
| 48 | + ); |
| 49 | + final Finder oneTransitionFinder = find.ancestor( |
| 50 | + of: find.text('1'), |
| 51 | + matching: find.byType(ScaleTransition), |
| 52 | + ); |
| 53 | + |
| 54 | + expect(zeroTransitionFinder, findsOneWidget); |
| 55 | + ScaleTransition zeroTransition = tester.widget(zeroTransitionFinder); |
| 56 | + expect(zeroTransition.scale.value, equals(1.0)); |
| 57 | + |
| 58 | + expect(oneTransitionFinder, findsNothing); |
| 59 | + |
| 60 | + // Tap on the button to increment the counter. |
| 61 | + await tester.tap( |
| 62 | + find.ancestor( |
| 63 | + of: find.text('Increment'), |
| 64 | + matching: find.byType(ElevatedButton), |
| 65 | + ), |
| 66 | + ); |
| 67 | + await tester.pump(); |
| 68 | + |
| 69 | + expect(zeroTransitionFinder, findsOneWidget); |
| 70 | + zeroTransition = tester.widget(zeroTransitionFinder); |
| 71 | + expect(zeroTransition.scale.value, equals(1.0)); |
| 72 | + |
| 73 | + expect(oneTransitionFinder, findsOneWidget); |
| 74 | + ScaleTransition oneTransition = tester.widget(oneTransitionFinder); |
| 75 | + expect(oneTransition.scale.value, equals(0.0)); |
| 76 | + |
| 77 | + // Advance animation to the middle. |
| 78 | + await tester.pump(animationDuration ~/ 2); |
| 79 | + |
| 80 | + expect(zeroTransitionFinder, findsOneWidget); |
| 81 | + zeroTransition = tester.widget(zeroTransitionFinder); |
| 82 | + expect(zeroTransition.scale.value, equals(0.5)); |
| 83 | + |
| 84 | + expect(oneTransitionFinder, findsOneWidget); |
| 85 | + oneTransition = tester.widget(oneTransitionFinder); |
| 86 | + expect(oneTransition.scale.value, equals(0.5)); |
| 87 | + |
| 88 | + // Advance animation to the end. |
| 89 | + await tester.pump(animationDuration ~/ 2); |
| 90 | + |
| 91 | + expect(zeroTransitionFinder, findsOneWidget); |
| 92 | + zeroTransition = tester.widget(zeroTransitionFinder); |
| 93 | + expect(zeroTransition.scale.value, equals(0.0)); |
| 94 | + |
| 95 | + expect(oneTransitionFinder, findsOneWidget); |
| 96 | + oneTransition = tester.widget(oneTransitionFinder); |
| 97 | + expect(oneTransition.scale.value, equals(1.0)); |
| 98 | + }); |
| 99 | +} |
0 commit comments