| // Copyright 2020 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 {describe} from 'mocha'; | |
| import sinon from 'sinon'; | |
| import * as Common from '../core/common/common.js'; | |
| import * as Host from '../core/host/host.js'; | |
| import * as Platform from '../core/platform/platform.js'; | |
| import * as Root from '../core/root/root.js'; | |
| import * as SDK from '../core/sdk/sdk.js'; | |
| import * as Bindings from '../models/bindings/bindings.js'; | |
| import * as Breakpoints from '../models/breakpoints/breakpoints.js'; | |
| import * as EmulationModel from '../models/emulation/emulation.js'; | |
| import * as Formatter from '../models/formatter/formatter.js'; | |
| import * as IssuesManager from '../models/issues_manager/issues_manager.js'; | |
| import * as Logs from '../models/logs/logs.js'; | |
| import * as Persistence from '../models/persistence/persistence.js'; | |
| import * as Workspace from '../models/workspace/workspace.js'; | |
| import type * as UIModule from '../ui/legacy/legacy.js'; | |
| import {deinitializeGlobalLocaleVars, initializeGlobalLocaleVars} from './LocaleHelpers.js'; | |
| import {cleanupRuntime, setupRuntime} from './RuntimeHelpers.js'; | |
| import {cleanupSettings, setupSettings} from './SettingsHelpers.js'; | |
| import {beforeEachWithTeardown} from './TestTeardown.js'; | |
| // Don't import UI at this stage because it will fail without | |
| // the environment. Instead we do the import at the end of the | |
| // initialization phase. | |
| // eslint-disable-next-line @typescript-eslint/naming-convention | |
| let UI: typeof UIModule; | |
| export {createTarget, waitForTarget} from './TargetHelpers.js'; | |
| export {beforeEachWithTeardown, registerTestTeardown} from './TestTeardown.js'; | |
| export {expectConsoleLogs} from './TrackConsoleLogs.js'; | |
| export function registerActions(actions: UIModule.ActionRegistration.ActionRegistration[]): void { | |
| for (const action of actions) { | |
| UI.ActionRegistration.maybeRemoveActionExtension(action.actionId); | |
| UI.ActionRegistration.registerActionExtension(action); | |
| } | |
| const actionRegistryInstance = UI.ActionRegistry.ActionRegistry.instance({forceNew: true}); | |
| UI.ShortcutRegistry.ShortcutRegistry.instance({forceNew: true, actionRegistry: actionRegistryInstance}); | |
| } | |
| export function registerNoopActions(actionIds: string[]): void { | |
| for (const actionId of actionIds) { | |
| UI.ActionRegistration.maybeRemoveActionExtension(actionId); | |
| UI.ActionRegistration.registerActionExtension({ | |
| actionId, | |
| category: UI.ActionRegistration.ActionCategory.NONE, | |
| title: () => 'mock' as Platform.UIString.LocalizedString, | |
| }); | |
| } | |
| const actionRegistryInstance = UI.ActionRegistry.ActionRegistry.instance({forceNew: true}); | |
| UI.ShortcutRegistry.ShortcutRegistry.instance({forceNew: true, actionRegistry: actionRegistryInstance}); | |
| } | |
| export async function initializeGlobalVars({reset = true}: { | |
| reset?: boolean | undefined, | |
| } = {}): Promise<void> { | |
| await initializeGlobalLocaleVars(); | |
| setupSettings(reset); | |
| setupRuntime(); | |
| // Dynamically import UI after the rest of the environment is set up, otherwise it will fail. | |
| UI = await import('../ui/legacy/legacy.js'); | |
| UI.ZoomManager.ZoomManager.instance( | |
| {forceNew: true, win: window, frontendHost: Host.InspectorFrontendHost.InspectorFrontendHostInstance}); | |
| // Initialize context menus. | |
| UI.UIUtils.initializeUIUtils(document); | |
| } | |
| export async function deinitializeGlobalVars(): Promise<void> { | |
| // Remove the global SDK. | |
| // eslint-disable-next-line @typescript-eslint/naming-convention | |
| const globalObject = (globalThis as unknown as {SDK?: unknown, ls?: unknown}); | |
| delete globalObject.SDK; | |
| delete globalObject.ls; | |
| for (const target of SDK.TargetManager.TargetManager.instance().targets()) { | |
| if (typeof target.dispose === 'function') { | |
| target.dispose('deinitializeGlobalVars'); | |
| } | |
| } | |
| // Remove instances. | |
| deinitializeGlobalLocaleVars(); | |
| Host.GdpClient.GdpClient.removeInstance(); | |
| Host.AidaClient.HostConfigTracker.removeInstance(); | |
| Logs.NetworkLog.NetworkLog.removeInstance(); | |
| SDK.TargetManager.TargetManager.removeInstance(); | |
| SDK.CPUThrottlingManager.CPUThrottlingManager.removeInstance(); | |
| SDK.FrameManager.FrameManager.removeInstance(); | |
| SDK.EventBreakpointsModel.EventBreakpointsManager.removeInstance(); | |
| SDK.PageResourceLoader.PageResourceLoader.removeInstance(); | |
| Common.Settings.Settings.removeInstance(); | |
| Common.Revealer.RevealerRegistry.removeInstance(); | |
| Common.Console.Console.removeInstance(); | |
| Workspace.Workspace.WorkspaceImpl.removeInstance(); | |
| Workspace.FileManager.FileManager.removeInstance(); | |
| Workspace.IgnoreListManager.IgnoreListManager.removeInstance(); | |
| Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.removeInstance(); | |
| Bindings.CSSWorkspaceBinding.CSSWorkspaceBinding.removeInstance(); | |
| Bindings.NetworkProject.NetworkProjectManager.removeInstance(); | |
| Breakpoints.BreakpointManager.BreakpointManager.removeInstance(); | |
| IssuesManager.IssuesManager.IssuesManager.removeInstance(); | |
| Persistence.Persistence.PersistenceImpl.removeInstance(); | |
| Persistence.IsolatedFileSystemManager.IsolatedFileSystemManager.removeInstance(); | |
| Persistence.NetworkPersistenceManager.NetworkPersistenceManager.removeInstance(); | |
| Formatter.FormatterWorkerPool.FormatterWorkerPool.removeInstance(); | |
| EmulationModel.DeviceModeModel.DeviceModeModel.removeInstance(); | |
| cleanupSettings(); | |
| // Protect against the dynamic import not having happened. | |
| if (UI) { | |
| UI.ZoomManager.ZoomManager.removeInstance(); | |
| UI.ViewManager.ViewManager.removeInstance(); | |
| UI.ViewManager.resetViewRegistration(); | |
| UI.Context.Context.removeInstance(); | |
| UI.InspectorView.InspectorView.removeInstance(); | |
| UI.ActionRegistry.ActionRegistry.reset(); | |
| } | |
| // We must dynamically import the highlighting module because evaluating | |
| // `HighlightElement.ts` requires `HTMLElement` to be available. When running | |
| // unit tests in the Node.js environment, `HTMLElement` is missing, so a | |
| // static import at the top of this file would break Node unit tests. | |
| if (typeof HTMLElement !== 'undefined') { | |
| const Highlighting = await import('../ui/components/highlighting/highlighting.js'); | |
| Highlighting.HighlightManager.HighlightManager.removeInstance(); | |
| } | |
| cleanupRuntime(); | |
| } | |
| export function describeWithEnvironment(title: string, fn: (this: Mocha.Suite) => void, opts: {reset: boolean} = { | |
| reset: true, | |
| }): Mocha.Suite { | |
| return describe(title, function() { | |
| beforeEachWithTeardown(async () => await initializeGlobalVars(opts), deinitializeGlobalVars); | |
| fn.call(this); | |
| }); | |
| } | |
| export namespace describeWithEnvironment { | |
| export function only(title: string, fn: (this: Mocha.Suite) => void, opts: {reset: boolean} = { | |
| reset: true, | |
| }): Mocha.Suite { | |
| // eslint-disable-next-line mocha/no-exclusive-tests | |
| return describe.only(title, function() { | |
| beforeEachWithTeardown(async () => await initializeGlobalVars(opts), deinitializeGlobalVars); | |
| fn.call(this); | |
| }); | |
| } | |
| } | |
| export function createFakeSetting<T>(name: string, defaultValue: T): Common.Settings.Setting<T> { | |
| const storage = new Common.Settings.SettingsStorage({}, undefined, 'test'); | |
| return new Common.Settings.Setting(name, defaultValue, new Common.ObjectWrapper.ObjectWrapper(), storage, | |
| Common.Console.Console.instance()); | |
| } | |
| export function createFakeRegExpSetting(name: string, defaultValue: string): Common.Settings.RegExpSetting { | |
| const storage = new Common.Settings.SettingsStorage({}, undefined, 'test'); | |
| return new Common.Settings.RegExpSetting(name, defaultValue, new Common.ObjectWrapper.ObjectWrapper(), storage, | |
| Common.Console.Console.instance()); | |
| } | |
| export function setupActionRegistry(): void { | |
| beforeEachWithTeardown( | |
| () => { | |
| const actionRegistry = UI.ActionRegistry.ActionRegistry.instance(); | |
| UI.ShortcutRegistry.ShortcutRegistry.instance({ | |
| forceNew: true, | |
| actionRegistry, | |
| }); | |
| }, | |
| () => { | |
| if (UI) { | |
| UI.ShortcutRegistry.ShortcutRegistry.removeInstance(); | |
| UI.ActionRegistry.ActionRegistry.removeInstance(); | |
| } | |
| }); | |
| } | |
| let userAgentStub: sinon.SinonStub|undefined; | |
| export function setUserAgentForTesting(): void { | |
| if (Platform.HostRuntime.HOST_RUNTIME.getUserAgent() !== 'Chrome/unit_test') { | |
| userAgentStub = sinon.stub(Platform.HostRuntime.HOST_RUNTIME, 'getUserAgent').returns('Chrome/unit_test'); | |
| } | |
| } | |
| export function restoreUserAgentForTesting(): void { | |
| userAgentStub?.restore(); | |
| userAgentStub = undefined; | |
| } | |
| export function resetHostConfig(): void { | |
| for (const key of Object.keys(Root.Runtime.hostConfig)) { | |
| // @ts-expect-error TypeScript does not deduce the correct type | |
| delete Root.Runtime.hostConfig[key]; | |
| } | |
| } | |
| /** | |
| * Update `Root.Runtime.hostConfig` for testing. | |
| * `Root.Runtime.hostConfig` is automatically cleaned-up between unit | |
| * tests. | |
| */ | |
| export function updateHostConfig(config: Root.Runtime.HostConfig): void { | |
| Object.assign(Root.Runtime.hostConfig, config); | |
| } | |
| export async function waitFor<T extends Element = Element>(selector: string, | |
| root?: Element|ShadowRoot): Promise<T|null> { | |
| let element = null; | |
| let polls = 0; | |
| // Poll for element until found | |
| while (!element) { | |
| assert.isBelow(polls, 200, `Element with selector ${selector} was not found.`); | |
| element = root ? root.querySelector<T>(selector) : document.querySelector<T>(selector); | |
| await new Promise(resolve => setTimeout(resolve, 10)); | |
| polls++; | |
| } | |
| return element; | |
| } |