Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6147)

Unified Diff: chrome/browser/resources/google_now/common_test_util.js

Issue 23477006: Unit tests for utility.js (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: robliao@ comments Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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;
+}

Powered by Google App Engine
This is Rietveld 408576698