"Fossies" - the Fresh Open Source Software Archive 
Member "flutter-3.7.1/packages/flutter/test/painting/alignment_test.dart" (1 Feb 2023, 12427 Bytes) of package /linux/misc/flutter-3.7.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 // Copyright 2014 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 import 'dart:math' as math;
6
7 import 'package:flutter/painting.dart';
8 import 'package:flutter_test/flutter_test.dart';
9
10 void approxExpect(Alignment a, Alignment b) {
11 expect(a.x, moreOrLessEquals(b.x));
12 expect(a.y, moreOrLessEquals(b.y));
13 }
14
15 void main() {
16 test('Alignment control test', () {
17 const Alignment alignment = Alignment(0.5, 0.25);
18
19 expect(alignment, hasOneLineDescription);
20 expect(alignment.hashCode, equals(const Alignment(0.5, 0.25).hashCode));
21
22 expect(alignment / 2.0, const Alignment(0.25, 0.125));
23 expect(alignment ~/ 2.0, Alignment.center);
24 expect(alignment % 5.0, const Alignment(0.5, 0.25));
25 });
26
27 test('Alignment.lerp()', () {
28 const Alignment a = Alignment.topLeft;
29 const Alignment b = Alignment.topCenter;
30 expect(Alignment.lerp(a, b, 0.25), equals(const Alignment(-0.75, -1.0)));
31
32 expect(Alignment.lerp(null, null, 0.25), isNull);
33 expect(Alignment.lerp(null, b, 0.25), equals(const Alignment(0.0, -0.25)));
34 expect(Alignment.lerp(a, null, 0.25), equals(const Alignment(-0.75, -0.75)));
35 });
36
37 test('AlignmentGeometry invariants', () {
38 const AlignmentDirectional topStart = AlignmentDirectional.topStart;
39 const AlignmentDirectional topEnd = AlignmentDirectional.topEnd;
40 const Alignment center = Alignment.center;
41 const Alignment topLeft = Alignment.topLeft;
42 const Alignment topRight = Alignment.topRight;
43 final List<double> numbers = <double>[0.0, 1.0, -1.0, 2.0, 0.25, 0.5, 100.0, -999.75];
44
45 expect((topEnd * 0.0).add(topRight * 0.0), center);
46 expect(topEnd.add(topRight) * 0.0, (topEnd * 0.0).add(topRight * 0.0));
47 expect(topStart.add(topLeft), topLeft.add(topStart));
48 expect(topStart.add(topLeft).resolve(TextDirection.ltr), (topStart.resolve(TextDirection.ltr)) + topLeft);
49 expect(topStart.add(topLeft).resolve(TextDirection.rtl), (topStart.resolve(TextDirection.rtl)) + topLeft);
50 expect(topStart.add(topLeft).resolve(TextDirection.ltr), topStart.resolve(TextDirection.ltr).add(topLeft));
51 expect(topStart.add(topLeft).resolve(TextDirection.rtl), topStart.resolve(TextDirection.rtl).add(topLeft));
52 expect(topStart.resolve(TextDirection.ltr), topLeft);
53 expect(topStart.resolve(TextDirection.rtl), topRight);
54 expect(topEnd * 0.0, center);
55 expect(topLeft * 0.0, center);
56 expect(topStart * 1.0, topStart);
57 expect(topEnd * 1.0, topEnd);
58 expect(topLeft * 1.0, topLeft);
59 expect(topRight * 1.0, topRight);
60 for (final double n in numbers) {
61 expect((topStart * n).add(topStart), topStart * (n + 1.0));
62 expect((topEnd * n).add(topEnd), topEnd * (n + 1.0));
63 for (final double m in numbers) {
64 expect((topStart * n).add(topStart * m), topStart * (n + m));
65 }
66 }
67 expect(topStart + topStart + topStart, topStart * 3.0); // without using "add"
68 for (final TextDirection x in TextDirection.values) {
69 expect((topEnd * 0.0).add(topRight * 0.0).resolve(x), center.add(center).resolve(x));
70 expect((topEnd * 0.0).add(topLeft).resolve(x), center.add(topLeft).resolve(x));
71 expect((topEnd * 0.0).resolve(x).add(topLeft.resolve(x)), center.resolve(x).add(topLeft.resolve(x)));
72 expect((topEnd * 0.0).resolve(x).add(topLeft), center.resolve(x).add(topLeft));
73 expect((topEnd * 0.0).resolve(x), center.resolve(x));
74 }
75 expect(topStart, isNot(topLeft));
76 expect(topEnd, isNot(topLeft));
77 expect(topStart, isNot(topRight));
78 expect(topEnd, isNot(topRight));
79 expect(topStart.add(topLeft), isNot(topLeft));
80 expect(topStart.add(topLeft), isNot(topStart));
81 });
82
83 test('AlignmentGeometry.resolve()', () {
84 expect(const AlignmentDirectional(0.25, 0.3).resolve(TextDirection.ltr), const Alignment(0.25, 0.3));
85 expect(const AlignmentDirectional(0.25, 0.3).resolve(TextDirection.rtl), const Alignment(-0.25, 0.3));
86 expect(const AlignmentDirectional(-0.25, 0.3).resolve(TextDirection.ltr), const Alignment(-0.25, 0.3));
87 expect(const AlignmentDirectional(-0.25, 0.3).resolve(TextDirection.rtl), const Alignment(0.25, 0.3));
88 expect(const AlignmentDirectional(1.25, 0.3).resolve(TextDirection.ltr), const Alignment(1.25, 0.3));
89 expect(const AlignmentDirectional(1.25, 0.3).resolve(TextDirection.rtl), const Alignment(-1.25, 0.3));
90 expect(const AlignmentDirectional(0.5, -0.3).resolve(TextDirection.ltr), const Alignment(0.5, -0.3));
91 expect(const AlignmentDirectional(0.5, -0.3).resolve(TextDirection.rtl), const Alignment(-0.5, -0.3));
92 expect(AlignmentDirectional.center.resolve(TextDirection.ltr), Alignment.center);
93 expect(AlignmentDirectional.center.resolve(TextDirection.rtl), Alignment.center);
94 expect(AlignmentDirectional.bottomEnd.resolve(TextDirection.ltr), Alignment.bottomRight);
95 expect(AlignmentDirectional.bottomEnd.resolve(TextDirection.rtl), Alignment.bottomLeft);
96 expect(AlignmentDirectional(nonconst(1.0), 2.0), AlignmentDirectional(nonconst(1.0), 2.0));
97 expect(const AlignmentDirectional(1.0, 2.0), isNot(const AlignmentDirectional(2.0, 1.0)));
98 expect(
99 AlignmentDirectional.centerStart.resolve(TextDirection.ltr),
100 AlignmentDirectional.centerEnd.resolve(TextDirection.rtl),
101 );
102 expect(
103 AlignmentDirectional.centerStart.resolve(TextDirection.ltr),
104 isNot(AlignmentDirectional.centerEnd.resolve(TextDirection.ltr)),
105 );
106 expect(
107 AlignmentDirectional.centerEnd.resolve(TextDirection.ltr),
108 isNot(AlignmentDirectional.centerEnd.resolve(TextDirection.rtl)),
109 );
110 });
111
112 test('AlignmentGeometry.lerp ad hoc tests', () {
113 final AlignmentGeometry mixed1 = const Alignment(10.0, 20.0).add(const AlignmentDirectional(30.0, 50.0));
114 final AlignmentGeometry mixed2 = const Alignment(70.0, 110.0).add(const AlignmentDirectional(130.0, 170.0));
115 final AlignmentGeometry mixed3 = const Alignment(25.0, 42.5).add(const AlignmentDirectional(55.0, 80.0));
116
117 for (final TextDirection direction in TextDirection.values) {
118 expect(AlignmentGeometry.lerp(mixed1, mixed2, 0.0)!.resolve(direction), mixed1.resolve(direction));
119 expect(AlignmentGeometry.lerp(mixed1, mixed2, 1.0)!.resolve(direction), mixed2.resolve(direction));
120 expect(AlignmentGeometry.lerp(mixed1, mixed2, 0.25)!.resolve(direction), mixed3.resolve(direction));
121 }
122 });
123
124 test('lerp commutes with resolve', () {
125 final List<AlignmentGeometry?> offsets = <AlignmentGeometry?>[
126 Alignment.topLeft,
127 Alignment.topCenter,
128 Alignment.topRight,
129 AlignmentDirectional.topStart,
130 AlignmentDirectional.topCenter,
131 AlignmentDirectional.topEnd,
132 Alignment.centerLeft,
133 Alignment.center,
134 Alignment.centerRight,
135 AlignmentDirectional.centerStart,
136 AlignmentDirectional.center,
137 AlignmentDirectional.centerEnd,
138 Alignment.bottomLeft,
139 Alignment.bottomCenter,
140 Alignment.bottomRight,
141 AlignmentDirectional.bottomStart,
142 AlignmentDirectional.bottomCenter,
143 AlignmentDirectional.bottomEnd,
144 const Alignment(-1.0, 0.65),
145 const AlignmentDirectional(-1.0, 0.45),
146 const AlignmentDirectional(0.125, 0.625),
147 const Alignment(0.25, 0.875),
148 const Alignment(0.0625, 0.5625).add(const AlignmentDirectional(0.1875, 0.6875)),
149 const AlignmentDirectional(2.0, 3.0),
150 const Alignment(2.0, 3.0),
151 const Alignment(2.0, 3.0).add(const AlignmentDirectional(5.0, 3.0)),
152 const Alignment(10.0, 20.0).add(const AlignmentDirectional(30.0, 50.0)),
153 const Alignment(70.0, 110.0).add(const AlignmentDirectional(130.0, 170.0)),
154 const Alignment(25.0, 42.5).add(const AlignmentDirectional(55.0, 80.0)),
155 null,
156 ];
157
158 final List<double> times = <double>[ 0.25, 0.5, 0.75 ];
159
160 for (final TextDirection direction in TextDirection.values) {
161 final Alignment defaultValue = AlignmentDirectional.center.resolve(direction);
162 for (final AlignmentGeometry? a in offsets) {
163 final Alignment resolvedA = a?.resolve(direction) ?? defaultValue;
164 for (final AlignmentGeometry? b in offsets) {
165 final Alignment resolvedB = b?.resolve(direction) ?? defaultValue;
166 approxExpect(Alignment.lerp(resolvedA, resolvedB, 0.0)!, resolvedA);
167 approxExpect(Alignment.lerp(resolvedA, resolvedB, 1.0)!, resolvedB);
168 approxExpect((AlignmentGeometry.lerp(a, b, 0.0) ?? defaultValue).resolve(direction), resolvedA);
169 approxExpect((AlignmentGeometry.lerp(a, b, 1.0) ?? defaultValue).resolve(direction), resolvedB);
170 for (final double t in times) {
171 assert(t > 0.0);
172 assert(t < 1.0);
173 final Alignment value = (AlignmentGeometry.lerp(a, b, t) ?? defaultValue).resolve(direction);
174 approxExpect(value, Alignment.lerp(resolvedA, resolvedB, t)!);
175 final double minDX = math.min(resolvedA.x, resolvedB.x);
176 final double maxDX = math.max(resolvedA.x, resolvedB.x);
177 final double minDY = math.min(resolvedA.y, resolvedB.y);
178 final double maxDY = math.max(resolvedA.y, resolvedB.y);
179 expect(value.x, inInclusiveRange(minDX, maxDX));
180 expect(value.y, inInclusiveRange(minDY, maxDY));
181 }
182 }
183 }
184 }
185 });
186
187 test('AlignmentGeometry add/subtract', () {
188 const AlignmentGeometry directional = AlignmentDirectional(1.0, 2.0);
189 const AlignmentGeometry normal = Alignment(3.0, 5.0);
190 expect(directional.add(normal).resolve(TextDirection.ltr), const Alignment(4.0, 7.0));
191 expect(directional.add(normal).resolve(TextDirection.rtl), const Alignment(2.0, 7.0));
192 expect(normal.add(normal), normal * 2.0);
193 expect(directional.add(directional), directional * 2.0);
194 });
195
196 test('AlignmentGeometry operators', () {
197 expect(const AlignmentDirectional(1.0, 2.0) * 2.0, const AlignmentDirectional(2.0, 4.0));
198 expect(const AlignmentDirectional(1.0, 2.0) / 2.0, const AlignmentDirectional(0.5, 1.0));
199 expect(const AlignmentDirectional(1.0, 2.0) % 2.0, AlignmentDirectional.centerEnd);
200 expect(const AlignmentDirectional(1.0, 2.0) ~/ 2.0, AlignmentDirectional.bottomCenter);
201 for (final TextDirection direction in TextDirection.values) {
202 expect(Alignment.center.add(const AlignmentDirectional(1.0, 2.0) * 2.0).resolve(direction), const AlignmentDirectional(2.0, 4.0).resolve(direction));
203 expect(Alignment.center.add(const AlignmentDirectional(1.0, 2.0) / 2.0).resolve(direction), const AlignmentDirectional(0.5, 1.0).resolve(direction));
204 expect(Alignment.center.add(const AlignmentDirectional(1.0, 2.0) % 2.0).resolve(direction), AlignmentDirectional.centerEnd.resolve(direction));
205 expect(Alignment.center.add(const AlignmentDirectional(1.0, 2.0) ~/ 2.0).resolve(direction), AlignmentDirectional.bottomCenter.resolve(direction));
206 }
207 expect(const Alignment(1.0, 2.0) * 2.0, const Alignment(2.0, 4.0));
208 expect(const Alignment(1.0, 2.0) / 2.0, const Alignment(0.5, 1.0));
209 expect(const Alignment(1.0, 2.0) % 2.0, Alignment.centerRight);
210 expect(const Alignment(1.0, 2.0) ~/ 2.0, Alignment.bottomCenter);
211 });
212
213 test('AlignmentGeometry operators', () {
214 expect(const Alignment(1.0, 2.0) + const Alignment(3.0, 5.0), const Alignment(4.0, 7.0));
215 expect(const Alignment(1.0, 2.0) - const Alignment(3.0, 5.0), const Alignment(-2.0, -3.0));
216 expect(const AlignmentDirectional(1.0, 2.0) + const AlignmentDirectional(3.0, 5.0), const AlignmentDirectional(4.0, 7.0));
217 expect(const AlignmentDirectional(1.0, 2.0) - const AlignmentDirectional(3.0, 5.0), const AlignmentDirectional(-2.0, -3.0));
218 });
219
220 test('AlignmentGeometry toString', () {
221 expect(const Alignment(1.0001, 2.0001).toString(), 'Alignment(1.0, 2.0)');
222 expect(Alignment.center.toString(), 'Alignment.center');
223 expect(Alignment.bottomLeft.add(AlignmentDirectional.centerEnd).toString(), 'Alignment.bottomLeft + AlignmentDirectional.centerEnd');
224 expect(const Alignment(0.0001, 0.0001).toString(), 'Alignment(0.0, 0.0)');
225 expect(Alignment.center.toString(), 'Alignment.center');
226 expect(AlignmentDirectional.center.toString(), 'AlignmentDirectional.center');
227 expect(Alignment.bottomRight.add(AlignmentDirectional.bottomEnd).toString(), 'Alignment(1.0, 2.0) + AlignmentDirectional.centerEnd');
228 });
229 }