Commit 6ca2e5dc authored by Yegor's avatar Yegor Committed by GitHub

fix _debugCheckOwnerBuildTargetExists; sync localizations and tests (#12245)

* Revert "When parts of the program are changed in a hot reload, but not executed during the reassemble, warn that a restart may be needed. (#12304)"

This reverts commit 90028813.

* fix _debugCheckOwnerBuildTargetExists; sync localizations and tests

* address comments
parent 2b78675b
...@@ -3272,29 +3272,28 @@ abstract class Element extends DiagnosticableTree implements BuildContext { ...@@ -3272,29 +3272,28 @@ abstract class Element extends DiagnosticableTree implements BuildContext {
@mustCallSuper @mustCallSuper
void didChangeDependencies() { void didChangeDependencies() {
assert(_active); // otherwise markNeedsBuild is a no-op assert(_active); // otherwise markNeedsBuild is a no-op
_debugCheckOwnerBuildTargetExists('didChangeDependencies'); assert(_debugCheckOwnerBuildTargetExists('didChangeDependencies'));
markNeedsBuild(); markNeedsBuild();
} }
void _debugCheckOwnerBuildTargetExists(String methodName) { bool _debugCheckOwnerBuildTargetExists(String methodName) {
assert(() { assert(() {
if (owner._debugCurrentBuildTarget == null) { if (owner._debugCurrentBuildTarget == null) {
throw new FlutterError( throw new FlutterError(
'$methodName for ${widget.runtimeType} was called at an ' '$methodName for ${widget.runtimeType} was called at an '
'inappropriate time.\n' 'inappropriate time.\n'
'\n' 'It may only be called while the widgets are being built. A possible '
'It may only be called while the widgets are being built. A possible ' 'cause of this error is when $methodName is called during '
'cause of this error is when $methodName is called during ' 'one of:\n'
'one of:\n' ' * network I/O event\n'
'\n' ' * file I/O event\n'
' * network I/O event\n' ' * timer\n'
' * file I/O event\n' ' * microtask (caused by Future.then, async/await, scheduleMicrotask)'
' * timer\n'
' * microtask (caused by Future.then, async/await, scheduleMicrotask)'
); );
} }
return true; return true;
}); }());
return true;
} }
/// Returns a description of what caused this element to be created. /// Returns a description of what caused this element to be created.
...@@ -3942,7 +3941,7 @@ class InheritedElement extends ProxyElement { ...@@ -3942,7 +3941,7 @@ class InheritedElement extends ProxyElement {
/// by first obtaining their [InheritedElement] using /// by first obtaining their [InheritedElement] using
/// [BuildContext.ancestorInheritedElementForWidgetOfExactType]. /// [BuildContext.ancestorInheritedElementForWidgetOfExactType].
void dispatchDidChangeDependencies() { void dispatchDidChangeDependencies() {
_debugCheckOwnerBuildTargetExists('dispatchDidChangeDependencies'); assert(_debugCheckOwnerBuildTargetExists('dispatchDidChangeDependencies'));
for (Element dependent in _dependents) { for (Element dependent in _dependents) {
assert(() { assert(() {
// check that it really is our descendant // check that it really is our descendant
......
...@@ -206,23 +206,20 @@ class _LocalizationsScope extends InheritedWidget { ...@@ -206,23 +206,20 @@ class _LocalizationsScope extends InheritedWidget {
Key key, Key key,
@required this.locale, @required this.locale,
@required this.localizationsState, @required this.localizationsState,
@required this.loadGeneration, @required this.typeToResources,
Widget child, Widget child,
}) : super(key: key, child: child) { }) : super(key: key, child: child) {
assert(localizationsState != null); assert(localizationsState != null);
assert(typeToResources != null);
} }
final Locale locale; final Locale locale;
final _LocalizationsState localizationsState; final _LocalizationsState localizationsState;
final Map<Type, dynamic> typeToResources;
/// A monotonically increasing number that changes after localizations
/// delegates have finished loading new data. When this number changes, it
/// triggers inherited widget notifications.
final int loadGeneration;
@override @override
bool updateShouldNotify(_LocalizationsScope old) { bool updateShouldNotify(_LocalizationsScope old) {
return loadGeneration != old.loadGeneration; return typeToResources != old.typeToResources;
} }
} }
...@@ -445,11 +442,6 @@ class _LocalizationsState extends State<Localizations> { ...@@ -445,11 +442,6 @@ class _LocalizationsState extends State<Localizations> {
final GlobalKey _localizedResourcesScopeKey = new GlobalKey(); final GlobalKey _localizedResourcesScopeKey = new GlobalKey();
Map<Type, dynamic> _typeToResources = <Type, dynamic>{}; Map<Type, dynamic> _typeToResources = <Type, dynamic>{};
/// A monotonically increasing number that increases after localizations
/// delegates have finished loading new data, triggering inherited widget
/// notifications.
int _loadGeneration = 0;
Locale get locale => _locale; Locale get locale => _locale;
Locale _locale; Locale _locale;
...@@ -513,7 +505,6 @@ class _LocalizationsState extends State<Localizations> { ...@@ -513,7 +505,6 @@ class _LocalizationsState extends State<Localizations> {
setState(() { setState(() {
_typeToResources = value; _typeToResources = value;
_locale = locale; _locale = locale;
_loadGeneration += 1;
}); });
}); });
} }
...@@ -539,7 +530,7 @@ class _LocalizationsState extends State<Localizations> { ...@@ -539,7 +530,7 @@ class _LocalizationsState extends State<Localizations> {
key: _localizedResourcesScopeKey, key: _localizedResourcesScopeKey,
locale: _locale, locale: _locale,
localizationsState: this, localizationsState: this,
loadGeneration: _loadGeneration, typeToResources: _typeToResources,
child: new Directionality( child: new Directionality(
textDirection: _textDirection, textDirection: _textDirection,
child: widget.child, child: widget.child,
......
...@@ -159,6 +159,23 @@ Widget buildFrame({ ...@@ -159,6 +159,23 @@ Widget buildFrame({
); );
} }
class SyncLoadTest extends StatefulWidget {
const SyncLoadTest();
@override
SyncLoadTestState createState() => new SyncLoadTestState();
}
class SyncLoadTestState extends State<SyncLoadTest> {
@override
Widget build(BuildContext context) {
return new Text(
TestLocalizations.of(context).message,
textDirection: TextDirection.rtl,
);
}
}
void main() { void main() {
testWidgets('Localizations.localeFor in a WidgetsApp with system locale', (WidgetTester tester) async { testWidgets('Localizations.localeFor in a WidgetsApp with system locale', (WidgetTester tester) async {
BuildContext pageContext; BuildContext pageContext;
...@@ -205,27 +222,27 @@ void main() { ...@@ -205,27 +222,27 @@ void main() {
}); });
testWidgets('Synchronously loaded localizations in a WidgetsApp', (WidgetTester tester) async { testWidgets('Synchronously loaded localizations in a WidgetsApp', (WidgetTester tester) async {
BuildContext pageContext; final List<LocalizationsDelegate<dynamic>> delegates = <LocalizationsDelegate<dynamic>>[
await tester.pumpWidget( new SyncTestLocalizationsDelegate(),
buildFrame( const DefaultWidgetsLocalizationsDelegate(),
delegates: <LocalizationsDelegate<dynamic>>[ ];
new SyncTestLocalizationsDelegate()
], Future<Null> pumpTest(Locale locale) async {
buildContent: (BuildContext context) { await tester.pumpWidget(new Localizations(
pageContext = context; locale: locale,
return new Text(TestLocalizations.of(context).message); delegates: delegates,
} child: const SyncLoadTest(),
) ));
); }
expect(TestLocalizations.of(pageContext), isNotNull); await pumpTest(const Locale('en', 'US'));
expect(find.text('en_US'), findsOneWidget); expect(find.text('en_US'), findsOneWidget);
await tester.binding.setLocale('en', 'GB'); await pumpTest(const Locale('en', 'GB'));
await tester.pump(); await tester.pump();
expect(find.text('en_GB'), findsOneWidget); expect(find.text('en_GB'), findsOneWidget);
await tester.binding.setLocale('en', 'US'); await pumpTest(const Locale('en', 'US'));
await tester.pump(); await tester.pump();
expect(find.text('en_US'), findsOneWidget); expect(find.text('en_US'), findsOneWidget);
}); });
......
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