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