Commit 4c8c96b1 authored by Zentaro Kavanagh's avatar Zentaro Kavanagh Committed by Commit Bot

Diagnostics: Add support for observing CPU usage

Bug: 1125150
Test: browser_tests --gtest_filter=DiagnosticsApp*
Change-Id: I1f3d4a7c29fb1bac0f5d4fd2b5ebd42217d79f2c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2420775
Commit-Queue: Zentaro Kavanagh <zentaro@chromium.org>
Reviewed-by: default avatarBailey Berro <baileyberro@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809617}
parent da38db9b
......@@ -7,7 +7,7 @@ import 'chrome://resources/mojo/mojo/public/js/mojo_bindings_lite.js';
import 'chrome://diagnostics/diagnostics_app.js';
import {SystemDataProviderInterface} from 'chrome://diagnostics/diagnostics_types.js';
import {fakeBatteryInfo, fakeSystemInfo} from 'chrome://diagnostics/fake_data.js';
import {fakeBatteryInfo, fakeCpuUsage, fakeSystemInfo} from 'chrome://diagnostics/fake_data.js';
import {FakeMethodResolver} from 'chrome://diagnostics/fake_method_resolver.js';
import {FakeObservables} from 'chrome://diagnostics/fake_observables.js';
import {FakeSystemDataProvider} from 'chrome://diagnostics/fake_system_data_provider.js';
......@@ -373,4 +373,17 @@ suite('FakeSystemDataProviderTest', () => {
assertDeepEquals(fakeBatteryInfo, batteryInfo);
});
});
test('ObserveCpuUsage', () => {
provider.setFakeCpuUsage(fakeCpuUsage);
/** @type {!CpuUsageObserver} */
const cpuObserverRemote = {
onCpuUsageUpdated: (cpuUsage) => {
assertDeepEquals(fakeCpuUsage[0], cpuUsage);
}
};
return provider.observeCpuUsage(cpuObserverRemote);
});
});
......@@ -10,12 +10,19 @@
* re-aliased to the corresponding mojo types, or replaced by them.
*/
/**
* Type of SystemDataProviderInterface.ObserveCpuUsage function.
* @typedef {!function(!CpuUsageObserver): !Promise}
*/
export let ObserveCpuUsageFunction;
/**
* Type alias for the SystemDataProviderInterface.
* TODO(zentaro): Replace with a real mojo type when implemented.
* @typedef {{
* getBatteryInfo: !function(): !Promise<!BatteryInfo>,
* getSystemInfo: !function(): !Promise<!SystemInfo>,
* observeCpuUsage: !ObserveCpuUsageFunction,
* }}
*/
export let SystemDataProviderInterface;
......@@ -57,3 +64,21 @@ export let SystemInfo;
* }}
*/
export let BatteryInfo;
/**
* Type alias for CpuUsageObserver.
* @typedef {{
* onCpuUsageUpdated: !function(!CpuUsage),
* }}
*/
export let CpuUsageObserver;
/**
* Type alias for CpuUsage.
* @typedef {{
* cpu_temp_degrees_celcius: number,
* percent_usage_system: number,
* percent_usage_user: number,
* }}
*/
export let CpuUsage;
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {SystemInfo} from './diagnostics_types.js'
import {CpuUsage, SystemInfo} from './diagnostics_types.js'
/* @type {!BatteryInfo} */
export const fakeBatteryInfo = {
......@@ -10,6 +10,13 @@ export const fakeBatteryInfo = {
manufacturer: 'BatterCorp USA',
};
/* @type {!Array<!CpuUsage>} */
export const fakeCpuUsage = [{
cpu_temp_degrees_celcius: 107,
percent_usage_system: 15,
percent_usage_user: 20,
}];
/* @type {!SystemInfo} */
export const fakeSystemInfo = {
board_name: 'CrOS Board',
......
......@@ -2,8 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {BatteryInfo, SystemInfo} from './diagnostics_types.js';
import {BatteryInfo, CpuUsage, CpuUsageObserver, SystemInfo} from './diagnostics_types.js';
import {FakeMethodResolver} from './fake_method_resolver.js';
import {FakeObservables} from './fake_observables.js';
/**
* @fileoverview
......@@ -15,9 +16,15 @@ export class FakeSystemDataProvider {
/** @private {!FakeMethodResolver} */
this.methods_ = new FakeMethodResolver();
/** @private {!FakeObservables} */
this.observables_ = new FakeObservables();
// Setup method resolvers.
this.methods_.register('getSystemInfo');
this.methods_.register('getBatteryInfo');
// Setup observables.
this.observables_.register('CpuUsageObserver_onCpuUsageUpdated');
}
/**
......@@ -50,4 +57,31 @@ export class FakeSystemDataProvider {
setFakeBatteryInfo(batteryInfo) {
this.methods_.setResult('getBatteryInfo', batteryInfo);
}
/*
* Implements SystemDataProviderInterface.ObserveCpuUsage.
* @param {!CpuUsageObserver} remote
* @return {!Promise}
*/
observeCpuUsage(remote) {
return new Promise((resolve) => {
this.observables_.observe(
'CpuUsageObserver_onCpuUsageUpdated', (cpuUsage) => {
remote.onCpuUsageUpdated(
/** @type {!CpuUsage} */ (cpuUsage));
});
this.observables_.trigger('CpuUsageObserver_onCpuUsageUpdated');
resolve();
});
}
/**
* Sets the values that will observed from observeCpuUsage.
* @param {!Array<!CpuUsage>} cpuUsageList
*/
setFakeCpuUsage(cpuUsageList) {
this.observables_.setObservableData(
'CpuUsageObserver_onCpuUsageUpdated', cpuUsageList);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment