summaryrefslogtreecommitdiff
path: root/test/inspector/test-inspector-break-when-eval.js
blob: 388edf6f5c21a70f6a5fc612296d1db36ee9cc88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
const assert = require('assert');
const helper = require('./inspector-helper.js');
const path = require('path');

const script = path.join(path.dirname(module.filename), 'global-function.js');


function setupExpectBreakOnLine(line, url, session) {
  return function(message) {
    if ('Debugger.paused' === message['method']) {
      const callFrame = message['params']['callFrames'][0];
      const location = callFrame['location'];
      assert.strictEqual(url, session.scriptUrlForId(location['scriptId']));
      assert.strictEqual(line, location['lineNumber']);
      return true;
    }
  };
}

function setupExpectConsoleOutputAndBreak(type, values) {
  if (!(values instanceof Array))
    values = [ values ];
  let consoleLog = false;
  function matchConsoleLog(message) {
    if ('Runtime.consoleAPICalled' === message['method']) {
      const params = message['params'];
      if (params['type'] === type) {
        let i = 0;
        for (const value of params['args']) {
          if (value['value'] !== values[i++])
            return false;
        }
        return i === values.length;
      }
    }
  }

  return function(message) {
    if (consoleLog)
      return message['method'] === 'Debugger.paused';
    consoleLog = matchConsoleLog(message);
    return false;
  };
}

function setupExpectContextDestroyed(id) {
  return function(message) {
    if ('Runtime.executionContextDestroyed' === message['method'])
      return message['params']['executionContextId'] === id;
  };
}

function setupDebugger(session) {
  console.log('[test]', 'Setting up a debugger');
  const commands = [
    { 'method': 'Runtime.enable' },
    { 'method': 'Debugger.enable' },
    { 'method': 'Debugger.setAsyncCallStackDepth',
      'params': {'maxDepth': 0} },
    { 'method': 'Runtime.runIfWaitingForDebugger' },
  ];

  session
    .sendInspectorCommands(commands)
    .expectMessages((message) => 'Runtime.consoleAPICalled' === message.method);
}

function breakOnLine(session) {
  console.log('[test]', 'Breaking in the code');
  const commands = [
    { 'method': 'Debugger.setBreakpointByUrl',
      'params': { 'lineNumber': 9,
                  'url': script,
                  'columnNumber': 0,
                  'condition': ''
      }
    },
    { 'method': 'Runtime.evaluate',
      'params': { 'expression': 'sum()',
                  'objectGroup': 'console',
                  'includeCommandLineAPI': true,
                  'silent': false,
                  'contextId': 1,
                  'returnByValue': false,
                  'generatePreview': true,
                  'userGesture': true,
                  'awaitPromise': false
      }
    }
  ];
  helper.markMessageNoResponse(commands[1]);
  session
    .sendInspectorCommands(commands)
    .expectMessages(setupExpectBreakOnLine(9, script, session));
}

function stepOverConsoleStatement(session) {
  console.log('[test]', 'Step over console statement and test output');
  session
    .sendInspectorCommands({ 'method': 'Debugger.stepOver' })
    .expectMessages(setupExpectConsoleOutputAndBreak('log', [0, 3]));
}

function testWaitsForFrontendDisconnect(session, harness) {
  console.log('[test]', 'Verify node waits for the frontend to disconnect');
  session.sendInspectorCommands({ 'method': 'Debugger.resume'})
    .expectMessages(setupExpectContextDestroyed(1))
    .expectStderrOutput('Waiting for the debugger to disconnect...')
    .disconnect(true);
}

function runTests(harness) {
  harness
    .runFrontendSession([
      setupDebugger,
      breakOnLine,
      stepOverConsoleStatement,
      testWaitsForFrontendDisconnect
    ]).expectShutDown(0);
}

helper.startNodeForInspectorTest(runTests,
                                 ['--inspect'],
                                 undefined,
                                 script);