Skip to content

Commit 006f4f5

Browse files
hashseedDevtools-frontend LUCI CQ
authored andcommitted
Allow device mode to be toggled for chrome:// pages
... but do not actually enable device mode for chrome:// pages. This restores the user flow of enabling device emulation on NTP before navigating to the site that the user wants to test. Fixed: 388411685, 388411685 Change-Id: If9b883b7337fe321fccd52a350bd939479c85146 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6289404 Auto-Submit: Yang Guo <[email protected]> Commit-Queue: Yang Guo <[email protected]> Reviewed-by: Philip Pfaffe <[email protected]>
1 parent aee15ed commit 006f4f5

File tree

3 files changed

+34
-77
lines changed

3 files changed

+34
-77
lines changed

front_end/panels/emulation/DeviceModeWrapper.ts

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ export class DeviceModeWrapper extends UI.Widget.VBox {
1919
private deviceModeView: DeviceModeView|null;
2020
private readonly toggleDeviceModeAction: UI.ActionRegistration.Action;
2121
private showDeviceModeSetting: Common.Settings.Setting<boolean>;
22-
private enableOncePossible: boolean;
2322

2423
private constructor(inspectedPagePlaceholder: InspectedPagePlaceholder) {
2524
super();
2625
this.inspectedPagePlaceholder = inspectedPagePlaceholder;
2726
this.deviceModeView = null;
28-
this.enableOncePossible = false;
2927
this.toggleDeviceModeAction = UI.ActionRegistry.ActionRegistry.instance().getAction('emulation.toggle-device-mode');
3028
const model = EmulationModel.DeviceModeModel.DeviceModeModel.instance();
3129
this.showDeviceModeSetting = model.enabledSetting();
@@ -35,7 +33,7 @@ export class DeviceModeWrapper extends UI.Widget.VBox {
3533
SDK.OverlayModel.OverlayModel, SDK.OverlayModel.Events.SCREENSHOT_REQUESTED,
3634
this.screenshotRequestedFromOverlay, this);
3735
SDK.TargetManager.TargetManager.instance().addEventListener(
38-
SDK.TargetManager.Events.INSPECTED_URL_CHANGED, this.inspectedUrlChanged, this);
36+
SDK.TargetManager.Events.INSPECTED_URL_CHANGED, () => this.update(), this);
3937
this.update(true);
4038
}
4139

@@ -56,26 +54,6 @@ export class DeviceModeWrapper extends UI.Widget.VBox {
5654
return deviceModeWrapperInstance;
5755
}
5856

59-
inspectedUrlChanged(event: {data: SDK.Target.Target}): void {
60-
const url = event.data.inspectedURL();
61-
// Only allow device mode for non chrome:// pages.
62-
const canEnable = url !== null && !Common.ParsedURL.schemeIs(url, 'chrome:');
63-
this.toggleDeviceModeAction.setEnabled(canEnable);
64-
if (!canEnable && this.isDeviceModeOn()) {
65-
// Device mode is enabled, but we navigated to a chrome:// page.
66-
// Remember to enable device mode again when next possible.
67-
this.toggleDeviceMode();
68-
this.enableOncePossible = true;
69-
}
70-
if (canEnable && !this.isDeviceModeOn() && this.enableOncePossible) {
71-
// Device mode is not enabled, could be enabled, and we have a reminder to enable.
72-
this.toggleDeviceMode();
73-
this.enableOncePossible = false;
74-
}
75-
76-
this.update();
77-
}
78-
7957
toggleDeviceMode(): void {
8058
this.showDeviceModeSetting.set(!this.showDeviceModeSetting.get());
8159
}
@@ -106,14 +84,20 @@ export class DeviceModeWrapper extends UI.Widget.VBox {
10684

10785
update(force?: boolean): void {
10886
this.toggleDeviceModeAction.setToggled(this.showDeviceModeSetting.get());
109-
if (!force) {
110-
const showing = this.deviceModeView?.isShowing();
111-
if (this.showDeviceModeSetting.get() === showing) {
112-
return;
113-
}
87+
88+
// Only allow device mode for non chrome:// pages.
89+
function allowDeviceMode(): boolean {
90+
const target = SDK.TargetManager.TargetManager.instance().primaryPageTarget();
91+
const url = target?.inspectedURL();
92+
return url ? !Common.ParsedURL.schemeIs(url, 'chrome:') : false;
93+
}
94+
95+
const shouldShow = this.showDeviceModeSetting.get() && allowDeviceMode();
96+
if (!force && shouldShow === this.deviceModeView?.isShowing()) {
97+
return;
11498
}
11599

116-
if (this.showDeviceModeSetting.get()) {
100+
if (shouldShow) {
117101
if (!this.deviceModeView) {
118102
this.deviceModeView = new DeviceModeView();
119103
}

test/e2e/emulation/disable-device-mode_test.ts

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import {
99
} from '../../shared/helper.js';
1010
import {
1111
clickDeviceModeToggler,
12-
deviceModeButtonCanEnable,
1312
deviceModeIsEnabled,
14-
openDeviceToolbar,
13+
deviceModeIsToggled,
1514
reloadDockableFrontEnd,
1615
} from '../helpers/emulation-helpers.js';
1716

@@ -22,50 +21,25 @@ describe('Disable device mode for chrome:// pages', () => {
2221
});
2322

2423
it('chrome:// pages disable device mode', async () => {
25-
// Button is untoggled and enabled initially.
26-
assert.isFalse(await deviceModeIsEnabled());
27-
assert.isTrue(await deviceModeButtonCanEnable());
28-
// Button is untoggled and enabled for about://blank.
29-
await goTo('about://blank');
30-
assert.isFalse(await deviceModeIsEnabled());
31-
assert.isTrue(await deviceModeButtonCanEnable());
32-
// Button is untoggled and enabled for chrome://version.
24+
// Button is untoggled and device mode is off for chrome://version.
3325
await goTo('chrome://version');
26+
assert.isFalse(await deviceModeIsToggled());
3427
assert.isFalse(await deviceModeIsEnabled());
35-
assert.isFalse(await deviceModeButtonCanEnable());
36-
// Clicking on the button does not enable device mode.
28+
// Clicking on the button toggles the button but does not enable device mode.
3729
await clickDeviceModeToggler();
30+
assert.isTrue(await deviceModeIsToggled());
3831
assert.isFalse(await deviceModeIsEnabled());
39-
assert.isFalse(await deviceModeButtonCanEnable());
40-
// Button is untoggled and enabled for about://blank.
41-
await goTo('about://blank');
42-
assert.isFalse(await deviceModeIsEnabled());
43-
assert.isTrue(await deviceModeButtonCanEnable());
44-
// Clicking on the button enables device mode.
45-
await openDeviceToolbar();
46-
assert.isTrue(await deviceModeIsEnabled());
47-
assert.isTrue(await deviceModeButtonCanEnable());
48-
});
49-
50-
it('device mode turns back on automatically', async () => {
51-
// Button is untoggled and enabled initially.
52-
assert.isFalse(await deviceModeIsEnabled());
53-
assert.isTrue(await deviceModeButtonCanEnable());
54-
// Button is untoggled and enabled for about://blank.
32+
// Button is is toggled and device mode is on for about://blank.
5533
await goTo('about://blank');
56-
assert.isFalse(await deviceModeIsEnabled());
57-
assert.isTrue(await deviceModeButtonCanEnable());
58-
// Clicking on the button enables device mode.
59-
await openDeviceToolbar();
34+
assert.isTrue(await deviceModeIsToggled());
6035
assert.isTrue(await deviceModeIsEnabled());
61-
assert.isTrue(await deviceModeButtonCanEnable());
62-
// Navigating to chrome://version disables device mode.
36+
// Navigating back to chrome://version turns device mode off but leaves the button toggled.
6337
await goTo('chrome://version');
38+
assert.isTrue(await deviceModeIsToggled());
39+
assert.isFalse(await deviceModeIsEnabled());
40+
// Clicking on the button untoggles the button and disables device mode.
41+
await clickDeviceModeToggler();
42+
assert.isFalse(await deviceModeIsToggled());
6443
assert.isFalse(await deviceModeIsEnabled());
65-
assert.isFalse(await deviceModeButtonCanEnable());
66-
// Navigating back to about://blank re-enables device mode.
67-
await goTo('about://blank');
68-
assert.isTrue(await deviceModeIsEnabled());
69-
assert.isTrue(await deviceModeButtonCanEnable());
7044
});
7145
});

test/e2e/helpers/emulation-helpers.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const reloadDockableFrontEnd = async () => {
3636
await reloadDevTools({canDock: true});
3737
};
3838

39-
export const deviceModeIsEnabled = async () => {
39+
export const deviceModeIsToggled = async () => {
4040
const deviceToolbarToggler = await waitFor(DEVICE_TOOLBAR_TOGGLER_SELECTOR);
4141
const pressed = await deviceToolbarToggler.evaluate(element => {
4242
const button = element.shadowRoot?.querySelector('.primary-toggle') as HTMLButtonElement;
@@ -45,6 +45,13 @@ export const deviceModeIsEnabled = async () => {
4545
return pressed === 'true';
4646
};
4747

48+
export const deviceModeIsEnabled = async () => {
49+
// Check the userAgent string to see whether emulation is really enabled.
50+
const {target} = getBrowserAndPages();
51+
const userAgent = await target.evaluate(() => navigator.userAgent);
52+
return userAgent.includes('Mobile');
53+
};
54+
4855
export const clickDeviceModeToggler = async () => {
4956
const deviceToolbarToggler = await waitFor(DEVICE_TOOLBAR_TOGGLER_SELECTOR);
5057
await clickElement(deviceToolbarToggler);
@@ -58,14 +65,6 @@ export const openDeviceToolbar = async () => {
5865
await waitFor(DEVICE_TOOLBAR_SELECTOR);
5966
};
6067

61-
export const deviceModeButtonCanEnable = async () => {
62-
const deviceToolbarToggler = await waitFor(DEVICE_TOOLBAR_TOGGLER_SELECTOR);
63-
return await deviceToolbarToggler.evaluate(element => {
64-
const button = element.shadowRoot?.querySelector('.primary-toggle') as HTMLButtonElement;
65-
return !button.disabled;
66-
});
67-
};
68-
6968
export const showMediaQueryInspector = async () => {
7069
const inspector = await $(MEDIA_QUERY_INSPECTOR_SELECTOR);
7170
if (inspector) {

0 commit comments

Comments
 (0)