Commit 70baebd0 authored by Raphael Kubo da Costa's avatar Raphael Kubo da Costa Committed by Commit Bot

sensors: Clean the iframe tests up some more.

The Mac bots are occasionally timing out. Clean up the iframe tests a bit
more hoping it helps:
* Remove redundant call to 'reset_sensor_backend' that is already performed
  by the test's cleanup function.
* Move the message handler to a separate function, so that we can then call
  it from a catch-all try/catch statement and ensure we will always send a
  reply. This also allows us to remove several try/catch blocks from the
  handler itself.

Bug: 1073865
Change-Id: I9756c85ee401a78dabe5f283250ab68110decabb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2424248
Auto-Submit: Raphael Kubo da Costa <raphael.kubo.da.costa@intel.com>
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809638}
parent f02e69d7
......@@ -126,7 +126,6 @@ function run_generic_sensor_iframe_tests(sensorName) {
await sensorWatcher.wait_for('reading');
assert_greater_than(sensor.timestamp, cachedTimeStamp);
sensor.stop();
await send_message_to_iframe(iframe, {command: 'reset_sensor_backend'});
}, `${sensorName}: sensor is not suspended when focus traverses from\
to same-origin frame`);
......
......@@ -7,52 +7,33 @@
let sensor = null;
let sensorType = null;
function postReply(event, reply) {
event.source.postMessage({ command: event.data.command, result: reply }, '*');
}
window.onmessage = async (e) => {
async function messageHandler(e) {
if (e.data.command === 'create_sensor') {
if (sensor) {
postReply(e, 'success');
return;
}
try {
if (!sensor) {
mockBackend = await initialize_generic_sensor_tests();
sensor = new self[e.data.type]();
sensorType = e.data.type;
postReply(e, 'success');
} catch (error) {
postReply(e, error);
}
return Promise.resolve('success');
} else if (e.data.command === 'start_sensor') {
if (!sensor) {
postReply(e, '"create_sensor" must be called first');
return;
return Promise.reject('"create_sensor" must be called first');
}
try {
return new Promise(resolve => {
sensor.addEventListener('reading', () => {
postReply(e, 'success');
resolve('success');
}, { once: true });
sensor.start();
} catch (error) {
postReply(e, error);
}
});
} else if (e.data.command === 'is_sensor_suspended') {
if (!mockBackend) {
postReply(e, '"create_sensor" must be called first');
return;
return Promise.reject('"create_sensor" must be called first');
}
try {
const mockPlatformSensor = await mockBackend.getSensorProvider().getCreatedSensor(sensorType);
postReply(e, !mockPlatformSensor.isReadingData());
} catch (error) {
postReply(e, error);
}
const mockPlatformSensor = await mockBackend.getSensorProvider().getCreatedSensor(sensorType);
return Promise.resolve(!mockPlatformSensor.isReadingData());
} else if (e.data.command === 'reset_sensor_backend') {
if (sensor) {
sensor.stop();
......@@ -61,8 +42,19 @@
sensor = null;
mockBackend = null;
}
return Promise.resolve('success');
} else {
return Promise.reject(`unknown command "${e.data.command}"`);
}
}
postReply(e, 'success');
window.onmessage = async (e) => {
let reply;
try {
reply = await messageHandler(e);
} catch (error) {
reply = error;
}
e.source.postMessage({ command: e.data.command, result: reply }, '*');
}
</script>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment