OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Test fixture for cards.js. | 6 * Test fixture for cards.js. |
7 * @constructor | 7 * @constructor |
8 * @extends {testing.Test} | 8 * @extends {testing.Test} |
9 */ | 9 */ |
10 function GoogleNowCardsUnitTest () { | 10 function GoogleNowCardsUnitTest () { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 } | 69 } |
70 | 70 |
71 TEST_F('GoogleNowCardsUnitTest', 'BuildCardManager', function() { | 71 TEST_F('GoogleNowCardsUnitTest', 'BuildCardManager', function() { |
72 // Tests that buildCardSet() call completes with no problems. | 72 // Tests that buildCardSet() call completes with no problems. |
73 var test = setUpCardManagerTest(this); | 73 var test = setUpCardManagerTest(this); |
74 | 74 |
75 assertEquals('object', typeof test.cardSet); | 75 assertEquals('object', typeof test.cardSet); |
76 assertEquals('function', typeof test.alarmCallback); | 76 assertEquals('function', typeof test.alarmCallback); |
77 }); | 77 }); |
78 | 78 |
79 TEST_F('GoogleNowCardsUnitTest', 'CreateCardEmptyTrigger', function() { | |
80 // Creates a new card with empty trigger. | |
81 | |
82 // Setup and expectations. | |
83 var test = setUpCardManagerTest(this); | |
84 this.mockApis.expects(once()). | |
85 chrome_alarms_clear(expectedHideAlarmId); | |
86 this.mockApis.expects(once()). | |
87 chrome_alarms_clear(expectedShowAlarmId); | |
88 var chromeNotificationsCreateSavedArgs = new SaveMockArguments(); | |
89 this.mockApis.expects(once()). | |
90 instrumented_notifications_create( | |
91 chromeNotificationsCreateSavedArgs.match(eq(testCardId)), | |
92 chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)), | |
93 chromeNotificationsCreateSavedArgs.match(ANYTHING)). | |
94 will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId)); | |
95 | |
96 // Call tested method. | |
97 var notificationData = test.cardSet.update(testCardId, { | |
98 notification: testNotification, | |
99 actionUrls: testActionUrls, | |
100 dismissals: testDismissals, | |
101 groupRank: testGroupRank, | |
102 version: 0, | |
103 trigger: {}}); | |
104 | |
105 // Check the return value. | |
106 assertEquals( | |
107 JSON.stringify({ | |
108 actionUrls: testActionUrls, | |
109 cardCreateInfo: { | |
110 notification: testNotification, | |
111 hideTime: undefined, | |
112 version: 0 | |
113 }, | |
114 dismissals: testDismissals | |
115 }), | |
116 JSON.stringify(notificationData)); | |
117 }); | |
118 | |
119 TEST_F('GoogleNowCardsUnitTest', 'CreateCardHideTime', function() { | |
120 // Creates a new card with trigger specifying hide time. | |
121 | |
122 // Setup and expectations. | |
123 var test = setUpCardManagerTest(this); | |
124 this.mockApis.expects(once()). | |
125 chrome_alarms_clear(expectedHideAlarmId); | |
126 this.mockApis.expects(once()). | |
127 chrome_alarms_clear(expectedShowAlarmId); | |
128 var chromeNotificationsCreateSavedArgs = new SaveMockArguments(); | |
129 this.mockApis.expects(once()). | |
130 instrumented_notifications_create( | |
131 chromeNotificationsCreateSavedArgs.match(eq(testCardId)), | |
132 chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)), | |
133 chromeNotificationsCreateSavedArgs.match(ANYTHING)). | |
134 will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId)); | |
135 this.mockApis.expects(once()). | |
136 chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 1313000})); | |
137 | |
138 // Call tested method. | |
139 var notificationData = test.cardSet.update(testCardId, { | |
140 notification: testNotification, | |
141 actionUrls: testActionUrls, | |
142 dismissals: testDismissals, | |
143 groupRank: testGroupRank, | |
144 version: 0, | |
145 trigger: {hideTime: 1313000}}); | |
146 | |
147 // Check the return value. | |
148 assertEquals( | |
149 JSON.stringify({ | |
150 actionUrls: testActionUrls, | |
151 cardCreateInfo: { | |
152 notification: testNotification, | |
153 hideTime: 1313000, | |
154 version: 0 | |
155 }, | |
156 dismissals: testDismissals | |
157 }), | |
158 JSON.stringify(notificationData)); | |
159 }); | |
160 | |
161 TEST_F('GoogleNowCardsUnitTest', 'CreateCardHideTimeExpired', function() { | |
162 // Creates a new card with trigger specifying hide time which is in the past. | |
163 | |
164 // Setup and expectations. | |
165 var test = setUpCardManagerTest(this); | |
166 this.mockApis.expects(once()). | |
167 chrome_alarms_clear(expectedHideAlarmId); | |
168 this.mockApis.expects(once()). | |
169 chrome_alarms_clear(expectedShowAlarmId); | |
170 this.mockApis.expects(once()). | |
171 chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 299999})); | |
172 | |
173 // Call tested method. | |
174 var notificationData = test.cardSet.update(testCardId, { | |
175 notification: testNotification, | |
176 actionUrls: testActionUrls, | |
177 dismissals: testDismissals, | |
178 groupRank: testGroupRank, | |
179 version: 0, | |
180 trigger: {hideTime: 299999}}); | |
181 | |
182 // Check the return value. | |
183 assertEquals( | |
184 JSON.stringify({ | |
185 actionUrls: testActionUrls, | |
186 cardCreateInfo: { | |
187 notification: testNotification, | |
188 hideTime: 299999, | |
189 version: 0 | |
190 }, | |
191 dismissals: testDismissals | |
192 }), | |
193 JSON.stringify(notificationData)); | |
194 }); | |
195 | |
196 TEST_F('GoogleNowCardsUnitTest', 'UpdateCardSameVersion', function() { | |
197 // Updates a card with another card with same version. | |
198 | |
199 // Setup and expectations. | |
200 var test = setUpCardManagerTest(this); | |
201 this.mockApis.expects(once()). | |
202 chrome_alarms_clear(expectedHideAlarmId); | |
203 this.mockApis.expects(once()). | |
204 chrome_alarms_clear(expectedShowAlarmId); | |
205 var chromeNotificationsCreateSavedArgs = new SaveMockArguments(); | |
206 this.mockApis.expects(once()). | |
207 instrumented_notifications_update( | |
208 chromeNotificationsCreateSavedArgs.match(eq(testCardId)), | |
209 chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)), | |
210 chromeNotificationsCreateSavedArgs.match(ANYTHING)). | |
211 will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, true)); | |
212 | |
213 // Call tested method. | |
214 var notificationData = test.cardSet.update(testCardId, { | |
215 notification: testNotification, | |
216 actionUrls: testActionUrls, | |
217 dismissals: testDismissals, | |
218 groupRank: testGroupRank, | |
219 version: 0, | |
220 trigger: {}}, | |
221 0); | |
222 | |
223 // Check the return value. | |
224 assertEquals( | |
225 JSON.stringify({ | |
226 actionUrls: testActionUrls, | |
227 cardCreateInfo: { | |
228 notification: testNotification, | |
229 version: 0, | |
230 previousVersion: 0 | |
231 }, | |
232 dismissals: testDismissals | |
233 }), | |
234 JSON.stringify(notificationData)); | |
235 }); | |
236 | |
237 TEST_F('GoogleNowCardsUnitTest', 'UpdateCardSameVersionHideTime', function() { | |
238 // Updates a card with another card with same version and specifying hide | |
239 // time. | |
240 | |
241 // Setup and expectations. | |
242 var test = setUpCardManagerTest(this); | |
243 this.mockApis.expects(once()). | |
244 chrome_alarms_clear(expectedHideAlarmId); | |
245 this.mockApis.expects(once()). | |
246 chrome_alarms_clear(expectedShowAlarmId); | |
247 var chromeNotificationsCreateSavedArgs = new SaveMockArguments(); | |
248 this.mockApis.expects(once()). | |
249 instrumented_notifications_update( | |
250 chromeNotificationsCreateSavedArgs.match(eq(testCardId)), | |
251 chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)), | |
252 chromeNotificationsCreateSavedArgs.match(ANYTHING)). | |
253 will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId)); | |
254 this.mockApis.expects(once()). | |
255 chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 1313000})); | |
256 | |
257 // Call tested method. | |
258 test.cardSet.update(testCardId, { | |
259 notification: testNotification, | |
260 actionUrls: testActionUrls, | |
261 dismissals: testDismissals, | |
262 groupRank: testGroupRank, | |
263 version: 0, | |
264 trigger: {hideTime: 1313000}}, | |
265 0); | |
266 }); | |
267 | |
268 TEST_F('GoogleNowCardsUnitTest', 'UpdateCardDifferentVersion', function() { | |
269 // Updates a card with another card with different version. | |
270 | |
271 // Setup and expectations. | |
272 var test = setUpCardManagerTest(this); | |
273 this.mockApis.expects(once()). | |
274 chrome_alarms_clear(expectedHideAlarmId); | |
275 this.mockApis.expects(once()). | |
276 chrome_alarms_clear(expectedShowAlarmId); | |
277 this.mockApis.expects(once()). | |
278 instrumented_notifications_create( | |
279 testCardId, eqJSON(testNotification), ANYTHING); | |
280 | |
281 // Call tested method. | |
282 test.cardSet.update(testCardId, { | |
283 notification: testNotification, | |
284 actionUrls: testActionUrls, | |
285 dismissals: testDismissals, | |
286 groupRank: testGroupRank, | |
287 version: 0, | |
288 trigger: {}}, | |
289 1); | |
290 }); | |
291 | |
292 TEST_F('GoogleNowCardsUnitTest', 'CreateCardTriggerShowNow', function() { | |
293 // Creates a new card with trigger that requires showing the card immediately. | |
294 | |
295 // Setup and expectations. | |
296 var test = setUpCardManagerTest(this); | |
297 this.mockApis.expects(once()). | |
298 chrome_alarms_clear(expectedHideAlarmId); | |
299 this.mockApis.expects(once()). | |
300 chrome_alarms_clear(expectedShowAlarmId); | |
301 this.mockApis.expects(once()). | |
302 instrumented_notifications_create( | |
303 testCardId, eqJSON(testNotification), ANYTHING); | |
304 | |
305 // Call tested method. | |
306 test.cardSet.update(testCardId, { | |
307 notification: testNotification, | |
308 actionUrls: testActionUrls, | |
309 dismissals: testDismissals, | |
310 groupRank: testGroupRank, | |
311 version: 0, | |
312 trigger: {showTime: Date.now()}}); | |
313 }); | |
314 | |
315 TEST_F('GoogleNowCardsUnitTest', 'CreateCardTriggerShowLater', function() { | |
316 // Creates a new card with trigger that requires showing the card later. | |
317 // We are supposed to schedule an alarm to show the notification later. | |
318 | |
319 // Setup and expectations. | |
320 var test = setUpCardManagerTest(this); | |
321 this.mockApis.expects(once()). | |
322 chrome_alarms_clear(expectedHideAlarmId); | |
323 this.mockApis.expects(once()). | |
324 chrome_alarms_create(expectedShowAlarmId, eqJSON({when: 539000})); | |
325 | |
326 // Call tested method. | |
327 test.cardSet.update(testCardId, { | |
328 notification: testNotification, | |
329 actionUrls: testActionUrls, | |
330 dismissals: testDismissals, | |
331 groupRank: testGroupRank, | |
332 version: 0, | |
333 trigger: {showTime: 539000}}); | |
334 }); | |
335 | |
336 TEST_F('GoogleNowCardsUnitTest', 'ClearCard', function() { | |
337 // Clears a card. | |
338 | |
339 // Setup and expectations. | |
340 var test = setUpCardManagerTest(this); | |
341 this.mockApis.expects(once()). | |
342 chrome_notifications_clear(testCardId, ANYTHING); | |
343 this.mockApis.expects(once()). | |
344 chrome_alarms_clear(expectedShowAlarmId); | |
345 this.mockApis.expects(once()). | |
346 chrome_alarms_clear(expectedHideAlarmId); | |
347 | |
348 // Call tested method. | |
349 test.cardSet.clear(testCardId, false); | |
350 }); | |
351 | |
352 TEST_F('GoogleNowCardsUnitTest', 'onAlarmUnrecognized', function() { | 79 TEST_F('GoogleNowCardsUnitTest', 'onAlarmUnrecognized', function() { |
353 // Tests onAlarm does nothing on an unrelated alarm. | 80 // Tests onAlarm does nothing on an unrelated alarm. |
354 var test = setUpCardManagerTest(this); | 81 var test = setUpCardManagerTest(this); |
355 | 82 |
356 // Call tested method. | 83 // Call tested method. |
357 test.alarmCallback({name: 'unrelated'}); | 84 test.alarmCallback({name: 'unrelated'}); |
358 }); | 85 }); |
359 | |
360 TEST_F('GoogleNowCardsUnitTest', 'onAlarmShowNoData', function() { | |
361 // Tests onAlarm for the 'show' alarm when there is no data for the card. | |
362 var test = setUpCardManagerTest(this); | |
363 | |
364 var tasksAddSavedArgs = new SaveMockArguments(); | |
365 this.mockApis.expects(once()). | |
366 tasks_add( | |
367 tasksAddSavedArgs.match(eq(SHOW_CARD_TASK_NAME)), | |
368 tasksAddSavedArgs.match(ANYTHING)). | |
369 will(invokeCallback(tasksAddSavedArgs,1)); | |
370 | |
371 var storageGetSavedArgs = new SaveMockArguments(); | |
372 this.mockApis.expects(once()). | |
373 instrumented_storage_local_get( | |
374 storageGetSavedArgs.match(eq('notificationsData')), | |
375 storageGetSavedArgs.match(ANYTHING)). | |
376 will(invokeCallback(storageGetSavedArgs, 1, {})); | |
377 | |
378 // Call tested method. | |
379 test.alarmCallback({name: expectedShowAlarmId}); | |
380 }); | |
381 | |
382 TEST_F('GoogleNowCardsUnitTest', 'onAlarmShowHasDataCreate', function() { | |
383 // Tests onAlarm for the 'show' alarm when there is data for the card. The | |
384 // notification will be created because there is no previous version. | |
385 var test = setUpCardManagerTest(this); | |
386 | |
387 var tasksAddSavedArgs = new SaveMockArguments(); | |
388 this.mockApis.expects(once()). | |
389 tasks_add( | |
390 tasksAddSavedArgs.match(eq(SHOW_CARD_TASK_NAME)), | |
391 tasksAddSavedArgs.match(ANYTHING)). | |
392 will(invokeCallback(tasksAddSavedArgs,1)); | |
393 | |
394 var testCardCreateInfo = { | |
395 notification: testNotification, | |
396 hideTime: 1313000, | |
397 version: 0}; | |
398 | |
399 var storageGetSavedArgs = new SaveMockArguments(); | |
400 this.mockApis.expects(once()). | |
401 instrumented_storage_local_get( | |
402 storageGetSavedArgs.match(eq('notificationsData')), | |
403 storageGetSavedArgs.match(ANYTHING)). | |
404 will(invokeCallback( | |
405 storageGetSavedArgs, | |
406 1, | |
407 { | |
408 notificationsData: { | |
409 'TEST CARD ID': { | |
410 actionUrls: testActionUrls, | |
411 cardCreateInfo: testCardCreateInfo}}})); | |
412 var chromeNotificationsCreateSavedArgs = new SaveMockArguments(); | |
413 this.mockApis.expects(once()). | |
414 instrumented_notifications_create( | |
415 chromeNotificationsCreateSavedArgs.match(eq(testCardId)), | |
416 chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)), | |
417 chromeNotificationsCreateSavedArgs.match(ANYTHING)). | |
418 will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId)); | |
419 this.mockApis.expects(once()).countLocationCard(eqJSON(testCardCreateInfo)); | |
420 this.mockApis.expects(once()). | |
421 chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 1313000})); | |
422 | |
423 // Call tested method. | |
424 test.alarmCallback({name: expectedShowAlarmId}); | |
425 }); | |
426 | |
427 TEST_F('GoogleNowCardsUnitTest', 'onAlarmShowHasDataUpdate', function() { | |
428 // Tests onAlarm for the 'show' alarm when there is data for the card. The | |
429 // notification will be updated because previous version is same as current. | |
430 var test = setUpCardManagerTest(this); | |
431 | |
432 var tasksAddSavedArgs = new SaveMockArguments(); | |
433 this.mockApis.expects(once()). | |
434 tasks_add( | |
435 tasksAddSavedArgs.match(eq(SHOW_CARD_TASK_NAME)), | |
436 tasksAddSavedArgs.match(ANYTHING)). | |
437 will(invokeCallback(tasksAddSavedArgs,1)); | |
438 | |
439 var storageGetSavedArgs = new SaveMockArguments(); | |
440 this.mockApis.expects(once()). | |
441 instrumented_storage_local_get( | |
442 storageGetSavedArgs.match(eq('notificationsData')), | |
443 storageGetSavedArgs.match(ANYTHING)). | |
444 will(invokeCallback( | |
445 storageGetSavedArgs, | |
446 1, | |
447 { | |
448 notificationsData: { | |
449 'TEST CARD ID': { | |
450 actionUrls: testActionUrls, | |
451 cardCreateInfo: { | |
452 notification: testNotification, | |
453 hideTime: 1313000, | |
454 version: 0, | |
455 previousVersion:0}}}})); | |
456 var chromeNotificationsCreateSavedArgs = new SaveMockArguments(); | |
457 this.mockApis.expects(once()). | |
458 instrumented_notifications_update( | |
459 testCardId, eqJSON(testNotification), ANYTHING); | |
460 | |
461 // Call tested method. | |
462 test.alarmCallback({name: expectedShowAlarmId}); | |
463 }); | |
464 | |
465 TEST_F('GoogleNowCardsUnitTest', 'onAlarmHide', function() { | |
466 // Tests onAlarm for the 'hide' alarm. | |
467 var test = setUpCardManagerTest(this); | |
468 var tasksAddSavedArgs = new SaveMockArguments(); | |
469 this.mockApis.expects(once()). | |
470 tasks_add( | |
471 tasksAddSavedArgs.match(eq(CLEAR_CARD_TASK_NAME)), | |
472 tasksAddSavedArgs.match(ANYTHING)). | |
473 will(invokeCallback(tasksAddSavedArgs,1)); | |
474 this.mockApis.expects(once()). | |
475 chrome_notifications_clear(testCardId, ANYTHING); | |
476 this.mockApis.expects(once()). | |
477 chrome_alarms_clear(expectedShowAlarmId); | |
478 this.mockApis.expects(once()). | |
479 chrome_alarms_clear(expectedHideAlarmId); | |
480 var storageGetSavedArgs = new SaveMockArguments(); | |
481 this.mockApis.expects(once()). | |
482 instrumented_storage_local_get( | |
483 storageGetSavedArgs.match( | |
484 eqJSON(['notificationsData', 'notificationGroups'])), | |
485 storageGetSavedArgs.match(ANYTHING)). | |
486 will(invokeCallback( | |
487 storageGetSavedArgs, | |
488 1, | |
489 { | |
490 notificationsData: { | |
491 'TEST CARD ID': {testField: 'TEST VALUE'}, | |
492 'TEST CARD ID 1': {testField: 'TEST VALUE 1'} | |
493 }, | |
494 notificationGroups: { | |
495 groupA: { | |
496 cards: [ | |
497 { | |
498 chromeNotificationId: 'TEST CARD ID', | |
499 testField: 'TEST VALUE', | |
500 }, | |
501 { | |
502 chromeNotificationId: 'TEST CARD ID 1', | |
503 testField: 'TEST VALUE 1', | |
504 } | |
505 ] | |
506 }, | |
507 groupB: { | |
508 cards: [ | |
509 { | |
510 chromeNotificationId: 'TEST CARD ID 0', | |
511 testField: 'TEST VALUE 0', | |
512 }, | |
513 { | |
514 chromeNotificationId: 'TEST CARD ID', | |
515 testField: 'TEST VALUE', | |
516 } | |
517 ] | |
518 } | |
519 }})); | |
520 this.mockApis.expects(once()). | |
521 chrome_storage_local_set(eqJSON({ | |
522 notificationsData: { | |
523 'TEST CARD ID 1': {testField: 'TEST VALUE 1'} | |
524 }, | |
525 notificationGroups: { | |
526 groupA: { | |
527 cards: [ | |
528 { | |
529 chromeNotificationId: 'TEST CARD ID 1', | |
530 testField: 'TEST VALUE 1', | |
531 } | |
532 ] | |
533 }, | |
534 groupB: { | |
535 cards: [ | |
536 { | |
537 chromeNotificationId: 'TEST CARD ID 0', | |
538 testField: 'TEST VALUE 0', | |
539 } | |
540 ] | |
541 } | |
542 } | |
543 })); | |
544 | |
545 // Call tested method. | |
546 test.alarmCallback({name: expectedHideAlarmId}); | |
547 }); | |
OLD | NEW |