positioned_test.dart 4.11 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie committed
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:async';

7 8
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
9
import 'package:flutter_test/flutter_test.dart';
10 11

void main() {
12
  testWidgets('Positioned constructors', (WidgetTester tester) async {
13 14
    final Widget child = Container();
    final Positioned a = Positioned(
15 16 17 18 19 20 21 22 23 24 25 26
      left: 101.0,
      right: 201.0,
      top: 301.0,
      bottom: 401.0,
      child: child,
    );
    expect(a.left, 101.0);
    expect(a.right, 201.0);
    expect(a.top, 301.0);
    expect(a.bottom, 401.0);
    expect(a.width, null);
    expect(a.height, null);
27
    final Positioned b = Positioned.fromRect(
Dan Field's avatar
Dan Field committed
28
      rect: const Rect.fromLTRB(
29 30 31 32 33 34 35 36 37 38 39 40 41
        102.0,
        302.0,
        202.0,
        502.0,
      ),
      child: child,
    );
    expect(b.left, 102.0);
    expect(b.right, null);
    expect(b.top, 302.0);
    expect(b.bottom, null);
    expect(b.width, 100.0);
    expect(b.height, 200.0);
42
    final Positioned c = Positioned.fromRelativeRect(
43
      rect: const RelativeRect.fromLTRB(
44 45 46 47 48 49 50 51 52 53 54 55 56 57
        103.0,
        303.0,
        203.0,
        403.0,
      ),
      child: child,
    );
    expect(c.left, 103.0);
    expect(c.right, 203.0);
    expect(c.top, 303.0);
    expect(c.bottom, 403.0);
    expect(c.width, null);
    expect(c.height, null);
  });
58

59
  testWidgets('Can animate position data', (WidgetTester tester) async {
60 61
    final RelativeRectTween rect = RelativeRectTween(
      begin: RelativeRect.fromRect(
Dan Field's avatar
Dan Field committed
62 63
        const Rect.fromLTRB(10.0, 20.0, 20.0, 30.0),
        const Rect.fromLTRB(0.0, 10.0, 100.0, 110.0),
64
      ),
65
      end: RelativeRect.fromRect(
Dan Field's avatar
Dan Field committed
66 67
        const Rect.fromLTRB(80.0, 90.0, 90.0, 100.0),
        const Rect.fromLTRB(0.0, 10.0, 100.0, 110.0),
68
      ),
69
    );
70
    final AnimationController controller = AnimationController(
71 72
      duration: const Duration(seconds: 10),
      vsync: tester,
73 74 75
    );
    final List<Size> sizes = <Size>[];
    final List<Offset> positions = <Offset>[];
76
    final GlobalKey key = GlobalKey();
77

78
    void recordMetrics() {
79 80
      final RenderBox box = key.currentContext!.findRenderObject()! as RenderBox;
      final BoxParentData boxParentData = box.parentData! as BoxParentData;
81 82 83
      sizes.add(box.size);
      positions.add(boxParentData.offset);
    }
84

85
    await tester.pumpWidget(
86
      Directionality(
87
        textDirection: TextDirection.ltr,
88
        child: Center(
89
          child: SizedBox(
90 91
            height: 100.0,
            width: 100.0,
92
            child: Stack(
93
              children: <Widget>[
94
                PositionedTransition(
95
                  rect: rect.animate(controller),
96
                  child: Container(
97 98 99 100 101 102 103 104
                    key: key,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
105 106
    ); // t=0
    recordMetrics();
107
    final Completer<void> completer = Completer<void>();
108 109
    controller.forward().whenComplete(completer.complete);
    expect(completer.isCompleted, isFalse);
110
    await tester.pump(); // t=0 again
111
    expect(completer.isCompleted, isFalse);
112
    recordMetrics();
113
    await tester.pump(const Duration(seconds: 1)); // t=1
114
    expect(completer.isCompleted, isFalse);
115
    recordMetrics();
116
    await tester.pump(const Duration(seconds: 1)); // t=2
117
    expect(completer.isCompleted, isFalse);
118
    recordMetrics();
119
    await tester.pump(const Duration(seconds: 3)); // t=5
120
    expect(completer.isCompleted, isFalse);
121
    recordMetrics();
122
    await tester.pump(const Duration(seconds: 5)); // t=10
123
    expect(completer.isCompleted, isFalse);
124
    recordMetrics();
125

126 127
    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)]));
128

129
    controller.stop(canceled: false);
130 131
    await tester.pump();
    expect(completer.isCompleted, isTrue);
132 133
  });
}