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,
23 // Test implementation of a function provided by utility.js.
24 function verify(condition, message) {
25 assertTrue(condition, message);
28 var testCardId = 'TEST CARD ID';
29 var testNotificationIdA = 'TEST CARD ID-A';
30 var testNotificationIdB = 'TEST CARD ID-B';
31 var testNotificationA = {
32 testNotificationField: 'TEST NOTIFICATION VALUE A',
35 var testNotificationB = {
36 testNotificationField: 'TEST NOTIFICATION VALUE B',
41 var expectedAlarmId = 'card-TEST CARD ID';
42 var testActionUrlsA = {testField: 'TEST VALUE A'};
43 var testActionUrlsB = {testField: 'TEST VALUE B'};
44 var testDismissalA = {testDismissalField: 'TEST DISMISSAL VALUE A'};
45 var testDismissalB = {testDismissalField: 'TEST DISMISSAL VALUE B'};
46 var LOCATION_CARDS_LINK_THRESHOLD = 10;
47 var localStorage = {locationCardsShown: 0};
48 var notificationUpdateSuccessful = true;
49 var notificationUpdateFailed = false;
51 function setUpCardManagerTest(fixture) {
52 fixture.makeAndRegisterMockApis([
53 'chrome.alarms.clear',
54 'chrome.alarms.create',
55 'instrumented.notifications.clear',
56 'chrome.storage.local.set',
58 'instrumented.alarms.onAlarm.addListener',
59 'instrumented.notifications.create',
60 'instrumented.notifications.update',
61 'instrumented.storage.local.get',
65 chrome.runtime = {}; // No error.
67 var onAlarmSavedArgs = new SaveMockArguments();
68 fixture.mockApis.expects(once()).
69 instrumented_alarms_onAlarm_addListener(
70 onAlarmSavedArgs.match(ANYTHING));
72 var cardSet = buildCardSet();
74 Mock4JS.verifyAllMocks();
76 Date.now = function() { return 300000; };
80 alarmCallback: onAlarmSavedArgs.arguments [0]
86 TEST_F('GoogleNowCardsUnitTest', 'BuildCardManager', function() {
87 // Tests that buildCardSet() call completes with no problems.
88 var test = setUpCardManagerTest(this);
90 assertEquals('object', typeof test.cardSet);
91 assertEquals('function', typeof test.alarmCallback);
94 TEST_F('GoogleNowCardsUnitTest', 'UpdateSimpleHideFuture', function() {
95 // Update a simple card with the hide event in future.
97 var testUncombinedNotification = {
98 receivedNotification: {
99 notificationId: testNotificationIdA,
100 chromeNotificationId: testCardId,
101 chromeNotificationOptions: testNotificationA,
102 actionUrls: testActionUrlsA,
103 dismissal: testDismissalA,
104 groupName: groupNameA
109 // Setup and expectations.
110 var test = setUpCardManagerTest(this);
111 this.mockApis.expects(once()).
112 chrome_alarms_clear(expectedAlarmId);
113 var chromeNotificationsUpdateSavedArgs = new SaveMockArguments();
114 this.mockApis.expects(once()).
115 instrumented_notifications_update(
116 chromeNotificationsUpdateSavedArgs.match(eq(testCardId)),
117 chromeNotificationsUpdateSavedArgs.match(eqJSON(testNotificationA)),
118 chromeNotificationsUpdateSavedArgs.match(ANYTHING)).
120 chromeNotificationsUpdateSavedArgs, 2, notificationUpdateSuccessful));
121 this.mockApis.expects(once()).
122 chrome_alarms_create(expectedAlarmId, eqJSON({when: 300001}));
124 // Call tested method.
125 var notificationData = test.cardSet.update(
126 testCardId, [testUncombinedNotification], {});
128 // Check the return value.
131 actionUrls: testActionUrlsA,
133 combinedCard: [testUncombinedNotification]
135 JSON.stringify(notificationData));
138 TEST_F('GoogleNowCardsUnitTest', 'CreateSimpleHideFuture', function() {
139 // Create a simple card with the hide event in future.
141 var testUncombinedNotification = {
142 receivedNotification: {
143 notificationId: testNotificationIdA,
144 chromeNotificationId: testCardId,
145 chromeNotificationOptions: testNotificationA,
146 actionUrls: testActionUrlsA,
147 dismissal: testDismissalA,
148 groupName: groupNameA
153 // Setup and expectations.
154 var test = setUpCardManagerTest(this);
155 this.makeMockLocalFunctions(['onCardShown']);
157 this.mockApis.expects(once()).
158 chrome_alarms_clear(expectedAlarmId);
160 // notifications.update will return false, which will trigger
161 // notifications.create.
162 var chromeNotificationsUpdateSavedArgs = new SaveMockArguments();
163 this.mockApis.expects(once()).
164 instrumented_notifications_update(
165 chromeNotificationsUpdateSavedArgs.match(eq(testCardId)),
166 chromeNotificationsUpdateSavedArgs.match(eqJSON(testNotificationA)),
167 chromeNotificationsUpdateSavedArgs.match(ANYTHING)).
169 chromeNotificationsUpdateSavedArgs, 2, notificationUpdateFailed));
170 var chromeNotificationsCreateSavedArgs = new SaveMockArguments();
171 this.mockApis.expects(once()).
172 instrumented_notifications_create(
173 chromeNotificationsCreateSavedArgs.match(eq(testCardId)),
174 chromeNotificationsCreateSavedArgs.match(eqJSON(testNotificationA)),
175 chromeNotificationsCreateSavedArgs.match(ANYTHING)).
176 will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId));
178 this.mockLocalFunctions.expects(once()).
179 onCardShown(eqJSON(testUncombinedNotification.receivedNotification));
181 this.mockApis.expects(once()).
182 chrome_alarms_create(expectedAlarmId, eqJSON({when: 300001}));
184 // Call tested method.
185 var notificationData = test.cardSet.update(
187 [testUncombinedNotification],
189 this.mockLocalFunctions.functions().onCardShown);
191 // Check the return value.
194 actionUrls: testActionUrlsA,
196 combinedCard: [testUncombinedNotification]
198 JSON.stringify(notificationData));
201 TEST_F('GoogleNowCardsUnitTest', 'CreateSimpleShowFuture', function() {
202 // Create a simple card with the show event in future. Should create an alarm
203 // to fire at the show time.
205 var testUncombinedNotification = {
206 receivedNotification: {
207 notificationId: testNotificationIdA,
208 chromeNotificationId: testCardId,
209 chromeNotificationOptions: testNotificationA,
210 actionUrls: testActionUrlsA,
211 dismissal: testDismissalA,
212 groupName: groupNameA
218 // Setup and expectations.
219 var test = setUpCardManagerTest(this);
221 this.mockApis.expects(once()).
222 chrome_alarms_clear(expectedAlarmId);
223 this.mockApis.expects(once()).
224 instrumented_notifications_clear(testCardId, ANYTHING);
225 this.mockApis.expects(once()).
226 chrome_alarms_create(expectedAlarmId, eqJSON({when: 300001}));
228 // Call tested method.
229 var notificationData = test.cardSet.update(
230 testCardId, [testUncombinedNotification], {});
232 // Check the return value.
236 combinedCard: [testUncombinedNotification]
238 JSON.stringify(notificationData));
241 TEST_F('GoogleNowCardsUnitTest', 'UpdateSimpleHidePast', function() {
242 // Update a simple card with the hide event in the past (actually, present).
243 // Should clear the notification.
245 var testUncombinedNotification = {
246 receivedNotification: {
247 notificationId: testNotificationIdA,
248 chromeNotificationId: testCardId,
249 chromeNotificationOptions: testNotificationA,
250 actionUrls: testActionUrlsA,
251 dismissal: testDismissalA,
252 groupName: groupNameA
257 // Setup and expectations.
258 var test = setUpCardManagerTest(this);
259 this.mockApis.expects(once()).
260 chrome_alarms_clear(expectedAlarmId);
261 this.mockApis.expects(once()).
262 instrumented_notifications_clear(testCardId, ANYTHING);
266 cards: [{chromeNotificationId: 'ID1'}, {chromeNotificationId: testCardId}]
269 cards: [{chromeNotificationId: testCardId}, {chromeNotificationId: 'ID2'}]
273 // Call tested method.
274 var notificationData = test.cardSet.update(
275 testCardId, [testUncombinedNotification], groups);
277 // Check the return value.
278 assertEquals(undefined, notificationData);
280 // Check that groups were cleaned up.
281 var expectedCleanedGroups = {
283 cards: [{chromeNotificationId: 'ID1'}]
286 cards: [{chromeNotificationId: 'ID2'}]
290 assertEquals(JSON.stringify(expectedCleanedGroups), JSON.stringify(groups));
293 TEST_F('GoogleNowCardsUnitTest', 'UpdateComplex', function() {
294 // Update a combined card that contains 2 uncombined cards. The card from
295 // group B has higher priority and wins.
297 var testUncombinedNotificationA = {
298 receivedNotification: {
299 notificationId: testNotificationIdA,
300 chromeNotificationId: testCardId,
301 chromeNotificationOptions: testNotificationA,
302 actionUrls: testActionUrlsA,
303 dismissal: testDismissalA,
304 groupName: groupNameA
309 var testUncombinedNotificationB = {
310 receivedNotification: {
311 notificationId: testNotificationIdB,
312 chromeNotificationId: testCardId,
313 chromeNotificationOptions: testNotificationB,
314 actionUrls: testActionUrlsB,
315 dismissal: testDismissalB,
316 groupName: groupNameB
321 // Setup and expectations.
322 var test = setUpCardManagerTest(this);
323 this.mockApis.expects(once()).
324 chrome_alarms_clear(expectedAlarmId);
325 var chromeNotificationsUpdateSavedArgs = new SaveMockArguments();
326 this.mockApis.expects(once()).
327 instrumented_notifications_update(
328 chromeNotificationsUpdateSavedArgs.match(eq(testCardId)),
329 chromeNotificationsUpdateSavedArgs.match(eqJSON(testNotificationB)),
330 chromeNotificationsUpdateSavedArgs.match(ANYTHING)).
332 chromeNotificationsUpdateSavedArgs, 2, notificationUpdateSuccessful));
333 this.mockApis.expects(once()).
334 chrome_alarms_create(expectedAlarmId, eqJSON({when: 300001}));
336 // Call tested method.
337 var notificationData = test.cardSet.update(
339 [testUncombinedNotificationA, testUncombinedNotificationB],
342 // Check the return value.
345 actionUrls: testActionUrlsB,
347 combinedCard: [testUncombinedNotificationA, testUncombinedNotificationB]
349 JSON.stringify(notificationData));
352 TEST_F('GoogleNowCardsUnitTest', 'DismissSimple', function() {
353 // Dismiss a combined card that contains 1 uncombined currently visible card.
354 // The notification should be deleted, and the card data should be cleared.
356 var testUncombinedNotificationA = {
357 receivedNotification: {
358 notificationId: testNotificationIdA,
359 chromeNotificationId: testCardId,
360 chromeNotificationOptions: testNotificationA,
361 actionUrls: testActionUrlsA,
362 dismissal: testDismissalA,
363 groupName: groupNameA
369 // Setup and expectations.
370 var test = setUpCardManagerTest(this);
371 this.mockApis.expects(once()).
372 chrome_alarms_clear(expectedAlarmId);
373 this.mockApis.expects(once()).
374 instrumented_notifications_clear(testCardId, ANYTHING);
378 cards: [{chromeNotificationId: 'ID1'}, {chromeNotificationId: testCardId}]
381 cards: [{chromeNotificationId: testCardId}, {chromeNotificationId: 'ID2'}]
385 // Call tested method.
386 var dismissalResult = test.cardSet.onDismissal(
389 actionUrls: testActionUrlsA,
391 combinedCard: [testUncombinedNotificationA]
395 // Check the return value.
399 {notificationId: testNotificationIdA, parameters: testDismissalA}
401 notificationData: undefined
403 JSON.stringify(dismissalResult));
405 // Check that groups were cleaned up.
406 var expectedCleanedGroups = {
408 cards: [{chromeNotificationId: 'ID1'}]
411 cards: [{chromeNotificationId: 'ID2'}]
415 assertEquals(JSON.stringify(expectedCleanedGroups), JSON.stringify(groups));
418 TEST_F('GoogleNowCardsUnitTest', 'DismissComplex', function() {
419 // Dismiss a combined card that contains 2 uncombined cards. One of them (B),
420 // is currently shown, and it should be dismissed. The other one (A) has
421 // showTime in future, and it should not be dismissed.
423 var testUncombinedNotificationA = {
424 receivedNotification: {
425 notificationId: testNotificationIdA,
426 chromeNotificationId: testCardId,
427 chromeNotificationOptions: testNotificationA,
428 actionUrls: testActionUrlsA,
429 dismissal: testDismissalA,
430 groupName: groupNameA
436 var testUncombinedNotificationB = {
437 receivedNotification: {
438 notificationId: testNotificationIdB,
439 chromeNotificationId: testCardId,
440 chromeNotificationOptions: testNotificationB,
441 actionUrls: testActionUrlsB,
442 dismissal: testDismissalB,
443 groupName: groupNameB
449 // Setup and expectations.
450 var test = setUpCardManagerTest(this);
451 this.mockApis.expects(once()).
452 chrome_alarms_clear(expectedAlarmId);
453 this.mockApis.expects(once()).
454 instrumented_notifications_clear(testCardId, ANYTHING);
455 this.mockApis.expects(once()).
456 chrome_alarms_create(expectedAlarmId, eqJSON({when: 300010}));
458 // Call tested method.
459 var dismissalResult = test.cardSet.onDismissal(
462 actionUrls: testActionUrlsB,
464 combinedCard: [testUncombinedNotificationA, testUncombinedNotificationB]
468 // Check the return value.
472 {notificationId: testNotificationIdB, parameters: testDismissalB}
476 combinedCard: [testUncombinedNotificationA]
478 JSON.stringify(dismissalResult));
481 TEST_F('GoogleNowCardsUnitTest', 'onAlarmUnrecognized', function() {
482 // Tests onAlarm does nothing on an unrelated alarm.
483 var test = setUpCardManagerTest(this);
485 // Call tested method.
486 test.alarmCallback({name: 'unrelated'});
489 TEST_F('GoogleNowCardsUnitTest', 'onAlarmClear', function() {
490 // Tests onAlarm fired at a moment past all card's events. The card should be
492 var testUncombinedNotification = {
493 receivedNotification: {
494 notificationId: testNotificationIdA,
495 chromeNotificationId: testCardId,
496 chromeNotificationOptions: testNotificationA,
497 actionUrls: testActionUrlsA,
498 dismissal: testDismissalA,
499 groupName: groupNameA
504 // Setup and expectations.
505 var test = setUpCardManagerTest(this);
506 var tasksAddSavedArgs = new SaveMockArguments();
507 this.mockApis.expects(once()).
509 tasksAddSavedArgs.match(eq(UPDATE_CARD_TASK_NAME)),
510 tasksAddSavedArgs.match(ANYTHING)).
511 will(invokeCallback(tasksAddSavedArgs,1));
512 var storageGetSavedArgs = new SaveMockArguments();
513 this.mockApis.expects(once()).
514 instrumented_storage_local_get(
515 storageGetSavedArgs.match(
516 eqJSON(['notificationsData', 'notificationGroups'])),
517 storageGetSavedArgs.match(ANYTHING)).
524 actionUrls: testActionUrlsA,
526 combinedCard: [testUncombinedNotification]
528 'TEST CARD ID 1': {testField: 'TEST VALUE 1'}
530 notificationGroups: {
533 {chromeNotificationId: 'ID1'},
534 {chromeNotificationId: testCardId}
539 {chromeNotificationId: testCardId},
540 {chromeNotificationId: 'ID2'}
543 this.mockApis.expects(once()).
544 instrumented_notifications_clear(testCardId, ANYTHING);
545 this.mockApis.expects(once()).
546 chrome_alarms_clear(expectedAlarmId);
547 this.mockApis.expects(once()).
548 chrome_storage_local_set(eqJSON({
550 'TEST CARD ID 1': {testField: 'TEST VALUE 1'}
552 notificationGroups: {
554 cards: [{chromeNotificationId: 'ID1'}]
557 cards: [{chromeNotificationId: 'ID2'}]
560 // Call tested method.
561 test.alarmCallback({name: expectedAlarmId});
564 TEST_F('GoogleNowCardsUnitTest', 'onAlarmUpdate', function() {
565 // Tests onAlarm fired at a moment when there are future card's events. The
566 // card needs to be updated.
567 var testUncombinedNotification = {
568 receivedNotification: {
569 notificationId: testNotificationIdA,
570 chromeNotificationId: testCardId,
571 chromeNotificationOptions: testNotificationA,
572 actionUrls: testActionUrlsA,
573 dismissal: testDismissalA,
574 groupName: groupNameA
579 // Setup and expectations.
580 var test = setUpCardManagerTest(this);
581 var tasksAddSavedArgs = new SaveMockArguments();
582 this.mockApis.expects(once()).
584 tasksAddSavedArgs.match(eq(UPDATE_CARD_TASK_NAME)),
585 tasksAddSavedArgs.match(ANYTHING)).
586 will(invokeCallback(tasksAddSavedArgs,1));
587 var storageGetSavedArgs = new SaveMockArguments();
588 this.mockApis.expects(once()).
589 instrumented_storage_local_get(
590 storageGetSavedArgs.match(
591 eqJSON(['notificationsData', 'notificationGroups'])),
592 storageGetSavedArgs.match(ANYTHING)).
599 actionUrls: testActionUrlsA,
601 combinedCard: [testUncombinedNotification]
603 this.mockApis.expects(once()).
604 chrome_alarms_clear(expectedAlarmId);
605 var chromeNotificationsUpdateSavedArgs = new SaveMockArguments();
606 this.mockApis.expects(once()).
607 instrumented_notifications_update(
608 chromeNotificationsUpdateSavedArgs.match(eq(testCardId)),
609 chromeNotificationsUpdateSavedArgs.match(eqJSON(testNotificationA)),
610 chromeNotificationsUpdateSavedArgs.match(ANYTHING)).
612 chromeNotificationsUpdateSavedArgs, 2, notificationUpdateSuccessful));
613 this.mockApis.expects(once()).
614 chrome_alarms_create(expectedAlarmId, eqJSON({when: 300001}));
615 this.mockApis.expects(once()).
616 chrome_storage_local_set(eqJSON({
619 actionUrls: testActionUrlsA,
621 combinedCard: [testUncombinedNotification]
624 notificationGroups: {
627 // Call tested method.
628 test.alarmCallback({name: expectedAlarmId});