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

import 'package:flutter/cupertino.dart';
6
import 'package:flutter/foundation.dart';
xster's avatar
xster committed
7 8
import 'package:flutter_test/flutter_test.dart';

9 10 11
int buildCount = 0;
CupertinoThemeData? actualTheme;
IconThemeData? actualIconTheme;
xster's avatar
xster committed
12 13 14 15 16

final Widget singletonThemeSubtree = Builder(
  builder: (BuildContext context) {
    buildCount++;
    actualTheme = CupertinoTheme.of(context);
17
    actualIconTheme = IconTheme.of(context);
xster's avatar
xster committed
18 19 20 21 22 23 24 25 26 27 28
    return const Placeholder();
  },
);

Future<CupertinoThemeData> testTheme(WidgetTester tester, CupertinoThemeData theme) async {
  await tester.pumpWidget(
    CupertinoTheme(
      data: theme,
      child: singletonThemeSubtree,
    ),
  );
29
  return actualTheme!;
xster's avatar
xster committed
30 31
}

32 33 34 35 36 37 38
Future<IconThemeData> testIconTheme(WidgetTester tester, CupertinoThemeData theme) async {
  await tester.pumpWidget(
    CupertinoTheme(
      data: theme,
      child: singletonThemeSubtree,
    ),
  );
39
  return actualIconTheme!;
40 41
}

xster's avatar
xster committed
42 43 44 45
void main() {
  setUp(() {
    buildCount = 0;
    actualTheme = null;
46
    actualIconTheme = null;
xster's avatar
xster committed
47 48 49 50 51
  });

  testWidgets('Default theme has defaults', (WidgetTester tester) async {
    final CupertinoThemeData theme = await testTheme(tester, const CupertinoThemeData());

52
    expect(theme.brightness, isNull);
xster's avatar
xster committed
53 54
    expect(theme.primaryColor, CupertinoColors.activeBlue);
    expect(theme.textTheme.textStyle.fontSize, 17.0);
55
    expect(theme.applyThemeToAll, false);
xster's avatar
xster committed
56 57 58 59
  });

  testWidgets('Theme attributes cascade', (WidgetTester tester) async {
    final CupertinoThemeData theme = await testTheme(tester, const CupertinoThemeData(
60
      primaryColor: CupertinoColors.systemRed,
xster's avatar
xster committed
61 62
    ));

63
    expect(theme.textTheme.actionTextStyle.color, isSameColorAs(CupertinoColors.systemRed.color));
xster's avatar
xster committed
64 65 66 67 68 69 70
  });

  testWidgets('Dependent attribute can be overridden from cascaded value', (WidgetTester tester) async {
    final CupertinoThemeData theme = await testTheme(tester, const CupertinoThemeData(
      brightness: Brightness.dark,
      textTheme: CupertinoTextThemeData(
        textStyle: TextStyle(color: CupertinoColors.black),
71
      ),
xster's avatar
xster committed
72 73 74
    ));

    // The brightness still cascaded down to the background color.
75
    expect(theme.scaffoldBackgroundColor, isSameColorAs(CupertinoColors.black));
xster's avatar
xster committed
76
    // But not to the font color which we overrode.
77
    expect(theme.textTheme.textStyle.color, isSameColorAs(CupertinoColors.black));
xster's avatar
xster committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  });

  testWidgets(
    'Reading themes creates dependencies',
    (WidgetTester tester) async {
      // Reading the theme creates a dependency.
      CupertinoThemeData theme = await testTheme(tester, const CupertinoThemeData(
        // Default brightness is light,
        barBackgroundColor: Color(0x11223344),
        textTheme: CupertinoTextThemeData(
          textStyle: TextStyle(fontFamily: 'Skeuomorphic'),
        ),
      ));

      expect(buildCount, 1);
      expect(theme.textTheme.textStyle.fontFamily, 'Skeuomorphic');

      // Changing another property also triggers a rebuild.
      theme = await testTheme(tester, const CupertinoThemeData(
        brightness: Brightness.light,
        barBackgroundColor: Color(0x11223344),
        textTheme: CupertinoTextThemeData(
          textStyle: TextStyle(fontFamily: 'Skeuomorphic'),
        ),
      ));

      expect(buildCount, 2);
      // Re-reading the same value doesn't change anything.
      expect(theme.textTheme.textStyle.fontFamily, 'Skeuomorphic');

      theme = await testTheme(tester, const CupertinoThemeData(
        brightness: Brightness.light,
        barBackgroundColor: Color(0x11223344),
        textTheme: CupertinoTextThemeData(
          textStyle: TextStyle(fontFamily: 'Flat'),
        ),
      ));

      expect(buildCount, 3);
      expect(theme.textTheme.textStyle.fontFamily, 'Flat');
    },
  );

  testWidgets(
    'copyWith works',
    (WidgetTester tester) async {
      const CupertinoThemeData originalTheme = CupertinoThemeData(
        brightness: Brightness.dark,
126
        applyThemeToAll: true,
xster's avatar
xster committed
127 128 129
      );

      final CupertinoThemeData theme = await testTheme(tester, originalTheme.copyWith(
130
        primaryColor: CupertinoColors.systemGreen,
131
        applyThemeToAll: false,
xster's avatar
xster committed
132 133 134
      ));

      expect(theme.brightness, Brightness.dark);
135
      expect(theme.primaryColor, isSameColorAs(CupertinoColors.systemGreen.darkColor));
xster's avatar
xster committed
136
      // Now check calculated derivatives.
137 138
      expect(theme.textTheme.actionTextStyle.color, isSameColorAs(CupertinoColors.systemGreen.darkColor));
      expect(theme.scaffoldBackgroundColor, isSameColorAs(CupertinoColors.black));
139 140

      expect(theme.applyThemeToAll, false);
xster's avatar
xster committed
141 142
    },
  );
143 144

  testWidgets("Theme has default IconThemeData, which is derived from the theme's primary color", (WidgetTester tester) async {
145
    const CupertinoDynamicColor primaryColor = CupertinoColors.systemRed;
146
    const CupertinoThemeData themeData = CupertinoThemeData(primaryColor: primaryColor);
147

148
    final IconThemeData resultingIconTheme = await testIconTheme(tester, themeData);
149

150
    expect(resultingIconTheme.color, isSameColorAs(primaryColor));
151 152 153 154 155

    // Works in dark mode if primaryColor is a CupertinoDynamicColor.
    final Color darkColor = (await testIconTheme(
      tester,
      themeData.copyWith(brightness: Brightness.dark),
156
    )).color!;
157 158

    expect(darkColor, isSameColorAs(primaryColor.darkColor));
159 160 161
  });

  testWidgets('IconTheme.of creates a dependency on iconTheme', (WidgetTester tester) async {
162
    IconThemeData iconTheme = await testIconTheme(tester, const CupertinoThemeData(primaryColor: CupertinoColors.destructiveRed));
163

164 165
    expect(buildCount, 1);
    expect(iconTheme.color, CupertinoColors.destructiveRed);
166

167 168 169
    iconTheme = await testIconTheme(tester, const CupertinoThemeData(primaryColor: CupertinoColors.activeOrange));
    expect(buildCount, 2);
    expect(iconTheme.color, CupertinoColors.activeOrange);
170
  });
171

172 173 174 175 176 177 178 179 180
  testWidgets('CupertinoTheme diagnostics', (WidgetTester tester) async {
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const CupertinoThemeData().debugFillProperties(builder);

    final Set<String> description = builder.properties
      .map((DiagnosticsNode node) => node.name.toString())
      .toSet();

    expect(
181
      setEquals(
182
        description,
183 184
        <String>{
          'brightness',
185 186 187 188
          'primaryColor',
          'primaryContrastingColor',
          'barBackgroundColor',
          'scaffoldBackgroundColor',
189
          'applyThemeToAll',
190 191 192 193 194 195 196
          'textStyle',
          'actionTextStyle',
          'tabLabelTextStyle',
          'navTitleTextStyle',
          'navLargeTitleTextStyle',
          'navActionTextStyle',
          'pickerTextStyle',
197
          'dateTimePickerTextStyle',
198
        },
199 200 201 202 203
      ),
      isTrue,
    );
  });

204 205 206 207 208 209 210 211 212 213 214
  testWidgets('CupertinoTheme.toStringDeep uses single-line style', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/47651.
    expect(
      const CupertinoTheme(
        data: CupertinoThemeData(primaryColor: Color(0x00000000)),
        child: SizedBox(),
      ).toStringDeep().trimRight(),
      isNot(contains('\n')),
    );
  });

215 216 217 218 219 220 221 222 223 224 225 226
  testWidgets('CupertinoThemeData equality', (WidgetTester tester) async {
    const CupertinoThemeData a = CupertinoThemeData(brightness: Brightness.dark);
    final CupertinoThemeData b = a.copyWith();
    final CupertinoThemeData c = a.copyWith(brightness: Brightness.light);
    expect(a, equals(b));
    expect(b, equals(a));
    expect(a, isNot(equals(c)));
    expect(c, isNot(equals(a)));
    expect(b, isNot(equals(c)));
    expect(c, isNot(equals(b)));
  });

227 228
  late Brightness currentBrightness;
  void colorMatches(Color? componentColor, CupertinoDynamicColor expectedDynamicColor) {
229 230 231 232 233 234 235 236 237 238
    switch (currentBrightness) {
      case Brightness.light:
        expect(componentColor, isSameColorAs(expectedDynamicColor.color));
        break;
      case Brightness.dark:
        expect(componentColor, isSameColorAs(expectedDynamicColor.darkColor));
        break;
    }
  }

239
  void dynamicColorsTestGroup() {
240 241 242 243
    testWidgets('CupertinoTheme.of resolves colors', (WidgetTester tester) async {
      final CupertinoThemeData data = CupertinoThemeData(brightness: currentBrightness, primaryColor: CupertinoColors.systemRed);
      final CupertinoThemeData theme = await testTheme(tester, data);

244
      expect(data.primaryColor, isSameColorAs(CupertinoColors.systemRed));
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
      colorMatches(theme.primaryColor, CupertinoColors.systemRed);
    });

    testWidgets('CupertinoTheme.of resolves default values', (WidgetTester tester) async {
      const CupertinoDynamicColor primaryColor = CupertinoColors.systemRed;
      final CupertinoThemeData data = CupertinoThemeData(brightness: currentBrightness, primaryColor: primaryColor);

      const CupertinoDynamicColor barBackgroundColor = CupertinoDynamicColor.withBrightness(
        color: Color(0xF0F9F9F9),
        darkColor: Color(0xF01D1D1D),
      );

      final CupertinoThemeData theme = await testTheme(tester, data);

      colorMatches(theme.primaryContrastingColor, CupertinoColors.systemBackground);
      colorMatches(theme.barBackgroundColor, barBackgroundColor);
      colorMatches(theme.scaffoldBackgroundColor, CupertinoColors.systemBackground);
      colorMatches(theme.textTheme.textStyle.color, CupertinoColors.label);
      colorMatches(theme.textTheme.actionTextStyle.color, primaryColor);
      colorMatches(theme.textTheme.tabLabelTextStyle.color, CupertinoColors.inactiveGray);
      colorMatches(theme.textTheme.navTitleTextStyle.color, CupertinoColors.label);
      colorMatches(theme.textTheme.navLargeTitleTextStyle.color, CupertinoColors.label);
      colorMatches(theme.textTheme.navActionTextStyle.color, primaryColor);
      colorMatches(theme.textTheme.pickerTextStyle.color, CupertinoColors.label);
      colorMatches(theme.textTheme.dateTimePickerTextStyle.color, CupertinoColors.label);
    });
271
  }
272 273 274 275 276 277

  currentBrightness = Brightness.light;
  group('light colors', dynamicColorsTestGroup);

  currentBrightness = Brightness.dark;
  group('dark colors', dynamicColorsTestGroup);
xster's avatar
xster committed
278
}