flutter/packages/flutter/test/widgets/animated_positioned_test.dart
Adam Barth c46347b6df Add RTL support to Stack (#11973)
Notice that the default alignment for Stack needs to know the
directionality.
2017-09-07 22:52:36 -07:00

251 lines
8.2 KiB
Dart

// Copyright 2015 The Chromium 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 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
void main() {
testWidgets('AnimatedPositioned.fromRect control test', (WidgetTester tester) async {
final AnimatedPositioned positioned = new AnimatedPositioned.fromRect(
rect: new Rect.fromLTWH(7.0, 5.0, 12.0, 16.0),
duration: const Duration(milliseconds: 200),
);
expect(positioned.left, equals(7.0));
expect(positioned.top, equals(5.0));
expect(positioned.width, equals(12.0));
expect(positioned.height, equals(16.0));
expect(positioned, hasOneLineDescription);
});
testWidgets('AnimatedPositioned - basics', (WidgetTester tester) async {
final GlobalKey key = new GlobalKey();
RenderBox box;
await tester.pumpWidget(
new Stack(
textDirection: TextDirection.ltr,
children: <Widget>[
new AnimatedPositioned(
child: new Container(key: key),
left: 50.0,
top: 30.0,
width: 70.0,
height: 110.0,
duration: const Duration(seconds: 2)
)
]
)
);
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(50.0 + 70.0 / 2.0, 30.0 + 110.0 / 2.0)));
await tester.pump(const Duration(seconds: 1));
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(50.0 + 70.0 / 2.0, 30.0 + 110.0 / 2.0)));
await tester.pumpWidget(
new Stack(
textDirection: TextDirection.ltr,
children: <Widget>[
new AnimatedPositioned(
child: new Container(key: key),
left: 37.0,
top: 31.0,
width: 59.0,
height: 71.0,
duration: const Duration(seconds: 2)
)
]
)
);
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(50.0 + 70.0 / 2.0, 30.0 + 110.0 / 2.0)));
await tester.pump(const Duration(seconds: 1));
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(50.0 - (50.0 - 37.0) / 2.0 + (70.0 - (70.0 - 59.0) / 2.0) / 2.0,
30.0 + (31.0 - 30.0) / 2.0 + (110.0 - (110.0 - 71.0) / 2.0) / 2.0)));
await tester.pump(const Duration(seconds: 1));
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(37.0 + 59.0 / 2.0, 31.0 + 71.0 / 2.0)));
expect(box, hasAGoodToStringDeep);
expect(
box.toStringDeep(),
equalsIgnoringHashCodes(
'RenderLimitedBox#00000\n'
' │ creator: LimitedBox ← Container-[GlobalKey#00000] ← Positioned ←\n'
' │ AnimatedPositioned ← Stack ← [root]\n'
' │ parentData: top=31.0; left=37.0; width=59.0; height=71.0;\n'
' │ offset=Offset(37.0, 31.0) (can use size)\n'
' │ constraints: BoxConstraints(w=59.0, h=71.0)\n'
' │ size: Size(59.0, 71.0)\n'
' │ maxWidth: 0.0\n'
' │ maxHeight: 0.0\n'
'\n'
' └─child: RenderConstrainedBox#00000\n'
' creator: ConstrainedBox ← LimitedBox ←\n'
' Container-[GlobalKey#00000] ← Positioned ← AnimatedPositioned ←\n'
' Stack ← [root]\n'
' parentData: <none> (can use size)\n'
' constraints: BoxConstraints(w=59.0, h=71.0)\n'
' size: Size(59.0, 71.0)\n'
' additionalConstraints: BoxConstraints(biggest)\n',
),
);
});
testWidgets('AnimatedPositioned - interrupted animation', (WidgetTester tester) async {
final GlobalKey key = new GlobalKey();
RenderBox box;
await tester.pumpWidget(
new Stack(
textDirection: TextDirection.ltr,
children: <Widget>[
new AnimatedPositioned(
child: new Container(key: key),
left: 0.0,
top: 0.0,
width: 100.0,
height: 100.0,
duration: const Duration(seconds: 2)
)
]
)
);
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(50.0, 50.0)));
await tester.pump(const Duration(seconds: 1));
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(50.0, 50.0)));
await tester.pumpWidget(
new Stack(
textDirection: TextDirection.ltr,
children: <Widget>[
new AnimatedPositioned(
child: new Container(key: key),
left: 100.0,
top: 100.0,
width: 100.0,
height: 100.0,
duration: const Duration(seconds: 2)
)
]
)
);
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(50.0, 50.0)));
await tester.pump(const Duration(seconds: 1));
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(100.0, 100.0)));
await tester.pumpWidget(
new Stack(
textDirection: TextDirection.ltr,
children: <Widget>[
new AnimatedPositioned(
child: new Container(key: key),
left: 150.0,
top: 150.0,
width: 100.0,
height: 100.0,
duration: const Duration(seconds: 2)
)
]
)
);
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(100.0, 100.0)));
await tester.pump(const Duration(seconds: 1));
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(150.0, 150.0)));
await tester.pump(const Duration(seconds: 1));
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(200.0, 200.0)));
});
testWidgets('AnimatedPositioned - switching variables', (WidgetTester tester) async {
final GlobalKey key = new GlobalKey();
RenderBox box;
await tester.pumpWidget(
new Stack(
textDirection: TextDirection.ltr,
children: <Widget>[
new AnimatedPositioned(
child: new Container(key: key),
left: 0.0,
top: 0.0,
width: 100.0,
height: 100.0,
duration: const Duration(seconds: 2)
)
]
)
);
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(50.0, 50.0)));
await tester.pump(const Duration(seconds: 1));
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(50.0, 50.0)));
await tester.pumpWidget(
new Stack(
textDirection: TextDirection.ltr,
children: <Widget>[
new AnimatedPositioned(
child: new Container(key: key),
left: 0.0,
top: 100.0,
right: 100.0, // 700.0 from the left
height: 100.0,
duration: const Duration(seconds: 2)
)
]
)
);
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(350.0, 50.0)));
await tester.pump(const Duration(seconds: 1));
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(350.0, 100.0)));
await tester.pump(const Duration(seconds: 1));
box = key.currentContext.findRenderObject();
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(350.0, 150.0)));
});
}