From 1217f2253b6f8fda06e371c15c5ae092e9d8dff8 Mon Sep 17 00:00:00 2001 From: Wei4 Wang Date: Tue, 21 Feb 2023 18:11:37 -0800 Subject: [PATCH] [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 --- ...ure_privacy_test.tentative.https.window.js | 73 ++++++++++++------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/compute-pressure/compute_pressure_privacy_test.tentative.https.window.js b/compute-pressure/compute_pressure_privacy_test.tentative.https.window.js index 3a4198f547cc4b5..141a791713ae5e2 100644 --- a/compute-pressure/compute_pressure_privacy_test.tentative.https.window.js +++ b/compute-pressure/compute_pressure_privacy_test.tentative.https.window.js @@ -8,6 +8,39 @@ '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'); + 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 video = await loadVideo(); document.body.appendChild(video); @@ -16,12 +49,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 +79,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 => { @@ -63,25 +99,6 @@ promise_test(async t => { }); }, '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);