Skip to content

Commit 92dda37

Browse files
authored
[flutter_test] Ensure LiveTestWidgetsFlutterBinding executes frame and pointer callbacks in the test zone (flutter#191595)
In LiveTestWidgetsFlutterBinding, engine frame callbacks (handleBeginFrame, handleDrawFrame) and pointer event handlers are invoked asynchronously from the platform dispatcher outside the test execution chain. Because the dispatcher callbacks capture the zone active during initial registration, frame execution and pointer dispatch in subsequent tests were executing in the first test's zone. This change tracks the active test zone in TestWidgetsFlutterBinding and wraps handleBeginFrame, handleDrawFrame, and handlePointerEvent in LiveTestWidgetsFlutterBinding to run within the active test zone when in a test. Fixes flutter#113885
1 parent bd40e71 commit 92dda37

2 files changed

Lines changed: 181 additions & 0 deletions

File tree

‎packages/flutter_test/lib/src/binding.dart‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
17271727
}
17281728

17291729
Zone? _parentZone;
1730+
Zone? _testZone;
17301731

17311732
VoidCallback _createTestCompletionHandler(String testDescription, Completer<void> completer) {
17321733
return () {
@@ -1948,6 +1949,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
19481949
);
19491950
_parentZone = Zone.current;
19501951
final Zone testZone = _parentZone!.fork(specification: errorHandlingZoneSpecification);
1952+
_testZone = testZone;
19511953
testZone
19521954
.runBinary<Future<void>, Future<void> Function(), VoidCallback>(
19531955
_runTestBody,
@@ -2161,6 +2163,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
21612163
FlutterError.demangleStackTrace = _oldStackTraceDemangler;
21622164
_pendingExceptionDetails = null;
21632165
_parentZone = null;
2166+
_testZone = null;
21642167
buildOwner!.focusManager.dispose();
21652168

21662169
if (TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.checkMockMessageHandler(
@@ -2812,6 +2815,14 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
28122815

28132816
@override
28142817
void handleBeginFrame(Duration? rawTimeStamp) {
2818+
if (_testZone != null) {
2819+
_testZone!.runUnary<void, Duration?>(_handleBeginFrame, rawTimeStamp);
2820+
} else {
2821+
_handleBeginFrame(rawTimeStamp);
2822+
}
2823+
}
2824+
2825+
void _handleBeginFrame(Duration? rawTimeStamp) {
28152826
if (_drawFrame != _HandleDrawFrame.reset) {
28162827
throw StateError('handleBeginFrame() called before previous handleDrawFrame()');
28172828
}
@@ -2830,6 +2841,14 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
28302841

28312842
@override
28322843
void handleDrawFrame() {
2844+
if (_testZone != null) {
2845+
_testZone!.run<void>(_handleDrawFrame);
2846+
} else {
2847+
_handleDrawFrame();
2848+
}
2849+
}
2850+
2851+
void _handleDrawFrame() {
28332852
if (_drawFrame == _HandleDrawFrame.reset) {
28342853
throw StateError('handleDrawFrame() called without paired handleBeginFrame()');
28352854
}
@@ -2947,6 +2966,14 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
29472966
/// forwarded to [deviceEventDispatcher].
29482967
@override
29492968
void handlePointerEvent(PointerEvent event) {
2969+
if (_testZone != null) {
2970+
_testZone!.runUnary<void, PointerEvent>(_handlePointerEvent, event);
2971+
} else {
2972+
_handlePointerEvent(event);
2973+
}
2974+
}
2975+
2976+
void _handlePointerEvent(PointerEvent event) {
29502977
switch (pointerEventSource) {
29512978
case TestBindingEventSource.test:
29522979
RenderView? target;

‎packages/flutter_test/test/live_binding_test.dart‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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';
6+
57
import 'package:flutter/gestures.dart';
68
import 'package:flutter/rendering.dart';
79
import 'package:flutter/widgets.dart';
@@ -10,6 +12,158 @@ import 'package:flutter_test/flutter_test.dart';
1012
// This file is for testings that require a `LiveTestWidgetsFlutterBinding`
1113
void main() {
1214
final binding = LiveTestWidgetsFlutterBinding();
15+
testWidgets('Widgets are built within the current test zone (test 1 of 2)', (
16+
WidgetTester tester,
17+
) async {
18+
// Regression test for https://github.com/flutter/flutter/issues/113885
19+
Zone? buildZone;
20+
final Zone bodyZone = Zone.current;
21+
await tester.pumpWidget(
22+
TestWidgetsApp(
23+
home: Builder(
24+
builder: (BuildContext context) {
25+
buildZone = Zone.current;
26+
return const Text('Test 1');
27+
},
28+
),
29+
),
30+
);
31+
expect(buildZone, equals(bodyZone));
32+
});
33+
34+
testWidgets('Widgets are built within the current test zone (test 2 of 2)', (
35+
WidgetTester tester,
36+
) async {
37+
// Regression test for https://github.com/flutter/flutter/issues/113885
38+
Zone? buildZone;
39+
final Zone bodyZone = Zone.current;
40+
await tester.pumpWidget(
41+
TestWidgetsApp(
42+
home: Builder(
43+
builder: (BuildContext context) {
44+
buildZone = Zone.current;
45+
return const Text('Test 2');
46+
},
47+
),
48+
),
49+
);
50+
expect(buildZone, equals(bodyZone));
51+
});
52+
53+
testWidgets('Frame callbacks and pointer events run in current test zone (test 1 of 2)', (
54+
WidgetTester tester,
55+
) async {
56+
// Regression test for https://github.com/flutter/flutter/issues/113885
57+
Zone? frameCallbackZone;
58+
Zone? postFrameCallbackZone;
59+
Zone? gestureZone;
60+
final Zone bodyZone = Zone.current;
61+
62+
tester.binding.scheduleFrameCallback((Duration timeStamp) {
63+
frameCallbackZone = Zone.current;
64+
});
65+
tester.binding.addPostFrameCallback((Duration timeStamp) {
66+
postFrameCallbackZone = Zone.current;
67+
});
68+
69+
await tester.pumpWidget(
70+
TestWidgetsApp(
71+
home: GestureDetector(
72+
onTap: () {
73+
gestureZone = Zone.current;
74+
},
75+
child: const Text('Tap target 1'),
76+
),
77+
),
78+
);
79+
80+
await tester.tap(find.text('Tap target 1'));
81+
await tester.pump();
82+
83+
expect(frameCallbackZone, equals(bodyZone));
84+
expect(postFrameCallbackZone, equals(bodyZone));
85+
expect(gestureZone, equals(bodyZone));
86+
});
87+
88+
testWidgets('Frame callbacks and pointer events run in current test zone (test 2 of 2)', (
89+
WidgetTester tester,
90+
) async {
91+
// Regression test for https://github.com/flutter/flutter/issues/113885
92+
Zone? frameCallbackZone;
93+
Zone? postFrameCallbackZone;
94+
Zone? gestureZone;
95+
final Zone bodyZone = Zone.current;
96+
97+
tester.binding.scheduleFrameCallback((Duration timeStamp) {
98+
frameCallbackZone = Zone.current;
99+
});
100+
tester.binding.addPostFrameCallback((Duration timeStamp) {
101+
postFrameCallbackZone = Zone.current;
102+
});
103+
104+
await tester.pumpWidget(
105+
TestWidgetsApp(
106+
home: GestureDetector(
107+
onTap: () {
108+
gestureZone = Zone.current;
109+
},
110+
child: const Text('Tap target 2'),
111+
),
112+
),
113+
);
114+
115+
await tester.tap(find.text('Tap target 2'));
116+
await tester.pump();
117+
118+
expect(frameCallbackZone, equals(bodyZone));
119+
expect(postFrameCallbackZone, equals(bodyZone));
120+
expect(gestureZone, equals(bodyZone));
121+
});
122+
123+
testWidgets(
124+
'Teardown callbacks and frame callbacks in teardown run in current test zone (test 1 of 2)',
125+
(WidgetTester tester) async {
126+
// Regression test for https://github.com/flutter/flutter/issues/113885
127+
final Zone bodyZone = Zone.current;
128+
addTearDown(() async {
129+
Zone? teardownBuildZone;
130+
await tester.pumpWidget(
131+
TestWidgetsApp(
132+
home: Builder(
133+
builder: (BuildContext context) {
134+
teardownBuildZone = Zone.current;
135+
return const Text('Teardown 1');
136+
},
137+
),
138+
),
139+
);
140+
expect(teardownBuildZone, equals(bodyZone));
141+
});
142+
},
143+
);
144+
145+
testWidgets(
146+
'Teardown callbacks and frame callbacks in teardown run in current test zone (test 2 of 2)',
147+
(WidgetTester tester) async {
148+
// Regression test for https://github.com/flutter/flutter/issues/113885
149+
final Zone bodyZone = Zone.current;
150+
addTearDown(() async {
151+
Zone? teardownBuildZone;
152+
await tester.pumpWidget(
153+
TestWidgetsApp(
154+
home: Builder(
155+
builder: (BuildContext context) {
156+
teardownBuildZone = Zone.current;
157+
return const Text('Teardown 2');
158+
},
159+
),
160+
),
161+
);
162+
expect(teardownBuildZone, equals(bodyZone));
163+
});
164+
},
165+
);
166+
13167
testWidgets('Input PointerAddedEvent', (WidgetTester tester) async {
14168
await tester.pumpWidget(const TestWidgetsApp(home: Text('Test')));
15169
await tester.pump();

0 commit comments

Comments
 (0)