Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Desobekify Viewport #1520

Merged
merged 6 commits into from
Nov 7, 2024
Merged
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
6 changes: 3 additions & 3 deletions browser/browser_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ func parseBrowserContextOptions(ctx context.Context, opts sobek.Value) (*common.
case "userAgent":
b.UserAgent = o.Get(k).String()
case "viewport":
var viewport common.Viewport
if err := viewport.Parse(ctx, o.Get(k).ToObject(rt)); err != nil {
var err error
b.Viewport, err = exportTo[common.Viewport](rt, o.Get(k))
if err != nil {
return nil, fmt.Errorf("parsing viewport options: %w", err)
}
b.Viewport = &viewport
}
}

Expand Down
4 changes: 2 additions & 2 deletions common/browser_context_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type BrowserContextOptions struct {
TimezoneID string `js:"timezoneID"`
UserAgent string `js:"userAgent"`
VideosPath string `js:"videosPath"`
Viewport *Viewport `js:"viewport"`
Viewport Viewport `js:"viewport"`
}

// NewBrowserContextOptions creates a default set of browser context options.
Expand All @@ -96,7 +96,7 @@ func NewBrowserContextOptions() *BrowserContextOptions {
Permissions: []string{},
ReducedMotion: ReducedMotionNoPreference,
Screen: Screen{Width: DefaultScreenWidth, Height: DefaultScreenHeight},
Viewport: &Viewport{Width: DefaultScreenWidth, Height: DefaultScreenHeight},
Viewport: Viewport{Width: DefaultScreenWidth, Height: DefaultScreenHeight},
}
}

Expand Down
2 changes: 1 addition & 1 deletion common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ func (fs *FrameSession) updateViewport() error {
if fs.hasUIWindow {
// add an inset to viewport depending on the operating system.
// this won't add an inset if we're running in headless mode.
viewport.calculateInset(
viewport = viewport.recalculateInset(
fs.page.browserCtx.browser.browserOpts.Headless,
runtime.GOOS,
)
Expand Down
36 changes: 13 additions & 23 deletions common/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,21 @@ type Viewport struct {
Height int64 `js:"height"`
}

// Parse viewport details from a given sobek viewport value.
func (v *Viewport) Parse(ctx context.Context, viewport sobek.Value) error {
rt := k6ext.Runtime(ctx)
if viewport != nil && !sobek.IsUndefined(viewport) && !sobek.IsNull(viewport) {
viewport := viewport.ToObject(rt)
for _, k := range viewport.Keys() {
switch k {
case "width":
v.Width = viewport.Get(k).ToInteger()
case "height":
v.Height = viewport.Get(k).ToInteger()
}
}
}

return nil
// IsEmpty returns true if the viewport is empty.
func (v Viewport) IsEmpty() bool {
return v.Width == 0 && v.Height == 0
}

func (v Viewport) String() string {
return fmt.Sprintf("%dx%d", v.Width, v.Height)
}

// calculateInset depending on a given operating system and,
// add the calculated inset width and height to Viewport.
// It won't update the Viewport if headless is true.
func (v *Viewport) calculateInset(headless bool, os string) {
// recalculateInset is used to calculate the inset width and height
// depending on the operating system and add it to the given v, and
// return a new Viewport. It returns the same Viewport if headless is true.
func (v Viewport) recalculateInset(headless bool, os string) Viewport {
if headless {
return
return v
}
// TODO: popup windows have their own insets.
var inset Viewport
Expand All @@ -123,6 +110,9 @@ func (v *Viewport) calculateInset(headless bool, os string) {
// on my Mac and w:0 h:79 works best.
inset = Viewport{Width: 0, Height: 79}
}
v.Width += inset.Width
v.Height += inset.Height

return Viewport{
Width: v.Width + inset.Width,
Height: v.Height + inset.Height,
}
}
8 changes: 4 additions & 4 deletions common/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestViewportCalculateInset(t *testing.T) {
t.Parallel()

headless, vp := true, Viewport{}
vp.calculateInset(headless, "any os")
vp = vp.recalculateInset(headless, "any os")
assert.Equal(t, vp, Viewport{},
"should not change the viewport if headless is true")
})
Expand All @@ -26,7 +26,7 @@ func TestViewportCalculateInset(t *testing.T) {
headless bool
vp Viewport
)
vp.calculateInset(headless, "any os")
vp = vp.recalculateInset(headless, "any os")
assert.NotEqual(t, vp, Viewport{},
"should add the default inset to the viewport if the"+
" operating system is unrecognized by the addInset.")
Expand All @@ -44,10 +44,10 @@ func TestViewportCalculateInset(t *testing.T) {
vp Viewport
)
// add the default inset to the viewport
vp.calculateInset(headless, "any os")
vp = vp.recalculateInset(headless, "any os")
defaultVp := vp
// add an os specific inset to the viewport
vp.calculateInset(headless, os)
vp = vp.recalculateInset(headless, os)

assert.NotEqual(t, vp, defaultVp, "inset for %q should exist", os)
// we multiply the default viewport by two to detect
Expand Down
8 changes: 4 additions & 4 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ func (c *ColorScheme) UnmarshalJSON(b []byte) error {

// EmulatedSize represents the emulated viewport and screen sizes.
type EmulatedSize struct {
Viewport *Viewport
Viewport Viewport
Screen Screen
}

// NewEmulatedSize creates and returns a new EmulatedSize.
func NewEmulatedSize(viewport *Viewport, screen Screen) *EmulatedSize {
func NewEmulatedSize(viewport Viewport, screen Screen) *EmulatedSize {
return &EmulatedSize{
Viewport: viewport,
Screen: screen,
Expand Down Expand Up @@ -275,7 +275,7 @@ func NewPage(

// We need to init viewport and screen size before initializing the main frame session,
// as that's where the emulation is activated.
if bctx.opts.Viewport != nil {
if !bctx.opts.Viewport.IsEmpty() {
p.emulatedSize = NewEmulatedSize(bctx.opts.Viewport, bctx.opts.Screen)
}

Expand Down Expand Up @@ -716,7 +716,7 @@ func (p *Page) setViewportSize(viewportSize *Size) error {
p.logger.Debugf("Page:setViewportSize", "sid:%v vps:%v",
p.sessionID(), viewportSize)

viewport := &Viewport{
viewport := Viewport{
Width: int64(viewportSize.Width),
Height: int64(viewportSize.Height),
}
Expand Down
4 changes: 2 additions & 2 deletions tests/browser_context_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestBrowserContextOptionsDefaultValues(t *testing.T) {
assert.Equal(t, common.Screen{Width: common.DefaultScreenWidth, Height: common.DefaultScreenHeight}, opts.Screen)
assert.Equal(t, "", opts.TimezoneID)
assert.Equal(t, "", opts.UserAgent)
assert.Equal(t, &common.Viewport{Width: common.DefaultScreenWidth, Height: common.DefaultScreenHeight}, opts.Viewport)
assert.Equal(t, common.Viewport{Width: common.DefaultScreenWidth, Height: common.DefaultScreenHeight}, opts.Viewport)
}

func TestBrowserContextOptionsDefaultViewport(t *testing.T) {
Expand All @@ -53,7 +53,7 @@ func TestBrowserContextOptionsSetViewport(t *testing.T) {

tb := newTestBrowser(t)
opts := common.NewBrowserContextOptions()
opts.Viewport = &common.Viewport{
opts.Viewport = common.Viewport{
Width: 800,
Height: 600,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/element_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestElementHandleClickConcealedLink(t *testing.T) {
tb := newTestBrowser(t, withFileServer())

bcopts := common.NewBrowserContextOptions()
bcopts.Viewport = &common.Viewport{
bcopts.Viewport = common.Viewport{
Width: 500,
Height: 240,
}
Expand Down
Loading