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