From 6067d8f2195d303fca946ae5400c05cc82f2e2e7 Mon Sep 17 00:00:00 2001 From: Kostia Sokolovskyi Date: Mon, 20 May 2024 12:00:40 +0200 Subject: [PATCH] Add test for fade_transition.0.dart API example. (#148178) This PR contributes to https://github.com/flutter/flutter/issues/130459 ### Description - Adds disposal of the `CurvedAnimation` in `examples/api/test/widgets/transitions/fade_transition.0.dart` - Adds tests for `examples/api/lib/widgets/transitions/fade_transition.0.dart` --- dev/bots/check_code_samples.dart | 1 - .../transitions/fade_transition.0.dart | 48 +++++++++-- .../transitions/fade_transition.0_test.dart | 83 +++++++++++++++++++ 3 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 examples/api/test/widgets/transitions/fade_transition.0_test.dart diff --git a/dev/bots/check_code_samples.dart b/dev/bots/check_code_samples.dart index e9049cf06b6c..fd720dfc35fd 100644 --- a/dev/bots/check_code_samples.dart +++ b/dev/bots/check_code_samples.dart @@ -404,7 +404,6 @@ final Set _knownMissingTests = { 'examples/api/test/widgets/transitions/positioned_transition.0_test.dart', 'examples/api/test/widgets/transitions/sliver_fade_transition.0_test.dart', 'examples/api/test/widgets/transitions/align_transition.0_test.dart', - 'examples/api/test/widgets/transitions/fade_transition.0_test.dart', 'examples/api/test/widgets/transitions/animated_builder.0_test.dart', 'examples/api/test/widgets/transitions/rotation_transition.0_test.dart', 'examples/api/test/widgets/transitions/animated_widget.0_test.dart', diff --git a/examples/api/lib/widgets/transitions/fade_transition.0.dart b/examples/api/lib/widgets/transitions/fade_transition.0.dart index a6dd32464129..b4db29b067c1 100644 --- a/examples/api/lib/widgets/transitions/fade_transition.0.dart +++ b/examples/api/lib/widgets/transitions/fade_transition.0.dart @@ -11,16 +11,30 @@ void main() => runApp(const FadeTransitionExampleApp()); class FadeTransitionExampleApp extends StatelessWidget { const FadeTransitionExampleApp({super.key}); + static const Duration duration = Duration(seconds: 2); + static const Curve curve = Curves.easeIn; + @override Widget build(BuildContext context) { return const MaterialApp( - home: FadeTransitionExample(), + home: FadeTransitionExample( + duration: duration, + curve: curve, + ), ); } } class FadeTransitionExample extends StatefulWidget { - const FadeTransitionExample({super.key}); + const FadeTransitionExample({ + required this.duration, + required this.curve, + super.key, + }); + + final Duration duration; + + final Curve curve; @override State createState() => _FadeTransitionExampleState(); @@ -28,18 +42,35 @@ class FadeTransitionExample extends StatefulWidget { /// [AnimationController]s can be created with `vsync: this` because of /// [TickerProviderStateMixin]. -class _FadeTransitionExampleState extends State with TickerProviderStateMixin { +class _FadeTransitionExampleState extends State + with TickerProviderStateMixin { late final AnimationController _controller = AnimationController( - duration: const Duration(seconds: 2), + duration: widget.duration, vsync: this, )..repeat(reverse: true); - late final Animation _animation = CurvedAnimation( + late final CurvedAnimation _animation = CurvedAnimation( parent: _controller, - curve: Curves.easeIn, + curve: widget.curve, ); + @override + void didUpdateWidget(FadeTransitionExample oldWidget) { + super.didUpdateWidget(oldWidget); + + if (oldWidget.duration != widget.duration) { + _controller + ..duration = widget.duration + ..repeat(reverse: true); + } + + if (oldWidget.curve != widget.curve) { + _animation.curve = widget.curve; + } + } + @override void dispose() { + _animation.dispose(); _controller.dispose(); super.dispose(); } @@ -50,7 +81,10 @@ class _FadeTransitionExampleState extends State with Tick color: Colors.white, child: FadeTransition( opacity: _animation, - child: const Padding(padding: EdgeInsets.all(8), child: FlutterLogo()), + child: const Padding( + padding: EdgeInsets.all(8), + child: FlutterLogo(), + ), ), ); } diff --git a/examples/api/test/widgets/transitions/fade_transition.0_test.dart b/examples/api/test/widgets/transitions/fade_transition.0_test.dart new file mode 100644 index 000000000000..1bed187a93fe --- /dev/null +++ b/examples/api/test/widgets/transitions/fade_transition.0_test.dart @@ -0,0 +1,83 @@ +// 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/transitions/fade_transition.0.dart' + as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Shows FlutterLogo inside a FadeTransition', (WidgetTester tester) async { + await tester.pumpWidget( + const example.FadeTransitionExampleApp(), + ); + + expect( + find.ancestor( + of: find.byType(FlutterLogo), + matching: find.byType(FadeTransition), + ), + findsOneWidget, + ); + }); + + testWidgets('FadeTransition animates', (WidgetTester tester) async { + await tester.pumpWidget( + const example.FadeTransitionExampleApp(), + ); + + final Finder fadeTransitionFinder = find.ancestor( + of: find.byType(FlutterLogo), + matching: find.byType(FadeTransition), + ); + + const double beginOpacity = 0.0; + const double endOpacity = 1.0; + + FadeTransition fadeTransition = tester.widget(fadeTransitionFinder); + expect(fadeTransition.opacity.value, equals(beginOpacity)); + + // Advance animation to the middle. + await tester.pump( + example.FadeTransitionExampleApp.duration ~/ 2, + ); + + final double t = example.FadeTransitionExampleApp.curve.transform(0.5); + + fadeTransition = tester.widget(fadeTransitionFinder); + expect( + fadeTransition.opacity.value, + equals(lerpDouble(beginOpacity, endOpacity, t)), + ); + + // Advance animation to the end. + await tester.pump( + example.FadeTransitionExampleApp.duration ~/ 2, + ); + + fadeTransition = tester.widget(fadeTransitionFinder); + expect(fadeTransition.opacity.value, equals(endOpacity)); + + // Advance animation to the middle. + await tester.pump( + example.FadeTransitionExampleApp.duration ~/ 2, + ); + + fadeTransition = tester.widget(fadeTransitionFinder); + expect( + fadeTransition.opacity.value, + equals(lerpDouble(beginOpacity, endOpacity, t)), + ); + + // Advance animation to the end. + await tester.pump( + example.FadeTransitionExampleApp.duration ~/ 2, + ); + + fadeTransition = tester.widget(fadeTransitionFinder); + expect(fadeTransition.opacity.value, equals(beginOpacity)); + }); +}