Bug 1248619 - Part 3: Restore the previous viewport size, user agent, display pixel ratio and touch simultation properties. r=rcaliman

We also provided reasonable defaults to all our SErvices.prefs getter in case an user's profile
is somehow missing those prefs.
This commit is contained in:
Gabriel Luong 2018-10-09 14:53:29 -04:00
Родитель 3d646a307f
Коммит 46551de89c
7 изменённых файлов: 141 добавлений и 13 удалений

Просмотреть файл

@ -307,6 +307,12 @@ pref("devtools.editor.detectindentation", true);
pref("devtools.editor.enableCodeFolding", true);
pref("devtools.editor.autocomplete", true);
// The width of the viewport.
pref("devtools.responsive.viewport.width", 320);
// The height of the viewport.
pref("devtools.responsive.viewport.height", 480);
// The pixel ratio of the viewport.
pref("devtools.responsive.viewport.pixelRatio", 0);
// Whether or not the viewports are left aligned.
pref("devtools.responsive.leftAlignViewport.enabled", false);
// Whether to reload when touch simulation is toggled
@ -315,6 +321,11 @@ pref("devtools.responsive.reloadConditions.touchSimulation", false);
pref("devtools.responsive.reloadConditions.userAgent", false);
// Whether to show the notification about reloading to apply emulation
pref("devtools.responsive.reloadNotification.enabled", true);
// Whether or not touch simulation is enabled.
pref("devtools.responsive.touchSimulation.enabled", false);
// The user agent of the viewport.
pref("devtools.responsive.userAgent", "");
// Whether to show the settings onboarding tooltip only in release or beta builds.
#if defined(RELEASE_OR_BETA)
pref("devtools.responsive.show-setting-tooltip", true);

Просмотреть файл

@ -7,10 +7,9 @@
const { Ci } = require("chrome");
const promise = require("promise");
const Services = require("Services");
const asyncStorage = require("devtools/shared/async-storage");
const EventEmitter = require("devtools/shared/event-emitter");
const TOOL_URL = "chrome://devtools/content/responsive.html/index.xhtml";
loader.lazyRequireGetter(this, "DebuggerClient", "devtools/shared/client/debugger-client", true);
loader.lazyRequireGetter(this, "DebuggerServer", "devtools/server/main", true);
loader.lazyRequireGetter(this, "throttlingProfiles", "devtools/client/shared/components/throttling/profiles");
@ -26,6 +25,8 @@ loader.lazyRequireGetter(this, "TargetFactory", "devtools/client/framework/targe
loader.lazyRequireGetter(this, "gDevTools", "devtools/client/framework/devtools", true);
loader.lazyRequireGetter(this, "Telemetry", "devtools/client/shared/telemetry");
const TOOL_URL = "chrome://devtools/content/responsive.html/index.xhtml";
const RELOAD_CONDITION_PREF_PREFIX = "devtools.responsive.reloadConditions.";
const RELOAD_NOTIFICATION_PREF = "devtools.responsive.reloadNotification.enabled";
const SHOW_SETTING_TOOLTIP_PREF = "devtools.responsive.show-setting-tooltip";
@ -361,6 +362,9 @@ ResponsiveUI.prototype = {
debug("Wait until RDP server connect");
await this.connectToServer();
// Restore the previous state of RDM.
await this.restoreState();
// Show the settings onboarding tooltip
if (Services.prefs.getBoolPref(SHOW_SETTING_TOOLTIP_PREF)) {
this.settingOnboardingTooltip =
@ -535,7 +539,7 @@ ResponsiveUI.prototype = {
this.onExit();
break;
case "remove-device-association":
this.onRemoveDeviceAssociation(event);
this.onRemoveDeviceAssociation();
break;
case "viewport-resize":
this.onViewportResize(event);
@ -604,7 +608,7 @@ ResponsiveUI.prototype = {
ResponsiveUIManager.closeIfNeeded(browserWindow, tab);
},
async onRemoveDeviceAssociation(event) {
async onRemoveDeviceAssociation() {
let reloadNeeded = false;
await this.updateDPPX();
reloadNeeded |= await this.updateUserAgent() &&
@ -626,6 +630,40 @@ ResponsiveUI.prototype = {
});
},
/**
* Restores the previous state of RDM.
*/
async restoreState() {
const deviceState = await asyncStorage.getItem("devtools.responsive.deviceState");
if (deviceState) {
// Return if there is a device state to restore, this will be done when the
// device list is loaded after the post-init.
return;
}
const pixelRatio =
Services.prefs.getIntPref("devtools.responsive.viewport.pixelRatio", 0);
const touchSimulationEnabled =
Services.prefs.getBoolPref("devtools.responsive.touchSimulation.enabled", false);
const userAgent = Services.prefs.getCharPref("devtools.responsive.userAgent", "");
let reloadNeeded = false;
await this.updateDPPX(pixelRatio);
if (touchSimulationEnabled) {
reloadNeeded |= await this.updateTouchSimulation(touchSimulationEnabled) &&
this.reloadOnChange("touchSimulation");
}
if (userAgent) {
reloadNeeded |= await this.updateUserAgent(userAgent) &&
this.reloadOnChange("userAgent");
}
if (reloadNeeded) {
this.getViewportBrowser().reload();
}
},
/**
* Set or clear the emulated device pixel ratio.
*

Просмотреть файл

@ -20,22 +20,24 @@ const LEFT_ALIGNMENT_ENABLED = "devtools.responsive.leftAlignViewport.enabled";
const RELOAD_ON_TOUCH_SIMULATION = "devtools.responsive.reloadConditions.touchSimulation";
const RELOAD_ON_USER_AGENT = "devtools.responsive.reloadConditions.userAgent";
const SHOW_USER_AGENT_INPUT = "devtools.responsive.showUserAgentInput";
const TOUCH_SIMULATION_ENABLED = "devtools.responsive.touchSimulation.enabled";
const USER_AGENT = "devtools.responsive.userAgent";
const INITIAL_UI = {
// The pixel ratio of the display.
displayPixelRatio: 0,
// Whether or not the viewports are left aligned.
leftAlignmentEnabled: Services.prefs.getBoolPref(LEFT_ALIGNMENT_ENABLED),
leftAlignmentEnabled: Services.prefs.getBoolPref(LEFT_ALIGNMENT_ENABLED, false),
// Whether or not to reload when touch simulation is toggled.
reloadOnTouchSimulation: Services.prefs.getBoolPref(RELOAD_ON_TOUCH_SIMULATION),
reloadOnTouchSimulation: Services.prefs.getBoolPref(RELOAD_ON_TOUCH_SIMULATION, false),
// Whether or not to reload when user agent is changed.
reloadOnUserAgent: Services.prefs.getBoolPref(RELOAD_ON_USER_AGENT),
reloadOnUserAgent: Services.prefs.getBoolPref(RELOAD_ON_USER_AGENT, false),
// Whether or not to show the user agent input in the toolbar.
showUserAgentInput: Services.prefs.getBoolPref(SHOW_USER_AGENT_INPUT),
showUserAgentInput: Services.prefs.getBoolPref(SHOW_USER_AGENT_INPUT, false),
// Whether or not touch simulation is enabled.
touchSimulationEnabled: false,
touchSimulationEnabled: Services.prefs.getBoolPref(TOUCH_SIMULATION_ENABLED, false),
// The user agent of the viewport.
userAgent: "",
userAgent: Services.prefs.getCharPref(USER_AGENT, ""),
};
const reducers = {
@ -48,6 +50,8 @@ const reducers = {
},
[CHANGE_USER_AGENT](ui, { userAgent }) {
Services.prefs.setCharPref(USER_AGENT, userAgent);
return {
...ui,
userAgent,
@ -91,6 +95,8 @@ const reducers = {
},
[TOGGLE_TOUCH_SIMULATION](ui, { enabled }) {
Services.prefs.setBoolPref(TOUCH_SIMULATION_ENABLED, enabled);
return {
...ui,
touchSimulationEnabled: enabled,

Просмотреть файл

@ -4,6 +4,8 @@
"use strict";
const Services = require("Services");
const {
ADD_VIEWPORT,
CHANGE_DEVICE,
@ -13,6 +15,10 @@ const {
ROTATE_VIEWPORT,
} = require("../actions/index");
const VIEWPORT_WIDTH_PREF = "devtools.responsive.viewport.width";
const VIEWPORT_HEIGHT_PREF = "devtools.responsive.viewport.height";
const VIEWPORT_PIXEL_RATIO_PREF = "devtools.responsive.viewport.pixelRatio";
let nextViewportId = 0;
const INITIAL_VIEWPORTS = [];
@ -20,9 +26,9 @@ const INITIAL_VIEWPORT = {
id: nextViewportId++,
device: "",
deviceType: "",
height: 480,
width: 320,
pixelRatio: 0,
height: Services.prefs.getIntPref(VIEWPORT_HEIGHT_PREF, 480),
width: Services.prefs.getIntPref(VIEWPORT_WIDTH_PREF, 320),
pixelRatio: Services.prefs.getIntPref(VIEWPORT_PIXEL_RATIO_PREF, 0),
userContextId: 0,
};
@ -63,6 +69,8 @@ const reducers = {
return viewport;
}
Services.prefs.setIntPref(VIEWPORT_PIXEL_RATIO_PREF, pixelRatio);
return {
...viewport,
pixelRatio,
@ -98,6 +106,9 @@ const reducers = {
width = viewport.width;
}
Services.prefs.setIntPref(VIEWPORT_WIDTH_PREF, width);
Services.prefs.setIntPref(VIEWPORT_HEIGHT_PREF, height);
return {
...viewport,
height,

Просмотреть файл

@ -52,6 +52,7 @@ tags = devtools geolocation
skip-if = true # Bug 1413765
[browser_preloaded_newtab.js]
[browser_screenshot_button.js]
[browser_state_restore.js]
[browser_tab_close.js]
[browser_tab_remoteness_change.js]
[browser_target_blank.js]

Просмотреть файл

@ -0,0 +1,56 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// Test that the previous viewport size, user agent, ddppx and touch simulation properties
// are restored when reopening RDM.
const TEST_URL = "data:text/html;charset=utf-8,";
const DEFAULT_DPPX = window.devicePixelRatio;
const NEW_DPPX = DEFAULT_DPPX + 1;
const NEW_USER_AGENT = "Mozilla/5.0 (Mobile; rv:39.0) Gecko/39.0 Firefox/39.0";
addRDMTask(TEST_URL, async function({ ui, manager }) {
const { store } = ui.toolWindow;
reloadOnTouchChange(true);
reloadOnUAChange(true);
await waitUntilState(store, state => state.viewports.length == 1);
info("Checking the default RDM state.");
testViewportDeviceMenuLabel(ui, "Responsive");
testViewportDimensions(ui, 320, 480);
await testUserAgent(ui, DEFAULT_UA);
await testDevicePixelRatio(ui, DEFAULT_DPPX);
await testTouchEventsOverride(ui, false);
info("Changing the RDM size, dppx, ua and toggle ON touch simulations.");
await setViewportSize(ui, manager, 90, 500);
await selectDevicePixelRatio(ui, NEW_DPPX);
await toggleTouchSimulation(ui);
await changeUserAgentInput(ui, NEW_USER_AGENT);
reloadOnTouchChange(false);
reloadOnUAChange(false);
});
addRDMTask(TEST_URL, async function({ ui }) {
const { store } = ui.toolWindow;
reloadOnTouchChange(true);
reloadOnUAChange(true);
info("Reopening RDM and checking that the previous device state is restored.");
await waitUntilState(store, state => state.viewports.length == 1);
testViewportDimensions(ui, 90, 500);
await testUserAgent(ui, NEW_USER_AGENT);
await testDevicePixelRatio(ui, NEW_DPPX);
await testTouchEventsOverride(ui, true);
reloadOnTouchChange(false);
reloadOnUAChange(false);
});

Просмотреть файл

@ -65,6 +65,11 @@ registerCleanupFunction(async () => {
Services.prefs.clearUserPref("devtools.responsive.reloadConditions.userAgent");
Services.prefs.clearUserPref("devtools.responsive.show-setting-tooltip");
Services.prefs.clearUserPref("devtools.responsive.showUserAgentInput");
Services.prefs.clearUserPref("devtools.responsive.touchSimulation.enabled");
Services.prefs.clearUserPref("devtools.responsive.userAgent");
Services.prefs.clearUserPref("devtools.responsive.viewport.height");
Services.prefs.clearUserPref("devtools.responsive.viewport.pixelRatio");
Services.prefs.clearUserPref("devtools.responsive.viewport.width");
await asyncStorage.removeItem("devtools.devices.url_cache");
await asyncStorage.removeItem("devtools.responsive.deviceState");
await removeLocalDevices();