| 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/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/notifications/balloon_notification_ui_manager.h" | 10 #include "chrome/browser/notifications/balloon_notification_ui_manager.h" |
| 11 #include "chrome/browser/notifications/fake_balloon_view.h" | 11 #include "chrome/browser/notifications/fake_balloon_view.h" |
| 12 #include "chrome/browser/prefs/browser_prefs.h" | 12 #include "chrome/browser/prefs/browser_prefs.h" |
| 13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/test/base/testing_browser_process.h" | 14 #include "chrome/test/base/testing_browser_process.h" |
| 15 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "chrome/test/base/testing_profile_manager.h" | 16 #include "chrome/test/base/testing_profile_manager.h" |
| 17 #include "content/public/common/show_desktop_notification_params.h" | 17 #include "content/public/common/show_desktop_notification_params.h" |
| 18 #include "ui/base/ime/input_method_initializer.h" | 18 #include "ui/base/ime/input_method_initializer.h" |
| 19 #include "ui/message_center/message_center.h" | 19 #include "ui/message_center/message_center.h" |
| 20 | 20 |
| 21 #if defined(USE_ASH) | 21 #if defined(RUN_MESSAGE_CENTER_TESTS) |
| 22 #include "ash/shell.h" | 22 #include "chrome/browser/notifications/message_center_notification_manager.h" |
| 23 #include "ash/test/test_shell_delegate.h" | 23 #include "chrome/browser/notifications/message_center_settings_controller.h" |
| 24 #include "ui/aura/env.h" | 24 #include "chrome/browser/profiles/profile_manager.h" |
| 25 #include "ui/aura/root_window.h" | |
| 26 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | |
| 27 #endif | 25 #endif |
| 28 | 26 |
| 29 | |
| 30 using content::BrowserThread; | 27 using content::BrowserThread; |
| 31 | 28 |
| 32 // static | 29 // static |
| 33 const int MockBalloonCollection::kMockBalloonSpace = 5; | 30 std::string DesktopNotificationsTest::log_output_; |
| 31 |
| 32 #if !defined(RUN_MESSAGE_CENTER_TESTS) |
| 34 | 33 |
| 35 // static | 34 // static |
| 36 std::string DesktopNotificationsTest::log_output_; | 35 const int MockBalloonCollection::kMockBalloonSpace = 5; |
| 37 | 36 |
| 38 MockBalloonCollection::MockBalloonCollection() {} | 37 MockBalloonCollection::MockBalloonCollection() {} |
| 39 | 38 |
| 40 MockBalloonCollection::~MockBalloonCollection() {} | 39 MockBalloonCollection::~MockBalloonCollection() {} |
| 41 | 40 |
| 42 void MockBalloonCollection::Add(const Notification& notification, | 41 void MockBalloonCollection::Add(const Notification& notification, |
| 43 Profile* profile) { | 42 Profile* profile) { |
| 44 // Swap in a logging proxy for the purpose of logging calls that | 43 // Swap in a logging proxy for the purpose of logging calls that |
| 45 // would be made into javascript, then pass this down to the | 44 // would be made into javascript, then pass this down to the |
| 46 // balloon collection. | 45 // balloon collection. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 int min = 0; | 84 int min = 0; |
| 86 std::deque<Balloon*>::iterator iter; | 85 std::deque<Balloon*>::iterator iter; |
| 87 for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) { | 86 for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) { |
| 88 int pos = (*iter)->GetPosition().y(); | 87 int pos = (*iter)->GetPosition().y(); |
| 89 if (iter == balloons_.begin() || pos < min) | 88 if (iter == balloons_.begin() || pos < min) |
| 90 min = pos; | 89 min = pos; |
| 91 } | 90 } |
| 92 return min; | 91 return min; |
| 93 } | 92 } |
| 94 | 93 |
| 94 #else |
| 95 |
| 96 class TestMessageCenterNotificationManager : |
| 97 public MessageCenterNotificationManager { |
| 98 public: |
| 99 TestMessageCenterNotificationManager( |
| 100 message_center::MessageCenter* message_center, |
| 101 PrefService* local_state, |
| 102 scoped_ptr<message_center::NotifierSettingsProvider> settings_provider) |
| 103 : MessageCenterNotificationManager(message_center, |
| 104 local_state, |
| 105 settings_provider.Pass()) { |
| 106 } |
| 107 virtual ~TestMessageCenterNotificationManager() {} |
| 108 |
| 109 virtual bool ShowNotification(const Notification& notification, |
| 110 Profile* profile) OVERRIDE { |
| 111 // Swap in a logging proxy for the purpose of logging calls that |
| 112 // would be made into javascript, then pass this down. |
| 113 Notification test_notification( |
| 114 notification.origin_url(), |
| 115 notification.content_url(), |
| 116 notification.display_source(), |
| 117 notification.replace_id(), |
| 118 new LoggingNotificationProxy(notification.notification_id())); |
| 119 return MessageCenterNotificationManager::ShowNotification( |
| 120 test_notification, profile); |
| 121 } |
| 122 |
| 123 private: |
| 124 DISALLOW_COPY_AND_ASSIGN(TestMessageCenterNotificationManager); |
| 125 }; |
| 126 |
| 127 #endif // !defined(RUN_MESSAGE_CENTER_TESTS) |
| 128 |
| 95 DesktopNotificationsTest::DesktopNotificationsTest() | 129 DesktopNotificationsTest::DesktopNotificationsTest() |
| 96 : ui_thread_(BrowserThread::UI, &message_loop_) { | 130 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 131 aura_test_helper_(&message_loop_) { |
| 97 } | 132 } |
| 98 | 133 |
| 99 DesktopNotificationsTest::~DesktopNotificationsTest() { | 134 DesktopNotificationsTest::~DesktopNotificationsTest() { |
| 100 } | 135 } |
| 101 | 136 |
| 137 // static |
| 138 void DesktopNotificationsTest::log(const std::string& message) { |
| 139 log_output_.append(message); |
| 140 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 141 base::MessageLoop::current()->QuitClosure()); |
| 142 } |
| 143 |
| 102 void DesktopNotificationsTest::SetUp() { | 144 void DesktopNotificationsTest::SetUp() { |
| 103 ui::InitializeInputMethodForTesting(); | 145 ui::InitializeInputMethodForTesting(); |
| 104 #if defined(USE_ASH) | 146 |
| 105 ui::ScopedAnimationDurationScaleMode normal_duration_mode( | 147 #if defined(USE_AURA) |
| 106 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION); | 148 aura_test_helper_.SetUp(); |
| 149 #endif |
| 150 |
| 151 chrome::RegisterLocalState(local_state_.registry()); |
| 152 |
| 153 profile_manager_.reset( |
| 154 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 155 ASSERT_TRUE(profile_manager_->SetUp()); |
| 156 profile_ = profile_manager_->CreateTestingProfile("notifications_test"); |
| 157 |
| 158 #if defined(RUN_MESSAGE_CENTER_TESTS) |
| 107 // The message center is notmally initialized on |g_browser_process| which | 159 // The message center is notmally initialized on |g_browser_process| which |
| 108 // is not created for these tests. | 160 // is not created for these tests. |
| 109 message_center::MessageCenter::Initialize(); | 161 message_center::MessageCenter::Initialize(); |
| 110 // MockBalloonCollection retrieves information about the screen on creation. | 162 scoped_ptr<message_center::NotifierSettingsProvider> settings_provider( |
| 111 // So it is necessary to make sure the desktop gets created first. | 163 new MessageCenterSettingsController( |
| 112 ash::Shell::CreateInstance(new ash::test::TestShellDelegate); | 164 profile_manager_->profile_info_cache())); |
| 165 ui_manager_.reset( |
| 166 new TestMessageCenterNotificationManager( |
| 167 message_center::MessageCenter::Get(), &local_state_, |
| 168 settings_provider.Pass())); |
| 169 #else |
| 170 BalloonNotificationUIManager* balloon_ui_manager = |
| 171 new BalloonNotificationUIManager(&local_state_); |
| 172 ui_manager_.reset(balloon_ui_manager); |
| 173 balloon_collection_ = new MockBalloonCollection(); |
| 174 balloon_ui_manager->SetBalloonCollection(balloon_collection_); |
| 113 #endif | 175 #endif |
| 114 | 176 |
| 115 chrome::RegisterLocalState(local_state_.registry()); | |
| 116 profile_.reset(new TestingProfile()); | |
| 117 ui_manager_.reset(new BalloonNotificationUIManager(&local_state_)); | |
| 118 balloon_collection_ = new MockBalloonCollection(); | |
| 119 ui_manager_->SetBalloonCollection(balloon_collection_); | |
| 120 service_.reset(new DesktopNotificationService(profile(), ui_manager_.get())); | 177 service_.reset(new DesktopNotificationService(profile(), ui_manager_.get())); |
| 121 log_output_.clear(); | 178 log_output_.clear(); |
| 122 } | 179 } |
| 123 | 180 |
| 124 void DesktopNotificationsTest::TearDown() { | 181 void DesktopNotificationsTest::TearDown() { |
| 125 service_.reset(NULL); | 182 service_.reset(); |
| 126 ui_manager_.reset(NULL); | 183 ui_manager_.reset(); |
| 127 profile_.reset(NULL); | 184 profile_ = NULL; |
| 128 #if defined(USE_ASH) | 185 profile_manager_.reset(); |
| 129 ash::Shell::DeleteInstance(); | 186 #if defined(RUN_MESSAGE_CENTER_TESTS) |
| 130 // The message center is notmally shutdown on |g_browser_process| which | |
| 131 // is not created for these tests. | |
| 132 message_center::MessageCenter::Shutdown(); | 187 message_center::MessageCenter::Shutdown(); |
| 133 aura::Env::DeleteInstance(); | 188 #endif |
| 189 #if defined(USE_AURA) |
| 190 aura_test_helper_.TearDown(); |
| 134 #endif | 191 #endif |
| 135 ui::ShutdownInputMethodForTesting(); | 192 ui::ShutdownInputMethodForTesting(); |
| 136 } | 193 } |
| 137 | 194 |
| 195 int DesktopNotificationsTest::GetNotificationCount() { |
| 196 #if defined(RUN_MESSAGE_CENTER_TESTS) |
| 197 return message_center::MessageCenter::Get()->GetNotifications().size(); |
| 198 #else |
| 199 return balloon_collection_->count(); |
| 200 #endif |
| 201 } |
| 202 |
| 138 content::ShowDesktopNotificationHostMsgParams | 203 content::ShowDesktopNotificationHostMsgParams |
| 139 DesktopNotificationsTest::StandardTestNotification() { | 204 DesktopNotificationsTest::StandardTestNotification() { |
| 140 content::ShowDesktopNotificationHostMsgParams params; | 205 content::ShowDesktopNotificationHostMsgParams params; |
| 141 params.notification_id = 0; | 206 params.notification_id = 0; |
| 142 params.origin = GURL("http://www.google.com"); | 207 params.origin = GURL("http://www.google.com"); |
| 143 params.is_html = false; | 208 params.is_html = false; |
| 144 params.icon_url = GURL("/icon.png"); | 209 params.icon_url = GURL("/icon.png"); |
| 145 params.title = ASCIIToUTF16("Title"); | 210 params.title = ASCIIToUTF16("Title"); |
| 146 params.body = ASCIIToUTF16("Text"); | 211 params.body = ASCIIToUTF16("Text"); |
| 147 params.direction = WebKit::WebTextDirectionDefault; | 212 params.direction = WebKit::WebTextDirectionDefault; |
| 148 return params; | 213 return params; |
| 149 } | 214 } |
| 150 | 215 |
| 151 TEST_F(DesktopNotificationsTest, TestShow) { | 216 TEST_F(DesktopNotificationsTest, TestShow) { |
| 152 content::ShowDesktopNotificationHostMsgParams params = | 217 content::ShowDesktopNotificationHostMsgParams params = |
| 153 StandardTestNotification(); | 218 StandardTestNotification(); |
| 154 params.notification_id = 1; | 219 params.notification_id = 1; |
| 155 | 220 |
| 156 EXPECT_TRUE(service_->ShowDesktopNotification( | 221 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 157 params, 0, 0, DesktopNotificationService::PageNotification)); | 222 params, 0, 0, DesktopNotificationService::PageNotification)); |
| 158 base::MessageLoopForUI::current()->RunUntilIdle(); | 223 base::MessageLoopForUI::current()->Run(); |
| 159 EXPECT_EQ(1, balloon_collection_->count()); | 224 EXPECT_EQ(1, GetNotificationCount()); |
| 160 | 225 |
| 161 content::ShowDesktopNotificationHostMsgParams params2; | 226 content::ShowDesktopNotificationHostMsgParams params2; |
| 162 params2.origin = GURL("http://www.google.com"); | 227 params2.origin = GURL("http://www.google.com"); |
| 163 params2.is_html = true; | 228 params2.is_html = true; |
| 164 params2.contents_url = GURL("http://www.google.com/notification.html"); | 229 params2.contents_url = GURL("http://www.google.com/notification.html"); |
| 165 params2.notification_id = 2; | 230 params2.notification_id = 2; |
| 166 | 231 |
| 167 EXPECT_TRUE(service_->ShowDesktopNotification( | 232 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 168 params2, 0, 0, DesktopNotificationService::PageNotification)); | 233 params2, 0, 0, DesktopNotificationService::PageNotification)); |
| 169 base::MessageLoopForUI::current()->RunUntilIdle(); | 234 base::MessageLoopForUI::current()->Run(); |
| 170 EXPECT_EQ(2, balloon_collection_->count()); | 235 EXPECT_EQ(2, GetNotificationCount()); |
| 171 | 236 |
| 172 EXPECT_EQ("notification displayed\n" | 237 EXPECT_EQ("notification displayed\n" |
| 173 "notification displayed\n", | 238 "notification displayed\n", |
| 174 log_output_); | 239 log_output_); |
| 175 } | 240 } |
| 176 | 241 |
| 242 #if !defined(RUN_MESSAGE_CENTER_TESTS) |
| 177 TEST_F(DesktopNotificationsTest, TestClose) { | 243 TEST_F(DesktopNotificationsTest, TestClose) { |
| 178 content::ShowDesktopNotificationHostMsgParams params = | 244 content::ShowDesktopNotificationHostMsgParams params = |
| 179 StandardTestNotification(); | 245 StandardTestNotification(); |
| 180 params.notification_id = 1; | 246 params.notification_id = 1; |
| 181 | 247 |
| 182 // Request a notification; should open a balloon. | 248 // Request a notification; should open a balloon. |
| 183 EXPECT_TRUE(service_->ShowDesktopNotification( | 249 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 184 params, 0, 0, DesktopNotificationService::PageNotification)); | 250 params, 0, 0, DesktopNotificationService::PageNotification)); |
| 185 base::MessageLoopForUI::current()->RunUntilIdle(); | 251 base::MessageLoopForUI::current()->Run(); |
| 186 EXPECT_EQ(1, balloon_collection_->count()); | 252 EXPECT_EQ(1, balloon_collection_->count()); |
| 187 | 253 |
| 188 // Close all the open balloons. | 254 // Close all the open balloons. |
| 189 while (balloon_collection_->count() > 0) { | 255 while (balloon_collection_->count() > 0) { |
| 190 (*(balloon_collection_->GetActiveBalloons().begin()))->OnClose(true); | 256 (*(balloon_collection_->GetActiveBalloons().begin()))->OnClose(true); |
| 191 } | 257 } |
| 192 | 258 |
| 193 EXPECT_EQ("notification displayed\n" | 259 EXPECT_EQ("notification displayed\n" |
| 194 "notification closed by user\n", | 260 "notification closed by user\n", |
| 195 log_output_); | 261 log_output_); |
| 196 } | 262 } |
| 263 #endif // !defined(RUN_MESSAGE_CENTER_TESTS) |
| 197 | 264 |
| 198 TEST_F(DesktopNotificationsTest, TestCancel) { | 265 TEST_F(DesktopNotificationsTest, TestCancel) { |
| 199 int process_id = 0; | 266 int process_id = 0; |
| 200 int route_id = 0; | 267 int route_id = 0; |
| 201 int notification_id = 1; | 268 int notification_id = 1; |
| 202 | 269 |
| 203 content::ShowDesktopNotificationHostMsgParams params = | 270 content::ShowDesktopNotificationHostMsgParams params = |
| 204 StandardTestNotification(); | 271 StandardTestNotification(); |
| 205 params.notification_id = notification_id; | 272 params.notification_id = notification_id; |
| 206 | 273 |
| 207 // Request a notification; should open a balloon. | 274 // Request a notification; should open a balloon. |
| 208 EXPECT_TRUE(service_->ShowDesktopNotification( | 275 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 209 params, process_id, route_id, | 276 params, process_id, route_id, |
| 210 DesktopNotificationService::PageNotification)); | 277 DesktopNotificationService::PageNotification)); |
| 211 base::MessageLoopForUI::current()->RunUntilIdle(); | 278 base::MessageLoopForUI::current()->Run(); |
| 212 EXPECT_EQ(1, balloon_collection_->count()); | 279 EXPECT_EQ(1, GetNotificationCount()); |
| 213 | 280 |
| 214 // Cancel the same notification | 281 // Cancel the same notification |
| 215 service_->CancelDesktopNotification(process_id, | 282 service_->CancelDesktopNotification(process_id, |
| 216 route_id, | 283 route_id, |
| 217 notification_id); | 284 notification_id); |
| 218 base::MessageLoopForUI::current()->RunUntilIdle(); | 285 base::MessageLoopForUI::current()->Run(); |
| 219 // Verify that the balloon collection is now empty. | 286 // Verify that the balloon collection is now empty. |
| 220 EXPECT_EQ(0, balloon_collection_->count()); | 287 EXPECT_EQ(0, GetNotificationCount()); |
| 221 | 288 |
| 222 EXPECT_EQ("notification displayed\n" | 289 EXPECT_EQ("notification displayed\n" |
| 223 "notification closed by script\n", | 290 "notification closed by script\n", |
| 224 log_output_); | 291 log_output_); |
| 225 } | 292 } |
| 226 | 293 |
| 227 #if defined(OS_WIN) || defined(TOOLKIT_VIEWS) | 294 #if (defined(OS_WIN) || defined(TOOLKIT_VIEWS)) && \ |
| 295 !defined(RUN_MESSAGE_CENTER_TESTS) |
| 228 TEST_F(DesktopNotificationsTest, TestPositioning) { | 296 TEST_F(DesktopNotificationsTest, TestPositioning) { |
| 229 content::ShowDesktopNotificationHostMsgParams params = | 297 content::ShowDesktopNotificationHostMsgParams params = |
| 230 StandardTestNotification(); | 298 StandardTestNotification(); |
| 231 std::string expected_log; | 299 std::string expected_log; |
| 232 // Create some toasts. After each but the first, make sure there | 300 // Create some toasts. After each but the first, make sure there |
| 233 // is a minimum separation between the toasts. | 301 // is a minimum separation between the toasts. |
| 234 int last_top = 0; | 302 int last_top = 0; |
| 235 for (int id = 0; id <= 3; ++id) { | 303 for (int id = 0; id <= 3; ++id) { |
| 236 params.notification_id = id; | 304 params.notification_id = id; |
| 237 EXPECT_TRUE(service_->ShowDesktopNotification( | 305 EXPECT_TRUE(service_->ShowDesktopNotification( |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 } | 354 } |
| 287 } | 355 } |
| 288 EXPECT_EQ(expected_log, log_output_); | 356 EXPECT_EQ(expected_log, log_output_); |
| 289 } | 357 } |
| 290 #endif | 358 #endif |
| 291 | 359 |
| 292 TEST_F(DesktopNotificationsTest, TestCancelByProfile) { | 360 TEST_F(DesktopNotificationsTest, TestCancelByProfile) { |
| 293 int process_id = 0; | 361 int process_id = 0; |
| 294 int route_id = 0; | 362 int route_id = 0; |
| 295 | 363 |
| 296 TestingBrowserProcess* browser_process = | |
| 297 TestingBrowserProcess::GetGlobal(); | |
| 298 TestingProfileManager profile_manager(browser_process); | |
| 299 ASSERT_TRUE(profile_manager.SetUp()); | |
| 300 | |
| 301 TestingProfile* second_profile = | 364 TestingProfile* second_profile = |
| 302 profile_manager.CreateTestingProfile("SecondTestingProfile"); | 365 profile_manager_->CreateTestingProfile("SecondTestingProfile"); |
| 303 | 366 |
| 304 scoped_ptr<DesktopNotificationService> second_service( | 367 scoped_ptr<DesktopNotificationService> second_service( |
| 305 new DesktopNotificationService(second_profile, ui_manager_.get())); | 368 new DesktopNotificationService(second_profile, ui_manager_.get())); |
| 306 | 369 |
| 307 // Request lots of identical notifications. | 370 // Request lots of identical notifications. |
| 308 content::ShowDesktopNotificationHostMsgParams params = | 371 content::ShowDesktopNotificationHostMsgParams params = |
| 309 StandardTestNotification(); | 372 StandardTestNotification(); |
| 310 params.notification_id = 1; | 373 params.notification_id = 1; |
| 311 // Notice that the first one is the only one that doesn't use | 374 // Notice that the first one is the only one that doesn't use |
| 312 // the second profile. | 375 // the second profile. |
| 313 EXPECT_TRUE(service_->ShowDesktopNotification( | 376 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 314 params, process_id, route_id, | 377 params, process_id, route_id, |
| 315 DesktopNotificationService::PageNotification)); | 378 DesktopNotificationService::PageNotification)); |
| 316 | 379 |
| 317 // |kLotsOfToasts| must be large enough to trigger a resize of the underlying | 380 // |kLotsOfToasts| must be large enough to trigger a resize of the underlying |
| 318 // std::deque while we're clearing it. | 381 // std::deque while we're clearing it. |
| 319 const int kLotsOfToasts = 20; | 382 const int kLotsOfToasts = 20; |
| 320 for (int id = 2; id <= kLotsOfToasts; ++id) { | 383 for (int id = 2; id <= kLotsOfToasts; ++id) { |
| 321 params.notification_id = id; | 384 params.notification_id = id; |
| 322 EXPECT_TRUE(second_service->ShowDesktopNotification( | 385 EXPECT_TRUE(second_service->ShowDesktopNotification( |
| 323 params, process_id, route_id, | 386 params, process_id, route_id, |
| 324 DesktopNotificationService::PageNotification)); | 387 DesktopNotificationService::PageNotification)); |
| 325 } | 388 } |
| 326 base::MessageLoopForUI::current()->RunUntilIdle(); | 389 base::MessageLoopForUI::current()->Run(); |
| 327 | 390 |
| 328 ui_manager_->CancelAllByProfile(second_profile); | 391 ui_manager_->CancelAllByProfile(second_profile); |
| 329 | 392 |
| 330 // Verify that the balloon collection only contains the single | 393 // Verify that the balloon collection only contains the single |
| 331 // notification from the first profile. | 394 // notification from the first profile. |
| 332 EXPECT_EQ(1, balloon_collection_->count()); | 395 EXPECT_EQ(1, GetNotificationCount()); |
| 333 } | 396 } |
| 334 | 397 |
| 335 TEST_F(DesktopNotificationsTest, TestCancelBySourceOrigin) { | 398 TEST_F(DesktopNotificationsTest, TestCancelBySourceOrigin) { |
| 336 int process_id = 0; | 399 int process_id = 0; |
| 337 int route_id = 0; | 400 int route_id = 0; |
| 338 | 401 |
| 339 // Request lots of identical notifications. | 402 // Request lots of identical notifications. |
| 340 content::ShowDesktopNotificationHostMsgParams params = | 403 content::ShowDesktopNotificationHostMsgParams params = |
| 341 StandardTestNotification(); | 404 StandardTestNotification(); |
| 342 | 405 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 353 | 416 |
| 354 // |kLotsOfToasts| must be large enough to trigger a resize of the underlying | 417 // |kLotsOfToasts| must be large enough to trigger a resize of the underlying |
| 355 // std::deque while we're clearing it. | 418 // std::deque while we're clearing it. |
| 356 const int kLotsOfToasts = 20; | 419 const int kLotsOfToasts = 20; |
| 357 for (int id = 2; id <= kLotsOfToasts; ++id) { | 420 for (int id = 2; id <= kLotsOfToasts; ++id) { |
| 358 odd_params.notification_id = id; | 421 odd_params.notification_id = id; |
| 359 EXPECT_TRUE(service_->ShowDesktopNotification( | 422 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 360 odd_params, process_id, route_id, | 423 odd_params, process_id, route_id, |
| 361 DesktopNotificationService::PageNotification)); | 424 DesktopNotificationService::PageNotification)); |
| 362 } | 425 } |
| 363 base::MessageLoopForUI::current()->RunUntilIdle(); | 426 base::MessageLoopForUI::current()->Run(); |
| 364 | 427 |
| 365 ui_manager_->CancelAllBySourceOrigin(odd_params.origin); | 428 ui_manager_->CancelAllBySourceOrigin(odd_params.origin); |
| 366 | 429 |
| 367 // Verify that the balloon collection only contains the single | 430 // Verify that the balloon collection only contains the single |
| 368 // notification which is not from the canceled origin. | 431 // notification which is not from the canceled origin. |
| 369 EXPECT_EQ(1, balloon_collection_->count()); | 432 EXPECT_EQ(1, GetNotificationCount()); |
| 370 } | 433 } |
| 371 | 434 |
| 435 #if !defined(RUN_MESSAGE_CENTER_TESTS) |
| 372 TEST_F(DesktopNotificationsTest, TestQueueing) { | 436 TEST_F(DesktopNotificationsTest, TestQueueing) { |
| 373 int process_id = 0; | 437 int process_id = 0; |
| 374 int route_id = 0; | 438 int route_id = 0; |
| 375 | 439 |
| 376 // Request lots of identical notifications. | 440 // Request lots of identical notifications. |
| 377 content::ShowDesktopNotificationHostMsgParams params = | 441 content::ShowDesktopNotificationHostMsgParams params = |
| 378 StandardTestNotification(); | 442 StandardTestNotification(); |
| 379 const int kLotsOfToasts = 20; | 443 const int kLotsOfToasts = 20; |
| 380 for (int id = 1; id <= kLotsOfToasts; ++id) { | 444 for (int id = 1; id <= kLotsOfToasts; ++id) { |
| 381 params.notification_id = id; | 445 params.notification_id = id; |
| 382 EXPECT_TRUE(service_->ShowDesktopNotification( | 446 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 383 params, process_id, route_id, | 447 params, process_id, route_id, |
| 384 DesktopNotificationService::PageNotification)); | 448 DesktopNotificationService::PageNotification)); |
| 385 } | 449 } |
| 386 base::MessageLoopForUI::current()->RunUntilIdle(); | 450 base::MessageLoopForUI::current()->Run(); |
| 387 | 451 |
| 388 // Build up an expected log of what should be happening. | 452 // Build up an expected log of what should be happening. |
| 389 std::string expected_log; | 453 std::string expected_log; |
| 390 for (int i = 0; i < balloon_collection_->max_balloon_count(); ++i) { | 454 for (int i = 0; i < balloon_collection_->max_balloon_count(); ++i) { |
| 391 expected_log.append("notification displayed\n"); | 455 expected_log.append("notification displayed\n"); |
| 392 } | 456 } |
| 393 | 457 |
| 394 // The max number that our balloon collection can hold should be | 458 // The max number that our balloon collection can hold should be |
| 395 // shown. | 459 // shown. |
| 396 EXPECT_EQ(balloon_collection_->max_balloon_count(), | 460 EXPECT_EQ(balloon_collection_->max_balloon_count(), |
| 397 balloon_collection_->count()); | 461 balloon_collection_->count()); |
| 398 EXPECT_EQ(expected_log, log_output_); | 462 EXPECT_EQ(expected_log, log_output_); |
| 399 | 463 |
| 400 // Cancel the notifications from the start; the balloon space should | 464 // Cancel the notifications from the start; the balloon space should |
| 401 // remain full. | 465 // remain full. |
| 402 { | 466 { |
| 403 int id; | 467 int id; |
| 404 for (id = 1; | 468 for (id = 1; |
| 405 id <= kLotsOfToasts - balloon_collection_->max_balloon_count(); | 469 id <= kLotsOfToasts - balloon_collection_->max_balloon_count(); |
| 406 ++id) { | 470 ++id) { |
| 407 service_->CancelDesktopNotification(process_id, route_id, id); | 471 service_->CancelDesktopNotification(process_id, route_id, id); |
| 408 base::MessageLoopForUI::current()->RunUntilIdle(); | 472 base::MessageLoopForUI::current()->Run(); |
| 409 expected_log.append("notification closed by script\n"); | 473 expected_log.append("notification closed by script\n"); |
| 410 expected_log.append("notification displayed\n"); | 474 expected_log.append("notification displayed\n"); |
| 411 EXPECT_EQ(balloon_collection_->max_balloon_count(), | 475 EXPECT_EQ(balloon_collection_->max_balloon_count(), |
| 412 balloon_collection_->count()); | 476 balloon_collection_->count()); |
| 413 EXPECT_EQ(expected_log, log_output_); | 477 EXPECT_EQ(expected_log, log_output_); |
| 414 } | 478 } |
| 415 | 479 |
| 416 // Now cancel the rest. It should empty the balloon space. | 480 // Now cancel the rest. It should empty the balloon space. |
| 417 for (; id <= kLotsOfToasts; ++id) { | 481 for (; id <= kLotsOfToasts; ++id) { |
| 418 service_->CancelDesktopNotification(process_id, route_id, id); | 482 service_->CancelDesktopNotification(process_id, route_id, id); |
| 419 expected_log.append("notification closed by script\n"); | 483 expected_log.append("notification closed by script\n"); |
| 420 base::MessageLoopForUI::current()->RunUntilIdle(); | 484 base::MessageLoopForUI::current()->Run(); |
| 421 EXPECT_EQ(expected_log, log_output_); | 485 EXPECT_EQ(expected_log, log_output_); |
| 422 } | 486 } |
| 423 } | 487 } |
| 424 | 488 |
| 425 // Verify that the balloon collection is now empty. | 489 // Verify that the balloon collection is now empty. |
| 426 EXPECT_EQ(0, balloon_collection_->count()); | 490 EXPECT_EQ(0, balloon_collection_->count()); |
| 427 } | 491 } |
| 492 #endif // !defined(RUN_MESSAGE_CENTER_TESTS) |
| 428 | 493 |
| 429 TEST_F(DesktopNotificationsTest, TestEarlyDestruction) { | 494 TEST_F(DesktopNotificationsTest, TestEarlyDestruction) { |
| 430 // Create some toasts and then prematurely delete the notification service, | 495 // Create some toasts and then prematurely delete the notification service, |
| 431 // just to make sure nothing crashes/leaks. | 496 // just to make sure nothing crashes/leaks. |
| 432 content::ShowDesktopNotificationHostMsgParams params = | 497 content::ShowDesktopNotificationHostMsgParams params = |
| 433 StandardTestNotification(); | 498 StandardTestNotification(); |
| 434 for (int id = 0; id <= 3; ++id) { | 499 for (int id = 0; id <= 3; ++id) { |
| 435 params.notification_id = id; | 500 params.notification_id = id; |
| 436 EXPECT_TRUE(service_->ShowDesktopNotification( | 501 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 437 params, 0, 0, DesktopNotificationService::PageNotification)); | 502 params, 0, 0, DesktopNotificationService::PageNotification)); |
| 438 } | 503 } |
| 439 service_.reset(NULL); | 504 service_.reset(NULL); |
| 440 } | 505 } |
| 441 | 506 |
| 507 #if !defined(RUN_MESSAGE_CENTER_TESTS) |
| 508 |
| 442 TEST_F(DesktopNotificationsTest, TestUserInputEscaping) { | 509 TEST_F(DesktopNotificationsTest, TestUserInputEscaping) { |
| 443 // Create a test script with some HTML; assert that it doesn't get into the | 510 // Create a test script with some HTML; assert that it doesn't get into the |
| 444 // data:// URL that's produced for the balloon. | 511 // data:// URL that's produced for the balloon. |
| 445 content::ShowDesktopNotificationHostMsgParams params = | 512 content::ShowDesktopNotificationHostMsgParams params = |
| 446 StandardTestNotification(); | 513 StandardTestNotification(); |
| 447 params.title = ASCIIToUTF16("<script>window.alert('uh oh');</script>"); | 514 params.title = ASCIIToUTF16("<script>window.alert('uh oh');</script>"); |
| 448 params.body = ASCIIToUTF16("<i>this text is in italics</i>"); | 515 params.body = ASCIIToUTF16("<i>this text is in italics</i>"); |
| 449 params.notification_id = 1; | 516 params.notification_id = 1; |
| 450 EXPECT_TRUE(service_->ShowDesktopNotification( | 517 EXPECT_TRUE(service_->ShowDesktopNotification( |
| 451 params, 0, 0, DesktopNotificationService::PageNotification)); | 518 params, 0, 0, DesktopNotificationService::PageNotification)); |
| 452 | 519 |
| 453 base::MessageLoopForUI::current()->RunUntilIdle(); | 520 base::MessageLoopForUI::current()->Run(); |
| 454 EXPECT_EQ(1, balloon_collection_->count()); | 521 EXPECT_EQ(1, balloon_collection_->count()); |
| 455 Balloon* balloon = (*balloon_collection_->balloons().begin()); | 522 Balloon* balloon = (*balloon_collection_->balloons().begin()); |
| 456 GURL data_url = balloon->notification().content_url(); | 523 GURL data_url = balloon->notification().content_url(); |
| 457 EXPECT_EQ(std::string::npos, data_url.spec().find("<script>")); | 524 EXPECT_EQ(std::string::npos, data_url.spec().find("<script>")); |
| 458 EXPECT_EQ(std::string::npos, data_url.spec().find("<i>")); | 525 EXPECT_EQ(std::string::npos, data_url.spec().find("<i>")); |
| 459 // URL-encoded versions of tags should also not be found. | 526 // URL-encoded versions of tags should also not be found. |
| 460 EXPECT_EQ(std::string::npos, data_url.spec().find("%3cscript%3e")); | 527 EXPECT_EQ(std::string::npos, data_url.spec().find("%3cscript%3e")); |
| 461 EXPECT_EQ(std::string::npos, data_url.spec().find("%3ci%3e")); | 528 EXPECT_EQ(std::string::npos, data_url.spec().find("%3ci%3e")); |
| 462 } | 529 } |
| 463 | 530 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 } | 629 } |
| 563 | 630 |
| 564 // Now change the position to upper left. Confirm that the X value for the | 631 // Now change the position to upper left. Confirm that the X value for the |
| 565 // balloons gets smaller. | 632 // balloons gets smaller. |
| 566 local_state_.SetInteger(prefs::kDesktopNotificationPosition, | 633 local_state_.SetInteger(prefs::kDesktopNotificationPosition, |
| 567 BalloonCollection::UPPER_LEFT); | 634 BalloonCollection::UPPER_LEFT); |
| 568 | 635 |
| 569 int current_x = (*balloons.begin())->GetPosition().x(); | 636 int current_x = (*balloons.begin())->GetPosition().x(); |
| 570 EXPECT_LT(current_x, last_x); | 637 EXPECT_LT(current_x, last_x); |
| 571 } | 638 } |
| 639 |
| 640 #endif // !defined(RUN_MESSAGE_CENTER_TESTS) |
| OLD | NEW |