From c013a531cfdd93da8f9b4f294498c6255c9927ca Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Sat, 29 Jul 2017 19:12:17 -0700 Subject: [PATCH] Refactor EmulationManager This patch refactors EmulationManager so that it is a normal class with a state. --- lib/EmulationManager.js | 40 +++++++++++++++++++++------------------- lib/Page.js | 28 +++++++++------------------- 2 files changed, 30 insertions(+), 38 deletions(-) diff --git a/lib/EmulationManager.js b/lib/EmulationManager.js index 9c851ecc97d..d5763770c63 100644 --- a/lib/EmulationManager.js +++ b/lib/EmulationManager.js @@ -17,41 +17,47 @@ class EmulationManager { /** * @param {!Connection} client + */ + constructor(client) { + this._client = client; + this._emulatingMobile = false; + this._injectedTouchScriptId = null; + } + + /** * @param {!Page.Viewport} viewport * @return {Promise} */ - static async emulateViewport(client, viewport) { + async emulateViewport(client, viewport) { const mobile = viewport.isMobile || false; - const landscape = viewport.isLandscape || false; const width = viewport.width; const height = viewport.height; const deviceScaleFactor = viewport.deviceScaleFactor || 1; - const screenOrientation = landscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' }; + const screenOrientation = viewport.isLandscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' }; await Promise.all([ - client.send('Emulation.setDeviceMetricsOverride', { mobile, width, height, deviceScaleFactor, screenOrientation }), - client.send('Emulation.setTouchEmulationEnabled', { + this._client.send('Emulation.setDeviceMetricsOverride', { mobile, width, height, deviceScaleFactor, screenOrientation }), + this._client.send('Emulation.setTouchEmulationEnabled', { enabled: viewport.hasTouch || false, configuration: viewport.isMobile ? 'mobile' : 'desktop' }) ]); let reloadNeeded = false; - if (viewport.hasTouch && !client[EmulationManager._touchScriptId]) { + if (viewport.hasTouch && !this._injectedTouchScriptId) { const source = `(${injectedTouchEventsFunction})()`; - client[EmulationManager._touchScriptId] = (await client.send('Page.addScriptToEvaluateOnNewDocument', { source })).identifier; + this._injectedTouchScriptId = (await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source })).identifier; + reloadNeeded = true; + } else if (!viewport.hasTouch && this._injectedTouchScriptId) { + await this._client.send('Page.removeScriptToEvaluateOnNewDocument', {identifier: this._injectedTouchScriptId}); + this._injectedTouchScriptId = null; reloadNeeded = true; } - if (!viewport.hasTouch && client[EmulationManager._touchScriptId]) { - await client.send('Page.removeScriptToEvaluateOnNewDocument', {identifier: client[EmulationManager._touchScriptId]}); - client[EmulationManager._touchScriptId] = null; + if (this._emulatingMobile !== mobile) reloadNeeded = true; - } - - if (client[EmulationManager._emulatingMobile] !== mobile) - reloadNeeded = true; - client[EmulationManager._emulatingMobile] = mobile; + this._emulatingMobile = mobile; + return reloadNeeded; function injectedTouchEventsFunction() { const touchEvents = ['ontouchstart', 'ontouchend', 'ontouchmove', 'ontouchcancel']; @@ -66,11 +72,7 @@ class EmulationManager { } } } - return reloadNeeded; } } -EmulationManager._touchScriptId = Symbol('emulatingTouchScriptId'); -EmulationManager._emulatingMobile = Symbol('emulatingMobile'); - module.exports = EmulationManager; diff --git a/lib/Page.js b/lib/Page.js index 708df3f1b35..660bb6c8304 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -38,36 +38,28 @@ class Page extends EventEmitter { client.send('Runtime.enable', {}), client.send('Security.enable', {}), ]); - const keyboard = new Keyboard(client); - const mouse = new Mouse(client, keyboard); - const frameManager = new FrameManager(client, mouse); - const networkManager = new NetworkManager(client); - const page = new Page(client, frameManager, networkManager, screenshotTaskQueue, mouse, keyboard); + const page = new Page(client, screenshotTaskQueue); + await page.navigate('about:blank'); // Initialize default page size. await page.setViewport({width: 400, height: 300}); - await page.navigate('about:blank'); return page; } /** * @param {!Connection} client - * @param {!FrameManager} frameManager - * @param {!NetworkManager} networkManager * @param {!TaskQueue} screenshotTaskQueue - * @param {!Mouse} mouse - * @param {!Keyboard} keyboard */ - constructor(client, frameManager, networkManager, screenshotTaskQueue, mouse, keyboard) { + constructor(client, screenshotTaskQueue) { super(); this._client = client; - this._frameManager = frameManager; - this._networkManager = networkManager; + this._keyboard = new Keyboard(client); + this._mouse = new Mouse(client, this._keyboard); + this._frameManager = new FrameManager(client, this._mouse); + this._networkManager = new NetworkManager(client); + this._emulationManager = new EmulationManager(client); /** @type {!Map} */ this._inPageCallbacks = new Map(); - this._keyboard = keyboard; - this._mouse = mouse; - this._screenshotTaskQueue = screenshotTaskQueue; this._frameManager.on(FrameManager.Events.FrameAttached, event => this.emit(Page.Events.FrameAttached, event)); @@ -274,8 +266,6 @@ class Page extends EventEmitter { * @return {!Promise} */ async reload(options) { - if (!this.mainFrame()) - return; this._client.send('Page.reload'); return this.waitForNavigation(options); } @@ -330,7 +320,7 @@ class Page extends EventEmitter { * @return {!Promise} */ async setViewport(viewport) { - const needsReload = await EmulationManager.emulateViewport(this._client, viewport); + const needsReload = await this._emulationManager.emulateViewport(this._client, viewport); this._viewport = viewport; if (needsReload) await this.reload();