-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathgetViewPort.js
76 lines (71 loc) · 2.04 KB
/
getViewPort.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
import { getLogger } from '@sitespeed.io/log';
import { isAndroidConfigured } from '../android/index.js';
import { getProperty } from '../support/util.js';
const log = getLogger('browsertime');
// Most use screen size as of 2018
// https://www.w3schools.com/browsers/browsers_display.asp
const DEFAULT_VIEWPORT = '1366x768';
// Keep numbers even to work with xvfb
const sizeMap = {
'iPhone 5': '320x568',
'iPhone 6': '376x668',
'iPhone 7': '376x668',
'iPhone 8': '376x668',
'iPhone 6 Plus': '414x736',
'iPhone 7 Plus': '414x736',
'iPhone 8 Plus': '414x736',
'iPhone X': '376x812',
'Galaxy S5': '360x640',
'Moto G4': '360x640',
'Pixel 2': '412x732',
'Pixel 2 XL': '412x824',
iPad: '768x1024',
'iPad Mini': '768x1024',
'iPad Air': '820x1180',
'iPad Pro': '1024x1366',
'iPhone XR': '414x896',
'iPhone SE': '377x668',
'iPhone 12 Pro': '390x844',
'iPhone 14 Pro': '430x932',
'iPhone 14 Pro Max': '430x932',
'Pixel 5': '394x852',
'Pixel 7': '412x916',
'Samsung Galaxy S8+': '412x846',
'Samsung Galaxy A51/71': '412x914'
};
export function getViewPort(options) {
// you cannot set the width/height for phone so just keep the viewport undefined
if (isAndroidConfigured(options)) {
return;
}
const emulatedWidth = getProperty(options, 'chrome.mobileEmulation.width');
const emulatedHeight = getProperty(options, 'chrome.mobileEmulation.height');
if (emulatedWidth && emulatedHeight) {
return `${emulatedWidth}x${emulatedHeight}`;
}
const deviceName = getProperty(
options,
'chrome.mobileEmulation.deviceName',
false
);
if (deviceName) {
// TODO if it don't map ...
const viewPort = sizeMap[deviceName];
if (viewPort) {
return viewPort;
} else {
log.error(
'No matching viewPort for device %s. Will use default viewport',
deviceName
);
return DEFAULT_VIEWPORT;
}
}
if (
/^\d+x\d+$/.test(options.viewPort) ||
(options.viewPort === 'maximize' && !options.xvfb)
) {
return options.viewPort;
}
return DEFAULT_VIEWPORT;
}