Skip to content

Commit cdbe141

Browse files
committed
test: Use color checks that tolerate floating-point errors
Done to follow a draft migration guide: https://github.com/flutter/website/blob/997c893ccec5fb15dd085a4d23fe8c43870c631b/src/content/release/breaking-changes/wide-gamut-framework.md for the proposed breaking change that raised flutter/flutter#155113 In our case we also needed to use `package:flutter_checks` and `package:legacy_checks` to wrap the legacy `Matcher`s `isSameColorAs` and `isSameColorSwatchAs` from `package:flutter_test`; see discussion of those: flutter/flutter#155113 (comment) flutter/flutter#155113 (comment)
1 parent 90c7b26 commit cdbe141

10 files changed

+80
-48
lines changed

test/widgets/channel_colors_test.dart

+25-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import 'package:checks/checks.dart';
22
import 'package:flutter/widgets.dart';
3+
import 'package:flutter_checks/flutter_checks.dart';
34
import 'package:flutter_test/flutter_test.dart';
45
import 'package:zulip/widgets/channel_colors.dart';
56

67
import 'channel_colors_checks.dart';
8+
import 'colors_checks.dart';
79

810
void main() {
911
group('ChannelColorSwatches', () {
@@ -12,12 +14,12 @@ void main() {
1214

1315
const base1 = 0xff76ce90;
1416
final swatch1 = instance.forBaseColor(base1);
15-
check(swatch1).equals(ChannelColorSwatch.light(base1));
17+
check(swatch1).isSameColorSwatchAs(ChannelColorSwatch.light(base1));
1618
check(instance.forBaseColor(base1)).identicalTo(swatch1);
1719

1820
const base2 = 0xfffae589;
1921
final swatch2 = instance.forBaseColor(base2);
20-
check(swatch2).equals(ChannelColorSwatch.light(base2));
22+
check(swatch2).isSameColorSwatchAs(ChannelColorSwatch.light(base2));
2123
check(instance.forBaseColor(base2)).identicalTo(swatch2);
2224
check(instance.forBaseColor(base1)).identicalTo(swatch1);
2325
});
@@ -28,12 +30,12 @@ void main() {
2830

2931
const base1 = 0xff76ce90;
3032
final swatch1 = instance.forBaseColor(base1);
31-
check(swatch1).equals(ChannelColorSwatch.dark(base1));
33+
check(swatch1).isSameColorSwatchAs(ChannelColorSwatch.dark(base1));
3234
check(instance.forBaseColor(base1)).identicalTo(swatch1);
3335

3436
const base2 = 0xfffae589;
3537
final swatch2 = instance.forBaseColor(base2);
36-
check(swatch2).equals(ChannelColorSwatch.dark(base2));
38+
check(swatch2).isSameColorSwatchAs(ChannelColorSwatch.dark(base2));
3739
check(instance.forBaseColor(base2)).identicalTo(swatch2);
3840
check(instance.forBaseColor(base1)).identicalTo(swatch1);
3941
});
@@ -53,13 +55,13 @@ void main() {
5355

5456
const base1 = 0xff76ce90;
5557
final swatch1 = instance.forBaseColor(base1);
56-
check(swatch1).equals(ChannelColorSwatch.lerp(
58+
check(swatch1).isSameColorSwatchAs(ChannelColorSwatch.lerp(
5759
ChannelColorSwatch.light(base1), ChannelColorSwatch.dark(base1), 0.4)!);
5860
check(instance.forBaseColor(base1)).identicalTo(swatch1);
5961

6062
const base2 = 0xfffae589;
6163
final swatch2 = instance.forBaseColor(base2);
62-
check(swatch2).equals(ChannelColorSwatch.lerp(
64+
check(swatch2).isSameColorSwatchAs(ChannelColorSwatch.lerp(
6365
ChannelColorSwatch.light(base2), ChannelColorSwatch.dark(base2), 0.4)!);
6466
check(instance.forBaseColor(base2)).identicalTo(swatch2);
6567
check(instance.forBaseColor(base1)).identicalTo(swatch1);
@@ -70,12 +72,14 @@ void main() {
7072
group('ChannelColorSwatch', () {
7173
group('light', () {
7274
test('base', () {
73-
check(ChannelColorSwatch.light(0xffffffff)).base.equals(const Color(0xffffffff));
75+
check(ChannelColorSwatch.light(0xffffffff))
76+
.base.isSameColorAs(const Color(0xffffffff));
7477
});
7578

7679
test('unreadCountBadgeBackground', () {
7780
void runCheck(int base, Color expected) {
78-
check(ChannelColorSwatch.light(base)).unreadCountBadgeBackground.equals(expected);
81+
check(ChannelColorSwatch.light(base))
82+
.unreadCountBadgeBackground.isSameColorAs(expected);
7983
}
8084

8185
// Check against everything in ZULIP_ASSIGNMENT_COLORS and EXTREME_COLORS
@@ -137,7 +141,8 @@ void main() {
137141

138142
test('iconOnPlainBackground', () {
139143
void runCheck(int base, Color expected) {
140-
check(ChannelColorSwatch.light(base)).iconOnPlainBackground.equals(expected);
144+
check(ChannelColorSwatch.light(base))
145+
.iconOnPlainBackground.isSameColorAs(expected);
141146
}
142147

143148
// Check against everything in ZULIP_ASSIGNMENT_COLORS
@@ -178,7 +183,8 @@ void main() {
178183

179184
test('iconOnBarBackground', () {
180185
void runCheck(int base, Color expected) {
181-
check(ChannelColorSwatch.light(base)).iconOnBarBackground.equals(expected);
186+
check(ChannelColorSwatch.light(base))
187+
.iconOnBarBackground.isSameColorAs(expected);
182188
}
183189

184190
// Check against everything in ZULIP_ASSIGNMENT_COLORS
@@ -219,7 +225,8 @@ void main() {
219225

220226
test('barBackground', () {
221227
void runCheck(int base, Color expected) {
222-
check(ChannelColorSwatch.light(base)).barBackground.equals(expected);
228+
check(ChannelColorSwatch.light(base))
229+
.barBackground.isSameColorAs(expected);
223230
}
224231

225232
// Check against everything in ZULIP_ASSIGNMENT_COLORS
@@ -262,13 +269,13 @@ void main() {
262269
group('dark', () {
263270
test('base', () {
264271
check(ChannelColorSwatch.dark(0xffffffff))
265-
.base.equals(const Color(0xffffffff));
272+
.base.isSameColorAs(const Color(0xffffffff));
266273
});
267274

268275
test('unreadCountBadgeBackground', () {
269276
void runCheck(int base, Color expected) {
270277
check(ChannelColorSwatch.dark(base))
271-
.unreadCountBadgeBackground.equals(expected);
278+
.unreadCountBadgeBackground.isSameColorAs(expected);
272279
}
273280

274281
// Check against everything in ZULIP_ASSIGNMENT_COLORS and EXTREME_COLORS
@@ -331,7 +338,7 @@ void main() {
331338
test('iconOnPlainBackground', () {
332339
void runCheck(int base, Color expected) {
333340
check(ChannelColorSwatch.dark(base))
334-
.iconOnPlainBackground.equals(expected);
341+
.iconOnPlainBackground.isSameColorAs(expected);
335342
}
336343

337344
// Check against everything in ZULIP_ASSIGNMENT_COLORS
@@ -373,7 +380,7 @@ void main() {
373380
test('iconOnBarBackground', () {
374381
void runCheck(int base, Color expected) {
375382
check(ChannelColorSwatch.dark(base))
376-
.iconOnBarBackground.equals(expected);
383+
.iconOnBarBackground.isSameColorAs(expected);
377384
}
378385

379386
// Check against everything in ZULIP_ASSIGNMENT_COLORS
@@ -415,7 +422,7 @@ void main() {
415422
test('barBackground', () {
416423
void runCheck(int base, Color expected) {
417424
check(ChannelColorSwatch.dark(base))
418-
.barBackground.equals(expected);
425+
.barBackground.isSameColorAs(expected);
419426
}
420427

421428
// Check against everything in ZULIP_ASSIGNMENT_COLORS
@@ -473,7 +480,7 @@ void main() {
473480
ChannelColorVariant.barBackground => (check(result).barBackground,
474481
Color.lerp(swatchA.barBackground, swatchB.barBackground, t)!),
475482
};
476-
subject.equals(expected);
483+
subject.isSameColorAs(expected);
477484
}
478485
}
479486
});
@@ -484,7 +491,7 @@ void main() {
484491
final swatch = ChannelColorSwatch.light(0xff76ce90);
485492
check(ChannelColorSwatch.lerp(swatch, swatch, 0.5)).isNotNull()
486493
..identicalTo(swatch)
487-
..base.equals(const Color(0xff76ce90));
494+
..base.isSameColorAs(const Color(0xff76ce90));
488495
});
489496
});
490497
}

test/widgets/colors_checks.dart

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'package:checks/checks.dart';
2+
import 'package:flutter_test/flutter_test.dart' as flutter_matcher;
3+
import 'package:flutter/painting.dart';
4+
import 'package:legacy_checks/legacy_checks.dart';
5+
6+
extension ColorSwatchChecks<T> on Subject<ColorSwatch<T>> {
7+
/// package:checks-style wrapper for [flutter_matcher.isSameColorSwatchAs].
8+
void isSameColorSwatchAs(ColorSwatch<T> colorSwatch) {
9+
legacyMatcher(flutter_matcher.isSameColorSwatchAs(colorSwatch));
10+
}
11+
}

test/widgets/compose_box_test.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:convert';
22

33
import 'package:checks/checks.dart';
44
import 'package:file_picker/file_picker.dart';
5+
import 'package:flutter_checks/flutter_checks.dart';
56
import 'package:http/http.dart' as http;
67
import 'package:flutter/material.dart';
78
import 'package:flutter_test/flutter_test.dart';
@@ -258,7 +259,7 @@ void main() {
258259
final expectedForegroundColor = expected
259260
? colorScheme.onSurface.withValues(alpha: 0.38)
260261
: colorScheme.onPrimary;
261-
check(sendButtonWidget.color).equals(expectedForegroundColor);
262+
check(sendButtonWidget.color).isNotNull().isSameColorAs(expectedForegroundColor);
262263
}
263264

264265
group('attach from media library', () {

test/widgets/content_test.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:flutter/cupertino.dart';
33
import 'package:flutter/foundation.dart';
44
import 'package:flutter/material.dart';
55
import 'package:flutter/rendering.dart';
6+
import 'package:flutter_checks/flutter_checks.dart';
67
import 'package:flutter_test/flutter_test.dart';
78
import 'package:url_launcher/url_launcher.dart';
89
import 'package:zulip/api/core.dart';
@@ -880,7 +881,7 @@ void main() {
880881
final textColor = mergedStyleOfSubstring(textSpan, renderedTextRegexp)!.color;
881882
check(textColor).isNotNull();
882883

883-
check(icon).color.equals(textColor!);
884+
check(icon).color.isNotNull().isSameColorAs(textColor!);
884885
});
885886
}
886887

test/widgets/emoji_reaction_test.dart

+13-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:checks/checks.dart';
55
import 'package:flutter/foundation.dart';
66
import 'package:flutter/material.dart';
77
import 'package:flutter/services.dart';
8+
import 'package:flutter_checks/flutter_checks.dart';
89

910
import 'package:flutter_test/flutter_test.dart';
1011
import 'package:zulip/api/model/events.dart';
@@ -238,20 +239,26 @@ void main() {
238239
return material.color;
239240
}
240241

241-
check(backgroundColor('smile')).equals(EmojiReactionTheme.light().bgSelected);
242-
check(backgroundColor('tada')).equals(EmojiReactionTheme.light().bgUnselected);
242+
check(backgroundColor('smile')).isNotNull()
243+
.isSameColorAs(EmojiReactionTheme.light().bgSelected);
244+
check(backgroundColor('tada')).isNotNull()
245+
.isSameColorAs(EmojiReactionTheme.light().bgUnselected);
243246

244247
tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
245248
await tester.pump();
246249

247250
await tester.pump(kThemeAnimationDuration * 0.4);
248251
final expectedLerped = EmojiReactionTheme.light().lerp(EmojiReactionTheme.dark(), 0.4);
249-
check(backgroundColor('smile')).equals(expectedLerped.bgSelected);
250-
check(backgroundColor('tada')).equals(expectedLerped.bgUnselected);
252+
check(backgroundColor('smile')).isNotNull()
253+
.isSameColorAs(expectedLerped.bgSelected);
254+
check(backgroundColor('tada')).isNotNull()
255+
.isSameColorAs(expectedLerped.bgUnselected);
251256

252257
await tester.pump(kThemeAnimationDuration * 0.6);
253-
check(backgroundColor('smile')).equals(EmojiReactionTheme.dark().bgSelected);
254-
check(backgroundColor('tada')).equals(EmojiReactionTheme.dark().bgUnselected);
258+
check(backgroundColor('smile')).isNotNull()
259+
.isSameColorAs(EmojiReactionTheme.dark().bgSelected);
260+
check(backgroundColor('tada')).isNotNull()
261+
.isSameColorAs(EmojiReactionTheme.dark().bgUnselected);
255262
});
256263

257264
// TODO more tests:

test/widgets/inbox_test.dart

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:checks/checks.dart';
22
import 'package:flutter/material.dart';
3+
import 'package:flutter_checks/flutter_checks.dart';
34
import 'package:flutter_test/flutter_test.dart';
45
import 'package:zulip/api/model/events.dart';
56
import 'package:zulip/api/model/model.dart';
@@ -332,7 +333,7 @@ void main() {
332333
final icon = findHeaderCollapseIcon(tester, headerRow!);
333334
check(icon).icon.equals(ZulipIcons.arrow_down);
334335
check(allDmsHeaderBackgroundColor(tester))
335-
.isNotNull().equals(const HSLColor.fromAHSL(1, 46, 0.35, 0.93).toColor());
336+
.isNotNull().isSameColorAs(const HSLColor.fromAHSL(1, 46, 0.35, 0.93).toColor());
336337
check(tester.widgetList(findSectionContent)).isNotEmpty();
337338
}
338339

@@ -350,7 +351,7 @@ void main() {
350351
final icon = findHeaderCollapseIcon(tester, headerRow!);
351352
check(icon).icon.equals(ZulipIcons.arrow_right);
352353
check(allDmsHeaderBackgroundColor(tester))
353-
.isNotNull().equals(Colors.white);
354+
.isNotNull().isSameColorAs(Colors.white);
354355
check(tester.widgetList(findSectionContent)).isEmpty();
355356
}
356357

@@ -425,10 +426,10 @@ void main() {
425426
final collapseIcon = findHeaderCollapseIcon(tester, headerRow!);
426427
check(collapseIcon).icon.equals(ZulipIcons.arrow_down);
427428
final streamIcon = findStreamHeaderIcon(tester, streamId);
428-
check(streamIcon).color.equals(
429+
check(streamIcon).color.isNotNull().isSameColorAs(
429430
ChannelColorSwatch.light(subscription.color).iconOnBarBackground);
430431
check(streamHeaderBackgroundColor(tester, streamId))
431-
.isNotNull().equals(ChannelColorSwatch.light(subscription.color).barBackground);
432+
.isNotNull().isSameColorAs(ChannelColorSwatch.light(subscription.color).barBackground);
432433
check(tester.widgetList(findSectionContent)).isNotEmpty();
433434
}
434435

@@ -448,10 +449,10 @@ void main() {
448449
final collapseIcon = findHeaderCollapseIcon(tester, headerRow!);
449450
check(collapseIcon).icon.equals(ZulipIcons.arrow_right);
450451
final streamIcon = findStreamHeaderIcon(tester, streamId);
451-
check(streamIcon).color.equals(
452+
check(streamIcon).color.isNotNull().isSameColorAs(
452453
ChannelColorSwatch.light(subscription.color).iconOnPlainBackground);
453454
check(streamHeaderBackgroundColor(tester, streamId))
454-
.isNotNull().equals(Colors.white);
455+
.isNotNull().isSameColorAs(Colors.white);
455456
check(tester.widgetList(findSectionContent)).isEmpty();
456457
}
457458

@@ -482,15 +483,15 @@ void main() {
482483
checkAppearsUncollapsed(tester, stream.streamId, find.text('specific topic'));
483484

484485
check(streamHeaderBackgroundColor(tester, 1))
485-
.equals(ChannelColorSwatch.light(initialColor).barBackground);
486+
.isNotNull().isSameColorAs(ChannelColorSwatch.light(initialColor).barBackground);
486487

487488
final newColor = Colors.orange.argbInt;
488489
store.handleEvent(SubscriptionUpdateEvent(id: 1, streamId: 1,
489490
property: SubscriptionProperty.color, value: newColor));
490491
await tester.pump();
491492

492493
check(streamHeaderBackgroundColor(tester, 1))
493-
.equals(ChannelColorSwatch.light(newColor).barBackground);
494+
.isNotNull().isSameColorAs(ChannelColorSwatch.light(newColor).barBackground);
494495
});
495496

496497
testWidgets('collapse stream section when partially offscreen: '

test/widgets/message_list_test.dart

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:checks/checks.dart';
44
import 'package:collection/collection.dart';
55
import 'package:flutter/material.dart';
66
import 'package:flutter/rendering.dart';
7+
import 'package:flutter_checks/flutter_checks.dart';
78
import 'package:flutter_test/flutter_test.dart';
89
import 'package:http/http.dart' as http;
910
import 'package:zulip/api/model/events.dart';
@@ -158,17 +159,17 @@ void main() {
158159
return widget.color;
159160
}
160161

161-
check(backgroundColor()).equals(MessageListTheme.light().streamMessageBgDefault);
162+
check(backgroundColor()).isSameColorAs(MessageListTheme.light().streamMessageBgDefault);
162163

163164
tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
164165
await tester.pump();
165166

166167
await tester.pump(kThemeAnimationDuration * 0.4);
167168
final expectedLerped = MessageListTheme.light().lerp(MessageListTheme.dark(), 0.4);
168-
check(backgroundColor()).equals(expectedLerped.streamMessageBgDefault);
169+
check(backgroundColor()).isSameColorAs(expectedLerped.streamMessageBgDefault);
169170

170171
await tester.pump(kThemeAnimationDuration * 0.6);
171-
check(backgroundColor()).equals(MessageListTheme.dark().streamMessageBgDefault);
172+
check(backgroundColor()).isSameColorAs(MessageListTheme.dark().streamMessageBgDefault);
172173
});
173174

174175
group('fetch older messages on scroll', () {
@@ -765,7 +766,7 @@ void main() {
765766
find.descendant(
766767
of: find.byType(StreamMessageRecipientHeader),
767768
matching: find.byType(ColoredBox),
768-
))).color.equals(swatch.barBackground);
769+
))).color.isNotNull().isSameColorAs(swatch.barBackground);
769770
});
770771

771772
testWidgets('color of stream icon', (tester) async {
@@ -777,7 +778,7 @@ void main() {
777778
subscriptions: [subscription]);
778779
await tester.pump();
779780
check(tester.widget<Icon>(find.byIcon(ZulipIcons.globe)))
780-
.color.equals(swatch.iconOnBarBackground);
781+
.color.isNotNull().isSameColorAs(swatch.iconOnBarBackground);
781782
});
782783

783784
testWidgets('normal streams show hash icon', (tester) async {
@@ -893,7 +894,7 @@ void main() {
893894
zulipLocalizations.messageListGroupYouAndOthers(
894895
zulipLocalizations.unknownUserName))).text;
895896
final icon = tester.widget<Icon>(find.byIcon(ZulipIcons.user));
896-
check(textSpan).style.isNotNull().color.equals(icon.color);
897+
check(textSpan).style.isNotNull().color.isNotNull().isSameColorAs(icon.color!);
897898
});
898899
});
899900

test/widgets/subscription_list_test.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:checks/checks.dart';
22
import 'package:flutter/material.dart';
3+
import 'package:flutter_checks/flutter_checks.dart';
34
import 'package:flutter_test/flutter_test.dart';
45
import 'package:zulip/api/model/initial_snapshot.dart';
56
import 'package:zulip/api/model/model.dart';
@@ -240,9 +241,9 @@ void main() {
240241
], unreadMsgs: unreadMsgs);
241242
check(getItemCount()).equals(1);
242243
check(tester.widget<Icon>(find.byIcon(iconDataForStream(stream))).color)
243-
.equals(swatch.iconOnPlainBackground);
244+
.isNotNull().isSameColorAs(swatch.iconOnPlainBackground);
244245
check(tester.widget<UnreadCountBadge>(find.byType(UnreadCountBadge)).backgroundColor)
245-
.equals(swatch);
246+
.isNotNull().isSameColorAs(swatch);
246247
});
247248

248249
testWidgets('muted streams are displayed as faded', (tester) async {

0 commit comments

Comments
 (0)