Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 /** | 5 /** |
| 6 * Test fixture for background.js. | 6 * Test fixture for background.js. |
| 7 * @constructor | 7 * @constructor |
| 8 * @extends {testing.Test} | 8 * @extends {testing.Test} |
| 9 */ | 9 */ |
| 10 function GoogleNowBackgroundUnitTest () { | 10 function GoogleNowBackgroundUnitTest () { |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 | 311 |
| 312 // Check the output parameter. | 312 // Check the output parameter. |
| 313 assertEquals( | 313 assertEquals( |
| 314 JSON.stringify({ | 314 JSON.stringify({ |
| 315 'ID 2': { testField: 'RESULT 2' }, | 315 'ID 2': { testField: 'RESULT 2' }, |
| 316 'ID 3': { testField: 'TEST_FIELD3'}, | 316 'ID 3': { testField: 'TEST_FIELD3'}, |
| 317 'ID 1': { testField: 'RESULT 1' }}), | 317 'ID 1': { testField: 'RESULT 1' }}), |
| 318 JSON.stringify(mergedCards)); | 318 JSON.stringify(mergedCards)); |
| 319 }); | 319 }); |
| 320 | 320 |
| 321 TEST_F( | |
| 322 'GoogleNowBackgroundUnitTest', | |
| 323 'MergeAndShowNotificationCards', | |
| 324 function() { | |
| 325 // Tests mergeAndShowNotificationCards function. | |
|
robliao
2013/10/21 23:08:33
Discuss what is passed in and what's expected.
vadimt
2013/10/22 00:57:07
Done.
| |
| 326 | |
| 327 // Setup and expectations. | |
| 328 var testGroups = { | |
| 329 'TEST GROUP 1': {testField: 'TEST VALUE 1'}, | |
| 330 'TEST GROUP 2': {testField: 'TEST VALUE 2'} | |
| 331 }; | |
| 332 | |
| 333 this.makeAndRegisterMockGlobals(['mergeGroup', 'showNotificationCards']); | |
| 334 | |
| 335 var mergeGroupSavedArgs = new SaveMockArguments(); | |
| 336 this.mockGlobals.expects(once()). | |
| 337 mergeGroup( | |
| 338 mergeGroupSavedArgs.match(eqJSON({})), | |
| 339 mergeGroupSavedArgs.match(eqJSON({testField: 'TEST VALUE 1'}))). | |
| 340 will(callFunction(function() { | |
| 341 mergeGroupSavedArgs.arguments[0].card1 = { | |
| 342 testValue: 'TEST CARD VALUE 1' | |
| 343 }; | |
| 344 })); | |
| 345 this.mockGlobals.expects(once()). | |
| 346 mergeGroup( | |
| 347 mergeGroupSavedArgs.match( | |
| 348 eqJSON({card1: {testValue: 'TEST CARD VALUE 1'}})), | |
|
robliao
2013/10/21 23:08:33
Is card1 guaranteed to be the first thing consider
vadimt
2013/10/22 00:57:07
This is how our JS implementation works.
I doubt t
robliao
2013/10/22 16:52:14
I think v8 is one of those implementations where t
| |
| 349 mergeGroupSavedArgs.match( | |
| 350 eqJSON({testField: 'TEST VALUE 2'}))). | |
| 351 will(callFunction(function() { | |
| 352 mergeGroupSavedArgs.arguments[0].card2 = { | |
| 353 testValue: 'TEST CARD VALUE 2' | |
| 354 }; | |
| 355 })); | |
| 356 this.mockGlobals.expects(once()). | |
| 357 showNotificationCards(eqJSON({ | |
| 358 card1: {testValue: 'TEST CARD VALUE 1'}, | |
| 359 card2: {testValue: 'TEST CARD VALUE 2'} | |
| 360 })); | |
| 361 | |
| 362 // Invoking the tested function. | |
| 363 mergeAndShowNotificationCards(testGroups); | |
| 364 }); | |
| 365 | |
| 366 // TODO(vadimt): Add more tests for parseAndShowNotificationCards(). | |
| 367 TEST_F( | |
| 368 'GoogleNowBackgroundUnitTest', | |
| 369 'ParseAndShowNotificationCardsAdd1Remove1', | |
| 370 function() { | |
| 371 // Tests parseAndShowNotificationCards function for the case when the | |
| 372 // extension has 2 groups, and the server sends update with 2 groups, one | |
| 373 // of which is new, and another one matches a stored group. The client | |
| 374 // has to delete the group that didn't receive an update, keep the | |
| 375 // existing group that received an update, and add a new stored group for | |
| 376 // the new group from the server. | |
| 377 | |
| 378 // Setup and expectations. | |
| 379 Date.now = function() { return 500; }; | |
| 380 | |
| 381 var serverResponse = { | |
| 382 groups: { | |
| 383 GROUP1: {}, | |
| 384 GROUP2: {} | |
| 385 } | |
| 386 }; | |
| 387 | |
| 388 var storedGroups = { | |
| 389 GROUP2: { | |
| 390 cards: ['c2'], | |
| 391 cardsTimestamp: 239, | |
| 392 nextPollTime: 10000, | |
| 393 rank: 1 | |
| 394 }, | |
| 395 GROUP3: { | |
| 396 cards: ['c3'], | |
| 397 cardsTimestamp: 240, | |
| 398 nextPollTime: 10001, | |
| 399 rank: 2 | |
| 400 } | |
| 401 }; | |
| 402 | |
| 403 var expectedUpdatedGroups = { | |
| 404 GROUP1: { | |
| 405 cards: [], | |
| 406 nextPollTime: Date.now() | |
| 407 }, | |
| 408 GROUP2: { | |
| 409 cards: ['c2'], | |
| 410 cardsTimestamp: 239, | |
| 411 nextPollTime: 10000, | |
| 412 rank: 1 | |
| 413 } | |
| 414 }; | |
| 415 | |
| 416 this.makeAndRegisterMockGlobals( | |
| 417 ['scheduleNextPoll', 'mergeAndShowNotificationCards', 'recordEvent']); | |
| 418 | |
| 419 this.makeAndRegisterMockApis([ | |
| 420 'chrome.storage.local.set', | |
| 421 'instrumented.storage.local.get' | |
| 422 ]); | |
| 423 | |
| 424 var storageGetSavedArgs = new SaveMockArguments(); | |
| 425 this.mockApis.expects(once()). | |
| 426 instrumented_storage_local_get( | |
| 427 storageGetSavedArgs.match(eq('notificationGroups')), | |
| 428 storageGetSavedArgs.match(ANYTHING)). | |
| 429 will(invokeCallback( | |
| 430 storageGetSavedArgs, 1, {notificationGroups: storedGroups})); | |
| 431 | |
| 432 this.mockGlobals.expects(once()). | |
| 433 scheduleNextPoll(eqJSON(expectedUpdatedGroups)); | |
| 434 | |
| 435 this.mockApis.expects(once()). | |
| 436 chrome_storage_local_set( | |
| 437 eqJSON({notificationGroups: expectedUpdatedGroups})); | |
| 438 | |
| 439 this.mockGlobals.expects(once()). | |
| 440 mergeAndShowNotificationCards(eqJSON(expectedUpdatedGroups)); | |
| 441 | |
| 442 this.mockGlobals.expects(once()). | |
| 443 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); | |
| 444 | |
| 445 // Invoking the tested function. | |
| 446 parseAndShowNotificationCards(JSON.stringify(serverResponse)); | |
| 447 }); | |
| 448 | |
| 321 /** | 449 /** |
| 322 * Mocks global functions and APIs that initialize() depends upon. | 450 * Mocks global functions and APIs that initialize() depends upon. |
| 323 * @param {Test} fixture Test fixture. | 451 * @param {Test} fixture Test fixture. |
| 324 */ | 452 */ |
| 325 function mockInitializeDependencies(fixture) { | 453 function mockInitializeDependencies(fixture) { |
| 326 fixture.makeAndRegisterMockGlobals([ | 454 fixture.makeAndRegisterMockGlobals([ |
| 327 'recordEvent', | 455 'recordEvent', |
| 328 'setBackgroundEnable', | 456 'setBackgroundEnable', |
| 329 'startPollingCards' | 457 'startPollingCards' |
| 330 ]); | 458 ]); |
| (...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 735 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), | 863 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), |
| 736 chromeTabsCreateSavedArgs.match(ANYTHING)). | 864 chromeTabsCreateSavedArgs.match(ANYTHING)). |
| 737 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); | 865 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); |
| 738 this.mockApis.expects(once()).chrome_windows_create( | 866 this.mockApis.expects(once()).chrome_windows_create( |
| 739 eqJSON({url: testActionUrl, focused: true})); | 867 eqJSON({url: testActionUrl, focused: true})); |
| 740 | 868 |
| 741 // Invoking the tested function. | 869 // Invoking the tested function. |
| 742 onNotificationClicked( | 870 onNotificationClicked( |
| 743 testNotificationId, this.mockLocalFunctions.functions().selector); | 871 testNotificationId, this.mockLocalFunctions.functions().selector); |
| 744 }); | 872 }); |
| OLD | NEW |