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 testTriggerUnmerged = {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: [ |
| 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 }); |
| 107 |
| 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. |
| 115 |
| 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 }; |
| 127 |
| 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 }; |
| 137 |
| 138 // Invoking the tested function. |
| 139 var resultingMergedCard = mergeCards( |
| 140 mergedCard, unmergedNotification, testTimestamp1, testGroupRank1); |
| 141 |
| 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 }); |
| 161 |
| 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. |
| 169 |
| 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 }; |
| 181 |
| 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 }; |
| 191 |
| 192 // Invoking the tested function. |
| 193 var resultingMergedCard = mergeCards( |
| 194 mergedCard, unmergedNotification, testTimestamp1, testGroupRank2); |
| 195 |
| 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 }); |
| 215 |
| 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. |
| 223 |
| 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 }; |
| 235 |
| 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 }; |
| 245 |
| 246 // Invoking the tested function. |
| 247 var resultingMergedCard = mergeCards( |
| 248 mergedCard, unmergedNotification, testTimestamp2, testGroupRank1); |
| 249 |
| 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 }); |
| 269 |
| 270 TEST_F( |
| 271 'GoogleNowBackgroundUnitTest', |
| 272 'MergeGroup', |
| 273 function() { |
| 274 // Tests mergeGroup method. |
| 275 |
| 276 // Setup. |
| 277 this.makeAndRegisterMockGlobals(['mergeCards']); |
| 278 |
| 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'})); |
| 293 |
| 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 }; |
| 303 |
| 304 var mergedCards = { |
| 305 'ID 2': { testField: 'TEST_FIELD22' }, |
| 306 'ID 3': { testField: 'TEST_FIELD3' } |
| 307 }; |
| 308 |
| 309 // Invoking the tested function. |
| 310 mergeGroup(mergedCards, group); |
| 311 |
| 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 }); |
| 320 |
52 /** | 321 /** |
53 * Mocks global functions and APIs that initialize() depends upon. | 322 * Mocks global functions and APIs that initialize() depends upon. |
54 * @param {Test} fixture Test fixture. | 323 * @param {Test} fixture Test fixture. |
55 */ | 324 */ |
56 function mockInitializeDependencies(fixture) { | 325 function mockInitializeDependencies(fixture) { |
57 fixture.makeAndRegisterMockGlobals([ | 326 fixture.makeAndRegisterMockGlobals([ |
58 'recordEvent', | 327 'recordEvent', |
59 'setBackgroundEnable', | 328 'setBackgroundEnable', |
60 'showWelcomeToast', | 329 'showWelcomeToast', |
61 'startPollingCards' | 330 'startPollingCards' |
62 ]); | 331 ]); |
63 fixture.makeAndRegisterMockApis([ | 332 fixture.makeAndRegisterMockApis([ |
64 'authenticationManager.isSignedIn', | 333 'authenticationManager.isSignedIn', |
65 'chrome.location.clearWatch', | 334 'chrome.location.clearWatch', |
66 'chrome.storage.local.set', | 335 'chrome.storage.local.remove', |
67 'instrumented.metricsPrivate.getVariationParams', | 336 'instrumented.metricsPrivate.getVariationParams', |
68 'instrumented.notifications.getAll', | 337 'instrumented.notifications.getAll', |
69 'instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get', | 338 'instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get', |
70 'instrumented.storage.local.get', | 339 'instrumented.storage.local.get', |
71 'tasks.add', | 340 'tasks.add', |
72 'updateCardsAttempts.isRunning', | 341 'updateCardsAttempts.isRunning', |
73 'updateCardsAttempts.stop' | 342 'updateCardsAttempts.stop' |
74 ]); | 343 ]); |
75 } | 344 } |
76 | 345 |
(...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 | 410 * 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. | 411 * outside of this context with the same argument matchers. |
143 * expects() calls cannot be chained with the same argument matchers. | 412 * expects() calls cannot be chained with the same argument matchers. |
144 */ | 413 */ |
145 function expectInitialization(mockApisObj) { | 414 function expectInitialization(mockApisObj) { |
146 mockApisObj.expects(once()). | 415 mockApisObj.expects(once()). |
147 chrome_location_clearWatch(ANYTHING); | 416 chrome_location_clearWatch(ANYTHING); |
148 mockApisObj.expects(once()). | 417 mockApisObj.expects(once()). |
149 updateCardsAttempts_stop(); | 418 updateCardsAttempts_stop(); |
150 mockApisObj.expects(once()). | 419 mockApisObj.expects(once()). |
151 chrome_storage_local_set(eqJSON({notificationsData: {}})); | 420 chrome_storage_local_remove( |
| 421 eqJSON(['notificationsData', 'notificationGroups'])); |
152 var tasksAddSavedArgs = new SaveMockArguments(); | 422 var tasksAddSavedArgs = new SaveMockArguments(); |
153 mockApisObj.expects(once()). | 423 mockApisObj.expects(once()). |
154 tasks_add( | 424 tasks_add( |
155 tasksAddSavedArgs.match(ANYTHING), | 425 tasksAddSavedArgs.match(ANYTHING), |
156 tasksAddSavedArgs.match(ANYTHING)). | 426 tasksAddSavedArgs.match(ANYTHING)). |
157 will(invokeCallback(tasksAddSavedArgs, 1, function() {})); | 427 will(invokeCallback(tasksAddSavedArgs, 1, function() {})); |
158 var updateCardsAttemptsIsRunningSavedArgs = new SaveMockArguments(); | 428 var updateCardsAttemptsIsRunningSavedArgs = new SaveMockArguments(); |
159 mockApisObj.expects(once()). | 429 mockApisObj.expects(once()). |
160 updateCardsAttempts_isRunning( | 430 updateCardsAttempts_isRunning( |
161 updateCardsAttemptsIsRunningSavedArgs.match(ANYTHING)). | 431 updateCardsAttemptsIsRunningSavedArgs.match(ANYTHING)). |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), | 789 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), |
520 chromeTabsCreateSavedArgs.match(ANYTHING)). | 790 chromeTabsCreateSavedArgs.match(ANYTHING)). |
521 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); | 791 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); |
522 this.mockApis.expects(once()).chrome_windows_create( | 792 this.mockApis.expects(once()).chrome_windows_create( |
523 eqJSON({url: testActionUrl, focused: true})); | 793 eqJSON({url: testActionUrl, focused: true})); |
524 | 794 |
525 // Invoking the tested function. | 795 // Invoking the tested function. |
526 onNotificationClicked( | 796 onNotificationClicked( |
527 testNotificationId, this.mockLocalFunctions.functions().selector); | 797 testNotificationId, this.mockLocalFunctions.functions().selector); |
528 }); | 798 }); |
OLD | NEW |