localizations_test.dart 10.9 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
9
  testWidgets('English translations exist for all MaterialLocalizations properties', (WidgetTester tester) async {
10
    const MaterialLocalizations localizations = DefaultMaterialLocalizations();
11 12 13 14

    expect(localizations.openAppDrawerTooltip, isNotNull);
    expect(localizations.backButtonTooltip, isNotNull);
    expect(localizations.closeButtonTooltip, isNotNull);
15
    expect(localizations.deleteButtonTooltip, isNotNull);
16
    expect(localizations.moreButtonTooltip, isNotNull);
17 18 19 20
    expect(localizations.nextMonthTooltip, isNotNull);
    expect(localizations.previousMonthTooltip, isNotNull);
    expect(localizations.nextPageTooltip, isNotNull);
    expect(localizations.previousPageTooltip, isNotNull);
21 22
    expect(localizations.firstPageTooltip, isNotNull);
    expect(localizations.lastPageTooltip, isNotNull);
23 24 25 26 27 28 29 30 31 32 33 34
    expect(localizations.showMenuTooltip, isNotNull);
    expect(localizations.licensesPageTitle, isNotNull);
    expect(localizations.rowsPerPageTitle, isNotNull);
    expect(localizations.cancelButtonLabel, isNotNull);
    expect(localizations.closeButtonLabel, isNotNull);
    expect(localizations.continueButtonLabel, isNotNull);
    expect(localizations.copyButtonLabel, isNotNull);
    expect(localizations.cutButtonLabel, isNotNull);
    expect(localizations.okButtonLabel, isNotNull);
    expect(localizations.pasteButtonLabel, isNotNull);
    expect(localizations.selectAllButtonLabel, isNotNull);
    expect(localizations.viewLicensesButtonLabel, isNotNull);
35 36 37 38
    expect(localizations.anteMeridiemAbbreviation, isNotNull);
    expect(localizations.postMeridiemAbbreviation, isNotNull);
    expect(localizations.timePickerHourModeAnnouncement, isNotNull);
    expect(localizations.timePickerMinuteModeAnnouncement, isNotNull);
39
    expect(localizations.modalBarrierDismissLabel, isNotNull);
40
    expect(localizations.drawerLabel, isNotNull);
41
    expect(localizations.menuBarMenuLabel, isNotNull);
42 43 44
    expect(localizations.popupMenuLabel, isNotNull);
    expect(localizations.dialogLabel, isNotNull);
    expect(localizations.alertDialogLabel, isNotNull);
45
    expect(localizations.searchFieldLabel, isNotNull);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    expect(localizations.dateSeparator, isNotNull);
    expect(localizations.dateHelpText, isNotNull);
    expect(localizations.selectYearSemanticsLabel, isNotNull);
    expect(localizations.unspecifiedDate, isNotNull);
    expect(localizations.unspecifiedDateRange, isNotNull);
    expect(localizations.dateInputLabel, isNotNull);
    expect(localizations.dateRangeStartLabel, isNotNull);
    expect(localizations.dateRangeEndLabel, isNotNull);
    expect(localizations.invalidDateFormatLabel, isNotNull);
    expect(localizations.invalidDateRangeLabel, isNotNull);
    expect(localizations.dateOutOfRangeLabel, isNotNull);
    expect(localizations.saveButtonLabel, isNotNull);
    expect(localizations.datePickerHelpText, isNotNull);
    expect(localizations.dateRangePickerHelpText, isNotNull);
    expect(localizations.calendarModeButtonLabel, isNotNull);
    expect(localizations.inputDateModeButtonLabel, isNotNull);
62 63 64 65 66 67 68
    expect(localizations.timePickerDialHelpText, isNotNull);
    expect(localizations.timePickerInputHelpText, isNotNull);
    expect(localizations.timePickerHourLabel, isNotNull);
    expect(localizations.timePickerMinuteLabel, isNotNull);
    expect(localizations.invalidTimeLabel, isNotNull);
    expect(localizations.dialModeButtonLabel, isNotNull);
    expect(localizations.inputTimeModeButtonLabel, isNotNull);
69 70 71
    expect(localizations.signedInLabel, isNotNull);
    expect(localizations.hideAccountsLabel, isNotNull);
    expect(localizations.showAccountsLabel, isNotNull);
72 73 74 75 76 77 78 79 80 81 82 83
    expect(localizations.reorderItemToStart, isNotNull);
    expect(localizations.reorderItemToEnd, isNotNull);
    expect(localizations.reorderItemUp, isNotNull);
    expect(localizations.reorderItemDown, isNotNull);
    expect(localizations.reorderItemLeft, isNotNull);
    expect(localizations.reorderItemRight, isNotNull);
    expect(localizations.expandedIconTapHint, isNotNull);
    expect(localizations.collapsedIconTapHint, isNotNull);
    expect(localizations.expansionTileExpandedHint, isNotNull);
    expect(localizations.expansionTileCollapsedHint, isNotNull);
    expect(localizations.expandedHint, isNotNull);
    expect(localizations.collapsedHint, isNotNull);
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
    expect(localizations.keyboardKeyAlt, isNotNull);
    expect(localizations.keyboardKeyAltGraph, isNotNull);
    expect(localizations.keyboardKeyBackspace, isNotNull);
    expect(localizations.keyboardKeyCapsLock, isNotNull);
    expect(localizations.keyboardKeyChannelDown, isNotNull);
    expect(localizations.keyboardKeyChannelUp, isNotNull);
    expect(localizations.keyboardKeyControl, isNotNull);
    expect(localizations.keyboardKeyDelete, isNotNull);
    expect(localizations.keyboardKeyEject, isNotNull);
    expect(localizations.keyboardKeyEnd, isNotNull);
    expect(localizations.keyboardKeyEscape, isNotNull);
    expect(localizations.keyboardKeyFn, isNotNull);
    expect(localizations.keyboardKeyHome, isNotNull);
    expect(localizations.keyboardKeyInsert, isNotNull);
    expect(localizations.keyboardKeyMeta, isNotNull);
    expect(localizations.keyboardKeyMetaMacOs, isNotNull);
    expect(localizations.keyboardKeyMetaWindows, isNotNull);
    expect(localizations.keyboardKeyNumLock, isNotNull);
    expect(localizations.keyboardKeyNumpad1, isNotNull);
    expect(localizations.keyboardKeyNumpad2, isNotNull);
    expect(localizations.keyboardKeyNumpad3, isNotNull);
    expect(localizations.keyboardKeyNumpad4, isNotNull);
    expect(localizations.keyboardKeyNumpad5, isNotNull);
    expect(localizations.keyboardKeyNumpad6, isNotNull);
    expect(localizations.keyboardKeyNumpad7, isNotNull);
    expect(localizations.keyboardKeyNumpad8, isNotNull);
    expect(localizations.keyboardKeyNumpad9, isNotNull);
    expect(localizations.keyboardKeyNumpad0, isNotNull);
    expect(localizations.keyboardKeyNumpadAdd, isNotNull);
    expect(localizations.keyboardKeyNumpadComma, isNotNull);
    expect(localizations.keyboardKeyNumpadDecimal, isNotNull);
    expect(localizations.keyboardKeyNumpadDivide, isNotNull);
    expect(localizations.keyboardKeyNumpadEnter, isNotNull);
    expect(localizations.keyboardKeyNumpadEqual, isNotNull);
    expect(localizations.keyboardKeyNumpadMultiply, isNotNull);
    expect(localizations.keyboardKeyNumpadParenLeft, isNotNull);
    expect(localizations.keyboardKeyNumpadParenRight, isNotNull);
    expect(localizations.keyboardKeyNumpadSubtract, isNotNull);
    expect(localizations.keyboardKeyPageDown, isNotNull);
    expect(localizations.keyboardKeyPageUp, isNotNull);
    expect(localizations.keyboardKeyPower, isNotNull);
    expect(localizations.keyboardKeyPowerOff, isNotNull);
    expect(localizations.keyboardKeyPrintScreen, isNotNull);
    expect(localizations.keyboardKeyScrollLock, isNotNull);
    expect(localizations.keyboardKeySelect, isNotNull);
129
    expect(localizations.keyboardKeyShift, isNotNull);
130
    expect(localizations.keyboardKeySpace, isNotNull);
131
    expect(localizations.currentDateLabel, isNotNull);
132 133 134 135
    expect(localizations.scrimLabel, isNotNull);
    expect(localizations.bottomSheetLabel, isNotNull);

    expect(localizations.scrimOnTapHint('FOO'), contains('FOO'));
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

    expect(localizations.aboutListTileTitle('FOO'), isNotNull);
    expect(localizations.aboutListTileTitle('FOO'), contains('FOO'));

    expect(localizations.selectedRowCountTitle(0), isNotNull);
    expect(localizations.selectedRowCountTitle(1), isNotNull);
    expect(localizations.selectedRowCountTitle(2), isNotNull);
    expect(localizations.selectedRowCountTitle(100), isNotNull);
    expect(localizations.selectedRowCountTitle(0).contains(r'$selectedRowCount'), isFalse);
    expect(localizations.selectedRowCountTitle(1).contains(r'$selectedRowCount'), isFalse);
    expect(localizations.selectedRowCountTitle(2).contains(r'$selectedRowCount'), isFalse);
    expect(localizations.selectedRowCountTitle(100).contains(r'$selectedRowCount'), isFalse);

    expect(localizations.pageRowsInfoTitle(1, 10, 100, true), isNotNull);
    expect(localizations.pageRowsInfoTitle(1, 10, 100, false), isNotNull);
    expect(localizations.pageRowsInfoTitle(1, 10, 100, true).contains(r'$firstRow'), isFalse);
    expect(localizations.pageRowsInfoTitle(1, 10, 100, true).contains(r'$lastRow'), isFalse);
    expect(localizations.pageRowsInfoTitle(1, 10, 100, true).contains(r'$rowCount'), isFalse);
    expect(localizations.pageRowsInfoTitle(1, 10, 100, false).contains(r'$firstRow'), isFalse);
    expect(localizations.pageRowsInfoTitle(1, 10, 100, false).contains(r'$lastRow'), isFalse);
    expect(localizations.pageRowsInfoTitle(1, 10, 100, false).contains(r'$rowCount'), isFalse);
157

158
    expect(localizations.licensesPackageDetailText(0), isNotNull);
159 160 161 162 163 164
    expect(localizations.licensesPackageDetailText(1), isNotNull);
    expect(localizations.licensesPackageDetailText(2), isNotNull);
    expect(localizations.licensesPackageDetailText(100), isNotNull);
    expect(localizations.licensesPackageDetailText(1).contains(r'$licensesCount'), isFalse);
    expect(localizations.licensesPackageDetailText(2).contains(r'$licensesCount'), isFalse);
    expect(localizations.licensesPackageDetailText(100).contains(r'$licensesCount'), isFalse);
165
  });
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

  testWidgets('MaterialLocalizations.of throws', (WidgetTester tester) async {
    final GlobalKey noLocalizationsAvailable = GlobalKey();
    final GlobalKey localizationsAvailable = GlobalKey();

    await tester.pumpWidget(
      Container(
        key: noLocalizationsAvailable,
        child: MaterialApp(
          home: Container(
            key: localizationsAvailable,
          ),
        ),
      ),
    );

    expect(() => MaterialLocalizations.of(noLocalizationsAvailable.currentContext!), throwsA(isAssertionError.having(
      (AssertionError e) => e.message,
      'message',
      contains('No MaterialLocalizations found'),
    )));

    expect(MaterialLocalizations.of(localizationsAvailable.currentContext!), isA<MaterialLocalizations>());
  });
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

  testWidgets("parseCompactDate doesn't throw an exception on invalid text", (WidgetTester tester) async {
    // This is a regression test for https://github.com/flutter/flutter/issues/126397.
    final GlobalKey localizations = GlobalKey();

    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          key: localizations,
          child: const SizedBox.expand(),
        ),
      ),
    );

    final MaterialLocalizations materialLocalizations = MaterialLocalizations.of(localizations.currentContext!);
    expect(materialLocalizations.parseCompactDate('10/05/2023'), isNotNull);
    expect(tester.takeException(), null);

    expect(materialLocalizations.parseCompactDate('10/05/2023666777889'), null);
    expect(tester.takeException(), null);
  });
211
}