Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 8ccdbda

Browse files
authored
Migrate more tests from litetest to package:test (#55119)
Partial work towards flutter/flutter#133569. Commentary on removal of `assert`-conditionals here: flutter/flutter#155054.
1 parent fabc3d9 commit 8ccdbda

17 files changed

+164
-221
lines changed

flutter_frontend_server/test/to_string_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import 'package:test/test.dart';
1010

1111
void main() {
1212
final engine = Engine.findWithin();
13-
final manualBuildDir = io.Platform.environment['ENGINE_BUILD_DIR'];
13+
final manualBuildDir = io.Platform.environment['FLUTTER_BUILD_DIRECTORY'];
1414
final buildDir = manualBuildDir ?? engine.latestOutput()?.path.path;
1515
if (buildDir == null) {
16-
fail('No build directory found. Set ENGINE_BUILD_DIR');
16+
fail('No build directory found. Set FLUTTER_BUILD_DIRECTORY');
1717
}
1818
final frontendServer = path.join(
1919
buildDir,

testing/dart/canvas_test.dart

Lines changed: 77 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import 'dart:math';
88
import 'dart:typed_data';
99
import 'dart:ui';
1010

11-
import 'package:litetest/litetest.dart';
1211
import 'package:path/path.dart' as path;
12+
import 'package:test/test.dart';
1313
import 'package:vector_math/vector_math_64.dart';
1414

1515
import 'goldens.dart';
@@ -189,7 +189,10 @@ void main() async {
189189
canvas.drawRawAtlas(image, Float32List(0), Float32List(0), Int32List(0), BlendMode.src, null, paint);
190190
canvas.drawRawAtlas(image, Float32List(0), Float32List(0), null, null, rect, paint);
191191

192-
expectAssertion(() => canvas.drawAtlas(image, <RSTransform>[transform], <Rect>[rect], <Color>[color], null, rect, paint));
192+
expect(
193+
() => canvas.drawAtlas(image, <RSTransform>[transform], <Rect>[rect], <Color>[color], null, rect, paint),
194+
throwsA(isA<AssertionError>()),
195+
);
193196
});
194197

195198
test('Data lengths must match for drawAtlas methods', () async {
@@ -208,15 +211,15 @@ void main() async {
208211
canvas.drawRawAtlas(image, Float32List(4), Float32List(4), Int32List(1), BlendMode.src, rect, paint);
209212
canvas.drawRawAtlas(image, Float32List(4), Float32List(4), null, null, rect, paint);
210213

211-
expectArgumentError(() => canvas.drawAtlas(image, <RSTransform>[transform], <Rect>[], <Color>[color], BlendMode.src, rect, paint));
212-
expectArgumentError(() => canvas.drawAtlas(image, <RSTransform>[], <Rect>[rect], <Color>[color], BlendMode.src, rect, paint));
213-
expectArgumentError(() => canvas.drawAtlas(image, <RSTransform>[transform], <Rect>[rect], <Color>[color, color], BlendMode.src, rect, paint));
214-
expectArgumentError(() => canvas.drawAtlas(image, <RSTransform>[transform], <Rect>[rect, rect], <Color>[color], BlendMode.src, rect, paint));
215-
expectArgumentError(() => canvas.drawAtlas(image, <RSTransform>[transform, transform], <Rect>[rect], <Color>[color], BlendMode.src, rect, paint));
216-
expectArgumentError(() => canvas.drawRawAtlas(image, Float32List(3), Float32List(3), null, null, rect, paint));
217-
expectArgumentError(() => canvas.drawRawAtlas(image, Float32List(4), Float32List(0), null, null, rect, paint));
218-
expectArgumentError(() => canvas.drawRawAtlas(image, Float32List(0), Float32List(4), null, null, rect, paint));
219-
expectArgumentError(() => canvas.drawRawAtlas(image, Float32List(4), Float32List(4), Int32List(2), BlendMode.src, rect, paint));
214+
expect(() => canvas.drawAtlas(image, <RSTransform>[transform], <Rect>[], <Color>[color], BlendMode.src, rect, paint), throwsArgumentError);
215+
expect(() => canvas.drawAtlas(image, <RSTransform>[], <Rect>[rect], <Color>[color], BlendMode.src, rect, paint), throwsArgumentError);
216+
expect(() => canvas.drawAtlas(image, <RSTransform>[transform], <Rect>[rect], <Color>[color, color], BlendMode.src, rect, paint), throwsArgumentError);
217+
expect(() => canvas.drawAtlas(image, <RSTransform>[transform], <Rect>[rect, rect], <Color>[color], BlendMode.src, rect, paint), throwsArgumentError);
218+
expect(() => canvas.drawAtlas(image, <RSTransform>[transform, transform], <Rect>[rect], <Color>[color], BlendMode.src, rect, paint), throwsArgumentError);
219+
expect(() => canvas.drawRawAtlas(image, Float32List(3), Float32List(3), null, null, rect, paint), throwsArgumentError);
220+
expect(() => canvas.drawRawAtlas(image, Float32List(4), Float32List(0), null, null, rect, paint), throwsArgumentError);
221+
expect(() => canvas.drawRawAtlas(image, Float32List(0), Float32List(4), null, null, rect, paint), throwsArgumentError);
222+
expect(() => canvas.drawRawAtlas(image, Float32List(4), Float32List(4), Int32List(2), BlendMode.src, rect, paint), throwsArgumentError);
220223
});
221224

222225
test('Canvas preserves perspective data in Matrix4', () async {
@@ -489,7 +492,7 @@ void main() async {
489492

490493
final ByteData dataSync = await drawOnCanvas(toImageImage);
491494
final ByteData data = await drawOnCanvas(toImageSyncImage);
492-
expect(data, listEquals(dataSync));
495+
expect(data.buffer.asUint8List(), equals(dataSync.buffer.asUint8List()));
493496
});
494497

495498
test('Canvas.drawParagraph throws when Paragraph.layout was not called', () async {
@@ -646,39 +649,6 @@ void main() async {
646649
await comparer.addGoldenImage(image, 'render_unordered_rects.png');
647650
});
648651

649-
Matcher closeToTransform(Float64List expected) => (dynamic v) {
650-
Expect.type<Float64List>(v);
651-
final Float64List value = v as Float64List;
652-
expect(expected.length, equals(16));
653-
expect(value.length, equals(16));
654-
for (int r = 0; r < 4; r++) {
655-
for (int c = 0; c < 4; c++) {
656-
final double vActual = value[r*4 + c];
657-
final double vExpected = expected[r*4 + c];
658-
if ((vActual - vExpected).abs() > 1e-10) {
659-
Expect.fail('matrix mismatch at $r, $c, $vActual not close to $vExpected');
660-
}
661-
}
662-
}
663-
};
664-
665-
Matcher notCloseToTransform(Float64List expected) => (dynamic v) {
666-
Expect.type<Float64List>(v);
667-
final Float64List value = v as Float64List;
668-
expect(expected.length, equals(16));
669-
expect(value.length, equals(16));
670-
for (int r = 0; r < 4; r++) {
671-
for (int c = 0; c < 4; c++) {
672-
final double vActual = value[r*4 + c];
673-
final double vExpected = expected[r*4 + c];
674-
if ((vActual - vExpected).abs() > 1e-10) {
675-
return;
676-
}
677-
}
678-
}
679-
Expect.fail('$value is too close to $expected');
680-
};
681-
682652
test('Canvas.translate affects canvas.getTransform', () async {
683653
final PictureRecorder recorder = PictureRecorder();
684654
final Canvas canvas = Canvas(recorder);
@@ -688,7 +658,7 @@ void main() async {
688658
expect(curMatrix, closeToTransform(matrix));
689659
canvas.translate(10, 10);
690660
final Float64List newCurMatrix = canvas.getTransform();
691-
expect(newCurMatrix, notCloseToTransform(matrix));
661+
expect(newCurMatrix, isNot(closeToTransform(matrix)));
692662
expect(curMatrix, closeToTransform(matrix));
693663
});
694664

@@ -701,7 +671,7 @@ void main() async {
701671
expect(curMatrix, closeToTransform(matrix));
702672
canvas.scale(10, 10);
703673
final Float64List newCurMatrix = canvas.getTransform();
704-
expect(newCurMatrix, notCloseToTransform(matrix));
674+
expect(newCurMatrix, isNot(closeToTransform(matrix)));
705675
expect(curMatrix, closeToTransform(matrix));
706676
});
707677

@@ -714,7 +684,7 @@ void main() async {
714684
expect(curMatrix, closeToTransform(matrix));
715685
canvas.rotate(pi / 2);
716686
final Float64List newCurMatrix = canvas.getTransform();
717-
expect(newCurMatrix, notCloseToTransform(matrix));
687+
expect(newCurMatrix, isNot(closeToTransform(matrix)));
718688
expect(curMatrix, closeToTransform(matrix));
719689
});
720690

@@ -727,7 +697,7 @@ void main() async {
727697
expect(curMatrix, closeToTransform(matrix));
728698
canvas.skew(10, 10);
729699
final Float64List newCurMatrix = canvas.getTransform();
730-
expect(newCurMatrix, notCloseToTransform(matrix));
700+
expect(newCurMatrix, isNot(closeToTransform(matrix)));
731701
expect(curMatrix, closeToTransform(matrix));
732702
});
733703

@@ -740,31 +710,10 @@ void main() async {
740710
expect(curMatrix, closeToTransform(matrix));
741711
canvas.translate(10, 10);
742712
final Float64List newCurMatrix = canvas.getTransform();
743-
expect(newCurMatrix, notCloseToTransform(matrix));
713+
expect(newCurMatrix, isNot(closeToTransform(matrix)));
744714
expect(curMatrix, closeToTransform(matrix));
745715
});
746716

747-
Matcher closeToRect(Rect expected) => (dynamic v) {
748-
Expect.type<Rect>(v);
749-
final Rect value = v as Rect;
750-
expect(value.left, closeTo(expected.left, 1e-6));
751-
expect(value.top, closeTo(expected.top, 1e-6));
752-
expect(value.right, closeTo(expected.right, 1e-6));
753-
expect(value.bottom, closeTo(expected.bottom, 1e-6));
754-
};
755-
756-
Matcher notCloseToRect(Rect expected) => (dynamic v) {
757-
Expect.type<Rect>(v);
758-
final Rect value = v as Rect;
759-
if ((value.left - expected.left).abs() > 1e-6 ||
760-
(value.top - expected.top).abs() > 1e-6 ||
761-
(value.right - expected.right).abs() > 1e-6 ||
762-
(value.bottom - expected.bottom).abs() > 1e-6) {
763-
return;
764-
}
765-
Expect.fail('$value is too close to $expected');
766-
};
767-
768717
test('Canvas.clipRect affects canvas.getClipBounds', () async {
769718
void testRect(Rect clipRect, bool doAA) {
770719
final PictureRecorder recorder = PictureRecorder();
@@ -798,8 +747,8 @@ void main() async {
798747
canvas.save();
799748
canvas.clipRect(const Rect.fromLTRB(0, 0, 15, 15));
800749
// Both clip bounds have changed
801-
expect(canvas.getLocalClipBounds(), notCloseToRect(clipExpandedBounds));
802-
expect(canvas.getDestinationClipBounds(), notCloseToRect(clipExpandedBounds));
750+
expect(canvas.getLocalClipBounds(), isNot(closeToRect(clipExpandedBounds)));
751+
expect(canvas.getDestinationClipBounds(), isNot(closeToRect(clipExpandedBounds)));
803752
// Previous return values have not changed
804753
expect(initialLocalBounds, closeToRect(clipExpandedBounds));
805754
expect(initialDestinationBounds, closeToRect(clipExpandedBounds));
@@ -881,8 +830,8 @@ void main() async {
881830
canvas.save();
882831
canvas.clipRect(const Rect.fromLTRB(0, 0, 15, 15));
883832
// Both clip bounds have changed
884-
expect(canvas.getLocalClipBounds(), notCloseToRect(clipExpandedBounds));
885-
expect(canvas.getDestinationClipBounds(), notCloseToRect(clipExpandedBounds));
833+
expect(canvas.getLocalClipBounds(), isNot(closeToRect(clipExpandedBounds)));
834+
expect(canvas.getDestinationClipBounds(), isNot(closeToRect(clipExpandedBounds)));
886835
// Previous return values have not changed
887836
expect(initialLocalBounds, closeToRect(clipExpandedBounds));
888837
expect(initialDestinationBounds, closeToRect(clipExpandedBounds));
@@ -921,8 +870,8 @@ void main() async {
921870
canvas.save();
922871
canvas.clipRect(const Rect.fromLTRB(0, 0, 15, 15), doAntiAlias: false);
923872
// Both clip bounds have changed
924-
expect(canvas.getLocalClipBounds(), notCloseToRect(clipBounds));
925-
expect(canvas.getDestinationClipBounds(), notCloseToRect(clipBounds));
873+
expect(canvas.getLocalClipBounds(), isNot(closeToRect(clipBounds)));
874+
expect(canvas.getDestinationClipBounds(), isNot(closeToRect(clipBounds)));
926875
// Previous return values have not changed
927876
expect(initialLocalBounds, closeToRect(clipBounds));
928877
expect(initialDestinationBounds, closeToRect(clipBounds));
@@ -985,8 +934,8 @@ void main() async {
985934
canvas.save();
986935
canvas.clipRect(const Rect.fromLTRB(0, 0, 15, 15));
987936
// Both clip bounds have changed
988-
expect(canvas.getLocalClipBounds(), notCloseToRect(clipExpandedBounds));
989-
expect(canvas.getDestinationClipBounds(), notCloseToRect(clipExpandedBounds));
937+
expect(canvas.getLocalClipBounds(), isNot(closeToRect(clipExpandedBounds)));
938+
expect(canvas.getDestinationClipBounds(), isNot(closeToRect(clipExpandedBounds)));
990939
// Previous return values have not changed
991940
expect(initialLocalBounds, closeToRect(clipExpandedBounds));
992941
expect(initialDestinationBounds, closeToRect(clipExpandedBounds));
@@ -1025,8 +974,8 @@ void main() async {
1025974
canvas.save();
1026975
canvas.clipRect(const Rect.fromLTRB(0, 0, 15, 15), doAntiAlias: false);
1027976
// Both clip bounds have changed
1028-
expect(canvas.getLocalClipBounds(), notCloseToRect(clipBounds));
1029-
expect(canvas.getDestinationClipBounds(), notCloseToRect(clipBounds));
977+
expect(canvas.getLocalClipBounds(), isNot(closeToRect(clipBounds)));
978+
expect(canvas.getDestinationClipBounds(), isNot(closeToRect(clipBounds)));
1030979
// Previous return values have not changed
1031980
expect(initialLocalBounds, closeToRect(clipBounds));
1032981
expect(initialDestinationBounds, closeToRect(clipBounds));
@@ -1300,7 +1249,6 @@ void main() async {
13001249
final ByteData? data = await resultImage.toByteData();
13011250
if (data == null) {
13021251
fail('Expected non-null byte data');
1303-
return;
13041252
}
13051253
final int rgba = data.buffer.asUint32List()[0];
13061254
expect(rgba, 0xFF0000FF);
@@ -1315,11 +1263,51 @@ Future<Image> createTestImage() async {
13151263
return picture.toImage(1, 1);
13161264
}
13171265

1318-
Matcher listEquals(ByteData expected) => (dynamic v) {
1319-
Expect.type<ByteData>(v);
1320-
final ByteData value = v as ByteData;
1321-
expect(value.lengthInBytes, expected.lengthInBytes);
1322-
for (int i = 0; i < value.lengthInBytes; i++) {
1323-
expect(value.getUint8(i), expected.getUint8(i));
1266+
Matcher closeToRect(Rect rect) => _CloseToRectMatcher(rect);
1267+
final class _CloseToRectMatcher extends Matcher {
1268+
const _CloseToRectMatcher(this._expectedRect);
1269+
final Rect _expectedRect;
1270+
1271+
@override
1272+
bool matches(Object? item, Map<Object?, Object?> matchState) {
1273+
if (item is! Rect) {
1274+
return false;
1275+
}
1276+
return (item.left - _expectedRect.left).abs() < 1e-6 &&
1277+
(item.top - _expectedRect.top).abs() < 1e-6 &&
1278+
(item.right - _expectedRect.right).abs() < 1e-6 &&
1279+
(item.bottom - _expectedRect.bottom).abs() < 1e-6;
1280+
}
1281+
1282+
@override
1283+
Description describe(Description description) {
1284+
return description.add('Rect is close (within 1e-6) to $_expectedRect');
1285+
}
1286+
}
1287+
1288+
Matcher closeToTransform(Float64List expected) => _CloseToTransformMatcher(expected);
1289+
final class _CloseToTransformMatcher extends Matcher {
1290+
_CloseToTransformMatcher(this._expected);
1291+
final Float64List _expected;
1292+
1293+
@override
1294+
bool matches(Object? item, Map<Object?, Object?> matchState) {
1295+
if (item is! Float64List) {
1296+
return false;
1297+
}
1298+
if (item.length != 16 || _expected.length != 16) {
1299+
return false;
1300+
}
1301+
for (int i = 0; i < 16; i++) {
1302+
if ((item[i] - _expected[i]).abs() > 1e-10) {
1303+
return false;
1304+
}
1305+
}
1306+
return true;
13241307
}
1325-
};
1308+
1309+
@override
1310+
Description describe(Description description) {
1311+
return description.add('Transform is close (within 1e-10) to $_expected');
1312+
}
1313+
}

testing/dart/color_test.dart

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'dart:ui';
66

7-
import 'package:litetest/litetest.dart';
7+
import 'package:test/test.dart';
88

99
/// Positive result when the Colors will map to the same argb8888 color.
1010
Matcher colorMatches(dynamic o) => (v) {
@@ -23,10 +23,6 @@ class NotAColor extends Color {
2323
const NotAColor(super.value);
2424
}
2525

26-
Matcher approxEquals(dynamic o) => (v) {
27-
Expect.approxEquals(o as num, v as num);
28-
};
29-
3026
void main() {
3127
test('color accessors should work', () {
3228
const Color foo = Color(0x12345678);
@@ -58,9 +54,9 @@ void main() {
5854
test('two colors are only == if they have the same runtime type', () {
5955
expect(const Color(0x12345678), equals(const Color(0x12345678)));
6056
expect(const Color(0x12345678), equals(Color(0x12345678))); // ignore: prefer_const_constructors
61-
expect(const Color(0x12345678), notEquals(const Color(0x87654321)));
62-
expect(const Color(0x12345678), notEquals(const NotAColor(0x12345678)));
63-
expect(const NotAColor(0x12345678), notEquals(const Color(0x12345678)));
57+
expect(const Color(0x12345678), isNot(const Color(0x87654321)));
58+
expect(const Color(0x12345678), isNot(const NotAColor(0x12345678)));
59+
expect(const NotAColor(0x12345678), isNot(const Color(0x12345678)));
6460
expect(const NotAColor(0x12345678), equals(const NotAColor(0x12345678)));
6561
});
6662

@@ -257,9 +253,9 @@ void main() {
257253
alpha: 1, red: 1, green: 0, blue: 0, colorSpace: ColorSpace.displayP3);
258254
final Color srgb = p3.withValues(colorSpace: ColorSpace.extendedSRGB);
259255
expect(srgb.a, equals(1.0));
260-
expect(srgb.r, approxEquals(1.0931));
261-
expect(srgb.g, approxEquals(-0.22684034705162098));
262-
expect(srgb.b, approxEquals(-0.15007957816123998));
256+
expect(srgb.r, closeTo(1.0931, 1e-4));
257+
expect(srgb.g, closeTo(-0.22684034705162098, 1e-4));
258+
expect(srgb.b, closeTo(-0.15007957816123998, 1e-4));
263259
expect(srgb.colorSpace, equals(ColorSpace.extendedSRGB));
264260
});
265261

@@ -268,9 +264,9 @@ void main() {
268264
alpha: 1, red: 1, green: 0, blue: 0, colorSpace: ColorSpace.displayP3);
269265
final Color srgb = p3.withValues(colorSpace: ColorSpace.sRGB);
270266
expect(srgb.a, equals(1.0));
271-
expect(srgb.r, approxEquals(1));
272-
expect(srgb.g, approxEquals(0));
273-
expect(srgb.b, approxEquals(0));
267+
expect(srgb.r, closeTo(1, 1e-4));
268+
expect(srgb.g, closeTo(0, 1e-4));
269+
expect(srgb.b, closeTo(0, 1e-4));
274270
expect(srgb.colorSpace, equals(ColorSpace.sRGB));
275271
});
276272

@@ -283,9 +279,9 @@ void main() {
283279
colorSpace: ColorSpace.extendedSRGB);
284280
final Color p3 = srgb.withValues(colorSpace: ColorSpace.displayP3);
285281
expect(p3.a, equals(1.0));
286-
expect(p3.r, approxEquals(1));
287-
expect(p3.g, approxEquals(0));
288-
expect(p3.b, approxEquals(0));
282+
expect(p3.r, closeTo(1, 1e-4));
283+
expect(p3.g, closeTo(0, 1e-4));
284+
expect(p3.b, closeTo(0, 1e-4));
289285
expect(p3.colorSpace, equals(ColorSpace.displayP3));
290286
});
291287

@@ -311,15 +307,15 @@ void main() {
311307
alpha: 1, red: 1, green: 0, blue: 0);
312308
const Color p3 = Color.from(
313309
alpha: 1, red: 1, green: 0, blue: 0, colorSpace: ColorSpace.displayP3);
314-
expect(srgb.hashCode, notEquals(p3.hashCode));
310+
expect(srgb.hashCode, isNot(p3.hashCode));
315311
});
316312

317313
test('equality considers colorspace', () {
318314
const Color srgb = Color.from(
319315
alpha: 1, red: 1, green: 0, blue: 0);
320316
const Color p3 = Color.from(
321317
alpha: 1, red: 1, green: 0, blue: 0, colorSpace: ColorSpace.displayP3);
322-
expect(srgb, notEquals(p3));
318+
expect(srgb, isNot(p3));
323319
});
324320

325321
// Regression test for https://github.com/flutter/flutter/issues/41257

0 commit comments

Comments
 (0)