Commit 1833bc9d authored by dpapad's avatar dpapad Committed by Commit Bot

WebUI: Remove registerMessageCallback() helper from test_api.js

The helper is no longer used. Mocking chrome.send() calls is happening
via the use of browser proxies in all newer WebUIs.

Fixed: 1148403
Change-Id: I543ad1d36a0a92110182b4998380b3061c478f78
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2537770
Auto-Submit: dpapad <dpapad@chromium.org>
Commit-Queue: Esmael Elmoslimany <aee@chromium.org>
Reviewed-by: default avatarEsmael Elmoslimany <aee@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827997}
parent 613523f0
......@@ -74,7 +74,6 @@ if (include_js_tests) {
"assertions.js",
"async_gen.js",
"bookmarks/bookmarks_browsertest.js",
"chrome_send_browsertest.js",
"cr_components/cr_components_browsertest.js",
"cr_components/cr_components_v3_browsertest.js",
"cr_elements/cr_elements_browsertest.js",
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Tests to ensure that chrome.send mocking works as expected.
* @author scr@chromium.org (Sheridan Rawlins)
* @see test_api.js
*/
GEN('#include "chrome/test/data/webui/chrome_send_browsertest.h"');
GEN('#include "content/public/test/browser_test.h"');
/**
* Test fixture for chrome send WebUI testing.
* @constructor
* @extends {testing.Test}
*/
function ChromeSendWebUITest() {}
ChromeSendWebUITest.prototype = {
__proto__: testing.Test.prototype,
/**
* Generate a real C++ class; don't typedef.
* @type {?string}
* @override
*/
typedefCppFixture: null,
/** @inheritDoc */
browsePreload: DUMMY_URL,
};
// Test that chrome.send can be mocked outside the preLoad method.
TEST_F('ChromeSendWebUITest', 'NotInPreload', function() {
let invoked = false;
registerMessageCallback('checkSend', undefined, () => {
invoked = true;
});
chrome.send('checkSend');
assertTrue(invoked);
});
......@@ -9,11 +9,13 @@ import {PromiseResolver} from 'chrome://resources/js/promise_resolver.m.js';
const CHROME_SEND_NAME = 'echoMessage';
suite('CrModuleSendWithPromiseTest', function() {
const originalChromeSend = chrome.send;
let rejectPromises = false;
function whenChromeSendCalled(name) {
assertEquals(originalChromeSend, chrome.send);
return new Promise(function(resolve, reject) {
registerMessageCallback(name, null, resolve);
chrome.send = (_, args) => resolve(args);
});
}
......@@ -31,6 +33,10 @@ suite('CrModuleSendWithPromiseTest', function() {
/** @override */
teardown(function() {
rejectPromises = false;
// Restore original chrome.send(), as it is necessary for the testing
// framework to signal test completion.
chrome.send = originalChromeSend;
});
test('ResponseObject', function() {
......
......@@ -402,45 +402,6 @@ TestCase.prototype = {
};
/**
* Registry of javascript-defined callbacks for {@code chrome.send}.
* @type {Object}
*/
var sendCallbacks = {};
/**
* Registers the message, object and callback for {@code chrome.send}
* @param {string} name The name of the message to route to this |callback|.
* @param {Object} messageHandler Pass as |this| when calling the |callback|.
* @param {function(...)} callback Called by {@code chrome.send}.
* @see sendCallbacks
*/
function registerMessageCallback(name, messageHandler, callback) {
sendCallbacks[name] = [messageHandler, callback];
}
/**
* When preloading JavaScript libraries, this is true until the
* DOMContentLoaded event has been received as globals cannot be overridden
* until the page has loaded its JavaScript.
* @type {boolean}
*/
var deferGlobalOverrides = false;
/**
* 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.
*/
function send(messageName) {
var callback = sendCallbacks[messageName];
if (callback !== undefined) {
callback[1].apply(callback[0], Array.prototype.slice.call(arguments, 1));
} else {
this.__proto__.send.apply(this.__proto__, arguments);
}
}
/**
* true when testDone has been called.
* @type {boolean}
......@@ -828,50 +789,16 @@ function createTestCase(testFixture, testName) {
return new TestCase(testName, fixture, testBody);
}
/**
* Overrides the |chrome| object to enable mocking calls to chrome.send().
*/
function overrideChrome() {
if (originalChrome) {
console.error('chrome object already overridden');
return;
}
originalChrome = chrome;
/** @suppress {const|checkTypes} */
chrome = {
__proto__: originalChrome,
send: send,
};
}
/**
* Used by WebUIBrowserTest to preload the javascript libraries at the
* appropriate time for javascript injection into the current page. This
* creates a test case and calls its preLoad for any early initialization such
* as registering handlers before the page's javascript runs it's OnLoad
* method. This is called before the page is loaded, so the |chrome| object is
* not yet bound and this DOMContentLoaded listener will be called first to
* override |chrome| in order to route messages registered in |sendCallbacks|.
* method. This is called before the page is loaded.
* @param {string} testFixture The test fixture name.
* @param {string} testName The test name.
* @see sendCallbacks
*/
function preloadJavascriptLibraries(testFixture, testName) {
deferGlobalOverrides = true;
// The document seems to change from the point of preloading to the point of
// events (and doesn't fire), whereas the window does not. Listening to the
// capture phase allows this event to fire first.
window.addEventListener('DOMContentLoaded', function() {
if (chrome.send) {
overrideChrome();
}
// Override globals at load time so they will be defined.
assertTrue(deferGlobalOverrides);
deferGlobalOverrides = false;
}, true);
currentTestCase = createTestCase(testFixture, testName);
currentTestCase.preLoad();
}
......@@ -1334,7 +1261,6 @@ exportExpects();
exportMock4JsHelpers();
exports.preloadJavascriptLibraries = preloadJavascriptLibraries;
exports.setWaitUser = setWaitUser;
exports.registerMessageCallback = registerMessageCallback;
exports.resetTestState = resetTestState;
exports.runAllActions = runAllActions;
exports.runAllActionsAsync = runAllActionsAsync;
......
......@@ -37,21 +37,22 @@ WebUIResourceAsyncTest.prototype = {
};
TEST_F('WebUIResourceAsyncTest', 'SendWithPromise', function() {
/**
* TODO(dpapad): Move this helper method in test_api.js.
* @param {string} name chrome.send message name.
* @return {!Promise} Fires when chrome.send is called with the given message
* name.
*/
function whenChromeSendCalled(name) {
return new Promise(function(resolve, reject) {
registerMessageCallback(name, null, resolve);
});
}
suite('SendWithPromise', function() {
const originalChromeSend = chrome.send;
var rejectPromises = false;
/**
* @param {string} name chrome.send message name.
* @return {!Promise} Fires when chrome.send is called with the given
* message name.
*/
function whenChromeSendCalled(name) {
assertEquals(originalChromeSend, chrome.send);
return new Promise(function(resolve, reject) {
chrome.send = (_, args) => resolve(args);
});
}
setup(function() {
// Simulate a WebUI handler that echoes back all parameters passed to it.
// Rejects the promise depending on |rejectPromises|.
......@@ -61,8 +62,13 @@ TEST_F('WebUIResourceAsyncTest', 'SendWithPromise', function() {
null, [callbackId, !rejectPromises].concat(args.slice(1)));
});
});
teardown(function() {
rejectPromises = false;
// Restore original chrome.send(), as it is necessary for the testing
// framework to signal test completion.
chrome.send = originalChromeSend;
});
test('sendWithPromise_ResponseObject', function() {
......
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