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

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

Issue 32583004: Some unit tests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: robliao@ comments Created 7 years, 2 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 /** 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
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.
326 // The test passes 2 groups to mergeAndShowNotificationCards, checks that
327 // it calls mergeGroup() for each of these groups and calls
328 // showNotificationCards() with the results of these mergeGroup() calls.
329
330 // Setup and expectations.
331 var testGroups = {
332 'TEST GROUP 1': {testField: 'TEST VALUE 1'},
333 'TEST GROUP 2': {testField: 'TEST VALUE 2'}
334 };
335
336 this.makeAndRegisterMockGlobals(['mergeGroup', 'showNotificationCards']);
337
338 var mergeGroupSavedArgs = new SaveMockArguments();
339 this.mockGlobals.expects(once()).
340 mergeGroup(
341 mergeGroupSavedArgs.match(eqJSON({})),
342 mergeGroupSavedArgs.match(eqJSON({testField: 'TEST VALUE 1'}))).
343 will(callFunction(function() {
344 mergeGroupSavedArgs.arguments[0].card1 = {
345 testValue: 'TEST CARD VALUE 1'
346 };
347 }));
348 this.mockGlobals.expects(once()).
349 mergeGroup(
350 mergeGroupSavedArgs.match(
351 eqJSON({card1: {testValue: 'TEST CARD VALUE 1'}})),
352 mergeGroupSavedArgs.match(
353 eqJSON({testField: 'TEST VALUE 2'}))).
354 will(callFunction(function() {
355 mergeGroupSavedArgs.arguments[0].card2 = {
356 testValue: 'TEST CARD VALUE 2'
357 };
358 }));
359 this.mockGlobals.expects(once()).
360 showNotificationCards(eqJSON({
361 card1: {testValue: 'TEST CARD VALUE 1'},
362 card2: {testValue: 'TEST CARD VALUE 2'}
363 }));
364
365 // Invoking the tested function.
366 mergeAndShowNotificationCards(testGroups);
367 });
368
369 // TODO(vadimt): Add more tests for parseAndShowNotificationCards().
370 TEST_F(
371 'GoogleNowBackgroundUnitTest',
372 'ParseAndShowNotificationCardsAdd1Remove1',
373 function() {
374 // Tests parseAndShowNotificationCards function for the case when the
375 // extension has 2 groups, and the server sends update with 2 groups, one
376 // of which is new, and another one matches a stored group. The client
377 // has to delete the group that didn't receive an update, keep the
378 // existing group that received an update, and add a new stored group for
379 // the new group from the server.
380
381 // Setup and expectations.
382 Date.now = function() { return 500; };
383
384 var serverResponse = {
385 groups: {
386 GROUP1: {},
387 GROUP2: {}
388 }
389 };
390
391 var storedGroups = {
392 GROUP2: {
393 cards: ['c2'],
394 cardsTimestamp: 239,
395 nextPollTime: 10000,
396 rank: 1
397 },
398 GROUP3: {
399 cards: ['c3'],
400 cardsTimestamp: 240,
401 nextPollTime: 10001,
402 rank: 2
403 }
404 };
405
406 var expectedUpdatedGroups = {
407 GROUP1: {
408 cards: [],
409 nextPollTime: Date.now()
410 },
411 GROUP2: {
412 cards: ['c2'],
413 cardsTimestamp: 239,
414 nextPollTime: 10000,
415 rank: 1
416 }
417 };
418
419 this.makeAndRegisterMockGlobals(
420 ['scheduleNextPoll', 'mergeAndShowNotificationCards', 'recordEvent']);
421
422 this.makeAndRegisterMockApis([
423 'chrome.storage.local.set',
424 'instrumented.storage.local.get'
425 ]);
426
427 var storageGetSavedArgs = new SaveMockArguments();
428 this.mockApis.expects(once()).
429 instrumented_storage_local_get(
430 storageGetSavedArgs.match(eq('notificationGroups')),
431 storageGetSavedArgs.match(ANYTHING)).
432 will(invokeCallback(
433 storageGetSavedArgs, 1, {notificationGroups: storedGroups}));
434
435 this.mockGlobals.expects(once()).
436 scheduleNextPoll(eqJSON(expectedUpdatedGroups));
437
438 this.mockApis.expects(once()).
439 chrome_storage_local_set(
440 eqJSON({notificationGroups: expectedUpdatedGroups}));
441
442 this.mockGlobals.expects(once()).
443 mergeAndShowNotificationCards(eqJSON(expectedUpdatedGroups));
444
445 this.mockGlobals.expects(once()).
446 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS);
447
448 // Invoking the tested function.
449 parseAndShowNotificationCards(JSON.stringify(serverResponse));
450 });
451
321 /** 452 /**
322 * Mocks global functions and APIs that initialize() depends upon. 453 * Mocks global functions and APIs that initialize() depends upon.
323 * @param {Test} fixture Test fixture. 454 * @param {Test} fixture Test fixture.
324 */ 455 */
325 function mockInitializeDependencies(fixture) { 456 function mockInitializeDependencies(fixture) {
326 fixture.makeAndRegisterMockGlobals([ 457 fixture.makeAndRegisterMockGlobals([
327 'recordEvent', 458 'recordEvent',
328 'setBackgroundEnable', 459 'setBackgroundEnable',
329 'startPollingCards' 460 'startPollingCards'
330 ]); 461 ]);
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), 866 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})),
736 chromeTabsCreateSavedArgs.match(ANYTHING)). 867 chromeTabsCreateSavedArgs.match(ANYTHING)).
737 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); 868 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab));
738 this.mockApis.expects(once()).chrome_windows_create( 869 this.mockApis.expects(once()).chrome_windows_create(
739 eqJSON({url: testActionUrl, focused: true})); 870 eqJSON({url: testActionUrl, focused: true}));
740 871
741 // Invoking the tested function. 872 // Invoking the tested function.
742 onNotificationClicked( 873 onNotificationClicked(
743 testNotificationId, this.mockLocalFunctions.functions().selector); 874 testNotificationId, this.mockLocalFunctions.functions().selector);
744 }); 875 });
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698