Add test for animated_fractionally_sized_box.0.dart API example. (#146721)

This PR contributes to https://github.com/flutter/flutter/issues/130459

### Description
- Adds `examples/api/test/widgets/implicit_animations/animated_fractionally_sized_box.0_test.dart` test
This commit is contained in:
Kostia Sokolovskyi 2024-05-01 18:03:12 +02:00 committed by GitHub
parent a1cb040511
commit 32ea4b3c46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 150 additions and 8 deletions

View File

@ -466,7 +466,6 @@ final Set<String> _knownMissingTests = <String>{
'examples/api/test/widgets/focus_scope/focus.0_test.dart',
'examples/api/test/widgets/focus_scope/focus.1_test.dart',
'examples/api/test/widgets/focus_scope/focus_scope.0_test.dart',
'examples/api/test/widgets/implicit_animations/animated_fractionally_sized_box.0_test.dart',
'examples/api/test/widgets/scroll_view/custom_scroll_view.1_test.dart',
'examples/api/test/widgets/inherited_notifier/inherited_notifier.0_test.dart',
'examples/api/test/animation/curves/curve2_d.0_test.dart',

View File

@ -11,25 +11,43 @@ void main() => runApp(const AnimatedFractionallySizedBoxExampleApp());
class AnimatedFractionallySizedBoxExampleApp extends StatelessWidget {
const AnimatedFractionallySizedBoxExampleApp({super.key});
static const Duration duration = Duration(seconds: 1);
static const Curve curve = Curves.fastOutSlowIn;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('AnimatedFractionallySizedBox Sample')),
body: const AnimatedFractionallySizedBoxExample(),
appBar: AppBar(
title: const Text('AnimatedFractionallySizedBox Sample'),
),
body: const AnimatedFractionallySizedBoxExample(
duration: duration,
curve: curve,
),
),
);
}
}
class AnimatedFractionallySizedBoxExample extends StatefulWidget {
const AnimatedFractionallySizedBoxExample({super.key});
const AnimatedFractionallySizedBoxExample({
required this.duration,
required this.curve,
super.key,
});
final Duration duration;
final Curve curve;
@override
State<AnimatedFractionallySizedBoxExample> createState() => _AnimatedFractionallySizedBoxExampleState();
State<AnimatedFractionallySizedBoxExample> createState() =>
_AnimatedFractionallySizedBoxExampleState();
}
class _AnimatedFractionallySizedBoxExampleState extends State<AnimatedFractionallySizedBoxExample> {
class _AnimatedFractionallySizedBoxExampleState
extends State<AnimatedFractionallySizedBoxExample> {
bool selected = false;
@override
@ -50,8 +68,8 @@ class _AnimatedFractionallySizedBoxExampleState extends State<AnimatedFractional
widthFactor: selected ? 0.25 : 0.75,
heightFactor: selected ? 0.75 : 0.25,
alignment: selected ? Alignment.topLeft : Alignment.bottomRight,
duration: const Duration(seconds: 1),
curve: Curves.fastOutSlowIn,
duration: widget.duration,
curve: widget.curve,
child: const ColoredBox(
color: Colors.blue,
child: FlutterLogo(size: 75),

View File

@ -0,0 +1,125 @@
// 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/implicit_animations/animated_fractionally_sized_box.0.dart'
as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets(
'AnimatedFractionallySizedBox animates on tap',
(WidgetTester tester) async {
await tester.pumpWidget(
const example.AnimatedFractionallySizedBoxExampleApp(),
);
final Finder fractionallySizedBoxFinder = find.descendant(
of: find.byType(AnimatedFractionallySizedBox),
matching: find.byType(FractionallySizedBox),
);
const double beginWidthFactor = 0.75;
const double endWidthFactor = 0.25;
const double beginHeightFactor = 0.25;
const double endHeightFactor = 0.75;
const Alignment beginAlignment = Alignment.bottomRight;
const Alignment endAlignment = Alignment.topLeft;
FractionallySizedBox fractionallySizedBox = tester.widget(
fractionallySizedBoxFinder,
);
expect(fractionallySizedBox.widthFactor, beginWidthFactor);
expect(fractionallySizedBox.heightFactor, beginHeightFactor);
expect(fractionallySizedBox.alignment, beginAlignment);
// Tap on the AnimatedFractionallySizedBoxExample to start the forward
// animation.
await tester.tap(
find.byType(example.AnimatedFractionallySizedBoxExample),
);
await tester.pump();
fractionallySizedBox = tester.widget(fractionallySizedBoxFinder);
expect(fractionallySizedBox.widthFactor, beginWidthFactor);
expect(fractionallySizedBox.heightFactor, beginHeightFactor);
expect(fractionallySizedBox.alignment, beginAlignment);
// Advance animation to the middle.
await tester.pump(
example.AnimatedFractionallySizedBoxExampleApp.duration ~/ 2,
);
final double t =
example.AnimatedFractionallySizedBoxExampleApp.curve.transform(0.5);
fractionallySizedBox = tester.widget(fractionallySizedBoxFinder);
expect(
fractionallySizedBox.widthFactor,
lerpDouble(beginWidthFactor, endWidthFactor, t),
);
expect(
fractionallySizedBox.heightFactor,
lerpDouble(beginHeightFactor, endHeightFactor, t),
);
expect(
fractionallySizedBox.alignment,
Alignment.lerp(beginAlignment, endAlignment, t),
);
// Advance animation to the end.
await tester.pump(
example.AnimatedFractionallySizedBoxExampleApp.duration ~/ 2,
);
fractionallySizedBox = tester.widget(fractionallySizedBoxFinder);
expect(fractionallySizedBox.widthFactor, endWidthFactor);
expect(fractionallySizedBox.heightFactor, endHeightFactor);
expect(fractionallySizedBox.alignment, endAlignment);
// Tap on the AnimatedFractionallySizedBoxExample again to start the
// reverse animation.
await tester.tap(
find.byType(example.AnimatedFractionallySizedBoxExample),
);
await tester.pump();
fractionallySizedBox = tester.widget(fractionallySizedBoxFinder);
expect(fractionallySizedBox.widthFactor, endWidthFactor);
expect(fractionallySizedBox.heightFactor, endHeightFactor);
expect(fractionallySizedBox.alignment, endAlignment);
// Advance animation to the middle.
await tester.pump(
example.AnimatedFractionallySizedBoxExampleApp.duration ~/ 2,
);
fractionallySizedBox = tester.widget(fractionallySizedBoxFinder);
expect(
fractionallySizedBox.widthFactor,
lerpDouble(endWidthFactor, beginWidthFactor, t),
);
expect(
fractionallySizedBox.heightFactor,
lerpDouble(endHeightFactor, beginHeightFactor, t),
);
expect(
fractionallySizedBox.alignment,
Alignment.lerp(endAlignment, beginAlignment, t),
);
// Advance animation to the end.
await tester.pump(
example.AnimatedFractionallySizedBoxExampleApp.duration ~/ 2,
);
fractionallySizedBox = tester.widget(fractionallySizedBoxFinder);
expect(fractionallySizedBox.widthFactor, beginWidthFactor);
expect(fractionallySizedBox.heightFactor, beginHeightFactor);
expect(fractionallySizedBox.alignment, beginAlignment);
},
);
}