text_style_test.dart 25.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Ian Hickson's avatar
Ian Hickson committed
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:ui' as ui show FontFeature, FontVariation, ParagraphStyle, Shadow, TextStyle;
Ian Hickson's avatar
Ian Hickson committed
6

7
import 'package:flutter/foundation.dart';
Ian Hickson's avatar
Ian Hickson committed
8
import 'package:flutter/painting.dart';
9
import 'package:flutter_test/flutter_test.dart';
Ian Hickson's avatar
Ian Hickson committed
10

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// This matcher verifies ui.TextStyle.toString (from dart:ui) reports a superset
// of the given TextStyle's (from painting.dart) properties.
class _DartUiTextStyleToStringMatcher extends Matcher {
  _DartUiTextStyleToStringMatcher(this.textStyle);

  final TextStyle textStyle;

  late final List<String> propertiesInOrder = <String>[
    _propertyToString('color', textStyle.color),
    _propertyToString('decoration', textStyle.decoration),
    _propertyToString('decorationColor', textStyle.decorationColor),
    _propertyToString('decorationStyle', textStyle.decorationStyle),
    _propertyToString('decorationThickness', textStyle.decorationThickness),
    _propertyToString('fontWeight', textStyle.fontWeight),
    _propertyToString('fontStyle', textStyle.fontStyle),
    _propertyToString('textBaseline', textStyle.textBaseline),
    _propertyToString('fontFamily', textStyle.fontFamily),
    _propertyToString('fontFamilyFallback', textStyle.fontFamilyFallback),
    _propertyToString('fontSize', textStyle.fontSize),
    _propertyToString('letterSpacing', textStyle.letterSpacing),
    _propertyToString('wordSpacing', textStyle.wordSpacing),
    _propertyToString('height', textStyle.height),
33 34 35
    // TODO(LongCatIsLooong): web support for
    // https://github.com/flutter/flutter/issues/72521
    if (!kIsWeb) _propertyToString('leadingDistribution', textStyle.leadingDistribution),
36 37 38 39 40
    _propertyToString('locale', textStyle.locale),
    _propertyToString('background', textStyle.background),
    _propertyToString('foreground', textStyle.foreground),
    _propertyToString('shadows', textStyle.shadows),
    _propertyToString('fontFeatures', textStyle.fontFeatures),
41
    _propertyToString('fontVariations', textStyle.fontVariations),
42 43 44 45 46 47 48 49 50 51 52 53
  ];

  static String _propertyToString(String name, Object? property) => '$name: ${property ?? 'unspecified'}';

  @override
  Description describe(Description description) => description.add('is a superset of $textStyle.');

  @override
  bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
    final String description = item.toString();
    const String prefix = 'TextStyle(';
    const String suffix = ')';
54
    if (!description.startsWith(prefix) || !description.endsWith(suffix)) {
55
      return false;
56
    }
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

    final String propertyDescription = description.substring(
      prefix.length,
      description.length - suffix.length,
    );
    int startIndex = 0;
    for (final String property in propertiesInOrder) {
      startIndex = propertyDescription.indexOf(property, startIndex);
      if (startIndex < 0) {
        matchState['missingProperty'] = property;
        return false;
      }
      startIndex += property.length;
    }
    return true;
  }

  @override
  Description describeMismatch(dynamic item, Description mismatchDescription, Map<dynamic, dynamic> matchState, bool verbose) {
    final Description description = super.describeMismatch(item, mismatchDescription, matchState, verbose);
    final String? property = matchState['missingProperty'] as String?;
    if (property != null) {
      description.add("expect property: '$property'");
      final int propertyIndex = propertiesInOrder.indexOf(property);
      if (propertyIndex > 0) {
        description.add(" after: '${propertiesInOrder[propertyIndex - 1]}'");
      }
      description.add('\n');
    }
    return description;
  }
88 89
}

90 91
Matcher matchesToStringOf(TextStyle textStyle) => _DartUiTextStyleToStringMatcher(textStyle);

Ian Hickson's avatar
Ian Hickson committed
92
void main() {
Ian Hickson's avatar
Ian Hickson committed
93
  test('TextStyle control test', () {
94 95 96 97 98
    expect(
      const TextStyle(inherit: false).toString(),
      equals('TextStyle(inherit: false, <no style specified>)'),
    );
    expect(
99
      const TextStyle().toString(),
100 101
      equals('TextStyle(<all styles inherited>)'),
    );
102

103
    const TextStyle s1 = TextStyle(
Ian Hickson's avatar
Ian Hickson committed
104 105 106 107 108 109 110 111 112
      fontSize: 10.0,
      fontWeight: FontWeight.w800,
      height: 123.0,
    );
    expect(s1.fontFamily, isNull);
    expect(s1.fontSize, 10.0);
    expect(s1.fontWeight, FontWeight.w800);
    expect(s1.height, 123.0);
    expect(s1, equals(s1));
113 114
    expect(
      s1.toString(),
115
      equals('TextStyle(inherit: true, size: 10.0, weight: 800, height: 123.0x)'),
116
    );
Ian Hickson's avatar
Ian Hickson committed
117

118 119 120 121 122 123
    // Check that the inherit flag can be set with copyWith().
    expect(
      s1.copyWith(inherit: false).toString(),
      equals('TextStyle(inherit: false, size: 10.0, weight: 800, height: 123.0x)'),
    );

124 125 126 127 128
    final TextStyle s2 = s1.copyWith(
      color: const Color(0xFF00FF00),
      height: 100.0,
      leadingDistribution: TextLeadingDistribution.even,
    );
Ian Hickson's avatar
Ian Hickson committed
129 130 131 132 133 134 135 136 137 138
    expect(s1.fontFamily, isNull);
    expect(s1.fontSize, 10.0);
    expect(s1.fontWeight, FontWeight.w800);
    expect(s1.height, 123.0);
    expect(s1.color, isNull);
    expect(s2.fontFamily, isNull);
    expect(s2.fontSize, 10.0);
    expect(s2.fontWeight, FontWeight.w800);
    expect(s2.height, 100.0);
    expect(s2.color, const Color(0xFF00FF00));
139
    expect(s2.leadingDistribution, TextLeadingDistribution.even);
Ian Hickson's avatar
Ian Hickson committed
140
    expect(s2, isNot(equals(s1)));
141 142 143
    expect(
      s2.toString(),
      equals(
144
        'TextStyle(inherit: true, color: Color(0xff00ff00), size: 10.0, weight: 800, height: 100.0x, leadingDistribution: even)',
145 146
      ),
    );
Ian Hickson's avatar
Ian Hickson committed
147

148
    final TextStyle s3 = s1.apply(fontSizeFactor: 2.0, fontSizeDelta: -2.0, fontWeightDelta: -4);
Ian Hickson's avatar
Ian Hickson committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    expect(s1.fontFamily, isNull);
    expect(s1.fontSize, 10.0);
    expect(s1.fontWeight, FontWeight.w800);
    expect(s1.height, 123.0);
    expect(s1.color, isNull);
    expect(s3.fontFamily, isNull);
    expect(s3.fontSize, 18.0);
    expect(s3.fontWeight, FontWeight.w400);
    expect(s3.height, 123.0);
    expect(s3.color, isNull);
    expect(s3, isNot(equals(s1)));

    expect(s1.apply(fontWeightDelta: -10).fontWeight, FontWeight.w100);
    expect(s1.apply(fontWeightDelta: 2).fontWeight, FontWeight.w900);
    expect(s1.merge(null), equals(s1));

165
    final TextStyle s4 = s2.merge(s1);
Ian Hickson's avatar
Ian Hickson committed
166 167 168 169 170 171 172 173 174 175
    expect(s1.fontFamily, isNull);
    expect(s1.fontSize, 10.0);
    expect(s1.fontWeight, FontWeight.w800);
    expect(s1.height, 123.0);
    expect(s1.color, isNull);
    expect(s2.fontFamily, isNull);
    expect(s2.fontSize, 10.0);
    expect(s2.fontWeight, FontWeight.w800);
    expect(s2.height, 100.0);
    expect(s2.color, const Color(0xFF00FF00));
176
    expect(s2.leadingDistribution, TextLeadingDistribution.even);
Ian Hickson's avatar
Ian Hickson committed
177 178 179 180 181 182 183
    expect(s2, isNot(equals(s1)));
    expect(s2, isNot(equals(s4)));
    expect(s4.fontFamily, isNull);
    expect(s4.fontSize, 10.0);
    expect(s4.fontWeight, FontWeight.w800);
    expect(s4.height, 123.0);
    expect(s4.color, const Color(0xFF00FF00));
184
    expect(s4.leadingDistribution, TextLeadingDistribution.even);
Ian Hickson's avatar
Ian Hickson committed
185

186
    final TextStyle s5 = TextStyle.lerp(s1, s3, 0.25)!;
Ian Hickson's avatar
Ian Hickson committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    expect(s1.fontFamily, isNull);
    expect(s1.fontSize, 10.0);
    expect(s1.fontWeight, FontWeight.w800);
    expect(s1.height, 123.0);
    expect(s1.color, isNull);
    expect(s3.fontFamily, isNull);
    expect(s3.fontSize, 18.0);
    expect(s3.fontWeight, FontWeight.w400);
    expect(s3.height, 123.0);
    expect(s3.color, isNull);
    expect(s3, isNot(equals(s1)));
    expect(s3, isNot(equals(s5)));
    expect(s5.fontFamily, isNull);
    expect(s5.fontSize, 12.0);
    expect(s5.fontWeight, FontWeight.w700);
    expect(s5.height, 123.0);
    expect(s5.color, isNull);

205 206
    expect(TextStyle.lerp(null, null, 0.5), isNull);

207
    final TextStyle s6 = TextStyle.lerp(null, s3, 0.25)!;
208 209 210 211 212 213 214 215 216 217 218 219
    expect(s3.fontFamily, isNull);
    expect(s3.fontSize, 18.0);
    expect(s3.fontWeight, FontWeight.w400);
    expect(s3.height, 123.0);
    expect(s3.color, isNull);
    expect(s3, isNot(equals(s6)));
    expect(s6.fontFamily, isNull);
    expect(s6.fontSize, isNull);
    expect(s6.fontWeight, FontWeight.w400);
    expect(s6.height, isNull);
    expect(s6.color, isNull);

220
    final TextStyle s7 = TextStyle.lerp(null, s3, 0.75)!;
221 222 223 224 225 226 227 228 229 230 231 232
    expect(s3.fontFamily, isNull);
    expect(s3.fontSize, 18.0);
    expect(s3.fontWeight, FontWeight.w400);
    expect(s3.height, 123.0);
    expect(s3.color, isNull);
    expect(s3, equals(s7));
    expect(s7.fontFamily, isNull);
    expect(s7.fontSize, 18.0);
    expect(s7.fontWeight, FontWeight.w400);
    expect(s7.height, 123.0);
    expect(s7.color, isNull);

233
    final TextStyle s8 = TextStyle.lerp(s3, null, 0.25)!;
234 235 236 237 238 239 240 241 242 243 244 245
    expect(s3.fontFamily, isNull);
    expect(s3.fontSize, 18.0);
    expect(s3.fontWeight, FontWeight.w400);
    expect(s3.height, 123.0);
    expect(s3.color, isNull);
    expect(s3, equals(s8));
    expect(s8.fontFamily, isNull);
    expect(s8.fontSize, 18.0);
    expect(s8.fontWeight, FontWeight.w400);
    expect(s8.height, 123.0);
    expect(s8.color, isNull);

246
    final TextStyle s9 = TextStyle.lerp(s3, null, 0.75)!;
247 248 249 250 251 252 253 254 255 256 257 258
    expect(s3.fontFamily, isNull);
    expect(s3.fontSize, 18.0);
    expect(s3.fontWeight, FontWeight.w400);
    expect(s3.height, 123.0);
    expect(s3.color, isNull);
    expect(s3, isNot(equals(s9)));
    expect(s9.fontFamily, isNull);
    expect(s9.fontSize, isNull);
    expect(s9.fontWeight, FontWeight.w400);
    expect(s9.height, isNull);
    expect(s9.color, isNull);

259
    final ui.TextStyle ts5 = s5.getTextStyle();
260
    expect(ts5, equals(ui.TextStyle(fontWeight: FontWeight.w700, fontSize: 12.0, height: 123.0)));
261
    expect(ts5, matchesToStringOf(s5));
262
    final ui.TextStyle ts2 = s2.getTextStyle();
263
    expect(ts2, equals(ui.TextStyle(color: const Color(0xFF00FF00), fontWeight: FontWeight.w800, fontSize: 10.0, height: 100.0, leadingDistribution: TextLeadingDistribution.even)));
264
    expect(ts2, matchesToStringOf(s2));
Ian Hickson's avatar
Ian Hickson committed
265

266
    final ui.ParagraphStyle ps2 = s2.getParagraphStyle(textAlign: TextAlign.center);
267 268 269 270
    expect(
      ps2,
      equals(ui.ParagraphStyle(textAlign: TextAlign.center, fontWeight: FontWeight.w800, fontSize: 10.0, height: 100.0, textHeightBehavior: const TextHeightBehavior(leadingDistribution: TextLeadingDistribution.even))),
    );
271
    final ui.ParagraphStyle ps5 = s5.getParagraphStyle();
272 273 274 275
    expect(
      ps5,
      equals(ui.ParagraphStyle(fontWeight: FontWeight.w700, fontSize: 12.0, height: 123.0)),
    );
276
  });
Ian Hickson's avatar
Ian Hickson committed
277

278
  test('TextStyle with text direction', () {
Ian Hickson's avatar
Ian Hickson committed
279
    final ui.ParagraphStyle ps6 = const TextStyle().getParagraphStyle(textDirection: TextDirection.ltr);
280
    expect(ps6, equals(ui.ParagraphStyle(textDirection: TextDirection.ltr, fontSize: 14.0)));
Ian Hickson's avatar
Ian Hickson committed
281 282

    final ui.ParagraphStyle ps7 = const TextStyle().getParagraphStyle(textDirection: TextDirection.rtl);
283
    expect(ps7, equals(ui.ParagraphStyle(textDirection: TextDirection.rtl, fontSize: 14.0)));
Ian Hickson's avatar
Ian Hickson committed
284
  });
285 286

  test('TextStyle using package font', () {
287
    const TextStyle s6 = TextStyle(fontFamily: 'test');
288
    expect(s6.fontFamily, 'test');
289
    expect(s6.getTextStyle(), matchesToStringOf(s6));
290

291
    const TextStyle s7 = TextStyle(fontFamily: 'test', package: 'p');
292
    expect(s7.fontFamily, 'packages/p/test');
293
    expect(s7.getTextStyle(), matchesToStringOf(s7));
294 295

    const TextStyle s8 = TextStyle(fontFamilyFallback: <String>['test', 'test2'], package: 'p');
296 297 298
    expect(s8.fontFamilyFallback![0], 'packages/p/test');
    expect(s8.fontFamilyFallback![1], 'packages/p/test2');
    expect(s8.fontFamilyFallback!.length, 2);
299 300 301 302 303 304

    const TextStyle s9 = TextStyle(package: 'p');
    expect(s9.fontFamilyFallback, null);

    const TextStyle s10 = TextStyle(fontFamilyFallback: <String>[], package: 'p');
    expect(s10.fontFamilyFallback, <String>[]);
305 306 307 308 309 310 311 312 313 314 315 316 317 318

    // Ensure that package prefix is not duplicated after copying.
    final TextStyle s11 = s8.copyWith();
    expect(s11.fontFamilyFallback![0], 'packages/p/test');
    expect(s11.fontFamilyFallback![1], 'packages/p/test2');
    expect(s11.fontFamilyFallback!.length, 2);
    expect(s8, s11);

    // Ensure that package prefix is not duplicated after applying.
    final TextStyle s12 = s8.apply();
    expect(s12.fontFamilyFallback![0], 'packages/p/test');
    expect(s12.fontFamilyFallback![1], 'packages/p/test2');
    expect(s12.fontFamilyFallback!.length, 2);
    expect(s8, s12);
319
  });
320

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
  test('TextStyle package font merge', () {
    const TextStyle s1 = TextStyle(package: 'p', fontFamily: 'font1', fontFamilyFallback: <String>['fallback1']);
    const TextStyle s2 = TextStyle(package: 'p', fontFamily: 'font2', fontFamilyFallback: <String>['fallback2']);

    final TextStyle emptyMerge = const TextStyle().merge(s1);
    expect(emptyMerge.fontFamily, 'packages/p/font1');
    expect(emptyMerge.fontFamilyFallback, <String>['packages/p/fallback1']);

    final TextStyle lerp1 = TextStyle.lerp(s1, s2, 0)!;
    expect(lerp1.fontFamily, 'packages/p/font1');
    expect(lerp1.fontFamilyFallback, <String>['packages/p/fallback1']);

    final TextStyle lerp2 = TextStyle.lerp(s1, s2, 1.0)!;
    expect(lerp2.fontFamily, 'packages/p/font2');
    expect(lerp2.fontFamilyFallback, <String>['packages/p/fallback2']);
  });

338 339
  test('TextStyle font family fallback', () {
    const TextStyle s1 = TextStyle(fontFamilyFallback: <String>['Roboto', 'test']);
340 341 342
    expect(s1.fontFamilyFallback![0], 'Roboto');
    expect(s1.fontFamilyFallback![1], 'test');
    expect(s1.fontFamilyFallback!.length, 2);
343 344

    const TextStyle s2 = TextStyle(fontFamily: 'foo', fontFamilyFallback: <String>['Roboto', 'test']);
345 346
    expect(s2.fontFamilyFallback![0], 'Roboto');
    expect(s2.fontFamilyFallback![1], 'test');
347
    expect(s2.fontFamily, 'foo');
348
    expect(s2.fontFamilyFallback!.length, 2);
349 350 351 352 353 354 355 356

    const TextStyle s3 = TextStyle(fontFamily: 'foo');
    expect(s3.fontFamily, 'foo');
    expect(s3.fontFamilyFallback, null);

    const TextStyle s4 = TextStyle(fontFamily: 'foo', fontFamilyFallback: <String>[]);
    expect(s4.fontFamily, 'foo');
    expect(s4.fontFamilyFallback, <String>[]);
357
    expect(s4.fontFamilyFallback!.isEmpty, true);
358 359

    final ui.TextStyle uis1 = s2.getTextStyle();
360
    expect(uis1, matchesToStringOf(s2));
361 362 363 364 365

    expect(s2.apply().fontFamily, 'foo');
    expect(s2.apply().fontFamilyFallback, const <String>['Roboto', 'test']);
    expect(s2.apply(fontFamily: 'bar').fontFamily, 'bar');
    expect(s2.apply(fontFamilyFallback: const <String>['Banana']).fontFamilyFallback, const <String>['Banana']);
366
  });
367 368

  test('TextStyle.debugLabel', () {
369 370 371 372
    const TextStyle unknown = TextStyle();
    const TextStyle foo = TextStyle(debugLabel: 'foo', fontSize: 1.0);
    const TextStyle bar = TextStyle(debugLabel: 'bar', fontSize: 2.0);
    const TextStyle baz = TextStyle(debugLabel: 'baz', fontSize: 3.0);
373 374 375 376 377 378 379 380

    expect(unknown.debugLabel, null);
    expect(unknown.toString(), 'TextStyle(<all styles inherited>)');
    expect(unknown.copyWith().debugLabel, null);
    expect(unknown.apply().debugLabel, null);

    expect(foo.debugLabel, 'foo');
    expect(foo.toString(), 'TextStyle(debugLabel: foo, inherit: true, size: 1.0)');
381 382 383 384
    expect(foo.merge(bar).debugLabel, '(foo).merge(bar)');
    expect(foo.merge(bar).merge(baz).debugLabel, '((foo).merge(bar)).merge(baz)');
    expect(foo.copyWith().debugLabel, '(foo).copyWith');
    expect(foo.apply().debugLabel, '(foo).apply');
385 386
    expect(TextStyle.lerp(foo, bar, 0.5)!.debugLabel, 'lerp(foo ⎯0.5→ bar)');
    expect(TextStyle.lerp(foo.merge(bar), baz, 0.51)!.copyWith().debugLabel, '(lerp((foo).merge(bar) ⎯0.5→ baz)).copyWith');
387
  });
388

389
  test('TextStyle.hashCode', () {
390 391 392 393 394 395 396 397 398 399 400 401
    const TextStyle a = TextStyle(
        fontFamilyFallback: <String>['Roboto'],
        shadows: <ui.Shadow>[ui.Shadow()],
        fontFeatures: <ui.FontFeature>[ui.FontFeature('abcd')],
        fontVariations: <ui.FontVariation>[ui.FontVariation('wght', 123.0)],
    );
    const TextStyle b = TextStyle(
        fontFamilyFallback: <String>['Noto'],
        shadows: <ui.Shadow>[ui.Shadow()],
        fontFeatures: <ui.FontFeature>[ui.FontFeature('abcd')],
        fontVariations: <ui.FontVariation>[ui.FontVariation('wght', 123.0)],
    );
402 403
    expect(a.hashCode, a.hashCode);
    expect(a.hashCode, isNot(equals(b.hashCode)));
404 405 406 407 408

    const TextStyle c = TextStyle(leadingDistribution: TextLeadingDistribution.even);
    const TextStyle d = TextStyle(leadingDistribution: TextLeadingDistribution.proportional);
    expect(c.hashCode, c.hashCode);
    expect(c.hashCode, isNot(d.hashCode));
409
  });
410

411
  test('TextStyle foreground and color combos', () {
412 413 414 415
    const Color red = Color.fromARGB(255, 255, 0, 0);
    const Color blue = Color.fromARGB(255, 0, 0, 255);
    const TextStyle redTextStyle = TextStyle(color: red);
    const TextStyle blueTextStyle = TextStyle(color: blue);
416 417
    final TextStyle redPaintTextStyle = TextStyle(foreground: Paint()..color = red);
    final TextStyle bluePaintTextStyle = TextStyle(foreground: Paint()..color = blue);
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

    // merge/copyWith
    final TextStyle redBlueBothForegroundMerged = redTextStyle.merge(blueTextStyle);
    expect(redBlueBothForegroundMerged.color, blue);
    expect(redBlueBothForegroundMerged.foreground, isNull);

    final TextStyle redBlueBothPaintMerged = redPaintTextStyle.merge(bluePaintTextStyle);
    expect(redBlueBothPaintMerged.color, null);
    expect(redBlueBothPaintMerged.foreground, bluePaintTextStyle.foreground);

    final TextStyle redPaintBlueColorMerged = redPaintTextStyle.merge(blueTextStyle);
    expect(redPaintBlueColorMerged.color, null);
    expect(redPaintBlueColorMerged.foreground, redPaintTextStyle.foreground);

    final TextStyle blueColorRedPaintMerged = blueTextStyle.merge(redPaintTextStyle);
    expect(blueColorRedPaintMerged.color, null);
    expect(blueColorRedPaintMerged.foreground, redPaintTextStyle.foreground);

    // apply
    expect(redPaintTextStyle.apply(color: blue).color, isNull);
438
    expect(redPaintTextStyle.apply(color: blue).foreground!.color, red);
439 440 441
    expect(redTextStyle.apply(color: blue).color, blue);

    // lerp
442 443 444 445 446 447 448 449
    expect(TextStyle.lerp(redTextStyle, blueTextStyle, .25)!.color, Color.lerp(red, blue, .25));
    expect(TextStyle.lerp(redTextStyle, bluePaintTextStyle, .25)!.color, isNull);
    expect(TextStyle.lerp(redTextStyle, bluePaintTextStyle, .25)!.foreground!.color, red);
    expect(TextStyle.lerp(redTextStyle, bluePaintTextStyle, .75)!.foreground!.color, blue);

    expect(TextStyle.lerp(redPaintTextStyle, bluePaintTextStyle, .25)!.color, isNull);
    expect(TextStyle.lerp(redPaintTextStyle, bluePaintTextStyle, .25)!.foreground!.color, red);
    expect(TextStyle.lerp(redPaintTextStyle, bluePaintTextStyle, .75)!.foreground!.color, blue);
450
  });
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491

  test('backgroundColor', () {
    const TextStyle s1 = TextStyle();
    expect(s1.backgroundColor, isNull);
    expect(s1.toString(), 'TextStyle(<all styles inherited>)');

    const TextStyle s2 = TextStyle(backgroundColor: Color(0xFF00FF00));
    expect(s2.backgroundColor, const Color(0xFF00FF00));
    expect(s2.toString(), 'TextStyle(inherit: true, backgroundColor: Color(0xff00ff00))');

    final ui.TextStyle ts2 = s2.getTextStyle();
    expect(ts2.toString(), contains('background: Paint(Color(0xff00ff00))'));
  });

  test('TextStyle background and backgroundColor combos', () {
    const Color red = Color.fromARGB(255, 255, 0, 0);
    const Color blue = Color.fromARGB(255, 0, 0, 255);
    const TextStyle redTextStyle = TextStyle(backgroundColor: red);
    const TextStyle blueTextStyle = TextStyle(backgroundColor: blue);
    final TextStyle redPaintTextStyle = TextStyle(background: Paint()..color = red);
    final TextStyle bluePaintTextStyle = TextStyle(background: Paint()..color = blue);

    // merge/copyWith
    final TextStyle redBlueBothForegroundMerged = redTextStyle.merge(blueTextStyle);
    expect(redBlueBothForegroundMerged.backgroundColor, blue);
    expect(redBlueBothForegroundMerged.foreground, isNull);

    final TextStyle redBlueBothPaintMerged = redPaintTextStyle.merge(bluePaintTextStyle);
    expect(redBlueBothPaintMerged.backgroundColor, null);
    expect(redBlueBothPaintMerged.background, bluePaintTextStyle.background);

    final TextStyle redPaintBlueColorMerged = redPaintTextStyle.merge(blueTextStyle);
    expect(redPaintBlueColorMerged.backgroundColor, null);
    expect(redPaintBlueColorMerged.background, redPaintTextStyle.background);

    final TextStyle blueColorRedPaintMerged = blueTextStyle.merge(redPaintTextStyle);
    expect(blueColorRedPaintMerged.backgroundColor, null);
    expect(blueColorRedPaintMerged.background, redPaintTextStyle.background);

    // apply
    expect(redPaintTextStyle.apply(backgroundColor: blue).backgroundColor, isNull);
492
    expect(redPaintTextStyle.apply(backgroundColor: blue).background!.color, red);
493 494 495
    expect(redTextStyle.apply(backgroundColor: blue).backgroundColor, blue);

    // lerp
496 497 498 499 500 501 502 503
    expect(TextStyle.lerp(redTextStyle, blueTextStyle, .25)!.backgroundColor, Color.lerp(red, blue, .25));
    expect(TextStyle.lerp(redTextStyle, bluePaintTextStyle, .25)!.backgroundColor, isNull);
    expect(TextStyle.lerp(redTextStyle, bluePaintTextStyle, .25)!.background!.color, red);
    expect(TextStyle.lerp(redTextStyle, bluePaintTextStyle, .75)!.background!.color, blue);

    expect(TextStyle.lerp(redPaintTextStyle, bluePaintTextStyle, .25)!.backgroundColor, isNull);
    expect(TextStyle.lerp(redPaintTextStyle, bluePaintTextStyle, .25)!.background!.color, red);
    expect(TextStyle.lerp(redPaintTextStyle, bluePaintTextStyle, .75)!.background!.color, blue);
504
  });
505 506 507 508 509 510

  test('TextStyle strut textScaleFactor', () {
    const TextStyle style0 = TextStyle(fontSize: 10);
    final ui.ParagraphStyle paragraphStyle0 = style0.getParagraphStyle(textScaleFactor: 2.5);

    const TextStyle style1 = TextStyle(fontSize: 25);
511
    final ui.ParagraphStyle paragraphStyle1 = style1.getParagraphStyle();
512 513 514

    expect(paragraphStyle0 == paragraphStyle1, true);
  });
515 516

  test('TextStyle apply', () {
517 518 519 520 521
    const TextStyle style = TextStyle(
      fontSize: 10,
      shadows: <ui.Shadow>[],
      fontStyle: FontStyle.normal,
      fontFeatures: <ui.FontFeature>[],
522
      fontVariations: <ui.FontVariation>[],
523 524 525
      textBaseline: TextBaseline.alphabetic,
      leadingDistribution: TextLeadingDistribution.even,
    );
526
    expect(style.apply().shadows, const <ui.Shadow>[]);
527
    expect(style.apply(shadows: const <ui.Shadow>[ui.Shadow(blurRadius: 2.0)]).shadows, const <ui.Shadow>[ui.Shadow(blurRadius: 2.0)]);
528
    expect(style.apply().fontStyle, FontStyle.normal);
529 530 531
    expect(style.apply(fontStyle: FontStyle.italic).fontStyle, FontStyle.italic);
    expect(style.apply().locale, isNull);
    expect(style.apply(locale: const Locale.fromSubtags(languageCode: 'es')).locale, const Locale.fromSubtags(languageCode: 'es'));
532
    expect(style.apply().fontFeatures, const <ui.FontFeature>[]);
533
    expect(style.apply(fontFeatures: const <ui.FontFeature>[ui.FontFeature.enable('test')]).fontFeatures, const <ui.FontFeature>[ui.FontFeature.enable('test')]);
534 535
    expect(style.apply().fontVariations, const <ui.FontVariation>[]);
    expect(style.apply(fontVariations: const <ui.FontVariation>[ui.FontVariation('test', 100.0)]).fontVariations, const <ui.FontVariation>[ui.FontVariation('test', 100.0)]);
536 537
    expect(style.apply().textBaseline, TextBaseline.alphabetic);
    expect(style.apply(textBaseline: TextBaseline.ideographic).textBaseline, TextBaseline.ideographic);
538 539 540 541 542
    expect(style.apply().leadingDistribution, TextLeadingDistribution.even);
    expect(
      style.apply(leadingDistribution: TextLeadingDistribution.proportional).leadingDistribution,
      TextLeadingDistribution.proportional,
    );
543
  });
544 545 546 547 548 549 550 551 552 553 554 555 556

  test('TextStyle fontFamily and package', () {
    expect(const TextStyle(fontFamily: 'fontFamily', package: 'foo') != const TextStyle(fontFamily: 'fontFamily', package: 'bar'), true);
    expect(const TextStyle(fontFamily: 'fontFamily', package: 'foo').hashCode != const TextStyle(package: 'bar', fontFamily: 'fontFamily').hashCode, true);
    expect(const TextStyle(fontFamily: 'fontFamily').fontFamily, 'fontFamily');
    expect(const TextStyle(fontFamily: 'fontFamily').fontFamily, 'fontFamily');
    expect(const TextStyle(fontFamily: 'fontFamily').copyWith(package: 'bar').fontFamily, 'packages/bar/fontFamily');
    expect(const TextStyle(fontFamily: 'fontFamily', package: 'foo').fontFamily, 'packages/foo/fontFamily');
    expect(const TextStyle(fontFamily: 'fontFamily', package: 'foo').copyWith(package: 'bar').fontFamily, 'packages/bar/fontFamily');
    expect(const TextStyle().merge(const TextStyle(fontFamily: 'fontFamily', package: 'bar')).fontFamily, 'packages/bar/fontFamily');
    expect(const TextStyle().apply(fontFamily: 'fontFamily', package: 'foo').fontFamily, 'packages/foo/fontFamily');
    expect(const TextStyle(fontFamily: 'fontFamily', package: 'foo').apply(fontFamily: 'fontFamily', package: 'bar').fontFamily, 'packages/bar/fontFamily');
  });
557

558 559 560 561 562 563
  test('TextStyle.lerp identical a,b', () {
    expect(TextStyle.lerp(null, null, 0), null);
    const TextStyle style = TextStyle();
    expect(identical(TextStyle.lerp(style, style, 0.5), style), true);
  });

564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
  test('Throws when lerping between inherit:true and inherit:false with unspecified fields', () {
    const TextStyle fromStyle = TextStyle();
    const TextStyle toStyle = TextStyle(inherit: false);
    expect(
      () => TextStyle.lerp(fromStyle, toStyle, 0.5),
      throwsFlutterError,
    );
    expect(TextStyle.lerp(fromStyle, fromStyle, 0.5), fromStyle);
  });

  test('Does not throw when lerping between inherit:true and inherit:false but fully specified styles', () {
    const TextStyle fromStyle = TextStyle();
    const TextStyle toStyle = TextStyle(
      inherit: false,
      color: Color(0x87654321),
      backgroundColor: Color(0x12345678),
      fontSize: 20,
      letterSpacing: 1,
      wordSpacing: 1,
      height: 20,
      decorationColor: Color(0x11111111),
      decorationThickness: 5,
    );
    expect(TextStyle.lerp(fromStyle, toStyle, 1), toStyle);
  });

Ian Hickson's avatar
Ian Hickson committed
590
}