blob: a36540d14b433fd7c74eeaddebfe19d484e3c0aa [file] [edit]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {assert} from 'chai';
import {afterEach, beforeEach, describe, it} from 'mocha';
import sinon from 'sinon';
import * as SDK from '../../core/sdk/sdk.js';
import * as Protocol from '../../generated/protocol.js';
import * as Bindings from '../../models/bindings/bindings.js';
import {deinitializeGlobalVars} from '../../testing/EnvironmentHelpers.js';
import {setupRuntimeHooks} from '../../testing/RuntimeHelpers.js';
import {TestUniverse} from '../../testing/TestUniverse.js';
import * as SourceMapScopes from './source_map_scopes.js';
describe('ScopeChainModel', () => {
setupRuntimeHooks();
let universe: TestUniverse;
let clock: sinon.SinonFakeTimers;
let stubPluginManager: sinon.SinonStubbedInstance<Bindings.DebuggerLanguagePlugins.DebuggerLanguagePluginManager>;
beforeEach(() => {
clock = sinon.useFakeTimers();
universe = new TestUniverse();
stubPluginManager = sinon.createStubInstance(
Bindings.DebuggerLanguagePlugins.DebuggerLanguagePluginManager, {resolveScopeChain: Promise.resolve(null)});
sinon.stub(universe.debuggerWorkspaceBinding, 'pluginManager').value(stubPluginManager);
});
afterEach(async () => {
await deinitializeGlobalVars();
});
it('emits an event after it was constructed with the scope chain', async () => {
const target = universe.createTarget();
const debuggerModel = target.model(SDK.DebuggerModel.DebuggerModel)!;
const fakeFrame = sinon.createStubInstance(SDK.DebuggerModel.CallFrame);
fakeFrame.debuggerModel = debuggerModel;
// @ts-expect-error readonly for test.
fakeFrame.script = sinon.createStubInstance(SDK.Script.Script, {isWasm: false});
fakeFrame.scopeChain.resolves([]);
const scopeChainModel = new SourceMapScopes.ScopeChainModel.ScopeChainModel(fakeFrame, universe.scopeChainResolver);
const listenerStub = sinon.stub();
scopeChainModel.addEventListener(SourceMapScopes.ScopeChainModel.Events.SCOPE_CHAIN_UPDATED, listenerStub);
await clock.tickAsync(10);
sinon.assert.calledOnce(listenerStub);
});
it('does not emit an event after it was disposed even with an update still in-flight', async () => {
// Stub out the pluginManagers `resolveScopeChain` with a promise that we control.
const {promise, resolve} = Promise.withResolvers<null>();
stubPluginManager.resolveScopeChain.returns(promise);
const target = universe.createTarget();
const debuggerModel = target.model(SDK.DebuggerModel.DebuggerModel)!;
const fakeFrame = sinon.createStubInstance(SDK.DebuggerModel.CallFrame);
fakeFrame.debuggerModel = debuggerModel;
// @ts-expect-error readonly for test.
fakeFrame.script = sinon.createStubInstance(SDK.Script.Script, {isWasm: false});
fakeFrame.scopeChain.resolves([]);
const scopeChainModel = new SourceMapScopes.ScopeChainModel.ScopeChainModel(fakeFrame, universe.scopeChainResolver);
const listenerStub = sinon.stub();
scopeChainModel.addEventListener(SourceMapScopes.ScopeChainModel.Events.SCOPE_CHAIN_UPDATED, listenerStub);
await clock.tickAsync(10);
sinon.assert.calledOnce(stubPluginManager.resolveScopeChain);
assert.isFalse(listenerStub.calledOnce);
scopeChainModel.dispose();
resolve(null);
await clock.tickAsync(10);
assert.isFalse(listenerStub.calledOnce);
});
it('emits an event when a source map is attached or detached for the call frame script', async () => {
const target = universe.createTarget();
const debuggerModel = target.model(SDK.DebuggerModel.DebuggerModel)!;
const fakeFrame = sinon.createStubInstance(SDK.DebuggerModel.CallFrame);
fakeFrame.debuggerModel = debuggerModel;
const script = sinon.createStubInstance(SDK.Script.Script, {
isWasm: false,
isContentScript: false,
target,
securityOrigin: SDK.SecurityOrigin.SecurityOrigin.create('http://example.com'),
});
// @ts-expect-error readonly for test.
fakeFrame.script = script;
fakeFrame.scopeChain.resolves([]);
const scopeChainModel = new SourceMapScopes.ScopeChainModel.ScopeChainModel(fakeFrame, universe.scopeChainResolver);
const listenerStub = sinon.stub();
scopeChainModel.addEventListener(SourceMapScopes.ScopeChainModel.Events.SCOPE_CHAIN_UPDATED, listenerStub);
await clock.tickAsync(10);
sinon.assert.calledOnce(listenerStub);
const sourceMap = sinon.createStubInstance(SDK.SourceMap.SourceMap, {sourceURLs: []});
debuggerModel.sourceMapManager().dispatchEventToListeners(SDK.SourceMapManager.Events.SourceMapAttached,
{client: script, sourceMap});
await clock.tickAsync(10);
sinon.assert.calledTwice(listenerStub);
debuggerModel.sourceMapManager().dispatchEventToListeners(SDK.SourceMapManager.Events.SourceMapDetached,
{client: script, sourceMap});
await clock.tickAsync(10);
sinon.assert.calledThrice(listenerStub);
scopeChainModel.dispose();
});
it('reuses the exact same ScopeChainEntry and RemoteObject instances across consumers and invalidates on DebugInfoAttached',
async () => {
const target = universe.createTarget();
const debuggerModel = target.model(SDK.DebuggerModel.DebuggerModel)!;
const fakeFrame = sinon.createStubInstance(SDK.DebuggerModel.CallFrame);
fakeFrame.debuggerModel = debuggerModel;
const script = sinon.createStubInstance(SDK.Script.Script, {isWasm: false});
// @ts-expect-error readonly for test.
fakeFrame.script = script;
const localScope = sinon.createStubInstance(SDK.DebuggerModel.Scope, {
callFrame: fakeFrame,
type: Protocol.Debugger.ScopeType.Local,
});
localScope.object.callsFake(() => new SDK.RemoteObject.LocalJSONObject({x: 42}));
fakeFrame.scopeChain.resolves([localScope]);
const model1 = new SourceMapScopes.ScopeChainModel.ScopeChainModel(fakeFrame, universe.scopeChainResolver);
const listenerStub = sinon.stub();
model1.addEventListener(SourceMapScopes.ScopeChainModel.Events.SCOPE_CHAIN_UPDATED, listenerStub);
await clock.tickAsync(10);
sinon.assert.calledOnce(listenerStub);
const chainFromEvent: SDK.DebuggerModel.ScopeChainEntry[] = listenerStub.firstCall.args[0].data.scopeChain;
const model2 = new SourceMapScopes.ScopeChainModel.ScopeChainModel(fakeFrame, universe.scopeChainResolver);
const chainFromSecondModel = await model2.resolveScopeChain();
sinon.assert.calledOnce(stubPluginManager.resolveScopeChain);
assert.lengthOf(chainFromEvent, 1);
assert.strictEqual(chainFromEvent, chainFromSecondModel);
assert.strictEqual(chainFromEvent[0], chainFromSecondModel[0]);
assert.strictEqual(chainFromEvent[0].object(), chainFromSecondModel[0].object());
sinon.assert.calledOnce(localScope.object);
debuggerModel.dispatchEventToListeners(SDK.DebuggerModel.Events.DebugInfoAttached, script);
await clock.tickAsync(10);
sinon.assert.calledTwice(listenerStub);
const refreshedChain: SDK.DebuggerModel.ScopeChainEntry[] = listenerStub.secondCall.args[0].data.scopeChain;
assert.notStrictEqual(refreshedChain, chainFromEvent);
assert.notStrictEqual(refreshedChain[0], chainFromEvent[0]);
model1.dispose();
model2.dispose();
});
it('does not emit an event when the scope chain for a different script is invalidated', async () => {
const target = universe.createTarget();
const debuggerModel = target.model(SDK.DebuggerModel.DebuggerModel)!;
const fakeFrame = sinon.createStubInstance(SDK.DebuggerModel.CallFrame);
fakeFrame.debuggerModel = debuggerModel;
// @ts-expect-error readonly for test.
fakeFrame.script = sinon.createStubInstance(SDK.Script.Script, {isWasm: false});
fakeFrame.scopeChain.resolves([]);
const scopeChainModel = new SourceMapScopes.ScopeChainModel.ScopeChainModel(fakeFrame, universe.scopeChainResolver);
const listenerStub = sinon.stub();
scopeChainModel.addEventListener(SourceMapScopes.ScopeChainModel.Events.SCOPE_CHAIN_UPDATED, listenerStub);
await clock.tickAsync(10);
sinon.assert.calledOnce(listenerStub);
const otherScript = sinon.createStubInstance(SDK.Script.Script);
debuggerModel.dispatchEventToListeners(SDK.DebuggerModel.Events.DebugInfoAttached, otherScript);
await clock.tickAsync(10);
sinon.assert.calledOnce(listenerStub);
scopeChainModel.dispose();
});
it('picks up a source map that was attached while no ScopeChainModel for the call frame existed', async () => {
const target = universe.createTarget();
const debuggerModel = target.model(SDK.DebuggerModel.DebuggerModel)!;
const fakeFrame = sinon.createStubInstance(SDK.DebuggerModel.CallFrame);
fakeFrame.debuggerModel = debuggerModel;
const script = sinon.createStubInstance(SDK.Script.Script, {
isWasm: false,
isContentScript: false,
target,
securityOrigin: SDK.SecurityOrigin.SecurityOrigin.create('http://example.com'),
});
// @ts-expect-error readonly for test.
fakeFrame.script = script;
fakeFrame.scopeChain.resolves([]);
// Simulate selecting the call frame, and then selecting a different one.
const model1 = new SourceMapScopes.ScopeChainModel.ScopeChainModel(fakeFrame, universe.scopeChainResolver);
const chainFromFirstModel = await model1.resolveScopeChain();
model1.dispose();
const sourceMap = sinon.createStubInstance(SDK.SourceMap.SourceMap, {sourceURLs: []});
debuggerModel.sourceMapManager().dispatchEventToListeners(SDK.SourceMapManager.Events.SourceMapAttached,
{client: script, sourceMap});
// Simulate selecting the original call frame again.
const model2 = new SourceMapScopes.ScopeChainModel.ScopeChainModel(fakeFrame, universe.scopeChainResolver);
const listenerStub = sinon.stub();
model2.addEventListener(SourceMapScopes.ScopeChainModel.Events.SCOPE_CHAIN_UPDATED, listenerStub);
await clock.tickAsync(10);
sinon.assert.calledOnce(listenerStub);
assert.notStrictEqual(listenerStub.firstCall.args[0].data.scopeChain, chainFromFirstModel);
sinon.assert.calledTwice(stubPluginManager.resolveScopeChain);
model2.dispose();
});
});