Index: chrome/test/data/webui/test_api.js |
diff --git a/chrome/test/data/webui/test_api.js b/chrome/test/data/webui/test_api.js |
index 9dc23c1b3e11720b6f0cb0ae488e8e7f326e754d..5b04392e03b84cd5098cb235bccfc6f17567c7cf 100644 |
--- a/chrome/test/data/webui/test_api.js |
+++ b/chrome/test/data/webui/test_api.js |
@@ -198,6 +198,36 @@ var testing = {}; |
}, |
/** |
+ * Create a container of mocked standalone functions to handle |
+ * '.'-separated |apiNames|, assign it to |this.mockApis|, register its API |
+ * overrides and return it. |
+ * @return {Mock} Mock handler class. |
+ * @see makeMockFunctions |
+ * @see registerMockApis |
+ */ |
+ makeAndRegisterMockApis: function (apiNames) { |
+ var apiMockNames = []; |
arv (Not doing code reviews)
2013/07/12 14:18:34
Maybe:
var apiMockNames = apiNames.map(function(n
vadimt
2013/07/12 18:22:35
Cool
|
+ for(var i = 0; i < apiNames.length; i++) { |
+ apiMockNames.push(apiNames [i].replace(/\./g, '_')); |
arv (Not doing code reviews)
2013/07/12 14:18:34
Another option is to keep the '.' in the name. The
vadimt
2013/07/12 18:22:35
I considered this. I feel that the using code woul
|
+ } |
+ |
+ this.mockApis = makeMockFunctions(apiMockNames); |
+ registerMockApis(this.mockApis); |
+ return this.mockApis; |
+ }, |
+ |
+ /** |
+ * Create a container of mocked standalone functions to handle |
+ * |functionNames|, assign it to |this.mockLocalFunctions| and return it. |
+ * @return {Mock} Mock handler class. |
arv (Not doing code reviews)
2013/07/12 14:18:34
@param {!Array.<string>} functionNames
vadimt
2013/07/12 18:22:35
Done.
|
+ * @see makeMockFunctions |
+ */ |
+ makeMockLocalFunctions: function(functionNames) { |
+ this.mockLocalFunctions = makeMockFunctions(functionNames); |
+ return this.mockLocalFunctions; |
+ }, |
+ |
+ /** |
* Override this method to perform initialization during preload (such as |
* creating mocks and registering handlers). |
* @type {Function} |
@@ -506,6 +536,28 @@ var testing = {}; |
} |
/** |
+ * Registers the mock API call and its function. |
+ * @param {string} name The '_'-separated name of the API call. |
+ * @param {function(...)} theFunction Mock function for this API call. |
+ */ |
+ function registerMockApi(name, theFunction) { |
+ var path = name.split('_'); |
+ |
+ var namespace = this; |
+ for(var i = 0; i < path.length-1; ++i) { |
arv (Not doing code reviews)
2013/07/12 14:18:34
missing whitespaces around bin op
arv (Not doing code reviews)
2013/07/12 14:18:34
i++
vadimt
2013/07/12 18:22:35
Done.
vadimt
2013/07/12 18:22:35
Done.
|
+ var fieldName = path[i]; |
+ if(!namespace[fieldName]) |
+ namespace[fieldName] = {}; |
+ |
+ namespace = namespace[fieldName]; |
arv (Not doing code reviews)
2013/07/12 14:18:34
Once again I'm confused. It seems like you are bui
vadimt
2013/07/12 18:22:35
See above about external mock4js that generates th
|
+ } |
+ |
+ var fieldName = path[path.length-1]; |
+ assertEquals(undefined, namespace[fieldName]); |
+ namespace[fieldName] = theFunction; |
+ } |
+ |
+ /** |
* Empty function for use in making mocks. |
* @const |
*/ |
@@ -525,6 +577,31 @@ var testing = {}; |
} |
/** |
+ * Create a new class to handle |functionNames|, add method 'functions()' |
+ * that returns a container of standalone functions based on the mock class |
+ * members, and return it. |
+ * @return {Mock} Mock handler class. |
+ */ |
+ function makeMockFunctions(functionNames) { |
+ var MockClass = makeMockClass(functionNames); |
+ var mockFunctions = mock(MockClass); |
+ var mockProxy = mockFunctions.proxy(); |
+ |
+ mockFunctions.functions_ = {}; |
+ |
+ for (var func in MockClass.prototype) { |
+ if (typeof MockClass.prototype[func] === 'function') |
arv (Not doing code reviews)
2013/07/12 14:18:34
What about accessors? Maybe add a todo?
vadimt
2013/07/12 18:22:35
This method is used to generate:
1. Local function
|
+ mockFunctions.functions_[func] = mockProxy[func].bind(mockProxy); |
+ } |
+ |
+ mockFunctions.functions = function () { |
+ return this.functions_; |
arv (Not doing code reviews)
2013/07/12 14:18:34
Is this the correct this? If so, maybe just rename
vadimt
2013/07/12 18:22:35
Yes, this is a correct 'this': the one of mockFunc
|
+ }; |
+ |
+ return mockFunctions; |
+ } |
+ |
+ /** |
* Register all methods of {@code mockClass.prototype} as overrides to global |
* functions of the same name as the method, using the proxy of the |
* |mockObject| to handle the functions. |
@@ -541,6 +618,19 @@ var testing = {}; |
} |
/** |
+ * Register all functions in |mockObject.functions()| as global API calls. |
+ * @param {Mock4JS.Mock} mockObject The mock to register callbacks against. |
+ * @see registerMockApi |
+ */ |
+ function registerMockApis(mockObject) { |
+ var functions = mockObject.functions(); |
+ for (var func in functions) { |
+ if (typeof functions[func] === 'function') |
+ registerMockApi(func, functions[func]); |
+ } |
+ } |
+ |
+ /** |
* Overrides {@code chrome.send} for routing messages to javascript |
* functions. Also falls back to sending with the original chrome object. |
* @param {string} messageName The message to route. |