Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / google_now / cards_unittest.gtestjs
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     'common_test_util.js',
20     'cards.js'
21   ]
22 };
23
24 // Test implementation of a function provided by utility.js.
25 function verify(condition, message) {
26   assertTrue(condition, message);
27 }
28
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',
34   priority: 1
35 };
36 var testNotificationB = {
37   testNotificationField: 'TEST NOTIFICATION VALUE B',
38   priority: 2
39 };
40 var groupNameA = 'A';
41 var groupNameB = '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;
51
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',
63       'tasks.add'
64   ]);
65
66   chrome.runtime = {}; // No error.
67
68   var onAlarmSavedArgs = new SaveMockArguments();
69   fixture.mockApis.expects(once()).
70       instrumented_alarms_onAlarm_addListener(
71           onAlarmSavedArgs.match(ANYTHING));
72
73   var cardSet = buildCardSet();
74
75   Mock4JS.verifyAllMocks();
76
77   Date.now = function() { return 300000; };
78
79   var test = {
80     cardSet: cardSet,
81     alarmCallback: onAlarmSavedArgs.arguments [0]
82   };
83
84   return test;
85 }
86
87 TEST_F('GoogleNowCardsUnitTest', 'BuildCardManager', function() {
88   // Tests that buildCardSet() call completes with no problems.
89   var test = setUpCardManagerTest(this);
90
91   assertEquals('object', typeof test.cardSet);
92   assertEquals('function', typeof test.alarmCallback);
93 });
94
95 TEST_F('GoogleNowCardsUnitTest', 'UpdateSimpleHideFuture', function() {
96   // Update a simple card with the hide event in future.
97
98   var testUncombinedNotification = {
99     receivedNotification: {
100       notificationId: testNotificationIdA,
101       chromeNotificationId: testCardId,
102       chromeNotificationOptions: testNotificationA,
103       actionUrls: testActionUrlsA,
104       dismissal: testDismissalA,
105       groupName: groupNameA
106     },
107     hideTime: 300001
108   };
109
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)).
120       will(invokeCallback(
121           chromeNotificationsUpdateSavedArgs, 2, notificationUpdateSuccessful));
122   this.mockApis.expects(once()).
123       chrome_alarms_create(expectedAlarmId, eqJSON({when: 300001}));
124
125   // Call tested method.
126   var notificationData = test.cardSet.update(
127       testCardId, [testUncombinedNotification], {});
128
129   // Check the return value.
130   assertEquals(
131       JSON.stringify({
132         actionUrls: testActionUrlsA,
133         timestamp: 300000,
134         combinedCard: [testUncombinedNotification]
135       }),
136       JSON.stringify(notificationData));
137 });
138
139 TEST_F('GoogleNowCardsUnitTest', 'CreateSimpleHideFuture', function() {
140   // Create a simple card with the hide event in future.
141
142   var testUncombinedNotification = {
143     receivedNotification: {
144       notificationId: testNotificationIdA,
145       chromeNotificationId: testCardId,
146       chromeNotificationOptions: testNotificationA,
147       actionUrls: testActionUrlsA,
148       dismissal: testDismissalA,
149       groupName: groupNameA
150     },
151     hideTime: 300001
152   };
153
154   // Setup and expectations.
155   var test = setUpCardManagerTest(this);
156   this.makeMockLocalFunctions(['onCardShown']);
157
158   this.mockApis.expects(once()).
159       chrome_alarms_clear(expectedAlarmId);
160
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)).
169       will(invokeCallback(
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));
178
179   this.mockLocalFunctions.expects(once()).
180       onCardShown(eqJSON(testUncombinedNotification.receivedNotification));
181
182   this.mockApis.expects(once()).
183       chrome_alarms_create(expectedAlarmId, eqJSON({when: 300001}));
184
185   // Call tested method.
186   var notificationData = test.cardSet.update(
187       testCardId,
188       [testUncombinedNotification],
189       {},
190       this.mockLocalFunctions.functions().onCardShown);
191
192   // Check the return value.
193   assertEquals(
194       JSON.stringify({
195         actionUrls: testActionUrlsA,
196         timestamp: 300000,
197         combinedCard: [testUncombinedNotification]
198       }),
199       JSON.stringify(notificationData));
200 });
201
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.
205
206   var testUncombinedNotification = {
207     receivedNotification: {
208       notificationId: testNotificationIdA,
209       chromeNotificationId: testCardId,
210       chromeNotificationOptions: testNotificationA,
211       actionUrls: testActionUrlsA,
212       dismissal: testDismissalA,
213       groupName: groupNameA
214     },
215     showTime: 300001,
216     hideTime: 300002
217   };
218
219   // Setup and expectations.
220   var test = setUpCardManagerTest(this);
221
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}));
228
229   // Call tested method.
230   var notificationData = test.cardSet.update(
231       testCardId, [testUncombinedNotification], {});
232
233   // Check the return value.
234   assertEquals(
235       JSON.stringify({
236         timestamp: 300000,
237         combinedCard: [testUncombinedNotification]
238       }),
239       JSON.stringify(notificationData));
240 });
241
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.
245
246   var testUncombinedNotification = {
247     receivedNotification: {
248       notificationId: testNotificationIdA,
249       chromeNotificationId: testCardId,
250       chromeNotificationOptions: testNotificationA,
251       actionUrls: testActionUrlsA,
252       dismissal: testDismissalA,
253       groupName: groupNameA
254     },
255     hideTime: 300000
256   };
257
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);
264
265   var groups = {
266     A: {
267       cards: [{chromeNotificationId: 'ID1'}, {chromeNotificationId: testCardId}]
268     },
269     B: {
270       cards: [{chromeNotificationId: testCardId}, {chromeNotificationId: 'ID2'}]
271     }
272   };
273
274   // Call tested method.
275   var notificationData = test.cardSet.update(
276       testCardId, [testUncombinedNotification], groups);
277
278   // Check the return value.
279   assertEquals(undefined, notificationData);
280
281   // Check that groups were cleaned up.
282   var expectedCleanedGroups = {
283     A: {
284       cards: [{chromeNotificationId: 'ID1'}]
285     },
286     B: {
287       cards: [{chromeNotificationId: 'ID2'}]
288     }
289   };
290
291   assertEquals(JSON.stringify(expectedCleanedGroups), JSON.stringify(groups));
292 });
293
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.
297
298   var testUncombinedNotificationA = {
299     receivedNotification: {
300       notificationId: testNotificationIdA,
301       chromeNotificationId: testCardId,
302       chromeNotificationOptions: testNotificationA,
303       actionUrls: testActionUrlsA,
304       dismissal: testDismissalA,
305       groupName: groupNameA
306     },
307     hideTime: 300001
308   };
309
310   var testUncombinedNotificationB = {
311     receivedNotification: {
312       notificationId: testNotificationIdB,
313       chromeNotificationId: testCardId,
314       chromeNotificationOptions: testNotificationB,
315       actionUrls: testActionUrlsB,
316       dismissal: testDismissalB,
317       groupName: groupNameB
318     },
319     hideTime: 300002
320   };
321
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)).
332       will(invokeCallback(
333           chromeNotificationsUpdateSavedArgs, 2, notificationUpdateSuccessful));
334   this.mockApis.expects(once()).
335       chrome_alarms_create(expectedAlarmId, eqJSON({when: 300001}));
336
337   // Call tested method.
338   var notificationData = test.cardSet.update(
339       testCardId,
340       [testUncombinedNotificationA, testUncombinedNotificationB],
341       {});
342
343   // Check the return value.
344   assertEquals(
345       JSON.stringify({
346         actionUrls: testActionUrlsB,
347         timestamp: 300000,
348         combinedCard: [testUncombinedNotificationA, testUncombinedNotificationB]
349       }),
350       JSON.stringify(notificationData));
351 });
352
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.
356
357   var testUncombinedNotificationA = {
358     receivedNotification: {
359       notificationId: testNotificationIdA,
360       chromeNotificationId: testCardId,
361       chromeNotificationOptions: testNotificationA,
362       actionUrls: testActionUrlsA,
363       dismissal: testDismissalA,
364       groupName: groupNameA
365     },
366     showTime: 299995,
367     hideTime: 300005
368   };
369
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);
376
377   var groups = {
378     A: {
379       cards: [{chromeNotificationId: 'ID1'}, {chromeNotificationId: testCardId}]
380     },
381     B: {
382       cards: [{chromeNotificationId: testCardId}, {chromeNotificationId: 'ID2'}]
383     }
384   };
385
386   // Call tested method.
387   var dismissalResult = test.cardSet.onDismissal(
388       testCardId,
389       {
390         actionUrls: testActionUrlsA,
391         timestamp: 299999,
392         combinedCard: [testUncombinedNotificationA]
393       },
394       groups);
395
396   // Check the return value.
397   assertEquals(
398       JSON.stringify({
399         dismissals: [
400           {notificationId: testNotificationIdA, parameters: testDismissalA}
401         ],
402         notificationData: undefined
403       }),
404       JSON.stringify(dismissalResult));
405
406   // Check that groups were cleaned up.
407   var expectedCleanedGroups = {
408     A: {
409       cards: [{chromeNotificationId: 'ID1'}]
410     },
411     B: {
412       cards: [{chromeNotificationId: 'ID2'}]
413     }
414   };
415
416   assertEquals(JSON.stringify(expectedCleanedGroups), JSON.stringify(groups));
417 });
418
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.
423
424   var testUncombinedNotificationA = {
425     receivedNotification: {
426       notificationId: testNotificationIdA,
427       chromeNotificationId: testCardId,
428       chromeNotificationOptions: testNotificationA,
429       actionUrls: testActionUrlsA,
430       dismissal: testDismissalA,
431       groupName: groupNameA
432     },
433     showTime: 300010,
434     hideTime: 300011
435   };
436
437   var testUncombinedNotificationB = {
438     receivedNotification: {
439       notificationId: testNotificationIdB,
440       chromeNotificationId: testCardId,
441       chromeNotificationOptions: testNotificationB,
442       actionUrls: testActionUrlsB,
443       dismissal: testDismissalB,
444       groupName: groupNameB
445     },
446     showTime: 299995,
447     hideTime: 300005
448   };
449
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}));
458
459   // Call tested method.
460   var dismissalResult = test.cardSet.onDismissal(
461       testCardId,
462       {
463         actionUrls: testActionUrlsB,
464         timestamp: 299999,
465         combinedCard: [testUncombinedNotificationA, testUncombinedNotificationB]
466       },
467       {});
468
469   // Check the return value.
470   assertEquals(
471       JSON.stringify({
472         dismissals: [
473           {notificationId: testNotificationIdB, parameters: testDismissalB}
474         ],
475         notificationData: {
476           timestamp: 300000,
477           combinedCard: [testUncombinedNotificationA]
478         }}),
479       JSON.stringify(dismissalResult));
480 });
481
482 TEST_F('GoogleNowCardsUnitTest', 'onAlarmUnrecognized', function() {
483   // Tests onAlarm does nothing on an unrelated alarm.
484   var test = setUpCardManagerTest(this);
485
486   // Call tested method.
487   test.alarmCallback({name: 'unrelated'});
488 });
489
490 TEST_F('GoogleNowCardsUnitTest', 'onAlarmClear', function() {
491   // Tests onAlarm fired at a moment past all card's events. The card should be
492   // cleaned.
493   var testUncombinedNotification = {
494     receivedNotification: {
495       notificationId: testNotificationIdA,
496       chromeNotificationId: testCardId,
497       chromeNotificationOptions: testNotificationA,
498       actionUrls: testActionUrlsA,
499       dismissal: testDismissalA,
500       groupName: groupNameA
501     },
502     hideTime: 299999
503   };
504
505   // Setup and expectations.
506   var test = setUpCardManagerTest(this);
507   var tasksAddSavedArgs = new SaveMockArguments();
508   this.mockApis.expects(once()).
509       tasks_add(
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)).
519       will(invokeCallback(
520           storageGetSavedArgs,
521           1,
522           {
523             notificationsData: {
524               'TEST CARD ID': {
525                 actionUrls: testActionUrlsA,
526                 timestamp: 299998,
527                 combinedCard: [testUncombinedNotification]
528               },
529               'TEST CARD ID 1': {testField: 'TEST VALUE 1'}
530             },
531             notificationGroups: {
532               A: {
533                 cards: [
534                   {chromeNotificationId: 'ID1'},
535                   {chromeNotificationId: testCardId}
536                 ]
537               },
538               B: {
539                 cards: [
540                   {chromeNotificationId: testCardId},
541                   {chromeNotificationId: 'ID2'}
542                 ]
543             }}}));
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({
550         notificationsData: {
551           'TEST CARD ID 1': {testField: 'TEST VALUE 1'}
552         },
553         notificationGroups: {
554           A: {
555             cards: [{chromeNotificationId: 'ID1'}]
556           },
557           B: {
558             cards: [{chromeNotificationId: 'ID2'}]
559         }}}));
560
561   // Call tested method.
562   test.alarmCallback({name: expectedAlarmId});
563 });
564
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
576     },
577     hideTime: 300001
578   };
579
580   // Setup and expectations.
581   var test = setUpCardManagerTest(this);
582   var tasksAddSavedArgs = new SaveMockArguments();
583   this.mockApis.expects(once()).
584       tasks_add(
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)).
594       will(invokeCallback(
595           storageGetSavedArgs,
596           1,
597           {
598             notificationsData: {
599               'TEST CARD ID': {
600                 actionUrls: testActionUrlsA,
601                 timestamp: 299998,
602                 combinedCard: [testUncombinedNotification]
603               }}}));
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)).
612       will(invokeCallback(
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({
618             notificationsData: {
619               'TEST CARD ID': {
620                 actionUrls: testActionUrlsA,
621                 timestamp: 300000,
622                 combinedCard: [testUncombinedNotification]
623               }
624             },
625             notificationGroups: {
626             }}));
627
628   // Call tested method.
629   test.alarmCallback({name: expectedAlarmId});
630 });