Skip to content

Commit 5bea4d9

Browse files
Revert "Assert that runApp is called in the same zone as binding.ensureInitialized (#117113)" (#122830)
Revert "Assert that runApp is called in the same zone as binding.ensureInitialized"
1 parent 62171df commit 5bea4d9

File tree

14 files changed

+11
-266
lines changed

14 files changed

+11
-266
lines changed

dev/tracing_tests/test/common.dart

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import 'dart:developer' as developer;
66
import 'dart:isolate' as isolate;
77

8-
import 'package:flutter/foundation.dart';
98
import 'package:flutter/scheduler.dart';
10-
import 'package:flutter/widgets.dart';
119
import 'package:flutter_test/flutter_test.dart';
1210
import 'package:vm_service/vm_service.dart';
1311
import 'package:vm_service/vm_service_io.dart';
@@ -54,29 +52,3 @@ Future<void> runFrame(VoidCallback callback) {
5452
callback();
5553
return result;
5654
}
57-
58-
// This binding skips the zones tests. These tests were written before we
59-
// verified zones properly, and they have been legacied-in to avoid having
60-
// to refactor them.
61-
//
62-
// When creating new tests, avoid relying on this class.
63-
class ZoneIgnoringTestBinding extends WidgetsFlutterBinding {
64-
@override
65-
void initInstances() {
66-
super.initInstances();
67-
_instance = this;
68-
}
69-
70-
@override
71-
bool debugCheckZone(String entryPoint) { return true; }
72-
73-
static ZoneIgnoringTestBinding get instance => BindingBase.checkInstance(_instance);
74-
static ZoneIgnoringTestBinding? _instance;
75-
76-
static ZoneIgnoringTestBinding ensureInitialized() {
77-
if (ZoneIgnoringTestBinding._instance == null) {
78-
ZoneIgnoringTestBinding();
79-
}
80-
return ZoneIgnoringTestBinding.instance;
81-
}
82-
}

dev/tracing_tests/test/image_painting_event_test.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import 'package:vm_service/vm_service.dart';
1313
import 'package:vm_service/vm_service_io.dart';
1414

1515
void main() {
16-
LiveTestWidgetsFlutterBinding.ensureInitialized();
17-
1816
late VmService vmService;
1917
late LiveTestWidgetsFlutterBinding binding;
2018
setUpAll(() async {
@@ -29,7 +27,7 @@ void main() {
2927
await vmService.streamListen(EventStreams.kExtension);
3028

3129
// Initialize bindings
32-
binding = LiveTestWidgetsFlutterBinding.instance;
30+
binding = LiveTestWidgetsFlutterBinding();
3331
binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.fullyLive;
3432
binding.attachRootWidget(const SizedBox.expand());
3533
expect(binding.framesEnabled, true);

dev/tracing_tests/test/inflate_widget_tracing_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final Set<String> interestingLabels = <String>{
1616
};
1717

1818
void main() {
19-
ZoneIgnoringTestBinding.ensureInitialized();
19+
WidgetsFlutterBinding.ensureInitialized();
2020
initTimelineTests();
2121
test('Children of MultiChildRenderObjectElement show up in tracing', () async {
2222
// We don't have expectations around the first frame because there's a race around

dev/tracing_tests/test/inflate_widget_update_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:flutter_test/flutter_test.dart';
99
import 'common.dart';
1010

1111
void main() {
12-
ZoneIgnoringTestBinding.ensureInitialized();
12+
WidgetsFlutterBinding.ensureInitialized();
1313
initTimelineTests();
1414
test('Widgets with updated keys produce well formed timelines', () async {
1515
await runFrame(() { runApp(const TestRoot()); });

dev/tracing_tests/test/timeline_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class TestRootState extends State<TestRoot> {
5858
}
5959

6060
void main() {
61-
ZoneIgnoringTestBinding.ensureInitialized();
61+
WidgetsFlutterBinding.ensureInitialized();
6262
initTimelineTests();
6363
test('Timeline', () async {
6464
// We don't have expectations around the first frame because there's a race around

packages/flutter/lib/src/foundation/binding.dart

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:async';
65
import 'dart:convert' show json;
76
import 'dart:developer' as developer;
87
import 'dart:io' show exit;
@@ -266,7 +265,6 @@ abstract class BindingBase {
266265
assert(_debugInitializedType == null);
267266
assert(() {
268267
_debugInitializedType = runtimeType;
269-
_debugBindingZone = Zone.current;
270268
return true;
271269
}());
272270
}
@@ -321,7 +319,7 @@ abstract class BindingBase {
321319
),
322320
ErrorHint(
323321
'It is also possible that $T does not implement "initInstances()" to assign a value to "instance". See the '
324-
'documentation of the BindingBase class for more details.',
322+
'documentation of the BaseBinding class for more details.',
325323
),
326324
ErrorHint(
327325
'The binding that was initialized was of the type "$_debugInitializedType". '
@@ -401,95 +399,6 @@ abstract class BindingBase {
401399
return _debugInitializedType;
402400
}
403401

404-
Zone? _debugBindingZone;
405-
406-
/// Whether [debugCheckZone] should throw (true) or just report the error (false).
407-
///
408-
/// Setting this to true makes it easier to catch cases where the zones are
409-
/// misconfigured, by allowing debuggers to stop at the point of error.
410-
///
411-
/// Currently this defaults to false, to avoid suddenly breaking applications
412-
/// that are affected by this check but appear to be working today. Applications
413-
/// are encouraged to resolve any issues that cause the [debugCheckZone] message
414-
/// to appear, as even if they appear to be working today, they are likely to be
415-
/// hiding hard-to-find bugs, and are more brittle (likely to collect bugs in
416-
/// the future).
417-
///
418-
/// To silence the message displayed by [debugCheckZone], ensure that the same
419-
/// zone is used when calling `ensureInitialized()` as when calling the framework
420-
/// in any other context (e.g. via [runApp]).
421-
static bool debugZoneErrorsAreFatal = false;
422-
423-
/// Checks that the current [Zone] is the same as that which was used
424-
/// to initialize the binding.
425-
///
426-
/// If the current zone ([Zone.current]) is not the zone that was active when
427-
/// the binding was initialized, then this method generates a [FlutterError]
428-
/// exception with detailed information. The exception is either thrown
429-
/// directly, or reported via [FlutterError.reportError], depending on the
430-
/// value of [BindingBase.debugZoneErrorsAreFatal].
431-
///
432-
/// To silence the message displayed by [debugCheckZone], ensure that the same
433-
/// zone is used when calling `ensureInitialized()` as when calling the
434-
/// framework in any other context (e.g. via [runApp]). For example, consider
435-
/// keeping a reference to the zone used to initialize the binding, and using
436-
/// [Zone.run] to use it again when calling into the framework.
437-
///
438-
/// ## Usage
439-
///
440-
/// The binding is considered initialized once [BindingBase.initInstances] has
441-
/// run; if this is called before then, it will throw an [AssertionError].
442-
///
443-
/// The `entryPoint` parameter is the name of the API that is checking the
444-
/// zones are consistent, for example, `'runApp'`.
445-
///
446-
/// This function always returns true (if it does not throw). It is expected
447-
/// to be invoked via the binding instance, e.g.:
448-
///
449-
/// ```dart
450-
/// void startup() {
451-
/// WidgetsBinding binding = WidgetsFlutterBinding.ensureInitialized();
452-
/// assert(binding.debugCheckZone('startup'));
453-
/// // ...
454-
/// }
455-
/// ```
456-
///
457-
/// If the binding expects to be used with multiple zones, it should override
458-
/// this method to return true always without throwing. (For example, the
459-
/// bindings used with [flutter_test] do this as they make heavy use of zones
460-
/// to drive the framework with an artificial clock and to catch errors and
461-
/// report them as test failures.)
462-
bool debugCheckZone(String entryPoint) {
463-
assert(() {
464-
assert(_debugBindingZone != null, 'debugCheckZone can only be used after the binding is fully initialized.');
465-
if (Zone.current != _debugBindingZone) {
466-
final Error message = FlutterError(
467-
'Zone mismatch.\n'
468-
'The Flutter bindings were initialized in a different zone than is now being used. '
469-
'This will likely cause confusion and bugs as any zone-specific configuration will '
470-
'inconsistently use the configuration of the original binding initialization zone '
471-
'or this zone based on hard-to-predict factors such as which zone was active when '
472-
'a particular callback was set.\n'
473-
'It is important to use the same zone when calling `ensureInitialized` on the binding '
474-
'as when calling `$entryPoint` later.\n'
475-
'To make this ${ debugZoneErrorsAreFatal ? 'error non-fatal' : 'warning fatal' }, '
476-
'set BindingBase.debugZoneErrorsAreFatal to ${!debugZoneErrorsAreFatal} before the '
477-
'bindings are initialized (i.e. as the first statement in `void main() { }`).',
478-
);
479-
if (debugZoneErrorsAreFatal) {
480-
throw message;
481-
}
482-
FlutterError.reportError(FlutterErrorDetails(
483-
exception: message,
484-
stack: StackTrace.current,
485-
context: ErrorDescription('during $entryPoint'),
486-
));
487-
}
488-
return true;
489-
}());
490-
return true;
491-
}
492-
493402
/// Called when the binding is initialized, to register service
494403
/// extensions.
495404
///

packages/flutter/lib/src/widgets/binding.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,6 @@ mixin WidgetsBinding on BindingBase, ServicesBinding, SchedulerBinding, GestureB
10311031
/// ensure the widget, element, and render trees are all built.
10321032
void runApp(Widget app) {
10331033
final WidgetsBinding binding = WidgetsFlutterBinding.ensureInitialized();
1034-
assert(binding.debugCheckZone('runApp'));
10351034
binding
10361035
..scheduleAttachRootWidget(binding.wrapWithDefaultView(app))
10371036
..scheduleWarmUpFrame();

packages/flutter/test/foundation/binding_2_test.dart

Lines changed: 0 additions & 63 deletions
This file was deleted.

packages/flutter/test/foundation/binding_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class FooLibraryBinding extends BindingBase with FooBinding {
2727
}
2828
}
2929

30+
3031
void main() {
3132
test('BindingBase.debugBindingType', () async {
3233
expect(BindingBase.debugBindingType(), isNull);

packages/flutter/test/painting/binding_test.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ class TestBindingBase implements BindingBase {
4747
@override
4848
void initInstances() {}
4949

50-
@override
51-
bool debugCheckZone(String entryPoint) { return true; }
52-
5350
@override
5451
void initServiceExtensions() {}
5552

packages/flutter/test/widgets/run_app_async_test.dart

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,12 @@
33
// found in the LICENSE file.
44

55
import 'package:fake_async/fake_async.dart';
6-
import 'package:flutter/foundation.dart';
76
import 'package:flutter/material.dart';
87
import 'package:flutter_test/flutter_test.dart';
98

10-
// This test is very fragile and bypasses some zone-related checks.
11-
// It is written this way to verify some invariants that would otherwise
12-
// be difficult to check.
13-
// Do not use this test as a guide for writing good Flutter code.
14-
15-
class TestBinding extends WidgetsFlutterBinding {
16-
@override
17-
void initInstances() {
18-
super.initInstances();
19-
_instance = this;
20-
}
21-
22-
@override
23-
bool debugCheckZone(String entryPoint) { return true; }
24-
25-
static TestBinding get instance => BindingBase.checkInstance(_instance);
26-
static TestBinding? _instance;
27-
28-
static TestBinding ensureInitialized() {
29-
if (TestBinding._instance == null) {
30-
TestBinding();
31-
}
32-
return TestBinding.instance;
33-
}
34-
}
35-
369
void main() {
3710
setUp(() {
38-
TestBinding.ensureInitialized();
11+
WidgetsFlutterBinding.ensureInitialized();
3912
WidgetsBinding.instance.resetEpoch();
4013
});
4114

packages/flutter_driver/lib/src/extension/extension.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ class _DriverBinding extends BindingBase with SchedulerBinding, ServicesBinding,
3939
final List<FinderExtension>? finders;
4040
final List<CommandExtension>? commands;
4141

42-
// Because you can't really control which zone a driver test uses,
43-
// we override the test for zones here.
44-
@override
45-
bool debugCheckZone(String entryPoint) { return true; }
46-
4742
@override
4843
void initServiceExtensions() {
4944
super.initServiceExtensions();

packages/flutter_test/lib/src/binding.dart

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,6 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
369369
// doesn't get generated for tests.
370370
}
371371

372-
@override
373-
bool debugCheckZone(String entryPoint) {
374-
// We skip all the zone checks in tests because the test framework makes heavy use
375-
// of zones and so the zones never quite match the way the framework expects.
376-
return true;
377-
}
378-
379372
/// Whether there is currently a test executing.
380373
bool get inTest;
381374

@@ -822,9 +815,9 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
822815
};
823816
FlutterError.demangleStackTrace = (StackTrace stack) {
824817
// package:stack_trace uses ZoneSpecification.errorCallback to add useful
825-
// information to stack traces, meaning Trace and Chain classes can be
826-
// present. Because these StackTrace implementations do not follow the
827-
// format the framework expects, we convert them to a vm trace here.
818+
// information to stack traces, in this case the Trace and Chain classes
819+
// can be present. Because these StackTrace implementations do not follow
820+
// the format the framework expects, we covert them to a vm trace here.
828821
if (stack is stack_trace.Trace) {
829822
return stack.vmTrace;
830823
}

0 commit comments

Comments
 (0)