"use strict";
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const elements_helpers_js_1 = require("../helpers/elements-helpers.js");
const emulation_helpers_js_1 = require("../helpers/emulation-helpers.js");
const ADD_DEVICE_BUTTON_SELECTOR = '#custom-device-add-button';
const FOCUSED_DEVICE_NAME_FIELD_SELECTOR = '#custom-device-name-field:focus';
const EDITOR_ADD_BUTTON_SELECTOR = '.editor-buttons > devtools-button:nth-of-type(2)';
async function elementTextContent(element) {
    return await element.evaluate(node => node.textContent || '');
}
async function targetTextContent(selector, inspectedPage) {
    const handle = await inspectedPage.waitForSelector(selector);
    chai_1.assert.isOk(handle, `targetTextContent: could not find element for ${selector}`);
    return await elementTextContent(handle);
}
describe('Custom devices', () => {
    async function navigateAndOpeDeviceToolbar(devToolsPage, inspectedPage) {
        await inspectedPage.goToResource('emulation/custom-ua-ch.html');
        await devToolsPage.waitFor('.tabbed-pane-left-toolbar');
        await (0, emulation_helpers_js_1.openDeviceToolbar)(devToolsPage, inspectedPage);
    }
    it('can add and then edit a custom device with UA-CH emulation', async ({ devToolsPage, inspectedPage }) => {
        await navigateAndOpeDeviceToolbar(devToolsPage, inspectedPage);
        await (0, emulation_helpers_js_1.selectEdit)(devToolsPage);
        await devToolsPage.click(ADD_DEVICE_BUTTON_SELECTOR);
        await devToolsPage.waitFor(FOCUSED_DEVICE_NAME_FIELD_SELECTOR);
        await devToolsPage.typeText('Test device');
        await devToolsPage.tabForward(); // Focus width.
        await devToolsPage.tabForward(); // Focus height.
        await devToolsPage.tabForward(); // Focus DPR.
        await devToolsPage.typeText('1.0');
        await devToolsPage.tabForward(); // Focus UA string.
        await devToolsPage.typeText('Test device browser 1.0');
        await devToolsPage.tabForward(); // Focus device type.
        await devToolsPage.tabForward(); // Focus folder.
        await devToolsPage.pressKey('Enter');
        await devToolsPage.tabForward(); // Focus info icon
        await devToolsPage.tabForward(); // Focus help button
        await devToolsPage.tabForward(); // Focus brand browser.
        await devToolsPage.typeText('Test browser');
        await devToolsPage.tabForward(); // Focus brand version.
        await devToolsPage.typeText('1.0');
        await devToolsPage.tabForward(); // Focus delete button.
        await devToolsPage.tabForward(); // Focus Add brand button.
        await devToolsPage.pressKey('Space');
        await devToolsPage.typeText('Friendly Dragon');
        await devToolsPage.tabForward(); //  Focus second row brand version.
        await devToolsPage.typeText('1.1');
        await devToolsPage.tabForward(); // Focus second row delete button.
        await devToolsPage.tabForward(); // Focus Add brand button.
        await devToolsPage.tabForward(); // focus full-version-list brand
        await devToolsPage.typeText('Ready Rover');
        await devToolsPage.tabForward(); // focus full-version-list brand version
        await devToolsPage.typeText('2.4.9');
        await devToolsPage.tabForward(); // focus delete full-version-list brand button
        await devToolsPage.tabForward(); // focus add full-version-list brand button
        await devToolsPage.tabForward(); // Focus full version.
        await devToolsPage.typeText('1.1.2345');
        // Focus on form factors checkbox
        for (let i = 0; i < 7; ++i) {
            await devToolsPage.tabForward();
            // Enable form factors Desktop and XR
            if (i === 0 || i === 4) {
                await devToolsPage.pressKey('Space');
            }
        }
        await devToolsPage.tabForward(); // Focus platform.
        await devToolsPage.typeText('Cyborg');
        await devToolsPage.tabForward(); // Focus platform version.
        await devToolsPage.typeText('C-1');
        await devToolsPage.tabForward(); // Focus architecture.
        await devToolsPage.typeText('Bipedal');
        await devToolsPage.tabForward(); // Focus device model.
        await devToolsPage.typeText('C-1-Gardener');
        await devToolsPage.tabForward(); // Focus cancel button.
        await devToolsPage.tabForward(); // Focus add button.
        const addDevToolsButton = await devToolsPage.waitFor('.editor-buttons devtools-button:nth-of-type(2)');
        const addButton = await addDevToolsButton.waitForSelector('>>> button:focus');
        chai_1.assert.isNotNull(addButton); // Check that the devtools-buttons is focus
        const finishAddText = await elementTextContent(addDevToolsButton);
        chai_1.assert.strictEqual(finishAddText, 'Add');
        await devToolsPage.clickElement(addDevToolsButton);
        // Select the device in the menu.
        await (0, emulation_helpers_js_1.selectTestDevice)(devToolsPage);
        // Reload the test page, and verify things working.
        await inspectedPage.reload();
        void (0, elements_helpers_js_1.waitForDomNodeToBeVisible)('#res-dump-done', inspectedPage);
        chai_1.assert.strictEqual(await targetTextContent('#res-ua', inspectedPage), 'Test device browser 1.0');
        chai_1.assert.strictEqual(await targetTextContent('#res-mobile', inspectedPage), 'true');
        chai_1.assert.strictEqual(await targetTextContent('#res-num-brands', inspectedPage), '2');
        chai_1.assert.strictEqual(await targetTextContent('#res-brand-0-name', inspectedPage), 'Test browser');
        chai_1.assert.strictEqual(await targetTextContent('#res-brand-0-version', inspectedPage), '1.0');
        chai_1.assert.strictEqual(await targetTextContent('#res-brand-1-name', inspectedPage), 'Friendly Dragon');
        chai_1.assert.strictEqual(await targetTextContent('#res-brand-1-version', inspectedPage), '1.1');
        chai_1.assert.strictEqual(await targetTextContent('#res-platform', inspectedPage), 'Cyborg');
        chai_1.assert.strictEqual(await targetTextContent('#res-platform-version', inspectedPage), 'C-1');
        chai_1.assert.strictEqual(await targetTextContent('#res-architecture', inspectedPage), 'Bipedal');
        chai_1.assert.strictEqual(await targetTextContent('#res-model', inspectedPage), 'C-1-Gardener');
        chai_1.assert.strictEqual(await targetTextContent('#res-ua-full-version', inspectedPage), '1.1.2345');
        chai_1.assert.strictEqual(await targetTextContent('#res-num-full-version-list', inspectedPage), '1');
        chai_1.assert.strictEqual(await targetTextContent('#res-full-version-list-0-name', inspectedPage), 'Ready Rover');
        chai_1.assert.strictEqual(await targetTextContent('#res-full-version-list-0-version', inspectedPage), '2.4.9');
        chai_1.assert.strictEqual(await targetTextContent('#res-num-form-factors', inspectedPage), '2');
        chai_1.assert.strictEqual(await targetTextContent('#res-form-factors-0', inspectedPage), 'Desktop');
        chai_1.assert.strictEqual(await targetTextContent('#res-form-factors-1', inspectedPage), 'XR');
        // Focus the first item in the device list, which should be the custom entry,
        // and then click the edit button that should appear.
        const firstDevice = await devToolsPage.waitFor('.devices-list-item');
        await firstDevice.focus();
        const editButton = await devToolsPage.waitForAria('Edit');
        await editButton.click();
        // Make sure the device name field is what's focused.
        await devToolsPage.waitFor(FOCUSED_DEVICE_NAME_FIELD_SELECTOR);
        // Skip over to the version field.
        for (let i = 0; i < 20; ++i) {
            if (i === 7) {
                await devToolsPage.pressKey('ArrowRight');
            }
            await devToolsPage.tabForward();
        }
        // Change the value.
        await devToolsPage.typeText('1.1.5');
        // Move to form factor Desktop checkbox, uncheck it.
        await devToolsPage.tabForward();
        await devToolsPage.pressKey('Space');
        // Save the changes.
        await devToolsPage.pressKey('Enter');
        // Reload the test page, and verify things working.
        await inspectedPage.reload();
        void (0, elements_helpers_js_1.waitForDomNodeToBeVisible)('#res-dump-done', inspectedPage);
        chai_1.assert.strictEqual(await targetTextContent('#res-ua', inspectedPage), 'Test device browser 1.0');
        chai_1.assert.strictEqual(await targetTextContent('#res-mobile', inspectedPage), 'true');
        chai_1.assert.strictEqual(await targetTextContent('#res-num-brands', inspectedPage), '2');
        chai_1.assert.strictEqual(await targetTextContent('#res-brand-0-name', inspectedPage), 'Test browser');
        chai_1.assert.strictEqual(await targetTextContent('#res-brand-0-version', inspectedPage), '1.0');
        chai_1.assert.strictEqual(await targetTextContent('#res-brand-1-name', inspectedPage), 'Friendly Dragon');
        chai_1.assert.strictEqual(await targetTextContent('#res-brand-1-version', inspectedPage), '1.1');
        chai_1.assert.strictEqual(await targetTextContent('#res-platform', inspectedPage), 'Cyborg');
        chai_1.assert.strictEqual(await targetTextContent('#res-platform-version', inspectedPage), 'C-1');
        chai_1.assert.strictEqual(await targetTextContent('#res-architecture', inspectedPage), 'Bipedal');
        chai_1.assert.strictEqual(await targetTextContent('#res-model', inspectedPage), 'C-1-Gardener');
        chai_1.assert.strictEqual(await targetTextContent('#res-ua-full-version', inspectedPage), '1.1.5');
        chai_1.assert.strictEqual(await targetTextContent('#res-num-full-version-list', inspectedPage), '1');
        chai_1.assert.strictEqual(await targetTextContent('#res-full-version-list-0-name', inspectedPage), 'Ready Rover');
        chai_1.assert.strictEqual(await targetTextContent('#res-full-version-list-0-version', inspectedPage), '2.4.9');
        chai_1.assert.strictEqual(await targetTextContent('#res-num-form-factors', inspectedPage), '1');
        chai_1.assert.strictEqual(await targetTextContent('#res-form-factors-0', inspectedPage), 'XR');
    });
    it('can add and properly display a device with a custom resolution', async ({ devToolsPage, inspectedPage }) => {
        await navigateAndOpeDeviceToolbar(devToolsPage, inspectedPage);
        await (0, emulation_helpers_js_1.selectEdit)(devToolsPage);
        await devToolsPage.click(ADD_DEVICE_BUTTON_SELECTOR);
        await devToolsPage.waitFor(FOCUSED_DEVICE_NAME_FIELD_SELECTOR);
        await devToolsPage.typeText('Prime numbers');
        await devToolsPage.tabForward(); // Focus width.
        await devToolsPage.typeText('863');
        await devToolsPage.tabForward(); // Focus height.
        await devToolsPage.typeText('1223');
        await devToolsPage.tabForward(); // Focus DPR.
        await devToolsPage.typeText('1.0');
        await devToolsPage.tabForward(); // Focus UA string.
        await devToolsPage.typeText('Test device browser 1.0');
        const finishAdd = await devToolsPage.waitFor(EDITOR_ADD_BUTTON_SELECTOR);
        const finishAddText = await elementTextContent(finishAdd);
        chai_1.assert.strictEqual(finishAddText, 'Add');
        await devToolsPage.clickElement(finishAdd);
        await devToolsPage.waitForNone(FOCUSED_DEVICE_NAME_FIELD_SELECTOR);
        // Select the device in the menu.
        await (0, emulation_helpers_js_1.selectDevice)('Prime numbers', devToolsPage);
        // Check fit-to-window text is selected.
        await (0, emulation_helpers_js_1.waitForZoom)(devToolsPage, '51% (fit to window)');
        // Select 100%.
        await (0, emulation_helpers_js_1.selectZoomLevel)(devToolsPage, '100%');
        await (0, emulation_helpers_js_1.waitForZoom)(devToolsPage, '100%');
    });
    it('shows an error if the pixel ratio is not a number', async ({ devToolsPage, inspectedPage }) => {
        await navigateAndOpeDeviceToolbar(devToolsPage, inspectedPage);
        await (0, emulation_helpers_js_1.selectEdit)(devToolsPage);
        await devToolsPage.click(ADD_DEVICE_BUTTON_SELECTOR);
        await devToolsPage.waitFor(FOCUSED_DEVICE_NAME_FIELD_SELECTOR);
        await devToolsPage.typeText('Prime numbers');
        await devToolsPage.tabForward(); // Focus width.
        await devToolsPage.typeText('700');
        await devToolsPage.tabForward(); // Focus height.
        await devToolsPage.typeText('400');
        await devToolsPage.tabForward(); // Focus DPR.
        await devToolsPage.typeText('zzz.213213');
        await devToolsPage.tabForward(); // Focus UA string.
        await devToolsPage.typeText('Test device browser 1.0');
        const error = await devToolsPage.waitFor('.list-widget-input-validation-error');
        const errorText = await error.evaluate(element => element.textContent);
        chai_1.assert.strictEqual(errorText, 'Device pixel ratio must be a number or blank.');
    });
});
//# sourceMappingURL=custom-devices.test.js.map