@@ -31,6 +31,33 @@ import 'examples/example_descriptor.dart';
3131import 'expect.dart' ;
3232
3333extension WidgetTesterExtension on WidgetTester {
34+ Future <RunFinishedAnalyticsEvent > waitForRunFinishedAnalyticsEvent ({
35+ Duration timeout = const Duration (seconds: 30 ),
36+ Duration step = const Duration (milliseconds: 100 ),
37+ Object ? previousEvent, // optional guard
38+ }) async {
39+ final deadline = DateTime .now ().add (timeout);
40+
41+ while (DateTime .now ().isBefore (deadline)) {
42+ // Drive frames/timers on Web-server device.
43+ await pump (step);
44+
45+ final e = PlaygroundComponents .analyticsService.lastEvent;
46+
47+ // Optional: ensure we don't accept an old event from a previous run.
48+ if (previousEvent != null && identical (e, previousEvent)) {
49+ continue ;
50+ }
51+
52+ if (e is RunFinishedAnalyticsEvent ) return e;
53+ }
54+
55+ throw TestFailure (
56+ 'Timed out waiting for RunFinishedAnalyticsEvent. '
57+ 'Last event: ${PlaygroundComponents .analyticsService .lastEvent }' ,
58+ );
59+ }
60+
3461 //workaround for https://github.com/flutter/flutter/issues/120060
3562 Future <void > enterCodeFieldText (String text) async {
3663 final codeField = widget (find.snippetCodeField ());
@@ -148,38 +175,25 @@ extension WidgetTesterExtension on WidgetTester {
148175 expectOutputIfDeployed (example, this );
149176 }
150177
151- Future <RunFinishedAnalyticsEvent > _waitForRunFinishedAnalyticsEvent ({
152- Duration timeout = const Duration (seconds: 30 ),
153- Duration pollInterval = const Duration (milliseconds: 100 ),
154- }) async {
155- final sw = Stopwatch ()..start ();
156- while (sw.elapsed < timeout) {
157- final e = PlaygroundComponents .analyticsService.lastEvent;
158- if (e is RunFinishedAnalyticsEvent ) return e;
159- await Future .delayed (pollInterval);
160- }
161- throw TestFailure (
162- 'Timed out waiting for RunFinishedAnalyticsEvent. '
163- 'Last event: ${PlaygroundComponents .analyticsService .lastEvent }' ,
164- );
165- }
166-
167178 Future <void > modifyRunExpectReal (ExampleDescriptor example) async {
168179 modifyCodeController ();
169180
170181 final playgroundController = findPlaygroundController ();
171182 final eventSnippetContext = playgroundController.eventSnippetContext;
172183 expect (eventSnippetContext.snippet, null );
173184
185+ final before = PlaygroundComponents .analyticsService.lastEvent;
174186 await tap (find.runOrCancelButton ());
175- await pumpAndSettle ();
187+ await pump (); // start the click handler
176188
177189 final actualText = findOutputText ();
178190 expect (actualText, isNot (startsWith (kCachedResultsLog)));
179191 expectOutputIfDeployed (example, this );
180192
181- // Wait until the run is fully finished and analytics event is emitted.
182- final finishedEvent = await _waitForRunFinishedAnalyticsEvent ();
193+ final finishedEvent = await waitForRunFinishedAnalyticsEvent (
194+ previousEvent: before,
195+ timeout: const Duration (seconds: 60 ), // optional bump for web
196+ );
183197
184198 expect (finishedEvent.snippetContext, eventSnippetContext);
185199 }
0 commit comments