date_time_test.dart 9.23 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
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
10
import 'package:intl/intl.dart';
11 12 13

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

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

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

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

52
      testWidgets('formats HH', (WidgetTester tester) async {
53 54
        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');
55

56 57
        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');
58 59
      });

60
      testWidgets('formats H', (WidgetTester tester) async {
61 62
        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');
63

64 65
        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)), '۲۰');
66 67 68 69
      });
    });

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

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

      testWidgets('formats ${TimeOfDayFormat.h_colon_mm_space_a}', (WidgetTester tester) async {
93 94
        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');
95 96
      });

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

102
      testWidgets('formats ${TimeOfDayFormat.H_colon_mm}', (WidgetTester tester) async {
103 104
        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');
105

106 107
        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');
108 109
      });

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

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

    group('date formatters', () {
      Future<Map<DateType, String>> formatDate(WidgetTester tester, Locale locale, DateTime dateTime) async {
121 122
        final Completer<Map<DateType, String>> completer = Completer<Map<DateType, String>>();
        await tester.pumpWidget(MaterialApp(
123 124
          supportedLocales: <Locale>[locale],
          locale: locale,
125
          localizationsDelegates: GlobalMaterialLocalizations.delegates,
126
          home: Builder(builder: (BuildContext context) {
127 128 129 130 131 132 133
            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),
            });
134
            return Container();
135 136 137 138 139 140
          }),
        ));
        return completer.future;
      }

      testWidgets('formats dates in English', (WidgetTester tester) async {
141
        final Map<DateType, String> formatted = await formatDate(tester, const Locale('en'), DateTime(2018, 8));
142 143 144 145
        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');
146 147 148
      });

      testWidgets('formats dates in German', (WidgetTester tester) async {
149
        final Map<DateType, String> formatted = await formatDate(tester, const Locale('de'), DateTime(2018, 8));
150 151 152 153 154
        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');
      });
155 156

      testWidgets('formats dates in Serbian', (WidgetTester tester) async {
157
        final Map<DateType, String> formatted = await formatDate(tester, const Locale('sr'), DateTime(2018, 8));
158 159 160 161 162 163 164 165
        expect(formatted[DateType.year], '2018.');
        expect(formatted[DateType.medium], 'сре 1. авг');
        expect(formatted[DateType.full], 'среда, 1. август 2018.');
        expect(formatted[DateType.monthYear], 'август 2018.');
      });

      testWidgets('formats dates in Serbian (Latin)', (WidgetTester tester) async {
        final Map<DateType, String> formatted = await formatDate(tester,
166
          const Locale.fromSubtags(languageCode:'sr', scriptCode: 'Latn'), DateTime(2018, 8));
167 168 169 170 171
        expect(formatted[DateType.year], '2018.');
        expect(formatted[DateType.medium], 'sre 1. avg');
        expect(formatted[DateType.full], 'sreda, 1. avgust 2018.');
        expect(formatted[DateType.monthYear], 'avgust 2018.');
      });
172
    });
173
  });
174 175 176

  // Regression test for https://github.com/flutter/flutter/issues/67644.
  testWidgets('en_US is initialized correctly by Flutter when DateFormat is used', (WidgetTester tester) async {
177
    late DateFormat dateFormat;
178 179 180

    await tester.pumpWidget(MaterialApp(
      locale: const Locale('en', 'US'),
181
      localizationsDelegates: GlobalMaterialLocalizations.delegates,
182 183 184 185 186 187
      home: Builder(builder: (BuildContext context) {
        dateFormat = DateFormat('EEE, d MMM yyyy HH:mm:ss', 'en_US');
        return Container();
      }),
    ));

188
    expect(dateFormat.locale, 'en_US');
189
  });
190
}
191 192

enum DateType { year, medium, full, monthYear }