app_title_test.dart 2.61 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
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/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
7

8
const Color kTitleColor = Color(0xFF333333);
9 10
const String kTitleString = 'Hello World';

11
Future<void> pumpApp(WidgetTester tester, { GenerateAppTitle? onGenerateTitle, Color? color }) async {
12
  await tester.pumpWidget(
13
    WidgetsApp(
14
      supportedLocales: const <Locale>[
15 16
        Locale('en', 'US'),
        Locale('en', 'GB'),
17 18
      ],
      title: kTitleString,
19
      color: color ?? kTitleColor,
20 21
      onGenerateTitle: onGenerateTitle,
      onGenerateRoute: (RouteSettings settings) {
22
        return PageRouteBuilder<void>(
23
          pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
24
            return Container();
25
          },
26 27 28 29 30 31 32 33 34 35 36 37 38
        );
      },
    ),
  );
}

void main() {
  testWidgets('Specified title and color are used to build a Title', (WidgetTester tester) async {
    await pumpApp(tester);
    expect(tester.widget<Title>(find.byType(Title)).title, kTitleString);
    expect(tester.widget<Title>(find.byType(Title)).color, kTitleColor);
  });

39 40 41 42 43 44 45 46 47
  testWidgets('Specified color is made opaque for Title', (WidgetTester tester) async {
    // The Title widget can only handle fully opaque colors, the WidgetApp should
    // ensure it only uses a fully opaque version of its color for the title.
    const Color transparentBlue = Color(0xDD0000ff);
    const Color opaqueBlue = Color(0xFF0000ff);
    await pumpApp(tester, color: transparentBlue);
    expect(tester.widget<Title>(find.byType(Title)).color, opaqueBlue);
  });

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  testWidgets('onGenerateTitle handles changing locales', (WidgetTester tester) async {
    String generateTitle(BuildContext context) {
      return Localizations.localeOf(context).toString();
    }

    await pumpApp(tester, onGenerateTitle: generateTitle);
    expect(tester.widget<Title>(find.byType(Title)).title, 'en_US');
    expect(tester.widget<Title>(find.byType(Title)).color, kTitleColor);

    await tester.binding.setLocale('en', 'GB');
    await tester.pump();
    expect(tester.widget<Title>(find.byType(Title)).title, 'en_GB');
    expect(tester.widget<Title>(find.byType(Title)).color, kTitleColor);

    // Not a supported locale, so we switch to supportedLocales[0], en_US
    await tester.binding.setLocale('fr', 'CA');
    await tester.pump();
    expect(tester.widget<Title>(find.byType(Title)).title, 'en_US');
    expect(tester.widget<Title>(find.byType(Title)).color, kTitleColor);
67
  });
68 69

}