From 7b228c01bc1fcfb642ec4bd79226ea145db7c8a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Z=C3=BCnd?= Date: Fri, 19 Jul 2024 17:16:37 +0200 Subject: [PATCH] [test] Rewrite CPUThrottlingManager tests to use mock connection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Commit-Queue: Simon Zünd --- .../core/sdk/CPUThrottlingManager.test.ts | 64 +++++++++++-------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/front_end/core/sdk/CPUThrottlingManager.test.ts b/front_end/core/sdk/CPUThrottlingManager.test.ts index 438ec1510f..cfc1a8a0aa 100644 --- a/front_end/core/sdk/CPUThrottlingManager.test.ts +++ b/front_end/core/sdk/CPUThrottlingManager.test.ts @@ -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); }); });