Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1872)

Side by Side Diff: chrome/browser/resources/google_now/cards_unittest.gtestjs

Issue 19749007: Processing timefences from the server. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing manifest typo Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 /**
6 * Test fixture for cards.js.
7 * @constructor
8 * @extends {testing.Test}
9 */
10 function GoogleNowCardsUnitTest () {
11 testing.Test.call(this);
12 }
13
14 GoogleNowCardsUnitTest.prototype = {
15 __proto__: testing.Test.prototype,
16
17 /** @override */
18 extraLibraries: [
19 'cards.js'
20 ]
21 };
22
23 var testCardId = 'TEST CARD ID';
24 var testNotification = { testNotificationField: 'TEST NOTIFICATION VALUE' };
25 var expectedShowAlarmId = 'card-show-TEST CARD ID';
26 var expectedHideAlarmId = 'card-hide-TEST CARD ID';
27 var testActionUrls = { testField: 'TEST VALUE' };
28
29 function setUpCardManagerTest(fixture) {
30 fixture.makeAndRegisterMockApis([
31 'chrome.alarms.onAlarm.addListener',
32 'chrome.alarms.clear',
33 'chrome.alarms.create',
34 'chrome.notifications.clear',
35 'chrome.notifications.create',
36 'chrome.notifications.update',
37 'storage.get'
38 ]);
39
40 chrome.runtime = {}; // No error.
41
42 var onAlarmSavedArgs = new SaveMockArguments();
43 fixture.mockApis.expects(once()).
44 chrome_alarms_onAlarm_addListener(
45 onAlarmSavedArgs.match(ANYTHING));
46
47 var cardSet = buildCardManager();
48
49 Mock4JS.verifyAllMocks();
50
51 Date.now = function() { return 300000; };
52
53 var test = {
54 cardSet: cardSet,
55 alarmCallback: onAlarmSavedArgs.arguments [0]
56 };
57
58 return test;
59 }
60
61 TEST_F('GoogleNowCardsUnitTest', 'BuildCardManager', function() {
62 // Tests that buildCardManager() call completes with no problems.
63 var test = setUpCardManagerTest(this);
64
65 assertEquals('object', typeof test.cardSet);
66 assertEquals('function', typeof test.alarmCallback);
67 });
68
69 TEST_F('GoogleNowCardsUnitTest', 'CreateCard', function() {
70 // Creates a new card with no trigger.
71
72 // Setup and expectations.
73 var test = setUpCardManagerTest(this);
74 this.mockApis.expects(once()).
75 chrome_alarms_clear(expectedHideAlarmId);
76 this.mockApis.expects(once()).
77 chrome_alarms_clear(expectedShowAlarmId);
78 var chromeNotificationsCreateSavedArgs = new SaveMockArguments();
79 this.mockApis.expects(once()).
80 chrome_notifications_create(
81 chromeNotificationsCreateSavedArgs.match(eq(testCardId)),
82 chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)),
83 chromeNotificationsCreateSavedArgs.match(ANYTHING)).
84 will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId))
85
86 // Call tested method.
87 var notificationData = test.cardSet.update({
88 notificationId: testCardId,
89 notification: testNotification,
90 actionUrls: testActionUrls,
91 version: 0});
92
93 // Check the return value.
94 assertEquals(
95 JSON.stringify({
96 actionUrls: testActionUrls,
97 cardCreateInfo: {
98 notification: testNotification,
99 timeHide: undefined,
100 version: 0
101 }}),
102 JSON.stringify(notificationData));
103 });
104
105 TEST_F('GoogleNowCardsUnitTest', 'CreateCardEmptyTrigger', function() {
106 // Creates a new card with empty trigger.
107
108 // Setup and expectations.
109 var test = setUpCardManagerTest(this);
110 this.mockApis.expects(once()).
111 chrome_alarms_clear(expectedHideAlarmId);
112 this.mockApis.expects(once()).
113 chrome_alarms_clear(expectedShowAlarmId);
114 this.mockApis.expects(once()).
115 chrome_notifications_create(
116 testCardId, eqJSON(testNotification), ANYTHING);
117
118 // Call tested method.
119 var notificationData = test.cardSet.update({
120 notificationId: testCardId,
121 notification: testNotification,
122 actionUrls: testActionUrls,
123 version: 0,
124 trigger: {}});
125
126 // Check the return value.
127 assertEquals(
128 JSON.stringify({
129 actionUrls: testActionUrls,
130 cardCreateInfo: {
131 notification: testNotification,
132 timeHide: undefined,
133 version: 0
134 }}),
135 JSON.stringify(notificationData));
136 });
137
138 TEST_F('GoogleNowCardsUnitTest', 'CreateCardHideTime', function() {
139 // Creates a new card with trigger specifying hide time.
140
141 // Setup and expectations.
142 var test = setUpCardManagerTest(this);
143 this.mockApis.expects(once()).
144 chrome_alarms_clear(expectedHideAlarmId);
145 this.mockApis.expects(once()).
146 chrome_alarms_clear(expectedShowAlarmId);
147 var chromeNotificationsCreateSavedArgs = new SaveMockArguments();
148 this.mockApis.expects(once()).
149 chrome_notifications_create(
150 chromeNotificationsCreateSavedArgs.match(eq(testCardId)),
151 chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)),
152 chromeNotificationsCreateSavedArgs.match(ANYTHING)).
153 will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId))
154 this.mockApis.expects(once()).
155 chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 1313000}));
156
157 // Call tested method.
158 var notificationData = test.cardSet.update({
159 notificationId: testCardId,
160 notification: testNotification,
161 actionUrls: testActionUrls,
162 version: 0,
163 trigger: {hideTimeSec: 1013}});
164
165 // Check the return value.
166 assertEquals(
167 JSON.stringify({
168 actionUrls: testActionUrls,
169 cardCreateInfo: {
170 notification: testNotification,
171 timeHide: 1313000,
172 version: 0
173 }}),
174 JSON.stringify(notificationData));
175 });
176
177 TEST_F('GoogleNowCardsUnitTest', 'UpdateCardSameVersion', function() {
178 // Updates a card with another card with same version.
179
180 // Setup and expectations.
181 var test = setUpCardManagerTest(this);
182 this.mockApis.expects(once()).
183 chrome_alarms_clear(expectedHideAlarmId);
184 this.mockApis.expects(once()).
185 chrome_alarms_clear(expectedShowAlarmId);
186 var chromeNotificationsCreateSavedArgs = new SaveMockArguments();
187 this.mockApis.expects(once()).
188 chrome_notifications_update(
189 chromeNotificationsCreateSavedArgs.match(eq(testCardId)),
190 chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)),
191 chromeNotificationsCreateSavedArgs.match(ANYTHING)).
192 will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, true))
193
194 // Call tested method.
195 test.cardSet.update({
196 notificationId: testCardId,
197 notification: testNotification,
198 actionUrls: testActionUrls,
199 version: 0},
200 0);
201 });
202
203 TEST_F('GoogleNowCardsUnitTest', 'UpdateCardSameVersionHideTime', function() {
204 // Updates a card with another card with same version and specifying hide
205 // time.
206
207 // Setup and expectations.
208 var test = setUpCardManagerTest(this);
209 this.mockApis.expects(once()).
210 chrome_alarms_clear(expectedHideAlarmId);
211 this.mockApis.expects(once()).
212 chrome_alarms_clear(expectedShowAlarmId);
213 var chromeNotificationsCreateSavedArgs = new SaveMockArguments();
214 this.mockApis.expects(once()).
215 chrome_notifications_update(
216 chromeNotificationsCreateSavedArgs.match(eq(testCardId)),
217 chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)),
218 chromeNotificationsCreateSavedArgs.match(ANYTHING)).
219 will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId))
220 this.mockApis.expects(once()).
221 chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 1313000}));
222
223 // Call tested method.
224 test.cardSet.update({
225 notificationId: testCardId,
226 notification: testNotification,
227 actionUrls: testActionUrls,
228 version: 0,
229 trigger: {hideTimeSec: 1013}},
230 0);
231 });
232
233 TEST_F('GoogleNowCardsUnitTest', 'UpdateCardDifferentVersion', function() {
234 // Updates a card with another card with different version.
235
236 // Setup and expectations.
237 var test = setUpCardManagerTest(this);
238 this.mockApis.expects(once()).
239 chrome_alarms_clear(expectedHideAlarmId);
240 this.mockApis.expects(once()).
241 chrome_alarms_clear(expectedShowAlarmId);
242 this.mockApis.expects(once()).
243 chrome_notifications_create(
244 testCardId, eqJSON(testNotification), ANYTHING);
245
246 // Call tested method.
247 test.cardSet.update({
248 notificationId: testCardId,
249 notification: testNotification,
250 actionUrls: testActionUrls,
251 version: 0},
252 1);
253 });
254
255 TEST_F('GoogleNowCardsUnitTest', 'CreateCardTriggerShowNow', function() {
256 // Creates a new card with trigger that requires showing the card immediately.
257
258 // Setup and expectations.
259 var test = setUpCardManagerTest(this);
260 this.mockApis.expects(once()).
261 chrome_alarms_clear(expectedHideAlarmId);
262 this.mockApis.expects(once()).
263 chrome_alarms_clear(expectedShowAlarmId);
264 this.mockApis.expects(once()).
265 chrome_notifications_create(
266 testCardId, eqJSON(testNotification), ANYTHING);
267
268 // Call tested method.
269 test.cardSet.update({
270 notificationId: testCardId,
271 notification: testNotification,
272 actionUrls: testActionUrls,
273 version: 0,
274 trigger: {showTimeSec: 0}});
275 });
276
277 TEST_F('GoogleNowCardsUnitTest', 'CreateCardTriggerShowLater', function() {
278 // Creates a new card with trigger that requires showing the card later.
279 // We are supposed to attempt cleaning the notification and schedule an alarm
280 // to show it later.
281
282 // Setup and expectations.
283 var test = setUpCardManagerTest(this);
284 this.mockApis.expects(once()).
285 chrome_alarms_clear(expectedHideAlarmId);
286 this.mockApis.expects(once()).
287 chrome_notifications_clear(testCardId, ANYTHING);
288 this.mockApis.expects(once()).
289 chrome_alarms_create(expectedShowAlarmId, eqJSON({when: 539000}));
290
291 // Call tested method.
292 test.cardSet.update({
293 notificationId: testCardId,
294 notification: testNotification,
295 actionUrls: testActionUrls,
296 version: 0,
297 trigger: {showTimeSec: 239}});
298 });
299
300 TEST_F('GoogleNowCardsUnitTest', 'ClearCard', function() {
301 // Clears a card.
302
303 // Setup and expectations.
304 var test = setUpCardManagerTest(this);
305 this.mockApis.expects(once()).
306 chrome_notifications_clear(testCardId, ANYTHING);
307 this.mockApis.expects(once()).
308 chrome_alarms_clear(expectedShowAlarmId);
309 this.mockApis.expects(once()).
310 chrome_alarms_clear(expectedHideAlarmId);
311
312 // Call tested method.
313 test.cardSet.clear(testCardId);
314 });
315
316 TEST_F('GoogleNowCardsUnitTest', 'onAlarmUnrecognized', function() {
rgustafson 2013/07/19 23:37:12 This test isn't really doing anything?
vadimt 2013/07/22 19:22:52 This alarm handler will get all alarms, for exampl
317 // Tests onAlarm does nothing on an unrelated alarm.
318 var test = setUpCardManagerTest(this);
319
320 // Call tested method.
321 test.alarmCallback({name: 'unrelated'});
322 });
323
324 TEST_F('GoogleNowCardsUnitTest', 'onAlarmShowNoData', function() {
325 // Tests onAlarm for the 'show' alarm when there is no data for the card.
326 var test = setUpCardManagerTest(this);
327 var storageGetSavedArgs = new SaveMockArguments();
328 this.mockApis.expects(once()).
329 storage_get(
330 storageGetSavedArgs.match(eq('notificationsData')),
331 storageGetSavedArgs.match(ANYTHING)).
332 will(invokeCallback(storageGetSavedArgs, 1, {}));
333
334 // Call tested method.
335 test.alarmCallback({name: expectedShowAlarmId});
336 });
337
338 TEST_F('GoogleNowCardsUnitTest', 'onAlarmShowHasData', function() {
339 // Tests onAlarm for the 'show' alarm when there is data for the card.
340 var test = setUpCardManagerTest(this);
341 var storageGetSavedArgs = new SaveMockArguments();
342 this.mockApis.expects(once()).
343 storage_get(
344 storageGetSavedArgs.match(eq('notificationsData')),
345 storageGetSavedArgs.match(ANYTHING)).
346 will(invokeCallback(
347 storageGetSavedArgs,
348 1, {
rgustafson 2013/07/19 23:37:12 The { on this line looks off. new line?
vadimt 2013/07/22 19:22:52 Done.
349 notificationsData: {
350 'TEST CARD ID': {
351 actionUrls: testActionUrls,
352 cardCreateInfo: {
353 notification: testNotification,
354 timeHide: 1313000,
355 version: 0
356 }}}}));
357 var chromeNotificationsCreateSavedArgs = new SaveMockArguments();
358 this.mockApis.expects(once()).
359 chrome_notifications_create(
360 chromeNotificationsCreateSavedArgs.match(eq(testCardId)),
361 chromeNotificationsCreateSavedArgs.match(eqJSON(testNotification)),
362 chromeNotificationsCreateSavedArgs.match(ANYTHING)).
363 will(invokeCallback(chromeNotificationsCreateSavedArgs, 2, testCardId))
364 this.mockApis.expects(once()).
365 chrome_alarms_create(expectedHideAlarmId, eqJSON({when: 1313000}));
366
367 // Call tested method.
368 test.alarmCallback({name: expectedShowAlarmId});
369 });
370
371 TEST_F('GoogleNowCardsUnitTest', 'onAlarmHide', function() {
372 // Tests onAlarm for the 'hide' alarm.
373 var test = setUpCardManagerTest(this);
374 this.mockApis.expects(once()).
375 chrome_notifications_clear(testCardId, ANYTHING);
376
377 // Call tested method.
378 test.alarmCallback({name: expectedHideAlarmId});
379 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698