Commit e87b8c7e authored by dpapad's avatar dpapad Committed by Commit Bot

WebUI: Remove unused functionality from test_api.js

Specifically removing:
 - callGlobalWithSavedArgs() and CallGlobalAction class
 - makeAndRegisterMockGlobals(), makeAndRegisterMockApis()
 - registerMockGlobal() and registerMockGlobals()
 - registerMockApi() and registerMockApis()
 - overrideGlobal(), globalOverrides and mockGlobals variables

Also removing an export for registerMockMessageCallbacks, since
this function is only used within test_api.js

Bug: 844820
Change-Id: I958123de5077fc408c3ddab687fbe31a37de0d27
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1637984
Auto-Submit: Demetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#665265}
parent d178f9bc
...@@ -190,24 +190,6 @@ function setTestRanTrue() { ...@@ -190,24 +190,6 @@ function setTestRanTrue() {
testRan = true; testRan = true;
} }
// Test overriding globals.
TEST_F('WebUIBrowserAsyncGenTest', 'TestRegisterMockGlobals', function() {
this.makeAndRegisterMockGlobals(['setTestRanTrue']);
// Mock the setTestRanTrue global function.
this.mockGlobals.expects(once()).setTestRanTrue().will(runAllActionsAsync(
WhenTestDone.ALWAYS, callGlobalWithSavedArgs(null, 'setTestRanTrue'),
callFunction(function() {
assertTrue(testRan);
})));
// Cause setTestRanTrue to be invoked asynchronously.
chrome.send('callJS', ['setTestRanTrue']);
// In case the global isn't called, call testDone to collect the results.
chrome.send('callJS', ['testDone']);
});
/** /**
* Will be set to the runTest continuation by the following test fixture. * Will be set to the runTest continuation by the following test fixture.
* @type {Function} * @type {Function}
...@@ -258,31 +240,3 @@ WebUIBrowserAsyncGenDeferredTest.prototype = { ...@@ -258,31 +240,3 @@ WebUIBrowserAsyncGenDeferredTest.prototype = {
TEST_F('WebUIBrowserAsyncGenDeferredTest', 'TestDeferRunTest', function() { TEST_F('WebUIBrowserAsyncGenDeferredTest', 'TestDeferRunTest', function() {
this.ranTest_ = true; this.ranTest_ = true;
}); });
/**
* Test fixture for testing async tests are deferred until global is called.
* @constructor
*/
function WebUIBrowserAsyncGenDeferredToGlobalTest() {}
WebUIBrowserAsyncGenDeferredToGlobalTest.prototype = {
__proto__: WebUIBrowserAsyncGenDeferredTest.prototype,
/** @inheritDoc */
setUp: function() {
this.makeAndRegisterMockGlobals(['setTestRanTrue']);
this.mockGlobals.expects(once()).setTestRanTrue().will(runAllActionsAsync(
WhenTestDone.ALWAYS, callGlobalWithSavedArgs(null, 'setTestRanTrue'),
callFunction(deferRunTest)));
// Cause setTestRanTrue to be invoked asynchronously.
chrome.send('callJS', ['setTestRanTrue']);
},
};
TEST_F(
'WebUIBrowserAsyncGenDeferredToGlobalTest', 'TestDeferRunTestToGlobal',
function() {
this.ranTest_ = true;
assertTrue(testRan);
});
...@@ -143,13 +143,6 @@ Test.prototype = { ...@@ -143,13 +143,6 @@ Test.prototype = {
*/ */
mockHandler: null, mockHandler: null,
/**
* This should be initialized by the test fixture and can be referenced
* during the test run. It holds any mocked global functions.
* @type {?Mock4JS.Mock}
*/
mockGlobals: null,
/** /**
* Value is passed through call to C++ RunJavascriptF to invoke this test. * Value is passed through call to C++ RunJavascriptF to invoke this test.
* @type {boolean} * @type {boolean}
...@@ -280,37 +273,6 @@ Test.prototype = { ...@@ -280,37 +273,6 @@ Test.prototype = {
return this.mockHandler; return this.mockHandler;
}, },
/**
* Create a new class to handle |functionNames|, assign it to
* |this.mockGlobals|, register its global overrides, and return it.
* @return {Mock} Mock handler class assigned to |this.mockGlobals|.
* @see registerMockGlobals
*/
makeAndRegisterMockGlobals: function(functionNames) {
var MockClass = makeMockClass(functionNames);
this.mockGlobals = mock(MockClass);
registerMockGlobals(this.mockGlobals, MockClass);
return this.mockGlobals;
},
/**
* 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 = apiNames.map(function(name) {
return name.replace(/\./g, '_');
});
this.mockApis = makeMockFunctions(apiMockNames);
registerMockApis(this.mockApis);
return this.mockApis;
},
/** /**
* Create a container of mocked standalone functions to handle * Create a container of mocked standalone functions to handle
* |functionNames|, assign it to |this.mockLocalFunctions| and return it. * |functionNames|, assign it to |this.mockLocalFunctions| and return it.
...@@ -611,12 +573,6 @@ function registerMockMessageCallbacks(mockObject, mockClass) { ...@@ -611,12 +573,6 @@ function registerMockMessageCallbacks(mockObject, mockClass) {
} }
} }
/**
* Holds the mapping of name -> global override information.
* @type {Object}
*/
var globalOverrides = {};
/** /**
* When preloading JavaScript libraries, this is true until the * When preloading JavaScript libraries, this is true until the
* DOMContentLoaded event has been received as globals cannot be overridden * DOMContentLoaded event has been received as globals cannot be overridden
...@@ -625,61 +581,6 @@ var globalOverrides = {}; ...@@ -625,61 +581,6 @@ var globalOverrides = {};
*/ */
var deferGlobalOverrides = false; var deferGlobalOverrides = false;
/**
* Override the global function |funcName| with its registered mock. This
* should not be called twice for the same |funcName|.
* @param {string} funcName The name of the global function to override.
*/
function overrideGlobal(funcName) {
assertNotEquals(undefined, this[funcName]);
var globalOverride = globalOverrides[funcName];
assertNotEquals(undefined, globalOverride);
assertEquals(undefined, globalOverride.original);
globalOverride.original = this[funcName];
this[funcName] = globalOverride.callback.bind(globalOverride.object);
}
/**
* Registers the global function name, object and callback.
* @param {string} name The name of the message to route to this |callback|.
* @param {Object} object Pass as |this| when calling the |callback|.
* @param {function(...)} callback Called by {@code chrome.send}.
* @see overrideGlobal
*/
function registerMockGlobal(name, object, callback) {
assertEquals(undefined, globalOverrides[name]);
globalOverrides[name] = {
object: object,
callback: callback,
};
if (!deferGlobalOverrides) {
overrideGlobal(name);
}
}
/**
* 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++) {
var fieldName = path[i];
if (!namespace[fieldName]) {
namespace[fieldName] = {};
}
namespace = namespace[fieldName];
}
var fieldName = path[path.length - 1];
namespace[fieldName] = theFunction;
}
/** /**
* Empty function for use in making mocks. * Empty function for use in making mocks.
* @const * @const
...@@ -726,37 +627,6 @@ function makeMockFunctions(functionNames) { ...@@ -726,37 +627,6 @@ function makeMockFunctions(functionNames) {
return mockFunctions; 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.
* @param {Mock4JS.Mock} mockObject The mock to register callbacks against.
* @param {function(new:Object)} mockClass Constructor for the mocked class.
* @see registerMockGlobal
*/
function registerMockGlobals(mockObject, mockClass) {
var mockProxy = mockObject.proxy();
for (var func in mockClass.prototype) {
if (typeof mockClass.prototype[func] === 'function') {
registerMockGlobal(func, mockProxy, mockProxy[func]);
}
}
}
/**
* 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 * Overrides {@code chrome.send} for routing messages to javascript
* functions. Also falls back to sending with the original chrome object. * functions. Also falls back to sending with the original chrome object.
...@@ -1217,9 +1087,6 @@ function preloadJavascriptLibraries(testFixture, testName) { ...@@ -1217,9 +1087,6 @@ function preloadJavascriptLibraries(testFixture, testName) {
// Override globals at load time so they will be defined. // Override globals at load time so they will be defined.
assertTrue(deferGlobalOverrides); assertTrue(deferGlobalOverrides);
deferGlobalOverrides = false; deferGlobalOverrides = false;
for (var funcName in globalOverrides) {
overrideGlobal(funcName);
}
}, true); }, true);
currentTestCase = createTestCase(testFixture, testName); currentTestCase = createTestCase(testFixture, testName);
currentTestCase.preLoad(); currentTestCase.preLoad();
...@@ -1511,54 +1378,6 @@ function callFunctionWithSavedArgs(savedArgs, func) { ...@@ -1511,54 +1378,6 @@ function callFunctionWithSavedArgs(savedArgs, func) {
null, savedArgs, func, Array.prototype.slice.call(arguments, 2)); null, savedArgs, func, Array.prototype.slice.call(arguments, 2));
} }
/**
* CallGlobalAction as a subclass of CallFunctionAction looks up the original
* global object in |globalOverrides| using |funcName| as the key. This allows
* tests, which need to wait until a global function to be called in order to
* start the test to run the original function. When used with runAllActions
* or runAllActionsAsync, Mock4JS expectations may call start or continue the
* test after calling the original function.
* @param {?SaveMockArguments} savedArgs when non-null, saved arguments are
* passed to the global function |funcName|.
* @param {string} funcName The name of the global function to call.
* @param {Array} args Any arguments to pass to func.
* @constructor
* @extends {CallFunctionAction}
* @see globalOverrides
*/
function CallGlobalAction(savedArgs, funcName, args) {
CallFunctionAction.call(this, null, savedArgs, funcName, args);
}
CallGlobalAction.prototype = {
__proto__: CallFunctionAction.prototype,
/**
* Fetch and return the original global function to call.
* @return {Function} The global function to invoke.
* @override
*/
get func() {
var func = globalOverrides[this.func_].original;
assertNotEquals(undefined, func);
return func;
},
};
/**
* Syntactic sugar for use with will() on a Mock4JS.Mock.
* @param {SaveMockArguments} savedArgs Arguments saved with this object
* are passed to the global function |funcName|.
* @param {string} funcName The name of a registered mock global function to
* call when the method is invoked.
* @param {...*} var_args Arguments to pass when calling func.
* @return {CallGlobalAction} Action for use in Mock4JS will().
*/
function callGlobalWithSavedArgs(savedArgs, funcName) {
return new CallGlobalAction(
savedArgs, funcName, Array.prototype.slice.call(arguments, 2));
}
/** /**
* When to call testDone(). * When to call testDone().
* @enum {number} * @enum {number}
...@@ -1838,7 +1657,6 @@ function exportExpects() { ...@@ -1838,7 +1657,6 @@ function exportExpects() {
function exportMock4JsHelpers() { function exportMock4JsHelpers() {
exports.callFunction = callFunction; exports.callFunction = callFunction;
exports.callFunctionWithSavedArgs = callFunctionWithSavedArgs; exports.callFunctionWithSavedArgs = callFunctionWithSavedArgs;
exports.callGlobalWithSavedArgs = callGlobalWithSavedArgs;
exports.eqJSON = eqJSON; exports.eqJSON = eqJSON;
exports.eqToString = eqToString; exports.eqToString = eqToString;
exports.invokeCallback = invokeCallback; exports.invokeCallback = invokeCallback;
...@@ -1859,8 +1677,6 @@ exports.preloadJavascriptLibraries = preloadJavascriptLibraries; ...@@ -1859,8 +1677,6 @@ exports.preloadJavascriptLibraries = preloadJavascriptLibraries;
exports.setWaitUser = setWaitUser; exports.setWaitUser = setWaitUser;
exports.go = go; exports.go = go;
exports.registerMessageCallback = registerMessageCallback; exports.registerMessageCallback = registerMessageCallback;
exports.registerMockGlobals = registerMockGlobals;
exports.registerMockMessageCallbacks = registerMockMessageCallbacks;
exports.resetTestState = resetTestState; exports.resetTestState = resetTestState;
exports.runAccessibilityAudit = runAccessibilityAudit; exports.runAccessibilityAudit = runAccessibilityAudit;
exports.runAllActions = runAllActions; exports.runAllActions = runAllActions;
......
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