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

5 6
// @dart = 2.8

7 8 9 10
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
11 12 13 14 15 16 17 18 19
  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', () {
20
    final Typography typography = Typography.material2018(platform: TargetPlatform.android);
21 22 23
    expect(typography.black, equals(typography.black.copyWith()));
    expect(typography.black, equals(typography.black.apply()));
    expect(typography.black, equals(typography.black.merge(null)));
24
    expect(typography.black, equals(const TextTheme().merge(typography.black)));
25 26 27 28 29 30 31
    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', () {
32
    final Typography typography = Typography.material2018(platform: TargetPlatform.android);
33
    final TextTheme whiteCopy = typography.black.copyWith(
34 35 36 37 38 39 40 41 42
      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,
43 44
      caption: typography.white.caption,
      button: typography.white.button,
45
      subtitle2: typography.white.subtitle2,
46 47 48 49 50 51 52
      overline: typography.white.overline,
    );
    expect(typography.white, equals(whiteCopy));
  });


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

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

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

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

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

99 100 101 102
    expect(theme.headline1.color, displayColor);
    expect(theme.headline2.color, displayColor);
    expect(theme.headline3.color, displayColor);
    expect(theme.headline4.color, displayColor);
103
    expect(theme.caption.color, displayColor);
104 105 106 107 108
    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);
109
    expect(theme.button.color, bodyColor);
110
    expect(theme.subtitle2.color, bodyColor);
111 112 113
    expect(theme.overline.color, bodyColor);

    final List<TextStyle> themeStyles = <TextStyle>[
114 115 116 117
      theme.headline1,
      theme.headline2,
      theme.headline3,
      theme.headline4,
118
      theme.caption,
119 120 121 122 123
      theme.headline5,
      theme.headline6,
      theme.subtitle1,
      theme.bodyText1,
      theme.bodyText2,
124
      theme.button,
125
      theme.subtitle2,
126 127 128 129 130 131 132 133 134
      theme.overline,
    ];
    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', () {
135 136
    final Typography typography = Typography.material2018(platform: TargetPlatform.android);
    final TextTheme baseTheme = Typography.englishLike2018.merge(typography.black);
137 138 139 140 141
    final TextTheme sizeTheme = baseTheme.apply(
      fontSizeFactor: 2.0,
      fontSizeDelta: 5.0,
    );

142 143 144 145
    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);
146
    expect(sizeTheme.caption.fontSize, baseTheme.caption.fontSize * 2.0 + 5.0);
147 148 149 150 151
    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);
152
    expect(sizeTheme.button.fontSize, baseTheme.button.fontSize * 2.0 + 5.0);
153
    expect(sizeTheme.subtitle2.fontSize, baseTheme.subtitle2.fontSize * 2.0 + 5.0);
154 155 156
    expect(sizeTheme.overline.fontSize, baseTheme.overline.fontSize * 2.0 + 5.0);
  });

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

161 162 163 164
    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));
165
    expect(lerped.caption, TextStyle.lerp(theme.caption, null, 0.25));
166 167 168 169 170
    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));
171
    expect(lerped.button, TextStyle.lerp(theme.button, null, 0.25));
172
    expect(lerped.subtitle2, TextStyle.lerp(theme.subtitle2, null, 0.25));
173 174 175 176
    expect(lerped.overline, TextStyle.lerp(theme.overline, null, 0.25));
  });

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

180 181 182 183
    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));
184
    expect(lerped.caption, TextStyle.lerp(null, theme.caption, 0.25));
185 186 187 188 189
    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));
190
    expect(lerped.button, TextStyle.lerp(null, theme.button, 0.25));
191
    expect(lerped.subtitle2, TextStyle.lerp(null, theme.subtitle2, 0.25));
192 193 194 195 196
    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);
197 198 199 200
    expect(lerped.headline1, null);
    expect(lerped.headline2, null);
    expect(lerped.headline3, null);
    expect(lerped.headline4, null);
201
    expect(lerped.caption, null);
202 203 204 205 206
    expect(lerped.headline5, null);
    expect(lerped.headline6, null);
    expect(lerped.subtitle1, null);
    expect(lerped.bodyText1, null);
    expect(lerped.bodyText2, null);
207
    expect(lerped.button, null);
208
    expect(lerped.subtitle2, null);
209 210
    expect(lerped.overline, null);
  });
211
}