transform_test.dart 9.93 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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;

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

import 'rendering_tester.dart';

12
Offset round(Offset value) {
13
  return Offset(value.dx.roundToDouble(), value.dy.roundToDouble());
14 15 16
}

void main() {
17 18
  TestRenderingFlutterBinding.ensureInitialized();

19 20
  test('RenderTransform - identity', () {
    RenderBox inner;
21 22
    final RenderBox sizer = RenderTransform(
      transform: Matrix4.identity(),
23
      alignment: Alignment.center,
24
      child: inner = RenderSizedBox(const Size(100.0, 100.0)),
25
    );
26
    layout(sizer, constraints: BoxConstraints.tight(const Size(100.0, 100.0)), alignment: Alignment.topLeft);
27
    expect(inner.globalToLocal(Offset.zero), equals(Offset.zero));
28 29 30
    expect(inner.globalToLocal(const Offset(100.0, 100.0)), equals(const Offset(100.0, 100.0)));
    expect(inner.globalToLocal(const Offset(25.0, 75.0)), equals(const Offset(25.0, 75.0)));
    expect(inner.globalToLocal(const Offset(50.0, 50.0)), equals(const Offset(50.0, 50.0)));
31
    expect(inner.localToGlobal(Offset.zero), equals(Offset.zero));
32 33 34
    expect(inner.localToGlobal(const Offset(100.0, 100.0)), equals(const Offset(100.0, 100.0)));
    expect(inner.localToGlobal(const Offset(25.0, 75.0)), equals(const Offset(25.0, 75.0)));
    expect(inner.localToGlobal(const Offset(50.0, 50.0)), equals(const Offset(50.0, 50.0)));
35 36 37 38
  });

  test('RenderTransform - identity with internal offset', () {
    RenderBox inner;
39 40
    final RenderBox sizer = RenderTransform(
      transform: Matrix4.identity(),
41
      alignment: Alignment.center,
42
      child: RenderPadding(
43
        padding: const EdgeInsets.only(left: 20.0),
44
        child: inner = RenderSizedBox(const Size(80.0, 100.0)),
45 46
      ),
    );
47
    layout(sizer, constraints: BoxConstraints.tight(const Size(100.0, 100.0)), alignment: Alignment.topLeft);
48
    expect(inner.globalToLocal(Offset.zero), equals(const Offset(-20.0, 0.0)));
49 50 51
    expect(inner.globalToLocal(const Offset(100.0, 100.0)), equals(const Offset(80.0, 100.0)));
    expect(inner.globalToLocal(const Offset(25.0, 75.0)), equals(const Offset(5.0, 75.0)));
    expect(inner.globalToLocal(const Offset(50.0, 50.0)), equals(const Offset(30.0, 50.0)));
52
    expect(inner.localToGlobal(Offset.zero), equals(const Offset(20.0, 0.0)));
53 54 55
    expect(inner.localToGlobal(const Offset(100.0, 100.0)), equals(const Offset(120.0, 100.0)));
    expect(inner.localToGlobal(const Offset(25.0, 75.0)), equals(const Offset(45.0, 75.0)));
    expect(inner.localToGlobal(const Offset(50.0, 50.0)), equals(const Offset(70.0, 50.0)));
56 57 58 59
  });

  test('RenderTransform - translation', () {
    RenderBox inner;
60 61
    final RenderBox sizer = RenderTransform(
      transform: Matrix4.translationValues(50.0, 200.0, 0.0),
62
      alignment: Alignment.center,
63
      child: inner = RenderSizedBox(const Size(100.0, 100.0)),
64
    );
65
    layout(sizer, constraints: BoxConstraints.tight(const Size(100.0, 100.0)), alignment: Alignment.topLeft);
66
    expect(inner.globalToLocal(Offset.zero), equals(const Offset(-50.0, -200.0)));
67 68 69
    expect(inner.globalToLocal(const Offset(100.0, 100.0)), equals(const Offset(50.0, -100.0)));
    expect(inner.globalToLocal(const Offset(25.0, 75.0)), equals(const Offset(-25.0, -125.0)));
    expect(inner.globalToLocal(const Offset(50.0, 50.0)), equals(const Offset(0.0, -150.0)));
70
    expect(inner.localToGlobal(Offset.zero), equals(const Offset(50.0, 200.0)));
71 72 73
    expect(inner.localToGlobal(const Offset(100.0, 100.0)), equals(const Offset(150.0, 300.0)));
    expect(inner.localToGlobal(const Offset(25.0, 75.0)), equals(const Offset(75.0, 275.0)));
    expect(inner.localToGlobal(const Offset(50.0, 50.0)), equals(const Offset(100.0, 250.0)));
74 75 76 77
  });

  test('RenderTransform - translation with internal offset', () {
    RenderBox inner;
78 79
    final RenderBox sizer = RenderTransform(
      transform: Matrix4.translationValues(50.0, 200.0, 0.0),
80
      alignment: Alignment.center,
81
      child: RenderPadding(
82
        padding: const EdgeInsets.only(left: 20.0),
83
        child: inner = RenderSizedBox(const Size(80.0, 100.0)),
84 85
      ),
    );
86
    layout(sizer, constraints: BoxConstraints.tight(const Size(100.0, 100.0)), alignment: Alignment.topLeft);
87
    expect(inner.globalToLocal(Offset.zero), equals(const Offset(-70.0, -200.0)));
88 89 90
    expect(inner.globalToLocal(const Offset(100.0, 100.0)), equals(const Offset(30.0, -100.0)));
    expect(inner.globalToLocal(const Offset(25.0, 75.0)), equals(const Offset(-45.0, -125.0)));
    expect(inner.globalToLocal(const Offset(50.0, 50.0)), equals(const Offset(-20.0, -150.0)));
91
    expect(inner.localToGlobal(Offset.zero), equals(const Offset(70.0, 200.0)));
92 93 94
    expect(inner.localToGlobal(const Offset(100.0, 100.0)), equals(const Offset(170.0, 300.0)));
    expect(inner.localToGlobal(const Offset(25.0, 75.0)), equals(const Offset(95.0, 275.0)));
    expect(inner.localToGlobal(const Offset(50.0, 50.0)), equals(const Offset(120.0, 250.0)));
95 96 97 98
  });

  test('RenderTransform - rotation', () {
    RenderBox inner;
99 100
    final RenderBox sizer = RenderTransform(
      transform: Matrix4.rotationZ(math.pi),
101
      alignment: Alignment.center,
102
      child: inner = RenderSizedBox(const Size(100.0, 100.0)),
103
    );
104
    layout(sizer, constraints: BoxConstraints.tight(const Size(100.0, 100.0)), alignment: Alignment.topLeft);
105 106
    expect(round(inner.globalToLocal(Offset.zero)), equals(const Offset(100.0, 100.0)));
    expect(round(inner.globalToLocal(const Offset(100.0, 100.0))), equals(Offset.zero));
107 108
    expect(round(inner.globalToLocal(const Offset(25.0, 75.0))), equals(const Offset(75.0, 25.0)));
    expect(round(inner.globalToLocal(const Offset(50.0, 50.0))), equals(const Offset(50.0, 50.0)));
109 110
    expect(round(inner.localToGlobal(Offset.zero)), equals(const Offset(100.0, 100.0)));
    expect(round(inner.localToGlobal(const Offset(100.0, 100.0))), equals(Offset.zero));
111 112
    expect(round(inner.localToGlobal(const Offset(25.0, 75.0))), equals(const Offset(75.0, 25.0)));
    expect(round(inner.localToGlobal(const Offset(50.0, 50.0))), equals(const Offset(50.0, 50.0)));
113 114 115 116
  });

  test('RenderTransform - rotation with internal offset', () {
    RenderBox inner;
117 118
    final RenderBox sizer = RenderTransform(
      transform: Matrix4.rotationZ(math.pi),
119
      alignment: Alignment.center,
120
      child: RenderPadding(
121
        padding: const EdgeInsets.only(left: 20.0),
122
        child: inner = RenderSizedBox(const Size(80.0, 100.0)),
123 124
      ),
    );
125
    layout(sizer, constraints: BoxConstraints.tight(const Size(100.0, 100.0)), alignment: Alignment.topLeft);
126
    expect(round(inner.globalToLocal(Offset.zero)), equals(const Offset(80.0, 100.0)));
127 128 129
    expect(round(inner.globalToLocal(const Offset(100.0, 100.0))), equals(const Offset(-20.0, 0.0)));
    expect(round(inner.globalToLocal(const Offset(25.0, 75.0))), equals(const Offset(55.0, 25.0)));
    expect(round(inner.globalToLocal(const Offset(50.0, 50.0))), equals(const Offset(30.0, 50.0)));
130
    expect(round(inner.localToGlobal(Offset.zero)), equals(const Offset(80.0, 100.0)));
131 132 133
    expect(round(inner.localToGlobal(const Offset(100.0, 100.0))), equals(const Offset(-20.0, 0.0)));
    expect(round(inner.localToGlobal(const Offset(25.0, 75.0))), equals(const Offset(55.0, 25.0)));
    expect(round(inner.localToGlobal(const Offset(50.0, 50.0))), equals(const Offset(30.0, 50.0)));
134 135 136 137
  });

  test('RenderTransform - perspective - globalToLocal', () {
    RenderBox inner;
138
    final RenderBox sizer = RenderTransform(
139
      transform: rotateAroundXAxis(math.pi * 0.25), // at pi/4, we are about 70 pixels high
140
      alignment: Alignment.center,
141
      child: inner = RenderSizedBox(const Size(100.0, 100.0)),
142
    );
143
    layout(sizer, constraints: BoxConstraints.tight(const Size(100.0, 100.0)), alignment: Alignment.topLeft);
144

145 146 147
    expect(round(inner.globalToLocal(const Offset(25.0, 50.0))), equals(const Offset(25.0, 50.0)));
    expect(inner.globalToLocal(const Offset(25.0, 17.0)).dy, greaterThan(0.0));
    expect(inner.globalToLocal(const Offset(25.0, 17.0)).dy, lessThan(10.0));
148 149
    expect(inner.globalToLocal(const Offset(25.0, 83.0)).dy, greaterThan(90.0));
    expect(inner.globalToLocal(const Offset(25.0, 83.0)).dy, lessThan(100.0));
150 151 152 153
    expect(
      round(inner.globalToLocal(const Offset(25.0, 17.0))).dy,
      equals(100 - round(inner.globalToLocal(const Offset(25.0, 83.0))).dy),
    );
154
  });
155 156 157

  test('RenderTransform - perspective - localToGlobal', () {
    RenderBox inner;
158
    final RenderBox sizer = RenderTransform(
159
      transform: rotateAroundXAxis(math.pi * 0.4999), // at pi/2, we're seeing the box on its edge,
160
      alignment: Alignment.center,
161
      child: inner = RenderSizedBox(const Size(100.0, 100.0)),
162
    );
163
    layout(sizer, constraints: BoxConstraints.tight(const Size(100.0, 100.0)), alignment: Alignment.topLeft);
164 165 166

    // the inner widget has a height of about half a pixel at this rotation, so
    // everything should end up around the middle of the outer box.
167 168 169
    expect(inner.localToGlobal(const Offset(25.0, 50.0)), equals(const Offset(25.0, 50.0)));
    expect(round(inner.localToGlobal(const Offset(25.0, 75.0))), equals(const Offset(25.0, 50.0)));
    expect(round(inner.localToGlobal(const Offset(25.0, 100.0))), equals(const Offset(25.0, 50.0)));
170 171 172 173 174
  });
}

Matrix4 rotateAroundXAxis(double a) {
  // 3D rotation transform with alpha=a
175 176 177
  const double x = 1.0;
  const double y = 0.0;
  const double z = 0.0;
178 179
  final double sc = math.sin(a / 2.0) * math.cos(a / 2.0);
  final double sq = math.sin(a / 2.0) * math.sin(a / 2.0);
180
  return Matrix4.fromList(<double>[
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    // col 1
    1.0 - 2.0 * (y * y + z * z) * sq,
    2.0 * (x * y * sq + z * sc),
    2.0 * (x * z * sq - y * sc),
    0.0,
    // col 2
    2.0 * (x * y * sq - z * sc),
    1.0 - 2.0 * (x * x + z * z) * sq,
    2.0 * (y * z * sq + x * sc),
    0.0,
    // col 3
    2.0 * (x * z * sq + y * sc),
    2.0 * (y * z * sq - x * sc),
    1.0 - 2.0 * (x * x + z * z) * sq,
    0.0,
    // col 4
    0.0, 0.0, 0.0, 1.0,
  ]);
199
}