From da4c7e7ed675c3bf405668739c3012d140856109 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 15 May 2024 05:34:42 +0200 Subject: Adding upstream version 126.0. Signed-off-by: Daniel Baumann --- .../compute_pressure_basic.https.any.js | 93 +++++++++++++++++++++ .../compute_pressure_basic.tentative.https.any.js | 93 --------------------- .../compute_pressure_detached_iframe.https.html | 95 ++++++++++++++++++++++ ...e_pressure_detached_iframe.tentative.https.html | 95 ---------------------- .../compute_pressure_disconnect.https.any.js | 44 ++++++++++ ...pute_pressure_disconnect.tentative.https.any.js | 44 ---------- ...ute_pressure_disconnect_idempotent.https.any.js | 37 +++++++++ ...re_disconnect_idempotent.tentative.https.any.js | 37 --------- ...te_pressure_disconnect_immediately.https.any.js | 67 +++++++++++++++ ...e_disconnect_immediately.tentative.https.any.js | 67 --------------- ...compute_pressure_duplicate_updates.https.any.js | 32 ++++++++ ...essure_duplicate_updates.tentative.https.any.js | 32 -------- .../compute_pressure_multiple.https.any.js | 35 ++++++++ ...ompute_pressure_multiple.tentative.https.any.js | 35 -------- ...ompute_pressure_observe_idempotent.https.any.js | 19 +++++ ...ssure_observe_idempotent.tentative.https.any.js | 19 ----- ...pressure_observe_unobserve_failure.https.any.js | 19 +++++ ...bserve_unobserve_failure.tentative.https.any.js | 19 ----- .../compute_pressure_options.https.any.js | 26 ++++++ ...compute_pressure_options.tentative.https.any.js | 25 ------ ...cation_mitigation_not_triggered.https.window.js | 50 ++++++++++++ ...igation_not_triggered.tentative.https.window.js | 50 ------------ ...bfuscation_mitigation_triggered.https.window.js | 56 +++++++++++++ ..._mitigation_triggered.tentative.https.window.js | 56 ------------- ...compute_pressure_supported_sources.https.any.js | 21 +++++ ...essure_supported_sources.tentative.https.any.js | 21 ----- .../compute_pressure_take_records.https.any.js | 29 +++++++ ...te_pressure_take_records.tentative.https.any.js | 29 ------- .../compute_pressure_timestamp.https.any.js | 75 +++++++++++++++++ ...mpute_pressure_timestamp.tentative.https.any.js | 79 ------------------ .../compute_pressure_update_toJSON.https.any.js | 17 ++++ ...e_pressure_update_toJSON.tentative.https.any.js | 17 ---- .../tests/compute-pressure/idlharness.https.any.js | 2 +- 33 files changed, 716 insertions(+), 719 deletions(-) create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_basic.https.any.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_basic.tentative.https.any.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_detached_iframe.https.html delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_detached_iframe.tentative.https.html create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_disconnect.https.any.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_disconnect.tentative.https.any.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_idempotent.https.any.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_idempotent.tentative.https.any.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_immediately.https.any.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_immediately.tentative.https.any.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_duplicate_updates.https.any.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_duplicate_updates.tentative.https.any.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_multiple.https.any.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_multiple.tentative.https.any.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_observe_idempotent.https.any.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_observe_idempotent.tentative.https.any.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_observe_unobserve_failure.https.any.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_observe_unobserve_failure.tentative.https.any.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_options.https.any.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_options.tentative.https.any.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_not_triggered.https.window.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_not_triggered.tentative.https.window.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_triggered.https.window.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_triggered.tentative.https.window.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_supported_sources.https.any.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_supported_sources.tentative.https.any.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_take_records.https.any.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_take_records.tentative.https.any.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_timestamp.https.any.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_timestamp.tentative.https.any.js create mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_update_toJSON.https.any.js delete mode 100644 testing/web-platform/tests/compute-pressure/compute_pressure_update_toJSON.tentative.https.any.js (limited to 'testing/web-platform/tests/compute-pressure') diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_basic.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_basic.https.any.js new file mode 100644 index 0000000000..15d572bd8e --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_basic.https.any.js @@ -0,0 +1,93 @@ +// META: timeout=long +// META: script=/resources/test-only-api.js +// META: script=resources/pressure-helpers.js +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +pressure_test((t, mockPressureService) => { + const observer = new PressureObserver(() => { + assert_unreached('The observer callback should not be called'); + }); + + mockPressureService.setExpectedFailure( + new DOMException('', 'NotSupportedError')); + return promise_rejects_dom(t, 'NotSupportedError', observer.observe('cpu')); +}, 'Return NotSupportedError when calling observer()'); + +pressure_test(async (t, mockPressureService) => { + const changes = await new Promise(resolve => { + const observer = new PressureObserver(resolve); + t.add_cleanup(() => observer.disconnect()); + observer.observe('cpu'); + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200); + }); + assert_true(changes.length === 1); + assert_equals(changes[0].state, 'critical'); + assert_equals(changes[0].source, 'cpu'); + assert_equals(typeof changes[0].time, 'number'); +}, 'Basic functionality test'); + +pressure_test((t, mockPressureService) => { + const observer = new PressureObserver(() => { + assert_unreached('The observer callback should not be called'); + }); + + const promise = observer.observe('cpu'); + observer.unobserve('cpu'); + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200); + + return promise_rejects_dom(t, 'AbortError', promise); +}, 'Removing observer before observe() resolves works'); + +pressure_test(async (t, mockPressureService) => { + const callbackPromises = []; + const observePromises = []; + + for (let i = 0; i < 2; i++) { + callbackPromises.push(new Promise(resolve => { + const observer = new PressureObserver(resolve); + t.add_cleanup(() => observer.disconnect()); + observePromises.push(observer.observe('cpu')); + })); + } + + await Promise.all(observePromises); + + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200); + + return Promise.all(callbackPromises); +}, 'Calling observe() multiple times works'); + +pressure_test(async (t, mockPressureService) => { + const observer1_changes = []; + await new Promise(resolve => { + const observer1 = new PressureObserver(changes => { + observer1_changes.push(changes); + resolve(); + }); + t.add_cleanup(() => observer1.disconnect()); + observer1.observe('cpu'); + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200); + }); + assert_true(observer1_changes.length === 1); + assert_equals(observer1_changes[0][0].source, 'cpu'); + assert_equals(observer1_changes[0][0].state, 'critical'); + + const observer2_changes = []; + await new Promise(resolve => { + const observer2 = new PressureObserver(changes => { + observer2_changes.push(changes); + resolve(); + }); + t.add_cleanup(() => observer2.disconnect()); + observer2.observe('cpu'); + }); + assert_true(observer2_changes.length === 1); + assert_equals(observer2_changes[0][0].source, 'cpu'); + assert_equals(observer2_changes[0][0].state, 'critical'); +}, 'Starting a new observer after an observer has started works'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_basic.tentative.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_basic.tentative.https.any.js deleted file mode 100644 index 28322ced72..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_basic.tentative.https.any.js +++ /dev/null @@ -1,93 +0,0 @@ -// META: timeout=long -// META: script=/resources/test-only-api.js -// META: script=resources/pressure-helpers.js -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -pressure_test((t, mockPressureService) => { - const observer = new PressureObserver(() => { - assert_unreached('The observer callback should not be called'); - }); - - mockPressureService.setExpectedFailure( - new DOMException('', 'NotSupportedError')); - return promise_rejects_dom(t, 'NotSupportedError', observer.observe('cpu')); -}, 'Return NotSupportedError when calling observer()'); - -pressure_test(async (t, mockPressureService) => { - const changes = await new Promise(resolve => { - const observer = new PressureObserver(resolve); - t.add_cleanup(() => observer.disconnect()); - observer.observe('cpu'); - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0); - }); - assert_true(changes.length === 1); - assert_equals(changes[0].state, 'critical'); - assert_equals(changes[0].source, 'cpu'); - assert_equals(typeof changes[0].time, 'number'); -}, 'Basic functionality test'); - -pressure_test((t, mockPressureService) => { - const observer = new PressureObserver(() => { - assert_unreached('The observer callback should not be called'); - }); - - const promise = observer.observe('cpu'); - observer.unobserve('cpu'); - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0); - - return promise_rejects_dom(t, 'AbortError', promise); -}, 'Removing observer before observe() resolves works'); - -pressure_test(async (t, mockPressureService) => { - const callbackPromises = []; - const observePromises = []; - - for (let i = 0; i < 2; i++) { - callbackPromises.push(new Promise(resolve => { - const observer = new PressureObserver(resolve); - t.add_cleanup(() => observer.disconnect()); - observePromises.push(observer.observe('cpu')); - })); - } - - await Promise.all(observePromises); - - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0); - - return Promise.all(callbackPromises); -}, 'Calling observe() multiple times works'); - -pressure_test(async (t, mockPressureService) => { - const observer1_changes = []; - await new Promise(resolve => { - const observer1 = new PressureObserver(changes => { - observer1_changes.push(changes); - resolve(); - }); - t.add_cleanup(() => observer1.disconnect()); - observer1.observe('cpu'); - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0); - }); - assert_true(observer1_changes.length === 1); - assert_equals(observer1_changes[0][0].source, 'cpu'); - assert_equals(observer1_changes[0][0].state, 'critical'); - - const observer2_changes = []; - await new Promise(resolve => { - const observer2 = new PressureObserver(changes => { - observer2_changes.push(changes); - resolve(); - }); - t.add_cleanup(() => observer2.disconnect()); - observer2.observe('cpu'); - }); - assert_true(observer2_changes.length === 1); - assert_equals(observer2_changes[0][0].source, 'cpu'); - assert_equals(observer2_changes[0][0].state, 'critical'); -}, 'Starting a new observer after an observer has started works'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_detached_iframe.https.html b/testing/web-platform/tests/compute-pressure/compute_pressure_detached_iframe.https.html new file mode 100644 index 0000000000..6123521248 --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_detached_iframe.https.html @@ -0,0 +1,95 @@ + + +PressureObserver on DOMWindow of detached iframe + + + + + + + diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_detached_iframe.tentative.https.html b/testing/web-platform/tests/compute-pressure/compute_pressure_detached_iframe.tentative.https.html deleted file mode 100644 index 5511a14704..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_detached_iframe.tentative.https.html +++ /dev/null @@ -1,95 +0,0 @@ - - -PressureObserver on DOMWindow of detached iframe - - - - - - - diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect.https.any.js new file mode 100644 index 0000000000..f8bc3fb357 --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect.https.any.js @@ -0,0 +1,44 @@ +// META: script=/resources/test-only-api.js +// META: script=resources/pressure-helpers.js +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +test(t => { + const observer = new PressureObserver(() => { + assert_unreached('The observer callback should not be called'); + }); + t.add_cleanup(() => observer.disconnect()); + observer.disconnect(); +}, 'Call disconnect() directly should not crash'); + +pressure_test(async (t, mockPressureService) => { + const observer1_changes = []; + const observer1 = new PressureObserver(change => { + observer1_changes.push(change); + }); + t.add_cleanup(() => observer1.disconnect()); + // Ensure that observer1's schema gets registered before observer2 starts. + await observer1.observe('cpu'); + observer1.disconnect(); + + const observer2_changes = []; + await new Promise((resolve, reject) => { + const observer2 = new PressureObserver(change => { + observer2_changes.push(change); + resolve(); + }); + t.add_cleanup(() => observer2.disconnect()); + observer2.observe('cpu').catch(reject); + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200); + }); + + assert_equals( + observer1_changes.length, 0, + 'disconnected observers should not receive callbacks'); + + assert_equals(observer2_changes.length, 1); + assert_equals(observer2_changes[0].length, 1); + assert_equals(observer2_changes[0][0].state, 'critical'); +}, 'Stopped PressureObserver do not receive changes'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect.tentative.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect.tentative.https.any.js deleted file mode 100644 index 1d188fad8b..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect.tentative.https.any.js +++ /dev/null @@ -1,44 +0,0 @@ -// META: script=/resources/test-only-api.js -// META: script=resources/pressure-helpers.js -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -test(t => { - const observer = new PressureObserver(() => { - assert_unreached('The observer callback should not be called'); - }); - t.add_cleanup(() => observer.disconnect()); - observer.disconnect(); -}, 'Call disconnect() directly should not crash'); - -pressure_test(async (t, mockPressureService) => { - const observer1_changes = []; - const observer1 = new PressureObserver(change => { - observer1_changes.push(change); - }); - t.add_cleanup(() => observer1.disconnect()); - // Ensure that observer1's schema gets registered before observer2 starts. - await observer1.observe('cpu'); - observer1.disconnect(); - - const observer2_changes = []; - await new Promise((resolve, reject) => { - const observer2 = new PressureObserver(change => { - observer2_changes.push(change); - resolve(); - }); - t.add_cleanup(() => observer2.disconnect()); - observer2.observe('cpu').catch(reject); - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0); - }); - - assert_equals( - observer1_changes.length, 0, - 'disconnected observers should not receive callbacks'); - - assert_equals(observer2_changes.length, 1); - assert_equals(observer2_changes[0].length, 1); - assert_equals(observer2_changes[0][0].state, 'critical'); -}, 'Stopped PressureObserver do not receive changes'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_idempotent.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_idempotent.https.any.js new file mode 100644 index 0000000000..3c9a6688a4 --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_idempotent.https.any.js @@ -0,0 +1,37 @@ +// META: script=/resources/test-only-api.js +// META: script=resources/pressure-helpers.js +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +pressure_test(async (t, mockPressureService) => { + const observer1_changes = []; + const observer1 = new PressureObserver(changes => { + observer1_changes.push(changes); + }); + t.add_cleanup(() => observer1.disconnect()); + // Ensure that observer1's schema gets registered before observer2 starts. + const promise = observer1.observe('cpu'); + observer1.disconnect(); + observer1.disconnect(); + await promise_rejects_dom(t, 'AbortError', promise); + + const observer2_changes = []; + await new Promise((resolve, reject) => { + const observer2 = new PressureObserver(changes => { + observer2_changes.push(changes); + resolve(); + }); + t.add_cleanup(() => observer2.disconnect()); + observer2.observe('cpu').catch(reject); + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200); + }); + + assert_equals( + observer1_changes.length, 0, + 'stopped observers should not receive callbacks'); + + assert_equals(observer2_changes.length, 1); + assert_equals(observer2_changes[0][0].state, 'critical'); +}, 'Stopped PressureObserver do not receive changes'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_idempotent.tentative.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_idempotent.tentative.https.any.js deleted file mode 100644 index 74d37bd6e5..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_idempotent.tentative.https.any.js +++ /dev/null @@ -1,37 +0,0 @@ -// META: script=/resources/test-only-api.js -// META: script=resources/pressure-helpers.js -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -pressure_test(async (t, mockPressureService) => { - const observer1_changes = []; - const observer1 = new PressureObserver(changes => { - observer1_changes.push(changes); - }); - t.add_cleanup(() => observer1.disconnect()); - // Ensure that observer1's schema gets registered before observer2 starts. - const promise = observer1.observe('cpu'); - observer1.disconnect(); - observer1.disconnect(); - await promise_rejects_dom(t, 'AbortError', promise); - - const observer2_changes = []; - await new Promise((resolve, reject) => { - const observer2 = new PressureObserver(changes => { - observer2_changes.push(changes); - resolve(); - }); - t.add_cleanup(() => observer2.disconnect()); - observer2.observe('cpu').catch(reject); - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0); - }); - - assert_equals( - observer1_changes.length, 0, - 'stopped observers should not receive callbacks'); - - assert_equals(observer2_changes.length, 1); - assert_equals(observer2_changes[0][0].state, 'critical'); -}, 'Stopped PressureObserver do not receive changes'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_immediately.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_immediately.https.any.js new file mode 100644 index 0000000000..86963e242a --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_immediately.https.any.js @@ -0,0 +1,67 @@ +// META: script=/resources/test-only-api.js +// META: script=resources/pressure-helpers.js +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +pressure_test(async (t, mockPressureService) => { + const observer1_changes = []; + const observer1 = new PressureObserver(changes => { + observer1_changes.push(changes); + }); + t.add_cleanup(() => observer1.disconnect()); + // Ensure that observer1's schema gets registered before observer2 starts. + const promise = observer1.observe('cpu'); + observer1.disconnect(); + await promise_rejects_dom(t, 'AbortError', promise); + + const observer2_changes = []; + await new Promise((resolve, reject) => { + const observer2 = new PressureObserver(changes => { + observer2_changes.push(changes); + resolve(); + }); + t.add_cleanup(() => observer2.disconnect()); + observer2.observe('cpu').catch(reject); + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200); + }); + + assert_equals( + observer1_changes.length, 0, + 'stopped observers should not receive callbacks'); + + assert_equals(observer2_changes.length, 1); + assert_equals(observer2_changes[0].length, 1); + assert_equals(observer2_changes[0][0].state, 'critical'); +}, 'Stopped PressureObserver do not receive changes'); + +pressure_test(async (t, mockPressureService) => { + const observer1_changes = []; + const observer1 = new PressureObserver(changes => { + observer1_changes.push(changes); + }); + t.add_cleanup(() => observer1.disconnect()); + + const observer2_changes = []; + await new Promise(async resolve => { + const observer2 = new PressureObserver(changes => { + observer2_changes.push(changes); + resolve(); + }); + t.add_cleanup(() => observer2.disconnect()); + const promise = observer1.observe('cpu'); + observer2.observe('cpu'); + observer1.disconnect(); + await promise_rejects_dom(t, 'AbortError', promise); + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200); + }); + + assert_equals( + observer1_changes.length, 0, + 'stopped observers should not receive callbacks'); + + assert_equals(observer2_changes.length, 1); + assert_equals(observer2_changes[0][0].state, 'critical'); +}, 'Removing observer before observe() resolves does not affect other observers'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_immediately.tentative.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_immediately.tentative.https.any.js deleted file mode 100644 index 9b545fbe1c..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_disconnect_immediately.tentative.https.any.js +++ /dev/null @@ -1,67 +0,0 @@ -// META: script=/resources/test-only-api.js -// META: script=resources/pressure-helpers.js -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -pressure_test(async (t, mockPressureService) => { - const observer1_changes = []; - const observer1 = new PressureObserver(changes => { - observer1_changes.push(changes); - }); - t.add_cleanup(() => observer1.disconnect()); - // Ensure that observer1's schema gets registered before observer2 starts. - const promise = observer1.observe('cpu'); - observer1.disconnect(); - await promise_rejects_dom(t, 'AbortError', promise); - - const observer2_changes = []; - await new Promise((resolve, reject) => { - const observer2 = new PressureObserver(changes => { - observer2_changes.push(changes); - resolve(); - }); - t.add_cleanup(() => observer2.disconnect()); - observer2.observe('cpu').catch(reject); - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0); - }); - - assert_equals( - observer1_changes.length, 0, - 'stopped observers should not receive callbacks'); - - assert_equals(observer2_changes.length, 1); - assert_equals(observer2_changes[0].length, 1); - assert_equals(observer2_changes[0][0].state, 'critical'); -}, 'Stopped PressureObserver do not receive changes'); - -pressure_test(async (t, mockPressureService) => { - const observer1_changes = []; - const observer1 = new PressureObserver(changes => { - observer1_changes.push(changes); - }); - t.add_cleanup(() => observer1.disconnect()); - - const observer2_changes = []; - await new Promise(async resolve => { - const observer2 = new PressureObserver(changes => { - observer2_changes.push(changes); - resolve(); - }); - t.add_cleanup(() => observer2.disconnect()); - const promise = observer1.observe('cpu'); - observer2.observe('cpu'); - observer1.disconnect(); - await promise_rejects_dom(t, 'AbortError', promise); - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0); - }); - - assert_equals( - observer1_changes.length, 0, - 'stopped observers should not receive callbacks'); - - assert_equals(observer2_changes.length, 1); - assert_equals(observer2_changes[0][0].state, 'critical'); -}, 'Removing observer before observe() resolves does not affect other observers'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_duplicate_updates.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_duplicate_updates.https.any.js new file mode 100644 index 0000000000..04c5df5e57 --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_duplicate_updates.https.any.js @@ -0,0 +1,32 @@ +// META: script=/resources/test-only-api.js +// META: script=resources/pressure-helpers.js +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +pressure_test(async (t, mockPressureService) => { + const pressureChanges = await new Promise(async resolve => { + const observer_changes = []; + let n = 0; + const observer = new PressureObserver(changes => { + observer_changes.push(changes); + if (++n === 2) + resolve(observer_changes); + }, {sampleInterval: 200}); + observer.observe('cpu'); + const updatesDelivered = mockPressureService.updatesDelivered(); + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(/*sampleInterval*/ 200); + // Deliver 2 updates. + await t.step_wait( + () => mockPressureService.updatesDelivered() >= (updatesDelivered + 2), + 'Wait for more than one update to be delivered to the observer'); + mockPressureService.setPressureUpdate('cpu', 'nominal'); + // Deliver more updates, |resolve()| will be called when the new pressure + // state reaches PressureObserver and its callback is invoked + // for the second time. + }); + assert_equals(pressureChanges.length, 2); + assert_equals(pressureChanges[0][0].state, 'critical'); + assert_equals(pressureChanges[1][0].state, 'nominal'); +}, 'Changes that fail the "has change in data" test are discarded.'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_duplicate_updates.tentative.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_duplicate_updates.tentative.https.any.js deleted file mode 100644 index dde92932dd..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_duplicate_updates.tentative.https.any.js +++ /dev/null @@ -1,32 +0,0 @@ -// META: script=/resources/test-only-api.js -// META: script=resources/pressure-helpers.js -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -pressure_test(async (t, mockPressureService) => { - const pressureChanges = await new Promise(async resolve => { - const observer_changes = []; - let n = 0; - const observer = new PressureObserver(changes => { - observer_changes.push(changes); - if (++n === 2) - resolve(observer_changes); - }, {sampleRate: 5.0}); - observer.observe('cpu'); - const updatesDelivered = mockPressureService.updatesDelivered(); - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(/*sampleRate*/ 5.0); - // Deliver 2 updates. - await t.step_wait( - () => mockPressureService.updatesDelivered() >= (updatesDelivered + 2), - 'Wait for more than one update to be delivered to the observer'); - mockPressureService.setPressureUpdate('cpu', 'nominal'); - // Deliver more updates, |resolve()| will be called when the new pressure - // state reaches PressureObserver and its callback is invoked - // for the second time. - }); - assert_equals(pressureChanges.length, 2); - assert_equals(pressureChanges[0][0].state, 'critical'); - assert_equals(pressureChanges[1][0].state, 'nominal'); -}, 'Changes that fail the "has change in data" test are discarded.'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_multiple.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_multiple.https.any.js new file mode 100644 index 0000000000..8c50cc4b3d --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_multiple.https.any.js @@ -0,0 +1,35 @@ +// META: script=/resources/test-only-api.js +// META: script=resources/pressure-helpers.js +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +pressure_test(async (t, mockPressureService) => { + const changes1_promise = new Promise((resolve, reject) => { + const observer = new PressureObserver(resolve); + t.add_cleanup(() => observer.disconnect()); + observer.observe('cpu').catch(reject); + }); + + const changes2_promise = new Promise((resolve, reject) => { + const observer = new PressureObserver(resolve); + t.add_cleanup(() => observer.disconnect()); + observer.observe('cpu').catch(reject); + }); + + const changes3_promise = new Promise((resolve, reject) => { + const observer = new PressureObserver(resolve); + t.add_cleanup(() => observer.disconnect()); + observer.observe('cpu').catch(reject); + }); + + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200); + + const [changes1, changes2, changes3] = + await Promise.all([changes1_promise, changes2_promise, changes3_promise]); + + for (const changes of [changes1, changes2, changes3]) { + assert_equals(changes[0].state, 'critical'); + } +}, 'Three PressureObserver instances receive changes'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_multiple.tentative.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_multiple.tentative.https.any.js deleted file mode 100644 index c8cef5beca..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_multiple.tentative.https.any.js +++ /dev/null @@ -1,35 +0,0 @@ -// META: script=/resources/test-only-api.js -// META: script=resources/pressure-helpers.js -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -pressure_test(async (t, mockPressureService) => { - const changes1_promise = new Promise((resolve, reject) => { - const observer = new PressureObserver(resolve); - t.add_cleanup(() => observer.disconnect()); - observer.observe('cpu').catch(reject); - }); - - const changes2_promise = new Promise((resolve, reject) => { - const observer = new PressureObserver(resolve); - t.add_cleanup(() => observer.disconnect()); - observer.observe('cpu').catch(reject); - }); - - const changes3_promise = new Promise((resolve, reject) => { - const observer = new PressureObserver(resolve); - t.add_cleanup(() => observer.disconnect()); - observer.observe('cpu').catch(reject); - }); - - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0); - - const [changes1, changes2, changes3] = - await Promise.all([changes1_promise, changes2_promise, changes3_promise]); - - for (const changes of [changes1, changes2, changes3]) { - assert_equals(changes[0].state, 'critical'); - } -}, 'Three PressureObserver instances receive changes'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_observe_idempotent.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_observe_idempotent.https.any.js new file mode 100644 index 0000000000..9fcbb49814 --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_observe_idempotent.https.any.js @@ -0,0 +1,19 @@ +// META: script=/resources/test-only-api.js +// META: script=resources/pressure-helpers.js +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +pressure_test(async (t, mockPressureService) => { + const update = await new Promise((resolve, reject) => { + const observer = new PressureObserver(resolve); + t.add_cleanup(() => observer.disconnect()); + observer.observe('cpu').catch(reject); + observer.observe('cpu').catch(reject); + observer.observe('cpu').catch(reject); + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200); + }); + + assert_equals(update[0].state, 'critical'); +}, 'PressureObserver.observe() is idempotent'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_observe_idempotent.tentative.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_observe_idempotent.tentative.https.any.js deleted file mode 100644 index 5dc3804b2f..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_observe_idempotent.tentative.https.any.js +++ /dev/null @@ -1,19 +0,0 @@ -// META: script=/resources/test-only-api.js -// META: script=resources/pressure-helpers.js -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -pressure_test(async (t, mockPressureService) => { - const update = await new Promise((resolve, reject) => { - const observer = new PressureObserver(resolve); - t.add_cleanup(() => observer.disconnect()); - observer.observe('cpu').catch(reject); - observer.observe('cpu').catch(reject); - observer.observe('cpu').catch(reject); - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0); - }); - - assert_equals(update[0].state, 'critical'); -}, 'PressureObserver.observe() is idempotent'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_observe_unobserve_failure.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_observe_unobserve_failure.https.any.js new file mode 100644 index 0000000000..8eafeb356d --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_observe_unobserve_failure.https.any.js @@ -0,0 +1,19 @@ +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +promise_test(async t => { + const observer = + new PressureObserver(t.unreached_func('oops should not end up here')); + t.add_cleanup(() => observer.disconnect()); + await promise_rejects_js(t, TypeError, observer.observe('random')); +}, 'PressureObserver.observe() requires a valid source'); + +test(t => { + const observer = + new PressureObserver(t.unreached_func('oops should not end up here')); + t.add_cleanup(() => observer.disconnect()); + assert_throws_js(TypeError, () => { + observer.unobserve('random'); + }); +}, 'PressureObserver.unobserve() requires a valid source'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_observe_unobserve_failure.tentative.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_observe_unobserve_failure.tentative.https.any.js deleted file mode 100644 index 8eafeb356d..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_observe_unobserve_failure.tentative.https.any.js +++ /dev/null @@ -1,19 +0,0 @@ -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -promise_test(async t => { - const observer = - new PressureObserver(t.unreached_func('oops should not end up here')); - t.add_cleanup(() => observer.disconnect()); - await promise_rejects_js(t, TypeError, observer.observe('random')); -}, 'PressureObserver.observe() requires a valid source'); - -test(t => { - const observer = - new PressureObserver(t.unreached_func('oops should not end up here')); - t.add_cleanup(() => observer.disconnect()); - assert_throws_js(TypeError, () => { - observer.unobserve('random'); - }); -}, 'PressureObserver.unobserve() requires a valid source'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_options.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_options.https.any.js new file mode 100644 index 0000000000..d0760ef622 --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_options.https.any.js @@ -0,0 +1,26 @@ +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +test(t => { + const observer = new PressureObserver(() => {}, {sampleInterval: 0}); + assert_equals(typeof observer, 'object'); +}, 'PressureObserver constructor doesnt throw error for sampleInterval value 0'); + + +test(t => { + assert_throws_js(TypeError, () => { + new PressureObserver(() => {}, {sampleInterval: -2}); + }); +}, 'PressureObserver constructor requires a positive sampleInterval'); + +test(t => { + assert_throws_js(TypeError, () => { + new PressureObserver(() => {}, {sampleInterval: 2 ** 32}); + }); +}, 'PressureObserver constructor requires a sampleInterval in unsigned long range'); + +test(t => { + const observer = new PressureObserver(() => {}, {}); + assert_equals(typeof observer, 'object'); +}, 'PressureObserver constructor succeeds on empty sampleInterval'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_options.tentative.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_options.tentative.https.any.js deleted file mode 100644 index 69999819d9..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_options.tentative.https.any.js +++ /dev/null @@ -1,25 +0,0 @@ -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -test(t => { - assert_throws_js(RangeError, () => { - new PressureObserver(() => {}, {sampleRate: 0}); - }); -}, 'PressureObserver constructor requires a non-zero sampleRate'); - -test(t => { - assert_throws_js(RangeError, () => { - new PressureObserver(() => {}, {sampleRate: -2}); - }); -}, 'PressureObserver constructor requires a positive sampleRate'); - -test(t => { - const observer = new PressureObserver(() => {}, {sampleRate: 0.5}); - assert_equals(typeof observer, 'object'); -}, 'PressureObserver constructor doesnt throw error on positive sampleRate'); - -test(t => { - const observer = new PressureObserver(() => {}, {}); - assert_equals(typeof observer, 'object'); -}, 'PressureObserver constructor succeeds on empty sampleRate'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_not_triggered.https.window.js b/testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_not_triggered.https.window.js new file mode 100644 index 0000000000..e348a8ea08 --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_not_triggered.https.window.js @@ -0,0 +1,50 @@ +// META: timeout=long +// META: script=/resources/test-only-api.js +// META: script=resources/pressure-helpers.js +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +pressure_test(async (t, mockPressureService) => { + const sampleIntervalInMs = 100; + const readings = ['nominal', 'fair', 'serious', 'critical']; + // Normative values for rate obfuscation parameters. + // https://w3c.github.io/compute-pressure/#rate-obfuscation-normative-parameters. + const minPenaltyTimeInMs = 5000; + const minChangesThreshold = 50; + + const changes = await new Promise(async resolve => { + const observerChanges = []; + const observer = new PressureObserver(changes => { + observerChanges.push(changes); + }, {sampleInterval: sampleIntervalInMs}); + + observer.observe('cpu'); + mockPressureService.startPlatformCollector(sampleIntervalInMs); + let i = 0; + // mockPressureService.updatesDelivered() does not necessarily match + // pressureChanges.length, as system load and browser optimizations can + // cause the actual timer used by mockPressureService to deliver readings + // to be a bit slower or faster than requested. + while (observerChanges.length < minChangesThreshold) { + mockPressureService.setPressureUpdate( + 'cpu', readings[i++ % readings.length]); + // Allow tasks to run (avoid a micro-task loop). + await new Promise((resolve) => t.step_timeout(resolve, 0)); + await t.step_wait( + () => mockPressureService.updatesDelivered() >= i, + `At least ${i} readings have been delivered`); + } + observer.disconnect(); + resolve(observerChanges); + }); + assert_equals(changes.length, minChangesThreshold); + + for (let i = 0; i < (changes.length - 1); i++) { + // Because no penalty should be triggered, the timestamp difference + // between samples should be less than the minimum penalty. + assert_less_than( + changes[i + 1][0].time - changes[i][0].time, minPenaltyTimeInMs, + 'Not in sample time boundaries'); + } +}, 'No rate obfuscation mitigation should happen, when number of changes is below minimum changes before penalty'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_not_triggered.tentative.https.window.js b/testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_not_triggered.tentative.https.window.js deleted file mode 100644 index cb1aa432ce..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_not_triggered.tentative.https.window.js +++ /dev/null @@ -1,50 +0,0 @@ -// META: timeout=long -// META: script=/resources/test-only-api.js -// META: script=resources/pressure-helpers.js -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -pressure_test(async (t, mockPressureService) => { - const sampleRateInHz = 10; - const readings = ['nominal', 'fair', 'serious', 'critical']; - // Normative values for rate obfuscation parameters. - // https://w3c.github.io/compute-pressure/#rate-obfuscation-normative-parameters. - const minPenaltyTimeInMs = 5000; - const minChangesThreshold = 50; - - const changes = await new Promise(async resolve => { - const observerChanges = []; - const observer = new PressureObserver(changes => { - observerChanges.push(changes); - }, {sampleRate: sampleRateInHz}); - - observer.observe('cpu'); - mockPressureService.startPlatformCollector(sampleRateInHz); - let i = 0; - // mockPressureService.updatesDelivered() does not necessarily match - // pressureChanges.length, as system load and browser optimizations can - // cause the actual timer used by mockPressureService to deliver readings - // to be a bit slower or faster than requested. - while (observerChanges.length < minChangesThreshold) { - mockPressureService.setPressureUpdate( - 'cpu', readings[i++ % readings.length]); - // Allow tasks to run (avoid a micro-task loop). - await new Promise((resolve) => t.step_timeout(resolve, 0)); - await t.step_wait( - () => mockPressureService.updatesDelivered() >= i, - `At least ${i} readings have been delivered`); - } - observer.disconnect(); - resolve(observerChanges); - }); - assert_equals(changes.length, minChangesThreshold); - - for (let i = 0; i < (changes.length - 1); i++) { - // Because no penalty should be triggered, the timestamp difference - // between samples should be less than the minimum penalty. - assert_less_than( - changes[i + 1][0].time - changes[i][0].time, minPenaltyTimeInMs, - 'Not in sample time boundaries'); - } -}, 'No rate obfuscation mitigation should happen, when number of changes is below minimum changes before penalty'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_triggered.https.window.js b/testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_triggered.https.window.js new file mode 100644 index 0000000000..ebe33bc8bf --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_triggered.https.window.js @@ -0,0 +1,56 @@ +// META: timeout=long +// META: script=/resources/test-only-api.js +// META: script=resources/pressure-helpers.js +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +pressure_test(async (t, mockPressureService) => { + const sampleIntervalInMs = 40; + const readings = ['nominal', 'fair', 'serious', 'critical']; + // Normative values for rate obfuscation parameters. + // https://w3c.github.io/compute-pressure/#rate-obfuscation-normative-parameters. + const minPenaltyTimeInMs = 5000; + const maxChangesThreshold = 100; + const minChangesThreshold = 50; + let gotPenalty = false; + await new Promise(async resolve => { + const observerChanges = []; + const observer = new PressureObserver(changes => { + if (observerChanges.length >= (minChangesThreshold - 1)) { + const lastSample = observerChanges.at(-1); + if ((changes[0].time - lastSample[0].time) >= minPenaltyTimeInMs) { + // The update delivery might still be working even if + // maxChangesThreshold have been reached and before disconnect() is + // processed. + // Therefore we are adding a flag to dismiss any updates after the + // penalty is detected, which is the condition for the test to pass. + gotPenalty = true; + observer.disconnect(); + resolve(); + } + } + observerChanges.push(changes); + }, {sampleInterval: sampleIntervalInMs}); + + observer.observe('cpu'); + mockPressureService.startPlatformCollector(sampleIntervalInMs); + let i = 0; + // mockPressureService.updatesDelivered() does not necessarily match + // pressureChanges.length, as system load and browser optimizations can + // cause the actual timer used by mockPressureService to deliver readings + // to be a bit slower or faster than requested. + while (observerChanges.length <= maxChangesThreshold || !gotPenalty) { + mockPressureService.setPressureUpdate( + 'cpu', readings[i++ % readings.length]); + // Allow tasks to run (avoid a micro-task loop). + await new Promise((resolve) => t.step_timeout(resolve, 0)); + await t.step_wait( + () => mockPressureService.updatesDelivered() >= i, + `At least ${i} readings have been delivered`); + } + + assert_true(gotPenalty, 'Penalty not triggered'); + + }); +}, 'Rate obfuscation mitigation should have been triggered, when changes is higher than minimum changes before penalty'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_triggered.tentative.https.window.js b/testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_triggered.tentative.https.window.js deleted file mode 100644 index 11dcc3c70a..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_rate_obfuscation_mitigation_triggered.tentative.https.window.js +++ /dev/null @@ -1,56 +0,0 @@ -// META: timeout=long -// META: script=/resources/test-only-api.js -// META: script=resources/pressure-helpers.js -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -pressure_test(async (t, mockPressureService) => { - const sampleRateInHz = 25; - const readings = ['nominal', 'fair', 'serious', 'critical']; - // Normative values for rate obfuscation parameters. - // https://w3c.github.io/compute-pressure/#rate-obfuscation-normative-parameters. - const minPenaltyTimeInMs = 5000; - const maxChangesThreshold = 100; - const minChangesThreshold = 50; - let gotPenalty = false; - await new Promise(async resolve => { - const observerChanges = []; - const observer = new PressureObserver(changes => { - if (observerChanges.length >= (minChangesThreshold - 1)) { - const lastSample = observerChanges.at(-1); - if ((changes[0].time - lastSample[0].time) >= minPenaltyTimeInMs) { - // The update delivery might still be working even if - // maxChangesThreshold have been reached and before disconnect() is - // processed. - // Therefore we are adding a flag to dismiss any updates after the - // penalty is detected, which is the condition for the test to pass. - gotPenalty = true; - observer.disconnect(); - resolve(); - } - } - observerChanges.push(changes); - }, {sampleRate: sampleRateInHz}); - - observer.observe('cpu'); - mockPressureService.startPlatformCollector(sampleRateInHz); - let i = 0; - // mockPressureService.updatesDelivered() does not necessarily match - // pressureChanges.length, as system load and browser optimizations can - // cause the actual timer used by mockPressureService to deliver readings - // to be a bit slower or faster than requested. - while (observerChanges.length <= maxChangesThreshold || !gotPenalty) { - mockPressureService.setPressureUpdate( - 'cpu', readings[i++ % readings.length]); - // Allow tasks to run (avoid a micro-task loop). - await new Promise((resolve) => t.step_timeout(resolve, 0)); - await t.step_wait( - () => mockPressureService.updatesDelivered() >= i, - `At least ${i} readings have been delivered`); - } - - assert_true(gotPenalty, 'Penalty not triggered'); - - }); -}, 'Rate obfuscation mitigation should have been triggered, when changes is higher than minimum changes before penalty'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_supported_sources.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_supported_sources.https.any.js new file mode 100644 index 0000000000..63f2666cca --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_supported_sources.https.any.js @@ -0,0 +1,21 @@ +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +test(() => { + // Compute Pressure should support at least "cpu" + const sources = PressureObserver.supportedSources; + assert_in_array('cpu', sources); +}, 'PressureObserver should support at least "cpu"'); + +test(() => { + // Compute Pressure should be frozen array + const sources = PressureObserver.supportedSources; + assert_equals(sources, PressureObserver.supportedSources); +}, 'PressureObserver must return always the same array'); + +test(() => { + // Compute Pressure should be frozen array + let sources = PressureObserver.supportedSources; + assert_equals(Object.isFrozen(), true); +}, 'PressureObserver must return a frozen array'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_supported_sources.tentative.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_supported_sources.tentative.https.any.js deleted file mode 100644 index 63f2666cca..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_supported_sources.tentative.https.any.js +++ /dev/null @@ -1,21 +0,0 @@ -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -test(() => { - // Compute Pressure should support at least "cpu" - const sources = PressureObserver.supportedSources; - assert_in_array('cpu', sources); -}, 'PressureObserver should support at least "cpu"'); - -test(() => { - // Compute Pressure should be frozen array - const sources = PressureObserver.supportedSources; - assert_equals(sources, PressureObserver.supportedSources); -}, 'PressureObserver must return always the same array'); - -test(() => { - // Compute Pressure should be frozen array - let sources = PressureObserver.supportedSources; - assert_equals(Object.isFrozen(), true); -}, 'PressureObserver must return a frozen array'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_take_records.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_take_records.https.any.js new file mode 100644 index 0000000000..55660b228b --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_take_records.https.any.js @@ -0,0 +1,29 @@ +// META: script=/resources/test-only-api.js +// META: script=resources/pressure-helpers.js +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +test(t => { + const observer = new PressureObserver( + t.unreached_func('This callback should not have been called.')); + + const records = observer.takeRecords(); + assert_equals(records.length, 0, 'No record before observe'); +}, 'Calling takeRecords() before observe()'); + +pressure_test(async (t, mockPressureService) => { + let observer; + const changes = await new Promise(resolve => { + observer = new PressureObserver(resolve); + t.add_cleanup(() => observer.disconnect()); + + observer.observe('cpu'); + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200); + }); + assert_equals(changes[0].state, 'critical'); + + const records = observer.takeRecords(); + assert_equals(records.length, 0, 'No record available'); +}, 'takeRecords() returns empty record after callback invoke'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_take_records.tentative.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_take_records.tentative.https.any.js deleted file mode 100644 index d93c9b5c88..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_take_records.tentative.https.any.js +++ /dev/null @@ -1,29 +0,0 @@ -// META: script=/resources/test-only-api.js -// META: script=resources/pressure-helpers.js -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -test(t => { - const observer = new PressureObserver( - t.unreached_func('This callback should not have been called.')); - - const records = observer.takeRecords(); - assert_equals(records.length, 0, 'No record before observe'); -}, 'Calling takeRecords() before observe()'); - -pressure_test(async (t, mockPressureService) => { - let observer; - const changes = await new Promise(resolve => { - observer = new PressureObserver(resolve); - t.add_cleanup(() => observer.disconnect()); - - observer.observe('cpu'); - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0); - }); - assert_equals(changes[0].state, 'critical'); - - const records = observer.takeRecords(); - assert_equals(records.length, 0, 'No record available'); -}, 'takeRecords() returns empty record after callback invoke'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_timestamp.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_timestamp.https.any.js new file mode 100644 index 0000000000..09caeb3478 --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_timestamp.https.any.js @@ -0,0 +1,75 @@ +// META: script=/resources/test-only-api.js +// META: script=resources/pressure-helpers.js +// META: global=window,dedicatedworker,sharedworker + +'use strict'; + +pressure_test(async (t, mockPressureService) => { + const readings = ['nominal', 'fair', 'serious', 'critical']; + + const sampleInterval = 250; + const pressureChanges = await new Promise(async resolve => { + const observerChanges = []; + const observer = new PressureObserver(changes => { + observerChanges.push(changes); + }, {sampleInterval}); + observer.observe('cpu'); + + mockPressureService.startPlatformCollector(sampleInterval / 2); + let i = 0; + // mockPressureService.updatesDelivered() does not necessarily match + // pressureChanges.length, as system load and browser optimizations can + // cause the actual timer used by mockPressureService to deliver readings + // to be a bit slower or faster than requested. + while (observerChanges.length < 4) { + mockPressureService.setPressureUpdate( + 'cpu', readings[i++ % readings.length]); + await t.step_wait( + () => mockPressureService.updatesDelivered() >= i, + `At least ${i} readings have been delivered`); + } + observer.disconnect(); + resolve(observerChanges); + }); + + assert_equals(pressureChanges.length, 4); + assert_greater_than_equal( + pressureChanges[1][0].time - pressureChanges[0][0].time, sampleInterval); + assert_greater_than_equal( + pressureChanges[2][0].time - pressureChanges[1][0].time, sampleInterval); + assert_greater_than_equal( + pressureChanges[3][0].time - pressureChanges[2][0].time, sampleInterval); +}, 'Faster collector: Timestamp difference between two changes should be higher or equal to the observer sample rate'); + +pressure_test(async (t, mockPressureService) => { + const pressureChanges = []; + const sampleInterval = 1000; + const observer = new PressureObserver(changes => { + pressureChanges.push(changes); + }, {sampleInterval}); + + await new Promise(async resolve => { + observer.observe('cpu'); + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(sampleInterval); + await t.step_wait(() => pressureChanges.length == 1); + observer.disconnect(); + resolve(); + }); + + await new Promise(async resolve => { + observer.observe('cpu'); + mockPressureService.setPressureUpdate('cpu', 'serious'); + mockPressureService.startPlatformCollector(sampleInterval / 4); + await t.step_wait(() => pressureChanges.length == 2); + observer.disconnect(); + resolve(); + }); + + assert_equals(pressureChanges.length, 2); + // When disconnect() is called, PressureRecord in [[LastRecordMap]] for cpu + // should be deleted. So the second PressureRecord is not discarded even + // though the time interval does not meet the requirement. + assert_less_than( + pressureChanges[1][0].time - pressureChanges[0][0].time, sampleInterval); +}, 'disconnect() should update [[LastRecordMap]]'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_timestamp.tentative.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_timestamp.tentative.https.any.js deleted file mode 100644 index f283caa6ba..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_timestamp.tentative.https.any.js +++ /dev/null @@ -1,79 +0,0 @@ -// META: script=/resources/test-only-api.js -// META: script=resources/pressure-helpers.js -// META: global=window,dedicatedworker,sharedworker - -'use strict'; - -pressure_test(async (t, mockPressureService) => { - const readings = ['nominal', 'fair', 'serious', 'critical']; - - const sampleRate = 4.0; - const pressureChanges = await new Promise(async resolve => { - const observerChanges = []; - const observer = new PressureObserver(changes => { - observerChanges.push(changes); - }, {sampleRate}); - observer.observe('cpu'); - - mockPressureService.startPlatformCollector(sampleRate * 2); - let i = 0; - // mockPressureService.updatesDelivered() does not necessarily match - // pressureChanges.length, as system load and browser optimizations can - // cause the actual timer used by mockPressureService to deliver readings - // to be a bit slower or faster than requested. - while (observerChanges.length < 4) { - mockPressureService.setPressureUpdate( - 'cpu', readings[i++ % readings.length]); - await t.step_wait( - () => mockPressureService.updatesDelivered() >= i, - `At least ${i} readings have been delivered`); - } - observer.disconnect(); - resolve(observerChanges); - }); - - assert_equals(pressureChanges.length, 4); - assert_greater_than_equal( - pressureChanges[1][0].time - pressureChanges[0][0].time, - (1 / sampleRate * 1000)); - assert_greater_than_equal( - pressureChanges[2][0].time - pressureChanges[1][0].time, - (1 / sampleRate * 1000)); - assert_greater_than_equal( - pressureChanges[3][0].time - pressureChanges[2][0].time, - (1 / sampleRate * 1000)); -}, 'Faster collector: Timestamp difference between two changes should be higher or equal to the observer sample rate'); - -pressure_test(async (t, mockPressureService) => { - const pressureChanges = []; - const sampleRate = 1.0; - const observer = new PressureObserver(changes => { - pressureChanges.push(changes); - }, {sampleRate}); - - await new Promise(async resolve => { - observer.observe('cpu'); - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(sampleRate); - await t.step_wait(() => pressureChanges.length == 1); - observer.disconnect(); - resolve(); - }); - - await new Promise(async resolve => { - observer.observe('cpu'); - mockPressureService.setPressureUpdate('cpu', 'serious'); - mockPressureService.startPlatformCollector(sampleRate * 4); - await t.step_wait(() => pressureChanges.length == 2); - observer.disconnect(); - resolve(); - }); - - assert_equals(pressureChanges.length, 2); - // When disconnect() is called, PressureRecord in [[LastRecordMap]] for cpu - // should be deleted. So the second PressureRecord is not discarded even - // though the time interval does not meet the requirement. - assert_less_than( - pressureChanges[1][0].time - pressureChanges[0][0].time, - (1 / sampleRate * 1000)); -}, 'disconnect() should update [[LastRecordMap]]'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_update_toJSON.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_update_toJSON.https.any.js new file mode 100644 index 0000000000..7f726698d6 --- /dev/null +++ b/testing/web-platform/tests/compute-pressure/compute_pressure_update_toJSON.https.any.js @@ -0,0 +1,17 @@ +// META: script=/resources/test-only-api.js +// META: script=resources/pressure-helpers.js +// META: global=window,dedicatedworker,sharedworker + +pressure_test(async (t, mockPressureService) => { + const changes = await new Promise(resolve => { + const observer = new PressureObserver(resolve); + observer.observe('cpu'); + mockPressureService.setPressureUpdate('cpu', 'critical'); + mockPressureService.startPlatformCollector(/*sampleInterval=*/ 200); + }); + assert_true(changes.length === 1); + const json = changes[0].toJSON(); + assert_equals(json.state, 'critical'); + assert_equals(json.source, 'cpu'); + assert_equals(typeof json.time, 'number'); +}, 'Basic functionality test'); diff --git a/testing/web-platform/tests/compute-pressure/compute_pressure_update_toJSON.tentative.https.any.js b/testing/web-platform/tests/compute-pressure/compute_pressure_update_toJSON.tentative.https.any.js deleted file mode 100644 index 0024d69754..0000000000 --- a/testing/web-platform/tests/compute-pressure/compute_pressure_update_toJSON.tentative.https.any.js +++ /dev/null @@ -1,17 +0,0 @@ -// META: script=/resources/test-only-api.js -// META: script=resources/pressure-helpers.js -// META: global=window,dedicatedworker,sharedworker - -pressure_test(async (t, mockPressureService) => { - const changes = await new Promise(resolve => { - const observer = new PressureObserver(resolve); - observer.observe('cpu'); - mockPressureService.setPressureUpdate('cpu', 'critical'); - mockPressureService.startPlatformCollector(/*sampleRate=*/ 5.0); - }); - assert_true(changes.length === 1); - const json = changes[0].toJSON(); - assert_equals(json.state, 'critical'); - assert_equals(json.source, 'cpu'); - assert_equals(typeof json.time, 'number'); -}, 'Basic functionality test'); diff --git a/testing/web-platform/tests/compute-pressure/idlharness.https.any.js b/testing/web-platform/tests/compute-pressure/idlharness.https.any.js index e828996232..48ab5615b0 100644 --- a/testing/web-platform/tests/compute-pressure/idlharness.https.any.js +++ b/testing/web-platform/tests/compute-pressure/idlharness.https.any.js @@ -11,5 +11,5 @@ idl_test(['compute-pressure'], ['dom', 'html'], async idl_array => { PressureObserver: ['observer'], }); - self.observer = new PressureObserver(() => {}, {sampleRate: 1.0}); + self.observer = new PressureObserver(() => {}, {sampleInterval: 1000}); }); -- cgit v1.2.3