|
| 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 'dart:ui'; |
| 6 | + |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | +import 'package:flutter_api_samples/widgets/implicit_animations/animated_positioned.0.dart' |
| 9 | + as example; |
| 10 | +import 'package:flutter_test/flutter_test.dart'; |
| 11 | + |
| 12 | +void main() { |
| 13 | + testWidgets( |
| 14 | + 'AnimatedPositioned animates on tap', |
| 15 | + (WidgetTester tester) async { |
| 16 | + await tester.pumpWidget( |
| 17 | + const example.AnimatedPositionedExampleApp(), |
| 18 | + ); |
| 19 | + |
| 20 | + final Finder positionedFinder = find.descendant( |
| 21 | + of: find.byType(AnimatedPositioned), |
| 22 | + matching: find.byType(Positioned), |
| 23 | + ); |
| 24 | + |
| 25 | + const double beginWidth = 50.0; |
| 26 | + const double endWidth = 200.0; |
| 27 | + const double beginHeight = 200.0; |
| 28 | + const double endHeight = 50.0; |
| 29 | + const double beginTop = 150.0; |
| 30 | + const double endTop = 50.0; |
| 31 | + |
| 32 | + Positioned positioned = tester.widget(positionedFinder); |
| 33 | + expect(positioned.width, beginWidth); |
| 34 | + expect(positioned.height, beginHeight); |
| 35 | + expect(positioned.top, beginTop); |
| 36 | + |
| 37 | + // Tap on the 'Tap me' text to start the forward animation. |
| 38 | + await tester.tap(find.text('Tap me')); |
| 39 | + await tester.pump(); |
| 40 | + |
| 41 | + positioned = tester.widget(positionedFinder); |
| 42 | + expect(positioned.width, beginWidth); |
| 43 | + expect(positioned.height, beginHeight); |
| 44 | + expect(positioned.top, beginTop); |
| 45 | + |
| 46 | + // Advance animation to the middle. |
| 47 | + await tester.pump(example.AnimatedPositionedExampleApp.duration ~/ 2); |
| 48 | + |
| 49 | + final double t = |
| 50 | + example.AnimatedPositionedExampleApp.curve.transform(0.5); |
| 51 | + |
| 52 | + positioned = tester.widget(positionedFinder); |
| 53 | + expect(positioned.width, lerpDouble(beginWidth, endWidth, t)); |
| 54 | + expect(positioned.height, lerpDouble(beginHeight, endHeight, t)); |
| 55 | + expect(positioned.top, lerpDouble(beginTop, endTop, t)); |
| 56 | + |
| 57 | + // Advance animation to the end. |
| 58 | + await tester.pump(example.AnimatedPositionedExampleApp.duration ~/ 2); |
| 59 | + |
| 60 | + positioned = tester.widget(positionedFinder); |
| 61 | + expect(positioned.width, endWidth); |
| 62 | + expect(positioned.height, endHeight); |
| 63 | + expect(positioned.top, endTop); |
| 64 | + |
| 65 | + // Tap on the 'Tap me' text again to start the reverse animation. |
| 66 | + await tester.tap(find.text('Tap me')); |
| 67 | + await tester.pump(); |
| 68 | + |
| 69 | + positioned = tester.widget(positionedFinder); |
| 70 | + expect(positioned.width, endWidth); |
| 71 | + expect(positioned.height, endHeight); |
| 72 | + expect(positioned.top, endTop); |
| 73 | + |
| 74 | + // Advance animation to the middle. |
| 75 | + await tester.pump(example.AnimatedPositionedExampleApp.duration ~/ 2); |
| 76 | + |
| 77 | + positioned = tester.widget(positionedFinder); |
| 78 | + expect(positioned.width, lerpDouble(endWidth, beginWidth, t)); |
| 79 | + expect(positioned.height, lerpDouble(endHeight, beginHeight, t)); |
| 80 | + expect(positioned.top, lerpDouble(endTop, beginTop, t)); |
| 81 | + |
| 82 | + // Advance animation to the end. |
| 83 | + await tester.pump(example.AnimatedPositionedExampleApp.duration ~/ 2); |
| 84 | + |
| 85 | + positioned = tester.widget(positionedFinder); |
| 86 | + expect(positioned.width, beginWidth); |
| 87 | + expect(positioned.height, beginHeight); |
| 88 | + expect(positioned.top, beginTop); |
| 89 | + }, |
| 90 | + ); |
| 91 | +} |
0 commit comments