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 function update_sensor_reading(buffer) { |
| 14 buffer[0] = window.performance.now(); |
| 15 buffer[1] = 3.1415; |
| 16 } |
| 17 |
| 18 sensor_test(sensor => { |
| 19 sensor.mockSensorProvider.setGetSensorShouldFail(true); |
| 20 let ambientLightSensor = new AmbientLightSensor(); |
| 21 ambientLightSensor.start(); |
| 22 return new Promise((resolve, reject) => { |
| 23 ambientLightSensor.onstatechange = event => { |
| 24 assert_equals(ambientLightSensor.state, 'errored'); |
| 25 resolve(); |
| 26 }; |
| 27 }); |
| 28 }, 'Test that sensor state changes to "errored" when sensor is not supported.'); |
| 29 |
| 30 sensor_test(sensor => { |
| 31 let mockSensorCreated = sensor.mockSensorProvider.getCreatedSensor(); |
| 32 let ambientLightSensor = new AmbientLightSensor({frequency: 1}); |
| 33 ambientLightSensor.start(); |
| 34 ambientLightSensor.stop(); |
| 35 return mockSensorCreated; |
| 36 }, 'Test that sensor can be successfully created if sensor is supported.'); |
| 37 |
| 38 sensor_test(sensor => { |
| 39 let ambientLightSensor = new AmbientLightSensor({frequency: 1}); |
| 40 let mockSensorCreated = sensor.mockSensorProvider.getCreatedSensor() |
| 41 .then(mockSensor => { |
| 42 mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); |
| 43 }) |
| 44 .then(() => { |
| 45 return new Promise((resolve, reject) => { |
| 46 ambientLightSensor.onchange = e => { |
| 47 if (e.reading.illuminance == 3.1415) { |
| 48 resolve(); |
| 49 } |
| 50 } |
| 51 ambientLightSensor.onerror = reject; |
| 52 ambientLightSensor.start(); |
| 53 }); |
| 54 }); |
| 55 return mockSensorCreated; |
| 56 }, 'Test that onChange is called and sensor reading is valid.'); |
| 57 |
| 58 sensor_test(sensor => { |
| 59 let ambientLightSensor = new AmbientLightSensor(); |
| 60 let mockSensorCreated = sensor.mockSensorProvider.getCreatedSensor() |
| 61 .then(mockSensor => { |
| 62 mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); |
| 63 }) |
| 64 .then(() => { |
| 65 return new Promise((resolve, reject) => { |
| 66 ambientLightSensor.onstatechange = () => { |
| 67 if (ambientLightSensor.state === 'active') { |
| 68 resolve(); |
| 69 } |
| 70 } |
| 71 ambientLightSensor.start(); |
| 72 }); |
| 73 }) |
| 74 .then(() => { |
| 75 return new Promise((resolve, reject) => { |
| 76 ambientLightSensor.onchange = e => { |
| 77 if (e.reading.illuminance == 3.1415) { |
| 78 resolve(); |
| 79 } |
| 80 } |
| 81 ambientLightSensor.onerror = reject; |
| 82 }); |
| 83 }) |
| 84 .then(() => { |
| 85 return new Promise((resolve, reject) => { |
| 86 ambientLightSensor.onstatechange = () => { |
| 87 if (ambientLightSensor.state === 'idle') { |
| 88 assert_equals(ambientLightSensor.reading, null); |
| 89 resolve(); |
| 90 } |
| 91 } |
| 92 |
| 93 ambientLightSensor.onerror = reject; |
| 94 ambientLightSensor.stop(); |
| 95 }); |
| 96 }); |
| 97 |
| 98 return mockSensorCreated; |
| 99 }, 'Test that sensor reading is not updated when sensor is stopped.'); |
| 100 |
| 101 sensor_test(sensor => { |
| 102 let ambientLightSensor = new AmbientLightSensor(); |
| 103 let mockSensorCreated = sensor.mockSensorProvider.getCreatedSensor() |
| 104 .then(mockSensor => { |
| 105 mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); |
| 106 }) |
| 107 .then(() => { |
| 108 return new Promise((resolve, reject) => { |
| 109 ambientLightSensor.onchange = e => { |
| 110 if (e.reading.illuminance == 3.1415) { |
| 111 resolve(); |
| 112 } |
| 113 } |
| 114 ambientLightSensor.onerror = reject; |
| 115 ambientLightSensor.start(); |
| 116 }); |
| 117 }); |
| 118 return mockSensorCreated; |
| 119 }, 'Test that sensor can be constructed with default configuration.'); |
| 120 |
| 121 sensor_test(sensor => { |
| 122 let ambientLightSensor = new AmbientLightSensor(); |
| 123 let mockSensorCreated = sensor.mockSensorProvider.getCreatedSensor() |
| 124 .then(mockSensor => { |
| 125 mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); |
| 126 }) |
| 127 .then(() => { |
| 128 return new Promise((resolve, reject) => { |
| 129 ambientLightSensor.onchange = e => { |
| 130 if (e.reading.illuminance == 3.1415) { |
| 131 resolve(); |
| 132 } |
| 133 } |
| 134 ambientLightSensor.onerror = reject; |
| 135 ambientLightSensor.start(); |
| 136 }); |
| 137 }) |
| 138 .then(() => { |
| 139 testRunner.setPageVisibility("hidden"); |
| 140 return mockSensor.isSuspendCalled(); |
| 141 }) |
| 142 .then(() => { |
| 143 testRunner.setPageVisibility("visible"); |
| 144 return mockSensor.isResumeCalled(); |
| 145 }); |
| 146 return mockSensorCreated; |
| 147 }, 'Test that sensor receives suspend / resume notifications when page' |
| 148 +' visibility changes.'); |
| 149 |
| 150 |
| 151 </script> |
OLD | NEW |