composited_transform_test.dart 5.99 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10
// 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() {
  testWidgets('Composited transforms - only offsets', (WidgetTester tester) async {
11 12
    final LayerLink link = LayerLink();
    final GlobalKey key = GlobalKey();
13
    await tester.pumpWidget(
14
      Directionality(
15
        textDirection: TextDirection.ltr,
16
        child: Stack(
17
          children: <Widget>[
18
            Positioned(
19 20
              left: 123.0,
              top: 456.0,
21
              child: CompositedTransformTarget(
22
                link: link,
23
                child: const SizedBox(height: 10.0, width: 10.0),
24
              ),
25
            ),
26
            Positioned(
27 28
              left: 787.0,
              top: 343.0,
29
              child: CompositedTransformFollower(
30
                link: link,
31
                child: Container(key: key, height: 10.0, width: 10.0),
32
              ),
33
            ),
34 35
          ],
        ),
36 37
      ),
    );
38
    final RenderBox box = key.currentContext.findRenderObject() as RenderBox;
39 40 41 42
    expect(box.localToGlobal(Offset.zero), const Offset(123.0, 456.0));
  });

  testWidgets('Composited transforms - with rotations', (WidgetTester tester) async {
43 44 45
    final LayerLink link = LayerLink();
    final GlobalKey key1 = GlobalKey();
    final GlobalKey key2 = GlobalKey();
46
    await tester.pumpWidget(
47
      Directionality(
48
        textDirection: TextDirection.ltr,
49
        child: Stack(
50
          children: <Widget>[
51
            Positioned(
52 53
              top: 123.0,
              left: 456.0,
54
              child: Transform.rotate(
55
                angle: 1.0, // radians
56
                child: CompositedTransformTarget(
57
                  link: link,
58
                  child: Container(key: key1, height: 10.0, width: 10.0),
59
                ),
60 61
              ),
            ),
62
            Positioned(
63 64
              top: 787.0,
              left: 343.0,
65
              child: Transform.rotate(
66
                angle: -0.3, // radians
67
                child: CompositedTransformFollower(
68
                  link: link,
69
                  child: Container(key: key2, height: 10.0, width: 10.0),
70
                ),
71 72
              ),
            ),
73 74
          ],
        ),
75 76
      ),
    );
77 78
    final RenderBox box1 = key1.currentContext.findRenderObject() as RenderBox;
    final RenderBox box2 = key2.currentContext.findRenderObject() as RenderBox;
79 80 81 82 83 84 85
    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 {
86 87 88
    final LayerLink link = LayerLink();
    final GlobalKey key1 = GlobalKey();
    final GlobalKey key2 = GlobalKey();
89
    await tester.pumpWidget(
90
      Directionality(
91
        textDirection: TextDirection.ltr,
92
        child: Stack(
93
          children: <Widget>[
94
            Positioned(
95 96
              top: 123.0,
              left: 456.0,
97
              child: Transform.rotate(
98
                angle: 1.0, // radians
99
                child: CompositedTransformTarget(
100
                  link: link,
101
                  child: Container(key: key1, height: 10.0, width: 10.0),
102
                ),
103 104
              ),
            ),
105
            Positioned(
106 107
              top: 787.0,
              left: 343.0,
108
              child: Transform.rotate(
109
                angle: -0.3, // radians
110
                child: Padding(
111
                  padding: const EdgeInsets.all(20.0),
112 113 114 115 116
                  child: CompositedTransformFollower(
                    link: LayerLink(),
                    child: Transform(
                      transform: Matrix4.skew(0.9, 1.1),
                      child: Padding(
117
                        padding: const EdgeInsets.all(20.0),
118
                        child: CompositedTransformFollower(
119
                          link: link,
120
                          child: Container(key: key2, height: 10.0, width: 10.0),
121
                        ),
122 123 124 125 126 127
                      ),
                    ),
                  ),
                ),
              ),
            ),
128 129
          ],
        ),
130 131
      ),
    );
132 133
    final RenderBox box1 = key1.currentContext.findRenderObject() as RenderBox;
    final RenderBox box2 = key2.currentContext.findRenderObject() as RenderBox;
134 135 136 137 138 139 140
    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 {
141 142 143 144
    final LayerLink link = LayerLink();
    final GlobalKey key1 = GlobalKey();
    final GlobalKey key2 = GlobalKey();
    final GlobalKey key3 = GlobalKey();
145 146
    bool _tapped = false;
    await tester.pumpWidget(
147
      Directionality(
148
        textDirection: TextDirection.ltr,
149
        child: Stack(
150
          children: <Widget>[
151
            Positioned(
152 153
              left: 123.0,
              top: 456.0,
154
              child: CompositedTransformTarget(
155
                link: link,
156
                child: Container(key: key1, height: 10.0, width: 10.0),
157
              ),
158
            ),
159
            CompositedTransformFollower(
160
              link: link,
161
              child: GestureDetector(
162 163 164
                key: key2,
                behavior: HitTestBehavior.opaque,
                onTap: () { _tapped = true; },
165
                child: Container(key: key3, height: 10.0, width: 10.0),
166
              ),
167
            ),
168 169
          ],
        ),
170 171
      ),
    );
172
    final RenderBox box2 = key2.currentContext.findRenderObject() as RenderBox;
173 174 175 176 177 178
    expect(box2.size, const Size(10.0, 10.0));
    expect(_tapped, isFalse);
    await tester.tap(find.byKey(key1));
    expect(_tapped, isTrue);
  });
}