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 12 matching lines...) Expand all Loading... | |
23 | 23 |
24 TEST_F('GoogleNowBackgroundUnitTest', 'AreTasksConflicting', function() { | 24 TEST_F('GoogleNowBackgroundUnitTest', 'AreTasksConflicting', function() { |
25 function testTaskPair(newTaskName, scheduledTaskName, expected) { | 25 function testTaskPair(newTaskName, scheduledTaskName, expected) { |
26 assertTrue(areTasksConflicting(newTaskName, scheduledTaskName) == expected, | 26 assertTrue(areTasksConflicting(newTaskName, scheduledTaskName) == expected, |
27 '(' + newTaskName + ', ' + scheduledTaskName + ')'); | 27 '(' + newTaskName + ', ' + scheduledTaskName + ')'); |
28 } | 28 } |
29 | 29 |
30 testTaskPair(UPDATE_CARDS_TASK_NAME, UPDATE_CARDS_TASK_NAME, true); | 30 testTaskPair(UPDATE_CARDS_TASK_NAME, UPDATE_CARDS_TASK_NAME, true); |
31 testTaskPair(UPDATE_CARDS_TASK_NAME, DISMISS_CARD_TASK_NAME, false); | 31 testTaskPair(UPDATE_CARDS_TASK_NAME, DISMISS_CARD_TASK_NAME, false); |
32 testTaskPair(UPDATE_CARDS_TASK_NAME, RETRY_DISMISS_TASK_NAME, false); | 32 testTaskPair(UPDATE_CARDS_TASK_NAME, RETRY_DISMISS_TASK_NAME, false); |
33 testTaskPair(UPDATE_CARDS_TASK_NAME, STATE_CHANGED_TASK_NAME, false); | |
33 | 34 |
34 testTaskPair(DISMISS_CARD_TASK_NAME, UPDATE_CARDS_TASK_NAME, false); | 35 testTaskPair(DISMISS_CARD_TASK_NAME, UPDATE_CARDS_TASK_NAME, false); |
35 testTaskPair(DISMISS_CARD_TASK_NAME, DISMISS_CARD_TASK_NAME, false); | 36 testTaskPair(DISMISS_CARD_TASK_NAME, DISMISS_CARD_TASK_NAME, false); |
36 testTaskPair(DISMISS_CARD_TASK_NAME, RETRY_DISMISS_TASK_NAME, false); | 37 testTaskPair(DISMISS_CARD_TASK_NAME, RETRY_DISMISS_TASK_NAME, false); |
38 testTaskPair(DISMISS_CARD_TASK_NAME, STATE_CHANGED_TASK_NAME, false); | |
37 | 39 |
38 testTaskPair(RETRY_DISMISS_TASK_NAME, UPDATE_CARDS_TASK_NAME, true); | 40 testTaskPair(RETRY_DISMISS_TASK_NAME, UPDATE_CARDS_TASK_NAME, true); |
39 testTaskPair(RETRY_DISMISS_TASK_NAME, DISMISS_CARD_TASK_NAME, true); | 41 testTaskPair(RETRY_DISMISS_TASK_NAME, DISMISS_CARD_TASK_NAME, true); |
40 testTaskPair(RETRY_DISMISS_TASK_NAME, RETRY_DISMISS_TASK_NAME, true); | 42 testTaskPair(RETRY_DISMISS_TASK_NAME, RETRY_DISMISS_TASK_NAME, true); |
43 testTaskPair(RETRY_DISMISS_TASK_NAME, STATE_CHANGED_TASK_NAME, false); | |
44 | |
45 testTaskPair(STATE_CHANGED_TASK_NAME, UPDATE_CARDS_TASK_NAME, false); | |
46 testTaskPair(STATE_CHANGED_TASK_NAME, DISMISS_CARD_TASK_NAME, false); | |
47 testTaskPair(STATE_CHANGED_TASK_NAME, RETRY_DISMISS_TASK_NAME, false); | |
48 testTaskPair(STATE_CHANGED_TASK_NAME, STATE_CHANGED_TASK_NAME, false); | |
41 }); | 49 }); |
42 | 50 |
43 /** | 51 /** |
44 * Mocks global functions and APIs that initialize() depends upon. | 52 * Mocks global functions and APIs that initialize() depends upon. |
45 * @param {Test} fixture Test fixture. | 53 * @param {Test} fixture Test fixture. |
46 */ | 54 */ |
47 function mockInitializeDependencies(fixture) { | 55 function mockInitializeDependencies(fixture) { |
48 fixture.makeAndRegisterMockGlobals([ | 56 fixture.makeAndRegisterMockGlobals([ |
49 'recordEvent', | 57 'recordEvent', |
50 'showWelcomeToast', | 58 'showWelcomeToast', |
51 'startPollingCards']); | 59 'startPollingCards']); |
52 fixture.makeAndRegisterMockApis( | 60 fixture.makeAndRegisterMockApis([ |
53 ['storage.get', 'chrome.identity.getAuthToken']); | 61 'chrome.identity.getAuthToken', |
62 'chrome.notifications.getAll', | |
63 'chrome.preferencesPrivate.googleGeolocationAccessEnabled.get', | |
64 'storage.get']); | |
65 } | |
66 | |
67 /** | |
68 * Sets up the test to expect the state machine calls and send | |
vadimt
2013/07/25 20:48:01
Please mention which tested method this is for.
robliao
2013/07/25 22:35:48
Done.
| |
69 * the specified state machine state. | |
70 * @param {object} mockApisObj Mock APIs Object. | |
71 * @param {string} testIdentityToken getAuthToken callback token. | |
72 * @param {boolean} testGeolocationPref Geolocation Preference callback value. | |
73 * @param {boolean} testUserRespondedToToast User Response to toast | |
74 & callback value. | |
75 */ | |
76 function expectStateMachineCalls( | |
77 mockApisObj, | |
78 testIdentityToken, | |
79 testGeolocationPref, | |
80 testUserRespondedToToast) { | |
81 var chromeIdentityGetAuthTokenSavedArgs = new SaveMockArguments(); | |
82 mockApisObj.expects(once()). | |
83 chrome_identity_getAuthToken( | |
84 chromeIdentityGetAuthTokenSavedArgs.match( | |
85 eqJSON({interactive: false})), | |
86 chromeIdentityGetAuthTokenSavedArgs.match(ANYTHING)). | |
87 will(invokeCallback( | |
88 chromeIdentityGetAuthTokenSavedArgs, 1, testIdentityToken)); | |
89 | |
90 var googleGeolocationPrefGetSavedArgs = new SaveMockArguments(); | |
91 mockApisObj.expects(once()). | |
92 chrome_preferencesPrivate_googleGeolocationAccessEnabled_get( | |
93 googleGeolocationPrefGetSavedArgs.match(eqJSON({})), | |
94 googleGeolocationPrefGetSavedArgs.match(ANYTHING)). | |
95 will(invokeCallback( | |
96 googleGeolocationPrefGetSavedArgs, 1, {value: testGeolocationPref})); | |
97 | |
98 var storageGetSavedArgs = new SaveMockArguments(); | |
99 mockApisObj.expects(once()). | |
100 storage_get( | |
101 storageGetSavedArgs.match(eq('userRespondedToToast')), | |
102 storageGetSavedArgs.match(ANYTHING)). | |
103 will(invokeCallback(storageGetSavedArgs, 1, testUserRespondedToToast)); | |
54 } | 104 } |
55 | 105 |
56 TEST_F( | 106 TEST_F( |
57 'GoogleNowBackgroundUnitTest', | 107 'GoogleNowBackgroundUnitTest', |
58 'Initialize_ToastStateEmpty1', | 108 'Initialize_ToastStateEmpty1', |
59 function() { | 109 function() { |
60 // Tests the case when toast state is empty and NOTIFICATION_CARDS_URL is | 110 // Tests the case when the user isn't signed in and NOTIFICATION_CARDS_URL |
61 // not set. In this case, the function should quietly exit after finding | 111 // is not set. Since NOTIFICATION_CARDS_URL is empty, |
62 // out that NOTIFICATION_CARDS_URL is empty. | 112 // nothing should start. |
63 | 113 |
64 // Setup and expectations. | 114 // Setup and expectations. |
65 var testToastState = {}; | |
66 NOTIFICATION_CARDS_URL = undefined; | 115 NOTIFICATION_CARDS_URL = undefined; |
116 var testIdentityToken = undefined; | |
117 var testGeolocationPref = false; | |
118 var testUserRespondedToToast = {}; | |
67 | 119 |
68 mockInitializeDependencies(this); | 120 mockInitializeDependencies(this); |
69 | 121 |
70 this.mockGlobals.expects(once()).recordEvent( | 122 this.mockGlobals.expects(once()).recordEvent( |
71 DiagnosticEvent.EXTENSION_START); | 123 DiagnosticEvent.EXTENSION_START); |
72 var storageGetSavedArgs = new SaveMockArguments(); | 124 |
73 this.mockApis.expects(once()). | 125 expectStateMachineCalls( |
74 storage_get( | 126 this.mockApis, |
75 storageGetSavedArgs.match(eq('toastState')), | 127 testIdentityToken, |
76 storageGetSavedArgs.match(ANYTHING)). | 128 testGeolocationPref, |
77 will(invokeCallback(storageGetSavedArgs, 1, testToastState)); | 129 testUserRespondedToToast); |
130 | |
131 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); | |
132 this.mockApis.expects(exactly(2)). | |
133 chrome_notifications_getAll( | |
134 chromeNotificationGetAllSavedArgs.match(ANYTHING)). | |
135 will( | |
136 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), | |
137 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); | |
78 | 138 |
79 // Invoking the tested function. | 139 // Invoking the tested function. |
80 initialize(); | 140 initialize(); |
vadimt
2013/07/25 20:48:01
Unit tests typically mock all callees and only che
robliao
2013/07/25 22:35:48
Done.
| |
81 }); | 141 }); |
82 | 142 |
83 TEST_F( | 143 TEST_F( |
84 'GoogleNowBackgroundUnitTest', | 144 'GoogleNowBackgroundUnitTest', |
85 'Initialize_ToastStateEmpty2', | 145 'Initialize_ToastStateEmpty2', |
86 function() { | 146 function() { |
87 // Tests the case when toast state is empty and NOTIFICATION_CARDS_URL is | 147 // Tests the case when NOTIFICATION_CARDS_URL is but getAuthToken fails |
88 // set, but getAuthToken fails most likely because the user is not signed | 148 // most likely because the user is not signed in. In this case, the |
89 // in. In this case, the function should quietly exit after finding out | 149 // function should quietly exit after finding out that getAuthToken fails. |
90 // that getAuthToken fails. | |
91 | 150 |
92 // Setup and expectations. | 151 // Setup and expectations. |
93 var testToastState = {}; | |
94 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; | 152 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; |
95 var testIdentityToken = undefined; | 153 var testIdentityToken = undefined; |
154 var testGeolocationPref = false; | |
155 var testUserRespondedToToast = {}; | |
96 | 156 |
97 mockInitializeDependencies(this); | 157 mockInitializeDependencies(this); |
98 | 158 |
99 this.mockGlobals.expects(once()).recordEvent( | 159 this.mockGlobals.expects(once()).recordEvent( |
100 DiagnosticEvent.EXTENSION_START); | 160 DiagnosticEvent.EXTENSION_START); |
101 var storageGetSavedArgs = new SaveMockArguments(); | 161 |
102 this.mockApis.expects(once()). | 162 expectStateMachineCalls( |
103 storage_get( | 163 this.mockApis, |
104 storageGetSavedArgs.match(eq('toastState')), | 164 testIdentityToken, |
105 storageGetSavedArgs.match(ANYTHING)). | 165 testGeolocationPref, |
106 will(invokeCallback(storageGetSavedArgs, 1, testToastState)); | 166 testUserRespondedToToast); |
107 var chromeIdentityGetAuthTokenSavedArgs = new SaveMockArguments(); | 167 |
108 this.mockApis.expects(once()). | 168 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); |
109 chrome_identity_getAuthToken( | 169 this.mockApis.expects(exactly(2)). |
110 chromeIdentityGetAuthTokenSavedArgs.match( | 170 chrome_notifications_getAll( |
111 eqJSON({interactive: false})), | 171 chromeNotificationGetAllSavedArgs.match(ANYTHING)). |
112 chromeIdentityGetAuthTokenSavedArgs.match(ANYTHING)). | 172 will( |
113 will(invokeCallback( | 173 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), |
114 chromeIdentityGetAuthTokenSavedArgs, 1, testIdentityToken)); | 174 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); |
175 | |
vadimt
2013/07/25 20:48:01
Remove empty line
robliao
2013/07/25 22:35:48
Done.
| |
115 | 176 |
116 // Invoking the tested function. | 177 // Invoking the tested function. |
117 initialize(); | 178 initialize(); |
118 }); | 179 }); |
119 | 180 |
120 TEST_F( | 181 TEST_F( |
121 'GoogleNowBackgroundUnitTest', | 182 'GoogleNowBackgroundUnitTest', |
122 'Initialize_ToastStateEmpty3', | 183 'Initialize_ToastStateEmpty3', |
123 function() { | 184 function() { |
124 // Tests the case when toast state is empty and NOTIFICATION_CARDS_URL is | 185 // Tests the case when NOTIFICATION_CARDS_URL is set, getAuthToken |
125 // set, and getAuthToken succeeds. In this case, the function should | 186 // succeeds, and the user has never responded to the toast. |
126 // invoke showWelcomeToast(). | 187 // In this case, the function should invoke showWelcomeToast(). |
127 | 188 |
128 // Setup and expectations. | 189 // Setup and expectations. |
129 var testToastState = {}; | |
130 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; | 190 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; |
131 var testIdentityToken = 'some identity token'; | 191 var testIdentityToken = 'some identity token'; |
192 var testGeolocationPref = false; | |
193 var testUserRespondedToToast = {}; | |
132 | 194 |
133 mockInitializeDependencies(this); | 195 mockInitializeDependencies(this); |
134 | 196 |
135 this.mockGlobals.expects(once()).recordEvent( | 197 this.mockGlobals.expects(once()).recordEvent( |
136 DiagnosticEvent.EXTENSION_START); | 198 DiagnosticEvent.EXTENSION_START); |
137 var storageGetSavedArgs = new SaveMockArguments(); | 199 |
138 this.mockApis.expects(once()). | 200 expectStateMachineCalls( |
139 storage_get( | 201 this.mockApis, |
140 storageGetSavedArgs.match(eq('toastState')), | 202 testIdentityToken, |
141 storageGetSavedArgs.match(ANYTHING)). | 203 testGeolocationPref, |
142 will(invokeCallback(storageGetSavedArgs, 1, testToastState)); | 204 testUserRespondedToToast); |
143 var chromeIdentityGetAuthTokenSavedArgs = new SaveMockArguments(); | 205 |
144 this.mockApis.expects(once()). | 206 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); |
145 chrome_identity_getAuthToken( | 207 this.mockApis.expects(exactly(2)). |
146 chromeIdentityGetAuthTokenSavedArgs.match( | 208 chrome_notifications_getAll( |
147 eqJSON({interactive: false})), | 209 chromeNotificationGetAllSavedArgs.match(ANYTHING)). |
148 chromeIdentityGetAuthTokenSavedArgs.match(ANYTHING)). | 210 will( |
149 will(invokeCallback( | 211 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), |
150 chromeIdentityGetAuthTokenSavedArgs, 1, testIdentityToken)); | 212 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); |
213 | |
151 this.mockGlobals.expects(once()).showWelcomeToast(); | 214 this.mockGlobals.expects(once()).showWelcomeToast(); |
152 | 215 |
153 // Invoking the tested function. | 216 // Invoking the tested function. |
154 initialize(); | 217 initialize(); |
155 }); | 218 }); |
156 | 219 |
157 TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_ToastStateYes', function() { | 220 TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_ToastStateYes', function() { |
vadimt
2013/07/25 20:48:01
The name and comment seem out of date. We don't st
robliao
2013/07/25 22:35:48
The comment is still valid. I've updated the name.
vadimt
2013/07/26 01:00:37
The name and comment are still confusing. The user
robliao
2013/07/26 01:12:17
This is the core user scenario we care about with
| |
158 // Tests the case when the user has answered "yes" to the toast in the past. | 221 // Tests the case when the user has answered "yes" to the toast in the past. |
159 // In this case, the function should invoke startPollingCards(). | 222 // In this case, the function should invoke startPollingCards(). |
160 | 223 |
161 // Setup and expectations. | 224 // Setup and expectations. |
162 var testToastState = {toastState: ToastOptionResponse.CHOSE_YES}; | 225 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; |
226 var testIdentityToken = 'some identity token'; | |
227 var testGeolocationPref = true; | |
228 var testUserRespondedToToast = {userRespondedToToast: true}; | |
163 | 229 |
164 mockInitializeDependencies(this); | 230 mockInitializeDependencies(this); |
165 | 231 |
166 this.mockGlobals.expects(once()).recordEvent(DiagnosticEvent.EXTENSION_START); | 232 this.mockGlobals.expects(once()).recordEvent( |
167 var storageGetSavedArgs = new SaveMockArguments(); | 233 DiagnosticEvent.EXTENSION_START); |
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 | 234 |
175 // Invoking the tested function. | 235 expectStateMachineCalls( |
176 initialize(); | 236 this.mockApis, |
237 testIdentityToken, | |
238 testGeolocationPref, | |
239 testUserRespondedToToast); | |
240 | |
241 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); | |
242 this.mockApis.expects(exactly(2)). | |
243 chrome_notifications_getAll( | |
244 chromeNotificationGetAllSavedArgs.match(ANYTHING)). | |
245 will( | |
246 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), | |
247 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); | |
248 | |
249 this.mockGlobals.expects(once()).startPollingCards(); | |
250 | |
251 // Invoking the tested function. | |
252 initialize(); | |
177 }); | 253 }); |
178 | 254 |
179 TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_ToastStateNo', function() { | 255 TEST_F('GoogleNowBackgroundUnitTest', 'Initialize_ToastStateNo', function() { |
180 // Tests the case when the user has answered "no" to the toast in the past. | 256 // Tests the case when the user has answered "no" to the toast in the past. |
181 // In this case, the function should do nothing. | 257 // In this case, the function should do nothing. |
182 | 258 |
183 // Setup and expectations. | 259 // Setup and expectations. |
184 var testToastState = {toastState: ToastOptionResponse.CHOSE_NO}; | 260 // Setup and expectations. |
261 NOTIFICATION_CARDS_URL = 'https://some.server.url.com'; | |
262 var testIdentityToken = 'some identity token'; | |
263 var testGeolocationPref = false; | |
264 var testUserRespondedToToast = {userRespondedToToast: true}; | |
185 | 265 |
186 mockInitializeDependencies(this); | 266 mockInitializeDependencies(this); |
187 | 267 |
188 this.mockGlobals.expects(once()).recordEvent(DiagnosticEvent.EXTENSION_START); | 268 this.mockGlobals.expects(once()).recordEvent( |
189 var storageGetSavedArgs = new SaveMockArguments(); | 269 DiagnosticEvent.EXTENSION_START); |
190 this.mockApis.expects(once()). | |
191 storage_get( | |
192 storageGetSavedArgs.match(eq('toastState')), | |
193 storageGetSavedArgs.match(ANYTHING)). | |
194 will(invokeCallback(storageGetSavedArgs, 1, testToastState)); | |
195 | 270 |
196 // Invoking the tested function. | 271 expectStateMachineCalls( |
197 initialize(); | 272 this.mockApis, |
273 testIdentityToken, | |
274 testGeolocationPref, | |
275 testUserRespondedToToast); | |
276 | |
277 var chromeNotificationGetAllSavedArgs = new SaveMockArguments(); | |
278 this.mockApis.expects(exactly(2)). | |
279 chrome_notifications_getAll( | |
280 chromeNotificationGetAllSavedArgs.match(ANYTHING)). | |
281 will( | |
282 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {}), | |
283 invokeCallback(chromeNotificationGetAllSavedArgs, 0, {})); | |
284 | |
285 // Invoking the tested function. | |
286 initialize(); | |
198 }); | 287 }); |
199 | 288 |
200 /** | 289 /** |
201 * Mocks global functions and APIs that onNotificationClicked() depends upon. | 290 * Mocks global functions and APIs that onNotificationClicked() depends upon. |
202 * @param {Test} fixture Test fixture. | 291 * @param {Test} fixture Test fixture. |
203 */ | 292 */ |
204 function mockOnNotificationClickedDependencies(fixture) { | 293 function mockOnNotificationClickedDependencies(fixture) { |
205 fixture.makeAndRegisterMockApis([ | 294 fixture.makeAndRegisterMockApis([ |
206 'storage.get', | 295 'storage.get', |
207 'chrome.tabs.create', | 296 'chrome.tabs.create', |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), | 458 chromeTabsCreateSavedArgs.match(eqJSON({url: testActionUrl})), |
370 chromeTabsCreateSavedArgs.match(ANYTHING)). | 459 chromeTabsCreateSavedArgs.match(ANYTHING)). |
371 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); | 460 will(invokeCallback(chromeTabsCreateSavedArgs, 1, testCreatedTab)); |
372 this.mockApis.expects(once()).chrome_windows_create( | 461 this.mockApis.expects(once()).chrome_windows_create( |
373 eqJSON({url: testActionUrl})); | 462 eqJSON({url: testActionUrl})); |
374 | 463 |
375 // Invoking the tested function. | 464 // Invoking the tested function. |
376 onNotificationClicked( | 465 onNotificationClicked( |
377 testNotificationId, this.mockLocalFunctions.functions().selector); | 466 testNotificationId, this.mockLocalFunctions.functions().selector); |
378 }); | 467 }); |
OLD | NEW |