blob: 7ce51229da1ee2f5c5d7b86877971cfd81fce4b3 [file] [log] [blame]
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview A set of utilities to help test the chrome://personalization
* SWA.
*/
import {emptyState, PersonalizationState} from 'chrome://personalization/trusted/personalization_state.js';
import {setWallpaperProviderForTesting} from 'chrome://personalization/trusted/wallpaper/wallpaper_interface_provider.js';
import {flush, PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {assertTrue} from 'chrome://webui-test/chai_assert.js';
import {flushTasks} from 'chrome://webui-test/test_util.js';
import {TestPersonalizationStore} from './test_personalization_store.js';
import {TestWallpaperProvider} from './test_wallpaper_interface_provider.js';
/**
* Constructs the given element with properties and appends it to body.
*/
export function initElement<T extends PolymerElement>(
cls: {new (): T; is: string}, properties = {}): T {
const element = document.createElement(cls.is) as T & HTMLElement;
for (const [key, value] of Object.entries(properties)) {
(element as any)[key] = value;
}
document.body.appendChild(element);
flush();
return element;
}
/**
* Tear down an element. Make sure the iframe load callback
* has completed to avoid weird race condition with loading.
* @see {b/185905694, crbug/466089}
*/
export async function teardownElement(element: HTMLElement|null) {
if (!element) {
return;
}
const iframe = await (element as any).iframePromise_;
if (iframe) {
iframe.remove();
await flushTasks();
}
element.remove();
await flushTasks();
}
/**
* Sets up the test wallpaper provider, test personalization store, and clears
* the page.
*/
export function baseSetup(initialState: PersonalizationState = emptyState()) {
const wallpaperProvider = new TestWallpaperProvider();
setWallpaperProviderForTesting(wallpaperProvider);
const personalizationStore = new TestPersonalizationStore(initialState);
personalizationStore.replaceSingleton();
document.body.innerHTML = '';
return {wallpaperProvider, personalizationStore};
}
function getDebugString(w: any) {
if (w === window) {
return w.location.href;
}
return 'iframe';
}
/**
* Helper function to test if two window objects are the same.
* Plain |assertEquals| fails when it attempts to get a debug string
* representation of cross-origin iframe window.
*/
export function assertWindowObjectsEqual(x: object|null, y: object|null) {
assertTrue(
x === y,
`Window objects are not identical: ${getDebugString(x)}, ${
getDebugString(y)}`);
}