Unverified Commit daa52b25 authored by Mitchell Goodwin's avatar Mitchell Goodwin Committed by GitHub

Seperate localization tests for Material2 and Material3 (#135779)

Seperates tests for the localizations package into Material2 and Material3 versions and removes dependency on theme.

More info in #127064
parent 0fa01b21
...@@ -7,7 +7,7 @@ import 'package:flutter_localizations/flutter_localizations.dart'; ...@@ -7,7 +7,7 @@ import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
void main() { void main() {
testWidgets('Nested Localizations', (WidgetTester tester) async { testWidgets('Material3 - Nested Localizations', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp( // Creates the outer Localizations widget. await tester.pumpWidget(MaterialApp( // Creates the outer Localizations widget.
theme: ThemeData(useMaterial3: true), theme: ThemeData(useMaterial3: true),
home: ListView( home: ListView(
...@@ -29,6 +29,28 @@ void main() { ...@@ -29,6 +29,28 @@ void main() {
expect(innerTracker.textBaseline, TextBaseline.ideographic); expect(innerTracker.textBaseline, TextBaseline.ideographic);
}); });
testWidgets('Material2 - Nested Localizations', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp( // Creates the outer Localizations widget.
theme: ThemeData(useMaterial3: false),
home: ListView(
children: <Widget>[
const LocalizationTracker(key: ValueKey<String>('outer')),
Localizations(
locale: const Locale('zh', 'CN'),
delegates: GlobalMaterialLocalizations.delegates,
child: const LocalizationTracker(key: ValueKey<String>('inner')),
),
],
),
));
// Most localized aspects of the TextTheme text styles are the same for the default US local and
// for Chinese for Material2. The baselines for all text styles differ.
final LocalizationTrackerState outerTracker = tester.state(find.byKey(const ValueKey<String>('outer'), skipOffstage: false));
expect(outerTracker.textBaseline, TextBaseline.alphabetic);
final LocalizationTrackerState innerTracker = tester.state(find.byKey(const ValueKey<String>('inner'), skipOffstage: false));
expect(innerTracker.textBaseline, TextBaseline.ideographic);
});
testWidgets('Localizations is compatible with ChangeNotifier.dispose() called during didChangeDependencies', (WidgetTester tester) async { testWidgets('Localizations is compatible with ChangeNotifier.dispose() called during didChangeDependencies', (WidgetTester tester) async {
// PageView calls ScrollPosition.dispose() during didChangeDependencies. // PageView calls ScrollPosition.dispose() during didChangeDependencies.
await tester.pumpWidget( await tester.pumpWidget(
......
...@@ -92,10 +92,10 @@ void main() { ...@@ -92,10 +92,10 @@ void main() {
} }
}); });
testWidgets('locale parameter overrides ambient locale', (WidgetTester tester) async { testWidgets('Material2 - locale parameter overrides ambient locale', (WidgetTester tester) async {
Widget buildFrame(bool useMaterial3) { Widget buildFrame() {
return MaterialApp( return MaterialApp(
theme: ThemeData(useMaterial3: useMaterial3), theme: ThemeData(useMaterial3: false),
locale: const Locale('en', 'US'), locale: const Locale('en', 'US'),
supportedLocales: const <Locale>[ supportedLocales: const <Locale>[
Locale('en', 'US'), Locale('en', 'US'),
...@@ -125,7 +125,7 @@ void main() { ...@@ -125,7 +125,7 @@ void main() {
Element getPicker() => tester.element(find.byType(CalendarDatePicker)); Element getPicker() => tester.element(find.byType(CalendarDatePicker));
await tester.pumpWidget(buildFrame(true)); await tester.pumpWidget(buildFrame());
await tester.tap(find.text('X')); await tester.tap(find.text('X'));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
...@@ -138,13 +138,43 @@ void main() { ...@@ -138,13 +138,43 @@ void main() {
TextDirection.ltr, TextDirection.ltr,
); );
await tester.tap(find.text('Annuler')); await tester.tap(find.text('ANNULER'));
});
testWidgets('Material3 - locale parameter overrides ambient locale', (WidgetTester tester) async {
Widget buildFrame() {
return MaterialApp(
theme: ThemeData(useMaterial3: true),
locale: const Locale('en', 'US'),
supportedLocales: const <Locale>[
Locale('en', 'US'),
Locale('fr', 'CA'),
],
localizationsDelegates: GlobalMaterialLocalizations.delegates,
home: Material(
child: Builder(
builder: (BuildContext context) {
return TextButton(
onPressed: () async {
await showDatePicker(
context: context,
initialDate: initialDate,
firstDate: firstDate,
lastDate: lastDate,
locale: const Locale('fr', 'CA'),
);
},
child: const Text('X'),
);
},
),
),
);
}
// The tests below are only relevant for Material 2. Once Material 2 Element getPicker() => tester.element(find.byType(CalendarDatePicker));
// support is deprecated and the APIs are removed, these tests
// can be deleted.
await tester.pumpWidget(buildFrame(false)); await tester.pumpWidget(buildFrame());
await tester.tap(find.text('X')); await tester.tap(find.text('X'));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
...@@ -157,14 +187,13 @@ void main() { ...@@ -157,14 +187,13 @@ void main() {
TextDirection.ltr, TextDirection.ltr,
); );
await tester.tap(find.text('ANNULER')); await tester.tap(find.text('Annuler'));
}); });
testWidgets('textDirection parameter overrides ambient textDirection', (WidgetTester tester) async { testWidgets('Material2 - textDirection parameter overrides ambient textDirection', (WidgetTester tester) async {
Widget buildFrame(bool useMaterial3) { Widget buildFrame() {
return MaterialApp( return MaterialApp(
theme: ThemeData(useMaterial3: useMaterial3), theme: ThemeData(useMaterial3: false),
locale: const Locale('en', 'US'), locale: const Locale('en', 'US'),
home: Material( home: Material(
child: Builder( child: Builder(
...@@ -189,7 +218,7 @@ void main() { ...@@ -189,7 +218,7 @@ void main() {
Element getPicker() => tester.element(find.byType(CalendarDatePicker)); Element getPicker() => tester.element(find.byType(CalendarDatePicker));
await tester.pumpWidget(buildFrame(true)); await tester.pumpWidget(buildFrame());
await tester.tap(find.text('X')); await tester.tap(find.text('X'));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
...@@ -198,13 +227,38 @@ void main() { ...@@ -198,13 +227,38 @@ void main() {
TextDirection.rtl, TextDirection.rtl,
); );
await tester.tap(find.text('Cancel')); await tester.tap(find.text('CANCEL'));
});
// The tests below are only relevant for Material 2. Once Material 2 testWidgets('Material3 - textDirection parameter overrides ambient textDirection', (WidgetTester tester) async {
// support is deprecated and the APIs are removed, these tests Widget buildFrame() {
// can be deleted. return MaterialApp(
theme: ThemeData(useMaterial3: true),
locale: const Locale('en', 'US'),
home: Material(
child: Builder(
builder: (BuildContext context) {
return TextButton(
onPressed: () async {
await showDatePicker(
context: context,
initialDate: initialDate,
firstDate: firstDate,
lastDate: lastDate,
textDirection: TextDirection.rtl,
);
},
child: const Text('X'),
);
},
),
),
);
}
Element getPicker() => tester.element(find.byType(CalendarDatePicker));
await tester.pumpWidget(buildFrame(false)); await tester.pumpWidget(buildFrame());
await tester.tap(find.text('X')); await tester.tap(find.text('X'));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
...@@ -213,13 +267,13 @@ void main() { ...@@ -213,13 +267,13 @@ void main() {
TextDirection.rtl, TextDirection.rtl,
); );
await tester.tap(find.text('CANCEL')); await tester.tap(find.text('Cancel'));
}); });
testWidgets('textDirection parameter takes precedence over locale parameter', (WidgetTester tester) async { testWidgets('Material2 - textDirection parameter takes precedence over locale parameter', (WidgetTester tester) async {
Widget buildFrame(bool useMaterial3) { Widget buildFrame() {
return MaterialApp( return MaterialApp(
theme: ThemeData(useMaterial3: useMaterial3), theme: ThemeData(useMaterial3: false),
locale: const Locale('en', 'US'), locale: const Locale('en', 'US'),
supportedLocales: const <Locale>[ supportedLocales: const <Locale>[
Locale('en', 'US'), Locale('en', 'US'),
...@@ -250,9 +304,9 @@ void main() { ...@@ -250,9 +304,9 @@ void main() {
Element getPicker() => tester.element(find.byType(CalendarDatePicker)); Element getPicker() => tester.element(find.byType(CalendarDatePicker));
await tester.pumpWidget(buildFrame(true)); await tester.pumpWidget(buildFrame());
await tester.tap(find.text('X')); await tester.tap(find.text('X'));
await tester.pumpAndSettle(const Duration(seconds: 1)); await tester.pumpAndSettle();
expect( expect(
Localizations.localeOf(getPicker()), Localizations.localeOf(getPicker()),
...@@ -264,15 +318,46 @@ void main() { ...@@ -264,15 +318,46 @@ void main() {
TextDirection.rtl, TextDirection.rtl,
); );
await tester.tap(find.text('Annuler')); await tester.tap(find.text('ANNULER'));
});
// The tests below are only relevant for Material 2. Once Material 2 testWidgets('Material3 - textDirection parameter takes precedence over locale parameter', (WidgetTester tester) async {
// support is deprecated and the APIs are removed, these tests Widget buildFrame() {
// can be deleted. return MaterialApp(
theme: ThemeData(useMaterial3: true),
locale: const Locale('en', 'US'),
supportedLocales: const <Locale>[
Locale('en', 'US'),
Locale('fr', 'CA'),
],
localizationsDelegates: GlobalMaterialLocalizations.delegates,
home: Material(
child: Builder(
builder: (BuildContext context) {
return TextButton(
onPressed: () async {
await showDatePicker(
context: context,
initialDate: initialDate,
firstDate: firstDate,
lastDate: lastDate,
locale: const Locale('fr', 'CA'),
textDirection: TextDirection.rtl,
);
},
child: const Text('X'),
);
},
),
),
);
}
await tester.pumpWidget(buildFrame(false)); Element getPicker() => tester.element(find.byType(CalendarDatePicker));
await tester.pumpWidget(buildFrame());
await tester.tap(find.text('X')); await tester.tap(find.text('X'));
await tester.pumpAndSettle(); await tester.pumpAndSettle(const Duration(seconds: 1));
expect( expect(
Localizations.localeOf(getPicker()), Localizations.localeOf(getPicker()),
...@@ -284,7 +369,7 @@ void main() { ...@@ -284,7 +369,7 @@ void main() {
TextDirection.rtl, TextDirection.rtl,
); );
await tester.tap(find.text('ANNULER')); await tester.tap(find.text('Annuler'));
}); });
group("locale fonts don't overflow layout", () { group("locale fonts don't overflow layout", () {
......
...@@ -7,7 +7,78 @@ import 'package:flutter_localizations/flutter_localizations.dart'; ...@@ -7,7 +7,78 @@ import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
void main() { void main() {
testWidgets('Text baseline with CJK locale', (WidgetTester tester) async { testWidgets('Material2 - Text baseline with CJK locale', (WidgetTester tester) async {
// This test in combination with 'Text baseline with EN locale' verify the baselines
// used to align text with ideographic baselines are reasonable. We are currently
// using the alphabetic baseline to lay out as the ideographic baseline is not yet
// properly implemented. When the ideographic baseline is better defined and implemented,
// the values of this test should change very slightly. See the issue this is based off
// of: https://github.com/flutter/flutter/issues/25782.
final Key targetKey = UniqueKey();
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(useMaterial3: false),
routes: <String, WidgetBuilder>{
'/next': (BuildContext context) {
return const Text('Next');
},
},
localizationsDelegates: GlobalMaterialLocalizations.delegates,
supportedLocales: const <Locale>[
Locale('en', 'US'),
Locale('es', 'ES'),
Locale('zh', 'CN'),
],
locale: const Locale('zh', 'CN'),
home: Material(
child: Center(
child: Builder(
key: targetKey,
builder: (BuildContext context) {
return PopupMenuButton<int>(
onSelected: (int value) {
Navigator.pushNamed(context, '/next');
},
itemBuilder: (BuildContext context) {
return <PopupMenuItem<int>>[
const PopupMenuItem<int>(
value: 1,
child: Text(
'hello, world',
style: TextStyle(color: Colors.blue),
),
),
const PopupMenuItem<int>(
value: 2,
child: Text(
'你好,世界',
style: TextStyle(color: Colors.blue),
),
),
];
},
);
},
),
),
),
),
);
await tester.tap(find.byKey(targetKey));
await tester.pumpAndSettle();
expect(find.text('hello, world'), findsOneWidget);
expect(find.text('你好,世界'), findsOneWidget);
expect(tester.getTopLeft(find.text('hello, world')).dy, 299.5);
expect(tester.getBottomLeft(find.text('hello, world')).dy, 316.5);
expect(tester.getTopLeft(find.text('你好,世界')).dy, 347.5);
expect(tester.getBottomLeft(find.text('你好,世界')).dy, 364.5);
});
testWidgets('Material3 - Text baseline with CJK locale', (WidgetTester tester) async {
// This test in combination with 'Text baseline with EN locale' verify the baselines // This test in combination with 'Text baseline with EN locale' verify the baselines
// used to align text with ideographic baselines are reasonable. We are currently // used to align text with ideographic baselines are reasonable. We are currently
// using the alphabetic baseline to lay out as the ideographic baseline is not yet // using the alphabetic baseline to lay out as the ideographic baseline is not yet
...@@ -78,7 +149,78 @@ void main() { ...@@ -78,7 +149,78 @@ void main() {
expect(tester.getBottomLeft(find.text('你好,世界')).dy, 366.0); expect(tester.getBottomLeft(find.text('你好,世界')).dy, 366.0);
}); });
testWidgets('Text baseline with EN locale', (WidgetTester tester) async { testWidgets('Material2 - Text baseline with EN locale', (WidgetTester tester) async {
// This test in combination with 'Text baseline with CJK locale' verify the baselines
// used to align text with ideographic baselines are reasonable. We are currently
// using the alphabetic baseline to lay out as the ideographic baseline is not yet
// properly implemented. When the ideographic baseline is better defined and implemented,
// the values of this test should change very slightly. See the issue this is based off
// of: https://github.com/flutter/flutter/issues/25782.
final Key targetKey = UniqueKey();
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(useMaterial3: false),
routes: <String, WidgetBuilder>{
'/next': (BuildContext context) {
return const Text('Next');
},
},
localizationsDelegates: GlobalMaterialLocalizations.delegates,
supportedLocales: const <Locale>[
Locale('en', 'US'),
Locale('es', 'ES'),
Locale('zh', 'CN'),
],
locale: const Locale('en', 'US'),
home: Material(
child: Center(
child: Builder(
key: targetKey,
builder: (BuildContext context) {
return PopupMenuButton<int>(
onSelected: (int value) {
Navigator.pushNamed(context, '/next');
},
itemBuilder: (BuildContext context) {
return <PopupMenuItem<int>>[
const PopupMenuItem<int>(
value: 1,
child: Text(
'hello, world',
style: TextStyle(color: Colors.blue),
),
),
const PopupMenuItem<int>(
value: 2,
child: Text(
'你好,世界',
style: TextStyle(color: Colors.blue),
),
),
];
},
);
},
),
),
),
),
);
await tester.tap(find.byKey(targetKey));
await tester.pumpAndSettle();
expect(find.text('hello, world'), findsOneWidget);
expect(find.text('你好,世界'), findsOneWidget);
expect(tester.getTopLeft(find.text('hello, world')).dy, 300.0);
expect(tester.getBottomLeft(find.text('hello, world')).dy, 316.0);
expect(tester.getTopLeft(find.text('你好,世界')).dy, 348.0);
expect(tester.getBottomLeft(find.text('你好,世界')).dy, 364.0);
});
testWidgets('Material3 - Text baseline with EN locale', (WidgetTester tester) async {
// This test in combination with 'Text baseline with CJK locale' verify the baselines // This test in combination with 'Text baseline with CJK locale' verify the baselines
// used to align text with ideographic baselines are reasonable. We are currently // used to align text with ideographic baselines are reasonable. We are currently
// using the alphabetic baseline to lay out as the ideographic baseline is not yet // using the alphabetic baseline to lay out as the ideographic baseline is not yet
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment