201 lines
5.8 KiB
TypeScript
201 lines
5.8 KiB
TypeScript
import { beforeEach, describe, expect, it } from "vite-plus/test";
|
|
|
|
import {
|
|
acquireBrowserSurface,
|
|
acquireBrowserSurfaceActivity,
|
|
resolveBrowserSurfacePanelRect,
|
|
useBrowserSurfaceStore,
|
|
} from "./browserSurfaceStore";
|
|
|
|
describe("browserSurfaceStore", () => {
|
|
beforeEach(() => {
|
|
useBrowserSurfaceStore.setState({ activityByTabId: {}, byTabId: {} });
|
|
});
|
|
|
|
it("keeps concurrent background work active until every lease is released", () => {
|
|
const first = acquireBrowserSurfaceActivity("background-browser");
|
|
const second = acquireBrowserSurfaceActivity("background-browser");
|
|
|
|
first();
|
|
expect(useBrowserSurfaceStore.getState().activityByTabId["background-browser"]).toBe(1);
|
|
|
|
second();
|
|
expect(useBrowserSurfaceStore.getState().activityByTabId["background-browser"]).toBeUndefined();
|
|
});
|
|
|
|
it("freezes the source content dimensions for a fitted presentation", () => {
|
|
const tabId = "fitted-browser-surface";
|
|
const sourceOwner = Symbol("source");
|
|
const sourceContent = {
|
|
x: 10,
|
|
y: 20,
|
|
width: 1_280,
|
|
height: 720,
|
|
scale: 1,
|
|
scrollLeft: 0,
|
|
scrollTop: 0,
|
|
};
|
|
useBrowserSurfaceStore.getState().claim(tabId, sourceOwner, false);
|
|
useBrowserSurfaceStore.getState().presentContent(tabId, sourceContent);
|
|
|
|
const fittedLease = acquireBrowserSurface(tabId, true);
|
|
useBrowserSurfaceStore.getState().presentContent(tabId, {
|
|
...sourceContent,
|
|
width: 360,
|
|
height: 203,
|
|
scale: 0.28125,
|
|
});
|
|
|
|
expect(useBrowserSurfaceStore.getState().byTabId[tabId]?.fittedSourceContent).toEqual(
|
|
sourceContent,
|
|
);
|
|
fittedLease.release();
|
|
});
|
|
|
|
it("freezes the first content dimensions when fitting starts before the browser is measured", () => {
|
|
const tabId = "pending-fitted-browser-surface";
|
|
const fittedLease = acquireBrowserSurface(tabId, true);
|
|
const sourceContent = {
|
|
x: 0,
|
|
y: 0,
|
|
width: 1_280,
|
|
height: 720,
|
|
scale: 1,
|
|
scrollLeft: 0,
|
|
scrollTop: 0,
|
|
};
|
|
|
|
useBrowserSurfaceStore.getState().presentContent(tabId, sourceContent);
|
|
useBrowserSurfaceStore.getState().presentContent(tabId, {
|
|
...sourceContent,
|
|
width: 320,
|
|
height: 180,
|
|
scale: 0.25,
|
|
});
|
|
|
|
expect(useBrowserSurfaceStore.getState().byTabId[tabId]?.fittedSourceContent).toEqual(
|
|
sourceContent,
|
|
);
|
|
fittedLease.release();
|
|
});
|
|
|
|
it("tracks content dimensions for a browser that has never been visible", () => {
|
|
const tabId = "hidden-browser-surface-content-test";
|
|
useBrowserSurfaceStore.getState().presentContent(tabId, {
|
|
x: 0,
|
|
y: 0,
|
|
width: 393,
|
|
height: 852,
|
|
scale: 1,
|
|
scrollLeft: 0,
|
|
scrollTop: 0,
|
|
});
|
|
|
|
expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toMatchObject({
|
|
rect: null,
|
|
visible: false,
|
|
content: { width: 393, height: 852 },
|
|
});
|
|
});
|
|
|
|
it("keeps a hidden background tab on its own last rect", () => {
|
|
const staleRect = { x: 0, y: 0, width: 500, height: 700 };
|
|
const liveRect = { x: 10, y: 20, width: 900, height: 640 };
|
|
expect(
|
|
resolveBrowserSurfacePanelRect(
|
|
{
|
|
hidden: {
|
|
rect: staleRect,
|
|
visible: false,
|
|
zIndex: 30,
|
|
content: null,
|
|
fittedSourceContent: null,
|
|
fitSourceContent: false,
|
|
cornerRadius: 0,
|
|
updatedAt: 1,
|
|
owner: null,
|
|
},
|
|
active: {
|
|
rect: liveRect,
|
|
visible: true,
|
|
zIndex: 30,
|
|
content: null,
|
|
fittedSourceContent: null,
|
|
fitSourceContent: false,
|
|
cornerRadius: 0,
|
|
updatedAt: 2,
|
|
owner: null,
|
|
},
|
|
},
|
|
"hidden",
|
|
),
|
|
).toEqual(staleRect);
|
|
});
|
|
|
|
it("ignores updates and releases from a stale surface lease", () => {
|
|
const tabId = "leased-browser-surface";
|
|
const staleRect = { x: 0, y: 0, width: 500, height: 700 };
|
|
const liveRect = { x: 10, y: 20, width: 900, height: 640 };
|
|
const staleLease = acquireBrowserSurface(tabId);
|
|
staleLease.present(staleRect, true);
|
|
|
|
const liveLease = acquireBrowserSurface(tabId);
|
|
liveLease.present(liveRect, true);
|
|
expect(staleLease.present(staleRect, true)).toBe(false);
|
|
staleLease.release();
|
|
|
|
expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toMatchObject({
|
|
rect: liveRect,
|
|
visible: true,
|
|
});
|
|
});
|
|
|
|
it("hides a surface when its current lease is released", () => {
|
|
const tabId = "released-browser-surface";
|
|
const lease = acquireBrowserSurface(tabId);
|
|
lease.present({ x: 10, y: 20, width: 900, height: 640 }, true);
|
|
|
|
lease.release();
|
|
lease.present({ x: 0, y: 0, width: 1, height: 1 }, true);
|
|
|
|
expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toMatchObject({
|
|
visible: false,
|
|
owner: null,
|
|
});
|
|
});
|
|
|
|
it("keeps the requested layer with the active surface lease", () => {
|
|
const tabId = "layered-browser-surface";
|
|
const lease = acquireBrowserSurface(tabId);
|
|
lease.present({ x: 10, y: 20, width: 320, height: 200 }, true, 12, 48);
|
|
|
|
expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toMatchObject({
|
|
visible: true,
|
|
zIndex: 48,
|
|
});
|
|
});
|
|
|
|
it("clears fitted presentation state when its lease is released", () => {
|
|
const tabId = "released-fitted-browser-surface";
|
|
const fittedLease = acquireBrowserSurface(tabId, true);
|
|
useBrowserSurfaceStore.getState().presentContent(tabId, {
|
|
x: 0,
|
|
y: 0,
|
|
width: 1_280,
|
|
height: 800,
|
|
scale: 1,
|
|
scrollLeft: 0,
|
|
scrollTop: 0,
|
|
});
|
|
|
|
fittedLease.release();
|
|
|
|
expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toMatchObject({
|
|
fittedSourceContent: null,
|
|
fitSourceContent: false,
|
|
owner: null,
|
|
visible: false,
|
|
});
|
|
});
|
|
});
|