Skip to content

Commit

Permalink
Add test for fade_transition.0.dart API example. (#148178)
Browse files Browse the repository at this point in the history
This PR contributes to flutter/flutter#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`
  • Loading branch information
ksokolovskyi authored May 20, 2024
1 parent 538625a commit 6067d8f
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 8 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 @@ -404,7 +404,6 @@ final Set<String> _knownMissingTests = <String>{
'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',
Expand Down
48 changes: 41 additions & 7 deletions examples/api/lib/widgets/transitions/fade_transition.0.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,66 @@ 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<FadeTransitionExample> createState() => _FadeTransitionExampleState();
}

/// [AnimationController]s can be created with `vsync: this` because of
/// [TickerProviderStateMixin].
class _FadeTransitionExampleState extends State<FadeTransitionExample> with TickerProviderStateMixin {
class _FadeTransitionExampleState extends State<FadeTransitionExample>
with TickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
duration: const Duration(seconds: 2),
duration: widget.duration,
vsync: this,
)..repeat(reverse: true);
late final Animation<double> _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();
}
Expand All @@ -50,7 +81,10 @@ class _FadeTransitionExampleState extends State<FadeTransitionExample> 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(),
),
),
);
}
Expand Down
83 changes: 83 additions & 0 deletions examples/api/test/widgets/transitions/fade_transition.0_test.dart
Original file line number Diff line number Diff line change
@@ -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));
});
}

0 comments on commit 6067d8f

Please sign in to comment.