-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathemulation.js
173 lines (156 loc) · 5.77 KB
/
emulation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* @license Copyright 2016 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
/**
* Nexus 5X metrics adapted from emulated_devices/module.json
*/
const NEXUS5X_EMULATION_METRICS = {
mobile: true,
screenWidth: 412,
screenHeight: 732,
width: 412,
height: 732,
positionX: 0,
positionY: 0,
scale: 1,
deviceScaleFactor: 2.625,
fitWindow: false,
screenOrientation: {
angle: 0,
type: 'portraitPrimary'
}
};
const NEXUS5X_USERAGENT = {
userAgent: 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5 Build/MRA58N) AppleWebKit/537.36' +
'(KHTML, like Gecko) Chrome/61.0.3116.0 Mobile Safari/537.36'
};
/**
* Adjustments needed for DevTools network throttling to simulate
* more realistic network conditions.
* See: crbug.com/721112
*/
const LATENCY_FACTOR = 3.75;
const THROUGHPUT_FACTOR = 0.9;
const TARGET_LATENCY = 150; // 150ms
const TARGET_DOWNLOAD_THROUGHPUT = Math.floor(1.6 * 1024 * 1024 / 8); // 1.6Mbps
const TARGET_UPLOAD_THROUGHPUT = Math.floor(750 * 1024 / 8); // 750Kbps
const TYPICAL_MOBILE_THROTTLING_METRICS = {
targetLatency: TARGET_LATENCY,
latency: TARGET_LATENCY * LATENCY_FACTOR,
targetDownloadThroughput: TARGET_DOWNLOAD_THROUGHPUT,
downloadThroughput: TARGET_DOWNLOAD_THROUGHPUT * THROUGHPUT_FACTOR,
targetUploadThroughput: TARGET_UPLOAD_THROUGHPUT,
uploadThroughput: TARGET_UPLOAD_THROUGHPUT * THROUGHPUT_FACTOR,
offline: false
};
const OFFLINE_METRICS = {
offline: true,
// values of 0 remove any active throttling. crbug.com/456324#c9
latency: 0,
downloadThroughput: 0,
uploadThroughput: 0
};
const NO_THROTTLING_METRICS = {
latency: 0,
downloadThroughput: 0,
uploadThroughput: 0,
offline: false
};
const NO_CPU_THROTTLE_METRICS = {
rate: 1
};
const CPU_THROTTLE_METRICS = {
rate: 4
};
function enableNexus5X(driver) {
// COMPAT FIMXE
// Injecting this function clientside is no longer neccessary as of m62. This is done
// on the backend when `Emulation.setTouchEmulationEnabled` is set.
// https://bugs.chromium.org/p/chromium/issues/detail?id=133915#c63
// Once m62 hits stable (~Oct 20th) we can nuke this entirely
/**
* Finalizes touch emulation by enabling `"ontouchstart" in window` feature detect
* to work. Messy hack, though copied verbatim from DevTools' emulation/TouchModel.js
* where it's been working for years. addScriptToEvaluateOnLoad runs before any of the
* page's JavaScript executes.
*/
/* eslint-disable no-proto */ /* global window, document */ /* istanbul ignore next */
const injectedTouchEventsFunction = function() {
const touchEvents = ['ontouchstart', 'ontouchend', 'ontouchmove', 'ontouchcancel'];
const recepients = [window.__proto__, document.__proto__];
for (let i = 0; i < touchEvents.length; ++i) {
for (let j = 0; j < recepients.length; ++j) {
if (!(touchEvents[i] in recepients[j])) {
Object.defineProperty(recepients[j], touchEvents[i], {
value: null, writable: true, configurable: true, enumerable: true
});
}
}
}
};
/* eslint-enable */
return Promise.all([
driver.sendCommand('Emulation.setDeviceMetricsOverride', NEXUS5X_EMULATION_METRICS),
// required for screenshotting emulated page size, rather than full size
driver.sendCommand('Emulation.setVisibleSize', {
width: NEXUS5X_EMULATION_METRICS.screenWidth,
height: NEXUS5X_EMULATION_METRICS.screenHeight
}),
// Network.enable must be called for UA overriding to work
driver.sendCommand('Network.enable'),
driver.sendCommand('Network.setUserAgentOverride', NEXUS5X_USERAGENT),
driver.sendCommand('Emulation.setTouchEmulationEnabled', {
enabled: true,
configuration: 'mobile'
}),
driver.sendCommand('Page.addScriptToEvaluateOnLoad', {
scriptSource: '(' + injectedTouchEventsFunction.toString() + ')()'
})
]);
}
function enableNetworkThrottling(driver) {
return driver.sendCommand('Network.emulateNetworkConditions', TYPICAL_MOBILE_THROTTLING_METRICS);
}
function disableNetworkThrottling(driver) {
return driver.sendCommand('Network.emulateNetworkConditions', NO_THROTTLING_METRICS);
}
function goOffline(driver) {
return driver.sendCommand('Network.emulateNetworkConditions', OFFLINE_METRICS);
}
function enableCPUThrottling(driver) {
return driver.sendCommand('Emulation.setCPUThrottlingRate', CPU_THROTTLE_METRICS);
}
function disableCPUThrottling(driver) {
return driver.sendCommand('Emulation.setCPUThrottlingRate', NO_CPU_THROTTLE_METRICS);
}
function getEmulationDesc() {
const {latency, downloadThroughput, uploadThroughput} = TYPICAL_MOBILE_THROTTLING_METRICS;
const byteToMbit = bytes => (bytes / 1024 / 1024 * 8).toFixed(1);
return {
'deviceEmulation': 'Nexus 5X',
'cpuThrottling': `${CPU_THROTTLE_METRICS.rate}x slowdown`,
'networkThrottling': `${latency}ms RTT, ${byteToMbit(downloadThroughput)}Mbps down, ` +
`${byteToMbit(uploadThroughput)}Mbps up`
};
}
module.exports = {
enableNexus5X,
enableNetworkThrottling,
disableNetworkThrottling,
enableCPUThrottling,
disableCPUThrottling,
goOffline,
getEmulationDesc,
settings: {
NEXUS5X_EMULATION_METRICS,
NEXUS5X_USERAGENT,
TYPICAL_MOBILE_THROTTLING_METRICS,
OFFLINE_METRICS,
NO_THROTTLING_METRICS,
NO_CPU_THROTTLE_METRICS,
CPU_THROTTLE_METRICS
}
};