theme_test.dart 9.4 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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

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

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

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

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

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

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

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

  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 126 127 128
  });

  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,
      );

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

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

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

144
    final IconThemeData resultingIconTheme = await testIconTheme(tester, themeData);
145

146
    expect(resultingIconTheme.color, isSameColorAs(primaryColor));
147 148 149 150 151

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

    expect(darkColor, isSameColorAs(primaryColor.darkColor));
155 156 157
  });

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

160 161
    expect(buildCount, 1);
    expect(iconTheme.color, CupertinoColors.destructiveRed);
162

163 164 165
    iconTheme = await testIconTheme(tester, const CupertinoThemeData(primaryColor: CupertinoColors.activeOrange));
    expect(buildCount, 2);
    expect(iconTheme.color, CupertinoColors.activeOrange);
166
  });
167

168 169 170 171 172 173 174 175 176
  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(
177
      setEquals(
178 179 180 181 182 183 184 185 186 187 188 189 190
        description,
        <String>{ 'brightness',
          'primaryColor',
          'primaryContrastingColor',
          'barBackgroundColor',
          'scaffoldBackgroundColor',
          'textStyle',
          'actionTextStyle',
          'tabLabelTextStyle',
          'navTitleTextStyle',
          'navLargeTitleTextStyle',
          'navActionTextStyle',
          'pickerTextStyle',
191
          'dateTimePickerTextStyle',
192 193 194 195 196 197
        }
      ),
      isTrue,
    );
  });

198 199 200 201 202 203 204 205 206 207 208
  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')),
    );
  });

209 210
  late Brightness currentBrightness;
  void colorMatches(Color? componentColor, CupertinoDynamicColor expectedDynamicColor) {
211 212 213 214 215 216 217 218 219 220
    switch (currentBrightness) {
      case Brightness.light:
        expect(componentColor, isSameColorAs(expectedDynamicColor.color));
        break;
      case Brightness.dark:
        expect(componentColor, isSameColorAs(expectedDynamicColor.darkColor));
        break;
    }
  }

221
  final VoidCallback dynamicColorsTestGroup = () {
222 223 224 225
    testWidgets('CupertinoTheme.of resolves colors', (WidgetTester tester) async {
      final CupertinoThemeData data = CupertinoThemeData(brightness: currentBrightness, primaryColor: CupertinoColors.systemRed);
      final CupertinoThemeData theme = await testTheme(tester, data);

226
      expect(data.primaryColor, isSameColorAs(CupertinoColors.systemRed));
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
      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);
    });
  };

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

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