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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 fixture.makeAndRegisterMockGlobals([ | 56 fixture.makeAndRegisterMockGlobals([ |
57 'recordEvent', | 57 'recordEvent', |
58 'setBackgroundEnable', | 58 'setBackgroundEnable', |
59 'showWelcomeToast', | 59 'showWelcomeToast', |
60 'startPollingCards' | 60 'startPollingCards' |
61 ]); | 61 ]); |
62 fixture.makeAndRegisterMockApis([ | 62 fixture.makeAndRegisterMockApis([ |
63 'authenticationManager.isSignedIn', | 63 'authenticationManager.isSignedIn', |
64 'chrome.location.clearWatch', | 64 'chrome.location.clearWatch', |
65 'chrome.storage.local.set', | 65 'chrome.storage.local.set', |
66 'instrumented.metricsPrivate.getFieldTrial', | 66 'instrumented.metricsPrivate.getVariationParams', |
67 'instrumented.notifications.getAll', | 67 'instrumented.notifications.getAll', |
68 'instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get', | 68 'instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get', |
69 'instrumented.storage.local.get', | 69 'instrumented.storage.local.get', |
70 'tasks.add', | 70 'tasks.add', |
71 'updateCardsAttempts.isRunning', | 71 'updateCardsAttempts.isRunning', |
72 'updateCardsAttempts.stop' | 72 'updateCardsAttempts.stop' |
73 ]); | 73 ]); |
74 } | 74 } |
75 | 75 |
76 /** | 76 /** |
77 * Sets up the test to expect the state machine calls and send | 77 * Sets up the test to expect the state machine calls and send |
78 * the specified state machine state. Currently used to test initialize(). | 78 * the specified state machine state. Currently used to test initialize(). |
79 * Note that this CAN NOT be used if any of the methods below are called | 79 * Note that this CAN NOT be used if any of the methods below are called |
80 * outside of this context with the same argument matchers. | 80 * outside of this context with the same argument matchers. |
81 * expects() calls cannot be chained with the same argument matchers. | 81 * expects() calls cannot be chained with the same argument matchers. |
82 * @param {object} fixture Test fixture. | 82 * @param {object} fixture Test fixture. |
83 * @param {string} testIdentityToken getAuthToken callback token. | 83 * @param {string} testIdentityToken getAuthToken callback token. |
84 * @param {boolean} testGeolocationPref Geolocation Preference callback value. | 84 * @param {boolean} testGeolocationPref Geolocation Preference callback value. |
85 * @param {boolean} testUserRespondedToToast User Response to toast | 85 * @param {boolean} testUserRespondedToToast User Response to toast |
86 & callback value. | 86 * callback value. |
| 87 * @param {object} testExperimentVariationParams Response of |
| 88 * metricsPrivate.getVariationParams |
87 */ | 89 */ |
88 function expectStateMachineCalls( | 90 function expectStateMachineCalls( |
89 fixture, | 91 fixture, |
90 testIdentityToken, | 92 testIdentityToken, |
91 testGeolocationPref, | 93 testGeolocationPref, |
92 testUserRespondedToToast, | 94 testUserRespondedToToast, |
93 testExperimentResponse) { | 95 testExperimentVariationParams) { |
94 var authenticationManagerIsSignedInSavedArgs = new SaveMockArguments(); | 96 var authenticationManagerIsSignedInSavedArgs = new SaveMockArguments(); |
95 fixture.mockApis.expects(once()). | 97 fixture.mockApis.expects(once()). |
96 authenticationManager_isSignedIn( | 98 authenticationManager_isSignedIn( |
97 authenticationManagerIsSignedInSavedArgs.match(ANYTHING)). | 99 authenticationManagerIsSignedInSavedArgs.match(ANYTHING)). |
98 will(invokeCallback( | 100 will(invokeCallback( |
99 authenticationManagerIsSignedInSavedArgs, | 101 authenticationManagerIsSignedInSavedArgs, |
100 0, | 102 0, |
101 testIdentityToken)); | 103 testIdentityToken)); |
102 | 104 |
103 var getFieldTrialSavedArgs = new SaveMockArguments(); | 105 var getVariationParamsSavedArgs = new SaveMockArguments(); |
104 fixture.mockApis.expects(once()). | 106 fixture.mockApis.expects(once()). |
105 instrumented_metricsPrivate_getFieldTrial( | 107 instrumented_metricsPrivate_getVariationParams( |
106 getFieldTrialSavedArgs.match(ANYTHING), | 108 getVariationParamsSavedArgs.match(ANYTHING), |
107 getFieldTrialSavedArgs.match(ANYTHING)). | 109 getVariationParamsSavedArgs.match(ANYTHING)). |
108 will(invokeCallback( | 110 will(invokeCallback( |
109 getFieldTrialSavedArgs, 1, testExperimentResponse)); | 111 getVariationParamsSavedArgs, 1, testExperimentVariationParams)); |
110 | 112 |
111 var googleGeolocationPrefGetSavedArgs = new SaveMockArguments(); | 113 var googleGeolocationPrefGetSavedArgs = new SaveMockArguments(); |
112 fixture.mockApis.expects(once()). | 114 fixture.mockApis.expects(once()). |
113 instrumented_preferencesPrivate_googleGeolocationAccessEnabled_get( | 115 instrumented_preferencesPrivate_googleGeolocationAccessEnabled_get( |
114 googleGeolocationPrefGetSavedArgs.match(eqJSON({})), | 116 googleGeolocationPrefGetSavedArgs.match(eqJSON({})), |
115 googleGeolocationPrefGetSavedArgs.match(ANYTHING)). | 117 googleGeolocationPrefGetSavedArgs.match(ANYTHING)). |
116 will(invokeCallback( | 118 will(invokeCallback( |
117 googleGeolocationPrefGetSavedArgs, 1, {value: testGeolocationPref})); | 119 googleGeolocationPrefGetSavedArgs, 1, {value: testGeolocationPref})); |
118 | 120 |
119 var storageGetSavedArgs = new SaveMockArguments(); | 121 var storageGetSavedArgs = new SaveMockArguments(); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 function() { | 169 function() { |
168 // Tests the case when the user isn't signed in and NOTIFICATION_CARDS_URL | 170 // Tests the case when the user isn't signed in and NOTIFICATION_CARDS_URL |
169 // is not set. Since NOTIFICATION_CARDS_URL is empty, | 171 // is not set. Since NOTIFICATION_CARDS_URL is empty, |
170 // nothing should start. | 172 // nothing should start. |
171 | 173 |
172 // Setup and expectations. | 174 // Setup and expectations. |
173 NOTIFICATION_CARDS_URL = undefined; | 175 NOTIFICATION_CARDS_URL = undefined; |
174 var testIdentityToken = undefined; | 176 var testIdentityToken = undefined; |
175 var testGeolocationPref = false; | 177 var testGeolocationPref = false; |
176 var testUserRespondedToToast = {}; | 178 var testUserRespondedToToast = {}; |
177 var testExperimentResponse = ''; | 179 var testExperimentVariationParams = {}; |
178 | 180 |
179 mockInitializeDependencies(this); | 181 mockInitializeDependencies(this); |
180 | 182 |
181 this.mockGlobals.expects(once()).recordEvent( | 183 this.mockGlobals.expects(once()).recordEvent( |
182 GoogleNowEvent.EXTENSION_START); | 184 GoogleNowEvent.EXTENSION_START); |
183 | 185 |
184 this.mockGlobals.expects(once()).recordEvent( | 186 this.mockGlobals.expects(once()).recordEvent( |
185 GoogleNowEvent.STOPPED); | 187 GoogleNowEvent.STOPPED); |
186 | 188 |
187 expectInitialization(this.mockApis); | 189 expectInitialization(this.mockApis); |
188 | 190 |
189 expectStateMachineCalls( | 191 expectStateMachineCalls( |
190 this, | 192 this, |
191 testIdentityToken, | 193 testIdentityToken, |
192 testGeolocationPref, | 194 testGeolocationPref, |
193 testUserRespondedToToast, | 195 testUserRespondedToToast, |
194 testExperimentResponse); | 196 testExperimentVariationParams); |
195 | 197 |
196 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); | 198 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); |
197 this.mockApis.expects(exactly(2)). | 199 this.mockApis.expects(exactly(2)). |
198 instrumented_notifications_getAll( | 200 instrumented_notifications_getAll( |
199 chromeNotificationGetAllSavedArgs.match(ANYTHING)). | 201 chromeNotificationGetAllSavedArgs.match(ANYTHING)). |
200 will( | 202 will( |
201 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), | 203 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), |
202 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); | 204 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); |
203 | 205 |
204 // TODO(robliao,vadimt): Determine the granularity of testing to perform. | 206 // TODO(robliao,vadimt): Determine the granularity of testing to perform. |
205 | 207 |
206 // Invoking the tested function. | 208 // Invoking the tested function. |
207 initialize(); | 209 initialize(); |
208 }); | 210 }); |
209 | 211 |
210 TEST_F( | 212 TEST_F( |
211 'GoogleNowBackgroundUnitTest', | 213 'GoogleNowBackgroundUnitTest', |
212 'Initialize_ToastStateEmpty2', | 214 'Initialize_ToastStateEmpty2', |
213 function() { | 215 function() { |
214 // Tests the case when NOTIFICATION_CARDS_URL is but getAuthToken fails | 216 // Tests the case when NOTIFICATION_CARDS_URL is but getAuthToken fails |
215 // most likely because the user is not signed in. In this case, the | 217 // most likely because the user is not signed in. In this case, the |
216 // function should quietly exit after finding out that getAuthToken fails. | 218 // function should quietly exit after finding out that getAuthToken fails. |
217 | 219 |
218 // Setup and expectations. | 220 // Setup and expectations. |
219 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; | 221 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; |
220 var testIdentityToken = undefined; | 222 var testIdentityToken = undefined; |
221 var testGeolocationPref = false; | 223 var testGeolocationPref = false; |
222 var testUserRespondedToToast = {}; | 224 var testUserRespondedToToast = {}; |
223 var testExperimentResponse = ''; | 225 var testExperimentVariationParams = {}; |
224 | 226 |
225 mockInitializeDependencies(this); | 227 mockInitializeDependencies(this); |
226 | 228 |
227 this.mockGlobals.expects(once()).recordEvent( | 229 this.mockGlobals.expects(once()).recordEvent( |
228 GoogleNowEvent.EXTENSION_START); | 230 GoogleNowEvent.EXTENSION_START); |
229 | 231 |
230 this.mockGlobals.expects(once()).recordEvent( | 232 this.mockGlobals.expects(once()).recordEvent( |
231 GoogleNowEvent.STOPPED); | 233 GoogleNowEvent.STOPPED); |
232 | 234 |
233 expectInitialization(this.mockApis); | 235 expectInitialization(this.mockApis); |
234 | 236 |
235 expectStateMachineCalls( | 237 expectStateMachineCalls( |
236 this, | 238 this, |
237 testIdentityToken, | 239 testIdentityToken, |
238 testGeolocationPref, | 240 testGeolocationPref, |
239 testUserRespondedToToast, | 241 testUserRespondedToToast, |
240 testExperimentResponse); | 242 testExperimentVariationParams); |
241 | 243 |
242 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); | 244 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); |
243 this.mockApis.expects(exactly(2)). | 245 this.mockApis.expects(exactly(2)). |
244 instrumented_notifications_getAll( | 246 instrumented_notifications_getAll( |
245 chromeNotificationGetAllSavedArgs.match(ANYTHING)). | 247 chromeNotificationGetAllSavedArgs.match(ANYTHING)). |
246 will( | 248 will( |
247 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), | 249 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), |
248 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); | 250 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); |
249 | 251 |
250 // Invoking the tested function. | 252 // Invoking the tested function. |
251 initialize(); | 253 initialize(); |
252 }); | 254 }); |
253 | 255 |
254 TEST_F( | 256 TEST_F( |
255 'GoogleNowBackgroundUnitTest', | 257 'GoogleNowBackgroundUnitTest', |
256 'Initialize_ToastStateEmpty3', | 258 'Initialize_ToastStateEmpty3', |
257 function() { | 259 function() { |
258 // Tests the case when NOTIFICATION_CARDS_URL is set, getAuthToken | 260 // Tests the case when NOTIFICATION_CARDS_URL is set, getAuthToken |
259 // succeeds, and the user has never responded to the toast. | 261 // succeeds, and the user has never responded to the toast. |
260 // In this case, the function should invoke showWelcomeToast(). | 262 // In this case, the function should invoke showWelcomeToast(). |
261 | 263 |
262 // Setup and expectations. | 264 // Setup and expectations. |
263 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; | 265 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; |
264 var testIdentityToken = 'some identity token'; | 266 var testIdentityToken = 'some identity token'; |
265 var testGeolocationPref = false; | 267 var testGeolocationPref = false; |
266 var testUserRespondedToToast = {}; | 268 var testUserRespondedToToast = {}; |
267 var testExperimentResponse = ''; | 269 var testExperimentVariationParams = {}; |
268 | 270 |
269 mockInitializeDependencies(this); | 271 mockInitializeDependencies(this); |
270 | 272 |
271 this.mockGlobals.expects(once()).recordEvent( | 273 this.mockGlobals.expects(once()).recordEvent( |
272 GoogleNowEvent.EXTENSION_START); | 274 GoogleNowEvent.EXTENSION_START); |
273 | 275 |
274 expectInitialization(this.mockApis); | 276 expectInitialization(this.mockApis); |
275 | 277 |
276 expectStateMachineCalls( | 278 expectStateMachineCalls( |
277 this, | 279 this, |
278 testIdentityToken, | 280 testIdentityToken, |
279 testGeolocationPref, | 281 testGeolocationPref, |
280 testUserRespondedToToast, | 282 testUserRespondedToToast, |
281 testExperimentResponse); | 283 testExperimentVariationParams); |
282 | 284 |
283 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); | 285 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); |
284 this.mockApis.expects(exactly(2)). | 286 this.mockApis.expects(exactly(2)). |
285 instrumented_notifications_getAll( | 287 instrumented_notifications_getAll( |
286 chromeNotificationGetAllSavedArgs.match(ANYTHING)). | 288 chromeNotificationGetAllSavedArgs.match(ANYTHING)). |
287 will( | 289 will( |
288 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), | 290 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), |
289 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); | 291 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); |
290 | 292 |
291 this.mockGlobals.expects(once()).showWelcomeToast(); | 293 this.mockGlobals.expects(once()).showWelcomeToast(); |
292 | 294 |
293 // Invoking the tested function. | 295 // Invoking the tested function. |
294 initialize(); | 296 initialize(); |
295 }); | 297 }); |
296 | 298 |
297 TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_RunGoogleNow', function() { | 299 TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_RunGoogleNow', function() { |
298 // Tests if Google Now will invoke startPollingCards when all | 300 // Tests if Google Now will invoke startPollingCards when all |
299 // of the required state is fulfilled. | 301 // of the required state is fulfilled. |
300 | 302 |
301 // Setup and expectations. | 303 // Setup and expectations. |
302 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; | 304 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; |
303 var testIdentityToken = 'some identity token'; | 305 var testIdentityToken = 'some identity token'; |
304 var testGeolocationPref = true; | 306 var testGeolocationPref = true; |
305 var testUserRespondedToToast = {userRespondedToToast: true}; | 307 var testUserRespondedToToast = {userRespondedToToast: true}; |
306 var testExperimentResponse = ''; | 308 var testExperimentVariationParams = {}; |
307 | 309 |
308 mockInitializeDependencies(this); | 310 mockInitializeDependencies(this); |
309 | 311 |
310 this.mockGlobals.expects(once()).recordEvent( | 312 this.mockGlobals.expects(once()).recordEvent( |
311 GoogleNowEvent.EXTENSION_START); | 313 GoogleNowEvent.EXTENSION_START); |
312 | 314 |
313 expectInitialization(this.mockApis); | 315 expectInitialization(this.mockApis); |
314 | 316 |
315 expectStateMachineCalls( | 317 expectStateMachineCalls( |
316 this, | 318 this, |
317 testIdentityToken, | 319 testIdentityToken, |
318 testGeolocationPref, | 320 testGeolocationPref, |
319 testUserRespondedToToast, | 321 testUserRespondedToToast, |
320 testExperimentResponse); | 322 testExperimentVariationParams); |
321 | 323 |
322 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); | 324 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); |
323 this.mockApis.expects(exactly(2)). | 325 this.mockApis.expects(exactly(2)). |
324 instrumented_notifications_getAll( | 326 instrumented_notifications_getAll( |
325 chromeNotificationGetAllSavedArgs.match(ANYTHING)). | 327 chromeNotificationGetAllSavedArgs.match(ANYTHING)). |
326 will( | 328 will( |
327 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), | 329 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), |
328 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); | 330 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); |
329 | 331 |
330 this.mockGlobals.expects(once()).startPollingCards(); | 332 this.mockGlobals.expects(once()).startPollingCards(); |
331 | 333 |
332 // Invoking the tested function. | 334 // Invoking the tested function. |
333 initialize(); | 335 initialize(); |
334 }); | 336 }); |
335 | 337 |
336 TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_NoGeolocation', function() { | 338 TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_NoGeolocation', function() { |
337 // Tests the case where everything is in place except for the | 339 // Tests the case where everything is in place except for the |
338 // Geolocation Preference after the user responded to the toast. | 340 // Geolocation Preference after the user responded to the toast. |
339 | 341 |
340 // Setup and expectations. | 342 // Setup and expectations. |
341 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; | 343 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; |
342 var testIdentityToken = 'some identity token'; | 344 var testIdentityToken = 'some identity token'; |
343 var testGeolocationPref = false; | 345 var testGeolocationPref = false; |
344 var testUserRespondedToToast = {userRespondedToToast: true}; | 346 var testUserRespondedToToast = {userRespondedToToast: true}; |
345 var testExperimentResponse = ''; | 347 var testExperimentVariationParams = {}; |
346 | 348 |
347 mockInitializeDependencies(this); | 349 mockInitializeDependencies(this); |
348 | 350 |
349 this.mockGlobals.expects(once()).recordEvent( | 351 this.mockGlobals.expects(once()).recordEvent( |
350 GoogleNowEvent.EXTENSION_START); | 352 GoogleNowEvent.EXTENSION_START); |
351 | 353 |
352 this.mockGlobals.expects(once()).recordEvent( | 354 this.mockGlobals.expects(once()).recordEvent( |
353 GoogleNowEvent.USER_SUPPRESSED); | 355 GoogleNowEvent.USER_SUPPRESSED); |
354 | 356 |
355 expectInitialization(this.mockApis); | 357 expectInitialization(this.mockApis); |
356 | 358 |
357 expectStateMachineCalls( | 359 expectStateMachineCalls( |
358 this, | 360 this, |
359 testIdentityToken, | 361 testIdentityToken, |
360 testGeolocationPref, | 362 testGeolocationPref, |
361 testUserRespondedToToast, | 363 testUserRespondedToToast, |
362 testExperimentResponse); | 364 testExperimentVariationParams); |
363 | 365 |
364 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); | 366 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); |
365 this.mockApis.expects(exactly(2)). | 367 this.mockApis.expects(exactly(2)). |
366 instrumented_notifications_getAll( | 368 instrumented_notifications_getAll( |
367 chromeNotificationGetAllSavedArgs.match(ANYTHING)). | 369 chromeNotificationGetAllSavedArgs.match(ANYTHING)). |
368 will( | 370 will( |
369 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), | 371 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), |
370 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); | 372 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); |
371 | 373 |
372 // Invoking the tested function. | 374 // Invoking the tested function. |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), | 547 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), |
546 chromeTabsCreateSavedArgs.match(ANYTHING)). | 548 chromeTabsCreateSavedArgs.match(ANYTHING)). |
547 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); | 549 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); |
548 this.mockApis.expects(once()).chrome_windows_create( | 550 this.mockApis.expects(once()).chrome_windows_create( |
549 eqJSON({url: testActionUrl})); | 551 eqJSON({url: testActionUrl})); |
550 | 552 |
551 // Invoking the tested function. | 553 // Invoking the tested function. |
552 onNotificationClicked( | 554 onNotificationClicked( |
553 testNotificationId, this.mockLocalFunctions.functions().selector); | 555 testNotificationId, this.mockLocalFunctions.functions().selector); |
554 }); | 556 }); |
OLD | NEW |