| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <script src="../resources/testharness.js"></script> | 2 <script src="../resources/testharness.js"></script> |
| 3 <script src="../resources/testharnessreport.js"></script> | 3 <script src="../resources/testharnessreport.js"></script> |
| 4 <script src="../resources/mojo-helpers.js"></script> | 4 <script src="../resources/mojo-helpers.js"></script> |
| 5 <script src="resources/sensor-helpers.js"></script> | 5 <script src="resources/sensor-helpers.js"></script> |
| 6 <script src="resources/generic-sensor-tests.js"></script> |
| 6 <script> | 7 <script> |
| 7 | 8 |
| 8 'use strict'; | 9 'use strict'; |
| 9 | 10 |
| 10 if (!window.testRunner) | 11 if (!window.testRunner) |
| 11 debug('This test cannot be run without the TestRunner'); | 12 debug('This test cannot be run without the TestRunner'); |
| 12 | 13 |
| 13 const kDefaultReadingValue = 3.1415; | 14 const kDefaultReadingValue = 3.1415; |
| 14 | 15 |
| 15 function update_sensor_reading(buffer) { | 16 function update_sensor_reading(buffer) { |
| 16 buffer[1] = window.performance.now(); | 17 buffer[1] = window.performance.now(); |
| 17 buffer[2] = kDefaultReadingValue; | 18 buffer[2] = kDefaultReadingValue; |
| 18 } | 19 } |
| 19 | 20 |
| 20 test(() => assert_throws( | 21 function verify_sensor_reading(reading) { |
| 21 new RangeError(), | 22 return reading.illuminance == kDefaultReadingValue; |
| 22 () => new AmbientLightSensor({frequency: -60})), | 23 } |
| 23 'Test that negative frequency causes exception from constructor.'); | |
| 24 | 24 |
| 25 sensor_test(sensor => { | 25 runGenericSensorTests(AmbientLightSensor, update_sensor_reading, verify_sensor_r
eading); |
| 26 sensor.mockSensorProvider.setGetSensorShouldFail(true); | |
| 27 let ambientLightSensor = new AmbientLightSensor(); | |
| 28 ambientLightSensor.start(); | |
| 29 return new Promise((resolve, reject) => { | |
| 30 let wrapper = new CallbackWrapper(event => { | |
| 31 assert_equals(ambientLightSensor.state, 'errored'); | |
| 32 assert_equals(event.error.name, 'NotFoundError'); | |
| 33 ambientLightSensor.onerror = null; | |
| 34 resolve(); | |
| 35 }, reject); | |
| 36 | |
| 37 ambientLightSensor.onerror = wrapper.callback; | |
| 38 }); | |
| 39 }, 'Test that "onerror" is send when sensor is not supported.'); | |
| 40 | |
| 41 sensor_test(sensor => { | |
| 42 let ambientLightSensor = new AmbientLightSensor({frequency: 560}); | |
| 43 ambientLightSensor.start(); | |
| 44 | |
| 45 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 46 .then(mockSensor => { | |
| 47 mockSensor.setStartShouldFail(true); | |
| 48 return mockSensor.addConfigurationCalled(); }) | |
| 49 .then(mockSensor => { | |
| 50 return new Promise((resolve, reject) => { | |
| 51 let wrapper = new CallbackWrapper(event => { | |
| 52 assert_equals(ambientLightSensor.state, 'errored'); | |
| 53 assert_equals(event.error.name, 'OperationError'); | |
| 54 ambientLightSensor.onerror = null; | |
| 55 resolve(); | |
| 56 }, reject); | |
| 57 | |
| 58 ambientLightSensor.onerror = wrapper.callback; | |
| 59 }); | |
| 60 }); | |
| 61 return testPromise; | |
| 62 }, 'Test that "onerror" is send when start() call has failed.'); | |
| 63 | |
| 64 sensor_test(sensor => { | |
| 65 let ambientLightSensor = new AmbientLightSensor({frequency: 560}); | |
| 66 ambientLightSensor.start(); | |
| 67 | |
| 68 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 69 .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) | |
| 70 .then(mockSensor => { | |
| 71 return new Promise((resolve, reject) => { | |
| 72 let wrapper = new CallbackWrapper(() => { | |
| 73 let configuration = mockSensor.active_sensor_configurations_[0]; | |
| 74 assert_equals(configuration.frequency, 60); | |
| 75 ambientLightSensor.stop(); | |
| 76 assert_equals(ambientLightSensor.state, 'idle'); | |
| 77 resolve(mockSensor); | |
| 78 }, reject); | |
| 79 | |
| 80 ambientLightSensor.onactivate = wrapper.callback; | |
| 81 ambientLightSensor.onerror = reject; | |
| 82 }); | |
| 83 }) | |
| 84 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 85 | |
| 86 return testPromise; | |
| 87 }, 'Test that frequency is capped to 60.0 Hz.'); | |
| 88 | |
| 89 sensor_test(sensor => { | |
| 90 let maxSupportedFrequency = 15; | |
| 91 sensor.mockSensorProvider.setMaximumSupportedFrequency(maxSupportedFrequency); | |
| 92 let ambientLightSensor = new AmbientLightSensor({frequency: 50}); | |
| 93 ambientLightSensor.start(); | |
| 94 | |
| 95 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 96 .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) | |
| 97 .then(mockSensor => { | |
| 98 return new Promise((resolve, reject) => { | |
| 99 let wrapper = new CallbackWrapper(() => { | |
| 100 let configuration = mockSensor.active_sensor_configurations_[0]; | |
| 101 assert_equals(configuration.frequency, maxSupportedFrequency); | |
| 102 ambientLightSensor.stop(); | |
| 103 assert_equals(ambientLightSensor.state, 'idle'); | |
| 104 resolve(mockSensor); | |
| 105 }, reject); | |
| 106 | |
| 107 ambientLightSensor.onactivate = wrapper.callback; | |
| 108 ambientLightSensor.onerror = reject; | |
| 109 }); | |
| 110 }) | |
| 111 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 112 | |
| 113 return testPromise; | |
| 114 }, 'Test that frequency is capped to the maximum supported from frequency.'); | |
| 115 | |
| 116 sensor_test(sensor => { | |
| 117 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); | |
| 118 ambientLightSensor.start(); | |
| 119 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 120 .then((mockSensor) => { | |
| 121 return new Promise((resolve, reject) => { | |
| 122 let wrapper = new CallbackWrapper(() => { | |
| 123 assert_equals(ambientLightSensor.state, 'active'); | |
| 124 ambientLightSensor.stop(); | |
| 125 assert_equals(ambientLightSensor.state, 'idle'); | |
| 126 resolve(mockSensor); | |
| 127 }, reject); | |
| 128 | |
| 129 ambientLightSensor.onactivate = wrapper.callback; | |
| 130 ambientLightSensor.onerror = reject; | |
| 131 }); | |
| 132 }) | |
| 133 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 134 | |
| 135 return testPromise; | |
| 136 }, 'Test that sensor can be successfully created if sensor is supported.'); | |
| 137 | |
| 138 sensor_test(sensor => { | |
| 139 let ambientLightSensor = new AmbientLightSensor(); | |
| 140 ambientLightSensor.start(); | |
| 141 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 142 .then((mockSensor) => { | |
| 143 return new Promise((resolve, reject) => { | |
| 144 let wrapper = new CallbackWrapper(() => { | |
| 145 assert_equals(ambientLightSensor.state, 'active'); | |
| 146 ambientLightSensor.stop(); | |
| 147 assert_equals(ambientLightSensor.state, 'idle'); | |
| 148 resolve(mockSensor); | |
| 149 }, reject); | |
| 150 | |
| 151 ambientLightSensor.onactivate = wrapper.callback; | |
| 152 ambientLightSensor.onerror = reject; | |
| 153 }); | |
| 154 }) | |
| 155 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 156 | |
| 157 return testPromise; | |
| 158 }, 'Test that sensor can be constructed with default configuration.'); | |
| 159 | |
| 160 sensor_test(sensor => { | |
| 161 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); | |
| 162 ambientLightSensor.start(); | |
| 163 | |
| 164 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 165 .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) | |
| 166 .then(mockSensor => { | |
| 167 return new Promise((resolve, reject) => { | |
| 168 let wrapper = new CallbackWrapper(() => { | |
| 169 assert_equals(ambientLightSensor.state, 'active'); | |
| 170 ambientLightSensor.stop(); | |
| 171 assert_equals(ambientLightSensor.state, 'idle'); | |
| 172 resolve(mockSensor); | |
| 173 }, reject); | |
| 174 | |
| 175 ambientLightSensor.onactivate = wrapper.callback; | |
| 176 ambientLightSensor.onerror = reject; | |
| 177 }); | |
| 178 }) | |
| 179 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 180 | |
| 181 return testPromise; | |
| 182 }, 'Test that addConfiguration and removeConfiguration is called.'); | |
| 183 | |
| 184 sensor_test(sensor => { | |
| 185 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); | |
| 186 ambientLightSensor.start(); | |
| 187 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 188 .then(mockSensor => { | |
| 189 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); | |
| 190 }) | |
| 191 .then((mockSensor) => { | |
| 192 return new Promise((resolve, reject) => { | |
| 193 let wrapper = new CallbackWrapper(() => { | |
| 194 assert_equals(ambientLightSensor.reading.illuminance, | |
| 195 kDefaultReadingValue); | |
| 196 ambientLightSensor.stop(); | |
| 197 assert_equals(ambientLightSensor.reading, null); | |
| 198 resolve(mockSensor); | |
| 199 }, reject); | |
| 200 | |
| 201 ambientLightSensor.onchange = wrapper.callback; | |
| 202 ambientLightSensor.onerror = reject; | |
| 203 }); | |
| 204 }) | |
| 205 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 206 | |
| 207 return testPromise; | |
| 208 }, 'Test that onChange is called and sensor reading is valid.'); | |
| 209 | |
| 210 sensor_test(sensor => { | |
| 211 let ambientLightSensor = new AmbientLightSensor(); | |
| 212 ambientLightSensor.start(); | |
| 213 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 214 .then(mockSensor => { | |
| 215 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); | |
| 216 }) | |
| 217 .then((mockSensor) => { | |
| 218 return new Promise((resolve, reject) => { | |
| 219 ambientLightSensor.onchange = () => { | |
| 220 if (ambientLightSensor.reading.illuminance | |
| 221 == kDefaultReadingValue) { | |
| 222 resolve(mockSensor); | |
| 223 } | |
| 224 } | |
| 225 ambientLightSensor.onerror = reject; | |
| 226 }); | |
| 227 }) | |
| 228 .then((mockSensor) => { | |
| 229 testRunner.setPageVisibility("hidden"); | |
| 230 return mockSensor.suspendCalled(); | |
| 231 }) | |
| 232 .then((mockSensor) => { | |
| 233 testRunner.setPageVisibility("visible"); | |
| 234 return mockSensor.resumeCalled(); | |
| 235 }) | |
| 236 .then((mockSensor) => { | |
| 237 return new Promise((resolve, reject) => { | |
| 238 ambientLightSensor.stop(); | |
| 239 resolve(mockSensor); | |
| 240 ambientLightSensor.onerror = reject; | |
| 241 }); | |
| 242 }) | |
| 243 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 244 | |
| 245 return testPromise; | |
| 246 }, 'Test that sensor receives suspend / resume notifications when page' | |
| 247 +' visibility changes.'); | |
| 248 | |
| 249 sensor_test(sensor => { | |
| 250 let sensor1 = new AmbientLightSensor({frequency: 60}); | |
| 251 sensor1.start(); | |
| 252 | |
| 253 let sensor2 = new AmbientLightSensor({frequency: 20}); | |
| 254 sensor2.start(); | |
| 255 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 256 .then(mockSensor => { | |
| 257 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); | |
| 258 }) | |
| 259 .then((mockSensor) => { | |
| 260 return new Promise((resolve, reject) => { | |
| 261 let wrapper = new CallbackWrapper(() => { | |
| 262 // Reading value is correct. | |
| 263 assert_equals(sensor1.reading.illuminance, kDefaultReadingValue); | |
| 264 | |
| 265 // Both sensors share the same reading instance. | |
| 266 let reading = sensor1.reading; | |
| 267 assert_equals(reading, sensor2.reading); | |
| 268 | |
| 269 // After first sensor stops its reading is null, reading for second | |
| 270 // sensor sensor remains. | |
| 271 sensor1.stop(); | |
| 272 assert_equals(sensor1.reading, null); | |
| 273 assert_equals(sensor2.reading.illuminance, kDefaultReadingValue); | |
| 274 | |
| 275 sensor2.stop(); | |
| 276 assert_equals(sensor2.reading, null); | |
| 277 | |
| 278 // Cached reading remains. | |
| 279 assert_equals(reading.illuminance, kDefaultReadingValue); | |
| 280 resolve(mockSensor); | |
| 281 }, reject); | |
| 282 | |
| 283 sensor1.onchange = wrapper.callback; | |
| 284 sensor1.onerror = reject; | |
| 285 sensor2.onerror = reject; | |
| 286 }); | |
| 287 }) | |
| 288 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 289 | |
| 290 return testPromise; | |
| 291 }, 'Test that sensor reading is correct.'); | |
| 292 | |
| 293 </script> | 26 </script> |
| OLD | NEW |