transform_test.dart 5.62 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// 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.

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6
import 'package:flutter/rendering.dart';
7
import 'package:flutter/widgets.dart';
8
import 'package:vector_math/vector_math_64.dart';
9 10

void main() {
11
  testWidgets('Transform origin', (WidgetTester tester) async {
12
    bool didReceiveTap = false;
13
    await tester.pumpWidget(
14 15 16 17 18 19 20 21
      new Stack(
        children: <Widget>[
          new Positioned(
            top: 100.0,
            left: 100.0,
            child: new Container(
              width: 100.0,
              height: 100.0,
22 23
              decoration: const BoxDecoration(
                backgroundColor: const Color(0xFF0000FF),
24 25
              ),
            ),
26 27 28 29 30 31 32 33
          ),
          new Positioned(
            top: 100.0,
            left: 100.0,
            child: new Container(
              width: 100.0,
              height: 100.0,
              child: new Transform(
34
                transform: new Matrix4.diagonal3Values(0.5, 0.5, 1.0),
35
                origin: const Offset(100.0, 50.0),
36 37 38 39 40
                child: new GestureDetector(
                  onTap: () {
                    didReceiveTap = true;
                  },
                  child: new Container(
41 42
                    decoration: const BoxDecoration(
                      backgroundColor: const Color(0xFF00FFFF),
43 44 45 46 47 48 49 50
                    ),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
51
    );
52

53
    expect(didReceiveTap, isFalse);
54
    await tester.tapAt(const Point(110.0, 110.0));
55
    expect(didReceiveTap, isFalse);
56
    await tester.tapAt(const Point(190.0, 150.0));
57
    expect(didReceiveTap, isTrue);
58
  });
Hixie's avatar
Hixie committed
59

60
  testWidgets('Transform alignment', (WidgetTester tester) async {
61
    bool didReceiveTap = false;
62
    await tester.pumpWidget(
63 64 65 66 67 68 69 70
      new Stack(
        children: <Widget>[
          new Positioned(
            top: 100.0,
            left: 100.0,
            child: new Container(
              width: 100.0,
              height: 100.0,
71 72
              decoration: const BoxDecoration(
                backgroundColor: const Color(0xFF0000FF),
73 74
              ),
            ),
75 76 77 78 79 80 81 82
          ),
          new Positioned(
            top: 100.0,
            left: 100.0,
            child: new Container(
              width: 100.0,
              height: 100.0,
              child: new Transform(
83
                transform: new Matrix4.diagonal3Values(0.5, 0.5, 1.0),
84
                alignment: const FractionalOffset(1.0, 0.5),
85 86 87 88 89
                child: new GestureDetector(
                  onTap: () {
                    didReceiveTap = true;
                  },
                  child: new Container(
90 91
                    decoration: const BoxDecoration(
                      backgroundColor: const Color(0xFF00FFFF),
92 93 94 95 96 97 98 99
                    ),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
100
    );
Hixie's avatar
Hixie committed
101

102
    expect(didReceiveTap, isFalse);
103
    await tester.tapAt(const Point(110.0, 110.0));
104
    expect(didReceiveTap, isFalse);
105
    await tester.tapAt(const Point(190.0, 150.0));
106
    expect(didReceiveTap, isTrue);
Hixie's avatar
Hixie committed
107 108
  });

109
  testWidgets('Transform offset + alignment', (WidgetTester tester) async {
110
    bool didReceiveTap = false;
111 112 113 114 115 116 117 118
    await tester.pumpWidget(new Stack(
      children: <Widget>[
        new Positioned(
          top: 100.0,
          left: 100.0,
          child: new Container(
            width: 100.0,
            height: 100.0,
119 120
            decoration: const BoxDecoration(
              backgroundColor: const Color(0xFF0000FF),
121
            ),
122
          ),
123 124 125 126 127 128 129 130 131
        ),
        new Positioned(
          top: 100.0,
          left: 100.0,
          child: new Container(
            width: 100.0,
            height: 100.0,
            child: new Transform(
              transform: new Matrix4.diagonal3Values(0.5, 0.5, 1.0),
132 133
              origin: const Offset(100.0, 0.0),
              alignment: const FractionalOffset(0.0, 0.5),
134 135 136 137 138
              child: new GestureDetector(
                onTap: () {
                  didReceiveTap = true;
                },
                child: new Container(
139 140
                  decoration: const BoxDecoration(
                    backgroundColor: const Color(0xFF00FFFF),
141 142 143 144 145 146 147 148
                  ),
                ),
              ),
            ),
          ),
        ),
      ],
    ));
Hixie's avatar
Hixie committed
149

150
    expect(didReceiveTap, isFalse);
151
    await tester.tapAt(const Point(110.0, 110.0));
152
    expect(didReceiveTap, isFalse);
153
    await tester.tapAt(const Point(190.0, 150.0));
154
    expect(didReceiveTap, isTrue);
Hixie's avatar
Hixie committed
155
  });
156 157 158 159 160 161 162 163 164 165 166 167 168

  testWidgets('Composited transform offset', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Center(
        child: new SizedBox(
          width: 400.0,
          height: 300.0,
          child: new ClipRect(
            child: new Transform(
              transform: new Matrix4.diagonal3Values(0.5, 0.5, 1.0),
              child: new Opacity(
                opacity: 0.9,
                child: new Container(
169
                  decoration: const BoxDecoration(
170 171 172 173 174 175 176 177 178 179
                    backgroundColor: const Color(0xFF00FF00),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );

180
    final List<Layer> layers = tester.layers
181 182 183
      ..retainWhere((Layer layer) => layer is TransformLayer);
    expect(layers.length, 2);
    // The first transform is from the render view.
184 185
    final TransformLayer layer = layers[1];
    final Matrix4 transform = layer.transform;
186 187
    expect(transform.getTranslation(), equals(new Vector3(100.0, 75.0, 0.0)));
  });
188
}