Skip to content

Commit

Permalink
Bug 1786668 - Add a test case that MobileViewportManager is properly …
Browse files Browse the repository at this point in the history
…updated when browser window moves from a HiDPI monitor to normal one. r=tnikkel

Unfortunately this test doesn't run as expected on our CI since macs on our CI
are running normal DPI mode.

I tested this test works properly on my macbook, it fails without the fix in the
previous commit and it passes with the fix.

Differential Revision: https://phabricator.services.mozilla.com/D153688
  • Loading branch information
hiikezoe committed Aug 26, 2022
1 parent 2f8576f commit 4917d58
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 0 deletions.
16 changes: 16 additions & 0 deletions dom/base/nsDOMWindowUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4809,3 +4809,19 @@ nsDOMWindowUtils::GetOrientationLock(uint32_t* aOrientationLock) {
*aOrientationLock = static_cast<uint32_t>(bc->GetOrientationLock());
return NS_OK;
}

NS_IMETHODIMP
nsDOMWindowUtils::SetHiDPIMode(bool aHiDPI) {
nsCOMPtr<nsIWidget> widget = GetWidget();
if (!widget) return NS_ERROR_FAILURE;

return widget->SetHiDPIMode(aHiDPI);
}

NS_IMETHODIMP
nsDOMWindowUtils::RestoreHiDPIMode() {
nsCOMPtr<nsIWidget> widget = GetWidget();
if (!widget) return NS_ERROR_FAILURE;

return widget->RestoreHiDPIMode();
}
10 changes: 10 additions & 0 deletions dom/interfaces/base/nsIDOMWindowUtils.idl
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,16 @@ interface nsIDOMWindowUtils : nsISupports {

bool isCoepCredentialless();

/**
* Simulate enabling/disabling HiDPI mode. This function only works on
* environments where HiDPI mode is available by default.
*/
void setHiDPIMode(in boolean aHiDPI);
/**
* Restore the simulated HiDPI mode.
*/
void restoreHiDPIMode();

/**
* NOTE: Currently works only on GTK+.
*/
Expand Down
2 changes: 2 additions & 0 deletions layout/base/tests/browser.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ support-files =
[browser_bug1701027-2.js]
support-files =
helper_bug1701027-2.html
[browser_bug1757410.js]
run-if = (os == 'mac')
[browser_disableDialogs_onbeforeunload.js]
[browser_onbeforeunload_only_after_interaction.js]
[browser_onbeforeunload_only_after_interaction_in_frame.js]
Expand Down
59 changes: 59 additions & 0 deletions layout/base/tests/browser_bug1757410.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const PAGECONTENT =
"<!DOCTYPE html>" +
"<html>" +
"<style>" +
"html { " +
" height: 120vh;" +
" overflow-y: scroll;" +
"}" +
"</style>" +
"</html>";

const pageUrl = "data:text/html," + encodeURIComponent(PAGECONTENT);

add_task(async function test() {
if (window.devicePixelRatio != 2) {
ok(
true,
"Skip this test since this test is supposed to run on HiDPI mode, " +
"the devixePixelRato on this machine is " +
window.devicePixelRatio
);
return;
}

const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, pageUrl);

// Scroll the content a bit.
const originalScrollPosition = await SpecialPowers.spawn(
tab.linkedBrowser,
[],
async () => {
content.document.scrollingElement.scrollTop = 100;
return content.document.scrollingElement.scrollTop;
}
);

// Disabling HiDPI mode and check the scroll position.
SpecialPowers.DOMWindowUtils.setHiDPIMode(false);
const scrollPosition = await SpecialPowers.spawn(
tab.linkedBrowser,
[],
async () => {
return content.document.scrollingElement.scrollTop;
}
);
is(
originalScrollPosition,
scrollPosition,
"The scroll position should be kept"
);
BrowserTestUtils.removeTab(tab);

SpecialPowers.DOMWindowUtils.restoreHiDPIMode();
});
4 changes: 4 additions & 0 deletions widget/cocoa/nsChildView.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,10 @@ class nsChildView final : public nsBaseWidget {
// Called by nsCocoaWindow when the window's fullscreen state changes.
void UpdateFullscreen(bool aFullscreen);

// test only.
virtual nsresult SetHiDPIMode(bool aHiDPI) override;
virtual nsresult RestoreHiDPIMode() override;

protected:
virtual ~nsChildView();

Expand Down
14 changes: 14 additions & 0 deletions widget/cocoa/nsChildView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4641,6 +4641,20 @@ - (void)pressureChangeWithEvent:(NSEvent*)event {
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}

nsresult nsChildView::SetHiDPIMode(bool aHiDPI) {
nsCocoaUtils::InvalidateHiDPIState();
Preferences::SetInt("gfx.hidpi.enabled", aHiDPI ? 1 : 0);
BackingScaleFactorChanged();
return NS_OK;
}

nsresult nsChildView::RestoreHiDPIMode() {
nsCocoaUtils::InvalidateHiDPIState();
Preferences::ClearUser("gfx.hidpi.enabled");
BackingScaleFactorChanged();
return NS_OK;
}

#pragma mark -

#ifdef ACCESSIBILITY
Expand Down
2 changes: 2 additions & 0 deletions widget/cocoa/nsCocoaUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ class nsCocoaUtils {
*/
static nsresult MaybeRequestScreenCapturePermission();

static void InvalidateHiDPIState();

private:
/**
* Completion handlers used as an argument to the macOS API to
Expand Down
3 changes: 3 additions & 0 deletions widget/cocoa/nsCocoaUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,9 @@ static bool ComputeIsEntirelyBlack(const DataSourceSurface::MappedSurface& aMap,
return sHiDPIEnabled;
}

// static
void nsCocoaUtils::InvalidateHiDPIState() { sHiDPIPrefInitialized = false; }

void nsCocoaUtils::GetCommandsFromKeyEvent(NSEvent* aEvent,
nsTArray<KeyBindingsCommand>& aCommands) {
NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;
Expand Down
5 changes: 5 additions & 0 deletions widget/nsIWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -2121,6 +2121,11 @@ class nsIWidget : public nsISupports {
virtual void SetWindowButtonRect(WindowButtonType aButtonType,
const LayoutDeviceIntRect& aClientRect) {}

virtual nsresult SetHiDPIMode(bool aHiDPI) {
return NS_ERROR_NOT_IMPLEMENTED;
}
virtual nsresult RestoreHiDPIMode() { return NS_ERROR_NOT_IMPLEMENTED; }

protected:
// keep the list of children. We also keep track of our siblings.
// The ownership model is as follows: parent holds a strong ref to
Expand Down

0 comments on commit 4917d58

Please sign in to comment.