OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/browser/notifications/desktop_notifications_unittest.h" | 5 #include "chrome/browser/notifications/desktop_notifications_unittest.h" |
6 | 6 |
7 #include "base/prefs/testing_pref_service.h" | 7 #include "base/prefs/testing_pref_service.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/notifications/balloon_notification_ui_manager.h" | 10 #include "chrome/browser/notifications/balloon_notification_ui_manager.h" |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 return params; | 166 return params; |
167 } | 167 } |
168 | 168 |
169 TEST_F(DesktopNotificationsTest, TestShow) { | 169 TEST_F(DesktopNotificationsTest, TestShow) { |
170 content::ShowDesktopNotificationHostMsgParams params = | 170 content::ShowDesktopNotificationHostMsgParams params = |
171 StandardTestNotification(); | 171 StandardTestNotification(); |
172 params.notification_id = 1; | 172 params.notification_id = 1; |
173 | 173 |
174 EXPECT_TRUE(service_->ShowDesktopNotification( | 174 EXPECT_TRUE(service_->ShowDesktopNotification( |
175 params, 0, 0, DesktopNotificationService::PageNotification)); | 175 params, 0, 0, DesktopNotificationService::PageNotification)); |
176 MessageLoopForUI::current()->RunUntilIdle(); | 176 base::MessageLoopForUI::current()->RunUntilIdle(); |
177 EXPECT_EQ(1, balloon_collection_->count()); | 177 EXPECT_EQ(1, balloon_collection_->count()); |
178 | 178 |
179 content::ShowDesktopNotificationHostMsgParams params2; | 179 content::ShowDesktopNotificationHostMsgParams params2; |
180 params2.origin = GURL("http://www.google.com"); | 180 params2.origin = GURL("http://www.google.com"); |
181 params2.is_html = true; | 181 params2.is_html = true; |
182 params2.contents_url = GURL("http://www.google.com/notification.html"); | 182 params2.contents_url = GURL("http://www.google.com/notification.html"); |
183 params2.notification_id = 2; | 183 params2.notification_id = 2; |
184 | 184 |
185 EXPECT_TRUE(service_->ShowDesktopNotification( | 185 EXPECT_TRUE(service_->ShowDesktopNotification( |
186 params2, 0, 0, DesktopNotificationService::PageNotification)); | 186 params2, 0, 0, DesktopNotificationService::PageNotification)); |
187 MessageLoopForUI::current()->RunUntilIdle(); | 187 base::MessageLoopForUI::current()->RunUntilIdle(); |
188 EXPECT_EQ(2, balloon_collection_->count()); | 188 EXPECT_EQ(2, balloon_collection_->count()); |
189 | 189 |
190 EXPECT_EQ("notification displayed\n" | 190 EXPECT_EQ("notification displayed\n" |
191 "notification displayed\n", | 191 "notification displayed\n", |
192 log_output_); | 192 log_output_); |
193 } | 193 } |
194 | 194 |
195 TEST_F(DesktopNotificationsTest, TestClose) { | 195 TEST_F(DesktopNotificationsTest, TestClose) { |
196 content::ShowDesktopNotificationHostMsgParams params = | 196 content::ShowDesktopNotificationHostMsgParams params = |
197 StandardTestNotification(); | 197 StandardTestNotification(); |
198 params.notification_id = 1; | 198 params.notification_id = 1; |
199 | 199 |
200 // Request a notification; should open a balloon. | 200 // Request a notification; should open a balloon. |
201 EXPECT_TRUE(service_->ShowDesktopNotification( | 201 EXPECT_TRUE(service_->ShowDesktopNotification( |
202 params, 0, 0, DesktopNotificationService::PageNotification)); | 202 params, 0, 0, DesktopNotificationService::PageNotification)); |
203 MessageLoopForUI::current()->RunUntilIdle(); | 203 base::MessageLoopForUI::current()->RunUntilIdle(); |
204 EXPECT_EQ(1, balloon_collection_->count()); | 204 EXPECT_EQ(1, balloon_collection_->count()); |
205 | 205 |
206 // Close all the open balloons. | 206 // Close all the open balloons. |
207 while (balloon_collection_->count() > 0) { | 207 while (balloon_collection_->count() > 0) { |
208 (*(balloon_collection_->GetActiveBalloons().begin()))->OnClose(true); | 208 (*(balloon_collection_->GetActiveBalloons().begin()))->OnClose(true); |
209 } | 209 } |
210 | 210 |
211 EXPECT_EQ("notification displayed\n" | 211 EXPECT_EQ("notification displayed\n" |
212 "notification closed by user\n", | 212 "notification closed by user\n", |
213 log_output_); | 213 log_output_); |
214 } | 214 } |
215 | 215 |
216 TEST_F(DesktopNotificationsTest, TestCancel) { | 216 TEST_F(DesktopNotificationsTest, TestCancel) { |
217 int process_id = 0; | 217 int process_id = 0; |
218 int route_id = 0; | 218 int route_id = 0; |
219 int notification_id = 1; | 219 int notification_id = 1; |
220 | 220 |
221 content::ShowDesktopNotificationHostMsgParams params = | 221 content::ShowDesktopNotificationHostMsgParams params = |
222 StandardTestNotification(); | 222 StandardTestNotification(); |
223 params.notification_id = notification_id; | 223 params.notification_id = notification_id; |
224 | 224 |
225 // Request a notification; should open a balloon. | 225 // Request a notification; should open a balloon. |
226 EXPECT_TRUE(service_->ShowDesktopNotification( | 226 EXPECT_TRUE(service_->ShowDesktopNotification( |
227 params, process_id, route_id, | 227 params, process_id, route_id, |
228 DesktopNotificationService::PageNotification)); | 228 DesktopNotificationService::PageNotification)); |
229 MessageLoopForUI::current()->RunUntilIdle(); | 229 base::MessageLoopForUI::current()->RunUntilIdle(); |
230 EXPECT_EQ(1, balloon_collection_->count()); | 230 EXPECT_EQ(1, balloon_collection_->count()); |
231 | 231 |
232 // Cancel the same notification | 232 // Cancel the same notification |
233 service_->CancelDesktopNotification(process_id, | 233 service_->CancelDesktopNotification(process_id, |
234 route_id, | 234 route_id, |
235 notification_id); | 235 notification_id); |
236 MessageLoopForUI::current()->RunUntilIdle(); | 236 base::MessageLoopForUI::current()->RunUntilIdle(); |
237 // Verify that the balloon collection is now empty. | 237 // Verify that the balloon collection is now empty. |
238 EXPECT_EQ(0, balloon_collection_->count()); | 238 EXPECT_EQ(0, balloon_collection_->count()); |
239 | 239 |
240 EXPECT_EQ("notification displayed\n" | 240 EXPECT_EQ("notification displayed\n" |
241 "notification closed by script\n", | 241 "notification closed by script\n", |
242 log_output_); | 242 log_output_); |
243 } | 243 } |
244 | 244 |
245 #if defined(OS_WIN) || defined(TOOLKIT_VIEWS) | 245 #if defined(OS_WIN) || defined(TOOLKIT_VIEWS) |
246 TEST_F(DesktopNotificationsTest, TestPositioning) { | 246 TEST_F(DesktopNotificationsTest, TestPositioning) { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 | 334 |
335 // |kLotsOfToasts| must be large enough to trigger a resize of the underlying | 335 // |kLotsOfToasts| must be large enough to trigger a resize of the underlying |
336 // std::deque while we're clearing it. | 336 // std::deque while we're clearing it. |
337 const int kLotsOfToasts = 20; | 337 const int kLotsOfToasts = 20; |
338 for (int id = 2; id <= kLotsOfToasts; ++id) { | 338 for (int id = 2; id <= kLotsOfToasts; ++id) { |
339 params.notification_id = id; | 339 params.notification_id = id; |
340 EXPECT_TRUE(second_service->ShowDesktopNotification( | 340 EXPECT_TRUE(second_service->ShowDesktopNotification( |
341 params, process_id, route_id, | 341 params, process_id, route_id, |
342 DesktopNotificationService::PageNotification)); | 342 DesktopNotificationService::PageNotification)); |
343 } | 343 } |
344 MessageLoopForUI::current()->RunUntilIdle(); | 344 base::MessageLoopForUI::current()->RunUntilIdle(); |
345 | 345 |
346 ui_manager_->CancelAllByProfile(second_profile); | 346 ui_manager_->CancelAllByProfile(second_profile); |
347 | 347 |
348 // Verify that the balloon collection only contains the single | 348 // Verify that the balloon collection only contains the single |
349 // notification from the first profile. | 349 // notification from the first profile. |
350 EXPECT_EQ(1, balloon_collection_->count()); | 350 EXPECT_EQ(1, balloon_collection_->count()); |
351 } | 351 } |
352 | 352 |
353 TEST_F(DesktopNotificationsTest, TestCancelBySourceOrigin) { | 353 TEST_F(DesktopNotificationsTest, TestCancelBySourceOrigin) { |
354 int process_id = 0; | 354 int process_id = 0; |
(...skipping 16 matching lines...) Expand all Loading... |
371 | 371 |
372 // |kLotsOfToasts| must be large enough to trigger a resize of the underlying | 372 // |kLotsOfToasts| must be large enough to trigger a resize of the underlying |
373 // std::deque while we're clearing it. | 373 // std::deque while we're clearing it. |
374 const int kLotsOfToasts = 20; | 374 const int kLotsOfToasts = 20; |
375 for (int id = 2; id <= kLotsOfToasts; ++id) { | 375 for (int id = 2; id <= kLotsOfToasts; ++id) { |
376 odd_params.notification_id = id; | 376 odd_params.notification_id = id; |
377 EXPECT_TRUE(service_->ShowDesktopNotification( | 377 EXPECT_TRUE(service_->ShowDesktopNotification( |
378 odd_params, process_id, route_id, | 378 odd_params, process_id, route_id, |
379 DesktopNotificationService::PageNotification)); | 379 DesktopNotificationService::PageNotification)); |
380 } | 380 } |
381 MessageLoopForUI::current()->RunUntilIdle(); | 381 base::MessageLoopForUI::current()->RunUntilIdle(); |
382 | 382 |
383 ui_manager_->CancelAllBySourceOrigin(odd_params.origin); | 383 ui_manager_->CancelAllBySourceOrigin(odd_params.origin); |
384 | 384 |
385 // Verify that the balloon collection only contains the single | 385 // Verify that the balloon collection only contains the single |
386 // notification which is not from the canceled origin. | 386 // notification which is not from the canceled origin. |
387 EXPECT_EQ(1, balloon_collection_->count()); | 387 EXPECT_EQ(1, balloon_collection_->count()); |
388 } | 388 } |
389 | 389 |
390 TEST_F(DesktopNotificationsTest, TestQueueing) { | 390 TEST_F(DesktopNotificationsTest, TestQueueing) { |
391 int process_id = 0; | 391 int process_id = 0; |
392 int route_id = 0; | 392 int route_id = 0; |
393 | 393 |
394 // Request lots of identical notifications. | 394 // Request lots of identical notifications. |
395 content::ShowDesktopNotificationHostMsgParams params = | 395 content::ShowDesktopNotificationHostMsgParams params = |
396 StandardTestNotification(); | 396 StandardTestNotification(); |
397 const int kLotsOfToasts = 20; | 397 const int kLotsOfToasts = 20; |
398 for (int id = 1; id <= kLotsOfToasts; ++id) { | 398 for (int id = 1; id <= kLotsOfToasts; ++id) { |
399 params.notification_id = id; | 399 params.notification_id = id; |
400 EXPECT_TRUE(service_->ShowDesktopNotification( | 400 EXPECT_TRUE(service_->ShowDesktopNotification( |
401 params, process_id, route_id, | 401 params, process_id, route_id, |
402 DesktopNotificationService::PageNotification)); | 402 DesktopNotificationService::PageNotification)); |
403 } | 403 } |
404 MessageLoopForUI::current()->RunUntilIdle(); | 404 base::MessageLoopForUI::current()->RunUntilIdle(); |
405 | 405 |
406 // Build up an expected log of what should be happening. | 406 // Build up an expected log of what should be happening. |
407 std::string expected_log; | 407 std::string expected_log; |
408 for (int i = 0; i < balloon_collection_->max_balloon_count(); ++i) { | 408 for (int i = 0; i < balloon_collection_->max_balloon_count(); ++i) { |
409 expected_log.append("notification displayed\n"); | 409 expected_log.append("notification displayed\n"); |
410 } | 410 } |
411 | 411 |
412 // The max number that our balloon collection can hold should be | 412 // The max number that our balloon collection can hold should be |
413 // shown. | 413 // shown. |
414 EXPECT_EQ(balloon_collection_->max_balloon_count(), | 414 EXPECT_EQ(balloon_collection_->max_balloon_count(), |
415 balloon_collection_->count()); | 415 balloon_collection_->count()); |
416 EXPECT_EQ(expected_log, log_output_); | 416 EXPECT_EQ(expected_log, log_output_); |
417 | 417 |
418 // Cancel the notifications from the start; the balloon space should | 418 // Cancel the notifications from the start; the balloon space should |
419 // remain full. | 419 // remain full. |
420 { | 420 { |
421 int id; | 421 int id; |
422 for (id = 1; | 422 for (id = 1; |
423 id <= kLotsOfToasts - balloon_collection_->max_balloon_count(); | 423 id <= kLotsOfToasts - balloon_collection_->max_balloon_count(); |
424 ++id) { | 424 ++id) { |
425 service_->CancelDesktopNotification(process_id, route_id, id); | 425 service_->CancelDesktopNotification(process_id, route_id, id); |
426 MessageLoopForUI::current()->RunUntilIdle(); | 426 base::MessageLoopForUI::current()->RunUntilIdle(); |
427 expected_log.append("notification closed by script\n"); | 427 expected_log.append("notification closed by script\n"); |
428 expected_log.append("notification displayed\n"); | 428 expected_log.append("notification displayed\n"); |
429 EXPECT_EQ(balloon_collection_->max_balloon_count(), | 429 EXPECT_EQ(balloon_collection_->max_balloon_count(), |
430 balloon_collection_->count()); | 430 balloon_collection_->count()); |
431 EXPECT_EQ(expected_log, log_output_); | 431 EXPECT_EQ(expected_log, log_output_); |
432 } | 432 } |
433 | 433 |
434 // Now cancel the rest. It should empty the balloon space. | 434 // Now cancel the rest. It should empty the balloon space. |
435 for (; id <= kLotsOfToasts; ++id) { | 435 for (; id <= kLotsOfToasts; ++id) { |
436 service_->CancelDesktopNotification(process_id, route_id, id); | 436 service_->CancelDesktopNotification(process_id, route_id, id); |
437 expected_log.append("notification closed by script\n"); | 437 expected_log.append("notification closed by script\n"); |
438 MessageLoopForUI::current()->RunUntilIdle(); | 438 base::MessageLoopForUI::current()->RunUntilIdle(); |
439 EXPECT_EQ(expected_log, log_output_); | 439 EXPECT_EQ(expected_log, log_output_); |
440 } | 440 } |
441 } | 441 } |
442 | 442 |
443 // Verify that the balloon collection is now empty. | 443 // Verify that the balloon collection is now empty. |
444 EXPECT_EQ(0, balloon_collection_->count()); | 444 EXPECT_EQ(0, balloon_collection_->count()); |
445 } | 445 } |
446 | 446 |
447 TEST_F(DesktopNotificationsTest, TestEarlyDestruction) { | 447 TEST_F(DesktopNotificationsTest, TestEarlyDestruction) { |
448 // Create some toasts and then prematurely delete the notification service, | 448 // Create some toasts and then prematurely delete the notification service, |
(...skipping 12 matching lines...) Expand all Loading... |
461 // Create a test script with some HTML; assert that it doesn't get into the | 461 // Create a test script with some HTML; assert that it doesn't get into the |
462 // data:// URL that's produced for the balloon. | 462 // data:// URL that's produced for the balloon. |
463 content::ShowDesktopNotificationHostMsgParams params = | 463 content::ShowDesktopNotificationHostMsgParams params = |
464 StandardTestNotification(); | 464 StandardTestNotification(); |
465 params.title = ASCIIToUTF16("<script>window.alert('uh oh');</script>"); | 465 params.title = ASCIIToUTF16("<script>window.alert('uh oh');</script>"); |
466 params.body = ASCIIToUTF16("<i>this text is in italics</i>"); | 466 params.body = ASCIIToUTF16("<i>this text is in italics</i>"); |
467 params.notification_id = 1; | 467 params.notification_id = 1; |
468 EXPECT_TRUE(service_->ShowDesktopNotification( | 468 EXPECT_TRUE(service_->ShowDesktopNotification( |
469 params, 0, 0, DesktopNotificationService::PageNotification)); | 469 params, 0, 0, DesktopNotificationService::PageNotification)); |
470 | 470 |
471 MessageLoopForUI::current()->RunUntilIdle(); | 471 base::MessageLoopForUI::current()->RunUntilIdle(); |
472 EXPECT_EQ(1, balloon_collection_->count()); | 472 EXPECT_EQ(1, balloon_collection_->count()); |
473 Balloon* balloon = (*balloon_collection_->balloons().begin()); | 473 Balloon* balloon = (*balloon_collection_->balloons().begin()); |
474 GURL data_url = balloon->notification().content_url(); | 474 GURL data_url = balloon->notification().content_url(); |
475 EXPECT_EQ(std::string::npos, data_url.spec().find("<script>")); | 475 EXPECT_EQ(std::string::npos, data_url.spec().find("<script>")); |
476 EXPECT_EQ(std::string::npos, data_url.spec().find("<i>")); | 476 EXPECT_EQ(std::string::npos, data_url.spec().find("<i>")); |
477 // URL-encoded versions of tags should also not be found. | 477 // URL-encoded versions of tags should also not be found. |
478 EXPECT_EQ(std::string::npos, data_url.spec().find("%3cscript%3e")); | 478 EXPECT_EQ(std::string::npos, data_url.spec().find("%3cscript%3e")); |
479 EXPECT_EQ(std::string::npos, data_url.spec().find("%3ci%3e")); | 479 EXPECT_EQ(std::string::npos, data_url.spec().find("%3ci%3e")); |
480 } | 480 } |
481 | 481 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
580 } | 580 } |
581 | 581 |
582 // Now change the position to upper left. Confirm that the X value for the | 582 // Now change the position to upper left. Confirm that the X value for the |
583 // balloons gets smaller. | 583 // balloons gets smaller. |
584 local_state_.SetInteger(prefs::kDesktopNotificationPosition, | 584 local_state_.SetInteger(prefs::kDesktopNotificationPosition, |
585 BalloonCollection::UPPER_LEFT); | 585 BalloonCollection::UPPER_LEFT); |
586 | 586 |
587 int current_x = (*balloons.begin())->GetPosition().x(); | 587 int current_x = (*balloons.begin())->GetPosition().x(); |
588 EXPECT_LT(current_x, last_x); | 588 EXPECT_LT(current_x, last_x); |
589 } | 589 } |
OLD | NEW |