positioned_test.dart 4.22 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
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
11 12

void main() {
13
  testWidgetsWithLeakTracking('Positioned constructors', (WidgetTester tester) async {
14 15
    final Widget child = Container();
    final Positioned a = Positioned(
16 17 18 19 20 21 22 23 24 25 26 27
      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);
28
    final Positioned b = Positioned.fromRect(
Dan Field's avatar
Dan Field committed
29
      rect: const Rect.fromLTRB(
30 31 32 33 34 35 36 37 38 39 40 41 42
        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);
43
    final Positioned c = Positioned.fromRelativeRect(
44
      rect: const RelativeRect.fromLTRB(
45 46 47 48 49 50 51 52 53 54 55 56 57 58
        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);
  });
59

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

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

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

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

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