|
| 1 | +// Copyright 2022, the Chromium project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:firebase_ui_localizations/firebase_ui_localizations.dart'; |
| 6 | +import 'package:firebase_ui_localizations/src/lang/zh_tw.dart'; |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | +import 'package:flutter_localizations/flutter_localizations.dart'; |
| 9 | +import 'package:flutter_test/flutter_test.dart'; |
| 10 | + |
| 11 | +const localeZh = Locale('zh'); |
| 12 | +const localeTW = Locale('zh', 'TW'); |
| 13 | + |
| 14 | +Future<void> main() async { |
| 15 | + late FirebaseUILocalizationDelegate delegate; |
| 16 | + |
| 17 | + group( |
| 18 | + 'FirebaseUILocalization loads the appropriate Chinese translation', |
| 19 | + () { |
| 20 | + localizeText(BuildContext context) { |
| 21 | + final labels = FirebaseUILocalizations.labelsOf(context); |
| 22 | + return labels.signInWithPhoneButtonText; |
| 23 | + } |
| 24 | + |
| 25 | + setUp(() async { |
| 26 | + delegate = const FirebaseUILocalizationDelegate(); |
| 27 | + }); |
| 28 | + |
| 29 | + test( |
| 30 | + 'Loads the correct translation with the language tag "${localeZh.toLanguageTag()}"', |
| 31 | + () async { |
| 32 | + final localizations = await delegate.load(localeZh); |
| 33 | + expect(localizations.labels.signInWithPhoneButtonText, '使用电话号码登录'); |
| 34 | + }, |
| 35 | + ); |
| 36 | + |
| 37 | + testWidgets( |
| 38 | + 'UI test for the "${localeZh.toLanguageTag()}" translation', |
| 39 | + (tester) async { |
| 40 | + await tester.pumpWidget( |
| 41 | + TestMaterialApp( |
| 42 | + locale: localeZh, |
| 43 | + localizeText: localizeText, |
| 44 | + ), |
| 45 | + ); |
| 46 | + expect(find.text('使用电话号码登录'), findsOneWidget); |
| 47 | + }, |
| 48 | + ); |
| 49 | + |
| 50 | + test( |
| 51 | + 'Loads the correct translation with the language tag "${localeTW.toLanguageTag()}"', |
| 52 | + () async { |
| 53 | + final localizations = await delegate.load(localeTW); |
| 54 | + expect(localizations.labels.signInWithPhoneButtonText, '使用電話號碼登入'); |
| 55 | + }, |
| 56 | + ); |
| 57 | + |
| 58 | + testWidgets( |
| 59 | + 'UI test for the "${localeTW.toLanguageTag()}" translation', |
| 60 | + (tester) async { |
| 61 | + await tester.pumpWidget( |
| 62 | + TestMaterialApp( |
| 63 | + locale: localeTW, |
| 64 | + localizeText: localizeText, |
| 65 | + ), |
| 66 | + ); |
| 67 | + expect(find.text('使用電話號碼登入'), findsOneWidget); |
| 68 | + }, |
| 69 | + ); |
| 70 | + }, |
| 71 | + ); |
| 72 | + |
| 73 | + group( |
| 74 | + 'Localization override', |
| 75 | + () { |
| 76 | + localizeText(BuildContext context) { |
| 77 | + return FirebaseUILocalizations.labelsOf(context).verifyEmailTitle; |
| 78 | + } |
| 79 | + |
| 80 | + test( |
| 81 | + 'Overrides the DefaultLocalizations', |
| 82 | + () async { |
| 83 | + final localizations = await const FirebaseUILocalizationDelegate( |
| 84 | + DefaultLocalizationsOverrides(), |
| 85 | + ).load(localeTW); |
| 86 | + expect(localizations.labels.verifyEmailTitle, 'Overwritten'); |
| 87 | + }, |
| 88 | + ); |
| 89 | + |
| 90 | + testWidgets( |
| 91 | + 'UI test for the default translation override', |
| 92 | + (tester) async { |
| 93 | + await tester.pumpWidget( |
| 94 | + TestMaterialApp( |
| 95 | + locale: localeZh, |
| 96 | + localizationsOverride: const FirebaseUILocalizationDelegate( |
| 97 | + DefaultLocalizationsOverrides(), |
| 98 | + ), |
| 99 | + localizeText: localizeText, |
| 100 | + ), |
| 101 | + ); |
| 102 | + expect(find.text('Overwritten'), findsOneWidget); |
| 103 | + }, |
| 104 | + ); |
| 105 | + |
| 106 | + test( |
| 107 | + 'Overrides the DefaultLocalizations', |
| 108 | + () async { |
| 109 | + final localizations = await const FirebaseUILocalizationDelegate( |
| 110 | + ZhTWLocalizationsOverrides(), |
| 111 | + ).load(localeTW); |
| 112 | + expect(localizations.labels.verifyEmailTitle, '覆寫標題'); |
| 113 | + }, |
| 114 | + ); |
| 115 | + |
| 116 | + testWidgets( |
| 117 | + 'UI test for the "${localeTW.toLanguageTag()}" translation override', |
| 118 | + (tester) async { |
| 119 | + await tester.pumpWidget( |
| 120 | + TestMaterialApp( |
| 121 | + locale: localeTW, |
| 122 | + localizationsOverride: const FirebaseUILocalizationDelegate( |
| 123 | + ZhTWLocalizationsOverrides(), |
| 124 | + ), |
| 125 | + localizeText: localizeText, |
| 126 | + ), |
| 127 | + ); |
| 128 | + expect(find.text('覆寫標題'), findsOneWidget); |
| 129 | + }, |
| 130 | + ); |
| 131 | + }, |
| 132 | + ); |
| 133 | +} |
| 134 | + |
| 135 | +class DefaultLocalizationsOverrides extends DefaultLocalizations { |
| 136 | + const DefaultLocalizationsOverrides(); |
| 137 | + |
| 138 | + @override |
| 139 | + String get verifyEmailTitle => 'Overwritten'; |
| 140 | +} |
| 141 | + |
| 142 | +class ZhTWLocalizationsOverrides extends ZhTWLocalizations { |
| 143 | + const ZhTWLocalizationsOverrides(); |
| 144 | + |
| 145 | + @override |
| 146 | + String get verifyEmailTitle => '覆寫標題'; |
| 147 | +} |
| 148 | + |
| 149 | +class TestMaterialApp extends StatelessWidget { |
| 150 | + final Locale locale; |
| 151 | + final LocalizationsDelegate? localizationsOverride; |
| 152 | + final String Function(BuildContext context) localizeText; |
| 153 | + |
| 154 | + const TestMaterialApp({ |
| 155 | + super.key, |
| 156 | + required this.locale, |
| 157 | + this.localizationsOverride, |
| 158 | + required this.localizeText, |
| 159 | + }); |
| 160 | + |
| 161 | + @override |
| 162 | + Widget build(BuildContext context) { |
| 163 | + return MaterialApp( |
| 164 | + supportedLocales: const [localeZh, localeTW], |
| 165 | + locale: locale, |
| 166 | + localizationsDelegates: [ |
| 167 | + GlobalMaterialLocalizations.delegate, |
| 168 | + GlobalCupertinoLocalizations.delegate, |
| 169 | + localizationsOverride == null |
| 170 | + ? FirebaseUILocalizations.delegate |
| 171 | + : localizationsOverride!, |
| 172 | + ], |
| 173 | + home: Builder( |
| 174 | + builder: (context) => Text( |
| 175 | + localizeText.call(context), |
| 176 | + ), |
| 177 | + ), |
| 178 | + ); |
| 179 | + } |
| 180 | +} |
0 commit comments