| // 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. |
| |
| /** |
| * Test fixture for cards.js. |
| * @constructor |
| * @extends {testing.Test} |
| */ |
| function GoogleNowCardsUnitTest () { |
| testing.Test.call(this); |
| } |
| |
| GoogleNowCardsUnitTest.prototype = { |
| __proto__: testing.Test.prototype, |
| |
| /** @override */ |
| extraLibraries: [ |
| 'cards.js' |
| ] |
| }; |
| |
| var testCardId = 'TEST CARD ID'; |
| var testNotification = {testNotificationField: 'TEST NOTIFICATION VALUE'}; |
| var expectedShowAlarmId = 'card-show-TEST CARD ID'; |
| var expectedHideAlarmId = 'card-hide-TEST CARD ID'; |
| var testActionUrls = {testField: 'TEST VALUE'}; |
| var testDismissals = [ |
| {testDismissalField: 'TEST DISMISSAL VALUE 1'}, |
| {testDismissalField: 'TEST DISMISSAL VALUE 2'} |
| ]; |
| var testGroupRank = 271; |
| var LOCATION_CARDS_LINK_THRESHOLD = 10; |
| var localStorage = {locationCardsShown: 0}; |
| |
| function setUpCardManagerTest(fixture) { |
| fixture.makeAndRegisterMockApis([ |
| 'chrome.alarms.clear', |
| 'chrome.alarms.create', |
| 'chrome.notifications.clear', |
| 'chrome.storage.local.set', |
| 'countLocationCard', |
| 'instrumented.alarms.onAlarm.addListener', |
| 'instrumented.notifications.create', |
| 'instrumented.notifications.update', |
| 'instrumented.storage.local.get', |
| 'tasks.add' |
| ]); |
| |
| chrome.runtime = {}; // No error. |
| |
| var onAlarmSavedArgs = new SaveMockArguments(); |
| fixture.mockApis.expects(once()). |
| instrumented_alarms_onAlarm_addListener( |
| onAlarmSavedArgs.match(ANYTHING)); |
| |
| var cardSet = buildCardSet(); |
| |
| Mock4JS.verifyAllMocks(); |
| |
| Date.now = function() { return 300000; }; |
| |
| var test = { |
| cardSet: cardSet, |
| alarmCallback: onAlarmSavedArgs.arguments [0] |
| }; |
| |
| return test; |
| } |
| |
| TEST_F('GoogleNowCardsUnitTest', 'BuildCardManager', function() { |
| // Tests that buildCardSet() call completes with no problems. |
| var test = setUpCardManagerTest(this); |
| |
| assertEquals('object', typeof test.cardSet); |
| assertEquals('function', typeof test.alarmCallback); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'CreateCardEmptyTrigger', function() { |
| // Creates a new card with empty trigger. |
| |
| // Setup and expectations. |
| var test = setUpCardManagerTest(this); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedHideAlarmId); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedShowAlarmId); |
| var chromeNotificationsCreateSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| instrumented_notifications_create( |
| chromeNotificationsCreateSavedArgs.match(eq(testCardId)), |
| chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)), |
| chromeNotificationsCreateSavedArgs.match(ANYTHING)). |
| will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId)); |
| |
| // Call tested method. |
| var notificationData = test.cardSet.update(testCardId, { |
| notification: testNotification, |
| actionUrls: testActionUrls, |
| dismissals: testDismissals, |
| groupRank: testGroupRank, |
| version: 0, |
| trigger: {}}); |
| |
| // Check the return value. |
| assertEquals( |
| JSON.stringify({ |
| actionUrls: testActionUrls, |
| cardCreateInfo: { |
| notification: testNotification, |
| hideTime: undefined, |
| version: 0 |
| }, |
| dismissals: testDismissals |
| }), |
| JSON.stringify(notificationData)); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'CreateCardHideTime', function() { |
| // Creates a new card with trigger specifying hide time. |
| |
| // Setup and expectations. |
| var test = setUpCardManagerTest(this); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedHideAlarmId); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedShowAlarmId); |
| var chromeNotificationsCreateSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| instrumented_notifications_create( |
| chromeNotificationsCreateSavedArgs.match(eq(testCardId)), |
| chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)), |
| chromeNotificationsCreateSavedArgs.match(ANYTHING)). |
| will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId)); |
| this.mockApis.expects(once()). |
| chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 1313000})); |
| |
| // Call tested method. |
| var notificationData = test.cardSet.update(testCardId, { |
| notification: testNotification, |
| actionUrls: testActionUrls, |
| dismissals: testDismissals, |
| groupRank: testGroupRank, |
| version: 0, |
| trigger: {hideTime: 1313000}}); |
| |
| // Check the return value. |
| assertEquals( |
| JSON.stringify({ |
| actionUrls: testActionUrls, |
| cardCreateInfo: { |
| notification: testNotification, |
| hideTime: 1313000, |
| version: 0 |
| }, |
| dismissals: testDismissals |
| }), |
| JSON.stringify(notificationData)); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'CreateCardHideTimeExpired', function() { |
| // Creates a new card with trigger specifying hide time which is in the past. |
| |
| // Setup and expectations. |
| var test = setUpCardManagerTest(this); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedHideAlarmId); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedShowAlarmId); |
| this.mockApis.expects(once()). |
| chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 299999})); |
| |
| // Call tested method. |
| var notificationData = test.cardSet.update(testCardId, { |
| notification: testNotification, |
| actionUrls: testActionUrls, |
| dismissals: testDismissals, |
| groupRank: testGroupRank, |
| version: 0, |
| trigger: {hideTime: 299999}}); |
| |
| // Check the return value. |
| assertEquals( |
| JSON.stringify({ |
| actionUrls: testActionUrls, |
| cardCreateInfo: { |
| notification: testNotification, |
| hideTime: 299999, |
| version: 0 |
| }, |
| dismissals: testDismissals |
| }), |
| JSON.stringify(notificationData)); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'UpdateCardSameVersion', function() { |
| // Updates a card with another card with same version. |
| |
| // Setup and expectations. |
| var test = setUpCardManagerTest(this); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedHideAlarmId); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedShowAlarmId); |
| var chromeNotificationsCreateSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| instrumented_notifications_update( |
| chromeNotificationsCreateSavedArgs.match(eq(testCardId)), |
| chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)), |
| chromeNotificationsCreateSavedArgs.match(ANYTHING)). |
| will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, true)); |
| |
| // Call tested method. |
| var notificationData = test.cardSet.update(testCardId, { |
| notification: testNotification, |
| actionUrls: testActionUrls, |
| dismissals: testDismissals, |
| groupRank: testGroupRank, |
| version: 0, |
| trigger: {}}, |
| 0); |
| |
| // Check the return value. |
| assertEquals( |
| JSON.stringify({ |
| actionUrls: testActionUrls, |
| cardCreateInfo: { |
| notification: testNotification, |
| version: 0, |
| previousVersion: 0 |
| }, |
| dismissals: testDismissals |
| }), |
| JSON.stringify(notificationData)); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'UpdateCardSameVersionHideTime', function() { |
| // Updates a card with another card with same version and specifying hide |
| // time. |
| |
| // Setup and expectations. |
| var test = setUpCardManagerTest(this); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedHideAlarmId); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedShowAlarmId); |
| var chromeNotificationsCreateSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| instrumented_notifications_update( |
| chromeNotificationsCreateSavedArgs.match(eq(testCardId)), |
| chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)), |
| chromeNotificationsCreateSavedArgs.match(ANYTHING)). |
| will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId)); |
| this.mockApis.expects(once()). |
| chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 1313000})); |
| |
| // Call tested method. |
| test.cardSet.update(testCardId, { |
| notification: testNotification, |
| actionUrls: testActionUrls, |
| dismissals: testDismissals, |
| groupRank: testGroupRank, |
| version: 0, |
| trigger: {hideTime: 1313000}}, |
| 0); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'UpdateCardDifferentVersion', function() { |
| // Updates a card with another card with different version. |
| |
| // Setup and expectations. |
| var test = setUpCardManagerTest(this); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedHideAlarmId); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedShowAlarmId); |
| this.mockApis.expects(once()). |
| instrumented_notifications_create( |
| testCardId, eqJSON(testNotification), ANYTHING); |
| |
| // Call tested method. |
| test.cardSet.update(testCardId, { |
| notification: testNotification, |
| actionUrls: testActionUrls, |
| dismissals: testDismissals, |
| groupRank: testGroupRank, |
| version: 0, |
| trigger: {}}, |
| 1); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'CreateCardTriggerShowNow', function() { |
| // Creates a new card with trigger that requires showing the card immediately. |
| |
| // Setup and expectations. |
| var test = setUpCardManagerTest(this); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedHideAlarmId); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedShowAlarmId); |
| this.mockApis.expects(once()). |
| instrumented_notifications_create( |
| testCardId, eqJSON(testNotification), ANYTHING); |
| |
| // Call tested method. |
| test.cardSet.update(testCardId, { |
| notification: testNotification, |
| actionUrls: testActionUrls, |
| dismissals: testDismissals, |
| groupRank: testGroupRank, |
| version: 0, |
| trigger: {showTime: Date.now()}}); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'CreateCardTriggerShowLater', function() { |
| // Creates a new card with trigger that requires showing the card later. |
| // We are supposed to schedule an alarm to show the notification later. |
| |
| // Setup and expectations. |
| var test = setUpCardManagerTest(this); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedHideAlarmId); |
| this.mockApis.expects(once()). |
| chrome_alarms_create(expectedShowAlarmId, eqJSON({when: 539000})); |
| |
| // Call tested method. |
| test.cardSet.update(testCardId, { |
| notification: testNotification, |
| actionUrls: testActionUrls, |
| dismissals: testDismissals, |
| groupRank: testGroupRank, |
| version: 0, |
| trigger: {showTime: 539000}}); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'ClearCard', function() { |
| // Clears a card. |
| |
| // Setup and expectations. |
| var test = setUpCardManagerTest(this); |
| this.mockApis.expects(once()). |
| chrome_notifications_clear(testCardId, ANYTHING); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedShowAlarmId); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedHideAlarmId); |
| |
| // Call tested method. |
| test.cardSet.clear(testCardId, false); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'onAlarmUnrecognized', function() { |
| // Tests onAlarm does nothing on an unrelated alarm. |
| var test = setUpCardManagerTest(this); |
| |
| // Call tested method. |
| test.alarmCallback({name: 'unrelated'}); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'onAlarmShowNoData', function() { |
| // Tests onAlarm for the 'show' alarm when there is no data for the card. |
| var test = setUpCardManagerTest(this); |
| |
| var tasksAddSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| tasks_add( |
| tasksAddSavedArgs.match(eq(SHOW_CARD_TASK_NAME)), |
| tasksAddSavedArgs.match(ANYTHING)). |
| will(invokeCallback(tasksAddSavedArgs,1)); |
| |
| var storageGetSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| instrumented_storage_local_get( |
| storageGetSavedArgs.match(eq('notificationsData')), |
| storageGetSavedArgs.match(ANYTHING)). |
| will(invokeCallback(storageGetSavedArgs, 1, {})); |
| |
| // Call tested method. |
| test.alarmCallback({name: expectedShowAlarmId}); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'onAlarmShowHasDataCreate', function() { |
| // Tests onAlarm for the 'show' alarm when there is data for the card. The |
| // notification will be created because there is no previous version. |
| var test = setUpCardManagerTest(this); |
| |
| var tasksAddSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| tasks_add( |
| tasksAddSavedArgs.match(eq(SHOW_CARD_TASK_NAME)), |
| tasksAddSavedArgs.match(ANYTHING)). |
| will(invokeCallback(tasksAddSavedArgs,1)); |
| |
| var testCardCreateInfo = { |
| notification: testNotification, |
| hideTime: 1313000, |
| version: 0}; |
| |
| var storageGetSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| instrumented_storage_local_get( |
| storageGetSavedArgs.match(eq('notificationsData')), |
| storageGetSavedArgs.match(ANYTHING)). |
| will(invokeCallback( |
| storageGetSavedArgs, |
| 1, |
| { |
| notificationsData: { |
| 'TEST CARD ID': { |
| actionUrls: testActionUrls, |
| cardCreateInfo: testCardCreateInfo}}})); |
| var chromeNotificationsCreateSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| instrumented_notifications_create( |
| chromeNotificationsCreateSavedArgs.match(eq(testCardId)), |
| chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)), |
| chromeNotificationsCreateSavedArgs.match(ANYTHING)). |
| will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId)); |
| this.mockApis.expects(once()).countLocationCard(eqJSON(testCardCreateInfo)); |
| this.mockApis.expects(once()). |
| chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 1313000})); |
| |
| // Call tested method. |
| test.alarmCallback({name: expectedShowAlarmId}); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'onAlarmShowHasDataUpdate', function() { |
| // Tests onAlarm for the 'show' alarm when there is data for the card. The |
| // notification will be updated because previous version is same as current. |
| var test = setUpCardManagerTest(this); |
| |
| var tasksAddSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| tasks_add( |
| tasksAddSavedArgs.match(eq(SHOW_CARD_TASK_NAME)), |
| tasksAddSavedArgs.match(ANYTHING)). |
| will(invokeCallback(tasksAddSavedArgs,1)); |
| |
| var storageGetSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| instrumented_storage_local_get( |
| storageGetSavedArgs.match(eq('notificationsData')), |
| storageGetSavedArgs.match(ANYTHING)). |
| will(invokeCallback( |
| storageGetSavedArgs, |
| 1, |
| { |
| notificationsData: { |
| 'TEST CARD ID': { |
| actionUrls: testActionUrls, |
| cardCreateInfo: { |
| notification: testNotification, |
| hideTime: 1313000, |
| version: 0, |
| previousVersion:0}}}})); |
| var chromeNotificationsCreateSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| instrumented_notifications_update( |
| testCardId, eqJSON(testNotification), ANYTHING); |
| |
| // Call tested method. |
| test.alarmCallback({name: expectedShowAlarmId}); |
| }); |
| |
| TEST_F('GoogleNowCardsUnitTest', 'onAlarmHide', function() { |
| // Tests onAlarm for the 'hide' alarm. |
| var test = setUpCardManagerTest(this); |
| var tasksAddSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| tasks_add( |
| tasksAddSavedArgs.match(eq(CLEAR_CARD_TASK_NAME)), |
| tasksAddSavedArgs.match(ANYTHING)). |
| will(invokeCallback(tasksAddSavedArgs,1)); |
| this.mockApis.expects(once()). |
| chrome_notifications_clear(testCardId, ANYTHING); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedShowAlarmId); |
| this.mockApis.expects(once()). |
| chrome_alarms_clear(expectedHideAlarmId); |
| var storageGetSavedArgs = new SaveMockArguments(); |
| this.mockApis.expects(once()). |
| instrumented_storage_local_get( |
| storageGetSavedArgs.match( |
| eqJSON(['notificationsData', 'notificationGroups'])), |
| storageGetSavedArgs.match(ANYTHING)). |
| will(invokeCallback( |
| storageGetSavedArgs, |
| 1, |
| { |
| notificationsData: { |
| 'TEST CARD ID': {testField: 'TEST VALUE'}, |
| 'TEST CARD ID 1': {testField: 'TEST VALUE 1'} |
| }, |
| notificationGroups: { |
| groupA: { |
| cards: [ |
| { |
| chromeNotificationId: 'TEST CARD ID', |
| testField: 'TEST VALUE', |
| }, |
| { |
| chromeNotificationId: 'TEST CARD ID 1', |
| testField: 'TEST VALUE 1', |
| } |
| ] |
| }, |
| groupB: { |
| cards: [ |
| { |
| chromeNotificationId: 'TEST CARD ID 0', |
| testField: 'TEST VALUE 0', |
| }, |
| { |
| chromeNotificationId: 'TEST CARD ID', |
| testField: 'TEST VALUE', |
| } |
| ] |
| } |
| }})); |
| this.mockApis.expects(once()). |
| chrome_storage_local_set(eqJSON({ |
| notificationsData: { |
| 'TEST CARD ID 1': {testField: 'TEST VALUE 1'} |
| }, |
| notificationGroups: { |
| groupA: { |
| cards: [ |
| { |
| chromeNotificationId: 'TEST CARD ID 1', |
| testField: 'TEST VALUE 1', |
| } |
| ] |
| }, |
| groupB: { |
| cards: [ |
| { |
| chromeNotificationId: 'TEST CARD ID 0', |
| testField: 'TEST VALUE 0', |
| } |
| ] |
| } |
| } |
| })); |
| |
| // Call tested method. |
| test.alarmCallback({name: expectedHideAlarmId}); |
| }); |