From 7ad661def10577dd7eb8bc008029d50442bbea27 Mon Sep 17 00:00:00 2001 From: Wei4 Wang Date: Mon, 6 Mar 2023 13:52:26 +0000 Subject: [PATCH] Bug 1817979 [wpt PR 38601] - [ComputePressure] Improve implementation of focus control, a=testonly Automatic update from web-platform-tests [ComputePressure] Improve implementation of focus control This CL improves implementation of focus control in two ways: 1. Check if FocusController is focused. This can resolve the bug that page not in focus still gets update. 2. Remove IsOutermostMainFrame() check. As discussed in [1], this is not the necessary condition. [1] https://github.com/w3c/compute-pressure/issues/185 Bug: 1403458 Change-Id: I65186b7bd01438ca0c2e5b7e8a09355b5704f8d5 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4273865 Commit-Queue: Wei4 Wang Reviewed-by: Raphael Kubo Da Costa Reviewed-by: Fr Cr-Commit-Position: refs/heads/main@{#1110203} -- wpt-commits: f72c88ab79843a46ffdf63264e833121811f08d9 wpt-pr: 38601 --- ...in_focus_control.tentative.https.window.js | 50 +++++++++++ ...ure_privacy_test.tentative.https.window.js | 87 +++---------------- ...in_focus_control.tentative.https.window.js | 35 ++++++++ 3 files changed, 97 insertions(+), 75 deletions(-) create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_cross_origin_focus_control.tentative.https.window.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_same_origin_focus_control.tentative.https.window.js diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_cross_origin_focus_control.tentative.https.window.js b/testing/web-platform/tests/compute-pressure/compute_pressure_cross_origin_focus_control.tentative.https.window.js new file mode 100644 index 0000000000000..982e4bba0e6cb --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_cross_origin_focus_control.tentative.https.window.js @@ -0,0 +1,50 @@ +// META: timeout=long +// META: script=/common/get-host-info.sub.js + +'use strict'; + +promise_test(t => { + const iframe = document.createElement('iframe'); + iframe.src = get_host_info().HTTPS_REMOTE_ORIGIN + + '/compute-pressure/resources/support-iframe.html'; + document.body.appendChild(iframe); + iframe.contentWindow.focus(); + + const observer = new PressureObserver(() => { + assert_unreached('The observer callback should not be called'); + }); + t.add_cleanup(() => { + observer.disconnect(); + iframe.remove(); + }); + + return new Promise(resolve => t.step_timeout(resolve, 2000)); +}, 'Observer in main frame should not receive PressureRecord when focused on cross-origin iframe'); + +promise_test(async t => { + const iframe = document.createElement('iframe'); + iframe.src = get_host_info().HTTPS_REMOTE_ORIGIN + + '/compute-pressure/resources/support-iframe.html'; + iframe.allow = 'compute-pressure'; + const iframeLoadWatcher = new EventWatcher(t, iframe, 'load'); + document.body.appendChild(iframe); + await iframeLoadWatcher.wait_for('load'); + // Focus on the main frame to make the iframe lose focus, so that + // PressureObserver in the iframe can't receive PressureRecord by default. + // If the main frame has focus, but the iframe is cross-origin with the main + // frame, PressureObserver in the iframe still can't receive PressureRecord. + window.focus(); + + return new Promise((resolve, reject) => { + window.addEventListener('message', (e) => { + if (e.data.result === 'timeout') { + resolve(); + } else if (e.data.result === 'success') { + reject('Observer should not receive PressureRecord'); + } else { + reject('Got unexpected reply'); + } + }, {once: true}); + iframe.contentWindow.postMessage({command: 'start'}, '*'); + }); +}, 'Observer in iframe should not receive PressureRecord when focused on cross-origin main frame'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_privacy_test.tentative.https.window.js b/testing/web-platform/tests/compute-pressure/compute_pressure_privacy_test.tentative.https.window.js index 3a4198f547cc4..ceea886323cdf 100644 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_privacy_test.tentative.https.window.js +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_privacy_test.tentative.https.window.js @@ -16,12 +16,13 @@ promise_test(async t => { assert_not_equals(pipWindow.height, 0); const iframe = document.createElement('iframe'); + iframe.src = get_host_info().HTTPS_REMOTE_ORIGIN + + '/compute-pressure/resources/support-iframe.html'; document.body.appendChild(iframe); - // Focus on the iframe to make the main frame lose focus, so that - // PressureObserver in the main frame can't receive PressureRecord - // by default. However, if the main frame is the initiator of active - // Picture-in-Picture session, PressureObserver in the main frame can - // receive PressureRecord. + // Focus on the cross-origin iframe, so that PressureObserver in the main + // frame can't receive PressureRecord by default. However, if the main + // frame is the initiator of active Picture-in-Picture session, + // PressureObserver in the main frame can receive PressureRecord. iframe.contentWindow.focus(); await new Promise(resolve => { @@ -45,11 +46,13 @@ promise_test(async t => { assert_true(stream.active); const iframe = document.createElement('iframe'); + iframe.src = get_host_info().HTTPS_REMOTE_ORIGIN + + '/compute-pressure/resources/support-iframe.html'; document.body.appendChild(iframe); - // Focus on the iframe to make the main frame lose focus, so that - // PressureObserver in the main frame can't receive PressureRecord - // by default. However, if the main frame's browsing context is capturing, - // PressureObserver in the main frame can receive PressureRecord. + // Focus on the cross-origin iframe, so that PressureObserver in the main + // frame can't receive PressureRecord by default. However, if the main + // frame's browsing context is capturing, PressureObserver in the main + // frame can receive PressureRecord. iframe.contentWindow.focus(); await new Promise(resolve => { @@ -62,69 +65,3 @@ promise_test(async t => { observer.observe('cpu'); }); }, 'Observer should receive PressureRecord if browsing context is capturing'); - -promise_test(async t => { - const iframe = document.createElement('iframe'); - document.body.appendChild(iframe); - // Focus on the iframe to make the main frame lose focus, so that - // PressureObserver in the main frame can't receive PressureRecord - // by default. - iframe.contentWindow.focus(); - - const observer = new PressureObserver(() => { - assert_unreached('The observer callback should not be called'); - }); - t.add_cleanup(() => { - observer.disconnect(); - iframe.remove(); - }); - - return new Promise(resolve => t.step_timeout(resolve, 2000)); -}, 'Observer should not receive PressureRecord when top-level browsing context does not have system focus'); - -promise_test(async t => { - const iframe = document.createElement('iframe'); - document.body.appendChild(iframe); - // Focus on the main frame to make the iframe lose focus, so that - // PressureObserver in the iframe can't receive PressureRecord by default. - // However, if the iframe is same-origin with the main frame and the main - // frame has focus, PressureObserver in iframe can receive PressureRecord. - window.focus(); - - await new Promise(resolve => { - const observer = new iframe.contentWindow.PressureObserver(resolve); - t.add_cleanup(() => { - observer.disconnect(); - iframe.remove(); - }); - observer.observe('cpu'); - }); -}, 'Observer in iframe should receive PressureRecord when focused on same-origin main frame'); - -promise_test(async t => { - const iframe = document.createElement('iframe'); - iframe.src = get_host_info().HTTPS_REMOTE_ORIGIN + - '/compute-pressure/resources/support-iframe.html'; - iframe.allow = 'compute-pressure'; - const iframeLoadWatcher = new EventWatcher(t, iframe, 'load'); - document.body.appendChild(iframe); - await iframeLoadWatcher.wait_for('load'); - // Focus on the main frame to make the iframe lose focus, so that - // PressureObserver in the iframe can't receive PressureRecord by default. - // If the main frame has focus, but the iframe is cross-origin with the main - // frame, PressureObserver in the iframe still can't receive PressureRecord. - window.focus(); - - return new Promise((resolve, reject) => { - window.addEventListener('message', (e) => { - if (e.data.result === 'timeout') { - resolve(); - } else if (e.data.result === 'success') { - reject('Observer should not receive PressureRecord'); - } else { - reject('Got unexpected reply'); - } - }, {once: true}); - iframe.contentWindow.postMessage({command: 'start'}, '*'); - }); -}, 'Observer in iframe should not receive PressureRecord when focused on cross-origin main frame'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_same_origin_focus_control.tentative.https.window.js b/testing/web-platform/tests/compute-pressure/compute_pressure_same_origin_focus_control.tentative.https.window.js new file mode 100644 index 0000000000000..746f119f249c8 --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_same_origin_focus_control.tentative.https.window.js @@ -0,0 +1,35 @@ +'use strict'; + +promise_test(async t => { + const iframe = document.createElement('iframe'); + document.body.appendChild(iframe); + iframe.contentWindow.focus(); + + await new Promise(resolve => { + const observer = new PressureObserver(resolve); + t.add_cleanup(async () => { + observer.disconnect(); + iframe.remove(); + }); + observer.observe('cpu'); + }); +}, 'Observer in main frame should receive PressureRecord when focused on same-origin iframe'); + +promise_test(async t => { + const iframe = document.createElement('iframe'); + document.body.appendChild(iframe); + // Focus on the main frame to make the iframe lose focus, so that + // PressureObserver in the iframe can't receive PressureRecord by default. + // However, if the iframe is same-origin with the main frame and the main + // frame has focus, PressureObserver in iframe can receive PressureRecord. + window.focus(); + + await new Promise(resolve => { + const observer = new iframe.contentWindow.PressureObserver(resolve); + t.add_cleanup(() => { + observer.disconnect(); + iframe.remove(); + }); + observer.observe('cpu'); + }); +}, 'Observer in iframe should receive PressureRecord when focused on same-origin main frame');