Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!DOCTYPE html> | |
| 2 <script src="../resources/testharness.js"></script> | |
| 3 <script src="../resources/testharnessreport.js"></script> | |
| 4 <script src="../resources/mojo-helpers.js"></script> | |
| 5 <script src="resources/sensor-helpers.js"></script> | |
| 6 <script> | |
| 7 | |
| 8 'use strict'; | |
| 9 | |
| 10 if (!window.testRunner) | |
| 11 debug('This test cannot be run without the TestRunner'); | |
| 12 | |
| 13 const kDefaultReadingValue = 3.1415; | |
| 14 | |
| 15 function update_sensor_reading(buffer) { | |
| 16 buffer[0] = window.performance.now(); | |
| 17 buffer[1] = kDefaultReadingValue; | |
| 18 } | |
| 19 | |
| 20 sensor_test(sensor => { | |
| 21 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); | |
| 22 ambientLightSensor.start(); | |
| 23 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 24 .then((mockSensor) => { | |
| 25 return new Promise((resolve, reject) => { | |
| 26 ambientLightSensor.onstatechange = event => { | |
| 27 if (ambientLightSensor.state === 'idle') { | |
| 28 resolve(mockSensor); | |
| 29 } | |
| 30 | |
| 31 if (ambientLightSensor.state === 'active') { | |
| 32 ambientLightSensor.stop(); | |
| 33 } | |
| 34 }; | |
| 35 ambientLightSensor.onerror = reject; | |
| 36 }); | |
| 37 }) | |
| 38 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 39 | |
| 40 return testPromise; | |
| 41 }, 'Test that sensor can be successfully created if sensor is supported.'); | |
| 42 | |
| 43 sensor_test(sensor => { | |
| 44 let ambientLightSensor = new AmbientLightSensor(); | |
| 45 ambientLightSensor.start(); | |
| 46 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 47 .then((mockSensor) => { | |
| 48 return new Promise((resolve, reject) => { | |
| 49 ambientLightSensor.onstatechange = event => { | |
| 50 if (ambientLightSensor.state === 'idle') { | |
| 51 resolve(mockSensor); | |
| 52 } | |
| 53 | |
| 54 if (ambientLightSensor.state === 'active') { | |
| 55 ambientLightSensor.stop(); | |
| 56 } | |
| 57 }; | |
| 58 | |
| 59 ambientLightSensor.onerror = reject; | |
| 60 }); | |
| 61 }) | |
| 62 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 63 | |
| 64 return testPromise; | |
| 65 }, 'Test that sensor can be constructed with default configuration.'); | |
| 66 | |
| 67 sensor_test(sensor => { | |
| 68 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); | |
| 69 ambientLightSensor.start(); | |
| 70 | |
| 71 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 72 .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) | |
| 73 .then(mockSensor => { | |
| 74 return new Promise((resolve, reject) => { | |
| 75 ambientLightSensor.onstatechange = event => { | |
| 76 if (ambientLightSensor.state === 'idle') { | |
| 77 resolve(mockSensor); | |
| 78 } | |
| 79 | |
| 80 if (ambientLightSensor.state === 'active') { | |
| 81 ambientLightSensor.stop(); | |
| 82 } | |
| 83 }; | |
| 84 }); | |
| 85 }) | |
| 86 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 87 | |
| 88 return testPromise; | |
| 89 }, 'Test that addConfiguration and removeConfiguration is called.'); | |
| 90 | |
| 91 sensor_test(sensor => { | |
| 92 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); | |
| 93 ambientLightSensor.start(); | |
| 94 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 95 .then(mockSensor => { | |
| 96 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); | |
| 97 }) | |
| 98 .then((mockSensor) => { | |
| 99 return new Promise((resolve, reject) => { | |
| 100 ambientLightSensor.onstatechange = event => { | |
| 101 if (ambientLightSensor.state === 'idle') { | |
| 102 resolve(mockSensor); | |
| 103 } | |
| 104 }; | |
| 105 | |
| 106 ambientLightSensor.onchange = e => { | |
| 107 assert_equals(e.reading.illuminance, kDefaultReadingValue); | |
| 108 ambientLightSensor.stop(); | |
| 109 }; | |
| 110 | |
| 111 ambientLightSensor.onerror = reject; | |
| 112 }); | |
| 113 }) | |
| 114 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 115 | |
| 116 return testPromise; | |
| 117 }, 'Test that onChange is called and sensor reading is valid.'); | |
| 118 | |
| 119 sensor_test(sensor => { | |
| 120 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); | |
| 121 ambientLightSensor.start(); | |
| 122 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 123 .then(mockSensor => { | |
| 124 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); | |
| 125 }) | |
| 126 .then((mockSensor) => { | |
| 127 return new Promise((resolve, reject) => { | |
| 128 ambientLightSensor.onstatechange = () => { | |
| 129 if (ambientLightSensor.state === 'idle') { | |
| 130 assert_equals(ambientLightSensor.reading, null); | |
| 131 resolve(mockSensor); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 ambientLightSensor.onchange = e => { | |
| 136 assert_equals(e.reading.illuminance, kDefaultReadingValue); | |
| 137 ambientLightSensor.stop(); | |
| 138 } | |
| 139 ambientLightSensor.onerror = reject; | |
| 140 }); | |
| 141 }) | |
| 142 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 143 | |
| 144 return testPromise; | |
| 145 }, 'Test that sensor reading is not updated when sensor is stopped.'); | |
| 146 | |
| 147 sensor_test(sensor => { | |
| 148 let ambientLightSensor = new AmbientLightSensor(); | |
| 149 ambientLightSensor.start(); | |
| 150 let testPromise = sensor.mockSensorProvider.getCreatedSensor() | |
| 151 .then(mockSensor => { | |
| 152 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); | |
| 153 }) | |
| 154 .then((mockSensor) => { | |
| 155 return new Promise((resolve, reject) => { | |
| 156 ambientLightSensor.onchange = e => { | |
| 157 if (e.reading.illuminance == kDefaultReadingValue) { | |
| 158 resolve(mockSensor); | |
| 159 } | |
| 160 } | |
| 161 ambientLightSensor.onerror = reject; | |
| 162 }); | |
| 163 }) | |
| 164 .then((mockSensor) => { | |
| 165 testRunner.setPageVisibility("hidden"); | |
| 166 return mockSensor.suspendCalled(); | |
| 167 }) | |
| 168 .then((mockSensor) => { | |
| 169 testRunner.setPageVisibility("visible"); | |
| 170 return mockSensor.resumeCalled(); | |
| 171 }) | |
| 172 .then((mockSensor) => { | |
| 173 return new Promise((resolve, reject) => { | |
| 174 ambientLightSensor.onstatechange = () => { | |
| 175 if (ambientLightSensor.state === 'idle') { | |
| 176 resolve(mockSensor); | |
| 177 } | |
| 178 } | |
| 179 ambientLightSensor.stop(); | |
| 180 ambientLightSensor.onerror = reject; | |
| 181 }); | |
| 182 }) | |
| 183 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); | |
| 184 | |
| 185 return testPromise; | |
| 186 }, 'Test that sensor receives suspend / resume notifications when page' | |
| 187 +' visibility changes.'); | |
| 188 | |
|
timvolodine
2016/09/19 16:37:32
Just a couple of ideas for future consideration:
-
| |
| 189 </script> | |
| OLD | NEW |