composited_transform_test.dart 6 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
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
// @dart = 2.8

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

void main() {
  testWidgets('Composited transforms - only offsets', (WidgetTester tester) async {
13 14
    final LayerLink link = LayerLink();
    final GlobalKey key = GlobalKey();
15
    await tester.pumpWidget(
16
      Directionality(
17
        textDirection: TextDirection.ltr,
18
        child: Stack(
19
          children: <Widget>[
20
            Positioned(
21 22
              left: 123.0,
              top: 456.0,
23
              child: CompositedTransformTarget(
24
                link: link,
25
                child: const SizedBox(height: 10.0, width: 10.0),
26
              ),
27
            ),
28
            Positioned(
29 30
              left: 787.0,
              top: 343.0,
31
              child: CompositedTransformFollower(
32
                link: link,
33
                child: Container(key: key, height: 10.0, width: 10.0),
34
              ),
35
            ),
36 37
          ],
        ),
38 39
      ),
    );
40
    final RenderBox box = key.currentContext.findRenderObject() as RenderBox;
41 42 43 44
    expect(box.localToGlobal(Offset.zero), const Offset(123.0, 456.0));
  });

  testWidgets('Composited transforms - with rotations', (WidgetTester tester) async {
45 46 47
    final LayerLink link = LayerLink();
    final GlobalKey key1 = GlobalKey();
    final GlobalKey key2 = GlobalKey();
48
    await tester.pumpWidget(
49
      Directionality(
50
        textDirection: TextDirection.ltr,
51
        child: Stack(
52
          children: <Widget>[
53
            Positioned(
54 55
              top: 123.0,
              left: 456.0,
56
              child: Transform.rotate(
57
                angle: 1.0, // radians
58
                child: CompositedTransformTarget(
59
                  link: link,
60
                  child: Container(key: key1, height: 10.0, width: 10.0),
61
                ),
62 63
              ),
            ),
64
            Positioned(
65 66
              top: 787.0,
              left: 343.0,
67
              child: Transform.rotate(
68
                angle: -0.3, // radians
69
                child: CompositedTransformFollower(
70
                  link: link,
71
                  child: Container(key: key2, height: 10.0, width: 10.0),
72
                ),
73 74
              ),
            ),
75 76
          ],
        ),
77 78
      ),
    );
79 80
    final RenderBox box1 = key1.currentContext.findRenderObject() as RenderBox;
    final RenderBox box2 = key2.currentContext.findRenderObject() as RenderBox;
81 82 83 84 85 86 87
    final Offset position1 = box1.localToGlobal(Offset.zero);
    final Offset position2 = box2.localToGlobal(Offset.zero);
    expect(position1.dx, moreOrLessEquals(position2.dx));
    expect(position1.dy, moreOrLessEquals(position2.dy));
  });

  testWidgets('Composited transforms - nested', (WidgetTester tester) async {
88 89 90
    final LayerLink link = LayerLink();
    final GlobalKey key1 = GlobalKey();
    final GlobalKey key2 = GlobalKey();
91
    await tester.pumpWidget(
92
      Directionality(
93
        textDirection: TextDirection.ltr,
94
        child: Stack(
95
          children: <Widget>[
96
            Positioned(
97 98
              top: 123.0,
              left: 456.0,
99
              child: Transform.rotate(
100
                angle: 1.0, // radians
101
                child: CompositedTransformTarget(
102
                  link: link,
103
                  child: Container(key: key1, height: 10.0, width: 10.0),
104
                ),
105 106
              ),
            ),
107
            Positioned(
108 109
              top: 787.0,
              left: 343.0,
110
              child: Transform.rotate(
111
                angle: -0.3, // radians
112
                child: Padding(
113
                  padding: const EdgeInsets.all(20.0),
114 115 116 117 118
                  child: CompositedTransformFollower(
                    link: LayerLink(),
                    child: Transform(
                      transform: Matrix4.skew(0.9, 1.1),
                      child: Padding(
119
                        padding: const EdgeInsets.all(20.0),
120
                        child: CompositedTransformFollower(
121
                          link: link,
122
                          child: Container(key: key2, height: 10.0, width: 10.0),
123
                        ),
124 125 126 127 128 129
                      ),
                    ),
                  ),
                ),
              ),
            ),
130 131
          ],
        ),
132 133
      ),
    );
134 135
    final RenderBox box1 = key1.currentContext.findRenderObject() as RenderBox;
    final RenderBox box2 = key2.currentContext.findRenderObject() as RenderBox;
136 137 138 139 140 141 142
    final Offset position1 = box1.localToGlobal(Offset.zero);
    final Offset position2 = box2.localToGlobal(Offset.zero);
    expect(position1.dx, moreOrLessEquals(position2.dx));
    expect(position1.dy, moreOrLessEquals(position2.dy));
  });

  testWidgets('Composited transforms - hit testing', (WidgetTester tester) async {
143 144 145 146
    final LayerLink link = LayerLink();
    final GlobalKey key1 = GlobalKey();
    final GlobalKey key2 = GlobalKey();
    final GlobalKey key3 = GlobalKey();
147 148
    bool _tapped = false;
    await tester.pumpWidget(
149
      Directionality(
150
        textDirection: TextDirection.ltr,
151
        child: Stack(
152
          children: <Widget>[
153
            Positioned(
154 155
              left: 123.0,
              top: 456.0,
156
              child: CompositedTransformTarget(
157
                link: link,
158
                child: Container(key: key1, height: 10.0, width: 10.0),
159
              ),
160
            ),
161
            CompositedTransformFollower(
162
              link: link,
163
              child: GestureDetector(
164 165 166
                key: key2,
                behavior: HitTestBehavior.opaque,
                onTap: () { _tapped = true; },
167
                child: Container(key: key3, height: 10.0, width: 10.0),
168
              ),
169
            ),
170 171
          ],
        ),
172 173
      ),
    );
174
    final RenderBox box2 = key2.currentContext.findRenderObject() as RenderBox;
175 176 177 178 179 180
    expect(box2.size, const Size(10.0, 10.0));
    expect(_tapped, isFalse);
    await tester.tap(find.byKey(key1));
    expect(_tapped, isTrue);
  });
}