|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +common.skipIfInspectorDisabled(); |
| 4 | +const assert = require('assert'); |
| 5 | +const helper = require('./inspector-helper.js'); |
| 6 | +const path = require('path'); |
| 7 | + |
| 8 | +const script = path.join(path.dirname(module.filename), 'global-function.js'); |
| 9 | + |
| 10 | + |
| 11 | +function setupExpectBreakOnLine(line, url, session) { |
| 12 | + return function(message) { |
| 13 | + if ('Debugger.paused' === message['method']) { |
| 14 | + const callFrame = message['params']['callFrames'][0]; |
| 15 | + const location = callFrame['location']; |
| 16 | + assert.strictEqual(url, session.scriptUrlForId(location['scriptId'])); |
| 17 | + assert.strictEqual(line, location['lineNumber']); |
| 18 | + return true; |
| 19 | + } |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +function setupExpectConsoleOutputAndBreak(type, values) { |
| 24 | + if (!(values instanceof Array)) |
| 25 | + values = [ values ]; |
| 26 | + let consoleLog = false; |
| 27 | + function matchConsoleLog(message) { |
| 28 | + if ('Runtime.consoleAPICalled' === message['method']) { |
| 29 | + const params = message['params']; |
| 30 | + if (params['type'] === type) { |
| 31 | + let i = 0; |
| 32 | + for (const value of params['args']) { |
| 33 | + if (value['value'] !== values[i++]) |
| 34 | + return false; |
| 35 | + } |
| 36 | + return i === values.length; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return function(message) { |
| 42 | + if (consoleLog) |
| 43 | + return message['method'] === 'Debugger.paused'; |
| 44 | + consoleLog = matchConsoleLog(message); |
| 45 | + return false; |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +function setupExpectContextDestroyed(id) { |
| 50 | + return function(message) { |
| 51 | + if ('Runtime.executionContextDestroyed' === message['method']) |
| 52 | + return message['params']['executionContextId'] === id; |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +function setupDebugger(session) { |
| 57 | + console.log('[test]', 'Setting up a debugger'); |
| 58 | + const commands = [ |
| 59 | + { 'method': 'Runtime.enable' }, |
| 60 | + { 'method': 'Debugger.enable' }, |
| 61 | + { 'method': 'Debugger.setAsyncCallStackDepth', |
| 62 | + 'params': {'maxDepth': 0} }, |
| 63 | + { 'method': 'Runtime.runIfWaitingForDebugger' }, |
| 64 | + ]; |
| 65 | + |
| 66 | + session |
| 67 | + .sendInspectorCommands(commands) |
| 68 | + .expectMessages((message) => 'Runtime.consoleAPICalled' === message.method); |
| 69 | +} |
| 70 | + |
| 71 | +function breakOnLine(session) { |
| 72 | + console.log('[test]', 'Breaking in the code'); |
| 73 | + const commands = [ |
| 74 | + { 'method': 'Debugger.setBreakpointByUrl', |
| 75 | + 'params': { 'lineNumber': 9, |
| 76 | + 'url': script, |
| 77 | + 'columnNumber': 0, |
| 78 | + 'condition': '' |
| 79 | + } |
| 80 | + }, |
| 81 | + { 'method': 'Runtime.evaluate', |
| 82 | + 'params': { 'expression': 'sum()', |
| 83 | + 'objectGroup': 'console', |
| 84 | + 'includeCommandLineAPI': true, |
| 85 | + 'silent': false, |
| 86 | + 'contextId': 1, |
| 87 | + 'returnByValue': false, |
| 88 | + 'generatePreview': true, |
| 89 | + 'userGesture': true, |
| 90 | + 'awaitPromise': false |
| 91 | + } |
| 92 | + } |
| 93 | + ]; |
| 94 | + helper.markMessageNoResponse(commands[1]); |
| 95 | + session |
| 96 | + .sendInspectorCommands(commands) |
| 97 | + .expectMessages(setupExpectBreakOnLine(9, script, session)); |
| 98 | +} |
| 99 | + |
| 100 | +function stepOverConsoleStatement(session) { |
| 101 | + console.log('[test]', 'Step over console statement and test output'); |
| 102 | + session |
| 103 | + .sendInspectorCommands({ 'method': 'Debugger.stepOver' }) |
| 104 | + .expectMessages(setupExpectConsoleOutputAndBreak('log', [0, 3])); |
| 105 | +} |
| 106 | + |
| 107 | +function testWaitsForFrontendDisconnect(session, harness) { |
| 108 | + console.log('[test]', 'Verify node waits for the frontend to disconnect'); |
| 109 | + session.sendInspectorCommands({ 'method': 'Debugger.resume'}) |
| 110 | + .expectMessages(setupExpectContextDestroyed(1)) |
| 111 | + .expectStderrOutput('Waiting for the debugger to disconnect...') |
| 112 | + .disconnect(true); |
| 113 | +} |
| 114 | + |
| 115 | +function runTests(harness) { |
| 116 | + harness |
| 117 | + .runFrontendSession([ |
| 118 | + setupDebugger, |
| 119 | + breakOnLine, |
| 120 | + stepOverConsoleStatement, |
| 121 | + testWaitsForFrontendDisconnect |
| 122 | + ]).expectShutDown(0); |
| 123 | +} |
| 124 | + |
| 125 | +helper.startNodeForInspectorTest(runTests, |
| 126 | + ['--inspect'], |
| 127 | + undefined, |
| 128 | + script); |
0 commit comments