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

Side by Side 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, 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
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 // function the first time the module is defined.
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 })();
29
30 define('Mojo Helpers', [
31 'mojo/public/js/core',
32 'mojo/public/js/router',
33 'mojo/public/js/support',
34 'content/public/renderer/service_provider'
35 ], (core, router, support, serviceProvider) => {
36 add_completion_callback(() => {
37 serviceProvider.clearServiceOverridesForTesting();
16 }); 38 });
17 } 39
40 return {
41 core: core,
42 router: router,
43 support: support,
44
45 // |serviceProvider| is a bit of a misnomer. It should probably be
46 // called |serviceRegistry|, so let's call it that here.
47 serviceRegistry: serviceProvider,
48 };
49 });
18 50
19 // Returns a promise to an object that exposes common Mojo module interfaces. 51 // 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 52 // 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. 53 // result will contain them, in the same order, in the |modules| field.
22 function loadMojoModules(name, modules = []) { 54 function loadMojoModules(name, modules = []) {
23 return define('Mojo layout test module: ' + name, [ 55 return define('Mojo modules: ' + name,
24 'mojo/public/js/core', 56 [ 'Mojo Helpers' ].concat(modules),
25 'mojo/public/js/router', 57 (mojo, ...rest) => {
26 'mojo/public/js/support', 58 mojo.modules = rest
27 'content/public/renderer/service_provider', 59 return mojo;
28 ].concat(modules), (core, router, support, serviceProvider, ...rest) => {
29 return {
30 core: core,
31 router: router,
32 support: support,
33
34 // |serviceProvider| is a bit of a misnomer. It should probably be
35 // called |serviceRegistry|, so let's call it that here.
36 serviceRegistry: serviceProvider,
37
38 modules: rest,
39 };
40 }); 60 });
41 } 61 }
42 62
43 function mojoTestCleanUp(mojo) {
44 mojo.serviceRegistry.clearServiceOverridesForTesting();
45 }
46
47 // Runs a promise_test which depends on the Mojo system API modules available to 63 // Runs a promise_test which depends on the Mojo system API modules available to
48 // all layout tests. The test implementation function is called with an Object 64 // all layout tests. The test implementation function is called with an Object
49 // that exposes common Mojo module interfaces. 65 // that exposes common Mojo module interfaces.
50 function mojo_test(func, name, properties) { 66 function mojo_test(func, name, properties) {
51 promise_test(() => loadMojoModules(name).then(mojo => { 67 promise_test(() => loadMojoModules(name).then(mojo => {
52 let result = Promise.resolve(func(mojo)); 68 return Promise.resolve(func(mojo));
53 let cleanUp = () => mojoTestCleanUp(mojo);
54 result.then(cleanUp, cleanUp);
55 return result;
56 }), name, properties); 69 }), name, properties);
57 } 70 }
58 71
59 // Waits for a message to become available on a pipe. 72 // Waits for a message to become available on a pipe.
60 function mojo_wait_for_incoming_message(mojo, pipe) { 73 function mojo_wait_for_incoming_message(mojo, pipe) {
61 return new Promise((resolve, reject) => { 74 return new Promise((resolve, reject) => {
62 mojo.support.asyncWait(pipe, mojo.core.HANDLE_SIGNAL_READABLE, result => { 75 mojo.support.asyncWait(pipe, mojo.core.HANDLE_SIGNAL_READABLE, result => {
63 if (result != mojo.core.RESULT_OK) { 76 if (result != mojo.core.RESULT_OK) {
64 reject(result); 77 reject(result);
65 return; 78 return;
66 } 79 }
67 let buffer, handles; 80 let buffer, handles;
68 ({ result, buffer, handles } = mojo.core.readMessage(pipe, 0)); 81 ({ result, buffer, handles } = mojo.core.readMessage(pipe, 0));
69 if (result !== mojo.core.RESULT_OK) { 82 if (result !== mojo.core.RESULT_OK) {
70 reject(result); 83 reject(result);
71 return; 84 return;
72 } 85 }
73 resolve({ buffer, handles }); 86 resolve({ buffer, handles });
74 }); 87 });
75 }); 88 });
76 }; 89 };
OLDNEW
« 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