test_deviceSensor.html (4626B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=402089 5 --> 6 <head> 7 <title>Test for Bug 742376</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <script src="/tests/SimpleTest/EventUtils.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 11 </head> 12 13 <body> 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=742376">Mozilla Bug 742376</a> 15 <script class="testbody" type="text/javascript"> 16 17 /** Test for Bug 742376 */ 18 let Cc = SpecialPowers.Cc; 19 let Ci = SpecialPowers.Ci; 20 let dss = Cc["@mozilla.org/devicesensors;1"].getService(Ci.nsIDeviceSensors); 21 22 function hasLightListeners() { 23 return dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_LIGHT, window); 24 } 25 26 function hasOrientationListeners() { 27 return dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_ORIENTATION, window) || 28 dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_ROTATION_VECTOR, window) || 29 dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_GAME_ROTATION_VECTOR, window); 30 } 31 32 function hasProximityListeners() { 33 return dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_PROXIMITY, window); 34 } 35 36 function hasMotionListeners() { 37 return dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_ACCELERATION, window) || 38 dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_LINEAR_ACCELERATION, window) || 39 dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_GYROSCOPE, window); 40 } 41 42 async function test_event_presence(prefName, eventCheck, eventName) { 43 function dumbListener(event) {} 44 function dumbListener2(event) {} 45 function dumbListener3(event) {} 46 47 await SpecialPowers.pushPrefEnv({"set": [ 48 [prefName, true] 49 ]}); 50 51 is(eventCheck(), false, "Must not have listeners before tests start"); 52 53 window.addEventListener(eventName, dumbListener); 54 window.addEventListener("random_event_name", function() {}); 55 window.addEventListener(eventName, dumbListener2); 56 57 is(eventCheck(), true, `Should have listeners when ${eventName} sensor is enabled`); 58 59 window.removeEventListener(eventName, dumbListener); 60 window.removeEventListener(eventName, dumbListener2); 61 62 is(eventCheck(), false, "Must not have listeners when removed"); 63 64 await SpecialPowers.pushPrefEnv({"set": [ 65 [prefName, false] 66 ]}); 67 68 window.addEventListener(eventName, dumbListener); 69 window.addEventListener("random_event_name", function() {}); 70 window.addEventListener(eventName, dumbListener2); 71 72 is(eventCheck(), false, "Must not have listeners when sensor is disabled"); 73 } 74 75 async function start() { 76 await SpecialPowers.pushPrefEnv({"set": [ 77 ["device.sensors.enabled", true], 78 ["device.sensors.orientation.enabled", true] 79 ]}); 80 81 is(hasOrientationListeners(), false, "Must not have listeners before tests start"); 82 83 function dumbListener(event) {} 84 function dumbListener2(event) {} 85 function dumbListener3(event) {} 86 87 window.addEventListener("deviceorientation", dumbListener); 88 window.addEventListener("random_event_name", function() {}); 89 window.addEventListener("deviceorientation", dumbListener2); 90 91 is(hasOrientationListeners(), true, "Listeners should have been added"); 92 93 await new Promise(resolve => { 94 window.setTimeout(function() { 95 window.removeEventListener("deviceorientation", dumbListener); 96 is(hasOrientationListeners(), true, "Only some listeners should have been removed"); 97 window.setTimeout(function() { 98 window.removeEventListener("deviceorientation", dumbListener2); 99 window.setTimeout(function() { 100 is(hasOrientationListeners(), false, "Listeners should have been removed"); 101 resolve(); 102 }, 0); 103 }, 0); 104 }, 0); 105 }); 106 107 await new Promise(resolve => { 108 window.ondeviceorientation = function() {} 109 window.setTimeout(function() { 110 is(hasOrientationListeners(), true, "Handler should have been added"); 111 window.ondeviceorientation = null; 112 window.setTimeout(function() { 113 is(hasOrientationListeners(), false, "Handler should have been removed"); 114 resolve(); 115 }, 0); 116 }, 0); 117 }); 118 119 await test_event_presence("device.sensors.ambientLight.enabled", hasLightListeners, "devicelight"); 120 await test_event_presence("device.sensors.proximity.enabled", hasProximityListeners, "userproximity"); 121 await test_event_presence("device.sensors.motion.enabled", hasMotionListeners, "devicemotion"); 122 await test_event_presence("device.sensors.orientation.enabled", hasOrientationListeners, "deviceorientation"); 123 124 SimpleTest.finish(); 125 126 } 127 128 SimpleTest.waitForExplicitFinish(); 129 130 start(); 131 132 </script> 133 </pre> 134 </body> 135 </html>