typography_test.dart 3.61 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
9
  test('TextTheme control test', () {
10
    final Typography typography = new Typography(platform: TargetPlatform.android);
11 12 13 14 15 16
    expect(typography.black, equals(typography.black.copyWith()));
    expect(typography.black, equals(typography.black.apply()));
    expect(typography.black.hashCode, equals(typography.black.copyWith().hashCode));
    expect(typography.black, isNot(equals(typography.white)));
  });

17 18
  test('Typography is defined for all target platforms', () {
    for (TargetPlatform platform in TargetPlatform.values) {
19
      final Typography typography = new Typography(platform: platform);
20 21 22 23 24 25
      expect(typography, isNotNull, reason: 'null typography for $platform');
      expect(typography.black, isNotNull, reason: 'null black typography for $platform');
      expect(typography.white, isNotNull, reason: 'null white typography for $platform');
    }
  });

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  test('TextTheme merges properly in the presence of null fields.', () {
    const TextTheme partialTheme = const TextTheme(title: const TextStyle(color: const Color(0xcafefeed)));
    final TextTheme fullTheme = ThemeData.fallback().textTheme.merge(partialTheme);
    expect(fullTheme.title.color, equals(partialTheme.title.color));

    const TextTheme onlyHeadlineAndTitle = const TextTheme(
      headline: const TextStyle(color: const Color(0xcafefeed)),
      title: const TextStyle(color: const Color(0xbeefcafe)),
    );
    const TextTheme onlyBody1AndTitle = const TextTheme(
      body1: const TextStyle(color: const Color(0xfeedfeed)),
      title: const TextStyle(color: const Color(0xdeadcafe)),
    );
    TextTheme merged = onlyHeadlineAndTitle.merge(onlyBody1AndTitle);
    expect(merged.body2, isNull);
    expect(merged.body1.color, equals(onlyBody1AndTitle.body1.color));
    expect(merged.headline.color, equals(onlyHeadlineAndTitle.headline.color));
    expect(merged.title.color, equals(onlyBody1AndTitle.title.color));

    merged = onlyHeadlineAndTitle.merge(null);
    expect(merged, equals(onlyHeadlineAndTitle));
  });

49 50 51 52 53 54 55
  test('Typography on Android, Fuchsia defaults to Roboto', () {
    expect(new Typography(platform: TargetPlatform.android).black.title.fontFamily, 'Roboto');
    expect(new Typography(platform: TargetPlatform.fuchsia).black.title.fontFamily, 'Roboto');
  });

  test('Typography on iOS defaults to the correct SF font family based on size', () {
    // Ref: https://developer.apple.com/ios/human-interface-guidelines/visual-design/typography/
56 57 58 59 60 61 62
    final Matcher isDisplayFont = predicate((TextStyle s) {
      return s.fontFamily == '.SF UI Display';
    }, 'Uses SF Display font');

    final Matcher isTextFont = predicate((TextStyle s) {
      return s.fontFamily == '.SF UI Text';
    }, 'Uses SF Text font');
63

64
    final Typography typography = new Typography(platform: TargetPlatform.iOS);
65
    for (TextTheme textTheme in <TextTheme>[typography.black, typography.white]) {
66 67 68 69 70 71 72 73 74 75 76
      expect(textTheme.display4, isDisplayFont);
      expect(textTheme.display3, isDisplayFont);
      expect(textTheme.display2, isDisplayFont);
      expect(textTheme.display1, isDisplayFont);
      expect(textTheme.headline, isDisplayFont);
      expect(textTheme.title, isDisplayFont);
      expect(textTheme.subhead, isTextFont);
      expect(textTheme.body2, isTextFont);
      expect(textTheme.body1, isTextFont);
      expect(textTheme.caption, isTextFont);
      expect(textTheme.button, isTextFont);
77 78 79
    }
  });
}