| 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 runGenericSensorTest(AmbientLightSensor, update_sensor_reading, verify_sensor_re
ading); |
| 26 sensor.mockSensorProvider.setGetSensorShouldFail(true); | |
| 27 let ambientLightSensor = new AmbientLightSensor(); | |
| 28 ambientLightSensor.start(); | |
| 29 return new Promise((resolve, reject) => { | |
| 30 ambientLightSensor.onstatechange = event => { | |
| 31 if(ambientLightSensor.state == 'errored') { | |
| 32 resolve(); | |
| 33 } | |
| 34 }; | |
| 35 }); | |
| 36 }, 'Test that sensor state changes to "errored" when sensor is not supported.'); | |
| 37 | |
| 38 sensor_test(sensor => { | |
| 39 sensor.mockSensorProvider.setGetSensorShouldFail(true); | |
| 40 let ambientLightSensor = new AmbientLightSensor(); | |
| 41 ambientLightSensor.start(); | |
| 42 return new Promise((resolve, reject) => { | |
| 43 ambientLightSensor.onerror = event => { | |
| 44 assert_equals(ambientLightSensor.state, 'errored'); | |
| 45 console.log(event.error.message); | |
| 46 assert_equals(event.error.name, 'NotFoundError'); | |
| 47 ambientLightSensor.onerror = null; | |
| 48 resolve(); | |
| 49 }; | |
| 50 | |
| 51 }); | |
| 52 }, 'Test that "onerror" is send when sensor is not supported.'); | |
| 53 | |
| 54 | |
| 55 sensor_test(sensor => { | |
| 56 let ambientLightSensor = new AmbientLightSensor({frequency: 560}); | |
| 57 ambientLightSensor.start(); | |
| 58 | |
| 59 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 60 .then(mockSensor => { | |
| 61 mockSensor.setStartShouldFail(true); | |
| 62 return mockSensor.addConfigurationCalled(); }) | |
| 63 .then(mockSensor => { | |
| 64 return new Promise((resolve, reject) => { | |
| 65 ambientLightSensor.onerror = event => { | |
| 66 assert_equals(ambientLightSensor.state, 'errored'); | |
| 67 assert_equals(event.error.name, 'OperationError'); | |
| 68 ambientLightSensor.onerror = null; | |
| 69 resolve(); | |
| 70 }; | |
| 71 }); | |
| 72 }); | |
| 73 return testPromise; | |
| 74 }, 'Test that "onerror" is send when start() call has failed.'); | |
| 75 | |
| 76 sensor_test(sensor => { | |
| 77 let ambientLightSensor = new AmbientLightSensor({frequency: 560}); | |
| 78 ambientLightSensor.start(); | |
| 79 | |
| 80 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 81 .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) | |
| 82 .then(mockSensor => { | |
| 83 return new Promise((resolve, reject) => { | |
| 84 ambientLightSensor.onstatechange = event => { | |
| 85 if (ambientLightSensor.state === 'idle') { | |
| 86 resolve(mockSensor); | |
| 87 } | |
| 88 | |
| 89 if (ambientLightSensor.state === 'active') { | |
| 90 let configuration = mockSensor.active_sensor_configurations_[0]; | |
| 91 assert_equals(configuration.frequency, 60); | |
| 92 ambientLightSensor.stop(); | |
| 93 } | |
| 94 }; | |
| 95 }); | |
| 96 }) | |
| 97 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 98 | |
| 99 return testPromise; | |
| 100 }, 'Test that frequency is capped to 60.0 Hz.'); | |
| 101 | |
| 102 sensor_test(sensor => { | |
| 103 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); | |
| 104 ambientLightSensor.start(); | |
| 105 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 106 .then((mockSensor) => { | |
| 107 return new Promise((resolve, reject) => { | |
| 108 ambientLightSensor.onstatechange = event => { | |
| 109 if (ambientLightSensor.state === 'idle') { | |
| 110 resolve(mockSensor); | |
| 111 } | |
| 112 | |
| 113 if (ambientLightSensor.state === 'active') { | |
| 114 ambientLightSensor.stop(); | |
| 115 } | |
| 116 }; | |
| 117 ambientLightSensor.onerror = reject; | |
| 118 }); | |
| 119 }) | |
| 120 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 121 | |
| 122 return testPromise; | |
| 123 }, 'Test that sensor can be successfully created if sensor is supported.'); | |
| 124 | |
| 125 sensor_test(sensor => { | |
| 126 let ambientLightSensor = new AmbientLightSensor(); | |
| 127 ambientLightSensor.start(); | |
| 128 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 129 .then((mockSensor) => { | |
| 130 return new Promise((resolve, reject) => { | |
| 131 ambientLightSensor.onstatechange = event => { | |
| 132 if (ambientLightSensor.state === 'idle') { | |
| 133 resolve(mockSensor); | |
| 134 } | |
| 135 | |
| 136 if (ambientLightSensor.state === 'active') { | |
| 137 ambientLightSensor.stop(); | |
| 138 } | |
| 139 }; | |
| 140 | |
| 141 ambientLightSensor.onerror = reject; | |
| 142 }); | |
| 143 }) | |
| 144 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 145 | |
| 146 return testPromise; | |
| 147 }, 'Test that sensor can be constructed with default configuration.'); | |
| 148 | |
| 149 sensor_test(sensor => { | |
| 150 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); | |
| 151 ambientLightSensor.start(); | |
| 152 | |
| 153 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 154 .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) | |
| 155 .then(mockSensor => { | |
| 156 return new Promise((resolve, reject) => { | |
| 157 ambientLightSensor.onstatechange = event => { | |
| 158 if (ambientLightSensor.state === 'idle') { | |
| 159 resolve(mockSensor); | |
| 160 } | |
| 161 | |
| 162 if (ambientLightSensor.state === 'active') { | |
| 163 ambientLightSensor.stop(); | |
| 164 } | |
| 165 }; | |
| 166 }); | |
| 167 }) | |
| 168 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 169 | |
| 170 return testPromise; | |
| 171 }, 'Test that addConfiguration and removeConfiguration is called.'); | |
| 172 | |
| 173 sensor_test(sensor => { | |
| 174 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); | |
| 175 ambientLightSensor.start(); | |
| 176 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 177 .then(mockSensor => { | |
| 178 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); | |
| 179 }) | |
| 180 .then((mockSensor) => { | |
| 181 return new Promise((resolve, reject) => { | |
| 182 ambientLightSensor.onstatechange = event => { | |
| 183 if (ambientLightSensor.state === 'idle') { | |
| 184 resolve(mockSensor); | |
| 185 } | |
| 186 }; | |
| 187 | |
| 188 ambientLightSensor.onchange = e => { | |
| 189 assert_equals(e.reading.illuminance, kDefaultReadingValue); | |
| 190 ambientLightSensor.stop(); | |
| 191 }; | |
| 192 | |
| 193 ambientLightSensor.onerror = reject; | |
| 194 }); | |
| 195 }) | |
| 196 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 197 | |
| 198 return testPromise; | |
| 199 }, 'Test that onChange is called and sensor reading is valid.'); | |
| 200 | |
| 201 sensor_test(sensor => { | |
| 202 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); | |
| 203 ambientLightSensor.start(); | |
| 204 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 205 .then(mockSensor => { | |
| 206 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); | |
| 207 }) | |
| 208 .then((mockSensor) => { | |
| 209 return new Promise((resolve, reject) => { | |
| 210 ambientLightSensor.onstatechange = () => { | |
| 211 if (ambientLightSensor.state === 'idle') { | |
| 212 assert_equals(ambientLightSensor.reading, null); | |
| 213 resolve(mockSensor); | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 ambientLightSensor.onchange = e => { | |
| 218 assert_equals(e.reading.illuminance, kDefaultReadingValue); | |
| 219 ambientLightSensor.stop(); | |
| 220 } | |
| 221 ambientLightSensor.onerror = reject; | |
| 222 }); | |
| 223 }) | |
| 224 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 225 | |
| 226 return testPromise; | |
| 227 }, 'Test that sensor reading is not updated when sensor is stopped.'); | |
| 228 | |
| 229 sensor_test(sensor => { | |
| 230 let ambientLightSensor = new AmbientLightSensor(); | |
| 231 ambientLightSensor.start(); | |
| 232 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 233 .then(mockSensor => { | |
| 234 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); | |
| 235 }) | |
| 236 .then((mockSensor) => { | |
| 237 return new Promise((resolve, reject) => { | |
| 238 ambientLightSensor.onchange = e => { | |
| 239 if (e.reading.illuminance == kDefaultReadingValue) { | |
| 240 resolve(mockSensor); | |
| 241 } | |
| 242 } | |
| 243 ambientLightSensor.onerror = reject; | |
| 244 }); | |
| 245 }) | |
| 246 .then((mockSensor) => { | |
| 247 testRunner.setPageVisibility("hidden"); | |
| 248 return mockSensor.suspendCalled(); | |
| 249 }) | |
| 250 .then((mockSensor) => { | |
| 251 testRunner.setPageVisibility("visible"); | |
| 252 return mockSensor.resumeCalled(); | |
| 253 }) | |
| 254 .then((mockSensor) => { | |
| 255 return new Promise((resolve, reject) => { | |
| 256 ambientLightSensor.onstatechange = () => { | |
| 257 if (ambientLightSensor.state === 'idle') { | |
| 258 resolve(mockSensor); | |
| 259 } | |
| 260 } | |
| 261 ambientLightSensor.stop(); | |
| 262 ambientLightSensor.onerror = reject; | |
| 263 }); | |
| 264 }) | |
| 265 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 266 | |
| 267 return testPromise; | |
| 268 }, 'Test that sensor receives suspend / resume notifications when page' | |
| 269 +' visibility changes.'); | |
| 270 | |
| 271 </script> | 26 </script> |
| OLD | NEW |