From 8c902ad479cc2b05b96e9a9a9663a188243451c1 Mon Sep 17 00:00:00 2001 From: Yegor Date: Wed, 22 Nov 2017 15:08:09 -0800 Subject: [PATCH] clear timeline events prior to starting a new action (#12984) * clear timeline events prior to starting a new action * trailing commas --- packages/flutter_driver/lib/src/driver.dart | 48 +++++++++--- .../test/flutter_driver_test.dart | 75 ++++++++++++++----- 2 files changed, 97 insertions(+), 26 deletions(-) diff --git a/packages/flutter_driver/lib/src/driver.dart b/packages/flutter_driver/lib/src/driver.dart index ffa8906dbd..f11b4f5563 100644 --- a/packages/flutter_driver/lib/src/driver.dart +++ b/packages/flutter_driver/lib/src/driver.dart @@ -127,6 +127,7 @@ class FlutterDriver { static const String _kFlutterExtensionMethod = 'ext.flutter.driver'; static const String _kSetVMTimelineFlagsMethod = '_setVMTimelineFlags'; static const String _kGetVMTimelineMethod = '_getVMTimeline'; + static const String _kClearVMTimelineMethod = '_clearVMTimeline'; static int _nextDriverId = 0; @@ -182,9 +183,9 @@ class FlutterDriver { } final FlutterDriver driver = new FlutterDriver.connectedTo( - client, connection.peer, isolate, - printCommunication: printCommunication, - logCommunicationToFile: logCommunicationToFile + client, connection.peer, isolate, + printCommunication: printCommunication, + logCommunicationToFile: logCommunicationToFile, ); // Attempts to resume the isolate, but does not crash if it fails because @@ -327,13 +328,13 @@ class FlutterDriver { throw new DriverError( 'Failed to fulfill ${command.runtimeType}: Flutter application not responding', error, - stackTrace + stackTrace, ); } catch (error, stackTrace) { throw new DriverError( 'Failed to fulfill ${command.runtimeType} due to remote error', error, - stackTrace + stackTrace, ); } if (response['isError']) @@ -503,7 +504,10 @@ class FlutterDriver { } /// Starts recording performance traces. - Future startTracing({ List streams: _defaultStreams, Duration timeout: _kShortTimeout }) async { + Future startTracing({ + List streams: _defaultStreams, + Duration timeout: _kShortTimeout, + }) async { assert(streams != null && streams.isNotEmpty); try { await _peer.sendRequest(_kSetVMTimelineFlagsMethod, { @@ -514,7 +518,7 @@ class FlutterDriver { throw new DriverError( 'Failed to start tracing due to remote error', error, - stackTrace + stackTrace, ); } } @@ -530,7 +534,7 @@ class FlutterDriver { throw new DriverError( 'Failed to stop tracing due to remote error', error, - stackTrace + stackTrace, ); } } @@ -545,12 +549,38 @@ class FlutterDriver { /// /// [streams] limits the recorded timeline event streams to only the ones /// listed. By default, all streams are recorded. - Future traceAction(Future action(), { List streams: _defaultStreams }) async { + /// + /// If [retainPriorEvents] is true, retains events recorded prior to calling + /// [action]. Otherwise, prior events are cleared before calling [action]. By + /// default, prior events are cleared. + Future traceAction( + Future action(), { + List streams: _defaultStreams, + bool retainPriorEvents: false, + }) async { + if (!retainPriorEvents) { + await clearTimeline(); + } await startTracing(streams: streams); await action(); return stopTracingAndDownloadTimeline(); } + /// Clears all timeline events recorded up until now. + Future clearTimeline({ Duration timeout: _kShortTimeout }) async { + try { + await _peer + .sendRequest(_kClearVMTimelineMethod, {}) + .timeout(timeout); + } catch(error, stackTrace) { + throw new DriverError( + 'Failed to clear event timeline due to remote error', + error, + stackTrace, + ); + } + } + /// [action] will be executed with the frame sync mechanism disabled. /// /// By default, Flutter Driver waits until there is no pending frame scheduled diff --git a/packages/flutter_driver/test/flutter_driver_test.dart b/packages/flutter_driver/test/flutter_driver_test.dart index 5317c13907..e9423dd316 100644 --- a/packages/flutter_driver/test/flutter_driver_test.dart +++ b/packages/flutter_driver/test/flutter_driver_test.dart @@ -230,25 +230,45 @@ void main() { }); }); + group('clearTimeline', () { + test('clears timeline', () async { + bool clearWasCalled = false; + when(mockPeer.sendRequest('_clearVMTimeline', argThat(equals({})))) + .thenAnswer((_) async { + clearWasCalled = true; + return null; + }); + await driver.clearTimeline(); + expect(clearWasCalled, isTrue); + }); + }); + group('traceAction', () { - test('traces action', () async { - bool actionCalled = false; - bool startTracingCalled = false; - bool stopTracingCalled = false; + List log; + + setUp(() async { + log = []; + + when(mockPeer.sendRequest('_clearVMTimeline', argThat(equals({})))) + .thenAnswer((_) async { + log.add('clear'); + return null; + }); when(mockPeer.sendRequest('_setVMTimelineFlags', argThat(equals({'recordedStreams': '[all]'})))) - .thenAnswer((_) async { - startTracingCalled = true; - return null; - }); + .thenAnswer((_) async { + log.add('startTracing'); + return null; + }); when(mockPeer.sendRequest('_setVMTimelineFlags', argThat(equals({'recordedStreams': '[]'})))) - .thenAnswer((_) async { - stopTracingCalled = true; - return null; - }); + .thenAnswer((_) async { + log.add('stopTracing'); + return null; + }); when(mockPeer.sendRequest('_getVMTimeline')).thenAnswer((_) async { + log.add('download'); return { 'traceEvents': [ { @@ -257,14 +277,34 @@ void main() { ], }; }); + }); + test('without clearing timeline', () async { final Timeline timeline = await driver.traceAction(() { - actionCalled = true; + log.add('action'); + }, retainPriorEvents: true); + + expect(log, const [ + 'startTracing', + 'action', + 'stopTracing', + 'download', + ]); + expect(timeline.events.single.name, 'test event'); + }); + + test('with clearing timeline', () async { + final Timeline timeline = await driver.traceAction(() { + log.add('action'); }); - expect(actionCalled, isTrue); - expect(startTracingCalled, isTrue); - expect(stopTracingCalled, isTrue); + expect(log, const [ + 'clear', + 'startTracing', + 'action', + 'stopTracing', + 'download', + ]); expect(timeline.events.single.name, 'test event'); }); }); @@ -304,7 +344,8 @@ void main() { TimelineStream.dart, TimelineStream.gc, TimelineStream.compiler - ]); + ], + retainPriorEvents: true); expect(actionCalled, isTrue); expect(startTracingCalled, isTrue);