positioned_test.dart 2.42 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// 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.

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6 7 8 9 10
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

void main() {

11
  testWidgets('Can animate position data', (WidgetTester tester) async {
12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    final RelativeRectTween rect = new RelativeRectTween(
      begin: new RelativeRect.fromRect(
        new Rect.fromLTRB(10.0, 20.0, 20.0, 30.0),
        new Rect.fromLTRB(0.0, 10.0, 100.0, 110.0)
      ),
      end: new RelativeRect.fromRect(
        new Rect.fromLTRB(80.0, 90.0, 90.0, 100.0),
        new Rect.fromLTRB(0.0, 10.0, 100.0, 110.0)
      )
    );
    final AnimationController controller = new AnimationController(
      duration: const Duration(seconds: 10)
    );
    final List<Size> sizes = <Size>[];
    final List<Offset> positions = <Offset>[];
    final GlobalKey key = new GlobalKey();
29

30 31 32 33 34 35
    void recordMetrics() {
      RenderBox box = key.currentContext.findRenderObject();
      BoxParentData boxParentData = box.parentData;
      sizes.add(box.size);
      positions.add(boxParentData.offset);
    }
36

37
    await tester.pumpWidget(
38 39 40 41 42 43 44 45 46 47
      new Center(
        child: new Container(
          height: 100.0,
          width: 100.0,
          child: new Stack(
            children: <Widget>[
              new PositionedTransition(
                rect: rect.animate(controller),
                child: new Container(
                  key: key
48
                )
49 50
              )
            ]
51 52
          )
        )
53 54 55 56
      )
    ); // t=0
    recordMetrics();
    controller.forward();
57
    await tester.pump(); // t=0 again
58
    recordMetrics();
59
    await tester.pump(const Duration(seconds: 1)); // t=1
60
    recordMetrics();
61
    await tester.pump(const Duration(seconds: 1)); // t=2
62
    recordMetrics();
63
    await tester.pump(const Duration(seconds: 3)); // t=5
64
    recordMetrics();
65
    await tester.pump(const Duration(seconds: 5)); // t=10
66
    recordMetrics();
67

68 69
    expect(sizes, equals(<Size>[const Size(10.0, 10.0), const Size(10.0, 10.0), const Size(10.0, 10.0), const Size(10.0, 10.0), const Size(10.0, 10.0), const Size(10.0, 10.0)]));
    expect(positions, equals(<Offset>[const Offset(10.0, 10.0), const Offset(10.0, 10.0), const Offset(17.0, 17.0), const Offset(24.0, 24.0), const Offset(45.0, 45.0), const Offset(80.0, 80.0)]));
70

71
    controller.stop();
72 73 74
  });

}