composited_transform_test.dart 6.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright 2017 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.

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 {
    final LayerLink link = new LayerLink();
    final GlobalKey key = new GlobalKey();
    await tester.pumpWidget(
14 15 16 17 18 19 20 21 22 23 24
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Stack(
          children: <Widget>[
            new Positioned(
              left: 123.0,
              top: 456.0,
              child: new CompositedTransformTarget(
                link: link,
                child: new Container(height: 10.0, width: 10.0),
              ),
25
            ),
26 27 28 29 30 31 32
            new Positioned(
              left: 787.0,
              top: 343.0,
              child: new CompositedTransformFollower(
                link: link,
                child: new Container(key: key, height: 10.0, width: 10.0),
              ),
33
            ),
34 35
          ],
        ),
36 37 38 39 40 41 42 43 44 45 46
      ),
    );
    final RenderBox box = key.currentContext.findRenderObject();
    expect(box.localToGlobal(Offset.zero), const Offset(123.0, 456.0));
  });

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