Skip to content

Commit 203f116

Browse files
authored
Update color assertions (#154752)
issue: #127855 This is a forward fix to help flutter/engine#54981 land. It makes the following changes: 1) Removes hardcoded `Color.toString()` assumptions in asserts 1) Switches some lerp tests to use numbers that are more friendly to uint8_t and floating point numbers 1) implements custom matchers for color 1) removes word wrapping for some asserts since it makes them fragile to changes in `Color.toString()` invocations ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 30835d7 commit 203f116

File tree

81 files changed

+1034
-865
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1034
-865
lines changed

packages/flutter/lib/src/foundation/diagnostics.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,9 @@ abstract class DiagnosticsNode {
17531753
/// `minLevel` specifies the minimum [DiagnosticLevel] for properties included
17541754
/// in the output.
17551755
///
1756+
/// `wrapWidth` specifies the column number where word wrapping will be
1757+
/// applied.
1758+
///
17561759
/// The [toStringDeep] method takes other arguments, but those are intended
17571760
/// for internal use when recursing to the descendants, and so can be ignored.
17581761
///
@@ -1768,12 +1771,13 @@ abstract class DiagnosticsNode {
17681771
String? prefixOtherLines,
17691772
TextTreeConfiguration? parentConfiguration,
17701773
DiagnosticLevel minLevel = DiagnosticLevel.debug,
1774+
int wrapWidth = 65,
17711775
}) {
17721776
String result = '';
17731777
assert(() {
17741778
result = TextTreeRenderer(
17751779
minLevel: minLevel,
1776-
wrapWidth: 65,
1780+
wrapWidth: wrapWidth,
17771781
).render(
17781782
this,
17791783
prefixLineOne: prefixLineOne,
@@ -3315,6 +3319,9 @@ abstract class DiagnosticableTree with Diagnosticable {
33153319
/// `minLevel` specifies the minimum [DiagnosticLevel] for properties included
33163320
/// in the output.
33173321
///
3322+
/// `wrapWidth` specifies the column number where word wrapping will be
3323+
/// applied.
3324+
///
33183325
/// The [toStringDeep] method takes other arguments, but those are intended
33193326
/// for internal use when recursing to the descendants, and so can be ignored.
33203327
///
@@ -3327,8 +3334,9 @@ abstract class DiagnosticableTree with Diagnosticable {
33273334
String prefixLineOne = '',
33283335
String? prefixOtherLines,
33293336
DiagnosticLevel minLevel = DiagnosticLevel.debug,
3337+
int wrapWidth = 65,
33303338
}) {
3331-
return toDiagnosticsNode().toStringDeep(prefixLineOne: prefixLineOne, prefixOtherLines: prefixOtherLines, minLevel: minLevel);
3339+
return toDiagnosticsNode().toStringDeep(prefixLineOne: prefixLineOne, prefixOtherLines: prefixOtherLines, minLevel: minLevel, wrapWidth: wrapWidth);
33323340
}
33333341

33343342
@override
@@ -3400,8 +3408,9 @@ mixin DiagnosticableTreeMixin implements DiagnosticableTree {
34003408
String prefixLineOne = '',
34013409
String? prefixOtherLines,
34023410
DiagnosticLevel minLevel = DiagnosticLevel.debug,
3411+
int wrapWidth = 65,
34033412
}) {
3404-
return toDiagnosticsNode().toStringDeep(prefixLineOne: prefixLineOne, prefixOtherLines: prefixOtherLines, minLevel: minLevel);
3413+
return toDiagnosticsNode().toStringDeep(prefixLineOne: prefixLineOne, prefixOtherLines: prefixOtherLines, minLevel: minLevel, wrapWidth: wrapWidth);
34053414
}
34063415

34073416
@override

packages/flutter/lib/src/rendering/object.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4002,11 +4002,13 @@ abstract class RenderObject with DiagnosticableTreeMixin implements HitTestTarge
40024002
String prefixLineOne = '',
40034003
String? prefixOtherLines = '',
40044004
DiagnosticLevel minLevel = DiagnosticLevel.debug,
4005+
int wrapWidth = 65,
40054006
}) {
40064007
return _withDebugActiveLayoutCleared(() => super.toStringDeep(
40074008
prefixLineOne: prefixLineOne,
40084009
prefixOtherLines: prefixOtherLines,
40094010
minLevel: minLevel,
4011+
wrapWidth: wrapWidth,
40104012
));
40114013
}
40124014

packages/flutter/lib/src/semantics/semantics.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3085,8 +3085,9 @@ class SemanticsNode with DiagnosticableTreeMixin {
30853085
String? prefixOtherLines,
30863086
DiagnosticLevel minLevel = DiagnosticLevel.debug,
30873087
DebugSemanticsDumpOrder childOrder = DebugSemanticsDumpOrder.traversalOrder,
3088+
int wrapWidth = 65,
30883089
}) {
3089-
return toDiagnosticsNode(childOrder: childOrder).toStringDeep(prefixLineOne: prefixLineOne, prefixOtherLines: prefixOtherLines, minLevel: minLevel);
3090+
return toDiagnosticsNode(childOrder: childOrder).toStringDeep(prefixLineOne: prefixLineOne, prefixOtherLines: prefixOtherLines, minLevel: minLevel, wrapWidth: wrapWidth);
30903091
}
30913092

30923093
@override

packages/flutter/test/animation/tween_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void main() {
4040
(FlutterError error) => error.diagnostics.map((DiagnosticsNode node) => node.toString()),
4141
'diagnostics',
4242
<String>[
43-
'Cannot lerp between "Color(0xff000000)" and "Color(0xffffffff)".',
43+
'Cannot lerp between "${const Color(0xff000000)}" and "${const Color(0xffffffff)}".',
4444
'The type Color might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
4545
'To lerp colors, consider ColorTween instead.',
4646
],
@@ -194,10 +194,10 @@ void main() {
194194
begin: const Color(0xff000000),
195195
end: const Color(0xffffffff),
196196
);
197-
expect(tween.lerp(0.0), const Color(0xff000000));
198-
expect(tween.lerp(0.5), const Color(0xff7f7f7f));
199-
expect(tween.lerp(0.7), const Color(0xffb2b2b2));
200-
expect(tween.lerp(1.0), const Color(0xffffffff));
197+
expect(tween.lerp(0.0), isSameColorAs(const Color(0xff000000)));
198+
expect(tween.lerp(0.5), isSameColorAs(const Color(0xff7f7f7f)));
199+
expect(tween.lerp(0.7), isSameColorAs(const Color(0xffb2b2b2)));
200+
expect(tween.lerp(1.0), isSameColorAs(const Color(0xffffffff)));
201201
});
202202

203203
test('StepTween', () {

packages/flutter/test/cupertino/colors_test.dart

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,20 @@ void main() {
130130
expect(
131131
dynamicColor.toString(),
132132
contains(
133-
'CupertinoDynamicColor(*color = Color(0xff000000)*, '
134-
'darkColor = Color(0xff000001), '
135-
'highContrastColor = Color(0xff000003), '
136-
'darkHighContrastColor = Color(0xff000005), '
137-
'elevatedColor = Color(0xff000002), '
138-
'darkElevatedColor = Color(0xff000004), '
139-
'highContrastElevatedColor = Color(0xff000006), '
140-
'darkHighContrastElevatedColor = Color(0xff000007)',
133+
'CupertinoDynamicColor(*color = ${const Color(0xff000000)}*, '
134+
'darkColor = ${const Color(0xff000001)}, '
135+
'highContrastColor = ${const Color(0xff000003)}, '
136+
'darkHighContrastColor = ${const Color(0xff000005)}, '
137+
'elevatedColor = ${const Color(0xff000002)}, '
138+
'darkElevatedColor = ${const Color(0xff000004)}, '
139+
'highContrastElevatedColor = ${const Color(0xff000006)}, '
140+
'darkHighContrastElevatedColor = ${const Color(0xff000007)}',
141141
),
142142
);
143-
expect(notSoDynamicColor1.toString(), contains('CupertinoDynamicColor(*color = Color(0xff000000)*'));
144-
expect(vibrancyDependentColor1.toString(), contains('CupertinoDynamicColor(*color = Color(0xff000001)*, darkColor = Color(0xff000000)'));
145-
expect(contrastDependentColor1.toString(), contains('CupertinoDynamicColor(*color = Color(0xff000001)*, highContrastColor = Color(0xff000000)'));
146-
expect(elevationDependentColor1.toString(), contains('CupertinoDynamicColor(*color = Color(0xff000001)*, elevatedColor = Color(0xff000000)'));
143+
expect(notSoDynamicColor1.toString(), contains('CupertinoDynamicColor(*color = ${const Color(0xff000000)}*'));
144+
expect(vibrancyDependentColor1.toString(), contains('CupertinoDynamicColor(*color = ${const Color(0xff000001)}*, darkColor = ${const Color(0xff000000)}'));
145+
expect(contrastDependentColor1.toString(), contains('CupertinoDynamicColor(*color = ${const Color(0xff000001)}*, highContrastColor = ${const Color(0xff000000)}'));
146+
expect(elevationDependentColor1.toString(), contains('CupertinoDynamicColor(*color = ${const Color(0xff000001)}*, elevatedColor = ${const Color(0xff000000)}'));
147147

148148
expect(
149149
const CupertinoDynamicColor.withBrightnessAndContrast(
@@ -153,10 +153,10 @@ void main() {
153153
darkHighContrastColor: color3,
154154
).toString(),
155155
contains(
156-
'CupertinoDynamicColor(*color = Color(0xff000000)*, '
157-
'darkColor = Color(0xff000001), '
158-
'highContrastColor = Color(0xff000002), '
159-
'darkHighContrastColor = Color(0xff000003)',
156+
'CupertinoDynamicColor(*color = ${const Color(0xff000000)}*, '
157+
'darkColor = ${const Color(0xff000001)}, '
158+
'highContrastColor = ${const Color(0xff000002)}, '
159+
'darkHighContrastColor = ${const Color(0xff000003)}',
160160
),
161161
);
162162
});

packages/flutter/test/cupertino/nav_bar_transition_test.dart

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ void main() {
214214
// The transition's stack is ordered. The bottom middle is inserted first.
215215
final RenderParagraph bottomMiddle =
216216
tester.renderObject(flying(tester, find.text('Page 1')).first);
217-
expect(bottomMiddle.text.style!.color, const Color(0xff000306));
217+
expect(bottomMiddle.text.style!.color, isSameColorAs(const Color(0xff000306)));
218218
expect(bottomMiddle.text.style!.fontWeight, FontWeight.w600);
219219
expect(bottomMiddle.text.style!.fontFamily, 'CupertinoSystemText');
220220
expect(bottomMiddle.text.style!.letterSpacing, -0.41);
@@ -225,7 +225,7 @@ void main() {
225225
// are flipped.
226226
final RenderParagraph topBackLabel =
227227
tester.renderObject(flying(tester, find.text('Page 1')).last);
228-
expect(topBackLabel.text.style!.color, const Color(0xff000306));
228+
expect(topBackLabel.text.style!.color, isSameColorAs(const Color(0xff000306)));
229229
expect(topBackLabel.text.style!.fontWeight, FontWeight.w600);
230230
expect(topBackLabel.text.style!.fontFamily, 'CupertinoSystemText');
231231
expect(topBackLabel.text.style!.letterSpacing, -0.41);
@@ -234,14 +234,14 @@ void main() {
234234

235235
// Move animation further a bit.
236236
await tester.pump(const Duration(milliseconds: 200));
237-
expect(bottomMiddle.text.style!.color, const Color(0xff005ec5));
237+
expect(bottomMiddle.text.style!.color, isSameColorAs(const Color(0xff005ec5)));
238238
expect(bottomMiddle.text.style!.fontWeight, FontWeight.w400);
239239
expect(bottomMiddle.text.style!.fontFamily, 'CupertinoSystemText');
240240
expect(bottomMiddle.text.style!.letterSpacing, -0.41);
241241

242242
checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.0);
243243

244-
expect(topBackLabel.text.style!.color, const Color(0xff005ec5));
244+
expect(topBackLabel.text.style!.color, isSameColorAs(const Color(0xff005ec5)));
245245
expect(topBackLabel.text.style!.fontWeight, FontWeight.w400);
246246
expect(topBackLabel.text.style!.fontFamily, 'CupertinoSystemText');
247247
expect(topBackLabel.text.style!.letterSpacing, -0.41);
@@ -262,7 +262,7 @@ void main() {
262262
// The transition's stack is ordered. The bottom middle is inserted first.
263263
final RenderParagraph bottomMiddle =
264264
tester.renderObject(flying(tester, find.text('Page 1')).first);
265-
expect(bottomMiddle.text.style!.color, const Color(0xfff8fbff));
265+
expect(bottomMiddle.text.style!.color, isSameColorAs(const Color(0xfff8fbff)));
266266
expect(bottomMiddle.text.style!.fontWeight, FontWeight.w600);
267267
expect(bottomMiddle.text.style!.fontFamily, 'CupertinoSystemText');
268268
expect(bottomMiddle.text.style!.letterSpacing, -0.41);
@@ -273,7 +273,7 @@ void main() {
273273
// are flipped.
274274
final RenderParagraph topBackLabel =
275275
tester.renderObject(flying(tester, find.text('Page 1')).last);
276-
expect(topBackLabel.text.style!.color, const Color(0xfff8fbff));
276+
expect(topBackLabel.text.style!.color, isSameColorAs(const Color(0xfff8fbff)));
277277
expect(topBackLabel.text.style!.fontWeight, FontWeight.w600);
278278
expect(topBackLabel.text.style!.fontFamily, 'CupertinoSystemText');
279279
expect(topBackLabel.text.style!.letterSpacing, -0.41);
@@ -282,14 +282,14 @@ void main() {
282282

283283
// Move animation further a bit.
284284
await tester.pump(const Duration(milliseconds: 200));
285-
expect(bottomMiddle.text.style!.color, const Color(0xff409fff));
285+
expect(bottomMiddle.text.style!.color, isSameColorAs(const Color(0xff409fff)));
286286
expect(bottomMiddle.text.style!.fontWeight, FontWeight.w400);
287287
expect(bottomMiddle.text.style!.fontFamily, 'CupertinoSystemText');
288288
expect(bottomMiddle.text.style!.letterSpacing, -0.41);
289289

290290
checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.0);
291291

292-
expect(topBackLabel.text.style!.color, const Color(0xff409fff));
292+
expect(topBackLabel.text.style!.color, isSameColorAs(const Color(0xff409fff)));
293293
expect(topBackLabel.text.style!.fontWeight, FontWeight.w400);
294294
expect(topBackLabel.text.style!.fontFamily, 'CupertinoSystemText');
295295
expect(topBackLabel.text.style!.letterSpacing, -0.41);
@@ -365,7 +365,7 @@ void main() {
365365
// The transition's stack is ordered. The bottom middle is inserted first.
366366
final RenderParagraph bottomMiddle =
367367
tester.renderObject(flying(tester, find.text('Page 1')).first);
368-
expect(bottomMiddle.text.style!.color, const Color(0xff000306));
368+
expect(bottomMiddle.text.style!.color, isSameColorAs(const Color(0xff000306)));
369369

370370
expect(
371371
tester.getTopLeft(flying(tester, find.text('Page 1')).first),
@@ -379,7 +379,7 @@ void main() {
379379
// are flipped.
380380
final RenderParagraph topBackLabel =
381381
tester.renderObject(flying(tester, find.text('Page 1')).last);
382-
expect(topBackLabel.text.style!.color, const Color(0xff000306));
382+
expect(topBackLabel.text.style!.color, isSameColorAs(const Color(0xff000306)));
383383
expect(
384384
tester.getTopLeft(flying(tester, find.text('Page 1')).last),
385385
const Offset(
@@ -417,7 +417,7 @@ void main() {
417417
// The transition's stack is ordered. The bottom middle is inserted first.
418418
final RenderParagraph bottomMiddle =
419419
tester.renderObject(flying(tester, find.text('Page 1')).first);
420-
expect(bottomMiddle.text.style!.color, const Color(0xff000306));
420+
expect(bottomMiddle.text.style!.color, isSameColorAs(const Color(0xff000306)));
421421
expect(
422422
tester.getTopLeft(flying(tester, find.text('Page 1')).first),
423423
const Offset(
@@ -430,7 +430,7 @@ void main() {
430430
// are flipped.
431431
final RenderParagraph topBackLabel =
432432
tester.renderObject(flying(tester, find.text('Page 1')).last);
433-
expect(topBackLabel.text.style!.color, const Color(0xff000306));
433+
expect(topBackLabel.text.style!.color, isSameColorAs(const Color(0xff000306)));
434434
expect(
435435
tester.getTopLeft(flying(tester, find.text('Page 1')).last),
436436
const Offset(
@@ -1116,27 +1116,27 @@ void main() {
11161116
// The transition's stack is ordered. The bottom large title is inserted first.
11171117
final RenderParagraph bottomLargeTitle =
11181118
tester.renderObject(flying(tester, find.text('Page 1')).first);
1119-
expect(bottomLargeTitle.text.style!.color, const Color(0xff000306));
1119+
expect(bottomLargeTitle.text.style!.color, isSameColorAs(const Color(0xff000306)));
11201120
expect(bottomLargeTitle.text.style!.fontWeight, FontWeight.w700);
11211121
expect(bottomLargeTitle.text.style!.fontFamily, 'CupertinoSystemDisplay');
11221122
expect(bottomLargeTitle.text.style!.letterSpacing, moreOrLessEquals(0.35967791542410854));
11231123

11241124
// The top back label is styled exactly the same way.
11251125
final RenderParagraph topBackLabel =
11261126
tester.renderObject(flying(tester, find.text('Page 1')).last);
1127-
expect(topBackLabel.text.style!.color, const Color(0xff000306));
1127+
expect(topBackLabel.text.style!.color, isSameColorAs(const Color(0xff000306)));
11281128
expect(topBackLabel.text.style!.fontWeight, FontWeight.w700);
11291129
expect(topBackLabel.text.style!.fontFamily, 'CupertinoSystemDisplay');
11301130
expect(topBackLabel.text.style!.letterSpacing, moreOrLessEquals(0.35967791542410854));
11311131

11321132
// Move animation further a bit.
11331133
await tester.pump(const Duration(milliseconds: 200));
1134-
expect(bottomLargeTitle.text.style!.color, const Color(0xff005ec5));
1134+
expect(bottomLargeTitle.text.style!.color, isSameColorAs(const Color(0xff005ec5)));
11351135
expect(bottomLargeTitle.text.style!.fontWeight, FontWeight.w500);
11361136
expect(bottomLargeTitle.text.style!.fontFamily, 'CupertinoSystemText');
11371137
expect(bottomLargeTitle.text.style!.letterSpacing, moreOrLessEquals(-0.23270857974886894));
11381138

1139-
expect(topBackLabel.text.style!.color, const Color(0xff005ec5));
1139+
expect(topBackLabel.text.style!.color, isSameColorAs(const Color(0xff005ec5)));
11401140
expect(topBackLabel.text.style!.fontWeight, FontWeight.w500);
11411141
expect(topBackLabel.text.style!.fontFamily, 'CupertinoSystemText');
11421142
expect(topBackLabel.text.style!.letterSpacing, moreOrLessEquals(-0.23270857974886894));

0 commit comments

Comments
 (0)