Add tests for tween_animation_builder.0.dart API example. (#148902)
This PR contributes to https://github.com/flutter/flutter/issues/130459 ### Description - Adds tests for `examples/api/lib/widgets/tween_animation_builder/tween_animation_builder.0.dart`
This commit is contained in:
parent
5933e99c6c
commit
488fb09582
@ -393,7 +393,6 @@ final Set<String> _knownMissingTests = <String>{
|
|||||||
'examples/api/test/widgets/notification_listener/notification.0_test.dart',
|
'examples/api/test/widgets/notification_listener/notification.0_test.dart',
|
||||||
'examples/api/test/widgets/overscroll_indicator/glowing_overscroll_indicator.1_test.dart',
|
'examples/api/test/widgets/overscroll_indicator/glowing_overscroll_indicator.1_test.dart',
|
||||||
'examples/api/test/widgets/overscroll_indicator/glowing_overscroll_indicator.0_test.dart',
|
'examples/api/test/widgets/overscroll_indicator/glowing_overscroll_indicator.0_test.dart',
|
||||||
'examples/api/test/widgets/tween_animation_builder/tween_animation_builder.0_test.dart',
|
|
||||||
'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.1_test.dart',
|
||||||
'examples/api/test/widgets/single_child_scroll_view/single_child_scroll_view.0_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/restoration/restoration_mixin.0_test.dart',
|
||||||
|
@ -15,7 +15,9 @@ class TweenAnimationBuilderExampleApp extends StatelessWidget {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
home: Scaffold(
|
home: Scaffold(
|
||||||
appBar: AppBar(title: const Text('TweenAnimationBuilder Sample')),
|
appBar: AppBar(
|
||||||
|
title: const Text('TweenAnimationBuilder Sample'),
|
||||||
|
),
|
||||||
body: const Center(
|
body: const Center(
|
||||||
child: TweenAnimationBuilderExample(),
|
child: TweenAnimationBuilderExample(),
|
||||||
),
|
),
|
||||||
@ -28,16 +30,18 @@ class TweenAnimationBuilderExample extends StatefulWidget {
|
|||||||
const TweenAnimationBuilderExample({super.key});
|
const TweenAnimationBuilderExample({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<TweenAnimationBuilderExample> createState() => _TweenAnimationBuilderExampleState();
|
State<TweenAnimationBuilderExample> createState() =>
|
||||||
|
_TweenAnimationBuilderExampleState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _TweenAnimationBuilderExampleState extends State<TweenAnimationBuilderExample> {
|
class _TweenAnimationBuilderExampleState
|
||||||
double targetValue = 24.0;
|
extends State<TweenAnimationBuilderExample> {
|
||||||
|
double _targetValue = 24.0;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return TweenAnimationBuilder<double>(
|
return TweenAnimationBuilder<double>(
|
||||||
tween: Tween<double>(begin: 0, end: targetValue),
|
tween: Tween<double>(begin: 0, end: _targetValue),
|
||||||
duration: const Duration(seconds: 1),
|
duration: const Duration(seconds: 1),
|
||||||
builder: (BuildContext context, double size, Widget? child) {
|
builder: (BuildContext context, double size, Widget? child) {
|
||||||
return IconButton(
|
return IconButton(
|
||||||
@ -46,7 +50,7 @@ class _TweenAnimationBuilderExampleState extends State<TweenAnimationBuilderExam
|
|||||||
icon: child!,
|
icon: child!,
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
setState(() {
|
setState(() {
|
||||||
targetValue = targetValue == 24.0 ? 48.0 : 24.0;
|
_targetValue = _targetValue == 24.0 ? 48.0 : 24.0;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -0,0 +1,160 @@
|
|||||||
|
// 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 'dart:ui';
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_api_samples/widgets/tween_animation_builder/tween_animation_builder.0.dart'
|
||||||
|
as example;
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
testWidgets('Animates icon size on first build', (WidgetTester tester) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
const example.TweenAnimationBuilderExampleApp(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// The animation duration defined in the example app.
|
||||||
|
const Duration animationDuration = Duration(seconds: 1);
|
||||||
|
|
||||||
|
const double beginSize = 0.0;
|
||||||
|
const double endSize = 24.0;
|
||||||
|
|
||||||
|
final Finder iconButtonFinder = find.byType(IconButton);
|
||||||
|
|
||||||
|
IconButton iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(iconButton.iconSize, equals(beginSize));
|
||||||
|
|
||||||
|
// Advance animation to the middle.
|
||||||
|
await tester.pump(animationDuration ~/ 2);
|
||||||
|
|
||||||
|
iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(
|
||||||
|
iconButton.iconSize,
|
||||||
|
equals(lerpDouble(beginSize, endSize, 0.5)),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Advance animation to the end.
|
||||||
|
await tester.pump(animationDuration ~/ 2);
|
||||||
|
|
||||||
|
iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(iconButton.iconSize, equals(endSize));
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('Animates icon size on IconButton tap', (WidgetTester tester) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
const example.TweenAnimationBuilderExampleApp(),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// The animation duration defined in the example app.
|
||||||
|
const Duration animationDuration = Duration(seconds: 1);
|
||||||
|
|
||||||
|
const double beginSize = 24.0;
|
||||||
|
const double endSize = 48.0;
|
||||||
|
|
||||||
|
final Finder iconButtonFinder = find.byType(IconButton);
|
||||||
|
|
||||||
|
IconButton iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(iconButton.iconSize, equals(beginSize));
|
||||||
|
|
||||||
|
// Tap on the IconButton to start the forward animation.
|
||||||
|
await tester.tap(iconButtonFinder);
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(iconButton.iconSize, equals(beginSize));
|
||||||
|
|
||||||
|
// Advance animation to the middle.
|
||||||
|
await tester.pump(animationDuration ~/ 2);
|
||||||
|
|
||||||
|
iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(
|
||||||
|
iconButton.iconSize,
|
||||||
|
equals(lerpDouble(beginSize, endSize, 0.5)),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Advance animation to the end.
|
||||||
|
await tester.pump(animationDuration ~/ 2);
|
||||||
|
|
||||||
|
iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(iconButton.iconSize, equals(endSize));
|
||||||
|
|
||||||
|
// Tap on the IconButton to start the reverse animation.
|
||||||
|
await tester.tap(iconButtonFinder);
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(iconButton.iconSize, equals(endSize));
|
||||||
|
|
||||||
|
// Advance animation to the middle.
|
||||||
|
await tester.pump(animationDuration ~/ 2);
|
||||||
|
|
||||||
|
iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(
|
||||||
|
iconButton.iconSize,
|
||||||
|
equals(lerpDouble(endSize, beginSize, 0.5)),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Advance animation to the end.
|
||||||
|
await tester.pump(animationDuration ~/ 2);
|
||||||
|
|
||||||
|
iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(iconButton.iconSize, equals(beginSize));
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('Animation target can be updated during the animation', (WidgetTester tester) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
const example.TweenAnimationBuilderExampleApp(),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// The animation duration defined in the example app.
|
||||||
|
const Duration animationDuration = Duration(seconds: 1);
|
||||||
|
|
||||||
|
const double beginSize = 24.0;
|
||||||
|
const double endSize = 48.0;
|
||||||
|
final double middleSize = lerpDouble(beginSize, endSize, 0.5)!;
|
||||||
|
|
||||||
|
final Finder iconButtonFinder = find.byType(IconButton);
|
||||||
|
|
||||||
|
IconButton iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(iconButton.iconSize, equals(beginSize));
|
||||||
|
|
||||||
|
// Tap on the IconButton to start the forward animation.
|
||||||
|
await tester.tap(iconButtonFinder);
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(iconButton.iconSize, equals(beginSize));
|
||||||
|
|
||||||
|
// Advance animation to the middle.
|
||||||
|
await tester.pump(animationDuration ~/ 2);
|
||||||
|
|
||||||
|
iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(iconButton.iconSize, equals(middleSize));
|
||||||
|
|
||||||
|
// Tap on the IconButton to start the backward animation.
|
||||||
|
await tester.tap(iconButtonFinder);
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(iconButton.iconSize, equals(middleSize));
|
||||||
|
|
||||||
|
// Advance animation to the middle.
|
||||||
|
await tester.pump(animationDuration ~/ 2);
|
||||||
|
|
||||||
|
iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(
|
||||||
|
iconButton.iconSize,
|
||||||
|
equals(lerpDouble(middleSize, beginSize, 0.5)),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Advance animation to the end.
|
||||||
|
await tester.pump(animationDuration ~/ 2);
|
||||||
|
|
||||||
|
iconButton = tester.widget(iconButtonFinder);
|
||||||
|
expect(iconButton.iconSize, equals(beginSize));
|
||||||
|
});
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user