1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Common test utilities.
7 function emptyMock() {}
9 // Container for event handlers added by mocked 'addListener' functions.
10 var mockEventHandlers = {};
13 * Mocks 'addListener' function of an API event. The mocked function will keep
15 * @param {Object} topLevelContainer Top-level container of the original
16 * function. Can be either 'chrome' or 'instrumented'.
17 * @param {string} eventIdentifier Event identifier, such as
18 * 'runtime.onSuspend'.
20 function mockChromeEvent(topLevelContainer, eventIdentifier) {
21 var eventIdentifierParts = eventIdentifier.split('.');
22 var eventName = eventIdentifierParts.pop();
23 var originalMethodContainer = topLevelContainer;
24 var mockEventContainer = mockEventHandlers;
25 eventIdentifierParts.forEach(function(fragment) {
26 originalMethodContainer =
27 originalMethodContainer[fragment] =
28 originalMethodContainer[fragment] || {};
30 mockEventContainer[fragment] =
31 mockEventContainer[fragment] || {};
34 mockEventContainer[eventName] = [];
35 originalMethodContainer[eventName] = {
36 addListener: function(callback) {
37 mockEventContainer[eventName].push(callback);
43 * Gets the array of event handlers added by a mocked 'addListener' function.
44 * @param {string} eventIdentifier Event identifier, such as
45 * 'runtime.onSuspend'.
46 * @return {Array.<Function>} Array of handlers.
48 function getMockHandlerContainer(eventIdentifier) {
49 var eventIdentifierParts = eventIdentifier.split('.');
50 var mockEventContainer = mockEventHandlers;
51 eventIdentifierParts.forEach(function(fragment) {
52 mockEventContainer = mockEventContainer[fragment];
55 return mockEventContainer;