animated_positioned_test.dart 6.58 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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';
import 'package:test/test.dart';

void main() {
  test('AnimatedPositioned - basics', () {
    testWidgets((WidgetTester tester) {
      GlobalKey key = new GlobalKey();

      RenderBox box;

      tester.pumpWidget(
        new Stack(
19
          children: <Widget>[
Ian Hickson's avatar
Ian Hickson committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
            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(Point.origin)), equals(const Point(50.0 + 70.0 / 2.0, 30.0 + 110.0 / 2.0)));

      tester.pump(const Duration(seconds: 1));

      box = key.currentContext.findRenderObject();
      expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(50.0 + 70.0 / 2.0, 30.0 + 110.0 / 2.0)));

      tester.pumpWidget(
        new Stack(
42
          children: <Widget>[
Ian Hickson's avatar
Ian Hickson committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
            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(Point.origin)), equals(const Point(50.0 + 70.0 / 2.0, 30.0 + 110.0 / 2.0)));

      tester.pump(const Duration(seconds: 1));

      box = key.currentContext.findRenderObject();
      expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(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)));

      tester.pump(const Duration(seconds: 1));

      box = key.currentContext.findRenderObject();
      expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(37.0 + 59.0 / 2.0, 31.0 + 71.0 / 2.0)));

    });
  });

  test('AnimatedPositioned - interrupted animation', () {
    testWidgets((WidgetTester tester) {
      GlobalKey key = new GlobalKey();

      RenderBox box;

      tester.pumpWidget(
        new Stack(
80
          children: <Widget>[
Ian Hickson's avatar
Ian Hickson committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
            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(Point.origin)), equals(const Point(50.0, 50.0)));

      tester.pump(const Duration(seconds: 1));

      box = key.currentContext.findRenderObject();
      expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(50.0, 50.0)));

      tester.pumpWidget(
        new Stack(
103
          children: <Widget>[
Ian Hickson's avatar
Ian Hickson committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
            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(Point.origin)), equals(const Point(50.0, 50.0)));

      tester.pump(const Duration(seconds: 1));

      box = key.currentContext.findRenderObject();
      expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(100.0, 100.0)));

      tester.pumpWidget(
        new Stack(
126
          children: <Widget>[
Ian Hickson's avatar
Ian Hickson committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
            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(Point.origin)), equals(const Point(100.0, 100.0)));

      tester.pump(const Duration(seconds: 1));

      box = key.currentContext.findRenderObject();
      expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(150.0, 150.0)));

      tester.pump(const Duration(seconds: 1));

      box = key.currentContext.findRenderObject();
      expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(200.0, 200.0)));

    });
  });

  test('AnimatedPositioned - switching variables', () {
    testWidgets((WidgetTester tester) {
      GlobalKey key = new GlobalKey();

      RenderBox box;

      tester.pumpWidget(
        new Stack(
163
          children: <Widget>[
Ian Hickson's avatar
Ian Hickson committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
            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(Point.origin)), equals(const Point(50.0, 50.0)));

      tester.pump(const Duration(seconds: 1));

      box = key.currentContext.findRenderObject();
      expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(50.0, 50.0)));

      tester.pumpWidget(
        new Stack(
186
          children: <Widget>[
Ian Hickson's avatar
Ian Hickson committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
            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(Point.origin)), equals(const Point(350.0, 50.0)));

      tester.pump(const Duration(seconds: 1));

      box = key.currentContext.findRenderObject();
      expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(350.0, 100.0)));

      tester.pump(const Duration(seconds: 1));

      box = key.currentContext.findRenderObject();
      expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(350.0, 150.0)));

    });
  });

}