Index: chrome/browser/resources/google_now/common_test_util.js |
diff --git a/chrome/browser/resources/google_now/common_test_util.js b/chrome/browser/resources/google_now/common_test_util.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..aeba31b1f4f84d45b9cdb7ab163688a13cc3d6f7 |
--- /dev/null |
+++ b/chrome/browser/resources/google_now/common_test_util.js |
@@ -0,0 +1,56 @@ |
+// Copyright 2013 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. |
+ |
+// Common test utilities. |
+ |
+function emptyMock() {} |
skare_
2013/08/29 00:03:37
the extension is assembled with sourcefile concate
vadimt
2013/08/29 00:28:22
This is not a part of the extension. There are 3 i
|
+ |
+// Container for event handlers added by mocked 'addListener' functions. |
+var mockEventHandlers = {}; |
+ |
+/** |
+ * Mocks 'addListener' function of an API event. The mocked function will add |
+ * handlers to arrays stored inside mockEventHandlers. |
skare_
2013/08/29 00:03:37
hide implementation details? e.g. "mocked function
vadimt
2013/08/29 00:28:22
Done.
|
+ * @param {Object} topLevelContainer Top-level container of the original |
+ * function. Can be either chrome or instrumented. |
skare_
2013/08/29 00:03:37
nit: maybe
'chrome' or 'instrumented'.
for consi
vadimt
2013/08/29 00:28:22
Done.
|
+ * @param {string} eventIdentifier Event identifier, such as |
+ * 'runtime.onSuspend'. |
+ */ |
+function mockChromeEvent(topLevelContainer, eventIdentifier) { |
+ var eventIdentifierParts = eventIdentifier.split('.'); |
+ var eventName = eventIdentifierParts.pop(); |
skare_
2013/08/29 00:03:37
maybe check that (eventName != undefined) or (even
vadimt
2013/08/29 00:28:22
Then it will crash. This is just a test anyways.
|
+ var originalMethodContainer = topLevelContainer; |
+ var mockEventContainer = mockEventHandlers; |
+ eventIdentifierParts.forEach(function(fragment) { |
skare_
2013/08/29 00:03:37
js styleguide suggests using normal iteration over
vadimt
2013/08/29 00:28:22
This is because for-in can lead to errors, not bec
|
+ originalMethodContainer = |
+ originalMethodContainer[fragment] = |
skare_
2013/08/29 00:03:37
quick check on this and 29-31 -- intended to be (x
vadimt
2013/08/29 00:28:22
This is exactly what happens:
originalMethodContai
|
+ originalMethodContainer[fragment] || {}; |
+ mockEventContainer = |
+ mockEventContainer[fragment] = |
+ mockEventContainer[fragment] || {}; |
+ }); |
+ |
+ mockEventContainer[eventName] = []; |
+ originalMethodContainer[eventName] = { |
+ addListener: function(callback) { |
+ mockEventContainer[eventName].push(callback); |
+ } |
+ }; |
+} |
+ |
+/** |
+ * Gets the array of event handlers added by a mocked 'addListener' function. |
+ * @param {string} eventIdentifier Event identifier, such as |
+ * 'runtime.onSuspend'. |
+ * @return {Array.<Function>} Array of handlers. |
+ */ |
+function getMockHandlerContainer(eventIdentifier) { |
+ var eventIdentifierParts = eventIdentifier.split('.'); |
+ var mockEventContainer = mockEventHandlers; |
+ eventIdentifierParts.forEach(function(fragment) { |
+ mockEventContainer = mockEventContainer[fragment]; |
+ }); |
+ |
+ return mockEventContainer; |
+} |