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 <deque> |
| 6 #include <string> |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "base/callback.h" |
| 10 #include "base/compiler_specific.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "base/stringprintf.h" |
| 14 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" |
| 16 #include "chrome/browser/api/infobars/infobar_delegate.h" |
| 17 #include "chrome/browser/browser_process.h" |
5 #include "chrome/browser/infobars/infobar_tab_helper.h" | 18 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 19 #include "chrome/browser/notifications/balloon.h" |
| 20 #include "chrome/browser/notifications/balloon_collection.h" |
| 21 #include "chrome/browser/notifications/balloon_host.h" |
| 22 #include "chrome/browser/notifications/desktop_notification_service.h" |
| 23 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
| 24 #include "chrome/browser/notifications/notification.h" |
| 25 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 26 #include "chrome/browser/profiles/profile.h" |
6 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
7 #include "chrome/browser/ui/browser_tabstrip.h" | 28 #include "chrome/browser/ui/browser_tabstrip.h" |
| 29 #include "chrome/browser/ui/browser_window.h" |
| 30 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 31 #include "chrome/common/chrome_notification_types.h" |
| 32 #include "chrome/common/content_settings.h" |
| 33 #include "chrome/common/content_settings_pattern.h" |
8 #include "chrome/test/base/in_process_browser_test.h" | 34 #include "chrome/test/base/in_process_browser_test.h" |
9 #include "chrome/test/base/ui_test_utils.h" | 35 #include "chrome/test/base/ui_test_utils.h" |
| 36 #include "content/public/browser/notification_service.h" |
| 37 #include "content/public/browser/notification_source.h" |
| 38 #include "content/public/browser/notification_types.h" |
| 39 #include "content/public/browser/render_view_host.h" |
10 #include "content/public/browser/web_contents.h" | 40 #include "content/public/browser/web_contents.h" |
11 #include "content/public/test/browser_test_utils.h" | 41 #include "content/public/test/browser_test_utils.h" |
| 42 #include "content/public/test/test_utils.h" |
| 43 #include "googleurl/src/gurl.h" |
12 #include "net/base/net_util.h" | 44 #include "net/base/net_util.h" |
13 #include "net/test/test_server.h" | 45 #include "net/test/test_server.h" |
14 | 46 #include "testing/gtest/include/gtest/gtest.h" |
15 class NotificationsPermissionTest : public InProcessBrowserTest { | 47 #include "webkit/glue/window_open_disposition.h" |
| 48 |
| 49 namespace { |
| 50 |
| 51 const char kExpectedIconUrl[] = "files/notifications/no_such_file.png"; |
| 52 |
| 53 enum InfobarAction { |
| 54 DISMISS = 0, |
| 55 ALLOW, |
| 56 DENY, |
| 57 }; |
| 58 |
| 59 class NotificationBalloonChangeObserver : public content::NotificationObserver { |
16 public: | 60 public: |
17 NotificationsPermissionTest() {} | 61 NotificationBalloonChangeObserver() |
| 62 : collection_( |
| 63 g_browser_process->notification_ui_manager()->balloon_collection()), |
| 64 collection_changed_(false), |
| 65 notification_received_(false), |
| 66 running_(false), |
| 67 done_(false) { |
| 68 registrar_.Add(this, chrome::NOTIFICATION_NOTIFY_BALLOON_CONNECTED, |
| 69 content::NotificationService::AllSources()); |
| 70 registrar_.Add(this, chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED, |
| 71 content::NotificationService::AllSources()); |
| 72 collection_->set_on_collection_changed_callback( |
| 73 base::Bind(&NotificationBalloonChangeObserver::OnCollectionChanged, |
| 74 base::Unretained(this))); |
| 75 } |
| 76 |
| 77 virtual ~NotificationBalloonChangeObserver() { |
| 78 collection_->set_on_collection_changed_callback(base::Closure()); |
| 79 } |
| 80 |
| 81 bool Wait() { |
| 82 if (!Check()) { |
| 83 running_ = true; |
| 84 message_loop_runner_ = new content::MessageLoopRunner; |
| 85 message_loop_runner_->Run(); |
| 86 EXPECT_TRUE(done_); |
| 87 } |
| 88 return done_; |
| 89 } |
| 90 |
| 91 bool Check() { |
| 92 if (done_) |
| 93 return true; |
| 94 |
| 95 if (collection_changed_ && notification_received_) { |
| 96 done_ = true; |
| 97 if (running_) { |
| 98 message_loop_runner_->Quit(); |
| 99 running_ = false; |
| 100 } |
| 101 } |
| 102 return done_; |
| 103 } |
| 104 |
| 105 void OnCollectionChanged() { |
| 106 collection_changed_ = true; |
| 107 Check(); |
| 108 } |
| 109 |
| 110 // Overridden from content::NotificationObserver: |
| 111 virtual void Observe(int type, |
| 112 const content::NotificationSource& source, |
| 113 const content::NotificationDetails& details) OVERRIDE { |
| 114 DCHECK(type == chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED || |
| 115 type == chrome::NOTIFICATION_NOTIFY_BALLOON_CONNECTED); |
| 116 notification_received_ = true; |
| 117 Check(); |
| 118 } |
| 119 |
| 120 private: |
| 121 content::NotificationRegistrar registrar_; |
| 122 BalloonCollection* collection_; |
| 123 |
| 124 bool collection_changed_; |
| 125 bool notification_received_; |
| 126 bool running_; |
| 127 bool done_; |
| 128 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 129 |
| 130 DISALLOW_COPY_AND_ASSIGN(NotificationBalloonChangeObserver); |
18 }; | 131 }; |
19 | 132 |
| 133 } // namespace |
| 134 |
| 135 class NotificationsTest : public InProcessBrowserTest { |
| 136 public: |
| 137 NotificationsTest() {} |
| 138 |
| 139 protected: |
| 140 // Overriden from InProcessBrowserTest: |
| 141 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; |
| 142 |
| 143 const std::deque<Balloon*>& GetActiveBalloons(); |
| 144 int GetNotificationCount(); |
| 145 |
| 146 bool CloseNotificationAndWait(const Notification& notification); |
| 147 void CloseBrowserWindow(Browser* browser); |
| 148 void CrashTab(Browser* browser, int index); |
| 149 void CrashNotification(Balloon* balloon); |
| 150 |
| 151 void SetDefaultPermissionSetting(ContentSetting setting); |
| 152 void DenyOrigin(const GURL& origin); |
| 153 void AllowOrigin(const GURL& origin); |
| 154 void AllowAllOrigins(); |
| 155 |
| 156 void VerifyInfobar(const Browser* browser, int index); |
| 157 std::string CreateNotification(Browser* browser, |
| 158 bool wait_for_new_balloon, |
| 159 const char* icon, |
| 160 const char* title, |
| 161 const char* body, |
| 162 const char* replace_id); |
| 163 std::string CreateSimpleNotification(Browser* browser, |
| 164 bool wait_for_new_balloon); |
| 165 bool RequestPermissionAndWait(Browser* browser); |
| 166 bool CancelNotification(const char* notification_id, Browser* browser); |
| 167 bool PerformActionOnInfobar(Browser* browser, |
| 168 InfobarAction action, |
| 169 int infobar_index, |
| 170 int tab_index); |
| 171 void GetPrefsByContentSetting(ContentSetting setting, |
| 172 ContentSettingsForOneType* settings); |
| 173 bool CheckOriginInSetting(const ContentSettingsForOneType& settings, |
| 174 const GURL& origin); |
| 175 |
| 176 GURL empty_page_url_; |
| 177 GURL test_page_url_; |
| 178 |
| 179 private: |
| 180 void DropOriginPreference(const GURL& origin); |
| 181 DesktopNotificationService* GetDesktopNotificationService(); |
| 182 }; |
| 183 |
| 184 void NotificationsTest::SetUpInProcessBrowserTestFixture() { |
| 185 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 186 |
| 187 ASSERT_TRUE(test_server()->Start()); |
| 188 empty_page_url_ = test_server()->GetURL("files/empty.html"); |
| 189 test_page_url_ = test_server()->GetURL( |
| 190 "files/notifications/notification_tester.html"); |
| 191 } |
| 192 |
| 193 const std::deque<Balloon*>& NotificationsTest::GetActiveBalloons() { |
| 194 return g_browser_process->notification_ui_manager()-> |
| 195 balloon_collection()->GetActiveBalloons(); |
| 196 } |
| 197 |
| 198 int NotificationsTest::GetNotificationCount() { |
| 199 return g_browser_process->notification_ui_manager()-> |
| 200 balloon_collection()->GetActiveBalloons().size(); |
| 201 } |
| 202 |
| 203 bool NotificationsTest::CloseNotificationAndWait( |
| 204 const Notification& notification) { |
| 205 NotificationBalloonChangeObserver observer; |
| 206 bool success = g_browser_process->notification_ui_manager()-> |
| 207 CancelById(notification.notification_id()); |
| 208 if (success) |
| 209 return observer.Wait(); |
| 210 return false; |
| 211 } |
| 212 |
| 213 void NotificationsTest::CloseBrowserWindow(Browser* browser) { |
| 214 content::WindowedNotificationObserver observer( |
| 215 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 216 content::Source<Browser>(browser)); |
| 217 browser->window()->Close(); |
| 218 observer.Wait(); |
| 219 } |
| 220 |
| 221 void NotificationsTest::CrashTab(Browser* browser, int index) { |
| 222 content::CrashTab(chrome::GetWebContentsAt(browser, index)); |
| 223 } |
| 224 |
| 225 void NotificationsTest::CrashNotification(Balloon* balloon) { |
| 226 content::CrashTab(balloon->balloon_view()->GetHost()->web_contents()); |
| 227 } |
| 228 |
| 229 void NotificationsTest::SetDefaultPermissionSetting(ContentSetting setting) { |
| 230 DesktopNotificationService* service = GetDesktopNotificationService(); |
| 231 service->SetDefaultContentSetting(setting); |
| 232 } |
| 233 |
| 234 void NotificationsTest::DenyOrigin(const GURL& origin) { |
| 235 DropOriginPreference(origin); |
| 236 GetDesktopNotificationService()->DenyPermission(origin); |
| 237 } |
| 238 |
| 239 void NotificationsTest::AllowOrigin(const GURL& origin) { |
| 240 DropOriginPreference(origin); |
| 241 GetDesktopNotificationService()->GrantPermission(origin); |
| 242 } |
| 243 |
| 244 void NotificationsTest::AllowAllOrigins() { |
| 245 GetDesktopNotificationService()->ResetAllOrigins(); |
| 246 GetDesktopNotificationService()->SetDefaultContentSetting( |
| 247 CONTENT_SETTING_ALLOW); |
| 248 } |
| 249 |
| 250 void NotificationsTest::VerifyInfobar(const Browser* browser, int index) { |
| 251 InfoBarTabHelper* infobar_helper = InfoBarTabHelper::FromWebContents( |
| 252 chrome::GetWebContentsAt(browser, index)); |
| 253 |
| 254 ASSERT_EQ(1U, infobar_helper->GetInfoBarCount()); |
| 255 InfoBarDelegate* infobar = infobar_helper->GetInfoBarDelegateAt(0); |
| 256 ConfirmInfoBarDelegate* confirm_infobar = infobar->AsConfirmInfoBarDelegate(); |
| 257 ASSERT_TRUE(confirm_infobar); |
| 258 int buttons = confirm_infobar->GetButtons(); |
| 259 EXPECT_TRUE(buttons & ConfirmInfoBarDelegate::BUTTON_OK); |
| 260 EXPECT_TRUE(buttons & ConfirmInfoBarDelegate::BUTTON_CANCEL); |
| 261 } |
| 262 |
| 263 std::string NotificationsTest::CreateNotification( |
| 264 Browser* browser, |
| 265 bool wait_for_new_balloon, |
| 266 const char* icon, |
| 267 const char* title, |
| 268 const char* body, |
| 269 const char* replace_id) { |
| 270 std::string script = base::StringPrintf( |
| 271 "createNotification('%s', '%s', '%s', '%s');", |
| 272 icon, title, body, replace_id); |
| 273 |
| 274 NotificationBalloonChangeObserver observer; |
| 275 std::string result; |
| 276 bool success = content::ExecuteJavaScriptAndExtractString( |
| 277 chrome::GetActiveWebContents(browser)->GetRenderViewHost(), |
| 278 L"", |
| 279 UTF8ToWide(script), |
| 280 &result); |
| 281 if (success && result != "-1" && wait_for_new_balloon) |
| 282 success = observer.Wait(); |
| 283 EXPECT_TRUE(success); |
| 284 |
| 285 return result; |
| 286 } |
| 287 |
| 288 std::string NotificationsTest::CreateSimpleNotification( |
| 289 Browser* browser, |
| 290 bool wait_for_new_balloon) { |
| 291 return CreateNotification( |
| 292 browser, wait_for_new_balloon, |
| 293 "no_such_file.png", "My Title", "My Body", ""); |
| 294 } |
| 295 |
| 296 bool NotificationsTest::RequestPermissionAndWait(Browser* browser) { |
| 297 InfoBarTabHelper* infobar_helper = InfoBarTabHelper::FromWebContents( |
| 298 chrome::GetActiveWebContents(browser)); |
| 299 content::WindowedNotificationObserver observer( |
| 300 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, |
| 301 content::Source<InfoBarTabHelper>(infobar_helper)); |
| 302 std::string result; |
| 303 bool success = content::ExecuteJavaScriptAndExtractString( |
| 304 chrome::GetActiveWebContents(browser)->GetRenderViewHost(), |
| 305 L"", |
| 306 L"requestPermission();", |
| 307 &result); |
| 308 if (!success || result != "1") |
| 309 return false; |
| 310 observer.Wait(); |
| 311 return true; |
| 312 } |
| 313 |
| 314 bool NotificationsTest::CancelNotification( |
| 315 const char* notification_id, |
| 316 Browser* browser) { |
| 317 std::string script = base::StringPrintf( |
| 318 "cancelNotification('%s');", |
| 319 notification_id); |
| 320 |
| 321 NotificationBalloonChangeObserver observer; |
| 322 std::string result; |
| 323 bool success = content::ExecuteJavaScriptAndExtractString( |
| 324 chrome::GetActiveWebContents(browser)->GetRenderViewHost(), |
| 325 L"", |
| 326 UTF8ToWide(script), |
| 327 &result); |
| 328 if (!success || result != "1") |
| 329 return false; |
| 330 return observer.Wait(); |
| 331 } |
| 332 |
| 333 bool NotificationsTest::PerformActionOnInfobar( |
| 334 Browser* browser, |
| 335 InfobarAction action, |
| 336 int infobar_index, |
| 337 int tab_index) { |
| 338 InfoBarTabHelper* infobar_helper = InfoBarTabHelper::FromWebContents( |
| 339 chrome::GetWebContentsAt(browser, tab_index)); |
| 340 |
| 341 InfoBarDelegate* infobar = infobar_helper->GetInfoBarDelegateAt( |
| 342 infobar_index); |
| 343 switch (action) { |
| 344 case DISMISS: { |
| 345 infobar->InfoBarDismissed(); |
| 346 infobar_helper->RemoveInfoBar(infobar); |
| 347 return true; |
| 348 } |
| 349 case ALLOW: { |
| 350 ConfirmInfoBarDelegate* confirm_bar = infobar->AsConfirmInfoBarDelegate(); |
| 351 if (confirm_bar->Accept()) { |
| 352 infobar_helper->RemoveInfoBar(infobar); |
| 353 return true; |
| 354 } |
| 355 } |
| 356 case DENY: { |
| 357 ConfirmInfoBarDelegate* confirm_bar = infobar->AsConfirmInfoBarDelegate(); |
| 358 if (confirm_bar->Cancel()) { |
| 359 infobar_helper->RemoveInfoBar(infobar); |
| 360 return true; |
| 361 } |
| 362 } |
| 363 } |
| 364 |
| 365 return false; |
| 366 } |
| 367 |
| 368 void NotificationsTest::GetPrefsByContentSetting( |
| 369 ContentSetting setting, |
| 370 ContentSettingsForOneType* settings) { |
| 371 DesktopNotificationService* service = GetDesktopNotificationService(); |
| 372 service->GetNotificationsSettings(settings); |
| 373 for (ContentSettingsForOneType::iterator it = settings->begin(); |
| 374 it != settings->end(); ) { |
| 375 if (it->setting != setting || it->source.compare("preference") != 0) |
| 376 it = settings->erase(it); |
| 377 else |
| 378 ++it; |
| 379 } |
| 380 } |
| 381 |
| 382 bool NotificationsTest::CheckOriginInSetting( |
| 383 const ContentSettingsForOneType& settings, |
| 384 const GURL& origin) { |
| 385 ContentSettingsPattern pattern = |
| 386 ContentSettingsPattern::FromURLNoWildcard(origin); |
| 387 for (ContentSettingsForOneType::const_iterator it = settings.begin(); |
| 388 it != settings.end(); ++it) { |
| 389 if (it->primary_pattern == pattern) |
| 390 return true; |
| 391 } |
| 392 return false; |
| 393 } |
| 394 |
| 395 void NotificationsTest::DropOriginPreference(const GURL& origin) { |
| 396 GetDesktopNotificationService()->ClearSetting( |
| 397 ContentSettingsPattern::FromURLNoWildcard(origin)); |
| 398 } |
| 399 |
| 400 DesktopNotificationService* NotificationsTest::GetDesktopNotificationService() { |
| 401 Profile* profile = browser()->profile(); |
| 402 return DesktopNotificationServiceFactory::GetForProfile(profile); |
| 403 } |
| 404 |
20 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428. | 405 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428. |
21 IN_PROC_BROWSER_TEST_F(NotificationsPermissionTest, TestUserGestureInfobar) { | 406 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestUserGestureInfobar) { |
22 ASSERT_TRUE(test_server()->Start()); | |
23 | |
24 ui_test_utils::NavigateToURL( | 407 ui_test_utils::NavigateToURL( |
25 browser(), | 408 browser(), |
26 test_server()->GetURL( | 409 test_server()->GetURL( |
27 "files/notifications/notifications_request_function.html")); | 410 "files/notifications/notifications_request_function.html")); |
28 | 411 |
29 // Request permission by calling request() while eval'ing an inline script; | 412 // Request permission by calling request() while eval'ing an inline script; |
30 // That's considered a user gesture to webkit, and should produce an infobar. | 413 // That's considered a user gesture to webkit, and should produce an infobar. |
31 bool result; | 414 bool result; |
32 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( | 415 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
33 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 416 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
34 L"", | 417 L"", |
35 L"window.domAutomationController.send(request());", | 418 L"window.domAutomationController.send(request());", |
36 &result)); | 419 &result)); |
37 EXPECT_TRUE(result); | 420 EXPECT_TRUE(result); |
38 | 421 |
39 EXPECT_EQ(1U, InfoBarTabHelper::FromWebContents( | 422 EXPECT_EQ(1U, InfoBarTabHelper::FromWebContents( |
40 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); | 423 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); |
41 } | 424 } |
42 | 425 |
43 // If this flakes, use http://crbug.com/62311. | 426 // If this flakes, use http://crbug.com/62311. |
44 IN_PROC_BROWSER_TEST_F(NotificationsPermissionTest, TestNoUserGestureInfobar) { | 427 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNoUserGestureInfobar) { |
45 ASSERT_TRUE(test_server()->Start()); | |
46 | |
47 // Load a page which just does a request; no user gesture should result | 428 // Load a page which just does a request; no user gesture should result |
48 // in no infobar. | 429 // in no infobar. |
49 ui_test_utils::NavigateToURL( | 430 ui_test_utils::NavigateToURL( |
50 browser(), | 431 browser(), |
51 test_server()->GetURL( | 432 test_server()->GetURL( |
52 "files/notifications/notifications_request_inline.html")); | 433 "files/notifications/notifications_request_inline.html")); |
53 | 434 |
54 EXPECT_EQ(0U, InfoBarTabHelper::FromWebContents( | 435 EXPECT_EQ(0U, InfoBarTabHelper::FromWebContents( |
55 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); | 436 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); |
56 } | 437 } |
| 438 |
| 439 // Disable new testcases on Chrome OS due to failure on creating notification. |
| 440 #if !defined(OS_CHROMEOS) |
| 441 |
| 442 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateSimpleNotification) { |
| 443 // Creates a simple notification. |
| 444 AllowAllOrigins(); |
| 445 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 446 |
| 447 std::string result = CreateSimpleNotification(browser(), true); |
| 448 EXPECT_NE("-1", result); |
| 449 |
| 450 const std::deque<Balloon*>& balloons = GetActiveBalloons(); |
| 451 ASSERT_EQ(1U, balloons.size()); |
| 452 Balloon* balloon = balloons[0]; |
| 453 const Notification& notification = balloon->notification(); |
| 454 GURL EXPECTED_ICON_URL = test_server()->GetURL(kExpectedIconUrl); |
| 455 EXPECT_EQ(EXPECTED_ICON_URL, notification.icon_url()); |
| 456 EXPECT_EQ(ASCIIToUTF16("My Title"), notification.title()); |
| 457 EXPECT_EQ(ASCIIToUTF16("My Body"), notification.body()); |
| 458 } |
| 459 |
| 460 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseNotification) { |
| 461 // Creates a notification and closes it. |
| 462 AllowAllOrigins(); |
| 463 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 464 |
| 465 std::string result = CreateSimpleNotification(browser(), true); |
| 466 EXPECT_NE("-1", result); |
| 467 |
| 468 const std::deque<Balloon*>& balloons = GetActiveBalloons(); |
| 469 ASSERT_EQ(1U, balloons.size()); |
| 470 EXPECT_TRUE(CloseNotificationAndWait(balloons[0]->notification())); |
| 471 ASSERT_EQ(0, GetNotificationCount()); |
| 472 } |
| 473 |
| 474 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCancelNotification) { |
| 475 // Creates a notification and cancels it in the origin page. |
| 476 AllowAllOrigins(); |
| 477 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 478 |
| 479 std::string note_id = CreateSimpleNotification(browser(), true); |
| 480 EXPECT_NE(note_id, "-1"); |
| 481 |
| 482 ASSERT_EQ(1, GetNotificationCount()); |
| 483 ASSERT_TRUE(CancelNotification(note_id.c_str(), browser())); |
| 484 ASSERT_EQ(0, GetNotificationCount()); |
| 485 } |
| 486 |
| 487 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestPermissionInfobarAppears) { |
| 488 // Requests notification privileges and verifies the infobar appears. |
| 489 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 490 ASSERT_TRUE(RequestPermissionAndWait(browser())); |
| 491 |
| 492 ASSERT_EQ(0, GetNotificationCount()); |
| 493 VerifyInfobar(browser(), 0); |
| 494 } |
| 495 |
| 496 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowOnPermissionInfobar) { |
| 497 // Tries to create a notification and clicks allow on the infobar. |
| 498 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 499 // This notification should not be shown because we do not have permission. |
| 500 CreateSimpleNotification(browser(), false); |
| 501 ASSERT_EQ(0, GetNotificationCount()); |
| 502 |
| 503 ASSERT_TRUE(RequestPermissionAndWait(browser())); |
| 504 ASSERT_TRUE(PerformActionOnInfobar(browser(), ALLOW, 0, 0)); |
| 505 |
| 506 CreateSimpleNotification(browser(), true); |
| 507 EXPECT_EQ(1, GetNotificationCount()); |
| 508 } |
| 509 |
| 510 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyOnPermissionInfobar) { |
| 511 // Test that no notification is created |
| 512 // when Deny is chosen from permission infobar. |
| 513 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 514 ASSERT_TRUE(RequestPermissionAndWait(browser())); |
| 515 PerformActionOnInfobar(browser(), DENY, 0, 0); |
| 516 CreateSimpleNotification(browser(), false); |
| 517 ASSERT_EQ(0, GetNotificationCount()); |
| 518 ContentSettingsForOneType settings; |
| 519 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); |
| 520 EXPECT_TRUE(CheckOriginInSetting(settings, test_page_url_)); |
| 521 } |
| 522 |
| 523 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestClosePermissionInfobar) { |
| 524 // Test that no notification is created when permission infobar is dismissed. |
| 525 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 526 ASSERT_TRUE(RequestPermissionAndWait(browser())); |
| 527 PerformActionOnInfobar(browser(), DISMISS, 0, 0); |
| 528 CreateSimpleNotification(browser(), false); |
| 529 ASSERT_EQ(0, GetNotificationCount()); |
| 530 ContentSettingsForOneType settings; |
| 531 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); |
| 532 EXPECT_EQ(0U, settings.size()); |
| 533 } |
| 534 |
| 535 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationWithPropertyMissing) { |
| 536 // Test that a notification can be created if one property is missing. |
| 537 AllowAllOrigins(); |
| 538 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 539 |
| 540 std::string result = CreateSimpleNotification(browser(), true); |
| 541 EXPECT_NE("-1", result); |
| 542 |
| 543 const std::deque<Balloon*>& balloons = GetActiveBalloons(); |
| 544 ASSERT_EQ(1U, balloons.size()); |
| 545 Balloon* balloon = balloons[0]; |
| 546 const Notification& notification = balloon->notification(); |
| 547 GURL EXPECTED_ICON_URL = test_server()->GetURL(kExpectedIconUrl); |
| 548 EXPECT_EQ(EXPECTED_ICON_URL, notification.icon_url()); |
| 549 EXPECT_EQ(ASCIIToUTF16("My Title"), notification.title()); |
| 550 } |
| 551 |
| 552 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowNotificationsFromAllSites) { |
| 553 // Verify that all domains can be allowed to show notifications. |
| 554 SetDefaultPermissionSetting(CONTENT_SETTING_ALLOW); |
| 555 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 556 |
| 557 std::string result = CreateSimpleNotification(browser(), true); |
| 558 EXPECT_NE("-1", result); |
| 559 |
| 560 ASSERT_EQ(1, GetNotificationCount()); |
| 561 EXPECT_EQ(0U, InfoBarTabHelper::FromWebContents( |
| 562 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); |
| 563 } |
| 564 |
| 565 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyNotificationsFromAllSites) { |
| 566 // Verify that no domain can show notifications. |
| 567 SetDefaultPermissionSetting(CONTENT_SETTING_BLOCK); |
| 568 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 569 |
| 570 std::string result = CreateSimpleNotification(browser(), false); |
| 571 EXPECT_EQ("-1", result); |
| 572 |
| 573 ASSERT_EQ(0, GetNotificationCount()); |
| 574 } |
| 575 |
| 576 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyDomainAndAllowAll) { |
| 577 // Verify that denying a domain and allowing all shouldn't show |
| 578 // notifications from the denied domain. |
| 579 DenyOrigin(test_page_url_.GetOrigin()); |
| 580 SetDefaultPermissionSetting(CONTENT_SETTING_ALLOW); |
| 581 |
| 582 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 583 |
| 584 std::string result = CreateSimpleNotification(browser(), false); |
| 585 EXPECT_EQ("-1", result); |
| 586 |
| 587 ASSERT_EQ(0, GetNotificationCount()); |
| 588 } |
| 589 |
| 590 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowDomainAndDenyAll) { |
| 591 // Verify that allowing a domain and denying all others should show |
| 592 // notifications from the allowed domain. |
| 593 AllowOrigin(test_page_url_.GetOrigin()); |
| 594 SetDefaultPermissionSetting(CONTENT_SETTING_BLOCK); |
| 595 |
| 596 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 597 |
| 598 std::string result = CreateSimpleNotification(browser(), true); |
| 599 EXPECT_NE("-1", result); |
| 600 |
| 601 ASSERT_EQ(1, GetNotificationCount()); |
| 602 } |
| 603 |
| 604 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyAndThenAllowDomain) { |
| 605 // Verify that denying and again allowing should show notifications. |
| 606 DenyOrigin(test_page_url_.GetOrigin()); |
| 607 |
| 608 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 609 |
| 610 std::string result = CreateSimpleNotification(browser(), false); |
| 611 EXPECT_EQ("-1", result); |
| 612 |
| 613 ASSERT_EQ(0, GetNotificationCount()); |
| 614 |
| 615 AllowOrigin(test_page_url_.GetOrigin()); |
| 616 result = CreateSimpleNotification(browser(), true); |
| 617 EXPECT_NE("-1", result); |
| 618 |
| 619 ASSERT_EQ(1, GetNotificationCount()); |
| 620 EXPECT_EQ(0U, InfoBarTabHelper::FromWebContents( |
| 621 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); |
| 622 } |
| 623 |
| 624 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateDenyCloseNotifications) { |
| 625 // Verify able to create, deny, and close the notification. |
| 626 AllowAllOrigins(); |
| 627 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 628 CreateSimpleNotification(browser(), true); |
| 629 ASSERT_EQ(1, GetNotificationCount()); |
| 630 |
| 631 DenyOrigin(test_page_url_.GetOrigin()); |
| 632 ContentSettingsForOneType settings; |
| 633 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); |
| 634 ASSERT_TRUE(CheckOriginInSetting(settings, test_page_url_.GetOrigin())); |
| 635 |
| 636 const std::deque<Balloon*>& balloons1 = GetActiveBalloons(); |
| 637 EXPECT_EQ(1U, balloons1.size()); |
| 638 ASSERT_TRUE(CloseNotificationAndWait(balloons1[0]->notification())); |
| 639 ASSERT_EQ(0, GetNotificationCount()); |
| 640 } |
| 641 |
| 642 // Crashes on Linux/Win. See http://crbug.com/160657. |
| 643 IN_PROC_BROWSER_TEST_F( |
| 644 NotificationsTest, |
| 645 DISABLED_TestOriginPrefsNotSavedInIncognito) { |
| 646 // Verify that allow/deny origin preferences are not saved in incognito. |
| 647 Browser* incognito = CreateIncognitoBrowser(); |
| 648 ui_test_utils::NavigateToURL(incognito, test_page_url_); |
| 649 ASSERT_TRUE(RequestPermissionAndWait(incognito)); |
| 650 PerformActionOnInfobar(incognito, DENY, 0, 0); |
| 651 CloseBrowserWindow(incognito); |
| 652 |
| 653 incognito = CreateIncognitoBrowser(); |
| 654 ui_test_utils::NavigateToURL(incognito, test_page_url_); |
| 655 ASSERT_TRUE(RequestPermissionAndWait(incognito)); |
| 656 PerformActionOnInfobar(incognito, ALLOW, 0, 0); |
| 657 CreateSimpleNotification(incognito, true); |
| 658 ASSERT_EQ(1, GetNotificationCount()); |
| 659 CloseBrowserWindow(incognito); |
| 660 |
| 661 incognito = CreateIncognitoBrowser(); |
| 662 ui_test_utils::NavigateToURL(incognito, test_page_url_); |
| 663 ASSERT_TRUE(RequestPermissionAndWait(incognito)); |
| 664 |
| 665 ContentSettingsForOneType settings; |
| 666 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); |
| 667 EXPECT_EQ(0U, settings.size()); |
| 668 GetPrefsByContentSetting(CONTENT_SETTING_ALLOW, &settings); |
| 669 EXPECT_EQ(0U, settings.size()); |
| 670 } |
| 671 |
| 672 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestExitBrowserWithInfobar) { |
| 673 // Exit the browser window, when the infobar appears. |
| 674 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 675 ASSERT_TRUE(RequestPermissionAndWait(browser())); |
| 676 } |
| 677 |
| 678 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCrashTabWithPermissionInfobar) { |
| 679 // Test crashing the tab with permission infobar doesn't crash Chrome. |
| 680 ui_test_utils::NavigateToURLWithDisposition( |
| 681 browser(), |
| 682 empty_page_url_, |
| 683 NEW_BACKGROUND_TAB, |
| 684 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); |
| 685 chrome::ActivateTabAt(browser(), 0, true); |
| 686 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 687 ASSERT_TRUE(RequestPermissionAndWait(browser())); |
| 688 CrashTab(browser(), 0); |
| 689 } |
| 690 |
| 691 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestKillNotificationProcess) { |
| 692 // Test killing a notification doesn't crash Chrome. |
| 693 AllowAllOrigins(); |
| 694 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 695 CreateSimpleNotification(browser(), true); |
| 696 ASSERT_EQ(1, GetNotificationCount()); |
| 697 |
| 698 const std::deque<Balloon*>& balloons = GetActiveBalloons(); |
| 699 ASSERT_EQ(1U, balloons.size()); |
| 700 CrashNotification(balloons[0]); |
| 701 ASSERT_EQ(0, GetNotificationCount()); |
| 702 } |
| 703 |
| 704 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestIncognitoNotification) { |
| 705 // Test notifications in incognito window. |
| 706 Browser* browser = CreateIncognitoBrowser(); |
| 707 ui_test_utils::NavigateToURL(browser, test_page_url_); |
| 708 chrome::ActivateTabAt(browser, 0, true); |
| 709 ASSERT_TRUE(RequestPermissionAndWait(browser)); |
| 710 PerformActionOnInfobar(browser, ALLOW, 0, 0); |
| 711 CreateSimpleNotification(browser, true); |
| 712 ASSERT_EQ(1, GetNotificationCount()); |
| 713 } |
| 714 |
| 715 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseTabWithPermissionInfobar) { |
| 716 // Test that user can close tab when infobar present. |
| 717 ui_test_utils::NavigateToURLWithDisposition( |
| 718 browser(), |
| 719 GURL("about:blank"), |
| 720 NEW_BACKGROUND_TAB, |
| 721 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); |
| 722 chrome::ActivateTabAt(browser(), 0, true); |
| 723 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 724 ASSERT_TRUE(RequestPermissionAndWait(browser())); |
| 725 content::WindowedNotificationObserver observer( |
| 726 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 727 content::NotificationService::AllSources()); |
| 728 chrome::CloseWebContents(browser(), chrome::GetWebContentsAt(browser(), 0)); |
| 729 observer.Wait(); |
| 730 } |
| 731 |
| 732 IN_PROC_BROWSER_TEST_F( |
| 733 NotificationsTest, |
| 734 TestNavigateAwayWithPermissionInfobar) { |
| 735 // Test navigating away when an infobar is present, |
| 736 // then trying to create a notification from the same page. |
| 737 ui_test_utils::NavigateToURLWithDisposition( |
| 738 browser(), |
| 739 GURL("about:blank"), |
| 740 NEW_BACKGROUND_TAB, |
| 741 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); |
| 742 chrome::ActivateTabAt(browser(), 0, true); |
| 743 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 744 ASSERT_TRUE(RequestPermissionAndWait(browser())); |
| 745 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 746 ASSERT_TRUE(RequestPermissionAndWait(browser())); |
| 747 PerformActionOnInfobar(browser(), ALLOW, 0, 0); |
| 748 CreateSimpleNotification(browser(), true); |
| 749 ASSERT_EQ(1, GetNotificationCount()); |
| 750 } |
| 751 |
| 752 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCrashRendererNotificationRemain) { |
| 753 // Test crashing renderer does not close or crash notification. |
| 754 AllowAllOrigins(); |
| 755 ui_test_utils::NavigateToURLWithDisposition( |
| 756 browser(), |
| 757 GURL("about:blank"), |
| 758 NEW_BACKGROUND_TAB, |
| 759 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); |
| 760 chrome::ActivateTabAt(browser(), 0, true); |
| 761 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 762 CreateSimpleNotification(browser(), true); |
| 763 ASSERT_EQ(1, GetNotificationCount()); |
| 764 CrashTab(browser(), 0); |
| 765 ASSERT_EQ(1, GetNotificationCount()); |
| 766 } |
| 767 |
| 768 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationReplacement) { |
| 769 // Test that we can replace a notification using the replaceId. |
| 770 AllowAllOrigins(); |
| 771 |
| 772 ui_test_utils::NavigateToURL(browser(), test_page_url_); |
| 773 |
| 774 std::string result = CreateNotification( |
| 775 browser(), true, "abc.png", "Title1", "Body1", "chat"); |
| 776 EXPECT_NE("-1", result); |
| 777 |
| 778 ASSERT_EQ(1, GetNotificationCount()); |
| 779 |
| 780 result = CreateNotification( |
| 781 browser(), false, "no_such_file.png", "Title2", "Body2", "chat"); |
| 782 EXPECT_NE("-1", result); |
| 783 |
| 784 const std::deque<Balloon*>& balloons = GetActiveBalloons(); |
| 785 EXPECT_EQ(1U, balloons.size()); |
| 786 const Notification& notification = balloons[0]->notification(); |
| 787 GURL EXPECTED_ICON_URL = test_server()->GetURL(kExpectedIconUrl); |
| 788 EXPECT_EQ(EXPECTED_ICON_URL, notification.icon_url()); |
| 789 EXPECT_EQ(ASCIIToUTF16("Title2"), notification.title()); |
| 790 EXPECT_EQ(ASCIIToUTF16("Body2"), notification.body()); |
| 791 } |
| 792 |
| 793 #endif // !defined(OS_CHROMEOS) |
OLD | NEW |