Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AmbientLightSensor: Update tests to be compliance with latest ED #4203

Merged
merged 7 commits into from
Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<iframe src="support-iframe.html" id="frame" style="display:none">
<iframe src="support-iframe.html" id="frame" style="display:none" sandbox="allow-scripts">
</iframe>
<script>

async_test(function (t) {
document.getElementById('frame').onload = t.step_func_done(function(event) {
var iframe = document.getElementById('frame').contentWindow;
let reading = iframe.document.getElementById('reading').value;
assert_equals(reading, "null");
async_test(t => {
window.onmessage = t.step_func(e => {
assert_equals(e.data, "SecurityError");
t.done();
});
}, "sensor readings can not be fired within iframes");
}, "throw a 'SecurityError' when firing sensor readings within iframes");

test(function() {
async_test(t => {
let sensor = new AmbientLightSensor();
sensor.start();
var win = window.open('', '_blank');
let reading = String(sensor.reading);
win.close();
sensor.stop();
assert_equals(reading, "null");
sensor.onactivate = t.step_func_done(() => {
assert_not_equals(sensor.reading, null);
let cachedReading = sensor.reading;
let win = window.open('', '_blank');
assert_equals(sensor.reading, cachedReading);
win.close();
sensor.stop();
});
}, "sensor readings can not be fired on the background tab");

</script>
23 changes: 23 additions & 0 deletions ambient-light/AmbientLightSensor_insecure_context.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>AmbientLightSensor Test: insecure context</title>
<link rel="author" title="Intel" href="http://www.intel.com">
<link rel="help" href="https://www.w3.org/TR/ambient-light/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<h2>Precondition</h2>
<ol>
<li>
Run test in an insecure context, e.g. http://example.com/.
</li>
</ol>
<div id="log"></div>
<script>

test(() => {
assert_throws('SecurityError', () => {
let sensor = new AmbientLightSensor();
});
}, "throw a 'SecurityError' when construct AmbientLightSensor in an insecure context");

</script>
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,20 @@
<link rel="help" href="https://www.w3.org/TR/ambient-light/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<h2>Description</h2>
<p>
This test validates that the "onchange" event can be invoked when changing the ambient light illuminance of the device.
</p>

<h2>Precondition</h2>
<ol>
<li>
Change the ambient light illuminance of the device.
</li>
</ol>

<div id="log"></div>
<script>

async_test(function(t) {
async_test(t => {
let sensor = new AmbientLightSensor();
sensor.start();

sensor.onchange = t.step_func_done(function(event) {
sensor.onchange = t.step_func_done(() => {
assert_greater_than_equal(sensor.reading.illuminance, 0);
assert_equals(sensor.state, "activated");
sensor.stop();
});

sensor.onerror = t.step_func_done(function(event) {
sensor.onerror = t.step_func_done(event => {
assert_unreached(event.error.name + ":" + event.error.message);
});
}, "event change fired");
Expand Down
32 changes: 32 additions & 0 deletions ambient-light/AmbientLightSensor_onerror-manual.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>AmbientLightSensor Test: onerror</title>
<link rel="author" title="Intel" href="http://www.intel.com">
<link rel="help" href="https://www.w3.org/TR/ambient-light/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<h2>Precondition</h2>
<ol>
<li>
Disable the Ambient Light Sensor or run test on a device without Amibent Light Sensor.
</li>
</ol>
<div id="log"></div>
<script>
let sensor;
setup(() => {
sensor = new AmbientLightSensor();
});

async_test(t => {
sensor.onactivate = t.step_func_done(assert_unreached);

sensor.onerror = t.step_func_done(event => {
assert_equals(sensor.state, 'errored');
assert_equals(event.error.name, 'NotFoundError');
});

sensor.start();
}, "Test that 'onerror' event is fired when sensor is not supported");

</script>
39 changes: 0 additions & 39 deletions ambient-light/AmbientLightSensor_onstatechange.html

This file was deleted.

64 changes: 64 additions & 0 deletions ambient-light/AmbientLightSensor_reading.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>AmbientLightSensor Test: reading</title>
<link rel="author" title="Intel" href="http://www.intel.com">
<link rel="help" href="https://www.w3.org/TR/ambient-light/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>

let sensor, sensor1, sensor2, cachedReading1, cachedReading2;

setup(() => {
sensor = new AmbientLightSensor();
sensor1 = new AmbientLightSensor();
sensor2 = new AmbientLightSensor();
sensor1.start();
sensor2.start();
});

async_test(t => {
let sensor = new AmbientLightSensor();
sensor.start();
sensor.onactivate = t.step_func_done(() => {
let cachedReading = sensor.reading;
let cachedIlluminance = cachedReading.illuminance;
sensor.stop();
assert_equals(cachedReading.illuminance, cachedIlluminance);
});
sensor.onerror = t.step_func_done(event => {
assert_unreached(event.error.name + ":" + event.error.message);
});
}, "Test that sensor reading must be immutable.");

async_test(t => {
sensor1.onactivate = t.step_func_done(() => {
cachedReading1 = sensor1.reading;
cachedReading2 = sensor2.reading;
//both sensors share the same reading instance
assert_equals(cachedReading1, cachedReading2);
//after first sensor stops its reading is null, second sensor remains
sensor1.stop();
assert_equals(sensor1.reading, null);
assert_equals(String(sensor2.reading), "[object AmbientLightSensorReading]");
});
sensor1.onerror = t.step_func_done(event => {
assert_unreached(event.error.name + ":" + event.error.message);
});
}, "Test that sensor reading is correct.");

async_test(t => {
t.step_timeout(() => {
sensor2.onchange = t.step_func_done(() => {
let cachedReading3 = sensor2.reading;
assert_not_equals(cachedReading2, cachedReading3);
sensor2.stop();
});
sensor2.onerror = t.step_func_done(event => {
assert_unreached(event.error.name + ":" + event.error.message);
});
}, 1000);
}, "Test that the sensor reading is updated when time passes.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is flaky. You should instead rely on a new onchange event.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed in new commit.


</script>
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,37 @@
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
var sensor, start_return;

setup(function() {
let sensor, start_return;

setup(() => {
sensor = new AmbientLightSensor();
start_return = sensor.start();
});

test(function() {
assert_equals(String(sensor.reading), "[object AmbientLightSensorReading]");
}, "the sensor.reading is AmbientLightSensorReading after executing start() method");
test(() => {
assert_equals(sensor.reading, null);
}, "The default sensor.reading is 'null'");

test(function() {
assert_throws("InvalidStateError", function() { sensor.start(); }, "start() twice");
}, "throw an InvalidStateError exception when state is neither idle nor errored");
test(() => {
assert_equals(sensor.state, "idle");
}, "The default sensor.state is 'idle'");

test(() => {
start_return = sensor.start();
assert_equals(sensor.state, "activating");
}, "The sensor.state changes to 'activating' after sensor.start()");

//TODO: The permission is not ready.

test(function() {
test(() => {
assert_equals(start_return, undefined);
}, "the sensor.start() return undefined");

test(() => {
assert_throws("InvalidStateError", () => { sensor.start(); }, "start() twice");
sensor.stop();
}, "throw an InvalidStateError exception when state is neither idle nor errored");



</script>
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@
<div id="log"></div>
<script>

var sensor, stop_return;
setup(function() {
let sensor, stop_return;

setup(() => {
sensor = new AmbientLightSensor();
sensor.start();
stop_return = sensor.stop();
});

test(function() {
assert_equals(String(sensor.reading), "null");
test(() => {
assert_equals(sensor.state, "idle");
}, "The sensor.state changes to 'idle' after sensor.stop()");

test(() => {
assert_equals(sensor.reading, null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might change. We might want to keep the latest reading around.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spec dependence.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tobie, I would like to leave it as is until we make the final conclusion.

}, "the sensor.reading is null after executing stop() method");

test(function() {
assert_throws("InvalidStateError", function() { sensor.stop(); }, "stop() twice");
test(() => {
assert_throws("InvalidStateError", () => { sensor.stop(); }, "stop() twice");
}, "throw an InvalidStateError exception when state is either idle or errored");

test(function() {
test(() => {
assert_equals(stop_return, undefined);
}, "the sensor.stop() returns undefined");

Expand Down
Loading