Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions desktop-app/e2e/tests/device-toolbar.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
import {ElectronApplication, Page} from '@playwright/test';
import {test, expect} from '../fixtures/electron-app';

const WEBVIEW_EXECUTION_FAILED = '__WEBVIEW_EXECUTION_FAILED__';

const execInWebview = async (
electronApp: ElectronApplication,
wcId: number,
script: string
): Promise<unknown> => {
return electronApp.evaluate(
async ({webContents}, {id, js}: {id: number; js: string}) => {
const wc = webContents.fromId(id);
if (!wc) {
return WEBVIEW_EXECUTION_FAILED;
}
try {
return await wc.executeJavaScript(js);
} catch (_error) {
return WEBVIEW_EXECUTION_FAILED;
}
},
{id: wcId, js: script}
);
};

const getNthWebviewId = async (page: Page, index: number): Promise<number | null> => {
return page.evaluate((nth) => {
const webview = document.querySelectorAll('webview')[nth] as Electron.WebviewTag | undefined;
return webview?.getWebContentsId?.() ?? null;
}, index);
};

test.describe('Device Toolbar', () => {
test('each device shows its name and dimensions in the header', async ({app}) => {
await app.dismissModals();
Expand Down Expand Up @@ -89,4 +120,129 @@ test.describe('Device Toolbar', () => {
await rulerBtn.click();
await app.page.waitForTimeout(300);
});

test('per-device javascript toggle remounts only the targeted preview and blocks screenshots', async ({
app,
testServerUrl,
}) => {
await app.dismissModals();
await app.navigateTo(`${testServerUrl}/test-page.html`, {timeout: 5000});

await expect(app.firstWebview).toHaveAttribute('src', /test-page\.html/, {
timeout: 15_000,
});

const initialFirstWebviewId = await getNthWebviewId(app.page, 0);
const initialSecondWebviewId = await getNthWebviewId(app.page, 1);

expect(initialFirstWebviewId).not.toBeNull();
expect(initialSecondWebviewId).not.toBeNull();

await expect
.poll(
() =>
execInWebview(
app.electronApp,
initialFirstWebviewId as number,
'typeof window.testClickCount'
),
{timeout: 10_000}
)
.toBe('number');
await expect
.poll(
() =>
execInWebview(
app.electronApp,
initialSecondWebviewId as number,
'typeof window.testClickCount'
),
{timeout: 10_000}
)
.toBe('number');

const firstToolbar = app.page
.locator('button[title="Refresh This View"]')
.first()
.locator('xpath=ancestor::div[contains(@class, "flex items-center justify-between gap-1")]');
const toolbarButtons = firstToolbar.locator(
'xpath=.//div[contains(@class, "my-1 inline-flex")]//button'
);
const quickScreenshotBtn = toolbarButtons.nth(1);
const fullPageScreenshotBtn = toolbarButtons.nth(2);
const disabledTitle =
'Screenshots are unavailable while JavaScript is disabled for this preview';

await expect(quickScreenshotBtn).toBeEnabled();
await expect(fullPageScreenshotBtn).toBeEnabled();

await firstToolbar.locator('button:has(span.sr-only)').first().click();
await app.page.locator('button:has-text("Disable JavaScript")').click();

await expect(quickScreenshotBtn).toBeDisabled();
await expect(fullPageScreenshotBtn).toBeDisabled();
await expect(quickScreenshotBtn).toHaveAttribute('title', disabledTitle);
await expect(fullPageScreenshotBtn).toHaveAttribute('title', disabledTitle);

await expect
.poll(() => getNthWebviewId(app.page, 0), {timeout: 10_000})
.not.toBe(initialFirstWebviewId);

const disabledFirstWebviewId = await getNthWebviewId(app.page, 0);
const disabledSecondWebviewId = await getNthWebviewId(app.page, 1);

expect(disabledFirstWebviewId).not.toBeNull();
expect(disabledSecondWebviewId).toBe(initialSecondWebviewId);

await expect
.poll(
() =>
execInWebview(
app.electronApp,
disabledFirstWebviewId as number,
'typeof window.testClickCount'
),
{timeout: 10_000}
)
.toBe(WEBVIEW_EXECUTION_FAILED);
await expect
.poll(
() =>
execInWebview(
app.electronApp,
disabledSecondWebviewId as number,
'typeof window.testClickCount'
),
{timeout: 10_000}
)
.toBe('number');

await firstToolbar.locator('button:has(span.sr-only)').first().click();
await app.page.locator('button:has-text("Enable JavaScript")').click();

await expect(quickScreenshotBtn).toBeEnabled();
await expect(fullPageScreenshotBtn).toBeEnabled();

await expect
.poll(() => getNthWebviewId(app.page, 0), {timeout: 10_000})
.not.toBe(disabledFirstWebviewId);

const reenabledFirstWebviewId = await getNthWebviewId(app.page, 0);
const reenabledSecondWebviewId = await getNthWebviewId(app.page, 1);

expect(reenabledFirstWebviewId).not.toBeNull();
expect(reenabledSecondWebviewId).toBe(initialSecondWebviewId);

await expect
.poll(
() =>
execInWebview(
app.electronApp,
reenabledFirstWebviewId as number,
'typeof window.testClickCount'
),
{timeout: 10_000}
)
.toBe('number');
});
});
114 changes: 114 additions & 0 deletions desktop-app/e2e/tests/menu-flyout.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
import {ElectronApplication} from '@playwright/test';
import {test, expect} from '../fixtures/electron-app';

const WEBVIEW_EXECUTION_FAILED = '__WEBVIEW_EXECUTION_FAILED__';

const getWebviewIds = async (electronApp: ElectronApplication): Promise<number[]> => {
return electronApp.evaluate(({webContents}) => {
return webContents
.getAllWebContents()
.filter((wc: Electron.WebContents & {getType?: () => string}) => wc.getType?.() === 'webview')
.map((wc: Electron.WebContents) => wc.id);
});
};

const execInWebview = async (
electronApp: ElectronApplication,
wcId: number,
script: string
): Promise<unknown> => {
return electronApp.evaluate(
async ({webContents}, {id, js}: {id: number; js: string}) => {
const wc = webContents.fromId(id);
if (!wc) {
return WEBVIEW_EXECUTION_FAILED;
}
try {
return await wc.executeJavaScript(js);
} catch (_error) {
return WEBVIEW_EXECUTION_FAILED;
}
},
{id: wcId, js: script}
);
};

test.describe('Menu Flyout', () => {
test('clicking the overflow menu button opens the flyout', async ({app}) => {
await app.dismissModals();
Expand Down Expand Up @@ -123,4 +156,85 @@ test.describe('Menu Flyout', () => {

await app.closeMenuFlyout();
});

test('javascript all previews toggle disables every preview and blocks screenshot all', async ({
app,
testServerUrl,
}) => {
await app.dismissModals();
await app.navigateTo(`${testServerUrl}/test-page.html`, {timeout: 5000});

await expect(app.firstWebview).toHaveAttribute('src', /test-page\.html/, {
timeout: 15_000,
});

const initialIds = await getWebviewIds(app.electronApp);
expect(initialIds.length).toBeGreaterThanOrEqual(1);

for (const id of initialIds) {
await expect
.poll(() => execInWebview(app.electronApp, id, 'typeof window.testClickCount'), {
timeout: 10_000,
})
.toBe('number');
}

const screenshotAllBtn = app.page.locator('button[title^="Screenshot All WebViews"]');
await expect(screenshotAllBtn).toBeEnabled();

await app.openMenuFlyout();
const jsRow = app.page.getByText('JavaScript (All Previews)').locator('xpath=..');
const jsToggle = jsRow.locator('input[type="checkbox"]');

await expect(jsToggle).toBeChecked();
await jsToggle.uncheck({force: true});

await expect(screenshotAllBtn).toBeDisabled();
await expect(screenshotAllBtn).toHaveAttribute(
'title',
'Screenshot All WebViews is unavailable while JavaScript is disabled for one or more previews'
);

await expect
.poll(async () => JSON.stringify(await getWebviewIds(app.electronApp)), {timeout: 10_000})
.not.toBe(JSON.stringify(initialIds));

const disabledIds = await getWebviewIds(app.electronApp);
expect(disabledIds.length).toBe(initialIds.length);

for (const id of disabledIds) {
await expect
.poll(() => execInWebview(app.electronApp, id, 'typeof window.testClickCount'), {
timeout: 10_000,
})
.toBe(WEBVIEW_EXECUTION_FAILED);
}

await app.closeMenuFlyout();
await app.openMenuFlyout();
const reenabledToggle = app.page
.getByText('JavaScript (All Previews)')
.locator('xpath=..')
.locator('input[type="checkbox"]');

await reenabledToggle.check({force: true});

await expect(screenshotAllBtn).toBeEnabled();
await expect(screenshotAllBtn).toHaveAttribute('title', 'Screenshot All WebViews');

await expect
.poll(async () => JSON.stringify(await getWebviewIds(app.electronApp)), {timeout: 10_000})
.not.toBe(JSON.stringify(disabledIds));

const reenabledIds = await getWebviewIds(app.electronApp);
for (const id of reenabledIds) {
await expect
.poll(() => execInWebview(app.electronApp, id, 'typeof window.testClickCount'), {
timeout: 10_000,
})
.toBe('number');
}

await app.closeMenuFlyout();
});
});
5 changes: 3 additions & 2 deletions desktop-app/src/renderer/components/DropDown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ interface Props {
label: JSX.Element | string;
options: OptionOrSeparator[];
className?: string | null;
showChevron?: boolean;
}

export function DropDown({label, options, className}: Props) {
export function DropDown({label, options, className, showChevron = true}: Props) {
return (
<div className="relative text-right">
<Menu as="div" className={`inline-block text-left ${className}`}>
<Float placement="bottom-end" flip portal>
<Menu.Button className="inline-flex w-full justify-center gap-1 rounded-md bg-opacity-20 p-2 text-sm font-medium hover:bg-slate-300 hover:bg-opacity-30 focus:outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-opacity-75 dark:hover:bg-slate-700">
{label}
<Icon icon="mdi:chevron-down" />
{showChevron ? <Icon icon="mdi:chevron-down" /> : null}
</Menu.Button>
<Transition
as={Fragment}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {configureStore} from '@reduxjs/toolkit';
import {render, screen} from '@testing-library/react';
import {Provider} from 'react-redux';
import javascriptReducer from 'renderer/store/features/javascript';
import Toolbar from './Toolbar';

vi.mock('use-sound', () => ({
default: () => [vi.fn()],
}));

vi.mock('./ColorBlindnessTools', () => ({
ColorBlindnessTools: () => null,
}));

vi.mock('./DesignOverlayControls', () => ({
default: () => null,
}));

const createStore = () =>
configureStore({
reducer: {
javascript: javascriptReducer,
},
});

const baseProps = {
webview: null,
device: {
id: '10008',
name: 'iPhone XR',
width: 414,
height: 896,
userAgent: 'test-agent',
type: 'phone',
dpr: 2,
isTouchCapable: true,
isMobileCapable: true,
capabilities: ['touch', 'mobile'],
},
setScreenshotInProgress: vi.fn(),
openDevTools: vi.fn(),
toggleRuler: vi.fn(),
onRotate: vi.fn(),
onIndividualLayoutHandler: vi.fn(),
isIndividualLayout: false,
isDeviceRotationEnabled: true,
};

describe('Device Toolbar', () => {
it('disables screenshot buttons when javascript is disabled for the preview', () => {
render(
<Provider store={createStore()}>
<Toolbar {...baseProps} isJavaScriptDisabled />
</Provider>
);

const disabledButtons = screen.getAllByTitle(
'Screenshots are unavailable while JavaScript is disabled for this preview'
);

expect(disabledButtons).toHaveLength(2);
disabledButtons.forEach((button) => {
expect(button).toBeDisabled();
});
});
});
Loading
Loading