| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library service_test_common; | 5 library service_test_common; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'package:observatory/models.dart' as M; | 8 import 'package:observatory/models.dart' as M; |
| 9 import 'package:observatory/service_common.dart'; | 9 import 'package:observatory/service_common.dart'; |
| 10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 sb.write(" $f [${await f.location.getLine()}]\n"); | 273 sb.write(" $f [${await f.location.getLine()}]\n"); |
| 274 } | 274 } |
| 275 throw sb.toString(); | 275 throw sb.toString(); |
| 276 } else { | 276 } else { |
| 277 print('Program is stopped at line: $line'); | 277 print('Program is stopped at line: $line'); |
| 278 } | 278 } |
| 279 }; | 279 }; |
| 280 } | 280 } |
| 281 | 281 |
| 282 | 282 |
| 283 IsolateTest stoppedInFunction(String functionName, {bool contains: false}) { | 283 IsolateTest stoppedInFunction(String functionName, |
| 284 {bool contains: false, bool includeOwner: false}) { |
| 284 return (Isolate isolate) async { | 285 return (Isolate isolate) async { |
| 285 print("Checking we are in function: $functionName"); | 286 print("Checking we are in function: $functionName"); |
| 286 | 287 |
| 287 ServiceMap stack = await isolate.getStack(); | 288 ServiceMap stack = await isolate.getStack(); |
| 288 expect(stack.type, equals('Stack')); | 289 expect(stack.type, equals('Stack')); |
| 289 | 290 |
| 290 List<Frame> frames = stack['frames']; | 291 List<Frame> frames = stack['frames']; |
| 291 expect(frames.length, greaterThanOrEqualTo(1)); | 292 expect(frames.length, greaterThanOrEqualTo(1)); |
| 292 | 293 |
| 293 Frame topFrame = stack['frames'][0]; | 294 Frame topFrame = stack['frames'][0]; |
| 294 ServiceFunction function = await topFrame.function.load(); | 295 ServiceFunction function = await topFrame.function.load(); |
| 295 final bool matches = | 296 String foundFunctionName = function.name; |
| 296 contains ? function.name.contains(functionName) : | 297 if (includeOwner) { |
| 297 function.name == functionName; | 298 await (function.dartOwner as ServiceObject).load(); |
| 299 String ownerName = (function.dartOwner as ServiceObject).name; |
| 300 foundFunctionName = "$ownerName.$foundFunctionName"; |
| 301 } |
| 302 final bool matches = contains |
| 303 ? foundFunctionName.contains(functionName) |
| 304 : foundFunctionName == functionName; |
| 298 if (!matches) { | 305 if (!matches) { |
| 299 StringBuffer sb = new StringBuffer(); | 306 StringBuffer sb = new StringBuffer(); |
| 300 sb.write("Expected to be in function $functionName but " | 307 sb.write("Expected to be in function $functionName but " |
| 301 "actually in function ${function.name}"); | 308 "actually in function ${function.name}"); |
| 302 sb.write("\nFull stack trace:\n"); | 309 sb.write("\nFull stack trace:\n"); |
| 303 for (Frame f in stack['frames']) { | 310 for (Frame f in stack['frames']) { |
| 304 await f.function.load(); | 311 await f.function.load(); |
| 305 await (f.function.dartOwner as ServiceObject).load(); | 312 await (f.function.dartOwner as ServiceObject).load(); |
| 306 String name = f.function.name; | 313 String name = f.function.name; |
| 307 String ownerName = (f.function.dartOwner as ServiceObject).name; | 314 String ownerName = (f.function.dartOwner as ServiceObject).name; |
| 308 sb.write(" $f [$name] [$ownerName]\n"); | 315 sb.write(" $f [$name] [$ownerName]\n"); |
| 309 } | 316 } |
| 310 throw sb.toString(); | 317 throw sb.toString(); |
| 311 } else { | 318 } else { |
| 312 print('Program is stopped in function: $functionName'); | 319 print('Program is stopped in function: $functionName'); |
| 313 } | 320 } |
| 314 }; | 321 }; |
| 315 } | 322 } |
| 316 | 323 |
| 317 | |
| 318 Future<Isolate> resumeIsolate(Isolate isolate) { | 324 Future<Isolate> resumeIsolate(Isolate isolate) { |
| 319 Completer completer = new Completer(); | 325 Completer completer = new Completer(); |
| 320 isolate.vm.getEventStream(VM.kDebugStream).then((stream) { | 326 isolate.vm.getEventStream(VM.kDebugStream).then((stream) { |
| 321 var subscription; | 327 var subscription; |
| 322 subscription = stream.listen((ServiceEvent event) { | 328 subscription = stream.listen((ServiceEvent event) { |
| 323 if (event.kind == ServiceEvent.kResume) { | 329 if (event.kind == ServiceEvent.kResume) { |
| 324 subscription.cancel(); | 330 subscription.cancel(); |
| 325 completer.complete(); | 331 completer.complete(); |
| 326 } | 332 } |
| 327 }); | 333 }); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 | 396 |
| 391 Future<Instance> rootLibraryFieldValue(Isolate isolate, | 397 Future<Instance> rootLibraryFieldValue(Isolate isolate, |
| 392 String fieldName) async { | 398 String fieldName) async { |
| 393 Library rootLib = await isolate.rootLibrary.load(); | 399 Library rootLib = await isolate.rootLibrary.load(); |
| 394 Field field = rootLib.variables.singleWhere((v) => v.name == fieldName); | 400 Field field = rootLib.variables.singleWhere((v) => v.name == fieldName); |
| 395 await field.load(); | 401 await field.load(); |
| 396 Instance value = field.staticValue; | 402 Instance value = field.staticValue; |
| 397 await value.load(); | 403 await value.load(); |
| 398 return value; | 404 return value; |
| 399 } | 405 } |
| 406 |
| 407 IsolateTest runStepThroughProgramRecordingStops(List<String> recordStops) { |
| 408 return (Isolate isolate) async { |
| 409 Completer completer = new Completer(); |
| 410 |
| 411 await subscribeToStream(isolate.vm, VM.kDebugStream, |
| 412 (ServiceEvent event) async { |
| 413 if (event.kind == ServiceEvent.kPauseBreakpoint) { |
| 414 await isolate.reload(); |
| 415 // We are paused: Step further. |
| 416 Frame frame = isolate.topFrame; |
| 417 recordStops.add(await frame.location.toUserString()); |
| 418 if (event.atAsyncSuspension) { |
| 419 isolate.stepOverAsyncSuspension(); |
| 420 } else { |
| 421 isolate.stepOver(); |
| 422 } |
| 423 } else if (event.kind == ServiceEvent.kPauseExit) { |
| 424 // We are at the exit: The test is done. |
| 425 await cancelStreamSubscription(VM.kDebugStream); |
| 426 completer.complete(); |
| 427 } |
| 428 }); |
| 429 isolate.resume(); |
| 430 return completer.future; |
| 431 }; |
| 432 } |
| 433 |
| 434 IsolateTest checkRecordedStops( |
| 435 List<String> recordStops, List<String> expectedStops) { |
| 436 return (Isolate isolate) async { |
| 437 int end = recordStops.length < expectedStops.length |
| 438 ? recordStops.length |
| 439 : expectedStops.length; |
| 440 for (int i = 0; i < end; ++i) { |
| 441 expect(recordStops[i], expectedStops[i]); |
| 442 } |
| 443 |
| 444 expect(recordStops.length >= expectedStops.length, true, |
| 445 reason: "Expects at least ${expectedStops.length} breaks."); |
| 446 }; |
| 447 } |
| OLD | NEW |