Skip to content

Commit

Permalink
Ensure interactions with persistent settings only happen after onload…
Browse files Browse the repository at this point in the history
… (#909)

event is called.

This is the only way to ensure persistent settings are validated before
setting.

b/289240360

Change-Id: I8505da45eb26b421f5b4888bbb3d625c352df306
  • Loading branch information
joeltine authored Jul 13, 2023
1 parent 3218054 commit 0795a37
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
39 changes: 20 additions & 19 deletions cobalt/black_box_tests/testdata/telemetry_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,31 @@
metrics.setMetricEventInterval(EVENT_INTERVAL_SECS);
metrics.onMetricEvent(metricEventHandler);
}
window.onload = function () {
initTelemetry();

initTelemetry();
setupFinished();

setupFinished();

setTimeout(() => {
assertTrue(metrics.isEnabled(), 'metrics should be enabled');
assertEqual('COBALT_UMA', lastMetricType, 'metricType should be uma');
assertTrue(lastPayload.length > 0, 'payload should be non-empty');
assertEqual(1, payloadCount, 'only one payload sent');
}, 4000);
setTimeout(() => {
assertTrue(metrics.isEnabled(), 'metrics should be enabled');
assertEqual('COBALT_UMA', lastMetricType, 'metricType should be uma');
assertTrue(lastPayload.length > 0, 'payload should be non-empty');
assertEqual(1, payloadCount, 'only one payload sent');
}, 4000);

setTimeout(() => {
assertEqual('COBALT_UMA', lastMetricType, 'metricType should be uma');
assertTrue(lastPayload.length > 0, 'payload should be non-empty');
assertEqual(2, payloadCount, 'two payloads sent');
setTimeout(() => {
assertEqual('COBALT_UMA', lastMetricType, 'metricType should be uma');
assertTrue(lastPayload.length > 0, 'payload should be non-empty');
assertEqual(2, payloadCount, 'two payloads sent');

metrics.disable();
metrics.disable();

setTimeout(() => {
assertFalse(metrics.isEnabled(), 'should disable metrics');
onEndTest();
}, 1000);
}, 11000);
setTimeout(() => {
assertFalse(metrics.isEnabled(), 'should disable metrics');
onEndTest();
}, 1000);
}, 11000);
}
</script>
</body>

Expand Down
9 changes: 6 additions & 3 deletions cobalt/demos/content/telemetry/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ <h1>LOG OUTPUT:</h1>
log('Enabling metrics reporting: metrics.enable()');
metrics.enable();
log(`Setting metric event interval: metrics.setMetricEventInterval(${EVENT_INTERVAL_SECS})`);
log(`Note that the first upload interval will always be 60 seconds, regardless of metricEventInterval.`);
metrics.setMetricEventInterval(EVENT_INTERVAL_SECS);
log('Binding metric event handler: metrics.onMetricEvent(metricEventHandler)');
metrics.onMetricEvent(metricEventHandler);
Expand All @@ -62,8 +61,12 @@ <h1>LOG OUTPUT:</h1>
log.textContent = logText;
LOG_CONTAINER.appendChild(log);
}

init();
// Persistent settings are only ready/validated after the browser module
// has fully loaded. As telemetry uses persistent settings, everything
// must happen after onload.
window.onload = function() {
init();
}
</script>
</body>

Expand Down

0 comments on commit 0795a37

Please sign in to comment.