| // Copyright 2022 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| import * as SDK from '../../core/sdk/sdk.js'; |
| import {createTarget} from '../../testing/EnvironmentHelpers.js'; |
| import {describeWithMockConnection, setMockConnectionResponseHandler} from '../../testing/MockConnection.js'; |
| |
| import type * as LighthouseModule from './lighthouse.js'; |
| |
| describeWithMockConnection('LighthouseProtocolService', () => { |
| // eslint-disable-next-line @typescript-eslint/naming-convention |
| let Lighthouse: typeof LighthouseModule; |
| let primaryTarget: SDK.Target.Target; |
| let rootTarget: SDK.Target.Target; |
| let suspendAllTargets: sinon.SinonStub; |
| let resumeAllTargets: sinon.SinonStub; |
| |
| beforeEach(async () => { |
| Lighthouse = await import('./lighthouse.js'); |
| rootTarget = createTarget({type: SDK.Target.Type.TAB}); |
| createTarget({parentTarget: rootTarget, subtype: 'prerender'}); |
| primaryTarget = createTarget({parentTarget: rootTarget}); |
| |
| const targetManager = SDK.TargetManager.TargetManager.instance(); |
| |
| suspendAllTargets = sinon.stub(targetManager, 'suspendAllTargets').resolves(); |
| resumeAllTargets = sinon.stub(targetManager, 'resumeAllTargets').resolves(); |
| SDK.ChildTargetManager.ChildTargetManager.install(); |
| const childTargetManager = primaryTarget.model(SDK.ChildTargetManager.ChildTargetManager); |
| assert.exists(childTargetManager); |
| |
| sinon.stub(childTargetManager, 'getParentTargetId').resolves(primaryTarget.targetInfo()?.targetId); |
| if (rootTarget !== primaryTarget) { |
| const rootChildTargetManager = rootTarget.model(SDK.ChildTargetManager.ChildTargetManager); |
| assert.exists(rootChildTargetManager); |
| sinon.stub(rootChildTargetManager, 'getParentTargetId').resolves(rootTarget.targetInfo()?.targetId); |
| } |
| }); |
| |
| it('suspends all targets', async () => { |
| const service = new Lighthouse.LighthouseProtocolService.ProtocolService(); |
| await service.attach(); |
| sinon.assert.calledOnce(suspendAllTargets); |
| }); |
| |
| it('attaches to to the root target', async () => { |
| const attachedToTargetStub = sinon.stub(); |
| setMockConnectionResponseHandler('Target.attachToTarget', attachedToTargetStub); |
| const service = new Lighthouse.LighthouseProtocolService.ProtocolService(); |
| |
| await service.attach(); |
| |
| sinon.assert.calledOnceWithExactly( |
| attachedToTargetStub, {targetId: rootTarget.targetInfo()?.targetId, flatten: true}); |
| }); |
| |
| it('resumes all targets', async () => { |
| const service = new Lighthouse.LighthouseProtocolService.ProtocolService(); |
| await service.attach(); |
| await service.detach(); |
| sinon.assert.calledOnce(resumeAllTargets); |
| }); |
| }); |