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

Diagnostics: Add helper class for fake async methods

- Maintains a mapping of fake methods, resolvers and fake data

Bug: 1125150
Test: browser_tests --gtest_filter=DiagnosticsApp*
Change-Id: Ic07ddfc300679611b4d8dc908bb61a30631e9874
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2417450
Commit-Queue: Zentaro Kavanagh <zentaro@chromium.org>
Reviewed-by: default avatarBailey Berro <baileyberro@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808543}
parent 4d4ea661
...@@ -7,8 +7,48 @@ import 'chrome://resources/mojo/mojo/public/js/mojo_bindings_lite.js'; ...@@ -7,8 +7,48 @@ import 'chrome://resources/mojo/mojo/public/js/mojo_bindings_lite.js';
import 'chrome://diagnostics/diagnostics_app.js'; import 'chrome://diagnostics/diagnostics_app.js';
import {SystemDataProviderInterface} from 'chrome://diagnostics/diagnostics_types.js'; import {SystemDataProviderInterface} from 'chrome://diagnostics/diagnostics_types.js';
import {FakeMethodResolver} from 'chrome://diagnostics/fake_method_resolver.js';
import {getSystemDataProvider, setSystemDataProviderForTesting} from 'chrome://diagnostics/mojo_interface_provider.js'; import {getSystemDataProvider, setSystemDataProviderForTesting} from 'chrome://diagnostics/mojo_interface_provider.js';
suite('DiagnosticsFakeMethodResolver', () => {
/** @type {?FakeMethodResolver} */
let resolver = null;
setup(function() {
resolver = new FakeMethodResolver();
});
teardown(function() {
resolver = null;
});
test('AddingMethodNoResult', () => {
resolver.register('foo');
return resolver.resolveMethod('foo').then((result) => {
assertEquals(undefined, result);
});
});
test('AddingMethodWithResult', () => {
resolver.register('foo');
const expected = {'foo': 'bar'};
resolver.setResult('foo', expected);
return resolver.resolveMethod('foo').then((result) => {
assertEquals(expected, result);
});
});
test('AddingTwoMethodCallingOne', () => {
resolver.register('foo');
resolver.register('bar');
const expected = {'fooKey': 'fooValue'};
resolver.setResult('foo', expected);
return resolver.resolveMethod('foo').then((result) => {
assertEquals(expected, result);
});
});
});
suite('DiagnosticsAppTest', () => { suite('DiagnosticsAppTest', () => {
/** @type {?DiagnosticsApp} */ /** @type {?DiagnosticsApp} */
let page = null; let page = null;
......
...@@ -13,6 +13,7 @@ js_type_check("closure_compile_module") { ...@@ -13,6 +13,7 @@ js_type_check("closure_compile_module") {
":diagnostics_app", ":diagnostics_app",
":diagnostics_card", ":diagnostics_card",
":diagnostics_types", ":diagnostics_types",
":fake_method_resolver",
":mojo_interface_provider", ":mojo_interface_provider",
] ]
} }
...@@ -33,6 +34,10 @@ js_library("diagnostics_card") { ...@@ -33,6 +34,10 @@ js_library("diagnostics_card") {
js_library("diagnostics_types") { js_library("diagnostics_types") {
} }
js_library("fake_method_resolver") {
deps = [ "//ui/webui/resources/js:cr.m" ]
}
js_library("mojo_interface_provider") { js_library("mojo_interface_provider") {
deps = [ "//ui/webui/resources/js:cr.m" ] deps = [ "//ui/webui/resources/js:cr.m" ]
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
<include name="IDR_DIAGNOSTICS_APP_ICON" file="app_icon_192.png" type="BINDATA" /> <include name="IDR_DIAGNOSTICS_APP_ICON" file="app_icon_192.png" type="BINDATA" />
<include name="IDR_DIAGNOSTICS_APP_INDEX_HTML" file="index.html" type="BINDATA" /> <include name="IDR_DIAGNOSTICS_APP_INDEX_HTML" file="index.html" type="BINDATA" />
<include name="IDR_DIAGNOSTICS_APP_JS" file="${root_gen_dir}/chromeos/components/diagnostics_ui/resources/diagnostics_app.js" use_base_dir="false" type="BINDATA"/> <include name="IDR_DIAGNOSTICS_APP_JS" file="${root_gen_dir}/chromeos/components/diagnostics_ui/resources/diagnostics_app.js" use_base_dir="false" type="BINDATA"/>
<include name="IDR_DIAGNOSTICS_FAKE_METHOD_RESOLVER_JS" file="fake_method_resolver.js" type="BINDATA"/>
<include name="IDR_DIAGNOSTICS_MOJO_INTERFACE_PROVIDER_JS" file="mojo_interface_provider.js" type="BINDATA"/> <include name="IDR_DIAGNOSTICS_MOJO_INTERFACE_PROVIDER_JS" file="mojo_interface_provider.js" type="BINDATA"/>
<include name="IDR_DIAGNOSTICS_SHARED_CSS_JS" file="${root_gen_dir}/chromeos/components/diagnostics_ui/resources/diagnostics_shared_css.js" use_base_dir="false" type="BINDDATA"/> <include name="IDR_DIAGNOSTICS_SHARED_CSS_JS" file="${root_gen_dir}/chromeos/components/diagnostics_ui/resources/diagnostics_shared_css.js" use_base_dir="false" type="BINDDATA"/>
<include name="IDR_DIAGNOSTICS_TYPES_JS" file="diagnostics_types.js" type="BINDATA"/> <include name="IDR_DIAGNOSTICS_TYPES_JS" file="diagnostics_types.js" 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';
import {PromiseResolver} from 'chrome://resources/js/promise_resolver.m.js';
/**
* @fileoverview
* Implements a helper class for managing a fake API with async
* methods.
*/
/**
* Maintains a resolver and the return value of the fake method.
* @template T
*/
class FakeMethodState {
constructor() {
/** @private {PromiseResolver} */
this.resolver_ = new PromiseResolver();
/** @private {T} */
this.result_ = undefined;
}
/**
* Resolve the method with the supplied result.
* @return {!Promise}
*/
resolveMethod() {
this.resolver_.resolve(this.result_);
return this.resolver_.promise;
}
/**
* Set the result for this method.
* @param {T} result
*/
setResult(result) {
this.result_ = result;
}
}
/**
* Manages a map of fake async methods, their resolvers and the fake
* return value they will produce.
* @template T
**/
export class FakeMethodResolver {
constructor() {
/** @private {!Map<string, !FakeMethodState>} */
this.methodMap_ = new Map();
}
/**
* Registers methodName with the resolver. Calling other methods with a
* methodName that has not been registered will assert.
* @param {string} methodName
*/
register(methodName) {
this.methodMap_.set(methodName, new FakeMethodState());
}
/**
* Set the value that methodName will produce when it resolves.
* @param {string} methodName
* @param {T} result
*/
setResult(methodName, result) {
this.getState_(methodName).setResult(result);
}
/**
* Causes the promise for methodName to resolve.
* @param {string} methodName
* @return {!Promise}
*/
resolveMethod(methodName) {
return this.getState_(methodName).resolveMethod();
}
/**
* Return the FakeMethodState for methodName.
* @param {string} methodName
* @return {!FakeMethodState}
* @private
*/
getState_(methodName) {
const state = this.methodMap_.get(methodName);
assert(!!state, `Method '${methodName}' not found.`);
return state;
}
}
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