text_theme_test.dart 9.79 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// 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 10 11 12 13 14 15 16 17
  test('TextTheme copyWith apply, merge basics with const TextTheme()', () {
    expect(const TextTheme(), equals(const TextTheme().copyWith()));
    expect(const TextTheme(), equals(const TextTheme().apply()));
    expect(const TextTheme(), equals(const TextTheme().merge(null)));
    expect(const TextTheme().hashCode, equals(const TextTheme().copyWith().hashCode));
    expect(const TextTheme(), equals(const TextTheme().copyWith()));
  });

  test('TextTheme copyWith apply, merge basics with Typography.black', () {
18
    final Typography typography = Typography.material2018();
19 20 21
    expect(typography.black, equals(typography.black.copyWith()));
    expect(typography.black, equals(typography.black.apply()));
    expect(typography.black, equals(typography.black.merge(null)));
22
    expect(typography.black, equals(const TextTheme().merge(typography.black)));
23 24 25 26 27 28 29
    expect(typography.black, equals(typography.black.merge(typography.black)));
    expect(typography.white, equals(typography.black.merge(typography.white)));
    expect(typography.black.hashCode, equals(typography.black.copyWith().hashCode));
    expect(typography.black, isNot(equals(typography.white)));
  });

  test('TextTheme copyWith', () {
30
    final Typography typography = Typography.material2018();
31
    final TextTheme whiteCopy = typography.black.copyWith(
32 33 34 35 36 37 38 39 40
      headline1: typography.white.headline1,
      headline2: typography.white.headline2,
      headline3: typography.white.headline3,
      headline4: typography.white.headline4,
      headline5: typography.white.headline5,
      headline6: typography.white.headline6,
      subtitle1: typography.white.subtitle1,
      bodyText1: typography.white.bodyText1,
      bodyText2: typography.white.bodyText2,
41 42
      caption: typography.white.caption,
      button: typography.white.button,
43
      subtitle2: typography.white.subtitle2,
44 45 46 47 48 49 50
      overline: typography.white.overline,
    );
    expect(typography.white, equals(whiteCopy));
  });


  test('TextTheme merges properly in the presence of null fields.', () {
51
    const TextTheme partialTheme = TextTheme(headline6: TextStyle(color: Color(0xcafefeed)));
52
    final TextTheme fullTheme = ThemeData.fallback().textTheme.merge(partialTheme);
53
    expect(fullTheme.headline6!.color, equals(partialTheme.headline6!.color));
54 55

    const TextTheme onlyHeadlineAndTitle = TextTheme(
56 57
      headline5: TextStyle(color: Color(0xcafefeed)),
      headline6: TextStyle(color: Color(0xbeefcafe)),
58 59
    );
    const TextTheme onlyBody1AndTitle = TextTheme(
60 61
      bodyText2: TextStyle(color: Color(0xfeedfeed)),
      headline6: TextStyle(color: Color(0xdeadcafe)),
62 63
    );
    TextTheme merged = onlyHeadlineAndTitle.merge(onlyBody1AndTitle);
64
    expect(merged.bodyText1, isNull);
65 66 67
    expect(merged.bodyText2!.color, equals(onlyBody1AndTitle.bodyText2!.color));
    expect(merged.headline5!.color, equals(onlyHeadlineAndTitle.headline5!.color));
    expect(merged.headline6!.color, equals(onlyBody1AndTitle.headline6!.color));
68 69 70 71 72 73

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

  test('TextTheme apply', () {
74 75
    // The `displayColor` is applied to [headline1], [headline2], [headline3],
    // [headline4], and [caption]. The `bodyColor` is applied to the remaining
76
    // text styles.
77 78
    const Color displayColor = Color(0x00000001);
    const Color bodyColor = Color(0x00000002);
79
    const String fontFamily = 'fontFamily';
80
    const Color decorationColor = Color(0x00000003);
81 82 83
    const TextDecorationStyle decorationStyle = TextDecorationStyle.dashed;
    final TextDecoration decoration = TextDecoration.combine(<TextDecoration>[
      TextDecoration.underline,
84
      TextDecoration.lineThrough,
85 86
    ]);

87
    final Typography typography = Typography.material2018();
88 89 90 91 92 93 94 95 96
    final TextTheme theme = typography.black.apply(
      fontFamily: fontFamily,
      displayColor: displayColor,
      bodyColor: bodyColor,
      decoration: decoration,
      decorationColor: decorationColor,
      decorationStyle: decorationStyle,
    );

97 98 99 100 101 102 103 104 105 106 107 108 109
    expect(theme.headline1!.color, displayColor);
    expect(theme.headline2!.color, displayColor);
    expect(theme.headline3!.color, displayColor);
    expect(theme.headline4!.color, displayColor);
    expect(theme.caption!.color, displayColor);
    expect(theme.headline5!.color, bodyColor);
    expect(theme.headline6!.color, bodyColor);
    expect(theme.subtitle1!.color, bodyColor);
    expect(theme.bodyText1!.color, bodyColor);
    expect(theme.bodyText2!.color, bodyColor);
    expect(theme.button!.color, bodyColor);
    expect(theme.subtitle2!.color, bodyColor);
    expect(theme.overline!.color, bodyColor);
110 111

    final List<TextStyle> themeStyles = <TextStyle>[
112 113 114 115 116 117 118 119 120 121 122 123 124
      theme.headline1!,
      theme.headline2!,
      theme.headline3!,
      theme.headline4!,
      theme.caption!,
      theme.headline5!,
      theme.headline6!,
      theme.subtitle1!,
      theme.bodyText1!,
      theme.bodyText2!,
      theme.button!,
      theme.subtitle2!,
      theme.overline!,
125 126 127 128 129 130 131 132
    ];
    expect(themeStyles.every((TextStyle style) => style.fontFamily == fontFamily), true);
    expect(themeStyles.every((TextStyle style) => style.decorationColor == decorationColor), true);
    expect(themeStyles.every((TextStyle style) => style.decorationStyle == decorationStyle), true);
    expect(themeStyles.every((TextStyle style) => style.decoration == decoration), true);
  });

  test('TextTheme apply fontSizeFactor fontSizeDelta', () {
133
    final Typography typography = Typography.material2018();
134
    final TextTheme baseTheme = Typography.englishLike2018.merge(typography.black);
135 136 137 138 139
    final TextTheme sizeTheme = baseTheme.apply(
      fontSizeFactor: 2.0,
      fontSizeDelta: 5.0,
    );

140 141 142 143 144 145 146 147 148 149 150 151 152
    expect(sizeTheme.headline1!.fontSize, baseTheme.headline1!.fontSize! * 2.0 + 5.0);
    expect(sizeTheme.headline2!.fontSize, baseTheme.headline2!.fontSize! * 2.0 + 5.0);
    expect(sizeTheme.headline3!.fontSize, baseTheme.headline3!.fontSize! * 2.0 + 5.0);
    expect(sizeTheme.headline4!.fontSize, baseTheme.headline4!.fontSize! * 2.0 + 5.0);
    expect(sizeTheme.caption!.fontSize, baseTheme.caption!.fontSize! * 2.0 + 5.0);
    expect(sizeTheme.headline5!.fontSize, baseTheme.headline5!.fontSize! * 2.0 + 5.0);
    expect(sizeTheme.headline6!.fontSize, baseTheme.headline6!.fontSize! * 2.0 + 5.0);
    expect(sizeTheme.subtitle1!.fontSize, baseTheme.subtitle1!.fontSize! * 2.0 + 5.0);
    expect(sizeTheme.bodyText1!.fontSize, baseTheme.bodyText1!.fontSize! * 2.0 + 5.0);
    expect(sizeTheme.bodyText2!.fontSize, baseTheme.bodyText2!.fontSize! * 2.0 + 5.0);
    expect(sizeTheme.button!.fontSize, baseTheme.button!.fontSize! * 2.0 + 5.0);
    expect(sizeTheme.subtitle2!.fontSize, baseTheme.subtitle2!.fontSize! * 2.0 + 5.0);
    expect(sizeTheme.overline!.fontSize, baseTheme.overline!.fontSize! * 2.0 + 5.0);
153 154
  });

155
  test('TextTheme lerp with second parameter null', () {
156
    final TextTheme theme = Typography.material2018().black;
157 158
    final TextTheme lerped = TextTheme.lerp(theme, null, 0.25);

159 160 161 162
    expect(lerped.headline1, TextStyle.lerp(theme.headline1, null, 0.25));
    expect(lerped.headline2, TextStyle.lerp(theme.headline2, null, 0.25));
    expect(lerped.headline3, TextStyle.lerp(theme.headline3, null, 0.25));
    expect(lerped.headline4, TextStyle.lerp(theme.headline4, null, 0.25));
163
    expect(lerped.caption, TextStyle.lerp(theme.caption, null, 0.25));
164 165 166 167 168
    expect(lerped.headline5, TextStyle.lerp(theme.headline5, null, 0.25));
    expect(lerped.headline6, TextStyle.lerp(theme.headline6, null, 0.25));
    expect(lerped.subtitle1, TextStyle.lerp(theme.subtitle1, null, 0.25));
    expect(lerped.bodyText1, TextStyle.lerp(theme.bodyText1, null, 0.25));
    expect(lerped.bodyText2, TextStyle.lerp(theme.bodyText2, null, 0.25));
169
    expect(lerped.button, TextStyle.lerp(theme.button, null, 0.25));
170
    expect(lerped.subtitle2, TextStyle.lerp(theme.subtitle2, null, 0.25));
171 172 173 174
    expect(lerped.overline, TextStyle.lerp(theme.overline, null, 0.25));
  });

  test('TextTheme lerp with first parameter null', () {
175
    final TextTheme theme = Typography.material2018().black;
176 177
    final TextTheme lerped = TextTheme.lerp(null, theme, 0.25);

178 179 180 181
    expect(lerped.headline1, TextStyle.lerp(null, theme.headline1, 0.25));
    expect(lerped.headline2, TextStyle.lerp(null, theme.headline2, 0.25));
    expect(lerped.headline3, TextStyle.lerp(null, theme.headline3, 0.25));
    expect(lerped.headline4, TextStyle.lerp(null, theme.headline4, 0.25));
182
    expect(lerped.caption, TextStyle.lerp(null, theme.caption, 0.25));
183 184 185 186 187
    expect(lerped.headline5, TextStyle.lerp(null, theme.headline5, 0.25));
    expect(lerped.headline6, TextStyle.lerp(null, theme.headline6, 0.25));
    expect(lerped.subtitle1, TextStyle.lerp(null, theme.subtitle1, 0.25));
    expect(lerped.bodyText1, TextStyle.lerp(null, theme.bodyText1, 0.25));
    expect(lerped.bodyText2, TextStyle.lerp(null, theme.bodyText2, 0.25));
188
    expect(lerped.button, TextStyle.lerp(null, theme.button, 0.25));
189
    expect(lerped.subtitle2, TextStyle.lerp(null, theme.subtitle2, 0.25));
190 191 192 193 194
    expect(lerped.overline, TextStyle.lerp(null, theme.overline, 0.25));
  });

  test('TextTheme lerp with null parameters', () {
    final TextTheme lerped = TextTheme.lerp(null, null, 0.25);
195 196 197 198
    expect(lerped.headline1, null);
    expect(lerped.headline2, null);
    expect(lerped.headline3, null);
    expect(lerped.headline4, null);
199
    expect(lerped.caption, null);
200 201 202 203 204
    expect(lerped.headline5, null);
    expect(lerped.headline6, null);
    expect(lerped.subtitle1, null);
    expect(lerped.bodyText1, null);
    expect(lerped.bodyText2, null);
205
    expect(lerped.button, null);
206
    expect(lerped.subtitle2, null);
207 208
    expect(lerped.overline, null);
  });
209
}