Index: third_party/WebKit/LayoutTests/sensor/ambient-light-sensor.html |
diff --git a/third_party/WebKit/LayoutTests/sensor/ambient-light-sensor.html b/third_party/WebKit/LayoutTests/sensor/ambient-light-sensor.html |
index a3e885620c913c5e8a9980bcbafd26644e838b98..1fd4e1074c1e40f17ddd131f60d8be2ca78e5e37 100644 |
--- a/third_party/WebKit/LayoutTests/sensor/ambient-light-sensor.html |
+++ b/third_party/WebKit/LayoutTests/sensor/ambient-light-sensor.html |
@@ -3,6 +3,7 @@ |
<script src="../resources/testharnessreport.js"></script> |
<script src="../resources/mojo-helpers.js"></script> |
<script src="resources/sensor-helpers.js"></script> |
+<script src="resources/generic-sensor-tests.js"></script> |
<script> |
'use strict'; |
@@ -17,277 +18,9 @@ function update_sensor_reading(buffer) { |
buffer[2] = kDefaultReadingValue; |
} |
-test(() => assert_throws( |
- new RangeError(), |
- () => new AmbientLightSensor({frequency: -60})), |
- 'Test that negative frequency causes exception from constructor.'); |
- |
-sensor_test(sensor => { |
- sensor.mockSensorProvider.setGetSensorShouldFail(true); |
- let ambientLightSensor = new AmbientLightSensor(); |
- ambientLightSensor.start(); |
- return new Promise((resolve, reject) => { |
- let wrapper = new CallbackWrapper(event => { |
- assert_equals(ambientLightSensor.state, 'errored'); |
- assert_equals(event.error.name, 'NotFoundError'); |
- ambientLightSensor.onerror = null; |
- resolve(); |
- }, reject); |
- |
- ambientLightSensor.onerror = wrapper.callback; |
- }); |
-}, 'Test that "onerror" is send when sensor is not supported.'); |
- |
-sensor_test(sensor => { |
- let ambientLightSensor = new AmbientLightSensor({frequency: 560}); |
- ambientLightSensor.start(); |
- |
- let testPromise = sensor.mockSensorProvider.getCreatedSensor() |
- .then(mockSensor => { |
- mockSensor.setStartShouldFail(true); |
- return mockSensor.addConfigurationCalled(); }) |
- .then(mockSensor => { |
- return new Promise((resolve, reject) => { |
- let wrapper = new CallbackWrapper(event => { |
- assert_equals(ambientLightSensor.state, 'errored'); |
- assert_equals(event.error.name, 'OperationError'); |
- ambientLightSensor.onerror = null; |
- resolve(); |
- }, reject); |
- |
- ambientLightSensor.onerror = wrapper.callback; |
- }); |
- }); |
- return testPromise; |
-}, 'Test that "onerror" is send when start() call has failed.'); |
- |
-sensor_test(sensor => { |
- let ambientLightSensor = new AmbientLightSensor({frequency: 560}); |
- ambientLightSensor.start(); |
- |
- let testPromise = sensor.mockSensorProvider.getCreatedSensor() |
- .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) |
- .then(mockSensor => { |
- return new Promise((resolve, reject) => { |
- let wrapper = new CallbackWrapper(() => { |
- let configuration = mockSensor.active_sensor_configurations_[0]; |
- assert_equals(configuration.frequency, 60); |
- ambientLightSensor.stop(); |
- assert_equals(ambientLightSensor.state, 'idle'); |
- resolve(mockSensor); |
- }, reject); |
- |
- ambientLightSensor.onactivate = wrapper.callback; |
- ambientLightSensor.onerror = reject; |
- }); |
- }) |
- .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); |
- |
- return testPromise; |
-}, 'Test that frequency is capped to 60.0 Hz.'); |
- |
-sensor_test(sensor => { |
- let maxSupportedFrequency = 15; |
- sensor.mockSensorProvider.setMaximumSupportedFrequency(maxSupportedFrequency); |
- let ambientLightSensor = new AmbientLightSensor({frequency: 50}); |
- ambientLightSensor.start(); |
- |
- let testPromise = sensor.mockSensorProvider.getCreatedSensor() |
- .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) |
- .then(mockSensor => { |
- return new Promise((resolve, reject) => { |
- let wrapper = new CallbackWrapper(() => { |
- let configuration = mockSensor.active_sensor_configurations_[0]; |
- assert_equals(configuration.frequency, maxSupportedFrequency); |
- ambientLightSensor.stop(); |
- assert_equals(ambientLightSensor.state, 'idle'); |
- resolve(mockSensor); |
- }, reject); |
- |
- ambientLightSensor.onactivate = wrapper.callback; |
- ambientLightSensor.onerror = reject; |
- }); |
- }) |
- .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); |
- |
- return testPromise; |
-}, 'Test that frequency is capped to the maximum supported from frequency.'); |
- |
-sensor_test(sensor => { |
- let ambientLightSensor = new AmbientLightSensor({frequency: 60}); |
- ambientLightSensor.start(); |
- let testPromise = sensor.mockSensorProvider.getCreatedSensor() |
- .then((mockSensor) => { |
- return new Promise((resolve, reject) => { |
- let wrapper = new CallbackWrapper(() => { |
- assert_equals(ambientLightSensor.state, 'active'); |
- ambientLightSensor.stop(); |
- assert_equals(ambientLightSensor.state, 'idle'); |
- resolve(mockSensor); |
- }, reject); |
- |
- ambientLightSensor.onactivate = wrapper.callback; |
- ambientLightSensor.onerror = reject; |
- }); |
- }) |
- .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); |
- |
- return testPromise; |
-}, 'Test that sensor can be successfully created if sensor is supported.'); |
- |
-sensor_test(sensor => { |
- let ambientLightSensor = new AmbientLightSensor(); |
- ambientLightSensor.start(); |
- let testPromise = sensor.mockSensorProvider.getCreatedSensor() |
- .then((mockSensor) => { |
- return new Promise((resolve, reject) => { |
- let wrapper = new CallbackWrapper(() => { |
- assert_equals(ambientLightSensor.state, 'active'); |
- ambientLightSensor.stop(); |
- assert_equals(ambientLightSensor.state, 'idle'); |
- resolve(mockSensor); |
- }, reject); |
- |
- ambientLightSensor.onactivate = wrapper.callback; |
- ambientLightSensor.onerror = reject; |
- }); |
- }) |
- .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); |
- |
- return testPromise; |
-}, 'Test that sensor can be constructed with default configuration.'); |
- |
-sensor_test(sensor => { |
- let ambientLightSensor = new AmbientLightSensor({frequency: 60}); |
- ambientLightSensor.start(); |
- |
- let testPromise = sensor.mockSensorProvider.getCreatedSensor() |
- .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) |
- .then(mockSensor => { |
- return new Promise((resolve, reject) => { |
- let wrapper = new CallbackWrapper(() => { |
- assert_equals(ambientLightSensor.state, 'active'); |
- ambientLightSensor.stop(); |
- assert_equals(ambientLightSensor.state, 'idle'); |
- resolve(mockSensor); |
- }, reject); |
- |
- ambientLightSensor.onactivate = wrapper.callback; |
- ambientLightSensor.onerror = reject; |
- }); |
- }) |
- .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); |
- |
- return testPromise; |
-}, 'Test that addConfiguration and removeConfiguration is called.'); |
- |
-sensor_test(sensor => { |
- let ambientLightSensor = new AmbientLightSensor({frequency: 60}); |
- ambientLightSensor.start(); |
- let testPromise = sensor.mockSensorProvider.getCreatedSensor() |
- .then(mockSensor => { |
- return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); |
- }) |
- .then((mockSensor) => { |
- return new Promise((resolve, reject) => { |
- let wrapper = new CallbackWrapper(() => { |
- assert_equals(ambientLightSensor.reading.illuminance, |
- kDefaultReadingValue); |
- ambientLightSensor.stop(); |
- assert_equals(ambientLightSensor.reading, null); |
- resolve(mockSensor); |
- }, reject); |
- |
- ambientLightSensor.onchange = wrapper.callback; |
- ambientLightSensor.onerror = reject; |
- }); |
- }) |
- .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); |
- |
- return testPromise; |
-}, 'Test that onChange is called and sensor reading is valid.'); |
- |
-sensor_test(sensor => { |
- let ambientLightSensor = new AmbientLightSensor(); |
- ambientLightSensor.start(); |
- let testPromise = sensor.mockSensorProvider.getCreatedSensor() |
- .then(mockSensor => { |
- return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); |
- }) |
- .then((mockSensor) => { |
- return new Promise((resolve, reject) => { |
- ambientLightSensor.onchange = () => { |
- if (ambientLightSensor.reading.illuminance |
- == kDefaultReadingValue) { |
- resolve(mockSensor); |
- } |
- } |
- ambientLightSensor.onerror = reject; |
- }); |
- }) |
- .then((mockSensor) => { |
- testRunner.setPageVisibility("hidden"); |
- return mockSensor.suspendCalled(); |
- }) |
- .then((mockSensor) => { |
- testRunner.setPageVisibility("visible"); |
- return mockSensor.resumeCalled(); |
- }) |
- .then((mockSensor) => { |
- return new Promise((resolve, reject) => { |
- ambientLightSensor.stop(); |
- resolve(mockSensor); |
- ambientLightSensor.onerror = reject; |
- }); |
- }) |
- .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); |
- |
- return testPromise; |
-}, 'Test that sensor receives suspend / resume notifications when page' |
- +' visibility changes.'); |
- |
-sensor_test(sensor => { |
- let sensor1 = new AmbientLightSensor({frequency: 60}); |
- sensor1.start(); |
- |
- let sensor2 = new AmbientLightSensor({frequency: 20}); |
- sensor2.start(); |
- let testPromise = sensor.mockSensorProvider.getCreatedSensor() |
- .then(mockSensor => { |
- return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); |
- }) |
- .then((mockSensor) => { |
- return new Promise((resolve, reject) => { |
- let wrapper = new CallbackWrapper(() => { |
- // Reading value is correct. |
- assert_equals(sensor1.reading.illuminance, kDefaultReadingValue); |
- |
- // Both sensors share the same reading instance. |
- let reading = sensor1.reading; |
- assert_equals(reading, sensor2.reading); |
- |
- // After first sensor stops its reading is null, reading for second |
- // sensor sensor remains. |
- sensor1.stop(); |
- assert_equals(sensor1.reading, null); |
- assert_equals(sensor2.reading.illuminance, kDefaultReadingValue); |
- |
- sensor2.stop(); |
- assert_equals(sensor2.reading, null); |
- |
- // Cached reading remains. |
- assert_equals(reading.illuminance, kDefaultReadingValue); |
- resolve(mockSensor); |
- }, reject); |
- |
- sensor1.onchange = wrapper.callback; |
- sensor1.onerror = reject; |
- sensor2.onerror = reject; |
- }); |
- }) |
- .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); |
- |
- return testPromise; |
-}, 'Test that sensor reading is correct.'); |
+function verify_sensor_reading(reading) { |
+ return reading.illuminance == kDefaultReadingValue; |
+} |
+runGenericSensorTests(AmbientLightSensor, update_sensor_reading, verify_sensor_reading); |
</script> |