positioned_test.dart 2.5 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 11 12 13 14
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:test/test.dart';

void main() {

  test('Can animate position data', () {
    testWidgets((WidgetTester tester) {

15 16
      final RelativeRectTween rect = new RelativeRectTween(
        begin: new RelativeRect.fromRect(
17 18 19 20 21 22
          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)
23
        )
24
      );
25
      final AnimationController controller = new AnimationController(
26 27 28
        duration: const Duration(seconds: 10)
      );
      final List<Size> sizes = <Size>[];
29
      final List<Offset> positions = <Offset>[];
30 31 32 33 34 35
      final GlobalKey key = new GlobalKey();

      void recordMetrics() {
        RenderBox box = key.currentContext.findRenderObject();
        BoxParentData boxParentData = box.parentData;
        sizes.add(box.size);
36
        positions.add(boxParentData.offset);
37 38 39 40 41 42 43
      }

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

      expect(sizes, equals([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)]));
71
      expect(positions, equals([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)]));
72 73 74 75 76

    });
  });

}