1 function setMetaViewport()
5 "w=dw": "width=device-width",
7 "none": "no viewport (desktop site)"
10 var viewport = VIEWPORTS["none"];
11 for (var key in VIEWPORTS) {
12 if (location.search == "?" + key) {
13 viewport = VIEWPORTS[key];
17 if (viewport != VIEWPORTS["none"])
18 document.write('<meta name="viewport" content="'+viewport+'">');
21 // matchMedia() polyfill from https://github.com/paulirish/matchMedia.js
22 // Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas. Dual MIT/BSD license
23 window.matchMedia = window.matchMedia || (function(doc, undefined){
25 docElem = doc.documentElement,
26 refNode = docElem.firstElementChild || docElem.firstChild,
27 // fakeBody required for <FF4 when executed in <head>
28 fakeBody = doc.createElement('body'),
29 div = doc.createElement('div');
32 div.style.cssText = "position:absolute;top:-100em";
33 fakeBody.style.background = "none";
34 fakeBody.appendChild(div);
37 div.innerHTML = '­<style media="'+q+'"> #mq-test-1 { width: 42px; }</style>';
39 docElem.insertBefore(fakeBody, refNode);
40 bool = div.offsetWidth == 42;
41 docElem.removeChild(fakeBody);
43 return { matches: bool, media: q };
49 function dumpMetrics()
52 writeResult("Device:", "");
53 testJS("window.screenX");
54 testJS("window.screenY");
56 writeResult("Viewport:", location.search);
59 testJS("window.orientation", "");
61 testMQDimension("resolution", null, "dpi");
62 testMQDevicePixelRatio(window.devicePixelRatio);
63 testJS("window.devicePixelRatio", "");
65 writeResult("Widths:", "");
67 testMQDimension("device-width", screen.width);
68 testJS("screen.width");
69 testJS("screen.availWidth");
70 testJS("window.outerWidth");
71 testJS("window.innerWidth");
72 testMQDimension("width", document.documentElement.clientWidth);
73 testJS("document.documentElement.clientWidth");
74 testJS("document.documentElement.offsetWidth");
75 testJS("document.documentElement.scrollWidth");
76 testJS("document.body.clientWidth");
77 testJS("document.body.offsetWidth");
78 testJS("document.body.scrollWidth");
80 writeResult("Heights:", "");
82 testMQDimension("device-height", screen.height);
83 testJS("screen.height");
84 testJS("screen.availHeight");
85 testJS("window.outerHeight");
86 testJS("window.innerHeight");
87 testMQDimension("height", document.documentElement.clientHeight);
88 testJS("document.documentElement.clientHeight");
89 testJS("document.documentElement.offsetHeight");
90 testJS("document.documentElement.scrollHeight");
91 testJS("document.body.clientHeight");
92 testJS("document.body.offsetHeight");
93 testJS("document.body.scrollHeight");
95 return results.join("\n");
98 function testJS(expr, unit)
100 if (unit === undefined)
103 var ans = eval(expr);
104 if (typeof ans == "number")
107 // Shorten long properties to make more readable
108 expr = expr.replace("documentElement", "docElem").replace("document", "doc");
110 writeResult(expr, ans);
113 function testMQOrientation()
115 if (matchMedia("screen and (orientation: portrait)").matches)
116 writeResult("@media orientation", "portrait");
117 else if (matchMedia("screen and (orientation: landscape)").matches)
118 writeResult("@media orientation", "landscape");
120 writeResult("@media orientation", "???");
123 function testMQDevicePixelRatio(guess)
125 // To save time, try the guess value first; otherwise try common values (TODO: binary search).
126 if (matchMedia("screen and (-webkit-device-pixel-ratio: "+guess+"), screen and (device-pixel-ratio: "+guess+")").matches)
127 writeResult("@media device-pixel-ratio", guess);
128 else if (matchMedia("screen and (-webkit-device-pixel-ratio: 2), screen and (device-pixel-ratio: 2)").matches)
129 writeResult("@media device-pixel-ratio", "2");
130 else if (matchMedia("screen and (-webkit-device-pixel-ratio: 1.5), screen and (device-pixel-ratio: 1.5)").matches)
131 writeResult("@media device-pixel-ratio", "1.5");
132 else if (matchMedia("screen and (-webkit-device-pixel-ratio: 1), screen and (device-pixel-ratio: 1)").matches)
133 writeResult("@media device-pixel-ratio", "1");
134 else if (matchMedia("screen and (-webkit-device-pixel-ratio: 2.25), screen and (device-pixel-ratio: 2.25)").matches)
135 writeResult("@media device-pixel-ratio", "2.25");
137 writeResult("@media device-pixel-ratio", "???");
140 function testMQDimension(feature, guess, unit)
143 // To save time, try the guess value first; otherwise binary search.
144 if (guess && matchMedia("screen and (" + feature + ":" + guess + unit + ")").matches) {
145 writeResult("@media " + feature, guess + unit);
147 var val = mqBinarySearch(feature, 1, 2560, unit);
148 writeResult("@media " + feature, val ? val + unit : "???");
152 // Searches the inclusive range [minValue, maxValue].
153 function mqBinarySearch(feature, minValue, maxValue, unit)
155 if (minValue == maxValue) {
156 if (matchMedia("screen and (" + feature + ":" + minValue + unit + ")").matches)
161 var mid = Math.ceil((minValue + maxValue) / 2);
162 if (matchMedia("screen and (min-" + feature + ":" + mid + unit + ")").matches)
163 return mqBinarySearch(feature, mid, maxValue, unit); // feature is >= mid
165 return mqBinarySearch(feature, minValue, mid - 1, unit); // feature is < mid
168 function writeResult(key, val)
170 results.push(key + (val ? " = " + val : ""));
173 var initialize_DeviceEmulationTest = function() {
175 InspectorTest.getPageMetrics = function(callback)
177 InspectorTest.evaluateInPage("dumpMetrics()", callback);
180 InspectorTest.applyEmulationAndReload = function(width, height, deviceScaleFactor, viewport, callback)
182 InspectorTest.addSniffer(WebInspector.overridesSupport, "_deviceMetricsOverrideAppliedForTest", emulateCallback);
183 WebInspector.overridesSupport.emulateDevice(width + "x" + height + "x" + deviceScaleFactor + "x0x0", "");
185 function emulateCallback()
187 var warning = WebInspector.overridesSupport.warningMessage();
189 InspectorTest._deviceEmulationResults.push("Emulation warning: " + warning);
190 InspectorTest.navigate(InspectorTest._deviceEmulationPageUrl + "?" + viewport, callback);
194 InspectorTest.emulateAndGetMetrics = function(width, height, deviceScaleFactor, viewport, callback)
196 InspectorTest._deviceEmulationResults.push("Emulating device: " + width + "x" + height + "x" + deviceScaleFactor + " viewport='" + viewport + "'");
197 InspectorTest.applyEmulationAndReload(width, height, deviceScaleFactor, viewport, InspectorTest.getPageMetrics.bind(InspectorTest, printMetrics));
199 function printMetrics(metrics)
201 InspectorTest._deviceEmulationResults.push(metrics.value + "\n");
206 InspectorTest.testDeviceEmulation = function(pageUrl, width, height, deviceScaleFactor, viewport)
208 InspectorTest._deviceEmulationPageUrl = pageUrl;
209 InspectorTest._deviceEmulationResults = [];
210 InspectorTest.emulateAndGetMetrics(width, height, deviceScaleFactor, viewport, callback);
214 InspectorTest.addResult(InspectorTest._deviceEmulationResults.join("\n"));
215 InspectorTest.completeTest();