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: add some basic functionality tests #3696

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions ambient-light/AmbientLightSensor_browsing_context.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>AmbientLightSensor Test: Sensor readings must only be available in the top-level browsing 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>
<div id="log"></div>
<iframe src="support-iframe.html" id="frame" style="display:none">
</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");
});
}, "sensor readings can not be fired within iframes");

test(function() {
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 readings can not be fired on the background tab");

</script>
36 changes: 36 additions & 0 deletions ambient-light/AmbientLightSensor_onchange-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>AmbientLightSensor Test: onchange</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>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>

<script>

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

sensor.onchange = t.step_func_done(function(event) {
sensor.stop();
});

sensor.onerror = t.step_func_done(function(event) {
assert_unreached(event.error.name + ":" + event.error.message);
});
}, "event change fired");

</script>
39 changes: 39 additions & 0 deletions ambient-light/AmbientLightSensor_onstatechange.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>AmbientLightSensor Test: onstatechange</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>
var sensor;
setup(function() {
sensor = new AmbientLightSensor();
});

test(function() {
assert_equals(sensor.state, "idle");
}, "The default sensor.state is 'idle'.");

async_test(function(t) {
sensor.onstatechange = t.step_func_done(function(event) {
assert_equals(sensor.state, "activating");
});
sensor.onerror = t.step_func_done(function(event) {
assert_unreached(event.error.name + ":" + event.error.message);
});
sensor.start();
}, "The sensor.state changes to 'activating' after sensor.start().");

async_test(function(t) {
sensor.onstatechange = t.step_func_done(function(event) {
assert_equals(sensor.state, "idle");
});
sensor.onerror = t.step_func_done(function(event) {
assert_unreached(event.error.name + ":" + event.error.message);
});
sensor.stop();
}, "The sensor.state changes to 'idle' after sensor.stop().");

</script>
35 changes: 35 additions & 0 deletions ambient-light/AmbientLightSensor_start.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>AmbientLightSensor Test: start()</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>
var sensor, start_return;

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

test(function() {
assert_equals(sensor.state, "activating");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is better to put all these assertions in this test file together within ambient-light/AmbientLightSensor_onstatechange.html.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zqzhang, thanks for your comments! Agree!

}, "the sensor.state is activating after executing start() method");

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

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

//TODO: The permission is not ready.

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

</script>
34 changes: 34 additions & 0 deletions ambient-light/AmbientLightSensor_stop.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>AmbientLightSensor Test: stop()</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>

var sensor, stop_return;
setup(function() {
sensor = new AmbientLightSensor();
sensor.start();
stop_return = sensor.stop();
});

test(function() {
assert_equals(sensor.state, "idle");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is better to put all these assertions in this test file together within ambient-light/AmbientLightSensor_onstatechange.html.

}, "the sensor.state is idle after executing stop() method");

test(function() {
assert_equals(String(sensor.reading), "null");
}, "the sensor.reading is null after executing stop() method");

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

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

</script>
11 changes: 11 additions & 0 deletions ambient-light/support-iframe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE HTML>
<meta charset="utf-8">
<input type="text" id="reading" />
<script>

let sensor = new AmbientLightSensor();
sensor.start();
document.getElementById("reading").value = String(sensor.reading);
sensor.stop();

</script>