[iOS] Suspend and resume device motion and device orientation updates when page is...
[WebKit-https.git] / ManualTests / ios / resources / suspend-orientation-and-motion-events-when-page-becomes-hidden.js
1 var eventFrequencyTable;
2
3 function resetTest()
4 {
5     eventFrequencyTable = {};
6 }
7
8 function log(message)
9 {
10     document.getElementById("console").appendChild(document.createTextNode(message + "\n"));
11 }
12
13 function checkEvent(event)
14 {
15     if (document.visibilityState === "visible")
16         return;
17     var type = event.type;
18     if (!eventFrequencyTable[type])
19         eventFrequencyTable[type] = 0;
20     ++eventFrequencyTable[type];
21 }
22
23 function handleVisibilityChange()
24 {
25     if (document.visibilityState === "hidden")
26         return;
27     var receivedEventsMessageParts = [];
28     for (var type in eventFrequencyTable)
29         receivedEventsMessageParts.push(type + " (\u00D7 " + eventFrequencyTable[type] + ")");
30     if (receivedEventsMessageParts.length)
31         log("Expected to receive no events when the page was hidden, but received: " + receivedEventsMessageParts.join(", ") + ".");
32     else
33         log("Received no events when the page was hidden.");
34     resetTest();
35 }
36
37 resetTest();
38
39 window.addEventListener("devicemotion", checkEvent, false);
40 window.addEventListener("deviceorientation", checkEvent, false);
41 document.addEventListener("visibilitychange", handleVisibilityChange, false);