Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-26 01:10:39 +00:00

435 lines
15 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2014 The Flutter Authors. All rights reserved.
// 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_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:intl/intl.dart' as intl;
void main() {
late DateTime firstDate;
late DateTime lastDate;
late DateTime initialDate;
setUp(() {
firstDate = DateTime(2001);
lastDate = DateTime(2031, DateTime.december, 31);
initialDate = DateTime(2016, DateTime.january, 15);
});
group(CalendarDatePicker, () {
final arabicNumbers = intl.NumberFormat('0', 'ar');
final testLocales = <Locale, Map<String, dynamic>>{
// Tests the default.
const Locale('en', 'US'): <String, dynamic>{
'textDirection': TextDirection.ltr,
'expectedDaysOfWeek': <String>['S', 'M', 'T', 'W', 'T', 'F', 'S'],
'expectedDaysOfMonth': List<String>.generate(30, (int i) => '${i + 1}'),
'expectedMonthYearHeader': 'September 2017',
},
// Tests a different first day of week.
const Locale('ru', 'RU'): <String, dynamic>{
'textDirection': TextDirection.ltr,
'expectedDaysOfWeek': <String>['В', 'П', 'В', 'С', 'Ч', 'П', 'С'],
'expectedDaysOfMonth': List<String>.generate(30, (int i) => '${i + 1}'),
'expectedMonthYearHeader': 'сентябрь 2017 г.',
},
const Locale('ro', 'RO'): <String, dynamic>{
'textDirection': TextDirection.ltr,
'expectedDaysOfWeek': <String>['D', 'L', 'M', 'M', 'J', 'V', 'S'],
'expectedDaysOfMonth': List<String>.generate(30, (int i) => '${i + 1}'),
'expectedMonthYearHeader': 'septembrie 2017',
},
// Tests RTL.
const Locale('ar', 'AR'): <String, dynamic>{
'textDirection': TextDirection.rtl,
'expectedDaysOfWeek': <String>['ح', 'ن', 'ث', 'ر', 'خ', 'ج', 'س'],
'expectedDaysOfMonth': List<String>.generate(30, (int i) => arabicNumbers.format(i + 1)),
'expectedMonthYearHeader': 'سبتمبر ٢٠١٧',
},
};
for (final Locale locale in testLocales.keys) {
testWidgets('shows dates for $locale', (WidgetTester tester) async {
final expectedDaysOfWeek = testLocales[locale]!['expectedDaysOfWeek'] as List<String>;
final expectedDaysOfMonth = testLocales[locale]!['expectedDaysOfMonth'] as List<String>;
final expectedMonthYearHeader = testLocales[locale]!['expectedMonthYearHeader'] as String;
final textDirection = testLocales[locale]!['textDirection'] as TextDirection;
final baseDate = DateTime(2017, 9, 27);
await _pumpBoilerplate(
tester,
CalendarDatePicker(
initialDate: baseDate,
firstDate: baseDate.subtract(const Duration(days: 90)),
lastDate: baseDate.add(const Duration(days: 90)),
onDateChanged: (DateTime newValue) {},
),
locale: locale,
textDirection: textDirection,
);
expect(find.text(expectedMonthYearHeader), findsOneWidget);
for (final dayOfWeek in expectedDaysOfWeek) {
expect(find.text(dayOfWeek), findsWidgets);
}
Offset? previousCellOffset;
for (final dayOfMonth in expectedDaysOfMonth) {
final Finder dayCell = find.descendant(
of: find.byType(GridView),
matching: find.text(dayOfMonth),
);
expect(dayCell, findsOneWidget);
// Check that cells are correctly positioned relative to each other,
// taking text direction into account.
final Offset offset = tester.getCenter(dayCell);
if (previousCellOffset != null) {
if (textDirection == TextDirection.ltr) {
expect(
offset.dx > previousCellOffset.dx && offset.dy == previousCellOffset.dy ||
offset.dy > previousCellOffset.dy,
true,
);
} else {
expect(
offset.dx < previousCellOffset.dx && offset.dy == previousCellOffset.dy ||
offset.dy > previousCellOffset.dy,
true,
);
}
}
previousCellOffset = offset;
}
});
}
});
testWidgets('Material2 - locale parameter overrides ambient locale', (WidgetTester tester) async {
Widget buildFrame() {
return MaterialApp(
theme: ThemeData(useMaterial3: false),
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'),
);
},
),
),
);
}
Element getPicker() => tester.element(find.byType(CalendarDatePicker));
await tester.pumpWidget(buildFrame());
await tester.tap(find.text('X'));
await tester.pumpAndSettle();
expect(Localizations.localeOf(getPicker()), const Locale('fr', 'CA'));
expect(Directionality.of(getPicker()), TextDirection.ltr);
await tester.tap(find.text('ANNULER'));
});
testWidgets('Material3 - locale parameter overrides ambient locale', (WidgetTester tester) async {
Widget buildFrame() {
return MaterialApp(
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'),
);
},
),
),
);
}
Element getPicker() => tester.element(find.byType(CalendarDatePicker));
await tester.pumpWidget(buildFrame());
await tester.tap(find.text('X'));
await tester.pumpAndSettle();
expect(Localizations.localeOf(getPicker()), const Locale('fr', 'CA'));
expect(Directionality.of(getPicker()), TextDirection.ltr);
await tester.tap(find.text('Annuler'));
});
testWidgets('Material2 - textDirection parameter overrides ambient textDirection', (
WidgetTester tester,
) async {
Widget buildFrame() {
return MaterialApp(
theme: ThemeData(useMaterial3: false),
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());
await tester.tap(find.text('X'));
await tester.pumpAndSettle();
expect(Directionality.of(getPicker()), TextDirection.rtl);
await tester.tap(find.text('CANCEL'));
});
testWidgets('Material3 - textDirection parameter overrides ambient textDirection', (
WidgetTester tester,
) async {
Widget buildFrame() {
return MaterialApp(
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());
await tester.tap(find.text('X'));
await tester.pumpAndSettle();
expect(Directionality.of(getPicker()), TextDirection.rtl);
await tester.tap(find.text('Cancel'));
});
testWidgets('Material2 - textDirection parameter takes precedence over locale parameter', (
WidgetTester tester,
) async {
Widget buildFrame() {
return MaterialApp(
theme: ThemeData(useMaterial3: false),
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'),
);
},
),
),
);
}
Element getPicker() => tester.element(find.byType(CalendarDatePicker));
await tester.pumpWidget(buildFrame());
await tester.tap(find.text('X'));
await tester.pumpAndSettle();
expect(Localizations.localeOf(getPicker()), const Locale('fr', 'CA'));
expect(Directionality.of(getPicker()), TextDirection.rtl);
await tester.tap(find.text('ANNULER'));
});
testWidgets('Material3 - textDirection parameter takes precedence over locale parameter', (
WidgetTester tester,
) async {
Widget buildFrame() {
return MaterialApp(
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'),
);
},
),
),
);
}
Element getPicker() => tester.element(find.byType(CalendarDatePicker));
await tester.pumpWidget(buildFrame());
await tester.tap(find.text('X'));
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(Localizations.localeOf(getPicker()), const Locale('fr', 'CA'));
expect(Directionality.of(getPicker()), TextDirection.rtl);
await tester.tap(find.text('Annuler'));
});
group("locale fonts don't overflow layout", () {
// Test screen layouts in various locales to ensure the fonts used
// don't overflow the layout
// Common screen size roughly based on a Pixel 1
const kCommonScreenSizePortrait = Size(1070, 1770);
const kCommonScreenSizeLandscape = Size(1770, 1070);
Future<void> showPicker(WidgetTester tester, Locale locale, Size size) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
await tester.pumpWidget(
MaterialApp(
home: Builder(
builder: (BuildContext context) {
return Localizations(
locale: locale,
delegates: GlobalMaterialLocalizations.delegates,
child: TextButton(
child: const Text('X'),
onPressed: () {
showDatePicker(
context: context,
initialDate: initialDate,
firstDate: firstDate,
lastDate: lastDate,
);
},
),
);
},
),
),
);
await tester.tap(find.text('X'));
await tester.pumpAndSettle();
}
// Regression test for https://github.com/flutter/flutter/issues/20171
testWidgets('common screen size - portrait - Chinese', (WidgetTester tester) async {
await showPicker(tester, const Locale('zh', 'CN'), kCommonScreenSizePortrait);
expect(tester.takeException(), isNull);
});
testWidgets('common screen size - landscape - Chinese', (WidgetTester tester) async {
await showPicker(tester, const Locale('zh', 'CN'), kCommonScreenSizeLandscape);
expect(tester.takeException(), isNull);
});
testWidgets('common screen size - portrait - Japanese', (WidgetTester tester) async {
await showPicker(tester, const Locale('ja', 'JA'), kCommonScreenSizePortrait);
expect(tester.takeException(), isNull);
});
testWidgets('common screen size - landscape - Japanese', (WidgetTester tester) async {
await showPicker(tester, const Locale('ja', 'JA'), kCommonScreenSizeLandscape);
expect(tester.takeException(), isNull);
});
});
}
Future<void> _pumpBoilerplate(
WidgetTester tester,
Widget child, {
Locale locale = const Locale('en', 'US'),
TextDirection textDirection = TextDirection.ltr,
}) async {
await tester.pumpWidget(
MaterialApp(
home: Directionality(
textDirection: TextDirection.ltr,
child: Localizations(
locale: locale,
delegates: GlobalMaterialLocalizations.delegates,
child: Material(child: child),
),
),
),
);
}