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.
6 * Test fixture for cards.js.
8 * @extends {testing.Test}
10 function GoogleNowCardsUnitTest () {
11 testing.Test.call(this);
14 GoogleNowCardsUnitTest.prototype = {
15 __proto__: testing.Test.prototype,
19 'common_test_util.js',
24 // Test implementation of a function provided by utility.js.
25 function verify(condition, message) {
26 assertTrue(condition, message);
29 var testCardId = 'TEST CARD ID';
30 var testNotificationIdA = 'TEST CARD ID-A';
31 var testNotificationIdB = 'TEST CARD ID-B';
32 var testNotificationA = {
33 testNotificationField: 'TEST NOTIFICATION VALUE A',
36 var testNotificationB = {
37 testNotificationField: 'TEST NOTIFICATION VALUE B',
42 var expectedAlarmId = 'card-TEST CARD ID';
43 var testActionUrlsA = {testField: 'TEST VALUE A'};
44 var testActionUrlsB = {testField: 'TEST VALUE B'};
45 var testDismissalA = {testDismissalField: 'TEST DISMISSAL VALUE A'};
46 var testDismissalB = {testDismissalField: 'TEST DISMISSAL VALUE B'};
47 var EXPLANATORY_CARDS_LINK_THRESHOLD = 4;
48 var localStorage = {explanatoryCardsShown: 0};
49 var notificationUpdateSuccessful = true;
50 var notificationUpdateFailed = false;
52 function setUpCardManagerTest(fixture) {
53 fixture.makeAndRegisterMockApis([
54 'chrome.alarms.clear',
55 'chrome.alarms.create',
56 'instrumented.notifications.clear',
57 'chrome.storage.local.set',
58 'countExplanatoryCard',
59 'instrumented.alarms.onAlarm.addListener',
60 'instrumented.notifications.create',
61 'instrumented.notifications.update',
62 'instrumented.storage.local.get',
66 chrome.runtime = {}; // No error.
68 var onAlarmSavedArgs = new SaveMockArguments();
69 fixture.mockApis.expects(once()).
70 instrumented_alarms_onAlarm_addListener(
71 onAlarmSavedArgs.match(ANYTHING));
73 var cardSet = buildCardSet();
75 Mock4JS.verifyAllMocks();
77 Date.now = function() { return 300000; };
81 alarmCallback: onAlarmSavedArgs.arguments [0]
87 TEST_F('GoogleNowCardsUnitTest', 'BuildCardManager', function() {
88 // Tests that buildCardSet() call completes with no problems.
89 var test = setUpCardManagerTest(this);
91 assertEquals('object', typeof test.cardSet);
92 assertEquals('function', typeof test.alarmCallback);
95 TEST_F('GoogleNowCardsUnitTest', 'UpdateSimpleHideFuture', function() {
96 // Update a simple card with the hide event in future.
98 var testUncombinedNotification = {
99 receivedNotification: {
100 notificationId: testNotificationIdA,
101 chromeNotificationId: testCardId,
102 chromeNotificationOptions: testNotificationA,
103 actionUrls: testActionUrlsA,
104 dismissal: testDismissalA,
105 groupName: groupNameA
110 // Setup and expectations.
111 var test = setUpCardManagerTest(this);
112 this.mockApis.expects(once()).
113 chrome_alarms_clear(expectedAlarmId);
114 var chromeNotificationsUpdateSavedArgs = new SaveMockArguments();
115 this.mockApis.expects(once()).
116 instrumented_notifications_update(
117 chromeNotificationsUpdateSavedArgs.match(eq(testCardId)),
118 chromeNotificationsUpdateSavedArgs.match(eqJSON(testNotificationA)),
119 chromeNotificationsUpdateSavedArgs.match(ANYTHING)).
121 chromeNotificationsUpdateSavedArgs, 2, notificationUpdateSuccessful));
122 this.mockApis.expects(once()).
123 chrome_alarms_create(expectedAlarmId, eqJSON({when: 300001}));
125 // Call tested method.
126 var notificationData = test.cardSet.update(
127 testCardId, [testUncombinedNotification], {});
129 // Check the return value.
132 actionUrls: testActionUrlsA,
134 combinedCard: [testUncombinedNotification]
136 JSON.stringify(notificationData));
139 TEST_F('GoogleNowCardsUnitTest', 'CreateSimpleHideFuture', function() {
140 // Create a simple card with the hide event in future.
142 var testUncombinedNotification = {
143 receivedNotification: {
144 notificationId: testNotificationIdA,
145 chromeNotificationId: testCardId,
146 chromeNotificationOptions: testNotificationA,
147 actionUrls: testActionUrlsA,
148 dismissal: testDismissalA,
149 groupName: groupNameA
154 // Setup and expectations.
155 var test = setUpCardManagerTest(this);
156 this.makeMockLocalFunctions(['onCardShown']);
158 this.mockApis.expects(once()).
159 chrome_alarms_clear(expectedAlarmId);
161 // notifications.update will return false, which will trigger
162 // notifications.create.
163 var chromeNotificationsUpdateSavedArgs = new SaveMockArguments();
164 this.mockApis.expects(once()).
165 instrumented_notifications_update(
166 chromeNotificationsUpdateSavedArgs.match(eq(testCardId)),
167 chromeNotificationsUpdateSavedArgs.match(eqJSON(testNotificationA)),
168 chromeNotificationsUpdateSavedArgs.match(ANYTHING)).
170 chromeNotificationsUpdateSavedArgs, 2, notificationUpdateFailed));
171 var chromeNotificationsCreateSavedArgs = new SaveMockArguments();
172 this.mockApis.expects(once()).
173 instrumented_notifications_create(
174 chromeNotificationsCreateSavedArgs.match(eq(testCardId)),
175 chromeNotificationsCreateSavedArgs.match(eqJSON(testNotificationA)),
176 chromeNotificationsCreateSavedArgs.match(ANYTHING)).
177 will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId));
179 this.mockLocalFunctions.expects(once()).
180 onCardShown(eqJSON(testUncombinedNotification.receivedNotification));
182 this.mockApis.expects(once()).
183 chrome_alarms_create(expectedAlarmId, eqJSON({when: 300001}));
185 // Call tested method.
186 var notificationData = test.cardSet.update(
188 [testUncombinedNotification],
190 this.mockLocalFunctions.functions().onCardShown);
192 // Check the return value.
195 actionUrls: testActionUrlsA,
197 combinedCard: [testUncombinedNotification]
199 JSON.stringify(notificationData));
202 TEST_F('GoogleNowCardsUnitTest', 'CreateSimpleShowFuture', function() {
203 // Create a simple card with the show event in future. Should create an alarm
204 // to fire at the show time.
206 var testUncombinedNotification = {
207 receivedNotification: {
208 notificationId: testNotificationIdA,
209 chromeNotificationId: testCardId,
210 chromeNotificationOptions: testNotificationA,
211 actionUrls: testActionUrlsA,
212 dismissal: testDismissalA,
213 groupName: groupNameA
219 // Setup and expectations.
220 var test = setUpCardManagerTest(this);
222 this.mockApis.expects(once()).
223 chrome_alarms_clear(expectedAlarmId);
224 this.mockApis.expects(once()).
225 instrumented_notifications_clear(testCardId, ANYTHING);
226 this.mockApis.expects(once()).
227 chrome_alarms_create(expectedAlarmId, eqJSON({when: 300001}));
229 // Call tested method.
230 var notificationData = test.cardSet.update(
231 testCardId, [testUncombinedNotification], {});
233 // Check the return value.
237 combinedCard: [testUncombinedNotification]
239 JSON.stringify(notificationData));
242 TEST_F('GoogleNowCardsUnitTest', 'UpdateSimpleHidePast', function() {
243 // Update a simple card with the hide event in the past (actually, present).
244 // Should clear the notification.
246 var testUncombinedNotification = {
247 receivedNotification: {
248 notificationId: testNotificationIdA,
249 chromeNotificationId: testCardId,
250 chromeNotificationOptions: testNotificationA,
251 actionUrls: testActionUrlsA,
252 dismissal: testDismissalA,
253 groupName: groupNameA
258 // Setup and expectations.
259 var test = setUpCardManagerTest(this);
260 this.mockApis.expects(once()).
261 chrome_alarms_clear(expectedAlarmId);
262 this.mockApis.expects(once()).
263 instrumented_notifications_clear(testCardId, ANYTHING);
267 cards: [{chromeNotificationId: 'ID1'}, {chromeNotificationId: testCardId}]
270 cards: [{chromeNotificationId: testCardId}, {chromeNotificationId: 'ID2'}]
274 // Call tested method.
275 var notificationData = test.cardSet.update(
276 testCardId, [testUncombinedNotification], groups);
278 // Check the return value.
279 assertEquals(undefined, notificationData);
281 // Check that groups were cleaned up.
282 var expectedCleanedGroups = {
284 cards: [{chromeNotificationId: 'ID1'}]
287 cards: [{chromeNotificationId: 'ID2'}]
291 assertEquals(JSON.stringify(expectedCleanedGroups), JSON.stringify(groups));
294 TEST_F('GoogleNowCardsUnitTest', 'UpdateComplex', function() {
295 // Update a combined card that contains 2 uncombined cards. The card from
296 // group B has higher priority and wins.
298 var testUncombinedNotificationA = {
299 receivedNotification: {
300 notificationId: testNotificationIdA,
301 chromeNotificationId: testCardId,
302 chromeNotificationOptions: testNotificationA,
303 actionUrls: testActionUrlsA,
304 dismissal: testDismissalA,
305 groupName: groupNameA
310 var testUncombinedNotificationB = {
311 receivedNotification: {
312 notificationId: testNotificationIdB,
313 chromeNotificationId: testCardId,
314 chromeNotificationOptions: testNotificationB,
315 actionUrls: testActionUrlsB,
316 dismissal: testDismissalB,
317 groupName: groupNameB
322 // Setup and expectations.
323 var test = setUpCardManagerTest(this);
324 this.mockApis.expects(once()).
325 chrome_alarms_clear(expectedAlarmId);
326 var chromeNotificationsUpdateSavedArgs = new SaveMockArguments();
327 this.mockApis.expects(once()).
328 instrumented_notifications_update(
329 chromeNotificationsUpdateSavedArgs.match(eq(testCardId)),
330 chromeNotificationsUpdateSavedArgs.match(eqJSON(testNotificationB)),
331 chromeNotificationsUpdateSavedArgs.match(ANYTHING)).
333 chromeNotificationsUpdateSavedArgs, 2, notificationUpdateSuccessful));
334 this.mockApis.expects(once()).
335 chrome_alarms_create(expectedAlarmId, eqJSON({when: 300001}));
337 // Call tested method.
338 var notificationData = test.cardSet.update(
340 [testUncombinedNotificationA, testUncombinedNotificationB],
343 // Check the return value.
346 actionUrls: testActionUrlsB,
348 combinedCard: [testUncombinedNotificationA, testUncombinedNotificationB]
350 JSON.stringify(notificationData));
353 TEST_F('GoogleNowCardsUnitTest', 'DismissSimple', function() {
354 // Dismiss a combined card that contains 1 uncombined currently visible card.
355 // The notification should be deleted, and the card data should be cleared.
357 var testUncombinedNotificationA = {
358 receivedNotification: {
359 notificationId: testNotificationIdA,
360 chromeNotificationId: testCardId,
361 chromeNotificationOptions: testNotificationA,
362 actionUrls: testActionUrlsA,
363 dismissal: testDismissalA,
364 groupName: groupNameA
370 // Setup and expectations.
371 var test = setUpCardManagerTest(this);
372 this.mockApis.expects(once()).
373 chrome_alarms_clear(expectedAlarmId);
374 this.mockApis.expects(once()).
375 instrumented_notifications_clear(testCardId, ANYTHING);
379 cards: [{chromeNotificationId: 'ID1'}, {chromeNotificationId: testCardId}]
382 cards: [{chromeNotificationId: testCardId}, {chromeNotificationId: 'ID2'}]
386 // Call tested method.
387 var dismissalResult = test.cardSet.onDismissal(
390 actionUrls: testActionUrlsA,
392 combinedCard: [testUncombinedNotificationA]
396 // Check the return value.
400 {notificationId: testNotificationIdA, parameters: testDismissalA}
402 notificationData: undefined
404 JSON.stringify(dismissalResult));
406 // Check that groups were cleaned up.
407 var expectedCleanedGroups = {
409 cards: [{chromeNotificationId: 'ID1'}]
412 cards: [{chromeNotificationId: 'ID2'}]
416 assertEquals(JSON.stringify(expectedCleanedGroups), JSON.stringify(groups));
419 TEST_F('GoogleNowCardsUnitTest', 'DismissComplex', function() {
420 // Dismiss a combined card that contains 2 uncombined cards. One of them (B),
421 // is currently shown, and it should be dismissed. The other one (A) has
422 // showTime in future, and it should not be dismissed.
424 var testUncombinedNotificationA = {
425 receivedNotification: {
426 notificationId: testNotificationIdA,
427 chromeNotificationId: testCardId,
428 chromeNotificationOptions: testNotificationA,
429 actionUrls: testActionUrlsA,
430 dismissal: testDismissalA,
431 groupName: groupNameA
437 var testUncombinedNotificationB = {
438 receivedNotification: {
439 notificationId: testNotificationIdB,
440 chromeNotificationId: testCardId,
441 chromeNotificationOptions: testNotificationB,
442 actionUrls: testActionUrlsB,
443 dismissal: testDismissalB,
444 groupName: groupNameB
450 // Setup and expectations.
451 var test = setUpCardManagerTest(this);
452 this.mockApis.expects(once()).
453 chrome_alarms_clear(expectedAlarmId);
454 this.mockApis.expects(once()).
455 instrumented_notifications_clear(testCardId, ANYTHING);
456 this.mockApis.expects(once()).
457 chrome_alarms_create(expectedAlarmId, eqJSON({when: 300010}));
459 // Call tested method.
460 var dismissalResult = test.cardSet.onDismissal(
463 actionUrls: testActionUrlsB,
465 combinedCard: [testUncombinedNotificationA, testUncombinedNotificationB]
469 // Check the return value.
473 {notificationId: testNotificationIdB, parameters: testDismissalB}
477 combinedCard: [testUncombinedNotificationA]
479 JSON.stringify(dismissalResult));
482 TEST_F('GoogleNowCardsUnitTest', 'onAlarmUnrecognized', function() {
483 // Tests onAlarm does nothing on an unrelated alarm.
484 var test = setUpCardManagerTest(this);
486 // Call tested method.
487 test.alarmCallback({name: 'unrelated'});
490 TEST_F('GoogleNowCardsUnitTest', 'onAlarmClear', function() {
491 // Tests onAlarm fired at a moment past all card's events. The card should be
493 var testUncombinedNotification = {
494 receivedNotification: {
495 notificationId: testNotificationIdA,
496 chromeNotificationId: testCardId,
497 chromeNotificationOptions: testNotificationA,
498 actionUrls: testActionUrlsA,
499 dismissal: testDismissalA,
500 groupName: groupNameA
505 // Setup and expectations.
506 var test = setUpCardManagerTest(this);
507 var tasksAddSavedArgs = new SaveMockArguments();
508 this.mockApis.expects(once()).
510 tasksAddSavedArgs.match(eq(UPDATE_CARD_TASK_NAME)),
511 tasksAddSavedArgs.match(ANYTHING)).
512 will(invokeCallback(tasksAddSavedArgs,1));
513 var storageGetSavedArgs = new SaveMockArguments();
514 this.mockApis.expects(once()).
515 instrumented_storage_local_get(
516 storageGetSavedArgs.match(
517 eqJSON(['notificationsData', 'notificationGroups'])),
518 storageGetSavedArgs.match(ANYTHING)).
525 actionUrls: testActionUrlsA,
527 combinedCard: [testUncombinedNotification]
529 'TEST CARD ID 1': {testField: 'TEST VALUE 1'}
531 notificationGroups: {
534 {chromeNotificationId: 'ID1'},
535 {chromeNotificationId: testCardId}
540 {chromeNotificationId: testCardId},
541 {chromeNotificationId: 'ID2'}
544 this.mockApis.expects(once()).
545 instrumented_notifications_clear(testCardId, ANYTHING);
546 this.mockApis.expects(once()).
547 chrome_alarms_clear(expectedAlarmId);
548 this.mockApis.expects(once()).
549 chrome_storage_local_set(eqJSON({
551 'TEST CARD ID 1': {testField: 'TEST VALUE 1'}
553 notificationGroups: {
555 cards: [{chromeNotificationId: 'ID1'}]
558 cards: [{chromeNotificationId: 'ID2'}]
561 // Call tested method.
562 test.alarmCallback({name: expectedAlarmId});
565 TEST_F('GoogleNowCardsUnitTest', 'onAlarmUpdate', function() {
566 // Tests onAlarm fired at a moment when there are future card's events. The
567 // card needs to be updated.
568 var testUncombinedNotification = {
569 receivedNotification: {
570 notificationId: testNotificationIdA,
571 chromeNotificationId: testCardId,
572 chromeNotificationOptions: testNotificationA,
573 actionUrls: testActionUrlsA,
574 dismissal: testDismissalA,
575 groupName: groupNameA
580 // Setup and expectations.
581 var test = setUpCardManagerTest(this);
582 var tasksAddSavedArgs = new SaveMockArguments();
583 this.mockApis.expects(once()).
585 tasksAddSavedArgs.match(eq(UPDATE_CARD_TASK_NAME)),
586 tasksAddSavedArgs.match(ANYTHING)).
587 will(invokeCallback(tasksAddSavedArgs,1));
588 var storageGetSavedArgs = new SaveMockArguments();
589 this.mockApis.expects(once()).
590 instrumented_storage_local_get(
591 storageGetSavedArgs.match(
592 eqJSON(['notificationsData', 'notificationGroups'])),
593 storageGetSavedArgs.match(ANYTHING)).
600 actionUrls: testActionUrlsA,
602 combinedCard: [testUncombinedNotification]
604 this.mockApis.expects(once()).
605 chrome_alarms_clear(expectedAlarmId);
606 var chromeNotificationsUpdateSavedArgs = new SaveMockArguments();
607 this.mockApis.expects(once()).
608 instrumented_notifications_update(
609 chromeNotificationsUpdateSavedArgs.match(eq(testCardId)),
610 chromeNotificationsUpdateSavedArgs.match(eqJSON(testNotificationA)),
611 chromeNotificationsUpdateSavedArgs.match(ANYTHING)).
613 chromeNotificationsUpdateSavedArgs, 2, notificationUpdateSuccessful));
614 this.mockApis.expects(once()).
615 chrome_alarms_create(expectedAlarmId, eqJSON({when: 300001}));
616 this.mockApis.expects(once()).
617 chrome_storage_local_set(eqJSON({
620 actionUrls: testActionUrlsA,
622 combinedCard: [testUncombinedNotification]
625 notificationGroups: {
628 // Call tested method.
629 test.alarmCallback({name: expectedAlarmId});