Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(380)

Unified Diff: third_party/WebKit/LayoutTests/resources/mojo-helpers.js

Issue 1730403006: Basic layout tests for WebUSB. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Mojo tests that depend on extra module loading. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/renderer/usb/web_usb_device_impl.cc ('k') | third_party/WebKit/LayoutTests/usb/mock-services.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/LayoutTests/resources/mojo-helpers.js
diff --git a/third_party/WebKit/LayoutTests/resources/mojo-helpers.js b/third_party/WebKit/LayoutTests/resources/mojo-helpers.js
index 10350f7fc6da14d53e0c579956f578d7d414356b..6e3f7cd9100c8c6a8821b1bd7dacce754a63333a 100644
--- a/third_party/WebKit/LayoutTests/resources/mojo-helpers.js
+++ b/third_party/WebKit/LayoutTests/resources/mojo-helpers.js
@@ -6,53 +6,66 @@
// Fix up the global window.define, since all baked-in Mojo modules expect to
// find it there. This define() also returns a promise to the module.
-function define(name, deps, factory) {
- return new Promise(resolve => {
- mojo.define(name, deps, (...modules) => {
- let result = factory(...modules);
- resolve(result);
- return result;
- });
+let define = (function(){
+ let moduleCache = new Map();
+
+ return function(name, deps, factory) {
+ let promise = moduleCache.get(name);
+ if (promise === undefined) {
+ // This promise must be cached as mojo.define will only call the factory
+ // function the first time the module is defined.
+ promise = new Promise(resolve => {
+ mojo.define(name, deps, (...modules) => {
+ let result = factory(...modules);
+ resolve(result);
+ return result;
+ });
+ });
+ moduleCache.set(name, promise);
+ }
+ return promise;
+ }
+})();
+
+define('Mojo Helpers', [
+ 'mojo/public/js/core',
+ 'mojo/public/js/router',
+ 'mojo/public/js/support',
+ 'content/public/renderer/service_provider'
+], (core, router, support, serviceProvider) => {
+ add_completion_callback(() => {
+ serviceProvider.clearServiceOverridesForTesting();
});
-}
+
+ return {
+ core: core,
+ router: router,
+ support: support,
+
+ // |serviceProvider| is a bit of a misnomer. It should probably be
+ // called |serviceRegistry|, so let's call it that here.
+ serviceRegistry: serviceProvider,
+ };
+});
// Returns a promise to an object that exposes common Mojo module interfaces.
// Additional modules to load can be specified in the |modules| parameter. The
// result will contain them, in the same order, in the |modules| field.
function loadMojoModules(name, modules = []) {
- return define('Mojo layout test module: ' + name, [
- 'mojo/public/js/core',
- 'mojo/public/js/router',
- 'mojo/public/js/support',
- 'content/public/renderer/service_provider',
- ].concat(modules), (core, router, support, serviceProvider, ...rest) => {
- return {
- core: core,
- router: router,
- support: support,
-
- // |serviceProvider| is a bit of a misnomer. It should probably be
- // called |serviceRegistry|, so let's call it that here.
- serviceRegistry: serviceProvider,
-
- modules: rest,
- };
+ return define('Mojo modules: ' + name,
+ [ 'Mojo Helpers' ].concat(modules),
+ (mojo, ...rest) => {
+ mojo.modules = rest
+ return mojo;
});
}
-function mojoTestCleanUp(mojo) {
- mojo.serviceRegistry.clearServiceOverridesForTesting();
-}
-
// Runs a promise_test which depends on the Mojo system API modules available to
// all layout tests. The test implementation function is called with an Object
// that exposes common Mojo module interfaces.
function mojo_test(func, name, properties) {
promise_test(() => loadMojoModules(name).then(mojo => {
- let result = Promise.resolve(func(mojo));
- let cleanUp = () => mojoTestCleanUp(mojo);
- result.then(cleanUp, cleanUp);
- return result;
+ return Promise.resolve(func(mojo));
}), name, properties);
}
« no previous file with comments | « content/renderer/usb/web_usb_device_impl.cc ('k') | third_party/WebKit/LayoutTests/usb/mock-services.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698