[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dev/bots/check_code_samples.dart
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Mayyybe give this a more explicit name. It took me awhile to track down what t was when I saw it used below, though admittedly that's only here in the GitHub UI that it's a problem since I would have used jump to definition in my editor. Maybe curveHalf or something?

It could be argued that this styleguide rule applies or does not apply here: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#avoid-abbreviations

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Mayyybe give this a more explicit name. It took me awhile to track down what t was when I saw it used below, though admittedly that's only here in the GitHub UI that it's a problem since I would have used jump to definition in my editor. Maybe curveHalf or something?

It could be argued that this styleguide rule applies or does not apply here: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#avoid-abbreviations

t is a variable typically used to represent the animation value in calls to lerp. Since the animation in this case isn't using any particularly confusing arithmetic, I think it would fine to just leave it as is.

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);
},
);
}