Finishing API error processing, and cleanup.
[chromium-blink-merge.git] / chrome / browser / resources / google_now / background_unittest.gtestjs
blob8e330dfc68ad6e97d4177d59108f5d1396e99e34
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 background.js.
7  * @constructor
8  * @extends {testing.Test}
9  */
10 function GoogleNowBackgroundUnitTest () {
11   testing.Test.call(this);
14 GoogleNowBackgroundUnitTest.prototype = {
15   __proto__: testing.Test.prototype,
17   /** @override */
18   extraLibraries: [
19     'common_test_util.js',
20     'background_test_util.js',
21     'background.js'
22   ]
25 TEST_F('GoogleNowBackgroundUnitTest', 'AreTasksConflicting', function() {
26   function testTaskPair(newTaskName, scheduledTaskName, expected) {
27     assertTrue(areTasksConflicting(newTaskName, scheduledTaskName) == expected,
28                '(' + newTaskName + ', ' + scheduledTaskName + ')');
29   }
31   testTaskPair(UPDATE_CARDS_TASK_NAME, UPDATE_CARDS_TASK_NAME, true);
32   testTaskPair(UPDATE_CARDS_TASK_NAME, DISMISS_CARD_TASK_NAME, false);
33   testTaskPair(UPDATE_CARDS_TASK_NAME, RETRY_DISMISS_TASK_NAME, false);
34   testTaskPair(UPDATE_CARDS_TASK_NAME, STATE_CHANGED_TASK_NAME, false);
36   testTaskPair(DISMISS_CARD_TASK_NAME, UPDATE_CARDS_TASK_NAME, false);
37   testTaskPair(DISMISS_CARD_TASK_NAME, DISMISS_CARD_TASK_NAME, false);
38   testTaskPair(DISMISS_CARD_TASK_NAME, RETRY_DISMISS_TASK_NAME, false);
39   testTaskPair(DISMISS_CARD_TASK_NAME, STATE_CHANGED_TASK_NAME, false);
41   testTaskPair(RETRY_DISMISS_TASK_NAME, UPDATE_CARDS_TASK_NAME, true);
42   testTaskPair(RETRY_DISMISS_TASK_NAME, DISMISS_CARD_TASK_NAME, true);
43   testTaskPair(RETRY_DISMISS_TASK_NAME, RETRY_DISMISS_TASK_NAME, true);
44   testTaskPair(RETRY_DISMISS_TASK_NAME, STATE_CHANGED_TASK_NAME, false);
46   testTaskPair(STATE_CHANGED_TASK_NAME, UPDATE_CARDS_TASK_NAME, false);
47   testTaskPair(STATE_CHANGED_TASK_NAME, DISMISS_CARD_TASK_NAME, false);
48   testTaskPair(STATE_CHANGED_TASK_NAME, RETRY_DISMISS_TASK_NAME, false);
49   testTaskPair(STATE_CHANGED_TASK_NAME, STATE_CHANGED_TASK_NAME, false);
50 });
52 var testNotificationId = 'TEST GROUP-SOME TEST ID';
53 var testChromeNotificationId = 'TEST CARD ID';
54 var testNotification1 =
55     {testNotificationField: 'TEST NOTIFICATION VALUE1', priority: -1};
56 var testNotification2 =
57     {testNotificationField: 'TEST NOTIFICATION VALUE2', priority: 0};
58 var testActionUrls1 = {testField: 'TEST VALUE1'};
59 var testActionUrls2 = {testField: 'TEST VALUE2'};
60 var testDismissal1 = {testDismissalField: 'TEST DISMISSAL VALUE 1'};
61 var testDismissal2 = {testDismissalField: 'TEST DISMISSAL VALUE 2'};
62 var testVersion = 7;
63 var testTimestamp1 = 300000;
64 var testTimestamp2 = 600000;
65 var testGroupRank1 = 19;
66 var testGroupRank2 = 23;
67 var testTriggerUnmerged = {showTimeSec: 100, hideTimeSec: 200};
68 var testTriggerMerged = {showTime: 300007, hideTime: 300011};
69 var testVersion1 = 29;
70 var testVersion2 = 31;
72 TEST_F(
73     'GoogleNowBackgroundUnitTest',
74     'MergeCardsEmptyNoTrigger',
75     function() {
76       // Tests merging a card with an empty trigger into an 'undefined' merged
77       // card. This should start a new card.
79       // Invoking the tested function.
80       var resultingMergedCard = mergeCards(undefined, {
81         notificationId: testNotificationId,
82         chromeNotificationId: testChromeNotificationId,
83         version: testVersion1,
84         chromeNotificationOptions: testNotification1,
85         actionUrls: testActionUrls1,
86         dismissal: testDismissal1
87       }, testTimestamp1, testGroupRank1);
89       // Check the return value.
90       assertEquals(
91           JSON.stringify({
92             dismissals: [
93               {
94                 notificationId: testNotificationId,
95                 parameters: testDismissal1
96               }
97             ],
98             groupRank: testGroupRank1,
99             trigger: {},
100             timestamp: testTimestamp1,
101             notification: testNotification1,
102             actionUrls: testActionUrls1,
103             version: testVersion1
104           }),
105         JSON.stringify(resultingMergedCard));
106     });
108 TEST_F(
109     'GoogleNowBackgroundUnitTest',
110     'MergeCards1',
111     function() {
112       // Tests merging a unmerged card into a merged card.
113       // Unmerged card priority is greater than merged card one. Unmerged card
114       // rank is less than merged card one, and it's older.
116       // Setup.
117       var mergedCard = {
118         trigger: testTriggerMerged,
119         version: testVersion1,
120         timestamp: testTimestamp2,
121         notification: testNotification1,
122         actionUrls: testActionUrls1,
123         groupRank: testGroupRank2,
124         dismissals:
125             [{notificationId: testNotificationId, parameters: testDismissal1}]
126       };
128       var unmergedNotification = {
129         notificationId: testNotificationId,
130         chromeNotificationId: testChromeNotificationId,
131         version: testVersion2,
132         chromeNotificationOptions: testNotification2,
133         actionUrls: testActionUrls2,
134         dismissal: testDismissal2,
135         trigger: testTriggerUnmerged
136       };
138       // Invoking the tested function.
139       var resultingMergedCard = mergeCards(
140           mergedCard, unmergedNotification, testTimestamp1, testGroupRank1);
142       // Check the return value.
143       assertEquals(
144           JSON.stringify({
145             trigger: testTriggerMerged,
146             version: testVersion1,
147             timestamp: testTimestamp2,
148             notification: {
149                 testNotificationField: 'TEST NOTIFICATION VALUE1',
150                 priority: 0
151              },
152             actionUrls: testActionUrls1,
153             groupRank: testGroupRank2,
154             dismissals: [
155               {notificationId: testNotificationId, parameters: testDismissal1},
156               {notificationId: testNotificationId, parameters: testDismissal2}
157             ]
158           }),
159         JSON.stringify(resultingMergedCard));
160     });
162 TEST_F(
163     'GoogleNowBackgroundUnitTest',
164     'MergeCards2',
165     function() {
166       // Tests merging a unmerged card into a merged card.
167       // Unmerged card priority is less than merged card one. Unmerged card rank
168       // is greater than merged card one, and it's older.
170       // Setup.
171       var mergedCard = {
172         trigger: testTriggerMerged,
173         version: testVersion1,
174         timestamp: testTimestamp2,
175         notification: testNotification2,
176         actionUrls: testActionUrls1,
177         groupRank: testGroupRank1,
178         dismissals:
179             [{notificationId: testNotificationId, parameters: testDismissal1}]
180       };
182       var unmergedNotification = {
183         notificationId: testNotificationId,
184         chromeNotificationId: testChromeNotificationId,
185         version: testVersion2,
186         chromeNotificationOptions: testNotification1,
187         actionUrls: testActionUrls2,
188         dismissal: testDismissal2,
189         trigger: testTriggerUnmerged
190       };
192       // Invoking the tested function.
193       var resultingMergedCard = mergeCards(
194           mergedCard, unmergedNotification, testTimestamp1, testGroupRank2);
196       // Check the return value.
197       assertEquals(
198           JSON.stringify({
199             trigger: {showTime: 400000, hideTime: 500000},
200             version: testVersion1,
201             timestamp: testTimestamp2,
202             notification: {
203                 testNotificationField: 'TEST NOTIFICATION VALUE2',
204                 priority: 0
205              },
206             actionUrls: testActionUrls1,
207             groupRank: testGroupRank2,
208             dismissals: [
209               {notificationId: testNotificationId, parameters: testDismissal1},
210               {notificationId: testNotificationId, parameters: testDismissal2}
211             ]
212           }),
213         JSON.stringify(resultingMergedCard));
214     });
216 TEST_F(
217     'GoogleNowBackgroundUnitTest',
218     'MergeCards3',
219     function() {
220       // Tests merging a unmerged card into a merged card.
221       // Unmerged card priority is less than merged card one. Unmerged card rank
222       // is less than merged card one, and it's newer.
224       // Setup.
225       var mergedCard = {
226         trigger: testTriggerMerged,
227         version: testVersion1,
228         timestamp: testTimestamp1,
229         notification: testNotification2,
230         actionUrls: testActionUrls1,
231         groupRank: testGroupRank2,
232         dismissals:
233             [{notificationId: testNotificationId, parameters: testDismissal1}]
234       };
236       var unmergedNotification = {
237         notificationId: testNotificationId,
238         chromeNotificationId: testChromeNotificationId,
239         version: testVersion2,
240         chromeNotificationOptions: testNotification1,
241         actionUrls: testActionUrls2,
242         dismissal: testDismissal2,
243         trigger: testTriggerUnmerged
244       };
246       // Invoking the tested function.
247       var resultingMergedCard = mergeCards(
248           mergedCard, unmergedNotification, testTimestamp2, testGroupRank1);
250       // Check the return value.
251       assertEquals(
252           JSON.stringify({
253             trigger: testTriggerMerged,
254             version: testVersion2,
255             timestamp: testTimestamp2,
256             notification: {
257                 testNotificationField: 'TEST NOTIFICATION VALUE1',
258                 priority: 0
259              },
260             actionUrls: testActionUrls2,
261             groupRank: testGroupRank2,
262             dismissals: [
263               {notificationId: testNotificationId, parameters: testDismissal1},
264               {notificationId: testNotificationId, parameters: testDismissal2}
265             ]
266           }),
267         JSON.stringify(resultingMergedCard));
268     });
270 TEST_F(
271     'GoogleNowBackgroundUnitTest',
272     'MergeGroup',
273     function() {
274       // Tests mergeGroup method.
276       // Setup.
277       this.makeAndRegisterMockGlobals(['mergeCards']);
279       this.mockGlobals.expects(once()).
280           mergeCards(
281               undefined,
282               eqJSON({chromeNotificationId: 'ID 1', testField: 'TEST_FIELD1'}),
283               300000,
284               17).
285           will(returnValue({testField: 'RESULT 1'}));
286       this.mockGlobals.expects(once()).
287           mergeCards(
288               eqJSON({testField: 'TEST_FIELD22'}),
289               eqJSON({chromeNotificationId: 'ID 2', testField: 'TEST_FIELD2'}),
290               300000,
291               17).
292           will(returnValue({testField: 'RESULT 2'}));
294       var group = {
295         cards: [
296           {chromeNotificationId: 'ID 1', testField: 'TEST_FIELD1'},
297           {chromeNotificationId: 'ID 2', testField: 'TEST_FIELD2'}
298         ],
299         cardsTimestamp: 300000,
300         nextPollTime: 600000,
301         rank: 17
302       };
304       var mergedCards = {
305         'ID 2': { testField: 'TEST_FIELD22' },
306         'ID 3': { testField: 'TEST_FIELD3' }
307       };
309       // Invoking the tested function.
310       mergeGroup(mergedCards, group);
312       // Check the output parameter.
313       assertEquals(
314           JSON.stringify({
315             'ID 2': { testField: 'RESULT 2' },
316             'ID 3': { testField: 'TEST_FIELD3'},
317             'ID 1': { testField: 'RESULT 1' }}),
318           JSON.stringify(mergedCards));
319     });
322  * Mocks global functions and APIs that initialize() depends upon.
323  * @param {Test} fixture Test fixture.
324  */
325 function mockInitializeDependencies(fixture) {
326   fixture.makeAndRegisterMockGlobals([
327     'recordEvent',
328     'setBackgroundEnable',
329     'showWelcomeToast',
330     'startPollingCards'
331   ]);
332   fixture.makeAndRegisterMockApis([
333     'authenticationManager.isSignedIn',
334     'chrome.location.clearWatch',
335     'chrome.storage.local.remove',
336     'instrumented.metricsPrivate.getVariationParams',
337     'instrumented.notifications.getAll',
338     'instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get',
339     'instrumented.storage.local.get',
340     'tasks.add',
341     'updateCardsAttempts.isRunning',
342     'updateCardsAttempts.stop'
343   ]);
347  * Sets up the test to expect the state machine calls and send
348  * the specified state machine state. Currently used to test initialize().
349  * Note that this CAN NOT be used if any of the methods below are called
350  * outside of this context with the same argument matchers.
351  * expects() calls cannot be chained with the same argument matchers.
352  * @param {object} fixture Test fixture.
353  * @param {string} testIdentityToken getAuthToken callback token.
354  * @param {boolean} testGeolocationPref Geolocation Preference callback value.
355  * @param {boolean} testUserRespondedToToast User Response to toast
356  *     callback value.
357  * @param {object} testExperimentVariationParams Response of
358  *     metricsPrivate.getVariationParams
359  */
360 function expectStateMachineCalls(
361     fixture,
362     testIdentityToken,
363     testGeolocationPref,
364     testUserRespondedToToast,
365     testExperimentVariationParams) {
366   var authenticationManagerIsSignedInSavedArgs = new SaveMockArguments();
367   fixture.mockApis.expects(once()).
368       authenticationManager_isSignedIn(
369           authenticationManagerIsSignedInSavedArgs.match(ANYTHING)).
370       will(invokeCallback(
371           authenticationManagerIsSignedInSavedArgs,
372           0,
373           testIdentityToken));
375   var getVariationParamsSavedArgs = new SaveMockArguments();
376   fixture.mockApis.expects(once()).
377       instrumented_metricsPrivate_getVariationParams(
378           getVariationParamsSavedArgs.match(ANYTHING),
379           getVariationParamsSavedArgs.match(ANYTHING)).
380       will(invokeCallback(
381           getVariationParamsSavedArgs, 1, testExperimentVariationParams));
383   var googleGeolocationPrefGetSavedArgs = new SaveMockArguments();
384   fixture.mockApis.expects(once()).
385       instrumented_preferencesPrivate_googleGeolocationAccessEnabled_get(
386           googleGeolocationPrefGetSavedArgs.match(eqJSON({})),
387           googleGeolocationPrefGetSavedArgs.match(ANYTHING)).
388       will(invokeCallback(
389           googleGeolocationPrefGetSavedArgs, 1, {value: testGeolocationPref}));
391   var storageGetSavedArgs = new SaveMockArguments();
392   fixture.mockApis.expects(once()).
393       instrumented_storage_local_get(
394           storageGetSavedArgs.match(eq('userRespondedToToast')),
395           storageGetSavedArgs.match(ANYTHING)).
396       will(invokeCallback(storageGetSavedArgs, 1, testUserRespondedToToast));
398   fixture.mockGlobals.expects(once()).
399       setBackgroundEnable(ANYTHING);
403  * Sets up the test to expect the initialization calls that
404  * initialize() invokes.
405  * Note that this CAN NOT be used if any of the methods below are called
406  * outside of this context with the same argument matchers.
407  * expects() calls cannot be chained with the same argument matchers.
408  */
409 function expectInitialization(mockApisObj) {
410   mockApisObj.expects(once()).
411       chrome_location_clearWatch(ANYTHING);
412   mockApisObj.expects(once()).
413       updateCardsAttempts_stop();
414   mockApisObj.expects(once()).
415       chrome_storage_local_remove(
416           eqJSON(['notificationsData', 'notificationGroups']));
417   var tasksAddSavedArgs = new SaveMockArguments();
418   mockApisObj.expects(once()).
419       tasks_add(
420           tasksAddSavedArgs.match(ANYTHING),
421           tasksAddSavedArgs.match(ANYTHING)).
422       will(invokeCallback(tasksAddSavedArgs, 1, function() {}));
423   var updateCardsAttemptsIsRunningSavedArgs = new SaveMockArguments();
424   mockApisObj.expects(once()).
425       updateCardsAttempts_isRunning(
426           updateCardsAttemptsIsRunningSavedArgs.match(ANYTHING)).
427       will(
428           invokeCallback(
429              updateCardsAttemptsIsRunningSavedArgs, 0, false));
432 TEST_F(
433     'GoogleNowBackgroundUnitTest',
434     'Initialize_ToastStateEmpty1',
435     function() {
436       // Tests the case when the user isn't signed in and NOTIFICATION_CARDS_URL
437       // is not set. Since NOTIFICATION_CARDS_URL is empty,
438       // nothing should start.
440       // Setup and expectations.
441       NOTIFICATION_CARDS_URL = undefined;
442       var testIdentityToken = undefined;
443       var testGeolocationPref = false;
444       var testUserRespondedToToast = {};
445       var testExperimentVariationParams = {};
447       mockInitializeDependencies(this);
449       this.mockGlobals.expects(once()).recordEvent(
450           GoogleNowEvent.EXTENSION_START);
452       this.mockGlobals.expects(once()).recordEvent(
453           GoogleNowEvent.STOPPED);
455       expectInitialization(this.mockApis);
457       expectStateMachineCalls(
458           this,
459           testIdentityToken,
460           testGeolocationPref,
461           testUserRespondedToToast,
462           testExperimentVariationParams);
464       var chromeNotificationGetAllSavedArgs = new SaveMockArguments();
465       this.mockApis.expects(exactly(2)).
466           instrumented_notifications_getAll(
467               chromeNotificationGetAllSavedArgs.match(ANYTHING)).
468           will(
469               invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}),
470               invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}));
472       // TODO(robliao,vadimt): Determine the granularity of testing to perform.
474       // Invoking the tested function.
475       initialize();
476     });
478 TEST_F(
479     'GoogleNowBackgroundUnitTest',
480     'Initialize_ToastStateEmpty2',
481     function() {
482       // Tests the case when NOTIFICATION_CARDS_URL is but getAuthToken fails
483       // most likely because the user is not signed in. In this case, the
484       // function should quietly exit after finding out that getAuthToken fails.
486       // Setup and expectations.
487       NOTIFICATION_CARDS_URL = 'https://some.server.url.com';
488       var testIdentityToken = undefined;
489       var testGeolocationPref = false;
490       var testUserRespondedToToast = {};
491       var testExperimentVariationParams = {};
493       mockInitializeDependencies(this);
495       this.mockGlobals.expects(once()).recordEvent(
496           GoogleNowEvent.EXTENSION_START);
498       this.mockGlobals.expects(once()).recordEvent(
499           GoogleNowEvent.STOPPED);
501       expectInitialization(this.mockApis);
503       expectStateMachineCalls(
504           this,
505           testIdentityToken,
506           testGeolocationPref,
507           testUserRespondedToToast,
508           testExperimentVariationParams);
510       var chromeNotificationGetAllSavedArgs = new SaveMockArguments();
511       this.mockApis.expects(exactly(2)).
512           instrumented_notifications_getAll(
513               chromeNotificationGetAllSavedArgs.match(ANYTHING)).
514           will(
515               invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}),
516               invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}));
518       // Invoking the tested function.
519       initialize();
520     });
522 TEST_F(
523     'GoogleNowBackgroundUnitTest',
524     'DISABLED_Initialize_ToastStateEmpty3',
525     function() {
526       // Tests the case when NOTIFICATION_CARDS_URL is set, getAuthToken
527       // succeeds, and the user has never responded to the toast.
528       // In this case, the function should invoke showWelcomeToast().
530       // Setup and expectations.
531       NOTIFICATION_CARDS_URL = 'https://some.server.url.com';
532       var testIdentityToken = 'some identity token';
533       var testGeolocationPref = false;
534       var testUserRespondedToToast = {};
535       var testExperimentVariationParams = {};
537       mockInitializeDependencies(this);
539       this.mockGlobals.expects(once()).recordEvent(
540           GoogleNowEvent.EXTENSION_START);
542       expectInitialization(this.mockApis);
544       expectStateMachineCalls(
545           this,
546           testIdentityToken,
547           testGeolocationPref,
548           testUserRespondedToToast,
549           testExperimentVariationParams);
551       var chromeNotificationGetAllSavedArgs = new SaveMockArguments();
552       this.mockApis.expects(exactly(2)).
553           instrumented_notifications_getAll(
554               chromeNotificationGetAllSavedArgs.match(ANYTHING)).
555           will(
556               invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}),
557               invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}));
559       this.mockGlobals.expects(once()).showWelcomeToast();
561       // Invoking the tested function.
562       initialize();
563     });
565 TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_RunGoogleNow', function() {
566   // Tests if Google Now will invoke startPollingCards when all
567   // of the required state is fulfilled.
569   // Setup and expectations.
570   NOTIFICATION_CARDS_URL = 'https://some.server.url.com';
571   var testIdentityToken = 'some identity token';
572   var testGeolocationPref = true;
573   var testUserRespondedToToast = {userRespondedToToast: true};
574   var testExperimentVariationParams = {};
576   mockInitializeDependencies(this);
578   this.mockGlobals.expects(once()).recordEvent(
579         GoogleNowEvent.EXTENSION_START);
581   expectInitialization(this.mockApis);
583   expectStateMachineCalls(
584       this,
585       testIdentityToken,
586       testGeolocationPref,
587       testUserRespondedToToast,
588       testExperimentVariationParams);
590   var chromeNotificationGetAllSavedArgs = new SaveMockArguments();
591   this.mockApis.expects(exactly(2)).
592       instrumented_notifications_getAll(
593           chromeNotificationGetAllSavedArgs.match(ANYTHING)).
594       will(
595           invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}),
596           invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}));
598   this.mockGlobals.expects(once()).startPollingCards();
600   // Invoking the tested function.
601   initialize();
604 TEST_F(
605     'GoogleNowBackgroundUnitTest',
606     'DISABLED_Initialize_NoGeolocation',
607     function() {
608   // Tests the case where everything is in place except for the
609   // Geolocation Preference after the user responded to the toast.
611   // Setup and expectations.
612   NOTIFICATION_CARDS_URL = 'https://some.server.url.com';
613   var testIdentityToken = 'some identity token';
614   var testGeolocationPref = false;
615   var testUserRespondedToToast = {userRespondedToToast: true};
616   var testExperimentVariationParams = {};
618   mockInitializeDependencies(this);
620   this.mockGlobals.expects(once()).recordEvent(
621         GoogleNowEvent.EXTENSION_START);
623   this.mockGlobals.expects(once()).recordEvent(
624       GoogleNowEvent.USER_SUPPRESSED);
626   expectInitialization(this.mockApis);
628   expectStateMachineCalls(
629       this,
630       testIdentityToken,
631       testGeolocationPref,
632       testUserRespondedToToast,
633       testExperimentVariationParams);
635   var chromeNotificationGetAllSavedArgs = new SaveMockArguments();
636   this.mockApis.expects(exactly(2)).
637       instrumented_notifications_getAll(
638           chromeNotificationGetAllSavedArgs.match(ANYTHING)).
639       will(
640           invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}),
641           invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}));
643   // Invoking the tested function.
644   initialize();
648  * Mocks global functions and APIs that onNotificationClicked() depends upon.
649  * @param {Test} fixture Test fixture.
650  */
651 function mockOnNotificationClickedDependencies(fixture) {
652   fixture.makeAndRegisterMockApis([
653       'chrome.windows.create',
654       'chrome.windows.update',
655       'instrumented.storage.local.get',
656       'instrumented.tabs.create']);
659 TEST_F(
660     'GoogleNowBackgroundUnitTest',
661     'OnNotificationClicked_NoData',
662     function() {
663       // Tests the case when there is no data associated with notification id.
664       // In this case, the function should do nothing.
666       // Setup and expectations.
667       var testNotificationId = 'TEST_ID';
668       var testNotificationData = {};
670       mockOnNotificationClickedDependencies(this);
671       this.makeMockLocalFunctions(['selector']);
673       var storageGetSavedArgs = new SaveMockArguments();
674       this.mockApis.expects(once()).
675           instrumented_storage_local_get(
676               storageGetSavedArgs.match(eq('notificationsData')),
677               storageGetSavedArgs.match(ANYTHING)).
678           will(invokeCallback(storageGetSavedArgs, 1, testNotificationData));
680       // Invoking the tested function.
681       onNotificationClicked(
682           testNotificationId, this.mockLocalFunctions.functions().selector);
683     });
685 TEST_F(
686     'GoogleNowBackgroundUnitTest',
687     'OnNotificationClicked_ActionUrlsUndefined',
688     function() {
689       // Tests the case when the data associated with notification id is
690       // 'undefined'.
691       // In this case, the function should do nothing.
693       // Setup and expectations.
694       var testActionUrls = undefined;
695       var testNotificationId = 'TEST_ID';
696       var testNotificationData = {
697           notificationsData: {'TEST_ID': {actionUrls: testActionUrls}}
698       };
700       mockOnNotificationClickedDependencies(this);
701       this.makeMockLocalFunctions(['selector']);
703       var storageGetSavedArgs = new SaveMockArguments();
704       this.mockApis.expects(once()).
705           instrumented_storage_local_get(
706               storageGetSavedArgs.match(eq('notificationsData')),
707               storageGetSavedArgs.match(ANYTHING)).
708           will(invokeCallback(storageGetSavedArgs, 1, testNotificationData));
709       this.mockLocalFunctions.expects(once()).selector(undefined).will(
710           returnValue(undefined));
712       // Invoking the tested function.
713       onNotificationClicked(
714           testNotificationId, this.mockLocalFunctions.functions().selector);
715     });
717 TEST_F(
718     'GoogleNowBackgroundUnitTest',
719     'OnNotificationClicked_TabCreateSuccess',
720     function() {
721       // Tests the selected URL is OK and crome.tabs.create suceeds.
723       // Setup and expectations.
724       var testActionUrls = {testField: 'TEST VALUE'};
725       var testNotificationId = 'TEST_ID';
726       var testNotificationData = {
727           notificationsData: {'TEST_ID': {actionUrls: testActionUrls}}
728       };
729       var testActionUrl = 'http://testurl.com';
730       var testCreatedTab = {windowId: 239};
732       mockOnNotificationClickedDependencies(this);
733       this.makeMockLocalFunctions(['selector']);
735       var storageGetSavedArgs = new SaveMockArguments();
736       this.mockApis.expects(once()).
737           instrumented_storage_local_get(
738               storageGetSavedArgs.match(eq('notificationsData')),
739               storageGetSavedArgs.match(ANYTHING)).
740           will(invokeCallback(storageGetSavedArgs, 1, testNotificationData));
741       this.mockLocalFunctions.expects(once()).selector(testActionUrls).will(
742           returnValue(testActionUrl));
743       var chromeTabsCreateSavedArgs = new SaveMockArguments();
744       this.mockApis.expects(once()).
745           instrumented_tabs_create(
746               chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})),
747               chromeTabsCreateSavedArgs.match(ANYTHING)).
748           will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab));
749       this.mockApis.expects(once()).chrome_windows_update(
750           testCreatedTab.windowId, 
751           eqJSON({focused: true}));
753       // Invoking the tested function.
754       onNotificationClicked(
755           testNotificationId, this.mockLocalFunctions.functions().selector);
756     });
758 TEST_F(
759     'GoogleNowBackgroundUnitTest',
760     'OnNotificationClicked_TabCreateFail',
761     function() {
762       // Tests the selected URL is OK and crome.tabs.create fails.
763       // In this case, the function should invoke chrome.windows.create as a
764       // second attempt.
766       // Setup and expectations.
767       var testActionUrls = {testField: 'TEST VALUE'};
768       var testNotificationId = 'TEST_ID';
769       var testNotificationData = {
770         notificationsData: {'TEST_ID': {actionUrls: testActionUrls}}
771       };
772       var testActionUrl = 'http://testurl.com';
773       var testCreatedTab = undefined; // chrome.tabs.create fails
775       mockOnNotificationClickedDependencies(this);
776       this.makeMockLocalFunctions(['selector']);
778       var storageGetSavedArgs = new SaveMockArguments();
779       this.mockApis.expects(once()).
780           instrumented_storage_local_get(
781               storageGetSavedArgs.match(eq('notificationsData')),
782               storageGetSavedArgs.match(ANYTHING)).
783           will(invokeCallback(storageGetSavedArgs, 1, testNotificationData));
784       this.mockLocalFunctions.expects(once()).selector(testActionUrls).will(
785           returnValue(testActionUrl));
786       var chromeTabsCreateSavedArgs = new SaveMockArguments();
787       this.mockApis.expects(once()).
788           instrumented_tabs_create(
789               chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})),
790               chromeTabsCreateSavedArgs.match(ANYTHING)).
791           will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab));
792       this.mockApis.expects(once()).chrome_windows_create(
793           eqJSON({url: testActionUrl, focused: true}));
795       // Invoking the tested function.
796       onNotificationClicked(
797           testNotificationId, this.mockLocalFunctions.functions().selector);
798     });