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

Unified Diff: chrome/browser/resources/google_now/background_unittest.gtestjs

Issue 5151418134560768: Adding first unit tests with mocks (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 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/background_unittest.gtestjs
diff --git a/chrome/browser/resources/google_now/background_unittest.gtestjs b/chrome/browser/resources/google_now/background_unittest.gtestjs
index 149c51ca41a0cffb74a206df15e8aa9e1aa4161d..0a56566da81976bf9fbc1f069ee1f175c582d3be 100644
--- a/chrome/browser/resources/google_now/background_unittest.gtestjs
+++ b/chrome/browser/resources/google_now/background_unittest.gtestjs
@@ -40,3 +40,339 @@ TEST_F('GoogleNowBackgroundUnitTest', 'AreTasksConflicting', function() {
testTaskPair(RETRY_DISMISS_TASK_NAME, RETRY_DISMISS_TASK_NAME, true);
});
+/**
+ * Mocks global functions and APIs that initialize() depends upon.
+ * @param {Test} fixture Test fixture.
+ */
+function mockInitializeDependencies(fixture) {
+ fixture.makeAndRegisterMockGlobals([
+ 'recordEvent',
+ 'showWelcomeToast',
+ 'startPollingCards']);
+ fixture.makeAndRegisterMockApis(
+ ['storage.get', 'chrome.identity.getAuthToken']);
+}
+
+TEST_F(
+ 'GoogleNowBackgroundUnitTest',
+ 'Initialize_ToastStateEmpty1',
+ function() {
+ // Tests the case when toast state is empty and NOTIFICATION_CARDS_URL is
+ // not set. In this case, the function should quietly exit after finding
+ // out that NOTIFICATION_CARDS_URL is empty.
+
+ // Setup and expectations.
+ var testToastState = {};
+ NOTIFICATION_CARDS_URL = undefined;
+
+ mockInitializeDependencies(this);
+
+ this.mockGlobals.expects(once()).recordEvent(
+ DiagnosticEvent.EXTENSION_START);
+ var storage_get_SavedArgs = new SaveMockArguments();
arv (Not doing code reviews) 2013/07/11 20:59:44 no underscores
vadimt 2013/07/11 21:28:10 I believe this is a special case. I mock an API ca
arv (Not doing code reviews) 2013/07/11 21:56:04 I don't think I understand. How can the api depend
vadimt 2013/07/11 22:18:24 Expanded story. This is a test for initialize() me
+ this.mockApis.expects(once()).
+ storage_get(
arv (Not doing code reviews) 2013/07/11 20:59:44 no underscores
vadimt 2013/07/11 21:28:10 Same story. storage_get is a name automatically ge
+ storage_get_SavedArgs.match(eq('toastState')),
+ storage_get_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(storage_get_SavedArgs, 1, testToastState));
+
+ // Invoking the tested function.
+ initialize();
+ });
+
+TEST_F(
+ 'GoogleNowBackgroundUnitTest',
+ 'Initialize_ToastStateEmpty2',
+ function() {
+ // Tests the case when toast state is empty and NOTIFICATION_CARDS_URL is
+ // set, but getAuthToken fails most likely because the user is not signed
+ // in. In this case, the function should quietly exit after finding out
+ // that getAuthToken fails.
+
+ // Setup and expectations.
+ var testToastState = {};
+ NOTIFICATION_CARDS_URL = 'https://some.server.url.com';
+ var testIdentityToken = undefined;
+
+ mockInitializeDependencies(this);
+
+ this.mockGlobals.expects(once()).recordEvent(
+ DiagnosticEvent.EXTENSION_START);
+ var storage_get_SavedArgs = new SaveMockArguments();
+ this.mockApis.expects(once()).
+ storage_get(
+ storage_get_SavedArgs.match(eq('toastState')),
+ storage_get_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(storage_get_SavedArgs, 1, testToastState));
+ var chrome_identity_getAuthToken_SavedArgs = new SaveMockArguments();
+ this.mockApis.expects(once()).
+ chrome_identity_getAuthToken(
+ chrome_identity_getAuthToken_SavedArgs.match(
arv (Not doing code reviews) 2013/07/11 20:59:44 incorrect indentation
vadimt 2013/07/11 21:28:10 Done.
+ eqJSON({interactive: false})),
+ chrome_identity_getAuthToken_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(
+ chrome_identity_getAuthToken_SavedArgs, 1, testIdentityToken));
+
+ // Invoking the tested function.
+ initialize();
+ });
+
+TEST_F(
+ 'GoogleNowBackgroundUnitTest',
+ 'Initialize_ToastStateEmpty3',
+ function() {
+ // Tests the case when toast state is empty and NOTIFICATION_CARDS_URL is
+ // set, and getAuthToken succeeds. In this case, the function should
+ // invoke showWelcomeToast().
+
+ // Setup and expectations.
+ var testToastState = {};
+ NOTIFICATION_CARDS_URL = 'https://some.server.url.com';
+ var testIdentityToken = 'some identity token';
+
+ mockInitializeDependencies(this);
+
+ this.mockGlobals.expects(once()).recordEvent(
+ DiagnosticEvent.EXTENSION_START);
+ var storage_get_SavedArgs = new SaveMockArguments();
+ this.mockApis.expects(once()).
+ storage_get(
+ storage_get_SavedArgs.match(eq('toastState')),
+ storage_get_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(storage_get_SavedArgs, 1, testToastState));
+ var chrome_identity_getAuthToken_SavedArgs = new SaveMockArguments();
+ this.mockApis.expects(once()).
+ chrome_identity_getAuthToken(
+ chrome_identity_getAuthToken_SavedArgs.match(
+ eqJSON({interactive: false})),
+ chrome_identity_getAuthToken_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(
+ chrome_identity_getAuthToken_SavedArgs, 1, testIdentityToken));
+ this.mockGlobals.expects(once()).showWelcomeToast();
+
+ // Invoking the tested function.
+ initialize();
+ });
+
+TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_ToastStateYes', function() {
+ // Tests the case when the user has answered "yes" to the toast in the past.
+ // In this case, the function should invoke startPollingCards().
+
+ // Setup and expectations.
+ var testToastState = {toastState: ToastOptionResponse.CHOSE_YES};
+
+ mockInitializeDependencies(this);
+
+ this.mockGlobals.expects(once()).recordEvent(DiagnosticEvent.EXTENSION_START);
+ var storage_get_SavedArgs = new SaveMockArguments();
+ this.mockApis.expects(once()).
+ storage_get(
+ storage_get_SavedArgs.match(eq('toastState')),
+ storage_get_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(storage_get_SavedArgs, 1, testToastState));
+ this.mockGlobals.expects(once()).startPollingCards();
+
+ // Invoking the tested function.
+ initialize();
+});
+
+TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_ToastStateNo', function() {
+ // Tests the case when the user has answered "no" to the toast in the past.
+ // In this case, the function should do nothing.
+
+ // Setup and expectations.
+ var testToastState = {toastState: ToastOptionResponse.CHOSE_NO};
+
+ mockInitializeDependencies(this);
+
+ this.mockGlobals.expects(once()).recordEvent(DiagnosticEvent.EXTENSION_START);
+ var storage_get_SavedArgs = new SaveMockArguments();
+ this.mockApis.expects(once()).
+ storage_get(
+ storage_get_SavedArgs.match(eq('toastState')),
+ storage_get_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(storage_get_SavedArgs, 1, testToastState));
+
+ // Invoking the tested function.
+ initialize();
+});
+
+/**
+ * Mocks global functions and APIs that onNotificationClicked() depends upon.
+ * @param {Test} fixture Test fixture.
+ */
+function mockOnNotificationClickedDependencies(fixture) {
+ fixture.makeAndRegisterMockApis([
+ 'storage.get',
+ 'chrome.tabs.create',
+ 'chrome.windows.create']);
+}
+
+TEST_F(
+ 'GoogleNowBackgroundUnitTest',
+ 'OnNotificationClicked_NoData',
+ function() {
+ // Tests the case when there is no data associated with notification id.
+ // In this case, the function should do nothing.
+
+ // Setup and expectations.
+ var testNotificationId = 'TEST_ID';
+ var testNotificationData = {};
+
+ mockOnNotificationClickedDependencies(this);
+ this.makeMockLocalFunctions(['selector']);
+
+ var storage_get_SavedArgs = new SaveMockArguments();
+ this.mockApis.expects(once()).
+ storage_get(
+ storage_get_SavedArgs.match(eq('notificationsData')),
+ storage_get_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(storage_get_SavedArgs, 1, testNotificationData));
+
+ // Invoking the tested function.
+ onNotificationClicked(
+ testNotificationId, this.mockLocalFunctions.functions().selector);
+ });
+
+TEST_F(
+ 'GoogleNowBackgroundUnitTest',
+ 'OnNotificationClicked_ActionUrlsNotObject',
+ function() {
+ // Tests the case when the data associated with notification id is not an
+ // object, probably because of a malformed server response.
+ // In this case, the function should do nothing.
+
+ // Setup and expectations.
+ var testActionUrls = 'string, not object';
+ var testNotificationId = 'TEST_ID';
+ var testNotificationData = {
+ notificationsData: {'TEST_ID': {actionUrls: testActionUrls}}
+ };
+
+ mockOnNotificationClickedDependencies(this);
+ this.makeMockLocalFunctions(['selector']);
+
+ var storage_get_SavedArgs = new SaveMockArguments();
+ this.mockApis.expects(once()).
+ storage_get(
+ storage_get_SavedArgs.match(eq('notificationsData')),
+ storage_get_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(storage_get_SavedArgs, 1, testNotificationData));
+
+ // Invoking the tested function.
+ onNotificationClicked(
+ testNotificationId, this.mockLocalFunctions.functions().selector);
+ });
+
+TEST_F(
+ 'GoogleNowBackgroundUnitTest',
+ 'OnNotificationClicked_UrlNotString',
+ function() {
+ // Tests the case when selector returns a non-string, probably because of
+ // a malformed server response.
+ // In this case, the function should do nothing.
+
+ // Setup and expectations.
+ var testActionUrls = {testField: 'TEST VALUE'};
+ var testNotificationId = 'TEST_ID';
+ var testNotificationData = {
+ notificationsData: {'TEST_ID': {actionUrls: testActionUrls}}
+ };
+ var testActionUrl = {};
+
+ mockOnNotificationClickedDependencies(this);
+ this.makeMockLocalFunctions(['selector']);
+
+ var storage_get_SavedArgs = new SaveMockArguments();
+ this.mockApis.expects(once()).
+ storage_get(
+ storage_get_SavedArgs.match(eq('notificationsData')),
+ storage_get_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(storage_get_SavedArgs, 1, testNotificationData));
+ this.mockLocalFunctions.expects(once()).selector(testActionUrls).will(
+ returnValue(testActionUrl));
+
+ // Invoking the tested function.
+ onNotificationClicked(
+ testNotificationId, this.mockLocalFunctions.functions().selector);
+ });
+
+TEST_F(
+ 'GoogleNowBackgroundUnitTest',
+ 'OnNotificationClicked_TabCreateSuccess',
+ function() {
+ // Tests the selected URL is OK and crome.tabs.create suceeds.
+
+ // Setup and expectations.
+ var testActionUrls = {testField: 'TEST VALUE'};
+ var testNotificationId = 'TEST_ID';
+ var testNotificationData = {
+ notificationsData: {'TEST_ID': {actionUrls: testActionUrls}}
+ };
+ var testActionUrl = 'http://testurl.com';
+ var testCreatedTab = {};
+
+ mockOnNotificationClickedDependencies(this);
+ this.makeMockLocalFunctions(['selector']);
+
+ var storage_get_SavedArgs = new SaveMockArguments();
+ this.mockApis.expects(once()).
+ storage_get(
+ storage_get_SavedArgs.match(eq('notificationsData')),
+ storage_get_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(storage_get_SavedArgs, 1, testNotificationData));
+ this.mockLocalFunctions.expects(once()).selector(testActionUrls).will(
+ returnValue(testActionUrl));
+ var chrome_tabs_create_SavedArgs = new SaveMockArguments();
+ this.mockApis.expects(once()).
+ chrome_tabs_create(
+ chrome_tabs_create_SavedArgs.match(eqJSON({url: testActionUrl})),
+ chrome_tabs_create_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(chrome_tabs_create_SavedArgs, 1, testCreatedTab));
+
+ // Invoking the tested function.
+ onNotificationClicked(
+ testNotificationId, this.mockLocalFunctions.functions().selector);
+ });
+
+TEST_F(
+ 'GoogleNowBackgroundUnitTest',
+ 'OnNotificationClicked_TabCreateFail',
+ function() {
+ // Tests the selected URL is OK and crome.tabs.create fails.
+ // In this case, the function should invoke chrome.windows.create as a
+ // second attempt.
+
+ // Setup and expectations.
+ var testActionUrls = {testField: 'TEST VALUE'};
+ var testNotificationId = 'TEST_ID';
+ var testNotificationData = {
+ notificationsData: {'TEST_ID': {actionUrls: testActionUrls}}
+ };
+ var testActionUrl = 'http://testurl.com';
+ var testCreatedTab = undefined; // chrome.tabs.create fails
+
+ mockOnNotificationClickedDependencies(this);
+ this.makeMockLocalFunctions(['selector']);
+
+ var storage_get_SavedArgs = new SaveMockArguments();
+ this.mockApis.expects(once()).
+ storage_get(
+ storage_get_SavedArgs.match(eq('notificationsData')),
+ storage_get_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(storage_get_SavedArgs, 1, testNotificationData));
+ this.mockLocalFunctions.expects(once()).selector(testActionUrls).will(
+ returnValue(testActionUrl));
+ var chrome_tabs_create_SavedArgs = new SaveMockArguments();
+ this.mockApis.expects(once()).
+ chrome_tabs_create(
+ chrome_tabs_create_SavedArgs.match(eqJSON({url: testActionUrl})),
+ chrome_tabs_create_SavedArgs.match(ANYTHING)).
+ will(invokeCallback(chrome_tabs_create_SavedArgs, 1, testCreatedTab));
+ this.mockApis.expects(once()).chrome_windows_create(
+ eqJSON({url: testActionUrl}));
+
+ // Invoking the tested function.
+ onNotificationClicked(
+ testNotificationId, this.mockLocalFunctions.functions().selector);
+ });

Powered by Google App Engine
This is Rietveld 408576698