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 22 matching lines...) Expand all Loading... |
33 | 33 |
34 testTaskPair(DISMISS_CARD_TASK_NAME, UPDATE_CARDS_TASK_NAME, false); | 34 testTaskPair(DISMISS_CARD_TASK_NAME, UPDATE_CARDS_TASK_NAME, false); |
35 testTaskPair(DISMISS_CARD_TASK_NAME, DISMISS_CARD_TASK_NAME, false); | 35 testTaskPair(DISMISS_CARD_TASK_NAME, DISMISS_CARD_TASK_NAME, false); |
36 testTaskPair(DISMISS_CARD_TASK_NAME, RETRY_DISMISS_TASK_NAME, false); | 36 testTaskPair(DISMISS_CARD_TASK_NAME, RETRY_DISMISS_TASK_NAME, false); |
37 | 37 |
38 testTaskPair(RETRY_DISMISS_TASK_NAME, UPDATE_CARDS_TASK_NAME, true); | 38 testTaskPair(RETRY_DISMISS_TASK_NAME, UPDATE_CARDS_TASK_NAME, true); |
39 testTaskPair(RETRY_DISMISS_TASK_NAME, DISMISS_CARD_TASK_NAME, true); | 39 testTaskPair(RETRY_DISMISS_TASK_NAME, DISMISS_CARD_TASK_NAME, true); |
40 testTaskPair(RETRY_DISMISS_TASK_NAME, RETRY_DISMISS_TASK_NAME, true); | 40 testTaskPair(RETRY_DISMISS_TASK_NAME, RETRY_DISMISS_TASK_NAME, true); |
41 }); | 41 }); |
42 | 42 |
| 43 /** |
| 44 * Mocks global functions and APIs that initialize() depends upon. |
| 45 * @param {Test} fixture Test fixture. |
| 46 */ |
| 47 function mockInitializeDependencies(fixture) { |
| 48 fixture.makeAndRegisterMockGlobals([ |
| 49 'recordEvent', |
| 50 'showWelcomeToast', |
| 51 'startPollingCards']); |
| 52 fixture.makeAndRegisterMockApis( |
| 53 ['storage.get', 'chrome.identity.getAuthToken']); |
| 54 } |
| 55 |
| 56 TEST_F( |
| 57 'GoogleNowBackgroundUnitTest', |
| 58 'Initialize_ToastStateEmpty1', |
| 59 function() { |
| 60 // Tests the case when toast state is empty and NOTIFICATION_CARDS_URL is |
| 61 // not set. In this case, the function should quietly exit after finding |
| 62 // out that NOTIFICATION_CARDS_URL is empty. |
| 63 |
| 64 // Setup and expectations. |
| 65 var testToastState = {}; |
| 66 NOTIFICATION_CARDS_URL = undefined; |
| 67 |
| 68 mockInitializeDependencies(this); |
| 69 |
| 70 this.mockGlobals.expects(once()).recordEvent( |
| 71 DiagnosticEvent.EXTENSION_START); |
| 72 var storageGetSavedArgs = new SaveMockArguments(); |
| 73 this.mockApis.expects(once()). |
| 74 storage_get( |
| 75 storageGetSavedArgs.match(eq('toastState')), |
| 76 storageGetSavedArgs.match(ANYTHING)). |
| 77 will(invokeCallback(storageGetSavedArgs, 1, testToastState)); |
| 78 |
| 79 // Invoking the tested function. |
| 80 initialize(); |
| 81 }); |
| 82 |
| 83 TEST_F( |
| 84 'GoogleNowBackgroundUnitTest', |
| 85 'Initialize_ToastStateEmpty2', |
| 86 function() { |
| 87 // Tests the case when toast state is empty and NOTIFICATION_CARDS_URL is |
| 88 // set, but getAuthToken fails most likely because the user is not signed |
| 89 // in. In this case, the function should quietly exit after finding out |
| 90 // that getAuthToken fails. |
| 91 |
| 92 // Setup and expectations. |
| 93 var testToastState = {}; |
| 94 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; |
| 95 var testIdentityToken = undefined; |
| 96 |
| 97 mockInitializeDependencies(this); |
| 98 |
| 99 this.mockGlobals.expects(once()).recordEvent( |
| 100 DiagnosticEvent.EXTENSION_START); |
| 101 var storageGetSavedArgs = new SaveMockArguments(); |
| 102 this.mockApis.expects(once()). |
| 103 storage_get( |
| 104 storageGetSavedArgs.match(eq('toastState')), |
| 105 storageGetSavedArgs.match(ANYTHING)). |
| 106 will(invokeCallback(storageGetSavedArgs, 1, testToastState)); |
| 107 var chromeIdentityGetAuthTokenSavedArgs = new SaveMockArguments(); |
| 108 this.mockApis.expects(once()). |
| 109 chrome_identity_getAuthToken( |
| 110 chromeIdentityGetAuthTokenSavedArgs.match( |
| 111 eqJSON({interactive: false})), |
| 112 chromeIdentityGetAuthTokenSavedArgs.match(ANYTHING)). |
| 113 will(invokeCallback( |
| 114 chromeIdentityGetAuthTokenSavedArgs, 1, testIdentityToken)); |
| 115 |
| 116 // Invoking the tested function. |
| 117 initialize(); |
| 118 }); |
| 119 |
| 120 TEST_F( |
| 121 'GoogleNowBackgroundUnitTest', |
| 122 'Initialize_ToastStateEmpty3', |
| 123 function() { |
| 124 // Tests the case when toast state is empty and NOTIFICATION_CARDS_URL is |
| 125 // set, and getAuthToken succeeds. In this case, the function should |
| 126 // invoke showWelcomeToast(). |
| 127 |
| 128 // Setup and expectations. |
| 129 var testToastState = {}; |
| 130 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; |
| 131 var testIdentityToken = 'some identity token'; |
| 132 |
| 133 mockInitializeDependencies(this); |
| 134 |
| 135 this.mockGlobals.expects(once()).recordEvent( |
| 136 DiagnosticEvent.EXTENSION_START); |
| 137 var storageGetSavedArgs = new SaveMockArguments(); |
| 138 this.mockApis.expects(once()). |
| 139 storage_get( |
| 140 storageGetSavedArgs.match(eq('toastState')), |
| 141 storageGetSavedArgs.match(ANYTHING)). |
| 142 will(invokeCallback(storageGetSavedArgs, 1, testToastState)); |
| 143 var chromeIdentityGetAuthTokenSavedArgs = new SaveMockArguments(); |
| 144 this.mockApis.expects(once()). |
| 145 chrome_identity_getAuthToken( |
| 146 chromeIdentityGetAuthTokenSavedArgs.match( |
| 147 eqJSON({interactive: false})), |
| 148 chromeIdentityGetAuthTokenSavedArgs.match(ANYTHING)). |
| 149 will(invokeCallback( |
| 150 chromeIdentityGetAuthTokenSavedArgs, 1, testIdentityToken)); |
| 151 this.mockGlobals.expects(once()).showWelcomeToast(); |
| 152 |
| 153 // Invoking the tested function. |
| 154 initialize(); |
| 155 }); |
| 156 |
| 157 TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_ToastStateYes', function() { |
| 158 // Tests the case when the user has answered "yes" to the toast in the past. |
| 159 // In this case, the function should invoke startPollingCards(). |
| 160 |
| 161 // Setup and expectations. |
| 162 var testToastState = {toastState: ToastOptionResponse.CHOSE_YES}; |
| 163 |
| 164 mockInitializeDependencies(this); |
| 165 |
| 166 this.mockGlobals.expects(once()).recordEvent(DiagnosticEvent.EXTENSION_START); |
| 167 var storageGetSavedArgs = new SaveMockArguments(); |
| 168 this.mockApis.expects(once()). |
| 169 storage_get( |
| 170 storageGetSavedArgs.match(eq('toastState')), |
| 171 storageGetSavedArgs.match(ANYTHING)). |
| 172 will(invokeCallback(storageGetSavedArgs, 1, testToastState)); |
| 173 this.mockGlobals.expects(once()).startPollingCards(); |
| 174 |
| 175 // Invoking the tested function. |
| 176 initialize(); |
| 177 }); |
| 178 |
| 179 TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_ToastStateNo', function() { |
| 180 // Tests the case when the user has answered "no" to the toast in the past. |
| 181 // In this case, the function should do nothing. |
| 182 |
| 183 // Setup and expectations. |
| 184 var testToastState = {toastState: ToastOptionResponse.CHOSE_NO}; |
| 185 |
| 186 mockInitializeDependencies(this); |
| 187 |
| 188 this.mockGlobals.expects(once()).recordEvent(DiagnosticEvent.EXTENSION_START); |
| 189 var storageGetSavedArgs = new SaveMockArguments(); |
| 190 this.mockApis.expects(once()). |
| 191 storage_get( |
| 192 storageGetSavedArgs.match(eq('toastState')), |
| 193 storageGetSavedArgs.match(ANYTHING)). |
| 194 will(invokeCallback(storageGetSavedArgs, 1, testToastState)); |
| 195 |
| 196 // Invoking the tested function. |
| 197 initialize(); |
| 198 }); |
| 199 |
| 200 /** |
| 201 * Mocks global functions and APIs that onNotificationClicked() depends upon. |
| 202 * @param {Test} fixture Test fixture. |
| 203 */ |
| 204 function mockOnNotificationClickedDependencies(fixture) { |
| 205 fixture.makeAndRegisterMockApis([ |
| 206 'storage.get', |
| 207 'chrome.tabs.create', |
| 208 'chrome.windows.create']); |
| 209 } |
| 210 |
| 211 TEST_F( |
| 212 'GoogleNowBackgroundUnitTest', |
| 213 'OnNotificationClicked_NoData', |
| 214 function() { |
| 215 // Tests the case when there is no data associated with notification id. |
| 216 // In this case, the function should do nothing. |
| 217 |
| 218 // Setup and expectations. |
| 219 var testNotificationId = 'TEST_ID'; |
| 220 var testNotificationData = {}; |
| 221 |
| 222 mockOnNotificationClickedDependencies(this); |
| 223 this.makeMockLocalFunctions(['selector']); |
| 224 |
| 225 var storageGetSavedArgs = new SaveMockArguments(); |
| 226 this.mockApis.expects(once()). |
| 227 storage_get( |
| 228 storageGetSavedArgs.match(eq('notificationsData')), |
| 229 storageGetSavedArgs.match(ANYTHING)). |
| 230 will(invokeCallback(storageGetSavedArgs, 1, testNotificationData)); |
| 231 |
| 232 // Invoking the tested function. |
| 233 onNotificationClicked( |
| 234 testNotificationId, this.mockLocalFunctions.functions().selector); |
| 235 }); |
| 236 |
| 237 TEST_F( |
| 238 'GoogleNowBackgroundUnitTest', |
| 239 'OnNotificationClicked_ActionUrlsNotObject', |
| 240 function() { |
| 241 // Tests the case when the data associated with notification id is not an |
| 242 // object, probably because of a malformed server response. |
| 243 // In this case, the function should do nothing. |
| 244 |
| 245 // Setup and expectations. |
| 246 var testActionUrls = 'string, not object'; |
| 247 var testNotificationId = 'TEST_ID'; |
| 248 var testNotificationData = { |
| 249 notificationsData: {'TEST_ID': {actionUrls: testActionUrls}} |
| 250 }; |
| 251 |
| 252 mockOnNotificationClickedDependencies(this); |
| 253 this.makeMockLocalFunctions(['selector']); |
| 254 |
| 255 var storageGetSavedArgs = new SaveMockArguments(); |
| 256 this.mockApis.expects(once()). |
| 257 storage_get( |
| 258 storageGetSavedArgs.match(eq('notificationsData')), |
| 259 storageGetSavedArgs.match(ANYTHING)). |
| 260 will(invokeCallback(storageGetSavedArgs, 1, testNotificationData)); |
| 261 |
| 262 // Invoking the tested function. |
| 263 onNotificationClicked( |
| 264 testNotificationId, this.mockLocalFunctions.functions().selector); |
| 265 }); |
| 266 |
| 267 TEST_F( |
| 268 'GoogleNowBackgroundUnitTest', |
| 269 'OnNotificationClicked_UrlNotString', |
| 270 function() { |
| 271 // Tests the case when selector returns a non-string, probably because of |
| 272 // a malformed server response. |
| 273 // In this case, the function should do nothing. |
| 274 |
| 275 // Setup and expectations. |
| 276 var testActionUrls = {testField: 'TEST VALUE'}; |
| 277 var testNotificationId = 'TEST_ID'; |
| 278 var testNotificationData = { |
| 279 notificationsData: {'TEST_ID': {actionUrls: testActionUrls}} |
| 280 }; |
| 281 var testActionUrl = {}; |
| 282 |
| 283 mockOnNotificationClickedDependencies(this); |
| 284 this.makeMockLocalFunctions(['selector']); |
| 285 |
| 286 var storageGetSavedArgs = new SaveMockArguments(); |
| 287 this.mockApis.expects(once()). |
| 288 storage_get( |
| 289 storageGetSavedArgs.match(eq('notificationsData')), |
| 290 storageGetSavedArgs.match(ANYTHING)). |
| 291 will(invokeCallback(storageGetSavedArgs, 1, testNotificationData)); |
| 292 this.mockLocalFunctions.expects(once()).selector(testActionUrls).will( |
| 293 returnValue(testActionUrl)); |
| 294 |
| 295 // Invoking the tested function. |
| 296 onNotificationClicked( |
| 297 testNotificationId, this.mockLocalFunctions.functions().selector); |
| 298 }); |
| 299 |
| 300 TEST_F( |
| 301 'GoogleNowBackgroundUnitTest', |
| 302 'OnNotificationClicked_TabCreateSuccess', |
| 303 function() { |
| 304 // Tests the selected URL is OK and crome.tabs.create suceeds. |
| 305 |
| 306 // Setup and expectations. |
| 307 var testActionUrls = {testField: 'TEST VALUE'}; |
| 308 var testNotificationId = 'TEST_ID'; |
| 309 var testNotificationData = { |
| 310 notificationsData: {'TEST_ID': {actionUrls: testActionUrls}} |
| 311 }; |
| 312 var testActionUrl = 'http://testurl.com'; |
| 313 var testCreatedTab = {}; |
| 314 |
| 315 mockOnNotificationClickedDependencies(this); |
| 316 this.makeMockLocalFunctions(['selector']); |
| 317 |
| 318 var storageGetSavedArgs = new SaveMockArguments(); |
| 319 this.mockApis.expects(once()). |
| 320 storage_get( |
| 321 storageGetSavedArgs.match(eq('notificationsData')), |
| 322 storageGetSavedArgs.match(ANYTHING)). |
| 323 will(invokeCallback(storageGetSavedArgs, 1, testNotificationData)); |
| 324 this.mockLocalFunctions.expects(once()).selector(testActionUrls).will( |
| 325 returnValue(testActionUrl)); |
| 326 var chromeTabsCreateSavedArgs = new SaveMockArguments(); |
| 327 this.mockApis.expects(once()). |
| 328 chrome_tabs_create( |
| 329 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), |
| 330 chromeTabsCreateSavedArgs.match(ANYTHING)). |
| 331 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); |
| 332 |
| 333 // Invoking the tested function. |
| 334 onNotificationClicked( |
| 335 testNotificationId, this.mockLocalFunctions.functions().selector); |
| 336 }); |
| 337 |
| 338 TEST_F( |
| 339 'GoogleNowBackgroundUnitTest', |
| 340 'OnNotificationClicked_TabCreateFail', |
| 341 function() { |
| 342 // Tests the selected URL is OK and crome.tabs.create fails. |
| 343 // In this case, the function should invoke chrome.windows.create as a |
| 344 // second attempt. |
| 345 |
| 346 // Setup and expectations. |
| 347 var testActionUrls = {testField: 'TEST VALUE'}; |
| 348 var testNotificationId = 'TEST_ID'; |
| 349 var testNotificationData = { |
| 350 notificationsData: {'TEST_ID': {actionUrls: testActionUrls}} |
| 351 }; |
| 352 var testActionUrl = 'http://testurl.com'; |
| 353 var testCreatedTab = undefined; // chrome.tabs.create fails |
| 354 |
| 355 mockOnNotificationClickedDependencies(this); |
| 356 this.makeMockLocalFunctions(['selector']); |
| 357 |
| 358 var storageGetSavedArgs = new SaveMockArguments(); |
| 359 this.mockApis.expects(once()). |
| 360 storage_get( |
| 361 storageGetSavedArgs.match(eq('notificationsData')), |
| 362 storageGetSavedArgs.match(ANYTHING)). |
| 363 will(invokeCallback(storageGetSavedArgs, 1, testNotificationData)); |
| 364 this.mockLocalFunctions.expects(once()).selector(testActionUrls).will( |
| 365 returnValue(testActionUrl)); |
| 366 var chromeTabsCreateSavedArgs = new SaveMockArguments(); |
| 367 this.mockApis.expects(once()). |
| 368 chrome_tabs_create( |
| 369 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), |
| 370 chromeTabsCreateSavedArgs.match(ANYTHING)). |
| 371 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); |
| 372 this.mockApis.expects(once()).chrome_windows_create( |
| 373 eqJSON({url: testActionUrl})); |
| 374 |
| 375 // Invoking the tested function. |
| 376 onNotificationClicked( |
| 377 testNotificationId, this.mockLocalFunctions.functions().selector); |
| 378 }); |
OLD | NEW |