|
| 1 | +import 'dart:ui'; |
| 2 | + |
| 3 | +import 'package:checks/checks.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:zulip/widgets/text.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + group('weightVariableTextStyle', () { |
| 10 | + Future<void> runCheck( |
| 11 | + String description, |
| 12 | + { |
| 13 | + required TextStyle Function(BuildContext context) styleBuilder, |
| 14 | + bool platformRequestsBold = false, |
| 15 | + required List<FontVariation> expectedFontVariations, |
| 16 | + required FontWeight expectedFontWeight, |
| 17 | + } |
| 18 | + ) async { |
| 19 | + testWidgets(description, (WidgetTester tester) async { |
| 20 | + await tester.pumpWidget( |
| 21 | + MaterialApp( |
| 22 | + home: MediaQuery( |
| 23 | + data: MediaQueryData(boldText: platformRequestsBold), |
| 24 | + child: Builder(builder: (context) => Text('', style: styleBuilder(context)))))); |
| 25 | + |
| 26 | + final TextStyle? style = tester.widget<Text>(find.byType(Text)).style; |
| 27 | + check(style) |
| 28 | + .isNotNull() |
| 29 | + ..has((style) => style.inherit, 'inherit').isTrue() |
| 30 | + ..has((style) => style.fontVariations, 'fontVariations') |
| 31 | + .isNotNull() |
| 32 | + .deepEquals(expectedFontVariations) |
| 33 | + ..has((style) => style.fontWeight, 'fontWeight') |
| 34 | + .isNotNull() |
| 35 | + .equals(expectedFontWeight); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + runCheck('no context passed; default wght values', |
| 40 | + styleBuilder: (context) => weightVariableTextStyle(null), |
| 41 | + expectedFontVariations: const [FontVariation('wght', 400)], |
| 42 | + expectedFontWeight: FontWeight.normal); |
| 43 | + runCheck('no context passed; specific wght', |
| 44 | + styleBuilder: (context) => weightVariableTextStyle(null, wght: 225), |
| 45 | + expectedFontVariations: const [FontVariation('wght', 225)], |
| 46 | + expectedFontWeight: FontWeight.w200); |
| 47 | + |
| 48 | + runCheck('default values; platform does not request bold', |
| 49 | + styleBuilder: (context) => weightVariableTextStyle(context), |
| 50 | + platformRequestsBold: false, |
| 51 | + expectedFontVariations: const [FontVariation('wght', 400)], |
| 52 | + expectedFontWeight: FontWeight.normal); |
| 53 | + runCheck('default values; platform requests bold', |
| 54 | + styleBuilder: (context) => weightVariableTextStyle(context), |
| 55 | + platformRequestsBold: true, |
| 56 | + expectedFontVariations: const [FontVariation('wght', 700)], |
| 57 | + expectedFontWeight: FontWeight.bold); |
| 58 | + runCheck('specific values; platform does not request bold', |
| 59 | + styleBuilder: (context) => weightVariableTextStyle(context, wght: 475, wghtPlatformRequestsBold: 675), |
| 60 | + platformRequestsBold: false, |
| 61 | + expectedFontVariations: const [FontVariation('wght', 475)], |
| 62 | + expectedFontWeight: FontWeight.w500); |
| 63 | + runCheck('specific values; platform requests bold', |
| 64 | + platformRequestsBold: true, |
| 65 | + styleBuilder: (context) => weightVariableTextStyle(context, wght: 475, wghtPlatformRequestsBold: 675), |
| 66 | + expectedFontVariations: const [FontVariation('wght', 675)], |
| 67 | + expectedFontWeight: FontWeight.w700); |
| 68 | + }); |
| 69 | + |
| 70 | + test('clampVariableFontWeight', () { |
| 71 | + check(clampVariableFontWeight(1)) .equals(FontWeight.w100); |
| 72 | + check(clampVariableFontWeight(99)) .equals(FontWeight.w100); |
| 73 | + check(clampVariableFontWeight(100)) .equals(FontWeight.w100); |
| 74 | + check(clampVariableFontWeight(101)) .equals(FontWeight.w100); |
| 75 | + |
| 76 | + check(clampVariableFontWeight(199)) .equals(FontWeight.w200); |
| 77 | + check(clampVariableFontWeight(200)) .equals(FontWeight.w200); |
| 78 | + check(clampVariableFontWeight(201)) .equals(FontWeight.w200); |
| 79 | + check(clampVariableFontWeight(250)) .equals(FontWeight.w200); |
| 80 | + |
| 81 | + check(clampVariableFontWeight(299)) .equals(FontWeight.w300); |
| 82 | + check(clampVariableFontWeight(300)) .equals(FontWeight.w300); |
| 83 | + check(clampVariableFontWeight(301)) .equals(FontWeight.w300); |
| 84 | + |
| 85 | + check(clampVariableFontWeight(399)) .equals(FontWeight.w400); |
| 86 | + check(clampVariableFontWeight(400)) .equals(FontWeight.w400); |
| 87 | + check(clampVariableFontWeight(401)) .equals(FontWeight.w400); |
| 88 | + |
| 89 | + check(clampVariableFontWeight(499)) .equals(FontWeight.w500); |
| 90 | + check(clampVariableFontWeight(500)) .equals(FontWeight.w500); |
| 91 | + check(clampVariableFontWeight(501)) .equals(FontWeight.w500); |
| 92 | + |
| 93 | + check(clampVariableFontWeight(599)) .equals(FontWeight.w600); |
| 94 | + check(clampVariableFontWeight(600)) .equals(FontWeight.w600); |
| 95 | + check(clampVariableFontWeight(601)) .equals(FontWeight.w600); |
| 96 | + |
| 97 | + check(clampVariableFontWeight(699)) .equals(FontWeight.w700); |
| 98 | + check(clampVariableFontWeight(700)) .equals(FontWeight.w700); |
| 99 | + check(clampVariableFontWeight(701)) .equals(FontWeight.w700); |
| 100 | + |
| 101 | + check(clampVariableFontWeight(799)) .equals(FontWeight.w800); |
| 102 | + check(clampVariableFontWeight(800)) .equals(FontWeight.w800); |
| 103 | + check(clampVariableFontWeight(801)) .equals(FontWeight.w800); |
| 104 | + |
| 105 | + check(clampVariableFontWeight(899)) .equals(FontWeight.w900); |
| 106 | + check(clampVariableFontWeight(900)) .equals(FontWeight.w900); |
| 107 | + check(clampVariableFontWeight(901)) .equals(FontWeight.w900); |
| 108 | + check(clampVariableFontWeight(999)) .equals(FontWeight.w900); |
| 109 | + check(clampVariableFontWeight(1000)) .equals(FontWeight.w900); |
| 110 | + }); |
| 111 | +} |
0 commit comments