Skip to content

Commit 01555e8

Browse files
committed
content test: Start testing font weight
1 parent a7b317b commit 01555e8

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

test/flutter_checks.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ extension TextStyleChecks on Subject<TextStyle> {
7777
// TODO others
7878
}
7979

80+
extension FontVariationChecks on Subject<FontVariation> {
81+
Subject<String> get axis => has((x) => x.axis, 'axis');
82+
Subject<double> get value => has((x) => x.value, 'value');
83+
}
8084

8185
extension TextThemeChecks on Subject<TextTheme> {
8286
Subject<TextStyle?> get displayLarge => has((t) => t.displayLarge, 'displayLarge');

test/widgets/content_test.dart

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:zulip/widgets/icons.dart';
1414
import 'package:zulip/widgets/message_list.dart';
1515
import 'package:zulip/widgets/page.dart';
1616
import 'package:zulip/widgets/store.dart';
17+
import 'package:zulip/widgets/text.dart';
1718
import 'package:zulip/widgets/theme.dart';
1819

1920
import '../example_data.dart' as eg;
@@ -154,6 +155,41 @@ void main() {
154155
});
155156
}
156157

158+
/// Test the font weight found by [styleFinder] in the rendering of [content].
159+
///
160+
/// The weight will be expected to be [expectedWght] when the system
161+
/// bold-text setting is not set, and to vary appropriately when it is set.
162+
///
163+
/// [styleFinder] must return the [TextStyle] containing the "wght"
164+
/// (in [TextStyle.fontVariations]) and the [TextStyle.fontWeight]
165+
/// to be checked.
166+
Future<void> testFontWeight(String description, {
167+
required Widget content,
168+
required double expectedWght,
169+
required TextStyle Function(WidgetTester tester) styleFinder,
170+
}) async {
171+
for (final platformRequestsBold in [false, true]) {
172+
testWidgets(
173+
description + (platformRequestsBold ? ' (platform requests bold)' : ''),
174+
(tester) async {
175+
tester.platformDispatcher.accessibilityFeaturesTestValue =
176+
FakeAccessibilityFeatures(boldText: platformRequestsBold);
177+
await prepareContent(tester, content);
178+
final style = styleFinder(tester);
179+
double effectiveExpectedWght = expectedWght;
180+
if (platformRequestsBold) {
181+
// bolderWght because that's what [weightVariableTextStyle] uses
182+
effectiveExpectedWght = bolderWght(expectedWght);
183+
}
184+
check(style)
185+
..fontVariations.isNotNull()
186+
.any((it) => it..axis.equals('wght')..value.equals(effectiveExpectedWght))
187+
..fontWeight.equals(clampVariableFontWeight(effectiveExpectedWght));
188+
tester.platformDispatcher.clearAccessibilityFeaturesTestValue();
189+
});
190+
}
191+
}
192+
157193
group('ThematicBreak', () {
158194
testWidgets('smoke ThematicBreak', (tester) async {
159195
await prepareContent(tester, plainContent(ContentExample.thematicBreak.html));
@@ -471,6 +507,18 @@ void main() {
471507

472508
group('strong (bold)', () {
473509
testContentSmoke(ContentExample.strong);
510+
511+
TextStyle findWordBold(WidgetTester tester) {
512+
final root = tester.renderObject<RenderParagraph>(find.textContaining('bold')).text;
513+
return mergedStyleOfSubstring(root, 'bold')!;
514+
}
515+
516+
testFontWeight('in plain paragraph',
517+
expectedWght: 600,
518+
// **bold**
519+
content: plainContent('<p><strong>bold</strong></p>'),
520+
styleFinder: findWordBold,
521+
);
474522
});
475523

476524
testContentSmoke(ContentExample.emphasis);
@@ -520,6 +568,34 @@ void main() {
520568
return style.fontSize!;
521569
});
522570
});
571+
572+
testFontWeight('silent or non-self mention in plain paragraph',
573+
expectedWght: 400,
574+
// @_**Greg Price**
575+
content: plainContent(
576+
'<p><span class="user-mention silent" data-user-id="2187">Greg Price</span></p>'),
577+
styleFinder: (tester) {
578+
return textStyleFromWidget(tester,
579+
tester.widget(find.byType(UserMention)), 'Greg Price');
580+
});
581+
582+
// TODO(#647):
583+
// testFontWeight('non-silent self-user mention in plain paragraph',
584+
// expectedWght: 600, // [etc.]
585+
586+
testFontWeight('silent or non-self mention in bold context',
587+
expectedWght: 600,
588+
// # @_**Chris Bobbe**
589+
content: plainContent(
590+
'<h1><span class="user-mention silent" data-user-id="13313">Chris Bobbe</span></h1>'),
591+
styleFinder: (tester) {
592+
return textStyleFromWidget(tester,
593+
tester.widget(find.byType(UserMention)), 'Chris Bobbe');
594+
});
595+
596+
// TODO(#647):
597+
// testFontWeight('non-silent self-user mention in bold context',
598+
// expectedWght: 800, // [etc.]
523599
});
524600

525601
Future<void> tapText(WidgetTester tester, Finder textFinder) async {

0 commit comments

Comments
 (0)