[go: nahoru, domu]

blob: dde92932dd14e7d8d2b65fcd49c947db8dda5fff [file] [log] [blame]
Arnaud Mandy8b1624b2022-09-22 11:58:451// META: script=/resources/test-only-api.js
2// META: script=resources/pressure-helpers.js
Wei4 Wangdd8db51dd2023-04-14 00:56:373// META: global=window,dedicatedworker,sharedworker
Arnaud Mandy8b1624b2022-09-22 11:58:454
5'use strict';
6
7pressure_test(async (t, mockPressureService) => {
Arnaud Mandy1be02822022-09-22 13:40:238 const pressureChanges = await new Promise(async resolve => {
9 const observer_changes = [];
Arnaud Mandy8b1624b2022-09-22 11:58:4510 let n = 0;
Arnaud Mandy1be02822022-09-22 13:40:2311 const observer = new PressureObserver(changes => {
12 observer_changes.push(changes);
Arnaud Mandy8b1624b2022-09-22 11:58:4513 if (++n === 2)
Arnaud Mandy1be02822022-09-22 13:40:2314 resolve(observer_changes);
Wei4 Wang1c72bb62023-01-09 01:44:0915 }, {sampleRate: 5.0});
Arnaud Mandy9369bc12022-09-27 12:54:3616 observer.observe('cpu');
17 const updatesDelivered = mockPressureService.updatesDelivered();
Wei4 Wangb5bd6a692023-04-13 01:13:4518 mockPressureService.setPressureUpdate('cpu', 'critical');
Wei4 Wang1c72bb62023-01-09 01:44:0919 mockPressureService.startPlatformCollector(/*sampleRate*/ 5.0);
Arnaud Mandy6924d072022-10-03 11:04:0820 // Deliver 2 updates.
Arnaud Mandy9369bc12022-09-27 12:54:3621 await t.step_wait(
Arnaud Mandy6924d072022-10-03 11:04:0822 () => mockPressureService.updatesDelivered() >= (updatesDelivered + 2),
Arnaud Mandy9369bc12022-09-27 12:54:3623 'Wait for more than one update to be delivered to the observer');
Wei4 Wangb5bd6a692023-04-13 01:13:4524 mockPressureService.setPressureUpdate('cpu', 'nominal');
Arnaud Mandy9369bc12022-09-27 12:54:3625 // Deliver more updates, |resolve()| will be called when the new pressure
26 // state reaches PressureObserver and its callback is invoked
27 // for the second time.
Arnaud Mandy8b1624b2022-09-22 11:58:4528 });
Arnaud Mandy1be02822022-09-22 13:40:2329 assert_equals(pressureChanges.length, 2);
30 assert_equals(pressureChanges[0][0].state, 'critical');
31 assert_equals(pressureChanges[1][0].state, 'nominal');
Arnaud Mandy9369bc12022-09-27 12:54:3632}, 'Changes that fail the "has change in data" test are discarded.');