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