|
| 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_fractionally_sized_box.0.dart' |
| 9 | + as example; |
| 10 | +import 'package:flutter_test/flutter_test.dart'; |
| 11 | + |
| 12 | +void main() { |
| 13 | + testWidgets( |
| 14 | + 'AnimatedFractionallySizedBox animates on tap', |
| 15 | + (WidgetTester tester) async { |
| 16 | + await tester.pumpWidget( |
| 17 | + const example.AnimatedFractionallySizedBoxExampleApp(), |
| 18 | + ); |
| 19 | + |
| 20 | + final Finder fractionallySizedBoxFinder = find.descendant( |
| 21 | + of: find.byType(AnimatedFractionallySizedBox), |
| 22 | + matching: find.byType(FractionallySizedBox), |
| 23 | + ); |
| 24 | + |
| 25 | + const double beginWidthFactor = 0.75; |
| 26 | + const double endWidthFactor = 0.25; |
| 27 | + const double beginHeightFactor = 0.25; |
| 28 | + const double endHeightFactor = 0.75; |
| 29 | + const Alignment beginAlignment = Alignment.bottomRight; |
| 30 | + const Alignment endAlignment = Alignment.topLeft; |
| 31 | + |
| 32 | + FractionallySizedBox fractionallySizedBox = tester.widget( |
| 33 | + fractionallySizedBoxFinder, |
| 34 | + ); |
| 35 | + expect(fractionallySizedBox.widthFactor, beginWidthFactor); |
| 36 | + expect(fractionallySizedBox.heightFactor, beginHeightFactor); |
| 37 | + expect(fractionallySizedBox.alignment, beginAlignment); |
| 38 | + |
| 39 | + // Tap on the AnimatedFractionallySizedBoxExample to start the forward |
| 40 | + // animation. |
| 41 | + await tester.tap( |
| 42 | + find.byType(example.AnimatedFractionallySizedBoxExample), |
| 43 | + ); |
| 44 | + await tester.pump(); |
| 45 | + |
| 46 | + fractionallySizedBox = tester.widget(fractionallySizedBoxFinder); |
| 47 | + expect(fractionallySizedBox.widthFactor, beginWidthFactor); |
| 48 | + expect(fractionallySizedBox.heightFactor, beginHeightFactor); |
| 49 | + expect(fractionallySizedBox.alignment, beginAlignment); |
| 50 | + |
| 51 | + // Advance animation to the middle. |
| 52 | + await tester.pump( |
| 53 | + example.AnimatedFractionallySizedBoxExampleApp.duration ~/ 2, |
| 54 | + ); |
| 55 | + |
| 56 | + final double t = |
| 57 | + example.AnimatedFractionallySizedBoxExampleApp.curve.transform(0.5); |
| 58 | + |
| 59 | + fractionallySizedBox = tester.widget(fractionallySizedBoxFinder); |
| 60 | + expect( |
| 61 | + fractionallySizedBox.widthFactor, |
| 62 | + lerpDouble(beginWidthFactor, endWidthFactor, t), |
| 63 | + ); |
| 64 | + expect( |
| 65 | + fractionallySizedBox.heightFactor, |
| 66 | + lerpDouble(beginHeightFactor, endHeightFactor, t), |
| 67 | + ); |
| 68 | + expect( |
| 69 | + fractionallySizedBox.alignment, |
| 70 | + Alignment.lerp(beginAlignment, endAlignment, t), |
| 71 | + ); |
| 72 | + |
| 73 | + // Advance animation to the end. |
| 74 | + await tester.pump( |
| 75 | + example.AnimatedFractionallySizedBoxExampleApp.duration ~/ 2, |
| 76 | + ); |
| 77 | + |
| 78 | + fractionallySizedBox = tester.widget(fractionallySizedBoxFinder); |
| 79 | + expect(fractionallySizedBox.widthFactor, endWidthFactor); |
| 80 | + expect(fractionallySizedBox.heightFactor, endHeightFactor); |
| 81 | + expect(fractionallySizedBox.alignment, endAlignment); |
| 82 | + |
| 83 | + // Tap on the AnimatedFractionallySizedBoxExample again to start the |
| 84 | + // reverse animation. |
| 85 | + await tester.tap( |
| 86 | + find.byType(example.AnimatedFractionallySizedBoxExample), |
| 87 | + ); |
| 88 | + await tester.pump(); |
| 89 | + |
| 90 | + fractionallySizedBox = tester.widget(fractionallySizedBoxFinder); |
| 91 | + expect(fractionallySizedBox.widthFactor, endWidthFactor); |
| 92 | + expect(fractionallySizedBox.heightFactor, endHeightFactor); |
| 93 | + expect(fractionallySizedBox.alignment, endAlignment); |
| 94 | + |
| 95 | + // Advance animation to the middle. |
| 96 | + await tester.pump( |
| 97 | + example.AnimatedFractionallySizedBoxExampleApp.duration ~/ 2, |
| 98 | + ); |
| 99 | + |
| 100 | + fractionallySizedBox = tester.widget(fractionallySizedBoxFinder); |
| 101 | + expect( |
| 102 | + fractionallySizedBox.widthFactor, |
| 103 | + lerpDouble(endWidthFactor, beginWidthFactor, t), |
| 104 | + ); |
| 105 | + expect( |
| 106 | + fractionallySizedBox.heightFactor, |
| 107 | + lerpDouble(endHeightFactor, beginHeightFactor, t), |
| 108 | + ); |
| 109 | + expect( |
| 110 | + fractionallySizedBox.alignment, |
| 111 | + Alignment.lerp(endAlignment, beginAlignment, t), |
| 112 | + ); |
| 113 | + |
| 114 | + // Advance animation to the end. |
| 115 | + await tester.pump( |
| 116 | + example.AnimatedFractionallySizedBoxExampleApp.duration ~/ 2, |
| 117 | + ); |
| 118 | + |
| 119 | + fractionallySizedBox = tester.widget(fractionallySizedBoxFinder); |
| 120 | + expect(fractionallySizedBox.widthFactor, beginWidthFactor); |
| 121 | + expect(fractionallySizedBox.heightFactor, beginHeightFactor); |
| 122 | + expect(fractionallySizedBox.alignment, beginAlignment); |
| 123 | + }, |
| 124 | + ); |
| 125 | +} |
0 commit comments