theme_test.dart 17.2 KB
Newer Older
1 2 3 4
// 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.

5
import 'dart:ui' as ui;
6
import 'package:flutter/material.dart';
7
import 'package:flutter/rendering.dart';
8
import 'package:flutter/src/foundation/diagnostics.dart';
9 10 11
import 'package:flutter_test/flutter_test.dart';

void main() {
12 13
  const TextTheme defaultGeometryTheme = Typography.englishLike2014;

14
  test('ThemeDataTween control test', () {
15
    final ThemeData light = ThemeData.light();
16
    final ThemeData dark = ThemeData.dark();
17
    final ThemeDataTween tween = ThemeDataTween(begin: light, end: dark);
18 19 20
    expect(tween.lerp(0.25), equals(ThemeData.lerp(light, dark, 0.25)));
  });

21
  testWidgets('PopupMenu inherits app theme', (WidgetTester tester) async {
22
    final Key popupMenuButtonKey = UniqueKey();
23
    await tester.pumpWidget(
24 25 26 27
      MaterialApp(
        theme: ThemeData(brightness: Brightness.dark),
        home: Scaffold(
          appBar: AppBar(
28
            actions: <Widget>[
29
              PopupMenuButton<String>(
30 31 32
                key: popupMenuButtonKey,
                itemBuilder: (BuildContext context) {
                  return <PopupMenuItem<String>>[
33
                    const PopupMenuItem<String>(child: Text('menuItem'))
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
                  ];
                }
              ),
            ]
          )
        )
      )
    );

    await tester.tap(find.byKey(popupMenuButtonKey));
    await tester.pump(const Duration(seconds: 1));

    expect(Theme.of(tester.element(find.text('menuItem'))).brightness, equals(Brightness.dark));
  });

49 50 51
  testWidgets('Fallback theme', (WidgetTester tester) async {
    BuildContext capturedContext;
    await tester.pumpWidget(
52
      Builder(
53 54
        builder: (BuildContext context) {
          capturedContext = context;
55
          return Container();
56 57 58 59
        }
      )
    );

60
    expect(Theme.of(capturedContext), equals(ThemeData.localize(ThemeData.fallback(), defaultGeometryTheme)));
61 62 63
    expect(Theme.of(capturedContext, shadowThemeOnly: true), isNull);
  });

64
  testWidgets('ThemeData.localize memoizes the result', (WidgetTester tester) async {
65 66
    final ThemeData light = ThemeData.light();
    final ThemeData dark = ThemeData.dark();
67 68 69

    // Same input, same output.
    expect(
70 71
      ThemeData.localize(light, defaultGeometryTheme),
      same(ThemeData.localize(light, defaultGeometryTheme)),
72 73 74 75
    );

    // Different text geometry, different output.
    expect(
76 77
      ThemeData.localize(light, defaultGeometryTheme),
      isNot(same(ThemeData.localize(light, Typography.tall2014))),
78 79 80 81
    );

    // Different base theme, different output.
    expect(
82 83
      ThemeData.localize(light, defaultGeometryTheme),
      isNot(same(ThemeData.localize(dark, defaultGeometryTheme))),
84 85 86
    );
  });

87 88
  testWidgets('PopupMenu inherits shadowed app theme', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/5572
89
    final Key popupMenuButtonKey = UniqueKey();
90
    await tester.pumpWidget(
91 92 93 94 95 96
      MaterialApp(
        theme: ThemeData(brightness: Brightness.dark),
        home: Theme(
          data: ThemeData(brightness: Brightness.light),
          child: Scaffold(
            appBar: AppBar(
97
              actions: <Widget>[
98
                PopupMenuButton<String>(
99 100 101
                  key: popupMenuButtonKey,
                  itemBuilder: (BuildContext context) {
                    return <PopupMenuItem<String>>[
102
                      const PopupMenuItem<String>(child: Text('menuItem'))
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
                    ];
                  }
                ),
              ]
            )
          )
        )
      )
    );

    await tester.tap(find.byKey(popupMenuButtonKey));
    await tester.pump(const Duration(seconds: 1));

    expect(Theme.of(tester.element(find.text('menuItem'))).brightness, equals(Brightness.light));
  });

  testWidgets('DropdownMenu inherits shadowed app theme', (WidgetTester tester) async {
120
    final Key dropdownMenuButtonKey = UniqueKey();
121
    await tester.pumpWidget(
122 123 124 125 126 127
      MaterialApp(
        theme: ThemeData(brightness: Brightness.dark),
        home: Theme(
          data: ThemeData(brightness: Brightness.light),
          child: Scaffold(
            appBar: AppBar(
128
              actions: <Widget>[
129
                DropdownButton<String>(
130 131 132
                  key: dropdownMenuButtonKey,
                  onChanged: (String newValue) { },
                  value: 'menuItem',
133
                  items: const <DropdownMenuItem<String>>[
134
                    DropdownMenuItem<String>(
135
                      value: 'menuItem',
136
                      child: Text('menuItem'),
137 138 139 140 141 142 143 144 145 146 147 148 149
                    ),
                  ],
                )
              ]
            )
          )
        )
      )
    );

    await tester.tap(find.byKey(dropdownMenuButtonKey));
    await tester.pump(const Duration(seconds: 1));

150
    for (Element item in tester.elementList(find.text('menuItem')))
151 152 153 154 155
      expect(Theme.of(item).brightness, equals(Brightness.light));
  });

  testWidgets('ModalBottomSheet inherits shadowed app theme', (WidgetTester tester) async {
    await tester.pumpWidget(
156 157 158 159 160 161 162
      MaterialApp(
        theme: ThemeData(brightness: Brightness.dark),
        home: Theme(
          data: ThemeData(brightness: Brightness.light),
          child: Scaffold(
            body: Center(
              child: Builder(
163
                builder: (BuildContext context) {
164
                  return RaisedButton(
165
                    onPressed: () {
166
                      showModalBottomSheet<void>(
167
                        context: context,
168
                        builder: (BuildContext context) => const Text('bottomSheet'),
169 170
                      );
                    },
171
                    child: const Text('SHOW'),
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
                  );
                }
              )
            )
          )
        )
      )
    );

    await tester.tap(find.text('SHOW'));
    await tester.pump(const Duration(seconds: 1));
    expect(Theme.of(tester.element(find.text('bottomSheet'))).brightness, equals(Brightness.light));

    await tester.tap(find.text('bottomSheet')); // dismiss the bottom sheet
    await tester.pump(const Duration(seconds: 1));
  });

  testWidgets('Dialog inherits shadowed app theme', (WidgetTester tester) async {
190
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
191
    await tester.pumpWidget(
192 193 194 195 196
      MaterialApp(
        theme: ThemeData(brightness: Brightness.dark),
        home: Theme(
          data: ThemeData(brightness: Brightness.light),
          child: Scaffold(
197
            key: scaffoldKey,
198 199
            body: Center(
              child: Builder(
200
                builder: (BuildContext context) {
201
                  return RaisedButton(
202
                    onPressed: () {
203
                      showDialog<void>(
204
                        context: context,
205
                        builder: (BuildContext context) => const Text('dialog'),
206 207
                      );
                    },
208
                    child: const Text('SHOW'),
209 210 211 212 213 214 215 216 217 218 219 220 221 222
                  );
                }
              )
            )
          )
        )
      )
    );

    await tester.tap(find.text('SHOW'));
    await tester.pump(const Duration(seconds: 1));
    expect(Theme.of(tester.element(find.text('dialog'))).brightness, equals(Brightness.light));
  });

223
  testWidgets("Scaffold inherits theme's scaffoldBackgroundColor", (WidgetTester tester) async {
224
    const Color green = Color(0xFF00FF00);
225 226

    await tester.pumpWidget(
227 228 229 230 231
      MaterialApp(
        theme: ThemeData(scaffoldBackgroundColor: green),
        home: Scaffold(
          body: Center(
            child: Builder(
232
              builder: (BuildContext context) {
233
                return GestureDetector(
234
                  onTap: () {
235
                    showDialog<void>(
236
                      context: context,
237 238
                      builder: (BuildContext context) {
                        return const Scaffold(
239
                          body: SizedBox(
240 241 242 243 244
                            width: 200.0,
                            height: 200.0,
                          ),
                        );
                      },
245 246
                    );
                  },
247
                  child: const Text('SHOW'),
248 249 250 251 252 253 254 255 256 257 258
                );
              },
            ),
          ),
        ),
      )
    );

    await tester.tap(find.text('SHOW'));
    await tester.pump(const Duration(seconds: 1));

259
    final List<Material> materials = tester.widgetList<Material>(find.byType(Material)).toList();
260 261 262 263
    expect(materials.length, equals(2));
    expect(materials[0].color, green); // app scaffold
    expect(materials[1].color, green); // dialog scaffold
  });
264 265 266

  testWidgets('IconThemes are applied', (WidgetTester tester) async {
    await tester.pumpWidget(
267 268
      MaterialApp(
        theme: ThemeData(iconTheme: const IconThemeData(color: Colors.green, size: 10.0)),
269 270 271 272 273 274 275 276 277 278
        home: const Icon(Icons.computer),
      )
    );

    RenderParagraph glyphText = tester.renderObject(find.byType(RichText));

    expect(glyphText.text.style.color, Colors.green);
    expect(glyphText.text.style.fontSize, 10.0);

    await tester.pumpWidget(
279 280
      MaterialApp(
        theme: ThemeData(iconTheme: const IconThemeData(color: Colors.orange, size: 20.0)),
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
        home: const Icon(Icons.computer),
      ),
    );
    await tester.pump(const Duration(milliseconds: 100)); // Halfway through the theme transition

    glyphText = tester.renderObject(find.byType(RichText));

    expect(glyphText.text.style.color, Color.lerp(Colors.green, Colors.orange, 0.5));
    expect(glyphText.text.style.fontSize, 15.0);

    await tester.pump(const Duration(milliseconds: 100)); // Finish the transition
    glyphText = tester.renderObject(find.byType(RichText));

    expect(glyphText.text.style.color, Colors.orange);
    expect(glyphText.text.style.fontSize, 20.0);
  });
297 298

  testWidgets(
Ian Hickson's avatar
Ian Hickson committed
299
    'Same ThemeData reapplied does not trigger descendants rebuilds',
300 301
    (WidgetTester tester) async {
      testBuildCalled = 0;
302
      ThemeData themeData = ThemeData(primaryColor: const Color(0xFF000000));
303

304
      Widget buildTheme() {
305
        return Theme(
306 307
          data: themeData,
          child: const Test(),
308 309 310 311
        );
      }

      await tester.pumpWidget(buildTheme());
312 313 314
      expect(testBuildCalled, 1);

      // Pump the same widgets again.
315
      await tester.pumpWidget(buildTheme());
316 317 318 319
      // No repeated build calls to the child since it's the same theme data.
      expect(testBuildCalled, 1);

      // New instance of theme data but still the same content.
320
      themeData = ThemeData(primaryColor: const Color(0xFF000000));
321
      await tester.pumpWidget(buildTheme());
322 323 324 325
      // Still no repeated calls.
      expect(testBuildCalled, 1);

      // Different now.
326
      themeData = ThemeData(primaryColor: const Color(0xFF222222));
327
      await tester.pumpWidget(buildTheme());
328 329 330 331
      // Should call build again.
      expect(testBuildCalled, 2);
    },
  );
332 333 334

  testWidgets('Text geometry set in Theme has higher precedence than that of Localizations', (WidgetTester tester) async {
    const double _kMagicFontSize = 4321.0;
335
    final ThemeData fallback = ThemeData.fallback();
336 337 338 339 340 341 342 343 344 345
    final ThemeData customTheme = fallback.copyWith(
      primaryTextTheme: fallback.primaryTextTheme.copyWith(
        body1: fallback.primaryTextTheme.body1.copyWith(
          fontSize: _kMagicFontSize,
        )
      ),
    );
    expect(customTheme.primaryTextTheme.body1.fontSize, _kMagicFontSize);

    double actualFontSize;
346
    await tester.pumpWidget(Directionality(
347
      textDirection: TextDirection.ltr,
348
      child: Theme(
349
        data: customTheme,
350
        child: Builder(builder: (BuildContext context) {
351 352
          final ThemeData theme = Theme.of(context);
          actualFontSize = theme.primaryTextTheme.body1.fontSize;
353
          return Text(
354 355 356 357 358 359 360 361 362
            'A',
            style: theme.primaryTextTheme.body1,
          );
        }),
      ),
    ));

    expect(actualFontSize, _kMagicFontSize);
  });
363 364 365

  testWidgets('Default Theme provides all basic TextStyle properties', (WidgetTester tester) async {
    ThemeData theme;
366
    await tester.pumpWidget(Directionality(
367
      textDirection: TextDirection.ltr,
368
      child: Builder(
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
        builder: (BuildContext context) {
          theme = Theme.of(context);
          return const Text('A');
        },
      ),
    ));

    List<TextStyle> extractStyles(TextTheme textTheme) {
      return <TextStyle>[
        textTheme.display4,
        textTheme.display3,
        textTheme.display2,
        textTheme.display1,
        textTheme.headline,
        textTheme.title,
        textTheme.subhead,
        textTheme.body2,
        textTheme.body1,
        textTheme.caption,
        textTheme.button,
      ];
    }

    for (TextTheme textTheme in <TextTheme>[theme.textTheme, theme.primaryTextTheme, theme.accentTextTheme]) {
393
      for (TextStyle style in extractStyles(textTheme).map<TextStyle>((TextStyle style) => _TextStyleProxy(style))) {
394 395 396 397 398 399 400 401 402 403 404 405 406 407
        expect(style.inherit, false);
        expect(style.color, isNotNull);
        expect(style.fontFamily, isNotNull);
        expect(style.fontSize, isNotNull);
        expect(style.fontWeight, isNotNull);
        expect(style.fontStyle, null);
        expect(style.letterSpacing, null);
        expect(style.wordSpacing, null);
        expect(style.textBaseline, isNotNull);
        expect(style.height, null);
        expect(style.decoration, TextDecoration.none);
        expect(style.decorationColor, null);
        expect(style.decorationStyle, null);
        expect(style.debugLabel, isNotNull);
408 409
        expect(style.locale, null);
        expect(style.background, null);
410 411 412
      }
    }

413
    expect(theme.textTheme.display4.debugLabel, '(englishLike display4 2014).merge(blackMountainView display4)');
414
  });
415 416 417 418 419 420 421
}

int testBuildCalled;
class Test extends StatefulWidget {
  const Test();

  @override
422
  _TestState createState() => _TestState();
423 424 425 426 427 428
}

class _TestState extends State<Test> {
  @override
  Widget build(BuildContext context) {
    testBuildCalled += 1;
429 430
    return Container(
      decoration: BoxDecoration(
431 432 433 434
        color: Theme.of(context).primaryColor,
      ),
    );
  }
435
}
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

/// This class exists only to make sure that we test all the properties of the
/// [TextStyle] class. If a property is added/removed/renamed, the analyzer will
/// complain that this class has incorrect overrides.
class _TextStyleProxy implements TextStyle {
  _TextStyleProxy(this._delegate);

  final TextStyle _delegate;

  // Do make sure that all the properties correctly forward to the _delegate.
  @override Color get color => _delegate.color;
  @override String get debugLabel => _delegate.debugLabel;
  @override TextDecoration get decoration => _delegate.decoration;
  @override Color get decorationColor => _delegate.decorationColor;
  @override TextDecorationStyle get decorationStyle => _delegate.decorationStyle;
  @override String get fontFamily => _delegate.fontFamily;
  @override double get fontSize => _delegate.fontSize;
  @override FontStyle get fontStyle => _delegate.fontStyle;
  @override FontWeight get fontWeight => _delegate.fontWeight;
  @override double get height => _delegate.height;
456
  @override Locale get locale => _delegate.locale;
457
  @override ui.Paint get foreground => _delegate.foreground;
458
  @override ui.Paint get background => _delegate.background;
459 460 461 462
  @override bool get inherit => _delegate.inherit;
  @override double get letterSpacing => _delegate.letterSpacing;
  @override TextBaseline get textBaseline => _delegate.textBaseline;
  @override double get wordSpacing => _delegate.wordSpacing;
463
  @override List<Shadow> get shadows => _delegate.shadows;
464

465 466 467 468
  @override
  String toString({DiagnosticLevel minLevel = DiagnosticLevel.debug}) =>
      super.toString();

469 470
  @override
  DiagnosticsNode toDiagnosticsNode({String name, DiagnosticsTreeStyle style}) {
471
    throw UnimplementedError();
472 473 474 475
  }

  @override
  String toStringShort() {
476
    throw UnimplementedError();
477 478 479
  }

  @override
480
  TextStyle apply({Color color, TextDecoration decoration, Color decorationColor, TextDecorationStyle decorationStyle, String fontFamily, double fontSizeFactor = 1.0, double fontSizeDelta = 0.0, int fontWeightDelta = 0, double letterSpacingFactor = 1.0, double letterSpacingDelta = 0.0, double wordSpacingFactor = 1.0, double wordSpacingDelta = 0.0, double heightFactor = 1.0, double heightDelta = 0.0}) {
481
    throw UnimplementedError();
482 483 484 485
  }

  @override
  RenderComparison compareTo(TextStyle other) {
486
    throw UnimplementedError();
487 488 489
  }

  @override
490
  TextStyle copyWith({Color color, String fontFamily, double fontSize, FontWeight fontWeight, FontStyle fontStyle, double letterSpacing, double wordSpacing, TextBaseline textBaseline, double height, Locale locale, ui.Paint foreground, ui.Paint background, List<Shadow> shadows, TextDecoration decoration, Color decorationColor, TextDecorationStyle decorationStyle, String debugLabel}) {
491
    throw UnimplementedError();
492 493 494
  }

  @override
495
  void debugFillProperties(DiagnosticPropertiesBuilder properties, {String prefix = ''}) {
496
    throw UnimplementedError();
497 498 499
  }

  @override
500
  ui.ParagraphStyle getParagraphStyle({TextAlign textAlign, TextDirection textDirection, double textScaleFactor = 1.0, String ellipsis, int maxLines, Locale locale}) {
501
    throw UnimplementedError();
502 503 504
  }

  @override
505
  ui.TextStyle getTextStyle({double textScaleFactor = 1.0}) {
506
    throw UnimplementedError();
507 508 509 510
  }

  @override
  TextStyle merge(TextStyle other) {
511
    throw UnimplementedError();
512 513
  }
}