@@ -5,8 +5,11 @@ import {
55 ThreadId ,
66 type ClientSettings ,
77 type DesktopPreviewBridge ,
8+ type PreviewViewportSetting ,
89} from "@t3tools/contracts" ;
910import { act } from "react" ;
11+
12+ import "~/index.css" ;
1013import { createRoot , type Root } from "react-dom/client" ;
1114import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
1215
@@ -201,3 +204,94 @@ describe("HostedBrowserWebview settings hydration", () => {
201204 expect ( mocks . setClientSettings ) . not . toHaveBeenCalled ( ) ;
202205 } ) ;
203206} ) ;
207+
208+ describe ( "HostedBrowserWebview viewport layout" , ( ) => {
209+ const runtimeTabId = "zoom-layout-tab" ;
210+ const owner = Symbol ( "panel" ) ;
211+ const panel = { x : 0 , y : 0 , width : 2000 , height : 1600 } ;
212+
213+ async function renderViewport ( viewport : PreviewViewportSetting ) {
214+ mocks . getClientSettings . mockResolvedValue ( {
215+ ...DEFAULT_CLIENT_SETTINGS ,
216+ browserDefaultZoomFactor : 1.5 ,
217+ } ) ;
218+ root ??= createRoot ( container ) ;
219+ await act ( async ( ) => {
220+ await ensureClientSettingsHydrated ( ) ;
221+ root ! . render (
222+ < HostedBrowserWebview
223+ threadRef = { {
224+ environmentId : EnvironmentId . make ( "zoom-layout" ) ,
225+ threadId : ThreadId . make ( "zoom-layout" ) ,
226+ } }
227+ tabId = "zoom-layout"
228+ runtimeTabId = { runtimeTabId }
229+ initialUrl = "about:blank"
230+ viewport = { viewport }
231+ pictureInPicture = { false }
232+ profileId = { undefined }
233+ zoomFactor = { 1.5 }
234+ /> ,
235+ ) ;
236+ } ) ;
237+ const guest = container . querySelector ( "webview" ) ;
238+ expect ( guest ) . not . toBeNull ( ) ;
239+ return guest ! ;
240+ }
241+
242+ it ( "keeps fixed viewports sized in guest pixels across host zoom and fitted surfaces" , async ( ) => {
243+ let windowZoom = 1.25 ;
244+ vi . stubGlobal ( "desktopBridge" , { getWindowZoomFactor : ( ) => windowZoom } ) ;
245+ useBrowserSurfaceStore . getState ( ) . claim ( runtimeTabId , owner , false ) ;
246+ useBrowserSurfaceStore . getState ( ) . present ( runtimeTabId , owner , panel , true , 0 , 30 ) ;
247+ const guest = await renderViewport ( { _tag : "freeform" , width : 800 , height : 600 } ) ;
248+ expect ( guest . getBoundingClientRect ( ) ) . toMatchObject ( { width : 960 , height : 720 } ) ;
249+
250+ windowZoom = 0.75 ;
251+ await act ( ( ) => window . dispatchEvent ( new Event ( "resize" ) ) ) ;
252+ expect ( guest . getBoundingClientRect ( ) ) . toMatchObject ( { width : 1600 , height : 1200 } ) ;
253+
254+ windowZoom = 1 ;
255+ await act ( ( ) => window . dispatchEvent ( new Event ( "resize" ) ) ) ;
256+ expect ( guest . getBoundingClientRect ( ) ) . toMatchObject ( { width : 1200 , height : 900 } ) ;
257+
258+ windowZoom = 1.25 ;
259+ await act ( ( ) => window . dispatchEvent ( new Event ( "resize" ) ) ) ;
260+ await renderViewport ( { _tag : "preset" , presetId : "iphone-se" , width : 375 , height : 667 } ) ;
261+ expect ( guest . getBoundingClientRect ( ) . width ) . toBe ( 450 ) ;
262+ expect ( guest . getBoundingClientRect ( ) . height ) . toBeCloseTo ( 800.4 , 1 ) ;
263+
264+ const miniPlayer = Symbol ( "mini-player" ) ;
265+ await act ( ( ) => {
266+ useBrowserSurfaceStore . getState ( ) . claim ( runtimeTabId , miniPlayer , true ) ;
267+ useBrowserSurfaceStore
268+ . getState ( )
269+ . present ( runtimeTabId , miniPlayer , { x : 0 , y : 0 , width : 225 , height : 401 } , true , 0 , 48 ) ;
270+ } ) ;
271+ expect ( guest . offsetWidth ) . toBe ( 450 ) ;
272+ expect ( guest . offsetHeight ) . toBe ( 800 ) ;
273+ expect ( guest . getBoundingClientRect ( ) . width ) . toBe ( 225 ) ;
274+ expect ( guest . getBoundingClientRect ( ) . height ) . toBeCloseTo ( 400.2 , 1 ) ;
275+
276+ await act ( ( ) => {
277+ useBrowserSurfaceStore . getState ( ) . claim ( runtimeTabId , owner , false ) ;
278+ useBrowserSurfaceStore . getState ( ) . present ( runtimeTabId , owner , panel , true , 0 , 30 ) ;
279+ } ) ;
280+ await renderViewport ( FILL_PREVIEW_VIEWPORT ) ;
281+ expect ( guest . getBoundingClientRect ( ) ) . toMatchObject ( { width : 2000 , height : 1600 } ) ;
282+ expect ( mocks . createTab ) . toHaveBeenCalledExactlyOnceWith ( runtimeTabId , {
283+ zoomFactor : 1.5 ,
284+ colorScheme : DEFAULT_CLIENT_SETTINGS . browserDefaultAppearance ,
285+ } ) ;
286+ } ) ;
287+
288+ it ( "preserves preview scaling with an older bridge or no desktop bridge" , async ( ) => {
289+ vi . stubGlobal ( "desktopBridge" , { } ) ;
290+ const guest = await renderViewport ( { _tag : "freeform" , width : 800 , height : 600 } ) ;
291+ expect ( guest . getBoundingClientRect ( ) ) . toMatchObject ( { width : 1200 , height : 900 } ) ;
292+
293+ vi . stubGlobal ( "desktopBridge" , undefined ) ;
294+ await act ( ( ) => window . dispatchEvent ( new Event ( "resize" ) ) ) ;
295+ expect ( guest . getBoundingClientRect ( ) ) . toMatchObject ( { width : 1200 , height : 900 } ) ;
296+ } ) ;
297+ } ) ;
0 commit comments