date_time_test.dart 7.87 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
import 'dart:async';

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

void main() {
  group(GlobalMaterialLocalizations, () {
13
    test('uses exact locale when exists', () async {
14 15
      final GlobalMaterialLocalizations localizations =
        await GlobalMaterialLocalizations.delegate.load(const Locale('pt', 'PT')) as GlobalMaterialLocalizations;
16 17 18
      expect(localizations.formatDecimal(10000), '10\u00A0000');
    });

19
    test('falls back to language code when exact locale is missing', () async {
20 21
      final GlobalMaterialLocalizations localizations =
        await GlobalMaterialLocalizations.delegate.load(const Locale('pt', 'XX')) as GlobalMaterialLocalizations;
22 23 24
      expect(localizations.formatDecimal(10000), '10.000');
    });

25 26 27 28
    test('fails when neither language code nor exact locale are available', () async {
      await expectLater(() async {
        await GlobalMaterialLocalizations.delegate.load(const Locale('xx', 'XX'));
      }, throwsAssertionError);
29 30 31
    });

    group('formatHour', () {
32
      Future<String> formatHour(WidgetTester tester, Locale locale, TimeOfDay timeOfDay) async {
33 34
        final Completer<String> completer = Completer<String>();
        await tester.pumpWidget(MaterialApp(
35 36
          supportedLocales: <Locale>[locale],
          locale: locale,
37
          localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
38 39
            GlobalMaterialLocalizations.delegate,
          ],
40
          home: Builder(builder: (BuildContext context) {
41
            completer.complete(MaterialLocalizations.of(context).formatHour(timeOfDay));
42
            return Container();
43 44 45 46 47 48 49 50
          }),
        ));
        return completer.future;
      }

      testWidgets('formats h', (WidgetTester tester) async {
        expect(await formatHour(tester, const Locale('en', 'US'), const TimeOfDay(hour: 10, minute: 0)), '10');
        expect(await formatHour(tester, const Locale('en', 'US'), const TimeOfDay(hour: 20, minute: 0)), '8');
51 52
      });

53
      testWidgets('formats HH', (WidgetTester tester) async {
54 55
        expect(await formatHour(tester, const Locale('de'), const TimeOfDay(hour: 9, minute: 0)), '09');
        expect(await formatHour(tester, const Locale('de'), const TimeOfDay(hour: 20, minute: 0)), '20');
56

57 58
        expect(await formatHour(tester, const Locale('en', 'GB'), const TimeOfDay(hour: 9, minute: 0)), '09');
        expect(await formatHour(tester, const Locale('en', 'GB'), const TimeOfDay(hour: 20, minute: 0)), '20');
59 60
      });

61
      testWidgets('formats H', (WidgetTester tester) async {
62 63
        expect(await formatHour(tester, const Locale('es'), const TimeOfDay(hour: 9, minute: 0)), '9');
        expect(await formatHour(tester, const Locale('es'), const TimeOfDay(hour: 20, minute: 0)), '20');
64

65 66
        expect(await formatHour(tester, const Locale('fa'), const TimeOfDay(hour: 9, minute: 0)), '۹');
        expect(await formatHour(tester, const Locale('fa'), const TimeOfDay(hour: 20, minute: 0)), '۲۰');
67 68 69 70
      });
    });

    group('formatMinute', () {
71
      test('formats English', () async {
72 73
        final GlobalMaterialLocalizations localizations =
          await GlobalMaterialLocalizations.delegate.load(const Locale('en', 'US')) as GlobalMaterialLocalizations;
74 75 76 77 78
        expect(localizations.formatMinute(const TimeOfDay(hour: 1, minute: 32)), '32');
      });
    });

    group('formatTimeOfDay', () {
79
      Future<String> formatTimeOfDay(WidgetTester tester, Locale locale, TimeOfDay timeOfDay) async {
80 81
        final Completer<String> completer = Completer<String>();
        await tester.pumpWidget(MaterialApp(
82 83
          supportedLocales: <Locale>[locale],
          locale: locale,
84
          localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
85 86
            GlobalMaterialLocalizations.delegate,
          ],
87
          home: Builder(builder: (BuildContext context) {
88
            completer.complete(MaterialLocalizations.of(context).formatTimeOfDay(timeOfDay));
89
            return Container();
90 91 92 93 94 95
          }),
        ));
        return completer.future;
      }

      testWidgets('formats ${TimeOfDayFormat.h_colon_mm_space_a}', (WidgetTester tester) async {
96 97
        expect(await formatTimeOfDay(tester, const Locale('en'), const TimeOfDay(hour: 9, minute: 32)), '9:32 AM');
        expect(await formatTimeOfDay(tester, const Locale('en'), const TimeOfDay(hour: 20, minute: 32)), '8:32 PM');
98 99
      });

100
      testWidgets('formats ${TimeOfDayFormat.HH_colon_mm}', (WidgetTester tester) async {
101
        expect(await formatTimeOfDay(tester, const Locale('de'), const TimeOfDay(hour: 9, minute: 32)), '09:32');
102
        expect(await formatTimeOfDay(tester, const Locale('en', 'ZA'), const TimeOfDay(hour: 9, minute: 32)), '09:32');
103 104
      });

105
      testWidgets('formats ${TimeOfDayFormat.H_colon_mm}', (WidgetTester tester) async {
106 107
        expect(await formatTimeOfDay(tester, const Locale('es'), const TimeOfDay(hour: 9, minute: 32)), '9:32');
        expect(await formatTimeOfDay(tester, const Locale('es'), const TimeOfDay(hour: 20, minute: 32)), '20:32');
108

109 110
        expect(await formatTimeOfDay(tester, const Locale('ja'), const TimeOfDay(hour: 9, minute: 32)), '9:32');
        expect(await formatTimeOfDay(tester, const Locale('ja'), const TimeOfDay(hour: 20, minute: 32)), '20:32');
111 112
      });

113 114
      testWidgets('formats ${TimeOfDayFormat.frenchCanadian}', (WidgetTester tester) async {
        expect(await formatTimeOfDay(tester, const Locale('fr', 'CA'), const TimeOfDay(hour: 9, minute: 32)), '09 h 32');
115 116
      });

117
      testWidgets('formats ${TimeOfDayFormat.a_space_h_colon_mm}', (WidgetTester tester) async {
118
        expect(await formatTimeOfDay(tester, const Locale('zh'), const TimeOfDay(hour: 9, minute: 32)), '上午 9:32');
119 120
      });
    });
121 122 123

    group('date formatters', () {
      Future<Map<DateType, String>> formatDate(WidgetTester tester, Locale locale, DateTime dateTime) async {
124 125
        final Completer<Map<DateType, String>> completer = Completer<Map<DateType, String>>();
        await tester.pumpWidget(MaterialApp(
126 127 128 129 130
          supportedLocales: <Locale>[locale],
          locale: locale,
          localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
            GlobalMaterialLocalizations.delegate,
          ],
131
          home: Builder(builder: (BuildContext context) {
132 133 134 135 136 137 138
            final MaterialLocalizations localizations = MaterialLocalizations.of(context);
            completer.complete(<DateType, String>{
              DateType.year: localizations.formatYear(dateTime),
              DateType.medium: localizations.formatMediumDate(dateTime),
              DateType.full: localizations.formatFullDate(dateTime),
              DateType.monthYear: localizations.formatMonthYear(dateTime),
            });
139
            return Container();
140 141 142 143 144 145
          }),
        ));
        return completer.future;
      }

      testWidgets('formats dates in English', (WidgetTester tester) async {
146 147 148 149 150
        final Map<DateType, String> formatted = await formatDate(tester, const Locale('en'), DateTime(2018, 8, 1));
        expect(formatted[DateType.year], '2018');
        expect(formatted[DateType.medium], 'Wed, Aug 1');
        expect(formatted[DateType.full], 'Wednesday, August 1, 2018');
        expect(formatted[DateType.monthYear], 'August 2018');
151 152 153
      });

      testWidgets('formats dates in German', (WidgetTester tester) async {
154
        final Map<DateType, String> formatted = await formatDate(tester, const Locale('de'), DateTime(2018, 8, 1));
155 156 157 158 159 160
        expect(formatted[DateType.year], '2018');
        expect(formatted[DateType.medium], 'Mi., 1. Aug.');
        expect(formatted[DateType.full], 'Mittwoch, 1. August 2018');
        expect(formatted[DateType.monthYear], 'August 2018');
      });
    });
161 162
  });
}
163 164

enum DateType { year, medium, full, monthYear }