Commit 571684fa authored by Zentaro Kavanagh's avatar Zentaro Kavanagh Committed by Commit Bot

Diagnostics: Support firing observables on a timer

Bug: 1125150
Test: browser_tests --gtest_filter=DiagnosticsApp*
Change-Id: If4bdef02e6b91446014916a4ec6ae51f97ae3cb9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2425424Reviewed-by: default avatarBailey Berro <baileyberro@chromium.org>
Commit-Queue: Zentaro Kavanagh <zentaro@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810358}
parent 61de461c
...@@ -5,13 +5,29 @@ ...@@ -5,13 +5,29 @@
import {BatteryChargeStatus, BatteryHealth, CpuUsage, ExternalPowerSource, MemoryUsage, SystemInfo} from './diagnostics_types.js' import {BatteryChargeStatus, BatteryHealth, CpuUsage, ExternalPowerSource, MemoryUsage, SystemInfo} from './diagnostics_types.js'
/* @type {!Array<!BatteryChargeStatus>} */ /* @type {!Array<!BatteryChargeStatus>} */
export const fakeBatteryChargeStatus = [{ export const fakeBatteryChargeStatus = [
charge_full_now_milliamp_hours: 6000, {
charge_full_now_milliamp_hours: 5700,
charge_now_milliamp_hours: 4200, charge_now_milliamp_hours: 4200,
current_now_milliamps: 1123, current_now_milliamps: 1123,
power_adapter_status: ExternalPowerSource.kAc, power_adapter_status: ExternalPowerSource.kAc,
power_time: '3h 15m', power_time: '3h 15m',
}]; },
{
charge_full_now_milliamp_hours: 5700,
charge_now_milliamp_hours: 4500,
current_now_milliamps: 1123,
power_adapter_status: ExternalPowerSource.kAc,
power_time: '3h 01m',
},
{
charge_full_now_milliamp_hours: 5700,
charge_now_milliamp_hours: 4800,
current_now_milliamps: 1123,
power_adapter_status: ExternalPowerSource.kAc,
power_time: '2h 45m',
}
];
/* @type {!Array<!BatteryHealth>} */ /* @type {!Array<!BatteryHealth>} */
export const fakeBatteryHealth = [{ export const fakeBatteryHealth = [{
...@@ -28,18 +44,52 @@ export const fakeBatteryInfo = { ...@@ -28,18 +44,52 @@ export const fakeBatteryInfo = {
}; };
/* @type {!Array<!CpuUsage>} */ /* @type {!Array<!CpuUsage>} */
export const fakeCpuUsage = [{ export const fakeCpuUsage = [
{
cpu_temp_degrees_celcius: 107, cpu_temp_degrees_celcius: 107,
percent_usage_system: 15, percent_usage_system: 15,
percent_usage_user: 20, percent_usage_user: 20,
}]; },
{
cpu_temp_degrees_celcius: 106,
percent_usage_system: 30,
percent_usage_user: 40,
},
{
cpu_temp_degrees_celcius: 107,
percent_usage_system: 31,
percent_usage_user: 45,
},
{
cpu_temp_degrees_celcius: 109,
percent_usage_system: 55,
percent_usage_user: 24,
}
];
/* @type {!Array<!MemoryUsage>} */ /* @type {!Array<!MemoryUsage>} */
export const fakeMemoryUsage = [{ export const fakeMemoryUsage = [
{
available_memory_kib: 57000, available_memory_kib: 57000,
free_memory_kib: 15000, free_memory_kib: 15000,
total_memory_kib: 128000, total_memory_kib: 128000,
}]; },
{
available_memory_kib: 52000,
free_memory_kib: 15000,
total_memory_kib: 128000,
},
{
available_memory_kib: 53000,
free_memory_kib: 15000,
total_memory_kib: 128000,
},
{
available_memory_kib: 65000,
free_memory_kib: 15000,
total_memory_kib: 128000,
}
];
/* @type {!SystemInfo} */ /* @type {!SystemInfo} */
export const fakeSystemInfo = { export const fakeSystemInfo = {
......
...@@ -33,6 +33,12 @@ class FakeObservableState { ...@@ -33,6 +33,12 @@ class FakeObservableState {
* @private {number} * @private {number}
**/ **/
this.index_ = -1; this.index_ = -1;
/**
* Id of the timer if enabled.
* @private {number}
*/
this.timerId_ = -1;
} }
/** @param {!Array<!T>} observations */ /** @param {!Array<!T>} observations */
...@@ -46,6 +52,31 @@ class FakeObservableState { ...@@ -46,6 +52,31 @@ class FakeObservableState {
this.observers_.push(callback); this.observers_.push(callback);
} }
/**
* Start firing the observers on a fixed interval. setObservableData() must
* already have been called.
* @param {number} intervalMs
*/
startTriggerOnInterval(intervalMs) {
assert(this.index_ >= 0);
if (this.timerId_ != -1) {
this.stopTriggerOnInterval();
}
assert(this.timerId_ == -1);
this.timerId_ = setInterval(this.trigger.bind(this), intervalMs);
}
/**
* Disables the observer firing automatically on an interval.
*/
stopTriggerOnInterval() {
if (this.timerId_ != -1) {
clearInterval(this.timerId_);
this.timerId_ = -1;
}
}
/** /**
* Causes the observable to trigger and notify all observers of the next * Causes the observable to trigger and notify all observers of the next
* observation value. * observation value.
...@@ -106,6 +137,32 @@ export class FakeObservables { ...@@ -106,6 +137,32 @@ export class FakeObservables {
setObservableData(methodName, observations) { setObservableData(methodName, observations) {
this.getObservable_(methodName).setObservableData(observations); this.getObservable_(methodName).setObservableData(observations);
} }
/**
* Start firing the observer on a fixed interval. setObservableData() must
* already have been called.
* @param {string} methodName
* @param {number} intervalMs
*/
startTriggerOnInterval(methodName, intervalMs) {
this.getObservable_(methodName).startTriggerOnInterval(intervalMs);
}
/**
* Disables the observer firing automatically on an interval.
* @param {string} methodName
*/
stopTriggerOnInterval(methodName) {
this.getObservable_(methodName).stopTriggerOnInterval();
}
/**
* Disables all observers firing automatically on an interval.
*/
stopAllTriggerIntervals() {
for (let obs of this.observables_.values()) {
obs.stopTriggerOnInterval();
}
}
/** /**
* Causes the observable to trigger and notify all observers of the next * Causes the observable to trigger and notify all observers of the next
......
...@@ -172,4 +172,25 @@ export class FakeSystemDataProvider { ...@@ -172,4 +172,25 @@ export class FakeSystemDataProvider {
this.observables_.setObservableData( this.observables_.setObservableData(
'MemoryUsageObserver_onMemoryUsageUpdated', memoryUsageList); 'MemoryUsageObserver_onMemoryUsageUpdated', memoryUsageList);
} }
/**
* Make the observables fire automatically on various intervals.
*/
startTriggerIntervals() {
this.observables_.startTriggerOnInterval(
'CpuUsageObserver_onCpuUsageUpdated', 1000);
this.observables_.startTriggerOnInterval(
'MemoryUsageObserver_onMemoryUsageUpdated', 5000);
this.observables_.startTriggerOnInterval(
'BatteryHealthObserver_onBatteryHealthUpdated', 30000);
this.observables_.startTriggerOnInterval(
'BatteryChargeStatusObserver_onBatteryChargeStatusUpdated', 30000);
}
/**
* Stop automatically triggering observables.
*/
stopTriggerIntervals() {
this.observables_.stopAllTriggerIntervals();
}
} }
...@@ -29,6 +29,9 @@ function setupFakeSystemDataProvider_() { ...@@ -29,6 +29,9 @@ function setupFakeSystemDataProvider_() {
provider.setFakeMemoryUsage(fakeMemoryUsage); provider.setFakeMemoryUsage(fakeMemoryUsage);
provider.setFakeSystemInfo(fakeSystemInfo); provider.setFakeSystemInfo(fakeSystemInfo);
// Start the timers to generate some observations.
provider.startTriggerIntervals();
// Set the fake provider. // Set the fake provider.
setSystemDataProviderForTesting(provider); setSystemDataProviderForTesting(provider);
} }
......
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