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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 testTaskPair(RETRY_DISMISS_TASK_NAME, DISMISS_CARD_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); | 43 testTaskPair(RETRY_DISMISS_TASK_NAME, RETRY_DISMISS_TASK_NAME, true); |
| 44 testTaskPair(RETRY_DISMISS_TASK_NAME, STATE_CHANGED_TASK_NAME, false); | 44 testTaskPair(RETRY_DISMISS_TASK_NAME, STATE_CHANGED_TASK_NAME, false); |
| 45 | 45 |
| 46 testTaskPair(STATE_CHANGED_TASK_NAME, UPDATE_CARDS_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); | 47 testTaskPair(STATE_CHANGED_TASK_NAME, DISMISS_CARD_TASK_NAME, false); |
| 48 testTaskPair(STATE_CHANGED_TASK_NAME, RETRY_DISMISS_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); | 49 testTaskPair(STATE_CHANGED_TASK_NAME, STATE_CHANGED_TASK_NAME, false); |
| 50 }); | 50 }); |
| 51 | 51 |
| 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 testTriggerWire = {showTimeSec: 100, hideTimeSec: 200}; | |
| 68 var testTriggerMerged = {showTime: 300007, hideTime: 300011}; | |
| 69 var testVersion1 = 29; | |
| 70 var testVersion2 = 31; | |
| 71 | |
| 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. | |
| 78 | |
| 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); | |
| 88 | |
| 89 // Check the return value. | |
| 90 assertEquals( | |
| 91 JSON.stringify({ | |
| 92 dismissals: [{notificationId: testNotificationId, parameters: testDi smissal1}], | |
|
robliao
2013/09/27 19:41:55
Add Linebreak
vadimt
2013/09/27 21:06:00
Done.
| |
| 93 groupRank: testGroupRank1, | |
| 94 trigger: {}, | |
| 95 timestamp: testTimestamp1, | |
| 96 notification: testNotification1, | |
| 97 actionUrls: testActionUrls1, | |
| 98 version: testVersion1 | |
| 99 }), | |
| 100 JSON.stringify(resultingMergedCard)); | |
| 101 }); | |
| 102 | |
| 103 TEST_F( | |
| 104 'GoogleNowBackgroundUnitTest', | |
| 105 'MergeCards1', | |
| 106 function() { | |
| 107 // Tests merging a wire card into a merged card. | |
| 108 // Wire card priority is greater than merged card one. Wire card rank is | |
|
robliao
2013/09/27 19:41:55
Do we have a case where wire card priority == merg
vadimt
2013/09/27 21:06:00
No; I just didn't think it's an interesting case t
robliao
2013/09/27 21:17:22
It may be interesting if the merged card changed i
vadimt
2013/09/30 17:07:04
Well, here we know that priority is not used for d
| |
| 109 // less than merged card one, and it's older. | |
| 110 | |
| 111 // Setup. | |
| 112 var mergedCard = { | |
| 113 trigger: testTriggerMerged, | |
| 114 version: testVersion1, | |
| 115 timestamp: testTimestamp2, | |
| 116 notification: testNotification1, | |
| 117 actionUrls: testActionUrls1, | |
| 118 groupRank: testGroupRank2, | |
| 119 dismissals: | |
| 120 [{notificationId: testNotificationId, parameters: testDismissal1}] | |
| 121 }; | |
| 122 | |
| 123 var unmergedNotification = { | |
| 124 notificationId: testNotificationId, | |
| 125 chromeNotificationId: testChromeNotificationId, | |
| 126 version: testVersion2, | |
| 127 chromeNotificationOptions: testNotification2, | |
| 128 actionUrls: testActionUrls2, | |
| 129 dismissal: testDismissal2, | |
| 130 trigger: testTriggerWire | |
| 131 }; | |
| 132 | |
| 133 // Invoking the tested function. | |
| 134 var resultingMergedCard = mergeCards( | |
| 135 mergedCard, unmergedNotification, testTimestamp1, testGroupRank1); | |
| 136 | |
| 137 // Check the return value. | |
| 138 assertEquals( | |
| 139 JSON.stringify({ | |
| 140 trigger: testTriggerMerged, | |
| 141 version: testVersion1, | |
| 142 timestamp: testTimestamp2, | |
| 143 notification: { | |
| 144 testNotificationField: 'TEST NOTIFICATION VALUE1', | |
| 145 priority: 0 | |
| 146 }, | |
| 147 actionUrls: testActionUrls1, | |
| 148 groupRank: testGroupRank2, | |
| 149 dismissals: [ | |
| 150 {notificationId: testNotificationId, parameters: testDismissal1}, | |
| 151 {notificationId: testNotificationId, parameters: testDismissal2} | |
| 152 ] | |
| 153 }), | |
| 154 JSON.stringify(resultingMergedCard)); | |
| 155 }); | |
| 156 | |
| 157 TEST_F( | |
| 158 'GoogleNowBackgroundUnitTest', | |
| 159 'MergeCards2', | |
| 160 function() { | |
| 161 // Tests merging a wire card into a merged card. | |
| 162 // Wire card priority is less than merged card one. Wire card rank is | |
| 163 // greater than merged card one, and it's older. | |
| 164 | |
| 165 // Setup. | |
| 166 var mergedCard = { | |
| 167 trigger: testTriggerMerged, | |
| 168 version: testVersion1, | |
| 169 timestamp: testTimestamp2, | |
| 170 notification: testNotification2, | |
| 171 actionUrls: testActionUrls1, | |
| 172 groupRank: testGroupRank1, | |
| 173 dismissals: | |
| 174 [{notificationId: testNotificationId, parameters: testDismissal1}] | |
| 175 }; | |
| 176 | |
| 177 var unmergedNotification = { | |
| 178 notificationId: testNotificationId, | |
| 179 chromeNotificationId: testChromeNotificationId, | |
| 180 version: testVersion2, | |
| 181 chromeNotificationOptions: testNotification1, | |
| 182 actionUrls: testActionUrls2, | |
| 183 dismissal: testDismissal2, | |
| 184 trigger: testTriggerWire | |
| 185 }; | |
| 186 | |
| 187 // Invoking the tested function. | |
| 188 var resultingMergedCard = mergeCards( | |
| 189 mergedCard, unmergedNotification, testTimestamp1, testGroupRank2); | |
| 190 | |
| 191 // Check the return value. | |
| 192 assertEquals( | |
| 193 JSON.stringify({ | |
| 194 trigger: {showTime: 400000, hideTime: 500000}, | |
| 195 version: testVersion1, | |
| 196 timestamp: testTimestamp2, | |
| 197 notification: { | |
| 198 testNotificationField: 'TEST NOTIFICATION VALUE2', | |
| 199 priority: 0 | |
| 200 }, | |
| 201 actionUrls: testActionUrls1, | |
| 202 groupRank: testGroupRank2, | |
| 203 dismissals: [ | |
| 204 {notificationId: testNotificationId, parameters: testDismissal1}, | |
| 205 {notificationId: testNotificationId, parameters: testDismissal2} | |
| 206 ] | |
| 207 }), | |
| 208 JSON.stringify(resultingMergedCard)); | |
| 209 }); | |
| 210 | |
| 211 TEST_F( | |
| 212 'GoogleNowBackgroundUnitTest', | |
| 213 'MergeCards3', | |
| 214 function() { | |
| 215 // Tests merging a wire card into a merged card. | |
| 216 // Wire card priority is less than merged card one. Wire card rank is | |
| 217 // less than merged card one, and it's newer. | |
| 218 | |
| 219 // Setup. | |
| 220 var mergedCard = { | |
| 221 trigger: testTriggerMerged, | |
| 222 version: testVersion1, | |
| 223 timestamp: testTimestamp1, | |
| 224 notification: testNotification2, | |
| 225 actionUrls: testActionUrls1, | |
| 226 groupRank: testGroupRank2, | |
| 227 dismissals: | |
| 228 [{notificationId: testNotificationId, parameters: testDismissal1}] | |
| 229 }; | |
| 230 | |
| 231 var unmergedNotification = { | |
| 232 notificationId: testNotificationId, | |
| 233 chromeNotificationId: testChromeNotificationId, | |
| 234 version: testVersion2, | |
| 235 chromeNotificationOptions: testNotification1, | |
| 236 actionUrls: testActionUrls2, | |
| 237 dismissal: testDismissal2, | |
| 238 trigger: testTriggerWire | |
| 239 }; | |
| 240 | |
| 241 // Invoking the tested function. | |
| 242 var resultingMergedCard = mergeCards( | |
| 243 mergedCard, unmergedNotification, testTimestamp2, testGroupRank1); | |
| 244 | |
| 245 // Check the return value. | |
| 246 assertEquals( | |
| 247 JSON.stringify({ | |
| 248 trigger: testTriggerMerged, | |
| 249 version: testVersion2, | |
| 250 timestamp: testTimestamp2, | |
| 251 notification: { | |
| 252 testNotificationField: 'TEST NOTIFICATION VALUE1', | |
| 253 priority: 0 | |
| 254 }, | |
| 255 actionUrls: testActionUrls2, | |
| 256 groupRank: testGroupRank2, | |
| 257 dismissals: [ | |
| 258 {notificationId: testNotificationId, parameters: testDismissal1}, | |
| 259 {notificationId: testNotificationId, parameters: testDismissal2} | |
| 260 ] | |
| 261 }), | |
| 262 JSON.stringify(resultingMergedCard)); | |
| 263 }); | |
| 264 | |
| 265 TEST_F( | |
| 266 'GoogleNowBackgroundUnitTest', | |
| 267 'MergeGroup', | |
| 268 function() { | |
| 269 // Tests mergeGroup method. | |
| 270 | |
| 271 // Setup. | |
| 272 this.makeAndRegisterMockGlobals(['mergeCards']); | |
| 273 | |
| 274 this.mockGlobals.expects(once()). | |
| 275 mergeCards( | |
| 276 undefined, | |
| 277 eqJSON({chromeNotificationId: 'ID 1', testField: 'TEST_FIELD1'}), | |
| 278 300000, | |
| 279 17). | |
| 280 will(returnValue({testField: 'RESULT 1'})); | |
| 281 this.mockGlobals.expects(once()). | |
| 282 mergeCards( | |
| 283 eqJSON({testField: 'TEST_FIELD22'}), | |
| 284 eqJSON({chromeNotificationId: 'ID 2', testField: 'TEST_FIELD2'}), | |
| 285 300000, | |
| 286 17). | |
| 287 will(returnValue({testField: 'RESULT 2'})); | |
| 288 | |
| 289 var group = { | |
| 290 cards: [ | |
| 291 {chromeNotificationId: 'ID 1', testField: 'TEST_FIELD1'}, | |
| 292 {chromeNotificationId: 'ID 2', testField: 'TEST_FIELD2'} | |
| 293 ], | |
| 294 cardsTimestamp: 300000, | |
| 295 nextPollTime: 600000, | |
| 296 rank: 17 | |
| 297 }; | |
| 298 | |
| 299 var mergedCards = { | |
| 300 'ID 2': { testField: 'TEST_FIELD22' }, | |
| 301 'ID 3': { testField: 'TEST_FIELD3' } | |
| 302 }; | |
| 303 | |
| 304 // Invoking the tested function. | |
| 305 mergeGroup(mergedCards, group); | |
| 306 | |
| 307 // Check the output parameter. | |
| 308 assertEquals( | |
| 309 JSON.stringify({ | |
| 310 'ID 2': { testField: 'RESULT 2' }, | |
| 311 'ID 3': { testField: 'TEST_FIELD3'}, | |
| 312 'ID 1': { testField: 'RESULT 1' }}), | |
| 313 JSON.stringify(mergedCards)); | |
| 314 }); | |
| 315 | |
| 52 /** | 316 /** |
| 53 * Mocks global functions and APIs that initialize() depends upon. | 317 * Mocks global functions and APIs that initialize() depends upon. |
| 54 * @param {Test} fixture Test fixture. | 318 * @param {Test} fixture Test fixture. |
| 55 */ | 319 */ |
| 56 function mockInitializeDependencies(fixture) { | 320 function mockInitializeDependencies(fixture) { |
| 57 fixture.makeAndRegisterMockGlobals([ | 321 fixture.makeAndRegisterMockGlobals([ |
| 58 'recordEvent', | 322 'recordEvent', |
| 59 'setBackgroundEnable', | 323 'setBackgroundEnable', |
| 60 'showWelcomeToast', | 324 'showWelcomeToast', |
| 61 'startPollingCards' | 325 'startPollingCards' |
| 62 ]); | 326 ]); |
| 63 fixture.makeAndRegisterMockApis([ | 327 fixture.makeAndRegisterMockApis([ |
| 64 'authenticationManager.isSignedIn', | 328 'authenticationManager.isSignedIn', |
| 65 'chrome.location.clearWatch', | 329 'chrome.location.clearWatch', |
| 66 'chrome.storage.local.set', | 330 'chrome.storage.local.remove', |
| 67 'instrumented.metricsPrivate.getVariationParams', | 331 'instrumented.metricsPrivate.getVariationParams', |
| 68 'instrumented.notifications.getAll', | 332 'instrumented.notifications.getAll', |
| 69 'instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get', | 333 'instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get', |
| 70 'instrumented.storage.local.get', | 334 'instrumented.storage.local.get', |
| 71 'tasks.add', | 335 'tasks.add', |
| 72 'updateCardsAttempts.isRunning', | 336 'updateCardsAttempts.isRunning', |
| 73 'updateCardsAttempts.stop' | 337 'updateCardsAttempts.stop' |
| 74 ]); | 338 ]); |
| 75 } | 339 } |
| 76 | 340 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 * Note that this CAN NOT be used if any of the methods below are called | 405 * Note that this CAN NOT be used if any of the methods below are called |
| 142 * outside of this context with the same argument matchers. | 406 * outside of this context with the same argument matchers. |
| 143 * expects() calls cannot be chained with the same argument matchers. | 407 * expects() calls cannot be chained with the same argument matchers. |
| 144 */ | 408 */ |
| 145 function expectInitialization(mockApisObj) { | 409 function expectInitialization(mockApisObj) { |
| 146 mockApisObj.expects(once()). | 410 mockApisObj.expects(once()). |
| 147 chrome_location_clearWatch(ANYTHING); | 411 chrome_location_clearWatch(ANYTHING); |
| 148 mockApisObj.expects(once()). | 412 mockApisObj.expects(once()). |
| 149 updateCardsAttempts_stop(); | 413 updateCardsAttempts_stop(); |
| 150 mockApisObj.expects(once()). | 414 mockApisObj.expects(once()). |
| 151 chrome_storage_local_set(eqJSON({notificationsData: {}})); | 415 chrome_storage_local_remove( |
| 416 eqJSON(['notificationsData', 'notificationGroups'])); | |
| 152 var tasksAddSavedArgs = new SaveMockArguments(); | 417 var tasksAddSavedArgs = new SaveMockArguments(); |
| 153 mockApisObj.expects(once()). | 418 mockApisObj.expects(once()). |
| 154 tasks_add( | 419 tasks_add( |
| 155 tasksAddSavedArgs.match(ANYTHING), | 420 tasksAddSavedArgs.match(ANYTHING), |
| 156 tasksAddSavedArgs.match(ANYTHING)). | 421 tasksAddSavedArgs.match(ANYTHING)). |
| 157 will(invokeCallback(tasksAddSavedArgs, 1, function() {})); | 422 will(invokeCallback(tasksAddSavedArgs, 1, function() {})); |
| 158 var updateCardsAttemptsIsRunningSavedArgs = new SaveMockArguments(); | 423 var updateCardsAttemptsIsRunningSavedArgs = new SaveMockArguments(); |
| 159 mockApisObj.expects(once()). | 424 mockApisObj.expects(once()). |
| 160 updateCardsAttempts_isRunning( | 425 updateCardsAttempts_isRunning( |
| 161 updateCardsAttemptsIsRunningSavedArgs.match(ANYTHING)). | 426 updateCardsAttemptsIsRunningSavedArgs.match(ANYTHING)). |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 519 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), | 784 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), |
| 520 chromeTabsCreateSavedArgs.match(ANYTHING)). | 785 chromeTabsCreateSavedArgs.match(ANYTHING)). |
| 521 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); | 786 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); |
| 522 this.mockApis.expects(once()).chrome_windows_create( | 787 this.mockApis.expects(once()).chrome_windows_create( |
| 523 eqJSON({url: testActionUrl, focused: true})); | 788 eqJSON({url: testActionUrl, focused: true})); |
| 524 | 789 |
| 525 // Invoking the tested function. | 790 // Invoking the tested function. |
| 526 onNotificationClicked( | 791 onNotificationClicked( |
| 527 testNotificationId, this.mockLocalFunctions.functions().selector); | 792 testNotificationId, this.mockLocalFunctions.functions().selector); |
| 528 }); | 793 }); |
| OLD | NEW |