summaryrefslogtreecommitdiffstats
path: root/dom/events/test/test_deviceSensor.html
blob: f4ce6582989697b277ac44d512d7f8e0598ec122 (plain)
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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=402089
-->
<head>
  <title>Test for Bug 742376</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>

<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=742376">Mozilla Bug 742376</a>
<script class="testbody" type="text/javascript">

/** Test for Bug 742376 **/
let Cc = SpecialPowers.Cc;
let Ci = SpecialPowers.Ci;
let dss = Cc["@mozilla.org/devicesensors;1"].getService(Ci.nsIDeviceSensors);

function hasLightListeners() {
  return dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_LIGHT, window);
}

function hasOrientationListeners() {
  return dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_ORIENTATION, window) ||
         dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_ROTATION_VECTOR, window) ||
         dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_GAME_ROTATION_VECTOR, window);
}

function hasProximityListeners() {
  return dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_PROXIMITY, window);
}

function hasMotionListeners() {
  return dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_ACCELERATION, window) ||
         dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_LINEAR_ACCELERATION, window) ||
         dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_GYROSCOPE, window);
}

async function test_event_presence(prefName, eventCheck, eventName) {
  function dumbListener(event) {}
  function dumbListener2(event) {}
  function dumbListener3(event) {}

  await SpecialPowers.pushPrefEnv({"set": [
    [prefName, true]
  ]});

  is(eventCheck(), false, "Must not have listeners before tests start");

  window.addEventListener(eventName, dumbListener);
  window.addEventListener("random_event_name", function() {});
  window.addEventListener(eventName, dumbListener2);

  is(eventCheck(), true, `Should have listeners when ${eventName} sensor is enabled`);

  window.removeEventListener(eventName, dumbListener);
  window.removeEventListener(eventName, dumbListener2);

  is(eventCheck(), false, "Must not have listeners when removed");

  await SpecialPowers.pushPrefEnv({"set": [
    [prefName, false]
  ]});

  window.addEventListener(eventName, dumbListener);
  window.addEventListener("random_event_name", function() {});
  window.addEventListener(eventName, dumbListener2);

  is(eventCheck(), false, "Must not have listeners when sensor is disabled");
}

async function start() {
  await SpecialPowers.pushPrefEnv({"set": [
    ["device.sensors.enabled", true],
    ["device.sensors.orientation.enabled", true]
  ]});

  is(hasOrientationListeners(), false, "Must not have listeners before tests start");

  function dumbListener(event) {}
  function dumbListener2(event) {}
  function dumbListener3(event) {}

  window.addEventListener("deviceorientation", dumbListener);
  window.addEventListener("random_event_name", function() {});
  window.addEventListener("deviceorientation", dumbListener2);

  is(hasOrientationListeners(), true, "Listeners should have been added");

  await new Promise(resolve => {
    window.setTimeout(function() {
      window.removeEventListener("deviceorientation", dumbListener);
      is(hasOrientationListeners(), true, "Only some listeners should have been removed");
      window.setTimeout(function() {
        window.removeEventListener("deviceorientation", dumbListener2);
        window.setTimeout(function() {
          is(hasOrientationListeners(), false, "Listeners should have been removed");
          resolve();
        }, 0);
      }, 0);
    }, 0);
  });

  await new Promise(resolve => {
    window.ondeviceorientation = function() {}
    window.setTimeout(function() {
      is(hasOrientationListeners(), true, "Handler should have been added");
      window.ondeviceorientation = null;
      window.setTimeout(function() {
        is(hasOrientationListeners(), false, "Handler should have been removed");
        resolve();
      }, 0);
    }, 0);
  });

  await test_event_presence("device.sensors.ambientLight.enabled", hasLightListeners, "devicelight");
  await test_event_presence("device.sensors.proximity.enabled", hasProximityListeners, "deviceproximity");
  await test_event_presence("device.sensors.motion.enabled", hasMotionListeners, "devicemotion");
  await test_event_presence("device.sensors.orientation.enabled", hasOrientationListeners, "deviceorientation");

  SimpleTest.finish();

}

SimpleTest.waitForExplicitFinish();

start();

</script>
</pre>
</body>
</html>