v8/test/inspector/debugger/set-breakpoint-breaks-on-first-breakable-location.js
Kim-Anh Tran bad0135180 [js] Test if setBreakpoint sets breakpoint on first breakable location
Bug: chromium:1105172
Change-Id: I1e683e5040501464ef6c7427d1e1f7923cd23831
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2304587
Reviewed-by: Benedikt Meurer <bmeurer@chromium.org>
Commit-Queue: Kim-Anh Tran <kimanh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#68941}
2020-07-20 12:45:47 +00:00

112 lines
3.9 KiB
JavaScript

// Copyright 2020 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('Tests if breakpoint set is first breakable location');
const source = `
// Add a comment to test a break location that is
// outside of a function.
function foo() {
return Promise.resolve();
}
function boo() {
return Promise.resolve().then(() => 42);
}`;
contextGroup.addScript(source);
Protocol.Debugger.enable();
runTest()
.catch(reason => InspectorTest.log(`Failed: ${reason}.`))
.then(InspectorTest.completeTest);
async function runTest() {
const {params: script} = await Protocol.Debugger.onceScriptParsed();
await checkSetBreakpointForScript(script.scriptId);
}
async function checkSetBreakpointForScript(scriptId) {
// If we try to set a breakpoint that is outside of any function,
// setBreakpoint will find the first breakable location outside of any
// functions. If there is none, then it sets a breakpoint at the end of the
// script.
InspectorTest.log('Set breakpoint outside of any function: (0, 0).');
await checkSetBreakpointIsFirstBreakableLocation(scriptId, 0, 0, undefined);
// If we try to set a breakpoint that is inside of a function and
// the location is breakable, setBreakpoint is expected to add
// a breakpoint at that location.
InspectorTest.log('Set breakpoint at a breakable location: (4, 17).');
let breakable = true;
await checkSetBreakpointIsFirstBreakableLocation(scriptId, 4, 17, breakable);
// If we try to set a breakpoint that is inside of a function and
// the location is not breakable, setBreakpoint is expected to add
// a breakpoint at the next breakable location.
InspectorTest.log('Set breakpoint at non-breakable location: (7, 0).')
breakable = false;
await checkSetBreakpointIsFirstBreakableLocation(scriptId, 7, 0, breakable);
}
async function checkSetBreakpointIsFirstBreakableLocation(
scriptId, lineNumber, columnNumber, breakable) {
const possibleLocationsMsg = await Protocol.Debugger.getPossibleBreakpoints({
start: {
lineNumber: lineNumber,
columnNumber: columnNumber,
scriptId: scriptId
},
end: {
lineNumber: lineNumber + 1,
columnNumber: columnNumber,
scriptId: scriptId
}
});
const setLocationMsg =
await setBreakpoint(scriptId, lineNumber, columnNumber);
const setLocation = setLocationMsg.result.actualLocation;
if (possibleLocationsMsg.result.locations.length === 0) {
InspectorTest.log('No breakable location inside a function was found');
InspectorTest.log(`Set breakpoint adds a breakpoint at (${
setLocation.lineNumber}, ${setLocation.columnNumber}).`);
return;
}
const possibleLocations = possibleLocationsMsg.result.locations;
// Check that setting a breakpoint at a line actually
// sets the breakpoint at the first breakable location.
locationIsEqual(setLocation, possibleLocations[0]);
// Make sure that the selected locations for the test
// are breakable/non breakable as expected.
if (breakable ===
(setLocation.lineNumber === lineNumber &&
setLocation.columnNumber === columnNumber)) {
InspectorTest.log(
`Initial location is expected to be breakable: ${breakable}.`);
};
}
function locationIsEqual(locA, locB) {
if (locA.lineNumber === locB.lineNumber &&
locA.columnNumber === locB.columnNumber) {
InspectorTest.log(
`Location match for (${locA.lineNumber}, ${locA.columnNumber}).`);
}
}
async function setBreakpoint(id, lineNumber, columnNumber) {
InspectorTest.log(
`Setting breakpoint for id: ${id} at ${lineNumber}, ${columnNumber}.`);
const location = {
scriptId: id,
lineNumber: lineNumber,
columnNumber: columnNumber
};
const msg = await Protocol.Debugger.setBreakpoint({location: location});
return msg;
}