Skip to content

Commit

Permalink
Add tests for tween_animation_builder.0.dart API example. (#148902)
Browse files Browse the repository at this point in the history
This PR contributes to #130459

### Description
- Adds tests for `examples/api/lib/widgets/tween_animation_builder/tween_animation_builder.0.dart`
  • Loading branch information
ksokolovskyi committed May 30, 2024
1 parent 5933e99 commit 488fb09
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 7 deletions.
1 change: 0 additions & 1 deletion dev/bots/check_code_samples.dart
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ final Set<String> _knownMissingTests = <String>{
'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.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.0_test.dart',
'examples/api/test/widgets/restoration/restoration_mixin.0_test.dart',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class TweenAnimationBuilderExampleApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('TweenAnimationBuilder Sample')),
appBar: AppBar(
title: const Text('TweenAnimationBuilder Sample'),
),
body: const Center(
child: TweenAnimationBuilderExample(),
),
Expand All @@ -28,16 +30,18 @@ class TweenAnimationBuilderExample extends StatefulWidget {
const TweenAnimationBuilderExample({super.key});

@override
State<TweenAnimationBuilderExample> createState() => _TweenAnimationBuilderExampleState();
State<TweenAnimationBuilderExample> createState() =>
_TweenAnimationBuilderExampleState();
}

class _TweenAnimationBuilderExampleState extends State<TweenAnimationBuilderExample> {
double targetValue = 24.0;
class _TweenAnimationBuilderExampleState
extends State<TweenAnimationBuilderExample> {
double _targetValue = 24.0;

@override
Widget build(BuildContext context) {
return TweenAnimationBuilder<double>(
tween: Tween<double>(begin: 0, end: targetValue),
tween: Tween<double>(begin: 0, end: _targetValue),
duration: const Duration(seconds: 1),
builder: (BuildContext context, double size, Widget? child) {
return IconButton(
Expand All @@ -46,7 +50,7 @@ class _TweenAnimationBuilderExampleState extends State<TweenAnimationBuilderExam
icon: child!,
onPressed: () {
setState(() {
targetValue = targetValue == 24.0 ? 48.0 : 24.0;
_targetValue = _targetValue == 24.0 ? 48.0 : 24.0;
});
},
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
});
}

0 comments on commit 488fb09

Please sign in to comment.