|
1 | 1 | import { expect, sourceURL, story } from "../../stories/playwright/story" |
| 2 | +import type { Locator, Page } from "@playwright/test" |
2 | 3 |
|
3 | 4 | const source = (path: string) => sourceURL(new URL(path, import.meta.url)) |
4 | 5 |
|
@@ -876,13 +877,18 @@ story("lists recent pages on a blank tab and opens one", async ({ page }) => { |
876 | 877 | await expect(address).toHaveValue("http://localhost:5173/settings") |
877 | 878 | }) |
878 | 879 |
|
879 | | -story("adds the whole page to the session and keeps the draft when the button is pressed again", async ({ page }) => { |
880 | | - const root = page.getByTestId("browser-pane-fixture") |
| 880 | +story("keeps a page note editable beside a tiny viewport and attaches it to the session", async ({ page }) => { |
| 881 | + const { root, viewport, width, height } = viewportControls(page) |
| 882 | + await viewport.click() |
| 883 | + await width.fill("8") |
| 884 | + await height.fill("8") |
| 885 | + await expectSize(root.getByTestId("native-Alpha"), 8, 8) |
881 | 886 | const field = root.locator('[data-component="browser-address"]') |
882 | 887 | const add = root.getByRole("button", { name: "Add page to session", exact: true }) |
883 | 888 | await field.hover() |
884 | 889 | await add.click() |
885 | 890 | const editor = root.locator('[data-slot="browser-comment-editor"] textarea') |
| 891 | + await editor.click() |
886 | 892 | await editor.fill("Check the header layout") |
887 | 893 | await field.hover() |
888 | 894 | await add.click() |
@@ -913,12 +919,51 @@ story("says a failed cookie read failed and reads again on retry", async ({ page |
913 | 919 | await expect(site.getByRole("button", { name: "Clear", exact: true })).toBeEnabled() |
914 | 920 | }) |
915 | 921 |
|
916 | | -story("zooms the page from the browser options in view and keeps its zoom in the address field", async ({ page }) => { |
| 922 | +function viewportControls(page: Page) { |
917 | 923 | const root = page.getByTestId("browser-pane-fixture") |
| 924 | + |
| 925 | + const controls = root.getByRole("region", { name: "Viewport", exact: true }) |
| 926 | + |
| 927 | + return { |
| 928 | + root, |
| 929 | + controls, |
| 930 | + viewport: root.getByRole("button", { name: "Toggle browser toolbar", exact: true }), |
| 931 | + preset: controls.getByRole("button", { name: "Device preset", exact: true }), |
| 932 | + option: (name: string) => page.getByRole("menuitemradio", { name, exact: true }), |
| 933 | + moreDevices: page.getByRole("menuitem", { name: "More devices", exact: true }), |
| 934 | + width: controls.getByLabel("Viewport width in CSS pixels", { exact: true }), |
| 935 | + height: controls.getByLabel("Viewport height in CSS pixels", { exact: true }), |
| 936 | + scale: controls.getByRole("button", { name: "Preview scale", exact: true }), |
| 937 | + rotate: controls.getByRole("button", { name: "Rotate viewport", exact: true }), |
| 938 | + } |
| 939 | +} |
| 940 | + |
| 941 | +function resizePane(root: Locator, width: number) { |
| 942 | + return root.locator("#browser-panel").evaluate((pane, width) => { |
| 943 | + if (!pane.parentElement) throw new Error("Missing pane container") |
| 944 | + pane.parentElement.style.width = `${width}px` |
| 945 | + }, width) |
| 946 | +} |
| 947 | + |
| 948 | +async function expectSize(native: Locator, width: number, height: number) { |
| 949 | + await expect(native).toHaveAttribute("data-width", String(width)) |
| 950 | + await expect(native).toHaveAttribute("data-height", String(height)) |
| 951 | +} |
| 952 | + |
| 953 | +story("zooms the page from the browser options in view and keeps its zoom in the address field", async ({ page }) => { |
| 954 | + const { root, controls, viewport, preset, option, scale } = viewportControls(page) |
| 955 | + await viewport.click() |
| 956 | + await preset.click() |
| 957 | + await option("Samsung Galaxy S26").click() |
| 958 | + await expect(preset).toHaveText("Samsung Galaxy S26") |
918 | 959 | const options = root.getByRole("button", { name: "Browser options", exact: true }) |
919 | 960 | await options.click() |
920 | 961 | await page.getByRole("menuitem", { name: /^Zoom in/ }).click() |
921 | 962 | await expect(root.getByText("Zoom: 110", { exact: true })).toBeVisible() |
| 963 | + await expect(controls.getByRole("status")).toContainText("The page is using the pane size because page zoom changed.") |
| 964 | + await expect(preset).toBeDisabled() |
| 965 | + await expect(preset).toHaveText("Responsive") |
| 966 | + await expect(scale).toBeDisabled() |
922 | 967 | // The menu covered the page with a still; it closes so the zoomed page shows at once. |
923 | 968 | await expect(page.getByRole("menu")).toBeHidden() |
924 | 969 | await expect(root.getByTestId("native-Alpha")).toHaveAttribute("data-visible", "true") |
@@ -928,4 +973,218 @@ story("zooms the page from the browser options in view and keeps its zoom in the |
928 | 973 | await zoom.click() |
929 | 974 | await expect(root.getByText("Zoom: 100", { exact: true })).toBeVisible() |
930 | 975 | await expect(zoom).toBeHidden() |
| 976 | + await expect(preset).toBeEnabled() |
| 977 | +}) |
| 978 | + |
| 979 | +story("restores the full pane when the toolbar closes and keeps the selected device on reopen", async ({ page }) => { |
| 980 | + const { root, controls, viewport, preset, option, moreDevices, width, height } = viewportControls(page) |
| 981 | + |
| 982 | + const native = root.getByTestId("native-Alpha") |
| 983 | + |
| 984 | + const fullWidth = await native.getAttribute("data-width") |
| 985 | + |
| 986 | + const fullHeight = await native.getAttribute("data-height") |
| 987 | + |
| 988 | + if (!fullWidth || !fullHeight) throw new Error("Missing native page bounds") |
| 989 | + |
| 990 | + await viewport.click() |
| 991 | + await preset.press("ArrowDown") |
| 992 | + const menu = page.getByRole("menu") |
| 993 | + await expect(option("Responsive")).toBeChecked() |
| 994 | + await expect(menu.getByRole("menuitemradio")).toHaveText([ |
| 995 | + "Responsive", |
| 996 | + "Samsung Galaxy S26", |
| 997 | + "iPhone 18 Pro", |
| 998 | + "iPad / iPad Air 11″", |
| 999 | + "Laptop", |
| 1000 | + "Desktop", |
| 1001 | + ]) |
| 1002 | + await page.keyboard.press("End") |
| 1003 | + await expect(moreDevices).toBeFocused() |
| 1004 | + await page.keyboard.press("ArrowRight") |
| 1005 | + const mini = option("iPad mini") |
| 1006 | + await mini.click() |
| 1007 | + await expect(preset).toHaveText("iPad mini") |
| 1008 | + await expect(preset).toBeFocused() |
| 1009 | + await expect(native).not.toHaveAttribute("data-width", fullWidth) |
| 1010 | + await viewport.click() |
| 1011 | + await expect(controls).toBeHidden() |
| 1012 | + await expect(native).toHaveAttribute("data-width", fullWidth) |
| 1013 | + await expect(native).toHaveAttribute("data-height", fullHeight) |
| 1014 | + await expect(root.getByRole("separator", { name: "Resize viewport width" })).toBeHidden() |
| 1015 | + await viewport.click() |
| 1016 | + await expect(preset).toHaveText("iPad mini") |
| 1017 | + await expect(width).toHaveValue("756") |
| 1018 | + await expect(height).toHaveValue("1158") |
| 1019 | + // Matching the preset's dimensions by hand still counts as Custom. |
| 1020 | + await width.fill("700") |
| 1021 | + await width.fill("756") |
| 1022 | + await expect(preset).toHaveText("Custom") |
| 1023 | + await preset.click() |
| 1024 | + await expect(option("Custom")).toBeChecked() |
| 1025 | + await moreDevices.hover() |
| 1026 | + await expect(mini).not.toBeChecked() |
| 1027 | +}) |
| 1028 | + |
| 1029 | +story("fits an exact viewport while resizing and rejects invalid edits", async ({ page }) => { |
| 1030 | + const { root, viewport, preset, option, scale, width, height } = viewportControls(page) |
| 1031 | + |
| 1032 | + const native = root.getByTestId("native-Alpha") |
| 1033 | + await viewport.click() |
| 1034 | + await expect(width).toHaveValue("624") |
| 1035 | + await width.fill("1248") |
| 1036 | + await expect(scale).toHaveText("Fit - 50%") |
| 1037 | + await expect(width).toBeFocused() |
| 1038 | + await height.fill("5") |
| 1039 | + // Chromium needs whole native pixels: round the fitted height up. |
| 1040 | + await expectSize(native, 624, 3) |
| 1041 | + await expect(height).toBeFocused() |
| 1042 | + |
| 1043 | + await resizePane(root, 328) |
| 1044 | + await expect(scale).toHaveText("Fit - 25%") |
| 1045 | + await expectSize(native, 312, 2) |
| 1046 | + await expect(width).toHaveValue("1248") |
| 1047 | + await expect(height).toHaveValue("5") |
| 1048 | + |
| 1049 | + for (const edit of ["", "4097"]) { |
| 1050 | + await width.fill(edit) |
| 1051 | + await expect(width).toHaveValue(edit) |
| 1052 | + await expect(width).toHaveAttribute("aria-invalid", "true") |
| 1053 | + await expect(width).toHaveAccessibleDescription("Use whole numbers from 1 to 4096 for width and height.") |
| 1054 | + await expectSize(native, 312, 2) |
| 1055 | + } |
| 1056 | + |
| 1057 | + await width.blur() |
| 1058 | + await expect(width).toHaveValue("1248") |
| 1059 | + await expect(width).not.toHaveAttribute("aria-invalid", "true") |
| 1060 | + await preset.click() |
| 1061 | + await option("Responsive").click() |
| 1062 | + await expect(preset).toHaveText("Responsive") |
| 1063 | + await expect(width).toHaveValue("312") |
| 1064 | + await expect(scale).toHaveText("Fit - 100%") |
| 1065 | + await resizePane(root, 640) |
| 1066 | + await expect(width).toHaveValue("624") |
| 1067 | + await expect(native).toHaveAttribute("data-width", "624") |
| 1068 | +}) |
| 1069 | + |
| 1070 | +story("resizes a device with the RTL side grip by pointer and keyboard", async ({ page }) => { |
| 1071 | + const { root, viewport, preset, option, scale, width, height } = viewportControls(page) |
| 1072 | + await root.evaluate((element) => element.setAttribute("dir", "rtl")) |
| 1073 | + await viewport.click() |
| 1074 | + await preset.click() |
| 1075 | + await option("Samsung Galaxy S26").click() |
| 1076 | + await scale.click() |
| 1077 | + await option("50%").click() |
| 1078 | + await expectSize(root.getByTestId("native-Alpha"), 180, 390) |
| 1079 | + const grip = root.getByRole("separator", { name: "Resize viewport width", exact: true }) |
| 1080 | + |
| 1081 | + const box = await grip.boundingBox() |
| 1082 | + |
| 1083 | + if (!box) throw new Error("Missing viewport resize grip") |
| 1084 | + const x = box.x + box.width / 2 |
| 1085 | + |
| 1086 | + const y = box.y + box.height / 2 |
| 1087 | + await page.mouse.move(x, y) |
| 1088 | + await page.mouse.down() |
| 1089 | + await page.mouse.move(x - 12, y) |
| 1090 | + // Both edges move 12 screen pixels, or 24 guest CSS pixels each at 50% scale. |
| 1091 | + await expect(width).toHaveValue("408") |
| 1092 | + await expect(height).toHaveValue("780") |
| 1093 | + await expect(preset).toHaveText("Custom") |
| 1094 | + await expectSize(root.getByTestId("native-Alpha"), 204, 390) |
| 1095 | + await expect(root.getByTestId("native-Alpha")).toHaveAttribute("data-visible", "true") |
| 1096 | + await expect(root.locator('img[elementtiming="embed-snapshot"]')).toHaveCount(0) |
| 1097 | + await page.mouse.up() |
| 1098 | + await grip.press("Shift+ArrowRight") |
| 1099 | + await expect(width).toHaveValue("398") |
| 1100 | +}) |
| 1101 | + |
| 1102 | +story("pins the responsive dimensions when a manual preview scale is selected", async ({ page }) => { |
| 1103 | + const { root, viewport, preset, option, scale, width, height } = viewportControls(page) |
| 1104 | + |
| 1105 | + const native = root.getByTestId("native-Alpha") |
| 1106 | + await viewport.click() |
| 1107 | + await expect(width).toHaveValue("624") |
| 1108 | + const responsiveHeight = await height.inputValue() |
| 1109 | + await expect(scale).toHaveText("Fit - 100%") |
| 1110 | + await scale.click() |
| 1111 | + const menu = page.getByRole("menu") |
| 1112 | + await expect(menu.getByRole("menuitemradio")).toHaveText([ |
| 1113 | + "Fit to pane", |
| 1114 | + "50%", |
| 1115 | + "75%", |
| 1116 | + "100%", |
| 1117 | + "125%", |
| 1118 | + "150%", |
| 1119 | + "200%", |
| 1120 | + ]) |
| 1121 | + const fit = option("Fit to pane") |
| 1122 | + await expect(fit).toBeChecked() |
| 1123 | + const double = option("200%") |
| 1124 | + await double.click() |
| 1125 | + await resizePane(root, 400) |
| 1126 | + await expect(scale).toHaveText("200%") |
| 1127 | + await expect(preset).toHaveText("Custom") |
| 1128 | + await expect(width).toHaveValue("624") |
| 1129 | + await expect(height).toHaveValue(responsiveHeight) |
| 1130 | + |
| 1131 | + await width.fill("120") |
| 1132 | + await height.fill("80") |
| 1133 | + await expectSize(native, 240, 160) |
| 1134 | + await scale.click() |
| 1135 | + await expect(double).toBeChecked() |
| 1136 | + await fit.click() |
| 1137 | + await expect(scale).toHaveText("Fit - 100%") |
| 1138 | + await expectSize(native, 120, 80) |
| 1139 | +}) |
| 1140 | + |
| 1141 | +story("keeps the selected device and its rotated viewport with its browser tab", async ({ page }) => { |
| 1142 | + const { root, viewport, preset, moreDevices, option, width, height, scale, rotate } = viewportControls(page) |
| 1143 | + |
| 1144 | + const switchTab = root.getByRole("button", { name: "Switch browser tab", exact: true }) |
| 1145 | + await viewport.click() |
| 1146 | + await scale.click() |
| 1147 | + await option("50%").click() |
| 1148 | + await preset.click() |
| 1149 | + await moreDevices.hover() |
| 1150 | + const pixel = option("Pixel 10") |
| 1151 | + await pixel.click() |
| 1152 | + await expect(preset).toHaveText("Pixel 10") |
| 1153 | + await rotate.click() |
| 1154 | + await expectSize(root.getByTestId("native-Alpha"), 462, 206) |
| 1155 | + |
| 1156 | + await switchTab.click() |
| 1157 | + await expect(root.getByTestId("native-Second")).toHaveAttribute("data-visible", "true") |
| 1158 | + await expect(preset).toHaveText("Responsive") |
| 1159 | + await width.fill("200") |
| 1160 | + await height.fill("180") |
| 1161 | + const second = root.getByTestId("native-Second") |
| 1162 | + await expectSize(second, 200, 180) |
| 1163 | + await expect(scale).toHaveText("Fit - 100%") |
| 1164 | + |
| 1165 | + // Wrapping the toolbar moves the centered native view on both axes without changing its size. |
| 1166 | + await page.evaluate(() => document.fonts.ready.then(() => undefined)) |
| 1167 | + |
| 1168 | + const embed = root.locator('[data-component="browser-preview"] > div:first-child') |
| 1169 | + |
| 1170 | + const wide = await embed.boundingBox() |
| 1171 | + await resizePane(root, 320) |
| 1172 | + const narrow = await embed.boundingBox() |
| 1173 | + |
| 1174 | + if (!wide || !narrow) throw new Error("Missing preview bounds") |
| 1175 | + expect(narrow.x).toBeLessThan(wide.x) |
| 1176 | + expect(narrow.y).toBeGreaterThan(wide.y) |
| 1177 | + await expect(second).toHaveAttribute("data-x", String(Math.round(narrow.x))) |
| 1178 | + await expect(second).toHaveAttribute("data-y", String(Math.round(narrow.y))) |
| 1179 | + await expectSize(second, 200, 180) |
| 1180 | + |
| 1181 | + await switchTab.click() |
| 1182 | + await expect(root.getByTestId("native-Alpha")).toHaveAttribute("data-visible", "true") |
| 1183 | + await expect(width).toHaveValue("924") |
| 1184 | + await expect(height).toHaveValue("412") |
| 1185 | + await expect(preset).toHaveText("Pixel 10") |
| 1186 | + await expect(scale).toHaveText("50%") |
| 1187 | + await preset.click() |
| 1188 | + await moreDevices.hover() |
| 1189 | + await expect(pixel).toBeChecked() |
931 | 1190 | }) |