|
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
5 | 5 | import 'dart:async'; |
| 6 | +import 'dart:developer' as developer; |
6 | 7 |
|
7 | 8 | import 'package:flutter/rendering.dart'; |
8 | 9 | import 'package:flutter_test/flutter_test.dart'; |
9 | 10 | import 'package:flutter/foundation.dart'; |
10 | 11 | import 'package:flutter/services.dart'; |
11 | 12 | import 'package:flutter/widgets.dart'; |
| 13 | +import 'package:vm_service/vm_service.dart' as vm; |
| 14 | +import 'package:vm_service/vm_service_io.dart' as vm_io; |
12 | 15 |
|
13 | 16 | import 'common.dart'; |
14 | 17 | import '_extension_io.dart' if (dart.library.html) '_extension_web.dart'; |
@@ -191,4 +194,92 @@ class IntegrationTestWidgetsFlutterBinding |
191 | 194 | ); |
192 | 195 | results[description] ??= _success; |
193 | 196 | } |
| 197 | + |
| 198 | + vm.VmService _vmService; |
| 199 | + |
| 200 | + /// Initialize the [vm.VmService] settings for the timeline. |
| 201 | + @visibleForTesting |
| 202 | + Future<void> enableTimeline({ |
| 203 | + List<String> streams = const <String>['all'], |
| 204 | + @visibleForTesting vm.VmService vmService, |
| 205 | + }) async { |
| 206 | + assert(streams != null); |
| 207 | + assert(streams.isNotEmpty); |
| 208 | + if (vmService != null) { |
| 209 | + _vmService = vmService; |
| 210 | + } |
| 211 | + if (_vmService == null) { |
| 212 | + final developer.ServiceProtocolInfo info = |
| 213 | + await developer.Service.getInfo(); |
| 214 | + assert(info.serverUri != null); |
| 215 | + _vmService = await vm_io.vmServiceConnectUri( |
| 216 | + 'ws://localhost:${info.serverUri.port}${info.serverUri.path}ws', |
| 217 | + ); |
| 218 | + } |
| 219 | + await _vmService.setVMTimelineFlags(streams); |
| 220 | + } |
| 221 | + |
| 222 | + /// Runs [action] and returns a [vm.Timeline] trace for it. |
| 223 | + /// |
| 224 | + /// Waits for the `Future` returned by [action] to complete prior to stopping |
| 225 | + /// the trace. |
| 226 | + /// |
| 227 | + /// The `streams` parameter limits the recorded timeline event streams to only |
| 228 | + /// the ones listed. By default, all streams are recorded. |
| 229 | + /// See `timeline_streams` in |
| 230 | + /// [Dart-SDK/runtime/vm/timeline.cc](https://github.com/dart-lang/sdk/blob/master/runtime/vm/timeline.cc) |
| 231 | + /// |
| 232 | + /// If [retainPriorEvents] is true, retains events recorded prior to calling |
| 233 | + /// [action]. Otherwise, prior events are cleared before calling [action]. By |
| 234 | + /// default, prior events are cleared. |
| 235 | + Future<vm.Timeline> traceTimeline( |
| 236 | + Future<dynamic> action(), { |
| 237 | + List<String> streams = const <String>['all'], |
| 238 | + bool retainPriorEvents = false, |
| 239 | + }) async { |
| 240 | + await enableTimeline(streams: streams); |
| 241 | + if (retainPriorEvents) { |
| 242 | + await action(); |
| 243 | + return await _vmService.getVMTimeline(); |
| 244 | + } |
| 245 | + |
| 246 | + await _vmService.clearVMTimeline(); |
| 247 | + final vm.Timestamp startTime = await _vmService.getVMTimelineMicros(); |
| 248 | + await action(); |
| 249 | + final vm.Timestamp endTime = await _vmService.getVMTimelineMicros(); |
| 250 | + return await _vmService.getVMTimeline( |
| 251 | + timeOriginMicros: startTime.timestamp, |
| 252 | + timeExtentMicros: endTime.timestamp, |
| 253 | + ); |
| 254 | + } |
| 255 | + |
| 256 | + /// This is a convenience wrap of [traceTimeline] and send the result back to |
| 257 | + /// the host for the [flutter_driver] style tests. |
| 258 | + /// |
| 259 | + /// This records the timeline during `action` and adds the result to |
| 260 | + /// [reportData] with `reportKey`. [reportData] contains the extra information |
| 261 | + /// of the test other than test success/fail. It will be passed back to the |
| 262 | + /// host and be processed by the [ResponseDataCallback] defined in |
| 263 | + /// [integrationDriver]. By default it will be written to |
| 264 | + /// `build/integration_response_data.json` with the key `timeline`. |
| 265 | + /// |
| 266 | + /// For tests with multiple calls of this method, `reportKey` needs to be a |
| 267 | + /// unique key, otherwise the later result will override earlier one. |
| 268 | + /// |
| 269 | + /// The `streams` and `retainPriorEvents` parameters are passed as-is to |
| 270 | + /// [traceTimeline]. |
| 271 | + Future<void> traceAction( |
| 272 | + Future<dynamic> action(), { |
| 273 | + List<String> streams = const <String>['all'], |
| 274 | + bool retainPriorEvents = false, |
| 275 | + String reportKey = 'timeline', |
| 276 | + }) async { |
| 277 | + vm.Timeline timeline = await traceTimeline( |
| 278 | + action, |
| 279 | + streams: streams, |
| 280 | + retainPriorEvents: retainPriorEvents, |
| 281 | + ); |
| 282 | + reportData ??= <String, dynamic>{}; |
| 283 | + reportData[reportKey] = timeline.toJson(); |
| 284 | + } |
194 | 285 | } |
0 commit comments