Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / resources / google_now / cards_unittest.gtestjs
blob032b72dc668f438d043b6af9bc2bc7b0ab23fcfa
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.
5 /**
6  * Test fixture for cards.js.
7  * @constructor
8  * @extends {testing.Test}
9  */
10 function GoogleNowCardsUnitTest () {
11   testing.Test.call(this);
14 GoogleNowCardsUnitTest.prototype = {
15   __proto__: testing.Test.prototype,
17   /** @override */
18   extraLibraries: [
19     'cards.js'
20   ]
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',
33   priority: 1
35 var testNotificationB = {
36   testNotificationField: 'TEST NOTIFICATION VALUE B',
37   priority: 2
39 var groupNameA = 'A';
40 var groupNameB = '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',
57       'countLocationCard',
58       'instrumented.alarms.onAlarm.addListener',
59       'instrumented.notifications.create',
60       'instrumented.notifications.update',
61       'instrumented.storage.local.get',
62       'tasks.add'
63   ]);
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; };
78   var test = {
79     cardSet: cardSet,
80     alarmCallback: onAlarmSavedArgs.arguments [0]
81   };
83   return test;
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);
92 });
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
105     },
106     hideTime: 300001
107   };
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)).
119       will(invokeCallback(
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.
129   assertEquals(
130       JSON.stringify({
131         actionUrls: testActionUrlsA,
132         timestamp: 300000,
133         combinedCard: [testUncombinedNotification]
134       }),
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
149     },
150     hideTime: 300001
151   };
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)).
168       will(invokeCallback(
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(
186       testCardId,
187       [testUncombinedNotification],
188       {},
189       this.mockLocalFunctions.functions().onCardShown);
191   // Check the return value.
192   assertEquals(
193       JSON.stringify({
194         actionUrls: testActionUrlsA,
195         timestamp: 300000,
196         combinedCard: [testUncombinedNotification]
197       }),
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
213     },
214     showTime: 300001,
215     hideTime: 300002
216   };
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.
233   assertEquals(
234       JSON.stringify({
235         timestamp: 300000,
236         combinedCard: [testUncombinedNotification]
237       }),
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
253     },
254     hideTime: 300000
255   };
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);
264   var groups = {
265     A: {
266       cards: [{chromeNotificationId: 'ID1'}, {chromeNotificationId: testCardId}]
267     },
268     B: {
269       cards: [{chromeNotificationId: testCardId}, {chromeNotificationId: 'ID2'}]
270     }
271   };
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 = {
282     A: {
283       cards: [{chromeNotificationId: 'ID1'}]
284     },
285     B: {
286       cards: [{chromeNotificationId: 'ID2'}]
287     }
288   };
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
305     },
306     hideTime: 300001
307   };
309   var testUncombinedNotificationB = {
310     receivedNotification: {
311       notificationId: testNotificationIdB,
312       chromeNotificationId: testCardId,
313       chromeNotificationOptions: testNotificationB,
314       actionUrls: testActionUrlsB,
315       dismissal: testDismissalB,
316       groupName: groupNameB
317     },
318     hideTime: 300002
319   };
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)).
331       will(invokeCallback(
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(
338       testCardId,
339       [testUncombinedNotificationA, testUncombinedNotificationB],
340       {});
342   // Check the return value.
343   assertEquals(
344       JSON.stringify({
345         actionUrls: testActionUrlsB,
346         timestamp: 300000,
347         combinedCard: [testUncombinedNotificationA, testUncombinedNotificationB]
348       }),
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
364     },
365     showTime: 299995,
366     hideTime: 300005
367   };
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);
376   var groups = {
377     A: {
378       cards: [{chromeNotificationId: 'ID1'}, {chromeNotificationId: testCardId}]
379     },
380     B: {
381       cards: [{chromeNotificationId: testCardId}, {chromeNotificationId: 'ID2'}]
382     }
383   };
385   // Call tested method.
386   var dismissalResult = test.cardSet.onDismissal(
387       testCardId,
388       {
389         actionUrls: testActionUrlsA,
390         timestamp: 299999,
391         combinedCard: [testUncombinedNotificationA]
392       },
393       groups);
395   // Check the return value.
396   assertEquals(
397       JSON.stringify({
398         dismissals: [
399           {notificationId: testNotificationIdA, parameters: testDismissalA}
400         ],
401         notificationData: undefined
402       }),
403       JSON.stringify(dismissalResult));
405   // Check that groups were cleaned up.
406   var expectedCleanedGroups = {
407     A: {
408       cards: [{chromeNotificationId: 'ID1'}]
409     },
410     B: {
411       cards: [{chromeNotificationId: 'ID2'}]
412     }
413   };
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
431     },
432     showTime: 300010,
433     hideTime: 300011
434   };
436   var testUncombinedNotificationB = {
437     receivedNotification: {
438       notificationId: testNotificationIdB,
439       chromeNotificationId: testCardId,
440       chromeNotificationOptions: testNotificationB,
441       actionUrls: testActionUrlsB,
442       dismissal: testDismissalB,
443       groupName: groupNameB
444     },
445     showTime: 299995,
446     hideTime: 300005
447   };
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(
460       testCardId,
461       {
462         actionUrls: testActionUrlsB,
463         timestamp: 299999,
464         combinedCard: [testUncombinedNotificationA, testUncombinedNotificationB]
465       },
466       {});
468   // Check the return value.
469   assertEquals(
470       JSON.stringify({
471         dismissals: [
472           {notificationId: testNotificationIdB, parameters: testDismissalB}
473         ],
474         notificationData: {
475           timestamp: 300000,
476           combinedCard: [testUncombinedNotificationA]
477         }}),
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
491   // cleaned.
492   var testUncombinedNotification = {
493     receivedNotification: {
494       notificationId: testNotificationIdA,
495       chromeNotificationId: testCardId,
496       chromeNotificationOptions: testNotificationA,
497       actionUrls: testActionUrlsA,
498       dismissal: testDismissalA,
499       groupName: groupNameA
500     },
501     hideTime: 299999
502   };
504   // Setup and expectations.
505   var test = setUpCardManagerTest(this);
506   var tasksAddSavedArgs = new SaveMockArguments();
507   this.mockApis.expects(once()).
508       tasks_add(
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)).
518       will(invokeCallback(
519           storageGetSavedArgs,
520           1,
521           {
522             notificationsData: {
523               'TEST CARD ID': {
524                 actionUrls: testActionUrlsA,
525                 timestamp: 299998,
526                 combinedCard: [testUncombinedNotification]
527               },
528               'TEST CARD ID 1': {testField: 'TEST VALUE 1'}
529             },
530             notificationGroups: {
531               A: {
532                 cards: [
533                   {chromeNotificationId: 'ID1'},
534                   {chromeNotificationId: testCardId}
535                 ]
536               },
537               B: {
538                 cards: [
539                   {chromeNotificationId: testCardId},
540                   {chromeNotificationId: 'ID2'}
541                 ]
542             }}}));
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({
549         notificationsData: {
550           'TEST CARD ID 1': {testField: 'TEST VALUE 1'}
551         },
552         notificationGroups: {
553           A: {
554             cards: [{chromeNotificationId: 'ID1'}]
555           },
556           B: {
557             cards: [{chromeNotificationId: 'ID2'}]
558         }}}));
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
575     },
576     hideTime: 300001
577   };
579   // Setup and expectations.
580   var test = setUpCardManagerTest(this);
581   var tasksAddSavedArgs = new SaveMockArguments();
582   this.mockApis.expects(once()).
583       tasks_add(
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)).
593       will(invokeCallback(
594           storageGetSavedArgs,
595           1,
596           {
597             notificationsData: {
598               'TEST CARD ID': {
599                 actionUrls: testActionUrlsA,
600                 timestamp: 299998,
601                 combinedCard: [testUncombinedNotification]
602               }}}));
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)).
611       will(invokeCallback(
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({
617             notificationsData: {
618               'TEST CARD ID': {
619                 actionUrls: testActionUrlsA,
620                 timestamp: 300000,
621                 combinedCard: [testUncombinedNotification]
622               }
623             },
624             notificationGroups: {
625             }}));
627   // Call tested method.
628   test.alarmCallback({name: expectedAlarmId});