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) { ...@@ -126,7 +126,6 @@ function run_generic_sensor_iframe_tests(sensorName) {
await sensorWatcher.wait_for('reading'); await sensorWatcher.wait_for('reading');
assert_greater_than(sensor.timestamp, cachedTimeStamp); assert_greater_than(sensor.timestamp, cachedTimeStamp);
sensor.stop(); sensor.stop();
await send_message_to_iframe(iframe, {command: 'reset_sensor_backend'});
}, `${sensorName}: sensor is not suspended when focus traverses from\ }, `${sensorName}: sensor is not suspended when focus traverses from\
to same-origin frame`); to same-origin frame`);
......
...@@ -7,52 +7,33 @@ ...@@ -7,52 +7,33 @@
let sensor = null; let sensor = null;
let sensorType = null; let sensorType = null;
function postReply(event, reply) { async function messageHandler(e) {
event.source.postMessage({ command: event.data.command, result: reply }, '*');
}
window.onmessage = async (e) => {
if (e.data.command === 'create_sensor') { if (e.data.command === 'create_sensor') {
if (sensor) { if (!sensor) {
postReply(e, 'success');
return;
}
try {
mockBackend = await initialize_generic_sensor_tests(); mockBackend = await initialize_generic_sensor_tests();
sensor = new self[e.data.type](); sensor = new self[e.data.type]();
sensorType = 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') { } else if (e.data.command === 'start_sensor') {
if (!sensor) { if (!sensor) {
postReply(e, '"create_sensor" must be called first'); return Promise.reject('"create_sensor" must be called first');
return;
} }
try { return new Promise(resolve => {
sensor.addEventListener('reading', () => { sensor.addEventListener('reading', () => {
postReply(e, 'success'); resolve('success');
}, { once: true }); }, { once: true });
sensor.start(); sensor.start();
} catch (error) { });
postReply(e, error);
}
} else if (e.data.command === 'is_sensor_suspended') { } else if (e.data.command === 'is_sensor_suspended') {
if (!mockBackend) { if (!mockBackend) {
postReply(e, '"create_sensor" must be called first'); return Promise.reject('"create_sensor" must be called first');
return;
} }
try { const mockPlatformSensor = await mockBackend.getSensorProvider().getCreatedSensor(sensorType);
const mockPlatformSensor = await mockBackend.getSensorProvider().getCreatedSensor(sensorType); return Promise.resolve(!mockPlatformSensor.isReadingData());
postReply(e, !mockPlatformSensor.isReadingData());
} catch (error) {
postReply(e, error);
}
} else if (e.data.command === 'reset_sensor_backend') { } else if (e.data.command === 'reset_sensor_backend') {
if (sensor) { if (sensor) {
sensor.stop(); sensor.stop();
...@@ -61,8 +42,19 @@ ...@@ -61,8 +42,19 @@
sensor = null; sensor = null;
mockBackend = 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> </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