v8/test/inspector/debugger/breakpoints.js
Benedikt Meurer d821a6a373 [inspector] Fix mapping between location and offset.
We weren't really translating between location (line and column number)
and source position (character offset) consistently, especially when it
came to inline <script>s. There were also inconsistencies between what
Debugger.getPossibleBreakpoints and Debugger.setBreakpointByUrl would
do.

With this CL, we are now consistently operating under the following
assumptions:

(1) For inline <scripts>s with a //@ sourceURL annotation, we assume
    that the line and column number that comes in via the protocol is
    in terms of the source text of the script.
(2) For inline <script>s without said annotation, we assume that the
    line and column numbers are in terms of the surrounding document.

This is finally aligned with how the DevTools front-end operates.

Fixed: chromium:1319828
Change-Id: I98c4ef04b34a97caf060ff4f32690b135edb6ee6
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3610622
Reviewed-by: Kim-Anh Tran <kimanh@chromium.org>
Auto-Submit: Benedikt Meurer <bmeurer@chromium.org>
Commit-Queue: Kim-Anh Tran <kimanh@chromium.org>
Cr-Commit-Position: refs/heads/main@{#80292}
2022-05-02 06:33:54 +00:00

118 lines
3.7 KiB
JavaScript

// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
let {session, contextGroup, Protocol} = InspectorTest.start('Checks breakpoints.');
session.setupScriptMap();
InspectorTest.runAsyncTestSuite([
async function testRemoveBreakpoint() {
InspectorTest.log('Debugger.removeBreakpoint when agent is disabled:');
InspectorTest.logMessage(await Protocol.Debugger.removeBreakpoint({
breakpointId: '1:test.js:0:0'
}));
Protocol.Debugger.enable();
InspectorTest.log('Remove breakpoint with invalid breakpoint id:')
InspectorTest.logMessage(await Protocol.Debugger.removeBreakpoint({
breakpointId: ''
}));
InspectorTest.logMessage(await Protocol.Debugger.removeBreakpoint({
breakpointId: ':::'
}));
await Protocol.Debugger.disable();
},
async function testSetBreakpointByUrl() {
await Protocol.Debugger.enable();
Protocol.Runtime.evaluate({expression: `
function foo1(arg) {
return arg;
}
//# sourceURL=testSetBreakpointByUrl.js`});
InspectorTest.log('Adding conditional (arg === 1) breakpoint');
let {result:{breakpointId}} = await Protocol.Debugger.setBreakpointByUrl({
lineNumber: 2,
url: 'testSetBreakpointByUrl.js',
columnNumber: 2,
condition: 'arg === 1'
});
await evaluate('foo1(0)');
await evaluate('foo1(1)', breakpointId);
InspectorTest.log('\nEvaluating another script with the same url')
Protocol.Runtime.evaluate({expression: `
function foo2(arg) {
return arg;
}
//# sourceURL=testSetBreakpointByUrl.js`});
await evaluate('foo2(0)');
await evaluate('foo2(1)', breakpointId);
InspectorTest.log('\nRemoving breakpoint');
await Protocol.Debugger.removeBreakpoint({breakpointId});
await evaluate('foo1(1)');
await evaluate('foo2(1)');
InspectorTest.log('\nAdding breakpoint back');
({result:{breakpointId}} = await Protocol.Debugger.setBreakpointByUrl({
lineNumber: 2,
url: 'testSetBreakpointByUrl.js',
columnNumber: 2,
condition: 'arg === 1'
}));
await evaluate('foo1(0)');
await evaluate('foo1(1)', breakpointId);
InspectorTest.log('\nDisabling debugger agent');
await Protocol.Debugger.disable();
await evaluate('foo1(1)');
await evaluate('foo2(1)');
InspectorTest.log('\nEnabling debugger agent');
await Protocol.Debugger.enable();
await evaluate('foo1(1)');
await evaluate('foo2(1)');
},
async function testSetBreakpointInScriptsWithDifferentOffsets() {
await Protocol.Debugger.enable();
InspectorTest.log('Adding breakpoint');
let {result:{breakpointId}} = await Protocol.Debugger.setBreakpointByUrl({
lineNumber: 2,
url: 'test2.js',
columnNumber: 2,
});
contextGroup.addScript(`
function foo1(arg) {
return arg;
}
//# sourceURL=test2.js`);
contextGroup.addScript(`
function foo2(arg) {
return arg;
}
//# sourceURL=test2.js`, 5);
await evaluate('foo1(0)', breakpointId);
await evaluate('foo2(0)', breakpointId);
}
]);
async function evaluate(expression, expectedBreakpoint) {
InspectorTest.log('evaluating ' + expression + ':');
let paused = Protocol.Debugger.oncePaused();
let evaluate = Protocol.Runtime.evaluate({expression});
let result = await Promise.race([paused, evaluate]);
if (result.method === 'Debugger.paused') {
if (result.params.hitBreakpoints) {
if (result.params.hitBreakpoints.find(b => b === expectedBreakpoint)) {
InspectorTest.log(' hit expected breakpoint')
} else {
InspectorTest.log(' hit unexpected breakpoint');
}
}
await Protocol.Debugger.resume();
} else {
InspectorTest.log(' not paused');
}
}