|
| 1 | +// Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:test/test.dart'; |
| 6 | + |
| 7 | +import '../support/cli_test_driver.dart'; |
| 8 | +import 'integration.dart'; |
| 9 | + |
| 10 | +void debuggingTests() { |
| 11 | + CliAppFixture appFixture; |
| 12 | + BrowserTabInstance tabInstance; |
| 13 | + |
| 14 | + setUp(() async { |
| 15 | + tabInstance = await browserManager.createNewTab(); |
| 16 | + }); |
| 17 | + |
| 18 | + tearDown(() async { |
| 19 | + await tabInstance?.close(); |
| 20 | + await appFixture?.teardown(); |
| 21 | + }); |
| 22 | + |
| 23 | + test('lists scripts', () async { |
| 24 | + appFixture = await CliAppFixture.create('test/fixtures/debugging_app.dart'); |
| 25 | + |
| 26 | + final DevtoolsManager tools = |
| 27 | + new DevtoolsManager(tabInstance, webdevFixture.baseUri); |
| 28 | + await tools.start(appFixture); |
| 29 | + await tools.switchPage('debugger'); |
| 30 | + |
| 31 | + final String currentPageId = await tools.currentPageId(); |
| 32 | + expect(currentPageId, 'debugger'); |
| 33 | + |
| 34 | + final DebuggingManager debuggingManager = new DebuggingManager(tools); |
| 35 | + |
| 36 | + // Get the list of scripts. |
| 37 | + final List<String> scripts = await debuggingManager.getScripts(); |
| 38 | + expect(scripts, isNotEmpty); |
| 39 | + expect(scripts, anyElement(endsWith(appFixture.appScriptPath))); |
| 40 | + }); |
| 41 | + |
| 42 | + test('breakpoints, variables, resume', () async { |
| 43 | + appFixture = await CliAppFixture.create('test/fixtures/debugging_app.dart'); |
| 44 | + |
| 45 | + final String source = appFixture.scriptSource; |
| 46 | + final List<int> breakpointLines = |
| 47 | + CliAppFixture.parseBreakpointLines(source); |
| 48 | + |
| 49 | + final DevtoolsManager tools = |
| 50 | + new DevtoolsManager(tabInstance, webdevFixture.baseUri); |
| 51 | + await tools.start(appFixture); |
| 52 | + await tools.switchPage('debugger'); |
| 53 | + |
| 54 | + final String currentPageId = await tools.currentPageId(); |
| 55 | + expect(currentPageId, 'debugger'); |
| 56 | + |
| 57 | + final DebuggingManager debuggingManager = new DebuggingManager(tools); |
| 58 | + |
| 59 | + // clear and verify breakpoints |
| 60 | + List<String> breakpoints = await debuggingManager.getBreakpoints(); |
| 61 | + expect(breakpoints, isEmpty); |
| 62 | + |
| 63 | + // set and verify breakpoints |
| 64 | + for (int line in breakpointLines) { |
| 65 | + await debuggingManager.addBreakpoint(appFixture.appScriptPath, line); |
| 66 | + } |
| 67 | + |
| 68 | + breakpoints = await debuggingManager.getBreakpoints(); |
| 69 | + expect(breakpoints, isNotEmpty); |
| 70 | + |
| 71 | + // wait for paused state |
| 72 | + await waitFor(() async => await debuggingManager.getState() == 'paused'); |
| 73 | + |
| 74 | + await shortDelay(); |
| 75 | + |
| 76 | + // verify location |
| 77 | + expect( |
| 78 | + await debuggingManager.getLocation(), |
| 79 | + endsWith('${appFixture.appScriptPath}:${breakpointLines.first}'), |
| 80 | + ); |
| 81 | + |
| 82 | + // verify stack frame |
| 83 | + final List<String> frames = await debuggingManager.getCallStackFrames(); |
| 84 | + expect(frames.length, greaterThan(2)); |
| 85 | + expect(frames.sublist(0, 2), [ |
| 86 | + 'Cat.performAction:debugging_app.dart', |
| 87 | + 'main.run.<anonymous closure>:debugging_app.dart', |
| 88 | + ]); |
| 89 | + |
| 90 | + // verify variables |
| 91 | + expect( |
| 92 | + await debuggingManager.getVariables(), |
| 93 | + unorderedEquals(['this:Cat', 'actionStr:catAction!']), |
| 94 | + ); |
| 95 | + |
| 96 | + // resume |
| 97 | + await debuggingManager.clearBreakpoints(); |
| 98 | + await debuggingManager.resume(); |
| 99 | + |
| 100 | + // verify state resumed |
| 101 | + expect(await debuggingManager.getState(), 'running'); |
| 102 | + }); |
| 103 | + |
| 104 | + test('stepping, async step, async frames', () async { |
| 105 | + appFixture = |
| 106 | + await CliAppFixture.create('test/fixtures/debugging_app_async.dart'); |
| 107 | + |
| 108 | + final String source = appFixture.scriptSource; |
| 109 | + final int breakpointLine = |
| 110 | + CliAppFixture.parseBreakpointLines(source).single; |
| 111 | + final List<int> steppingLines = CliAppFixture.parseSteppingLines(source); |
| 112 | + |
| 113 | + final DevtoolsManager tools = |
| 114 | + new DevtoolsManager(tabInstance, webdevFixture.baseUri); |
| 115 | + await tools.start(appFixture); |
| 116 | + await tools.switchPage('debugger'); |
| 117 | + |
| 118 | + final String currentPageId = await tools.currentPageId(); |
| 119 | + expect(currentPageId, 'debugger'); |
| 120 | + |
| 121 | + final DebuggingManager debuggingManager = new DebuggingManager(tools); |
| 122 | + |
| 123 | + // clear and verify breakpoints |
| 124 | + List<String> breakpoints = await debuggingManager.getBreakpoints(); |
| 125 | + expect(breakpoints, isEmpty); |
| 126 | + |
| 127 | + // set and verify breakpoint |
| 128 | + await debuggingManager.addBreakpoint( |
| 129 | + appFixture.appScriptPath, breakpointLine); |
| 130 | + |
| 131 | + breakpoints = await debuggingManager.getBreakpoints(); |
| 132 | + expect(breakpoints, hasLength(1)); |
| 133 | + |
| 134 | + // wait for paused state |
| 135 | + await waitFor(() async => await debuggingManager.getState() == 'paused'); |
| 136 | + |
| 137 | + await shortDelay(); |
| 138 | + |
| 139 | + // verify location |
| 140 | + expect( |
| 141 | + await debuggingManager.getLocation(), |
| 142 | + endsWith('${appFixture.appScriptPath}:$breakpointLine'), |
| 143 | + ); |
| 144 | + |
| 145 | + // test stepping |
| 146 | + for (int stepLine in steppingLines) { |
| 147 | + // step |
| 148 | + await debuggingManager.step(); |
| 149 | + |
| 150 | + // wait for paused state |
| 151 | + await waitFor(() async => await debuggingManager.getState() == 'paused'); |
| 152 | + |
| 153 | + await shortDelay(); |
| 154 | + |
| 155 | + // verify location |
| 156 | + expect( |
| 157 | + await debuggingManager.getLocation(), |
| 158 | + endsWith('${appFixture.appScriptPath}:$stepLine'), |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + // verify an async stack frame |
| 163 | + final List<String> frames = await debuggingManager.getCallStackFrames(); |
| 164 | + expect(frames.length, greaterThan(4)); |
| 165 | + expect(frames.sublist(0, 4), [ |
| 166 | + 'performAction:debugging_app_async.dart', |
| 167 | + '<async break>', |
| 168 | + 'main.run.<anonymous closure>:debugging_app_async.dart', |
| 169 | + '<async break>', |
| 170 | + ]); |
| 171 | + |
| 172 | + // resume |
| 173 | + await debuggingManager.clearBreakpoints(); |
| 174 | + await debuggingManager.resume(); |
| 175 | + |
| 176 | + // verify state resumed |
| 177 | + expect(await debuggingManager.getState(), 'running'); |
| 178 | + }); |
| 179 | + |
| 180 | + test('break on exceptions', () async { |
| 181 | + appFixture = await CliAppFixture.create( |
| 182 | + 'test/fixtures/debugging_app_exception.dart'); |
| 183 | + |
| 184 | + final String source = appFixture.scriptSource; |
| 185 | + final int exceptionLine = CliAppFixture.parseExceptionLines(source).first; |
| 186 | + |
| 187 | + final DevtoolsManager tools = |
| 188 | + new DevtoolsManager(tabInstance, webdevFixture.baseUri); |
| 189 | + await tools.start(appFixture); |
| 190 | + await tools.switchPage('debugger'); |
| 191 | + |
| 192 | + final String currentPageId = await tools.currentPageId(); |
| 193 | + expect(currentPageId, 'debugger'); |
| 194 | + |
| 195 | + final DebuggingManager debuggingManager = new DebuggingManager(tools); |
| 196 | + |
| 197 | + // verify running state |
| 198 | + expect(await debuggingManager.getState(), 'running'); |
| 199 | + |
| 200 | + // set break on exceptions mode |
| 201 | + await debuggingManager.setExceptionPauseMode('All'); |
| 202 | + |
| 203 | + // wait for paused state |
| 204 | + await waitFor(() async => await debuggingManager.getState() == 'paused'); |
| 205 | + |
| 206 | + await shortDelay(); |
| 207 | + |
| 208 | + // verify location |
| 209 | + expect( |
| 210 | + await debuggingManager.getLocation(), |
| 211 | + endsWith('${appFixture.appScriptPath}:$exceptionLine'), |
| 212 | + ); |
| 213 | + |
| 214 | + // verify locals, including the exception object |
| 215 | + expect(await debuggingManager.getVariables(), [ |
| 216 | + '<exception>:StateError', |
| 217 | + 'foo:2', |
| 218 | + ]); |
| 219 | + |
| 220 | + // resume |
| 221 | + await debuggingManager.setExceptionPauseMode('Unhandled'); |
| 222 | + await debuggingManager.resume(); |
| 223 | + |
| 224 | + // verify state resumed |
| 225 | + expect(await debuggingManager.getState(), 'running'); |
| 226 | + }); |
| 227 | + |
| 228 | + test('console output', () async { |
| 229 | + appFixture = await CliAppFixture.create( |
| 230 | + 'test/fixtures/debugging_app_exception.dart'); |
| 231 | + |
| 232 | + final DevtoolsManager tools = |
| 233 | + new DevtoolsManager(tabInstance, webdevFixture.baseUri); |
| 234 | + await tools.start(appFixture); |
| 235 | + await tools.switchPage('debugger'); |
| 236 | + |
| 237 | + final String currentPageId = await tools.currentPageId(); |
| 238 | + expect(currentPageId, 'debugger'); |
| 239 | + |
| 240 | + final DebuggingManager debuggingManager = new DebuggingManager(tools); |
| 241 | + |
| 242 | + // verify running state |
| 243 | + expect(await debuggingManager.getState(), 'running'); |
| 244 | + |
| 245 | + // wait until there's console output |
| 246 | + await waitFor( |
| 247 | + () async => (await debuggingManager.getConsoleContents()).isNotEmpty); |
| 248 | + |
| 249 | + // verify the console contents |
| 250 | + expect( |
| 251 | + await debuggingManager.getConsoleContents(), |
| 252 | + contains('1\n'), |
| 253 | + ); |
| 254 | + }); |
| 255 | +} |
| 256 | + |
| 257 | +class DebuggingManager { |
| 258 | + DebuggingManager(this.tools); |
| 259 | + |
| 260 | + final DevtoolsManager tools; |
| 261 | + |
| 262 | + Future<void> resume() async { |
| 263 | + await tools.tabInstance.send('debugger.resume'); |
| 264 | + } |
| 265 | + |
| 266 | + Future<void> step() async { |
| 267 | + await tools.tabInstance.send('debugger.step'); |
| 268 | + } |
| 269 | + |
| 270 | + Future<String> getLocation() async { |
| 271 | + final AppResponse response = |
| 272 | + await tools.tabInstance.send('debugger.getLocation'); |
| 273 | + return response.result; |
| 274 | + } |
| 275 | + |
| 276 | + Future<List<String>> getVariables() async { |
| 277 | + final AppResponse response = |
| 278 | + await tools.tabInstance.send('debugger.getVariables'); |
| 279 | + final List<dynamic> result = response.result; |
| 280 | + return result.cast<String>(); |
| 281 | + } |
| 282 | + |
| 283 | + Future<String> getState() async { |
| 284 | + final AppResponse response = |
| 285 | + await tools.tabInstance.send('debugger.getState'); |
| 286 | + return response.result; |
| 287 | + } |
| 288 | + |
| 289 | + Future<String> getConsoleContents() async { |
| 290 | + final AppResponse response = |
| 291 | + await tools.tabInstance.send('debugger.getConsoleContents'); |
| 292 | + return response.result; |
| 293 | + } |
| 294 | + |
| 295 | + Future<void> clearBreakpoints() async { |
| 296 | + await tools.tabInstance.send('debugger.clearBreakpoints'); |
| 297 | + } |
| 298 | + |
| 299 | + Future<void> addBreakpoint(String path, int line) async { |
| 300 | + await tools.tabInstance.send('debugger.addBreakpoint', [path, line]); |
| 301 | + } |
| 302 | + |
| 303 | + Future<void> setExceptionPauseMode(String mode) async { |
| 304 | + await tools.tabInstance.send('debugger.setExceptionPauseMode', mode); |
| 305 | + } |
| 306 | + |
| 307 | + Future<List<String>> getBreakpoints() async { |
| 308 | + final AppResponse response = |
| 309 | + await tools.tabInstance.send('debugger.getBreakpoints'); |
| 310 | + final List<dynamic> result = response.result; |
| 311 | + return result.cast<String>(); |
| 312 | + } |
| 313 | + |
| 314 | + Future<List<String>> getScripts() async { |
| 315 | + final AppResponse response = |
| 316 | + await tools.tabInstance.send('debugger.getScripts'); |
| 317 | + final List<dynamic> result = response.result; |
| 318 | + return result.cast<String>(); |
| 319 | + } |
| 320 | + |
| 321 | + Future<List<String>> getCallStackFrames() async { |
| 322 | + final AppResponse response = |
| 323 | + await tools.tabInstance.send('debugger.getCallStackFrames'); |
| 324 | + final List<dynamic> result = response.result; |
| 325 | + return result.cast<String>(); |
| 326 | + } |
| 327 | +} |
0 commit comments