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