Home | History | Annotate | Download | only in google_now
      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.
      4 
      5 /**
      6  * Test fixture for cards.js.
      7  * @constructor
      8  * @extends {testing.Test}
      9  */
     10 function GoogleNowCardsUnitTest () {
     11   testing.Test.call(this);
     12 }
     13 
     14 GoogleNowCardsUnitTest.prototype = {
     15   __proto__: testing.Test.prototype,
     16 
     17   /** @override */
     18   extraLibraries: [
     19     'cards.js'
     20   ]
     21 };
     22 
     23 var testCardId = 'TEST CARD ID';
     24 var testNotification = {testNotificationField: 'TEST NOTIFICATION VALUE'};
     25 var expectedShowAlarmId = 'card-show-TEST CARD ID';
     26 var expectedHideAlarmId = 'card-hide-TEST CARD ID';
     27 var testActionUrls = {testField: 'TEST VALUE'};
     28 var testDismissal = {testDismissalField: 'TEST DISMISSAL VALUE'};
     29 
     30 function setUpCardManagerTest(fixture) {
     31   fixture.makeAndRegisterMockApis([
     32       'chrome.alarms.clear',
     33       'chrome.alarms.create',
     34       'chrome.notifications.clear',
     35       'instrumented.alarms.onAlarm.addListener',
     36       'instrumented.notifications.create',
     37       'instrumented.notifications.update',
     38       'instrumented.storage.local.get'
     39   ]);
     40 
     41   chrome.runtime = {}; // No error.
     42 
     43   var onAlarmSavedArgs = new SaveMockArguments();
     44   fixture.mockApis.expects(once()).
     45       instrumented_alarms_onAlarm_addListener(
     46           onAlarmSavedArgs.match(ANYTHING));
     47 
     48   var cardSet = buildCardSet();
     49 
     50   Mock4JS.verifyAllMocks();
     51 
     52   Date.now = function() { return 300000; };
     53 
     54   var test = {
     55     cardSet: cardSet,
     56     alarmCallback: onAlarmSavedArgs.arguments [0]
     57   };
     58 
     59   return test;
     60 }
     61 
     62 TEST_F('GoogleNowCardsUnitTest', 'BuildCardManager', function() {
     63   // Tests that buildCardSet() call completes with no problems.
     64   var test = setUpCardManagerTest(this);
     65 
     66   assertEquals('object', typeof test.cardSet);
     67   assertEquals('function', typeof test.alarmCallback);
     68 });
     69 
     70 TEST_F('GoogleNowCardsUnitTest', 'CreateCard', function() {
     71   // Creates a new card with no trigger.
     72 
     73   // Setup and expectations.
     74   var test = setUpCardManagerTest(this);
     75   this.mockApis.expects(once()).
     76       chrome_alarms_clear(expectedHideAlarmId);
     77   this.mockApis.expects(once()).
     78       chrome_alarms_clear(expectedShowAlarmId);
     79   var chromeNotificationsCreateSavedArgs = new SaveMockArguments();
     80   this.mockApis.expects(once()).
     81       instrumented_notifications_create(
     82           chromeNotificationsCreateSavedArgs.match(eq(testCardId)),
     83           chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)),
     84           chromeNotificationsCreateSavedArgs.match(ANYTHING)).
     85       will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId));
     86 
     87   // Call tested method.
     88   var notificationData = test.cardSet.update({
     89     notificationId: testCardId,
     90     notification: testNotification,
     91     actionUrls: testActionUrls,
     92     dismissal: testDismissal,
     93     version: 0});
     94 
     95   // Check the return value.
     96   assertEquals(
     97       JSON.stringify({
     98         actionUrls: testActionUrls,
     99         cardCreateInfo: {
    100           notification: testNotification,
    101           timeHide: undefined,
    102           version: 0
    103         },
    104         dismissalParameters: testDismissal
    105       }),
    106     JSON.stringify(notificationData));
    107 });
    108 
    109 TEST_F('GoogleNowCardsUnitTest', 'CreateCardEmptyTrigger', function() {
    110   // Creates a new card with empty trigger.
    111 
    112   // Setup and expectations.
    113   var test = setUpCardManagerTest(this);
    114   this.mockApis.expects(once()).
    115       chrome_alarms_clear(expectedHideAlarmId);
    116   this.mockApis.expects(once()).
    117       chrome_alarms_clear(expectedShowAlarmId);
    118   this.mockApis.expects(once()).
    119       instrumented_notifications_create(
    120           testCardId, eqJSON(testNotification), ANYTHING);
    121 
    122   // Call tested method.
    123   var notificationData = test.cardSet.update({
    124     notificationId: testCardId,
    125     notification: testNotification,
    126     actionUrls: testActionUrls,
    127     dismissal: testDismissal,
    128     version: 0,
    129     trigger: {}});
    130 
    131   // Check the return value.
    132   assertEquals(
    133       JSON.stringify({
    134         actionUrls: testActionUrls,
    135         cardCreateInfo: {
    136           notification: testNotification,
    137           timeHide: undefined,
    138           version: 0
    139         },
    140         dismissalParameters: testDismissal
    141      }),
    142     JSON.stringify(notificationData));
    143 });
    144 
    145 TEST_F('GoogleNowCardsUnitTest', 'CreateCardHideTime', function() {
    146   // Creates a new card with trigger specifying hide time.
    147 
    148   // Setup and expectations.
    149   var test = setUpCardManagerTest(this);
    150   this.mockApis.expects(once()).
    151       chrome_alarms_clear(expectedHideAlarmId);
    152   this.mockApis.expects(once()).
    153       chrome_alarms_clear(expectedShowAlarmId);
    154   var chromeNotificationsCreateSavedArgs = new SaveMockArguments();
    155   this.mockApis.expects(once()).
    156       instrumented_notifications_create(
    157           chromeNotificationsCreateSavedArgs.match(eq(testCardId)),
    158           chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)),
    159           chromeNotificationsCreateSavedArgs.match(ANYTHING)).
    160       will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId));
    161   this.mockApis.expects(once()).
    162       chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 1313000}));
    163 
    164   // Call tested method.
    165   var notificationData = test.cardSet.update({
    166     notificationId: testCardId,
    167     notification: testNotification,
    168     actionUrls: testActionUrls,
    169     dismissal: testDismissal,
    170     version: 0,
    171     trigger: {hideTimeSec: 1013}});
    172 
    173   // Check the return value.
    174   assertEquals(
    175       JSON.stringify({
    176         actionUrls: testActionUrls,
    177         cardCreateInfo: {
    178           notification: testNotification,
    179           timeHide: 1313000,
    180           version: 0
    181         },
    182         dismissalParameters: testDismissal
    183       }),
    184     JSON.stringify(notificationData));
    185 });
    186 
    187 TEST_F('GoogleNowCardsUnitTest', 'UpdateCardSameVersion', function() {
    188   // Updates a card with another card with same version.
    189 
    190   // Setup and expectations.
    191   var test = setUpCardManagerTest(this);
    192   this.mockApis.expects(once()).
    193       chrome_alarms_clear(expectedHideAlarmId);
    194   this.mockApis.expects(once()).
    195       chrome_alarms_clear(expectedShowAlarmId);
    196   var chromeNotificationsCreateSavedArgs = new SaveMockArguments();
    197   this.mockApis.expects(once()).
    198       instrumented_notifications_update(
    199           chromeNotificationsCreateSavedArgs.match(eq(testCardId)),
    200           chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)),
    201           chromeNotificationsCreateSavedArgs.match(ANYTHING)).
    202       will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, true));
    203 
    204   // Call tested method.
    205   var notificationData = test.cardSet.update({
    206     notificationId: testCardId,
    207     notification: testNotification,
    208     actionUrls: testActionUrls,
    209     dismissal: testDismissal,
    210     version: 0},
    211       0);
    212 
    213   // Check the return value.
    214   assertEquals(
    215       JSON.stringify({
    216         actionUrls: testActionUrls,
    217         cardCreateInfo: {
    218           notification: testNotification,
    219           version: 0,
    220           previousVersion: 0
    221         },
    222         dismissalParameters: testDismissal
    223       }),
    224     JSON.stringify(notificationData));
    225 });
    226 
    227 TEST_F('GoogleNowCardsUnitTest', 'UpdateCardSameVersionHideTime', function() {
    228   // Updates a card with another card with same version and specifying hide
    229   // time.
    230 
    231   // Setup and expectations.
    232   var test = setUpCardManagerTest(this);
    233   this.mockApis.expects(once()).
    234       chrome_alarms_clear(expectedHideAlarmId);
    235   this.mockApis.expects(once()).
    236       chrome_alarms_clear(expectedShowAlarmId);
    237   var chromeNotificationsCreateSavedArgs = new SaveMockArguments();
    238   this.mockApis.expects(once()).
    239       instrumented_notifications_update(
    240           chromeNotificationsCreateSavedArgs.match(eq(testCardId)),
    241           chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)),
    242           chromeNotificationsCreateSavedArgs.match(ANYTHING)).
    243       will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId));
    244   this.mockApis.expects(once()).
    245       chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 1313000}));
    246 
    247   // Call tested method.
    248   test.cardSet.update({
    249                         notificationId: testCardId,
    250                         notification: testNotification,
    251                         actionUrls: testActionUrls,
    252                         dismissal: testDismissal,
    253                         version: 0,
    254                         trigger: {hideTimeSec: 1013}},
    255                       0);
    256 });
    257 
    258 TEST_F('GoogleNowCardsUnitTest', 'UpdateCardDifferentVersion', function() {
    259   // Updates a card with another card with different version.
    260 
    261   // Setup and expectations.
    262   var test = setUpCardManagerTest(this);
    263   this.mockApis.expects(once()).
    264       chrome_alarms_clear(expectedHideAlarmId);
    265   this.mockApis.expects(once()).
    266       chrome_alarms_clear(expectedShowAlarmId);
    267   this.mockApis.expects(once()).
    268       instrumented_notifications_create(
    269           testCardId, eqJSON(testNotification), ANYTHING);
    270 
    271   // Call tested method.
    272   test.cardSet.update({
    273                         notificationId: testCardId,
    274                         notification: testNotification,
    275                         actionUrls: testActionUrls,
    276                         dismissal: testDismissal,
    277                         version: 0},
    278                       1);
    279 });
    280 
    281 TEST_F('GoogleNowCardsUnitTest', 'CreateCardTriggerShowNow', function() {
    282   // Creates a new card with trigger that requires showing the card immediately.
    283 
    284   // Setup and expectations.
    285   var test = setUpCardManagerTest(this);
    286   this.mockApis.expects(once()).
    287       chrome_alarms_clear(expectedHideAlarmId);
    288   this.mockApis.expects(once()).
    289       chrome_alarms_clear(expectedShowAlarmId);
    290   this.mockApis.expects(once()).
    291       instrumented_notifications_create(
    292           testCardId, eqJSON(testNotification), ANYTHING);
    293 
    294   // Call tested method.
    295   test.cardSet.update({
    296     notificationId: testCardId,
    297     notification: testNotification,
    298     actionUrls: testActionUrls,
    299     dismissal: testDismissal,
    300     version: 0,
    301     trigger: {showTimeSec: 0}});
    302 });
    303 
    304 TEST_F('GoogleNowCardsUnitTest', 'CreateCardTriggerShowLater', function() {
    305   // Creates a new card with trigger that requires showing the card later.
    306   // We are supposed to schedule an alarm to show the notification later.
    307 
    308   // Setup and expectations.
    309   var test = setUpCardManagerTest(this);
    310   this.mockApis.expects(once()).
    311       chrome_alarms_clear(expectedHideAlarmId);
    312   this.mockApis.expects(once()).
    313       chrome_alarms_create(expectedShowAlarmId, eqJSON({when: 539000}));
    314 
    315   // Call tested method.
    316   test.cardSet.update({
    317     notificationId: testCardId,
    318     notification: testNotification,
    319     actionUrls: testActionUrls,
    320     dismissal: testDismissal,
    321     version: 0,
    322     trigger: {showTimeSec: 239}});
    323 });
    324 
    325 TEST_F('GoogleNowCardsUnitTest', 'ClearCard', function() {
    326   // Clears a card.
    327 
    328   // Setup and expectations.
    329   var test = setUpCardManagerTest(this);
    330   this.mockApis.expects(once()).
    331       chrome_notifications_clear(testCardId, ANYTHING);
    332   this.mockApis.expects(once()).
    333       chrome_alarms_clear(expectedShowAlarmId);
    334   this.mockApis.expects(once()).
    335       chrome_alarms_clear(expectedHideAlarmId);
    336 
    337   // Call tested method.
    338   test.cardSet.clear(testCardId);
    339 });
    340 
    341 TEST_F('GoogleNowCardsUnitTest', 'onAlarmUnrecognized', function() {
    342   // Tests onAlarm does nothing on an unrelated alarm.
    343   var test = setUpCardManagerTest(this);
    344 
    345   // Call tested method.
    346   test.alarmCallback({name: 'unrelated'});
    347 });
    348 
    349 TEST_F('GoogleNowCardsUnitTest', 'onAlarmShowNoData', function() {
    350   // Tests onAlarm for the 'show' alarm when there is no data for the card.
    351   var test = setUpCardManagerTest(this);
    352   var storageGetSavedArgs = new SaveMockArguments();
    353   this.mockApis.expects(once()).
    354       instrumented_storage_local_get(
    355           storageGetSavedArgs.match(eq('notificationsData')),
    356           storageGetSavedArgs.match(ANYTHING)).
    357       will(invokeCallback(storageGetSavedArgs, 1, {}));
    358 
    359   // Call tested method.
    360   test.alarmCallback({name: expectedShowAlarmId});
    361 });
    362 
    363 TEST_F('GoogleNowCardsUnitTest', 'onAlarmShowHasDataCreate', function() {
    364   // Tests onAlarm for the 'show' alarm when there is data for the card. The
    365   // notification will be created because there is no previous version.
    366   var test = setUpCardManagerTest(this);
    367   var storageGetSavedArgs = new SaveMockArguments();
    368   this.mockApis.expects(once()).
    369       instrumented_storage_local_get(
    370           storageGetSavedArgs.match(eq('notificationsData')),
    371           storageGetSavedArgs.match(ANYTHING)).
    372       will(invokeCallback(
    373           storageGetSavedArgs,
    374           1,
    375           {
    376             notificationsData: {
    377               'TEST CARD ID': {
    378               actionUrls: testActionUrls,
    379               cardCreateInfo: {
    380                 notification: testNotification,
    381                 timeHide: 1313000,
    382                 version: 0}}}}));
    383   var chromeNotificationsCreateSavedArgs = new SaveMockArguments();
    384   this.mockApis.expects(once()).
    385       instrumented_notifications_create(
    386           chromeNotificationsCreateSavedArgs.match(eq(testCardId)),
    387           chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)),
    388           chromeNotificationsCreateSavedArgs.match(ANYTHING)).
    389       will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId));
    390   this.mockApis.expects(once()).
    391       chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 1313000}));
    392 
    393   // Call tested method.
    394   test.alarmCallback({name: expectedShowAlarmId});
    395 });
    396 
    397 TEST_F('GoogleNowCardsUnitTest', 'onAlarmShowHasDataUpdate', function() {
    398   // Tests onAlarm for the 'show' alarm when there is data for the card. The
    399   // notification will be updated because previous version is same as current.
    400   var test = setUpCardManagerTest(this);
    401   var storageGetSavedArgs = new SaveMockArguments();
    402   this.mockApis.expects(once()).
    403       instrumented_storage_local_get(
    404           storageGetSavedArgs.match(eq('notificationsData')),
    405           storageGetSavedArgs.match(ANYTHING)).
    406       will(invokeCallback(
    407           storageGetSavedArgs,
    408           1,
    409           {
    410             notificationsData: {
    411               'TEST CARD ID': {
    412               actionUrls: testActionUrls,
    413               cardCreateInfo: {
    414                 notification: testNotification,
    415                 timeHide: 1313000,
    416                 version: 0,
    417                 previousVersion:0}}}}));
    418   var chromeNotificationsCreateSavedArgs = new SaveMockArguments();
    419   this.mockApis.expects(once()).
    420       instrumented_notifications_update(
    421           testCardId, eqJSON(testNotification), ANYTHING);
    422 
    423   // Call tested method.
    424   test.alarmCallback({name: expectedShowAlarmId});
    425 });
    426 
    427 TEST_F('GoogleNowCardsUnitTest', 'onAlarmHide', function() {
    428   // Tests onAlarm for the 'hide' alarm.
    429   var test = setUpCardManagerTest(this);
    430   this.mockApis.expects(once()).
    431       chrome_notifications_clear(testCardId, ANYTHING);
    432 
    433   // Call tested method.
    434   test.alarmCallback({name: expectedHideAlarmId});
    435 });
    436