Convert DevicesDescriptors into puppeteer format

This patch converts lib/DevicesDescriptors from a devtools front-end
format into a puppeteer format.

This patch does this via introducing a scripts utils/fetch_devices.js
which grabs devices from upstream of DevTools Front-end and
converts them into puppeteer devices.

References #88.
This commit is contained in:
Andrey Lushnikov
2017-07-20 16:43:54 -07:00
parent ffc5a8ae4f
commit 76ac3bded5
5 changed files with 764 additions and 1151 deletions

View File

@@ -21,7 +21,7 @@ class EmulationManager {
* @return {!Promise<!Array<string>>}
*/
static deviceNames() {
return Promise.resolve(DeviceDescriptors.map(entry => entry['device'].title));
return Promise.resolve(DeviceDescriptors.map(device => device.name));
}
/**
@@ -29,31 +29,20 @@ class EmulationManager {
* @param {!Object=} options
* @return {!Page.Viewport}
*/
static deviceViewport(name, options) {
options = options || {};
const descriptor = DeviceDescriptors.find(entry => entry['device'].title === name)['device'];
if (!descriptor)
static deviceViewport(name) {
const device = DeviceDescriptors.find(device => device.name === name);
if (!device)
throw new Error(`Unable to emulate ${name}, no such device metrics in the library.`);
const device = EmulationManager.loadFromJSONV1(descriptor);
const viewport = options.landscape ? device.horizontal : device.vertical;
return {
width: viewport.width,
height: viewport.height,
deviceScaleFactor: device.deviceScaleFactor,
isMobile: device.capabilities.includes('mobile'),
hasTouch: device.capabilities.includes('touch'),
isLandscape: options.landscape || false
};
return device.viewport;
}
/**
* @param {string} name
*/
static deviceUserAgent(name, options) {
const descriptor = DeviceDescriptors.find(entry => entry['device'].title === name)['device'];
if (!descriptor)
static deviceUserAgent(name) {
const device = DeviceDescriptors.find(device => device.name === name);
if (!device)
throw new Error(`Unable to emulate ${name}, no such device metrics in the library.`);
const device = EmulationManager.loadFromJSONV1(descriptor);
return device.userAgent;
}
@@ -110,86 +99,6 @@ class EmulationManager {
}
return reloadNeeded;
}
/**
* @param {*} json
* @return {?Object}
*/
static loadFromJSONV1(json) {
/**
* @param {*} object
* @param {string} key
* @param {string} type
* @param {*=} defaultValue
* @return {*}
*/
function parseValue(object, key, type, defaultValue) {
if (typeof object !== 'object' || object === null || !object.hasOwnProperty(key)) {
if (typeof defaultValue !== 'undefined')
return defaultValue;
throw new Error('Emulated device is missing required property \'' + key + '\'');
}
const value = object[key];
if (typeof value !== type || value === null)
throw new Error('Emulated device property \'' + key + '\' has wrong type \'' + typeof value + '\'');
return value;
}
/**
* @param {*} object
* @param {string} key
* @return {number}
*/
function parseIntValue(object, key) {
const value = /** @type {number} */ (parseValue(object, key, 'number'));
if (value !== Math.abs(value))
throw new Error('Emulated device value \'' + key + '\' must be integer');
return value;
}
/**
* @param {*} json
* @return {!{width: number, height: number}}
*/
function parseOrientation(json) {
const result = {};
const minDeviceSize = 50;
const maxDeviceSize = 9999;
result.width = parseIntValue(json, 'width');
if (result.width < 0 || result.width > maxDeviceSize ||
result.width < minDeviceSize)
throw new Error('Emulated device has wrong width: ' + result.width);
result.height = parseIntValue(json, 'height');
if (result.height < 0 || result.height > maxDeviceSize ||
result.height < minDeviceSize)
throw new Error('Emulated device has wrong height: ' + result.height);
return /** @type {!{width: number, height: number}} */ (result);
}
const result = {};
result.type = /** @type {string} */ (parseValue(json, 'type', 'string'));
result.userAgent = /** @type {string} */ (parseValue(json, 'user-agent', 'string'));
const capabilities = parseValue(json, 'capabilities', 'object', []);
if (!Array.isArray(capabilities))
throw new Error('Emulated device capabilities must be an array');
result.capabilities = [];
for (let i = 0; i < capabilities.length; ++i) {
if (typeof capabilities[i] !== 'string')
throw new Error('Emulated device capability must be a string');
result.capabilities.push(capabilities[i]);
}
result.deviceScaleFactor = /** @type {number} */ (parseValue(json['screen'], 'device-pixel-ratio', 'number'));
if (result.deviceScaleFactor < 0 || result.deviceScaleFactor > 100)
throw new Error('Emulated device has wrong deviceScaleFactor: ' + result.deviceScaleFactor);
result.vertical = parseOrientation(parseValue(json['screen'], 'vertical', 'object'));
result.horizontal = parseOrientation(parseValue(json['screen'], 'horizontal', 'object'));
return result;
}
}
EmulationManager._touchScriptId = Symbol('emulatingTouchScriptId');