| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/prefs/browser_prefs.h" | 9 #include "chrome/browser/prefs/browser_prefs.h" |
| 10 #include "chrome/common/pref_names.h" | 10 #include "chrome/common/pref_names.h" |
| 11 #include "chrome/test/base/testing_pref_service.h" | 11 #include "chrome/test/base/testing_pref_service.h" |
| 12 #include "content/common/desktop_notification_messages.h" | 12 #include "content/public/common/show_desktop_notification_params.h" |
| 13 | 13 |
| 14 using content::BrowserThread; | 14 using content::BrowserThread; |
| 15 | 15 |
| 16 // static | 16 // static |
| 17 const int MockBalloonCollection::kMockBalloonSpace = 5; | 17 const int MockBalloonCollection::kMockBalloonSpace = 5; |
| 18 | 18 |
| 19 // static | 19 // static |
| 20 std::string DesktopNotificationsTest::log_output_; | 20 std::string DesktopNotificationsTest::log_output_; |
| 21 | 21 |
| 22 MockBalloonCollection::MockBalloonCollection() {} | 22 MockBalloonCollection::MockBalloonCollection() {} |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 service_.reset(new DesktopNotificationService(profile(), ui_manager_.get())); | 92 service_.reset(new DesktopNotificationService(profile(), ui_manager_.get())); |
| 93 log_output_.clear(); | 93 log_output_.clear(); |
| 94 } | 94 } |
| 95 | 95 |
| 96 void DesktopNotificationsTest::TearDown() { | 96 void DesktopNotificationsTest::TearDown() { |
| 97 service_.reset(NULL); | 97 service_.reset(NULL); |
| 98 ui_manager_.reset(NULL); | 98 ui_manager_.reset(NULL); |
| 99 profile_.reset(NULL); | 99 profile_.reset(NULL); |
| 100 } | 100 } |
| 101 | 101 |
| 102 DesktopNotificationHostMsg_Show_Params | 102 content::ShowDesktopNotificationHostMsgParams |
| 103 DesktopNotificationsTest::StandardTestNotification() { | 103 DesktopNotificationsTest::StandardTestNotification() { |
| 104 DesktopNotificationHostMsg_Show_Params params; | 104 content::ShowDesktopNotificationHostMsgParams params; |
| 105 params.notification_id = 0; | 105 params.notification_id = 0; |
| 106 params.origin = GURL("http://www.google.com"); | 106 params.origin = GURL("http://www.google.com"); |
| 107 params.is_html = false; | 107 params.is_html = false; |
| 108 params.icon_url = GURL("/icon.png"); | 108 params.icon_url = GURL("/icon.png"); |
| 109 params.title = ASCIIToUTF16("Title"); | 109 params.title = ASCIIToUTF16("Title"); |
| 110 params.body = ASCIIToUTF16("Text"); | 110 params.body = ASCIIToUTF16("Text"); |
| 111 params.direction = WebKit::WebTextDirectionDefault; | 111 params.direction = WebKit::WebTextDirectionDefault; |
| 112 return params; | 112 return params; |
| 113 } | 113 } |
| 114 | 114 |
| 115 TEST_F(DesktopNotificationsTest, TestShow) { | 115 TEST_F(DesktopNotificationsTest, TestShow) { |
| 116 DesktopNotificationHostMsg_Show_Params params = StandardTestNotification(); | 116 content::ShowDesktopNotificationHostMsgParams params = |
| 117 StandardTestNotification(); |
| 117 params.notification_id = 1; | 118 params.notification_id = 1; |
| 118 | 119 |
| 119 EXPECT_TRUE(service_->ShowDesktopNotification( | 120 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 120 params, 0, 0, DesktopNotificationService::PageNotification)); | 121 params, 0, 0, DesktopNotificationService::PageNotification)); |
| 121 MessageLoopForUI::current()->RunAllPending(); | 122 MessageLoopForUI::current()->RunAllPending(); |
| 122 EXPECT_EQ(1, balloon_collection_->count()); | 123 EXPECT_EQ(1, balloon_collection_->count()); |
| 123 | 124 |
| 124 DesktopNotificationHostMsg_Show_Params params2; | 125 content::ShowDesktopNotificationHostMsgParams params2; |
| 125 params2.origin = GURL("http://www.google.com"); | 126 params2.origin = GURL("http://www.google.com"); |
| 126 params2.is_html = true; | 127 params2.is_html = true; |
| 127 params2.contents_url = GURL("http://www.google.com/notification.html"); | 128 params2.contents_url = GURL("http://www.google.com/notification.html"); |
| 128 params2.notification_id = 2; | 129 params2.notification_id = 2; |
| 129 | 130 |
| 130 EXPECT_TRUE(service_->ShowDesktopNotification( | 131 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 131 params2, 0, 0, DesktopNotificationService::PageNotification)); | 132 params2, 0, 0, DesktopNotificationService::PageNotification)); |
| 132 MessageLoopForUI::current()->RunAllPending(); | 133 MessageLoopForUI::current()->RunAllPending(); |
| 133 EXPECT_EQ(2, balloon_collection_->count()); | 134 EXPECT_EQ(2, balloon_collection_->count()); |
| 134 | 135 |
| 135 EXPECT_EQ("notification displayed\n" | 136 EXPECT_EQ("notification displayed\n" |
| 136 "notification displayed\n", | 137 "notification displayed\n", |
| 137 log_output_); | 138 log_output_); |
| 138 } | 139 } |
| 139 | 140 |
| 140 TEST_F(DesktopNotificationsTest, TestClose) { | 141 TEST_F(DesktopNotificationsTest, TestClose) { |
| 141 DesktopNotificationHostMsg_Show_Params params = StandardTestNotification(); | 142 content::ShowDesktopNotificationHostMsgParams params = |
| 143 StandardTestNotification(); |
| 142 params.notification_id = 1; | 144 params.notification_id = 1; |
| 143 | 145 |
| 144 // Request a notification; should open a balloon. | 146 // Request a notification; should open a balloon. |
| 145 EXPECT_TRUE(service_->ShowDesktopNotification( | 147 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 146 params, 0, 0, DesktopNotificationService::PageNotification)); | 148 params, 0, 0, DesktopNotificationService::PageNotification)); |
| 147 MessageLoopForUI::current()->RunAllPending(); | 149 MessageLoopForUI::current()->RunAllPending(); |
| 148 EXPECT_EQ(1, balloon_collection_->count()); | 150 EXPECT_EQ(1, balloon_collection_->count()); |
| 149 | 151 |
| 150 // Close all the open balloons. | 152 // Close all the open balloons. |
| 151 while (balloon_collection_->count() > 0) { | 153 while (balloon_collection_->count() > 0) { |
| 152 (*(balloon_collection_->GetActiveBalloons().begin()))->OnClose(true); | 154 (*(balloon_collection_->GetActiveBalloons().begin()))->OnClose(true); |
| 153 } | 155 } |
| 154 | 156 |
| 155 EXPECT_EQ("notification displayed\n" | 157 EXPECT_EQ("notification displayed\n" |
| 156 "notification closed by user\n", | 158 "notification closed by user\n", |
| 157 log_output_); | 159 log_output_); |
| 158 } | 160 } |
| 159 | 161 |
| 160 TEST_F(DesktopNotificationsTest, TestCancel) { | 162 TEST_F(DesktopNotificationsTest, TestCancel) { |
| 161 int process_id = 0; | 163 int process_id = 0; |
| 162 int route_id = 0; | 164 int route_id = 0; |
| 163 int notification_id = 1; | 165 int notification_id = 1; |
| 164 | 166 |
| 165 DesktopNotificationHostMsg_Show_Params params = StandardTestNotification(); | 167 content::ShowDesktopNotificationHostMsgParams params = |
| 168 StandardTestNotification(); |
| 166 params.notification_id = notification_id; | 169 params.notification_id = notification_id; |
| 167 | 170 |
| 168 // Request a notification; should open a balloon. | 171 // Request a notification; should open a balloon. |
| 169 EXPECT_TRUE(service_->ShowDesktopNotification( | 172 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 170 params, process_id, route_id, | 173 params, process_id, route_id, |
| 171 DesktopNotificationService::PageNotification)); | 174 DesktopNotificationService::PageNotification)); |
| 172 MessageLoopForUI::current()->RunAllPending(); | 175 MessageLoopForUI::current()->RunAllPending(); |
| 173 EXPECT_EQ(1, balloon_collection_->count()); | 176 EXPECT_EQ(1, balloon_collection_->count()); |
| 174 | 177 |
| 175 // Cancel the same notification | 178 // Cancel the same notification |
| 176 service_->CancelDesktopNotification(process_id, | 179 service_->CancelDesktopNotification(process_id, |
| 177 route_id, | 180 route_id, |
| 178 notification_id); | 181 notification_id); |
| 179 MessageLoopForUI::current()->RunAllPending(); | 182 MessageLoopForUI::current()->RunAllPending(); |
| 180 // Verify that the balloon collection is now empty. | 183 // Verify that the balloon collection is now empty. |
| 181 EXPECT_EQ(0, balloon_collection_->count()); | 184 EXPECT_EQ(0, balloon_collection_->count()); |
| 182 | 185 |
| 183 EXPECT_EQ("notification displayed\n" | 186 EXPECT_EQ("notification displayed\n" |
| 184 "notification closed by script\n", | 187 "notification closed by script\n", |
| 185 log_output_); | 188 log_output_); |
| 186 } | 189 } |
| 187 | 190 |
| 188 #if defined(OS_WIN) || defined(TOOLKIT_VIEWS) | 191 #if defined(OS_WIN) || defined(TOOLKIT_VIEWS) |
| 189 TEST_F(DesktopNotificationsTest, TestPositioning) { | 192 TEST_F(DesktopNotificationsTest, TestPositioning) { |
| 190 DesktopNotificationHostMsg_Show_Params params = StandardTestNotification(); | 193 content::ShowDesktopNotificationHostMsgParams params = |
| 194 StandardTestNotification(); |
| 191 std::string expected_log; | 195 std::string expected_log; |
| 192 // Create some toasts. After each but the first, make sure there | 196 // Create some toasts. After each but the first, make sure there |
| 193 // is a minimum separation between the toasts. | 197 // is a minimum separation between the toasts. |
| 194 int last_top = 0; | 198 int last_top = 0; |
| 195 for (int id = 0; id <= 3; ++id) { | 199 for (int id = 0; id <= 3; ++id) { |
| 196 params.notification_id = id; | 200 params.notification_id = id; |
| 197 EXPECT_TRUE(service_->ShowDesktopNotification( | 201 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 198 params, 0, 0, DesktopNotificationService::PageNotification)); | 202 params, 0, 0, DesktopNotificationService::PageNotification)); |
| 199 expected_log.append("notification displayed\n"); | 203 expected_log.append("notification displayed\n"); |
| 200 int top = balloon_collection_->UppermostVerticalPosition(); | 204 int top = balloon_collection_->UppermostVerticalPosition(); |
| 201 if (id > 0) | 205 if (id > 0) |
| 202 EXPECT_LE(top, last_top - balloon_collection_->MinHeight()); | 206 EXPECT_LE(top, last_top - balloon_collection_->MinHeight()); |
| 203 last_top = top; | 207 last_top = top; |
| 204 } | 208 } |
| 205 | 209 |
| 206 EXPECT_EQ(expected_log, log_output_); | 210 EXPECT_EQ(expected_log, log_output_); |
| 207 } | 211 } |
| 208 | 212 |
| 209 TEST_F(DesktopNotificationsTest, TestVariableSize) { | 213 TEST_F(DesktopNotificationsTest, TestVariableSize) { |
| 210 DesktopNotificationHostMsg_Show_Params params; | 214 content::ShowDesktopNotificationHostMsgParams params; |
| 211 params.origin = GURL("http://long.google.com"); | 215 params.origin = GURL("http://long.google.com"); |
| 212 params.is_html = false; | 216 params.is_html = false; |
| 213 params.icon_url = GURL("/icon.png"); | 217 params.icon_url = GURL("/icon.png"); |
| 214 params.title = ASCIIToUTF16("Really Really Really Really Really Really " | 218 params.title = ASCIIToUTF16("Really Really Really Really Really Really " |
| 215 "Really Really Really Really Really Really " | 219 "Really Really Really Really Really Really " |
| 216 "Really Really Really Really Really Really " | 220 "Really Really Really Really Really Really " |
| 217 "Really Long Title"), | 221 "Really Long Title"), |
| 218 params.body = ASCIIToUTF16("Text"); | 222 params.body = ASCIIToUTF16("Text"); |
| 219 params.notification_id = 0; | 223 params.notification_id = 0; |
| 220 | 224 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 247 } | 251 } |
| 248 EXPECT_EQ(expected_log, log_output_); | 252 EXPECT_EQ(expected_log, log_output_); |
| 249 } | 253 } |
| 250 #endif | 254 #endif |
| 251 | 255 |
| 252 TEST_F(DesktopNotificationsTest, TestQueueing) { | 256 TEST_F(DesktopNotificationsTest, TestQueueing) { |
| 253 int process_id = 0; | 257 int process_id = 0; |
| 254 int route_id = 0; | 258 int route_id = 0; |
| 255 | 259 |
| 256 // Request lots of identical notifications. | 260 // Request lots of identical notifications. |
| 257 DesktopNotificationHostMsg_Show_Params params = StandardTestNotification(); | 261 content::ShowDesktopNotificationHostMsgParams params = |
| 262 StandardTestNotification(); |
| 258 const int kLotsOfToasts = 20; | 263 const int kLotsOfToasts = 20; |
| 259 for (int id = 1; id <= kLotsOfToasts; ++id) { | 264 for (int id = 1; id <= kLotsOfToasts; ++id) { |
| 260 params.notification_id = id; | 265 params.notification_id = id; |
| 261 EXPECT_TRUE(service_->ShowDesktopNotification( | 266 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 262 params, process_id, route_id, | 267 params, process_id, route_id, |
| 263 DesktopNotificationService::PageNotification)); | 268 DesktopNotificationService::PageNotification)); |
| 264 } | 269 } |
| 265 MessageLoopForUI::current()->RunAllPending(); | 270 MessageLoopForUI::current()->RunAllPending(); |
| 266 | 271 |
| 267 // Build up an expected log of what should be happening. | 272 // Build up an expected log of what should be happening. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 } | 306 } |
| 302 } | 307 } |
| 303 | 308 |
| 304 // Verify that the balloon collection is now empty. | 309 // Verify that the balloon collection is now empty. |
| 305 EXPECT_EQ(0, balloon_collection_->count()); | 310 EXPECT_EQ(0, balloon_collection_->count()); |
| 306 } | 311 } |
| 307 | 312 |
| 308 TEST_F(DesktopNotificationsTest, TestEarlyDestruction) { | 313 TEST_F(DesktopNotificationsTest, TestEarlyDestruction) { |
| 309 // Create some toasts and then prematurely delete the notification service, | 314 // Create some toasts and then prematurely delete the notification service, |
| 310 // just to make sure nothing crashes/leaks. | 315 // just to make sure nothing crashes/leaks. |
| 311 DesktopNotificationHostMsg_Show_Params params = StandardTestNotification(); | 316 content::ShowDesktopNotificationHostMsgParams params = |
| 317 StandardTestNotification(); |
| 312 for (int id = 0; id <= 3; ++id) { | 318 for (int id = 0; id <= 3; ++id) { |
| 313 params.notification_id = id; | 319 params.notification_id = id; |
| 314 EXPECT_TRUE(service_->ShowDesktopNotification( | 320 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 315 params, 0, 0, DesktopNotificationService::PageNotification)); | 321 params, 0, 0, DesktopNotificationService::PageNotification)); |
| 316 } | 322 } |
| 317 service_.reset(NULL); | 323 service_.reset(NULL); |
| 318 } | 324 } |
| 319 | 325 |
| 320 TEST_F(DesktopNotificationsTest, TestUserInputEscaping) { | 326 TEST_F(DesktopNotificationsTest, TestUserInputEscaping) { |
| 321 // Create a test script with some HTML; assert that it doesn't get into the | 327 // Create a test script with some HTML; assert that it doesn't get into the |
| 322 // data:// URL that's produced for the balloon. | 328 // data:// URL that's produced for the balloon. |
| 323 DesktopNotificationHostMsg_Show_Params params = StandardTestNotification(); | 329 content::ShowDesktopNotificationHostMsgParams params = |
| 330 StandardTestNotification(); |
| 324 params.title = ASCIIToUTF16("<script>window.alert('uh oh');</script>"); | 331 params.title = ASCIIToUTF16("<script>window.alert('uh oh');</script>"); |
| 325 params.body = ASCIIToUTF16("<i>this text is in italics</i>"); | 332 params.body = ASCIIToUTF16("<i>this text is in italics</i>"); |
| 326 params.notification_id = 1; | 333 params.notification_id = 1; |
| 327 EXPECT_TRUE(service_->ShowDesktopNotification( | 334 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 328 params, 0, 0, DesktopNotificationService::PageNotification)); | 335 params, 0, 0, DesktopNotificationService::PageNotification)); |
| 329 | 336 |
| 330 MessageLoopForUI::current()->RunAllPending(); | 337 MessageLoopForUI::current()->RunAllPending(); |
| 331 EXPECT_EQ(1, balloon_collection_->count()); | 338 EXPECT_EQ(1, balloon_collection_->count()); |
| 332 Balloon* balloon = (*balloon_collection_->balloons().begin()); | 339 Balloon* balloon = (*balloon_collection_->balloons().begin()); |
| 333 GURL data_url = balloon->notification().content_url(); | 340 GURL data_url = balloon->notification().content_url(); |
| 334 EXPECT_EQ(std::string::npos, data_url.spec().find("<script>")); | 341 EXPECT_EQ(std::string::npos, data_url.spec().find("<script>")); |
| 335 EXPECT_EQ(std::string::npos, data_url.spec().find("<i>")); | 342 EXPECT_EQ(std::string::npos, data_url.spec().find("<i>")); |
| 336 // URL-encoded versions of tags should also not be found. | 343 // URL-encoded versions of tags should also not be found. |
| 337 EXPECT_EQ(std::string::npos, data_url.spec().find("%3cscript%3e")); | 344 EXPECT_EQ(std::string::npos, data_url.spec().find("%3cscript%3e")); |
| 338 EXPECT_EQ(std::string::npos, data_url.spec().find("%3ci%3e")); | 345 EXPECT_EQ(std::string::npos, data_url.spec().find("%3ci%3e")); |
| 339 } | 346 } |
| 340 | 347 |
| 341 TEST_F(DesktopNotificationsTest, TestBoundingBox) { | 348 TEST_F(DesktopNotificationsTest, TestBoundingBox) { |
| 342 // Create some notifications. | 349 // Create some notifications. |
| 343 DesktopNotificationHostMsg_Show_Params params = StandardTestNotification(); | 350 content::ShowDesktopNotificationHostMsgParams params = |
| 351 StandardTestNotification(); |
| 344 for (int id = 0; id <= 3; ++id) { | 352 for (int id = 0; id <= 3; ++id) { |
| 345 params.notification_id = id; | 353 params.notification_id = id; |
| 346 EXPECT_TRUE(service_->ShowDesktopNotification( | 354 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 347 params, 0, 0, DesktopNotificationService::PageNotification)); | 355 params, 0, 0, DesktopNotificationService::PageNotification)); |
| 348 } | 356 } |
| 349 | 357 |
| 350 gfx::Rect box = balloon_collection_->GetBalloonsBoundingBox(); | 358 gfx::Rect box = balloon_collection_->GetBalloonsBoundingBox(); |
| 351 | 359 |
| 352 // Try this for all positions. | 360 // Try this for all positions. |
| 353 BalloonCollection::PositionPreference pref; | 361 BalloonCollection::PositionPreference pref; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 370 } | 378 } |
| 371 } | 379 } |
| 372 } | 380 } |
| 373 | 381 |
| 374 TEST_F(DesktopNotificationsTest, TestPositionPreference) { | 382 TEST_F(DesktopNotificationsTest, TestPositionPreference) { |
| 375 // Set position preference to lower right. | 383 // Set position preference to lower right. |
| 376 local_state_.SetInteger(prefs::kDesktopNotificationPosition, | 384 local_state_.SetInteger(prefs::kDesktopNotificationPosition, |
| 377 BalloonCollection::LOWER_RIGHT); | 385 BalloonCollection::LOWER_RIGHT); |
| 378 | 386 |
| 379 // Create some notifications. | 387 // Create some notifications. |
| 380 DesktopNotificationHostMsg_Show_Params params = StandardTestNotification(); | 388 content::ShowDesktopNotificationHostMsgParams params = |
| 389 StandardTestNotification(); |
| 381 for (int id = 0; id <= 3; ++id) { | 390 for (int id = 0; id <= 3; ++id) { |
| 382 params.notification_id = id; | 391 params.notification_id = id; |
| 383 EXPECT_TRUE(service_->ShowDesktopNotification( | 392 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 384 params, 0, 0, DesktopNotificationService::PageNotification)); | 393 params, 0, 0, DesktopNotificationService::PageNotification)); |
| 385 } | 394 } |
| 386 | 395 |
| 387 std::deque<Balloon*>& balloons = balloon_collection_->balloons(); | 396 std::deque<Balloon*>& balloons = balloon_collection_->balloons(); |
| 388 std::deque<Balloon*>::iterator iter; | 397 std::deque<Balloon*>::iterator iter; |
| 389 | 398 |
| 390 // Check that they decrease in y-position (for MAC, with reversed | 399 // Check that they decrease in y-position (for MAC, with reversed |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 } | 446 } |
| 438 | 447 |
| 439 // Now change the position to upper left. Confirm that the X value for the | 448 // Now change the position to upper left. Confirm that the X value for the |
| 440 // balloons gets smaller. | 449 // balloons gets smaller. |
| 441 local_state_.SetInteger(prefs::kDesktopNotificationPosition, | 450 local_state_.SetInteger(prefs::kDesktopNotificationPosition, |
| 442 BalloonCollection::UPPER_LEFT); | 451 BalloonCollection::UPPER_LEFT); |
| 443 | 452 |
| 444 int current_x = (*balloons.begin())->GetPosition().x(); | 453 int current_x = (*balloons.begin())->GetPosition().x(); |
| 445 EXPECT_LT(current_x, last_x); | 454 EXPECT_LT(current_x, last_x); |
| 446 } | 455 } |
| OLD | NEW |