[test] Rewrite CPUThrottlingManager tests to use mock connection

We want to get rid of 'describeWithRealConnection' in the long-term.
It's probably enough that CPUThrottlingManager invokes CDP the
right way and uses the corresponding result.

R=pfaffe@chromium.org

Bug: None
Change-Id: I416de5a78ff4c88ade7b0cd48249555ce7a5d4af
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/5725356
Reviewed-by: Philip Pfaffe <pfaffe@chromium.org>
Commit-Queue: Simon Zünd <szuend@chromium.org>
This commit is contained in:
Simon Zünd
2024-07-19 19:00:18 +00:00
committed by Devtools-frontend LUCI CQ
parent 3dba79f655
commit 7b228c01bc
+36 -28
View File
@@ -2,40 +2,48 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {describeWithRealConnection} from '../../testing/RealConnection.js';
import {createTarget} from '../../testing/EnvironmentHelpers.js';
import {describeWithMockConnection, setMockConnectionResponseHandler} from '../../testing/MockConnection.js';
import * as SDK from './sdk.js';
describeWithRealConnection('CPUThrottlingManager', () => {
it('can get the current hardwareConcurrency.', async () => {
const manager = SDK.CPUThrottlingManager.CPUThrottlingManager.instance();
const concurrency = await manager.getHardwareConcurrency();
assert.deepEqual(concurrency, navigator.hardwareConcurrency);
describeWithMockConnection('CPUThrottlingManager', () => {
let target: SDK.Target.Target;
beforeEach(() => {
// We need one target since CPUThrottlingManager gets it from TargetManager.
target = createTarget();
});
it('can set the current hardwareConcurrency.', async () => {
it('can get the current hardwareConcurrency.', async () => {
setMockConnectionResponseHandler('Runtime.evaluate', ({expression}) => {
assert.strictEqual(expression, 'navigator.hardwareConcurrency');
return ({getError: () => undefined, result: {value: 42}});
});
const manager = SDK.CPUThrottlingManager.CPUThrottlingManager.instance();
const concurrency = await manager.getHardwareConcurrency();
assert.strictEqual(concurrency, 42);
});
it('can set the current hardwareConcurrency', async () => {
const cdpStub = sinon.stub(target.emulationAgent(), 'invoke_setHardwareConcurrencyOverride').resolves();
const manager = SDK.CPUThrottlingManager.CPUThrottlingManager.instance();
const nativeConcurrency = navigator.hardwareConcurrency;
manager.setHardwareConcurrency(5);
{
const concurrency = await manager.getHardwareConcurrency();
assert.deepEqual(concurrency, 5);
assert.notDeepEqual(concurrency, nativeConcurrency);
}
{
manager.setHardwareConcurrency(0);
const concurrency = await manager.getHardwareConcurrency();
assert.deepEqual(concurrency, 5);
}
{
manager.setHardwareConcurrency(-1);
const concurrency = await manager.getHardwareConcurrency();
assert.deepEqual(concurrency, 5);
}
{
manager.setHardwareConcurrency(2 * nativeConcurrency);
const concurrency = await manager.getHardwareConcurrency();
assert.deepEqual(concurrency, 2 * nativeConcurrency);
}
assert.isTrue(cdpStub.calledOnce);
assert.isTrue(cdpStub.calledWithExactly({hardwareConcurrency: 5}));
});
it('does not set concurrency to 0 or negative numbers', async () => {
const cdpStub = sinon.stub(target.emulationAgent(), 'invoke_setHardwareConcurrencyOverride').resolves();
const manager = SDK.CPUThrottlingManager.CPUThrottlingManager.instance();
manager.setHardwareConcurrency(0);
assert.isFalse(cdpStub.called);
manager.setHardwareConcurrency(-1);
assert.isFalse(cdpStub.called);
});
});