Commit b60c7a01 authored by Zentaro Kavanagh's avatar Zentaro Kavanagh Committed by Commit Bot

Diagnostics: Add a fake observable helper

- Simplifies implementing fake observers
- Currently only supports a single observable value

Bug: 1125150
Test: browser_tests --gtest_filter=DiagnosticsApp*
Change-Id: Idcddbbe30e220ecab8e7218450630062fb69c8e1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2420769Reviewed-by: default avatarBailey Berro <baileyberro@chromium.org>
Commit-Queue: Zentaro Kavanagh <zentaro@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809588}
parent af99c735
...@@ -9,8 +9,10 @@ import 'chrome://diagnostics/diagnostics_app.js'; ...@@ -9,8 +9,10 @@ import 'chrome://diagnostics/diagnostics_app.js';
import {SystemDataProviderInterface} from 'chrome://diagnostics/diagnostics_types.js'; import {SystemDataProviderInterface} from 'chrome://diagnostics/diagnostics_types.js';
import {fakeBatteryInfo, fakeSystemInfo} from 'chrome://diagnostics/fake_data.js'; import {fakeBatteryInfo, fakeSystemInfo} from 'chrome://diagnostics/fake_data.js';
import {FakeMethodResolver} from 'chrome://diagnostics/fake_method_resolver.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'; import {FakeSystemDataProvider} from 'chrome://diagnostics/fake_system_data_provider.js';
import {getSystemDataProvider, setSystemDataProviderForTesting} from 'chrome://diagnostics/mojo_interface_provider.js'; import {getSystemDataProvider, setSystemDataProviderForTesting} from 'chrome://diagnostics/mojo_interface_provider.js';
import {PromiseResolver} from 'chrome://resources/js/promise_resolver.m.js';
import {flush} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; import {flush} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {flushTasks} from 'chrome://test/test_util.m.js'; import {flushTasks} from 'chrome://test/test_util.m.js';
...@@ -296,6 +298,36 @@ suite('FakeMojoProviderTest', () => { ...@@ -296,6 +298,36 @@ suite('FakeMojoProviderTest', () => {
}); });
}); });
suite('FakeObservablesTest', () => {
/** @type {?FakeObservables} */
let observables = null;
setup(() => {
observables = new FakeObservables();
});
teardown(() => {
observables = null;
});
test('RegisterSimpleObservable', () => {
observables.register('ObserveFoo_OnFooUpdated');
/** @type !Array<string> */
const expected = ['bar'];
observables.setObservableData('ObserveFoo_OnFooUpdated', expected);
let resolver = new PromiseResolver();
observables.observe('ObserveFoo_OnFooUpdated', (foo) => {
assertEquals(expected[0], foo);
resolver.resolve();
});
observables.trigger('ObserveFoo_OnFooUpdated');
return resolver.promise;
});
});
suite('FakeSystemDataProviderTest', () => { suite('FakeSystemDataProviderTest', () => {
/** @type {?FakeSystemDataProvider} */ /** @type {?FakeSystemDataProvider} */
let provider = null; let provider = null;
......
...@@ -17,6 +17,7 @@ js_type_check("closure_compile_module") { ...@@ -17,6 +17,7 @@ js_type_check("closure_compile_module") {
":diagnostics_types", ":diagnostics_types",
":fake_data", ":fake_data",
":fake_method_resolver", ":fake_method_resolver",
":fake_observables",
":fake_system_data_provider", ":fake_system_data_provider",
":memory_card", ":memory_card",
":mojo_interface_provider", ":mojo_interface_provider",
...@@ -64,6 +65,10 @@ js_library("fake_method_resolver") { ...@@ -64,6 +65,10 @@ js_library("fake_method_resolver") {
deps = [ "//ui/webui/resources/js:cr.m" ] deps = [ "//ui/webui/resources/js:cr.m" ]
} }
js_library("fake_observables") {
deps = [ "//ui/webui/resources/js:cr.m" ]
}
js_library("fake_system_data_provider") { js_library("fake_system_data_provider") {
deps = [ "//ui/webui/resources/js:cr.m" ] deps = [ "//ui/webui/resources/js:cr.m" ]
} }
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
<include name="IDR_DIAGNOSTICS_CPU_CARD_JS" file="${root_gen_dir}/chromeos/components/diagnostics_ui/resources/cpu_card.js" use_base_dir="false" type="BINDATA"/> <include name="IDR_DIAGNOSTICS_CPU_CARD_JS" file="${root_gen_dir}/chromeos/components/diagnostics_ui/resources/cpu_card.js" use_base_dir="false" type="BINDATA"/>
<include name="IDR_DIAGNOSTICS_FAKE_DATA_JS" file="fake_data.js" type="BINDATA"/> <include name="IDR_DIAGNOSTICS_FAKE_DATA_JS" file="fake_data.js" type="BINDATA"/>
<include name="IDR_DIAGNOSTICS_FAKE_METHOD_RESOLVER_JS" file="fake_method_resolver.js" type="BINDATA"/> <include name="IDR_DIAGNOSTICS_FAKE_METHOD_RESOLVER_JS" file="fake_method_resolver.js" type="BINDATA"/>
<include name="IDR_DIAGNOSTICS_FAKE_OBSERVABLES_JS" file="fake_observables.js" type="BINDATA"/>
<include name="IDR_DIAGNOSTICS_FAKE_SYSTEM_DATA_PROVIDER_JS" file="fake_system_data_provider.js" type="BINDATA"/> <include name="IDR_DIAGNOSTICS_FAKE_SYSTEM_DATA_PROVIDER_JS" file="fake_system_data_provider.js" type="BINDATA"/>
<include name="IDR_DIAGNOSTICS_FONTS_CSS_JS" file="${root_gen_dir}/chromeos/components/diagnostics_ui/resources/diagnostics_fonts_css.js" use_base_dir="false" type="BINDATA"/> <include name="IDR_DIAGNOSTICS_FONTS_CSS_JS" file="${root_gen_dir}/chromeos/components/diagnostics_ui/resources/diagnostics_fonts_css.js" use_base_dir="false" type="BINDATA"/>
<include name="IDR_DIAGNOSTICS_MEMORY_CARD_JS" file="${root_gen_dir}/chromeos/components/diagnostics_ui/resources/memory_card.js" use_base_dir="false" type="BINDATA"/> <include name="IDR_DIAGNOSTICS_MEMORY_CARD_JS" file="${root_gen_dir}/chromeos/components/diagnostics_ui/resources/memory_card.js" use_base_dir="false" type="BINDATA"/>
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {assert} from 'chrome://resources/js/assert.m.js';
/**
* @fileoverview
* Implements a helper class for faking asynchronous observables.
*/
/**
* Maintains state about an observable and the data it will produce.
* @template T
**/
class FakeObservableState {
constructor() {
/**
* The list of functions that will be notified when the observable
* is triggered.
* @private {!Array<!function(!T)>}
**/
this.observers_ = [];
/**
* Array of observations to be supplied by the observer.
* @private {!Array<T>}
**/
this.observations_ = [];
/**
* The index of the next observation.
* @private {number}
**/
this.index_ = -1;
}
/** @param {!Array<!T>} observations */
setObservableData(observations) {
// TODO(zentaro): Fully support multiple observations.
assert(observations.length == 1);
this.observations_ = observations;
this.index_ = 0;
}
/** @param {!function(!T)} callback */
addObserver(callback) {
this.observers_.push(callback);
}
/**
* Causes the observable to trigger and notify all observers of the next
* observation value.
*/
trigger() {
assert(this.observations_.length > 0);
assert(this.index_ >= 0);
assert(this.index_ < this.observations_.length);
// Get the value of this observation and update the index to point to the
// next one.
const value = this.observations_[this.index_];
this.index_ = (this.index_ + 1) % this.observations_.length;
// Fire all the callbacks that are observing this observable.
for (const fn of this.observers_) {
fn(value);
}
}
}
/**
* Manages a map of fake observables and the fake data they will produce
* when triggered.
* @template T
*/
export class FakeObservables {
constructor() {
/** @private {!Map<string, !FakeObservableState>} */
this.observables_ = new Map();
}
/**
* Register an observable. Other calls to this class will assert if the
* observable has not been registered.
* @param {string} methodName
*/
register(methodName) {
this.observables_.set(methodName, new FakeObservableState());
}
/**
* Supply the callback for observing methodName.
* @param {string} methodName
* @param {!function(!T)} callback
*/
observe(methodName, callback) {
this.getObservable_(methodName).addObserver(callback);
}
/**
* Sets the data that will be produced when the observable is triggered.
* Each observation produces the next value in the array and wraps around
* when all observations have been produced.
* @param {string} methodName
* @param {!Array<!T>} observations
*/
setObservableData(methodName, observations) {
this.getObservable_(methodName).setObservableData(observations);
}
/**
* Causes the observable to trigger and notify all observers of the next
* observation value.
* @param {string} methodName
*/
trigger(methodName) {
this.getObservable_(methodName).trigger();
}
/**
* Return the Observable for methodName.
* @param {string} methodName
* @return {!FakeObservableState}
* @private
*/
getObservable_(methodName) {
let observable = this.observables_.get(methodName);
assert(!!observable, `Observable '${methodName}' not found.`);
return observable;
}
}
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