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

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

Issue 1726943002: First set of WebUSB layout tests with Mojo service mocks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix mojo-helpers.js's define function. Created 4 years, 9 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 unified diff | Download patch
« no previous file with comments | « no previous file | third_party/WebKit/LayoutTests/usb/mock-services.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * mojo-helpers contains extensions to testharness.js useful for consuming 2 * mojo-helpers contains extensions to testharness.js useful for consuming
3 * and mocking Mojo services directly within test code. 3 * and mocking Mojo services directly within test code.
4 */ 4 */
5 'use strict'; 5 'use strict';
6 6
7 // Fix up the global window.define, since all baked-in Mojo modules expect to 7 // Fix up the global window.define, since all baked-in Mojo modules expect to
8 // find it there. This define() also returns a promise to the module. 8 // find it there. This define() also returns a promise to the module.
9 function define(name, deps, factory) { 9 let define = (function(){
10 return new Promise(resolve => { 10 let moduleCache = new Map();
11 mojo.define(name, deps, (...modules) => { 11
12 let result = factory(...modules); 12 return function(name, deps, factory) {
13 resolve(result); 13 let promise = moduleCache.get(name);
14 return result; 14 if (promise === undefined) {
15 }); 15 // This promise must be cached as mojo.define will only call the factory
16 }); 16 // function the first time the module is defined.
17 } 17 promise = new Promise(resolve => {
18 mojo.define(name, deps, (...modules) => {
19 let result = factory(...modules);
20 resolve(result);
21 return result;
22 });
23 });
24 moduleCache.set(name, promise);
25 }
26 return promise;
27 }
28 })();
18 29
19 // Returns a promise to an object that exposes common Mojo module interfaces. 30 // Returns a promise to an object that exposes common Mojo module interfaces.
20 // Additional modules to load can be specified in the |modules| parameter. The 31 // Additional modules to load can be specified in the |modules| parameter. The
21 // result will contain them, in the same order, in the |modules| field. 32 // result will contain them, in the same order, in the |modules| field.
22 function loadMojoModules(name, modules = []) { 33 function loadMojoModules(name, modules = []) {
23 return define('Mojo layout test module: ' + name, [ 34 return define('Mojo layout test module: ' + name, [
24 'mojo/public/js/core', 35 'mojo/public/js/core',
25 'mojo/public/js/router', 36 'mojo/public/js/router',
26 'mojo/public/js/support', 37 'mojo/public/js/support',
27 'content/public/renderer/service_provider', 38 'content/public/renderer/service_provider',
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 let buffer, handles; 78 let buffer, handles;
68 ({ result, buffer, handles } = mojo.core.readMessage(pipe, 0)); 79 ({ result, buffer, handles } = mojo.core.readMessage(pipe, 0));
69 if (result !== mojo.core.RESULT_OK) { 80 if (result !== mojo.core.RESULT_OK) {
70 reject(result); 81 reject(result);
71 return; 82 return;
72 } 83 }
73 resolve({ buffer, handles }); 84 resolve({ buffer, handles });
74 }); 85 });
75 }); 86 });
76 }; 87 };
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/LayoutTests/usb/mock-services.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698