Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(387)

Side by Side Diff: chrome/browser/resources/google_now/background_unittest.gtestjs

Issue 166033010: Convert Google Now's use of chrome.local.storage.get to use Promises (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: CR Feedback Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // TODO(robliao,vadimt): Determine the granularity of testing to perform. 5 // TODO(robliao,vadimt): Determine the granularity of testing to perform.
6 6
7 /** 7 /**
8 * Test fixture for background.js. 8 * Test fixture for background.js.
9 * @constructor 9 * @constructor
10 * @extends {testing.Test} 10 * @extends {testing.Test}
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 */ 57 */
58 function mockInitializeDependencies(fixture) { 58 function mockInitializeDependencies(fixture) {
59 fixture.makeAndRegisterMockGlobals([ 59 fixture.makeAndRegisterMockGlobals([
60 'recordEvent', 60 'recordEvent',
61 'setBackgroundEnable', 61 'setBackgroundEnable',
62 'startPollingCards' 62 'startPollingCards'
63 ]); 63 ]);
64 fixture.makeAndRegisterMockApis([ 64 fixture.makeAndRegisterMockApis([
65 'authenticationManager.isSignedIn', 65 'authenticationManager.isSignedIn',
66 'chrome.storage.local.remove', 66 'chrome.storage.local.remove',
67 'fillFromChromeLocalStorage',
67 'instrumented.metricsPrivate.getVariationParams', 68 'instrumented.metricsPrivate.getVariationParams',
68 'instrumented.notifications.getAll', 69 'instrumented.notifications.getAll',
69 'instrumented.notifications.getPermissionLevel', 70 'instrumented.notifications.getPermissionLevel',
70 'instrumented.storage.local.get',
71 'instrumented.webstorePrivate.getBrowserLogin', 71 'instrumented.webstorePrivate.getBrowserLogin',
72 'tasks.add', 72 'tasks.add',
73 'updateCardsAttempts.isRunning', 73 'updateCardsAttempts.isRunning',
74 'updateCardsAttempts.stop' 74 'updateCardsAttempts.stop'
75 ]); 75 ]);
76 } 76 }
77 77
78 /** 78 /**
79 * Sets up the test to expect the state machine calls and send 79 * Sets up the test to expect the state machine calls and send
80 * the specified state machine state. Currently used to test initialize(). 80 * the specified state machine state. Currently used to test initialize().
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 112
113 var notificationGetPermissionLevelSavedArgs = new SaveMockArguments(); 113 var notificationGetPermissionLevelSavedArgs = new SaveMockArguments();
114 fixture.mockApis.expects(once()). 114 fixture.mockApis.expects(once()).
115 instrumented_notifications_getPermissionLevel( 115 instrumented_notifications_getPermissionLevel(
116 notificationGetPermissionLevelSavedArgs.match(ANYTHING)). 116 notificationGetPermissionLevelSavedArgs.match(ANYTHING)).
117 will(invokeCallback( 117 will(invokeCallback(
118 notificationGetPermissionLevelSavedArgs, 118 notificationGetPermissionLevelSavedArgs,
119 0, 119 0,
120 testNotificationPermissionLevel)) 120 testNotificationPermissionLevel))
121 121
122 var storageGetSavedArgs = new SaveMockArguments(); 122 expectChromeLocalStorageGet(
123 fixture.mockApis.expects(once()). 123 fixture,
124 instrumented_storage_local_get( 124 {googleNowEnabled: false},
125 storageGetSavedArgs.match(eq('googleNowEnabled')), 125 {googleNowEnabled: testGoogleNowEnabled});
126 storageGetSavedArgs.match(ANYTHING)).
127 will(invokeCallback(
128 storageGetSavedArgs, 1, {googleNowEnabled: testGoogleNowEnabled}));
129 126
130 fixture.mockGlobals.expects(once()). 127 fixture.mockGlobals.expects(once()).
131 setBackgroundEnable(ANYTHING); 128 setBackgroundEnable(ANYTHING);
132 } 129 }
133 130
134 /** 131 /**
135 * Sets up the test to expect the initialization calls that 132 * Sets up the test to expect the initialization calls that
136 * initialize() invokes. 133 * initialize() invokes.
137 * Note that this CAN NOT be used if any of the methods below are called 134 * Note that this CAN NOT be used if any of the methods below are called
138 * outside of this context with the same argument matchers. 135 * outside of this context with the same argument matchers.
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 }); 217 });
221 218
222 /** 219 /**
223 * Mocks global functions and APIs that onNotificationClicked() depends upon. 220 * Mocks global functions and APIs that onNotificationClicked() depends upon.
224 * @param {Test} fixture Test fixture. 221 * @param {Test} fixture Test fixture.
225 */ 222 */
226 function mockOnNotificationClickedDependencies(fixture) { 223 function mockOnNotificationClickedDependencies(fixture) {
227 fixture.makeAndRegisterMockApis([ 224 fixture.makeAndRegisterMockApis([
228 'chrome.windows.create', 225 'chrome.windows.create',
229 'chrome.windows.update', 226 'chrome.windows.update',
230 'instrumented.storage.local.get', 227 'fillFromChromeLocalStorage',
231 'instrumented.tabs.create']); 228 'instrumented.tabs.create']);
232 } 229 }
233 230
234 TEST_F( 231 TEST_F(
235 'GoogleNowBackgroundUnitTest', 232 'GoogleNowBackgroundUnitTest',
236 'OnNotificationClicked_NoData', 233 'OnNotificationClicked_NoData',
237 function() { 234 function() {
238 // Tests the case when there is no data associated with notification id. 235 // Tests the case when there is no data associated with notification id.
239 // In this case, the function should do nothing. 236 // In this case, the function should do nothing.
240 237
241 // Setup and expectations. 238 // Setup and expectations.
242 var testNotificationId = 'TEST_ID'; 239 var testNotificationId = 'TEST_ID';
243 var testNotificationData = {}; 240 var testNotificationDataRequest = {notificationsData: {}};
241 var testNotificationData = {notificationsData: {}};
244 242
245 mockOnNotificationClickedDependencies(this); 243 mockOnNotificationClickedDependencies(this);
246 this.makeMockLocalFunctions(['selector']); 244 this.makeMockLocalFunctions(['selector']);
247 245
248 var storageGetSavedArgs = new SaveMockArguments(); 246 expectChromeLocalStorageGet(
249 this.mockApis.expects(once()). 247 this, testNotificationDataRequest, testNotificationData);
250 instrumented_storage_local_get(
251 storageGetSavedArgs.match(eq('notificationsData')),
252 storageGetSavedArgs.match(ANYTHING)).
253 will(invokeCallback(storageGetSavedArgs, 1, testNotificationData));
254 248
255 // Invoking the tested function. 249 // Invoking the tested function.
256 onNotificationClicked( 250 onNotificationClicked(
257 testNotificationId, this.mockLocalFunctions.functions().selector); 251 testNotificationId, this.mockLocalFunctions.functions().selector);
258 }); 252 });
259 253
260 TEST_F( 254 TEST_F(
261 'GoogleNowBackgroundUnitTest', 255 'GoogleNowBackgroundUnitTest',
262 'OnNotificationClicked_ActionUrlsUndefined', 256 'OnNotificationClicked_ActionUrlsUndefined',
263 function() { 257 function() {
264 // Tests the case when the data associated with notification id is 258 // Tests the case when the data associated with notification id is
265 // 'undefined'. 259 // 'undefined'.
266 // In this case, the function should do nothing. 260 // In this case, the function should do nothing.
267 261
268 // Setup and expectations. 262 // Setup and expectations.
269 var testActionUrls = undefined; 263 var testActionUrls = undefined;
270 var testNotificationId = 'TEST_ID'; 264 var testNotificationId = 'TEST_ID';
265 var testNotificationDataRequest = {notificationsData: {}};
271 var testNotificationData = { 266 var testNotificationData = {
272 notificationsData: {'TEST_ID': {actionUrls: testActionUrls}} 267 notificationsData: {'TEST_ID': {actionUrls: testActionUrls}}
273 }; 268 };
274 269
275 mockOnNotificationClickedDependencies(this); 270 mockOnNotificationClickedDependencies(this);
276 this.makeMockLocalFunctions(['selector']); 271 this.makeMockLocalFunctions(['selector']);
277 272
278 var storageGetSavedArgs = new SaveMockArguments(); 273 expectChromeLocalStorageGet(
279 this.mockApis.expects(once()). 274 this, testNotificationDataRequest, testNotificationData);
280 instrumented_storage_local_get( 275
281 storageGetSavedArgs.match(eq('notificationsData')),
282 storageGetSavedArgs.match(ANYTHING)).
283 will(invokeCallback(storageGetSavedArgs, 1, testNotificationData));
284 this.mockLocalFunctions.expects(once()).selector(undefined).will( 276 this.mockLocalFunctions.expects(once()).selector(undefined).will(
285 returnValue(undefined)); 277 returnValue(undefined));
286 278
287 // Invoking the tested function. 279 // Invoking the tested function.
288 onNotificationClicked( 280 onNotificationClicked(
289 testNotificationId, this.mockLocalFunctions.functions().selector); 281 testNotificationId, this.mockLocalFunctions.functions().selector);
290 }); 282 });
291 283
292 TEST_F( 284 TEST_F(
293 'GoogleNowBackgroundUnitTest', 285 'GoogleNowBackgroundUnitTest',
294 'OnNotificationClicked_TabCreateSuccess', 286 'OnNotificationClicked_TabCreateSuccess',
295 function() { 287 function() {
296 // Tests the selected URL is OK and crome.tabs.create suceeds. 288 // Tests the selected URL is OK and crome.tabs.create suceeds.
297 289
298 // Setup and expectations. 290 // Setup and expectations.
299 var testActionUrls = {testField: 'TEST VALUE'}; 291 var testActionUrls = {testField: 'TEST VALUE'};
300 var testNotificationId = 'TEST_ID'; 292 var testNotificationId = 'TEST_ID';
293 var testNotificationDataRequest = {notificationsData: {}};
301 var testNotificationData = { 294 var testNotificationData = {
302 notificationsData: {'TEST_ID': {actionUrls: testActionUrls}} 295 notificationsData: {'TEST_ID': {actionUrls: testActionUrls}}
303 }; 296 };
304 var testActionUrl = 'http://testurl.com'; 297 var testActionUrl = 'http://testurl.com';
305 var testCreatedTab = {windowId: 239}; 298 var testCreatedTab = {windowId: 239};
306 299
307 mockOnNotificationClickedDependencies(this); 300 mockOnNotificationClickedDependencies(this);
308 this.makeMockLocalFunctions(['selector']); 301 this.makeMockLocalFunctions(['selector']);
309 302
310 var storageGetSavedArgs = new SaveMockArguments(); 303 expectChromeLocalStorageGet(
311 this.mockApis.expects(once()). 304 this, testNotificationDataRequest, testNotificationData);
312 instrumented_storage_local_get(
313 storageGetSavedArgs.match(eq('notificationsData')),
314 storageGetSavedArgs.match(ANYTHING)).
315 will(invokeCallback(storageGetSavedArgs, 1, testNotificationData));
316 this.mockLocalFunctions.expects(once()).selector(testActionUrls).will( 305 this.mockLocalFunctions.expects(once()).selector(testActionUrls).will(
317 returnValue(testActionUrl)); 306 returnValue(testActionUrl));
318 var chromeTabsCreateSavedArgs = new SaveMockArguments(); 307 var chromeTabsCreateSavedArgs = new SaveMockArguments();
319 this.mockApis.expects(once()). 308 this.mockApis.expects(once()).
320 instrumented_tabs_create( 309 instrumented_tabs_create(
321 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), 310 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})),
322 chromeTabsCreateSavedArgs.match(ANYTHING)). 311 chromeTabsCreateSavedArgs.match(ANYTHING)).
323 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); 312 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab));
324 this.mockApis.expects(once()).chrome_windows_update( 313 this.mockApis.expects(once()).chrome_windows_update(
325 testCreatedTab.windowId, 314 testCreatedTab.windowId,
326 eqJSON({focused: true})); 315 eqJSON({focused: true}));
327 316
328 // Invoking the tested function. 317 // Invoking the tested function.
329 onNotificationClicked( 318 onNotificationClicked(
330 testNotificationId, this.mockLocalFunctions.functions().selector); 319 testNotificationId, this.mockLocalFunctions.functions().selector);
331 }); 320 });
332 321
333 TEST_F( 322 TEST_F(
334 'GoogleNowBackgroundUnitTest', 323 'GoogleNowBackgroundUnitTest',
335 'OnNotificationClicked_TabCreateFail', 324 'OnNotificationClicked_TabCreateFail',
336 function() { 325 function() {
337 // Tests the selected URL is OK and crome.tabs.create fails. 326 // Tests the selected URL is OK and crome.tabs.create fails.
338 // In this case, the function should invoke chrome.windows.create as a 327 // In this case, the function should invoke chrome.windows.create as a
339 // second attempt. 328 // second attempt.
340 329
341 // Setup and expectations. 330 // Setup and expectations.
342 var testActionUrls = {testField: 'TEST VALUE'}; 331 var testActionUrls = {testField: 'TEST VALUE'};
343 var testNotificationId = 'TEST_ID'; 332 var testNotificationId = 'TEST_ID';
333 var testNotificationDataRequest = {notificationsData: {}};
344 var testNotificationData = { 334 var testNotificationData = {
345 notificationsData: {'TEST_ID': {actionUrls: testActionUrls}} 335 notificationsData: {'TEST_ID': {actionUrls: testActionUrls}}
346 }; 336 };
347 var testActionUrl = 'http://testurl.com'; 337 var testActionUrl = 'http://testurl.com';
348 var testCreatedTab = undefined; // chrome.tabs.create fails 338 var testCreatedTab = undefined; // chrome.tabs.create fails
349 339
350 mockOnNotificationClickedDependencies(this); 340 mockOnNotificationClickedDependencies(this);
351 this.makeMockLocalFunctions(['selector']); 341 this.makeMockLocalFunctions(['selector']);
352 342
353 var storageGetSavedArgs = new SaveMockArguments(); 343 expectChromeLocalStorageGet(
354 this.mockApis.expects(once()). 344 this, testNotificationDataRequest, testNotificationData);
355 instrumented_storage_local_get(
356 storageGetSavedArgs.match(eq('notificationsData')),
357 storageGetSavedArgs.match(ANYTHING)).
358 will(invokeCallback(storageGetSavedArgs, 1, testNotificationData));
359 this.mockLocalFunctions.expects(once()).selector(testActionUrls).will( 345 this.mockLocalFunctions.expects(once()).selector(testActionUrls).will(
360 returnValue(testActionUrl)); 346 returnValue(testActionUrl));
361 var chromeTabsCreateSavedArgs = new SaveMockArguments(); 347 var chromeTabsCreateSavedArgs = new SaveMockArguments();
362 this.mockApis.expects(once()). 348 this.mockApis.expects(once()).
363 instrumented_tabs_create( 349 instrumented_tabs_create(
364 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), 350 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})),
365 chromeTabsCreateSavedArgs.match(ANYTHING)). 351 chromeTabsCreateSavedArgs.match(ANYTHING)).
366 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); 352 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab));
367 this.mockApis.expects(once()).chrome_windows_create( 353 this.mockApis.expects(once()).chrome_windows_create(
368 eqJSON({url: testActionUrl, focused: true})); 354 eqJSON({url: testActionUrl, focused: true}));
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 } 632 }
647 }; 633 };
648 634
649 var expectedUpdatedRecentDismissals = { 635 var expectedUpdatedRecentDismissals = {
650 c5: 1800001 636 c5: 1800001
651 }; 637 };
652 638
653 var fakeOnCardShownFunction = 'FAKE ON CARD SHOWN FUNCTION'; 639 var fakeOnCardShownFunction = 'FAKE ON CARD SHOWN FUNCTION';
654 640
655 this.makeAndRegisterMockGlobals([ 641 this.makeAndRegisterMockGlobals([
656 'scheduleNextPoll',
657 'combineAndShowNotificationCards', 642 'combineAndShowNotificationCards',
658 'recordEvent' 643 'recordEvent',
644 'scheduleNextPoll'
659 ]); 645 ]);
660 646
661 this.makeAndRegisterMockApis([ 647 this.makeAndRegisterMockApis([
662 'chrome.storage.local.set', 648 'chrome.storage.local.set',
663 'instrumented.storage.local.get' 649 'fillFromChromeLocalStorage'
664 ]); 650 ]);
665 651
666 var storageGetSavedArgs = new SaveMockArguments(); 652 expectChromeLocalStorageGet(
667 this.mockApis.expects(once()). 653 this,
668 instrumented_storage_local_get( 654 {
669 storageGetSavedArgs.match( 655 notificationGroups: {},
670 eq(['notificationGroups', 'recentDismissals'])), 656 recentDismissals: {}
671 storageGetSavedArgs.match(ANYTHING)). 657 },
672 will(invokeCallback( 658 {
673 storageGetSavedArgs, 659 notificationGroups: storedGroups,
674 1, 660 recentDismissals: recentDismissals
675 { 661 });
676 notificationGroups: storedGroups,
677 recentDismissals: recentDismissals
678 }));
679 662
680 this.mockGlobals.expects(once()). 663 this.mockGlobals.expects(once()).
681 scheduleNextPoll(eqJSON(expectedUpdatedGroups), true); 664 scheduleNextPoll(eqJSON(expectedUpdatedGroups), true);
682 665
683 var combineAndShowNotificationCardsSavedArgs = new SaveMockArguments(); 666 var combineAndShowNotificationCardsSavedArgs = new SaveMockArguments();
684 this.mockGlobals.expects(once()). 667 this.mockGlobals.expects(once()).
685 combineAndShowNotificationCards( 668 combineAndShowNotificationCards(
686 combineAndShowNotificationCardsSavedArgs.match( 669 combineAndShowNotificationCardsSavedArgs.match(
687 eqJSON(expectedUpdatedGroups)), 670 eqJSON(expectedUpdatedGroups)),
688 combineAndShowNotificationCardsSavedArgs.match( 671 combineAndShowNotificationCardsSavedArgs.match(
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 nextPollTime: 10001 714 nextPollTime: 10001
732 } 715 }
733 }; 716 };
734 717
735 var expectedUpdatedGroups = { 718 var expectedUpdatedGroups = {
736 }; 719 };
737 720
738 var fakeOnCardShownFunction = 'FAKE ON CARD SHOWN FUNCTION'; 721 var fakeOnCardShownFunction = 'FAKE ON CARD SHOWN FUNCTION';
739 722
740 this.makeAndRegisterMockGlobals([ 723 this.makeAndRegisterMockGlobals([
741 'scheduleNextPoll',
742 'combineAndShowNotificationCards', 724 'combineAndShowNotificationCards',
725 'onStateChange',
743 'recordEvent', 726 'recordEvent',
744 'onStateChange' 727 'scheduleNextPoll'
745 ]); 728 ]);
746 729
747 this.makeAndRegisterMockApis([ 730 this.makeAndRegisterMockApis([
748 'chrome.storage.local.set', 731 'chrome.storage.local.set',
749 'instrumented.storage.local.get' 732 'fillFromChromeLocalStorage'
750 ]); 733 ]);
751 734
752 this.mockApis.expects(once()). 735 this.mockApis.expects(once()).
753 chrome_storage_local_set( 736 chrome_storage_local_set(
754 eqJSON({googleNowEnabled: false})); 737 eqJSON({googleNowEnabled: false}));
755 738
756 this.mockGlobals.expects(once()).onStateChange(); 739 this.mockGlobals.expects(once()).onStateChange();
757 740
758 var storageGetSavedArgs = new SaveMockArguments(); 741 expectChromeLocalStorageGet(
759 this.mockApis.expects(once()). 742 this,
760 instrumented_storage_local_get( 743 {
761 storageGetSavedArgs.match( 744 notificationGroups: {},
762 eq(['notificationGroups', 'recentDismissals'])), 745 recentDismissals: {}
763 storageGetSavedArgs.match(ANYTHING)). 746 },
764 will(invokeCallback( 747 {
765 storageGetSavedArgs, 1, {notificationGroups: storedGroups})); 748 notificationGroups: storedGroups,
749 recentDismissals: {}
750 });
766 751
767 this.mockGlobals.expects(once()). 752 this.mockGlobals.expects(once()).
768 scheduleNextPoll(eqJSON(expectedUpdatedGroups), false); 753 scheduleNextPoll(eqJSON(expectedUpdatedGroups), false);
769 754
770 var combineAndShowNotificationCardsSavedArgs = new SaveMockArguments(); 755 var combineAndShowNotificationCardsSavedArgs = new SaveMockArguments();
771 this.mockGlobals.expects(once()). 756 this.mockGlobals.expects(once()).
772 combineAndShowNotificationCards( 757 combineAndShowNotificationCards(
773 combineAndShowNotificationCardsSavedArgs.match( 758 combineAndShowNotificationCardsSavedArgs.match(
774 eqJSON(expectedUpdatedGroups)), 759 eqJSON(expectedUpdatedGroups)),
775 combineAndShowNotificationCardsSavedArgs.match( 760 combineAndShowNotificationCardsSavedArgs.match(
776 ANYTHING), 761 ANYTHING),
777 combineAndShowNotificationCardsSavedArgs.match( 762 combineAndShowNotificationCardsSavedArgs.match(
778 eq(fakeOnCardShownFunction))). 763 eq(fakeOnCardShownFunction))).
779 will(invokeCallback(combineAndShowNotificationCardsSavedArgs, 1)); 764 will(invokeCallback(combineAndShowNotificationCardsSavedArgs, 1));
780 765
781 this.mockApis.expects(once()). 766 this.mockApis.expects(once()).
782 chrome_storage_local_set( 767 chrome_storage_local_set(
783 eqJSON({ 768 eqJSON({
784 notificationGroups: expectedUpdatedGroups, 769 notificationGroups: expectedUpdatedGroups,
785 recentDismissals: {}})); 770 recentDismissals: {}}));
786 771
787 this.mockGlobals.expects(once()). 772 this.mockGlobals.expects(once()).
788 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); 773 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS);
789 774
790 // Invoking the tested function. 775 // Invoking the tested function.
791 processServerResponse(serverResponse, fakeOnCardShownFunction); 776 processServerResponse(serverResponse, fakeOnCardShownFunction);
792 }); 777 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/google_now/background.js ('k') | chrome/browser/resources/google_now/cards.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698