animated_positioned_test.dart 6.3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5 6 7 8 9
// 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() {
10
  testWidgets('AnimatedPositioned - basics', (WidgetTester tester) async {
11 12 13 14
    GlobalKey key = new GlobalKey();

    RenderBox box;

15
    await tester.pumpWidget(
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
      new Stack(
        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(Point.origin)), equals(const Point(50.0 + 70.0 / 2.0, 30.0 + 110.0 / 2.0)));

33
    await tester.pump(const Duration(seconds: 1));
34 35 36 37

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

38
    await tester.pumpWidget(
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
      new Stack(
        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(Point.origin)), equals(const Point(50.0 + 70.0 / 2.0, 30.0 + 110.0 / 2.0)));

56
    await tester.pump(const Duration(seconds: 1));
57 58 59 60 61

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

62
    await tester.pump(const Duration(seconds: 1));
63 64 65

    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)));
Ian Hickson's avatar
Ian Hickson committed
66 67
  });

68
  testWidgets('AnimatedPositioned - interrupted animation', (WidgetTester tester) async {
69 70 71 72
    GlobalKey key = new GlobalKey();

    RenderBox box;

73
    await tester.pumpWidget(
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
      new Stack(
        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(Point.origin)), equals(const Point(50.0, 50.0)));

91
    await tester.pump(const Duration(seconds: 1));
92 93 94 95

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

96
    await tester.pumpWidget(
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
      new Stack(
        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(Point.origin)), equals(const Point(50.0, 50.0)));

114
    await tester.pump(const Duration(seconds: 1));
115 116 117 118

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

119
    await tester.pumpWidget(
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
      new Stack(
        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(Point.origin)), equals(const Point(100.0, 100.0)));

137
    await tester.pump(const Duration(seconds: 1));
138 139 140 141

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

142
    await tester.pump(const Duration(seconds: 1));
143 144 145

    box = key.currentContext.findRenderObject();
    expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(200.0, 200.0)));
Ian Hickson's avatar
Ian Hickson committed
146 147
  });

148
  testWidgets('AnimatedPositioned - switching variables', (WidgetTester tester) async {
149 150 151 152
    GlobalKey key = new GlobalKey();

    RenderBox box;

153
    await tester.pumpWidget(
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
      new Stack(
        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(Point.origin)), equals(const Point(50.0, 50.0)));

171
    await tester.pump(const Duration(seconds: 1));
172 173 174 175

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

176
    await tester.pumpWidget(
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
      new Stack(
        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(Point.origin)), equals(const Point(350.0, 50.0)));

194
    await tester.pump(const Duration(seconds: 1));
195 196 197 198

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

199
    await tester.pump(const Duration(seconds: 1));
200 201 202

    box = key.currentContext.findRenderObject();
    expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(350.0, 150.0)));
Ian Hickson's avatar
Ian Hickson committed
203 204 205
  });

}