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/utf_string_conversions.h" | |
| 6 #include "chrome/app/chrome_command_ids.h" | |
| 7 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" | |
| 8 #include "chrome/browser/api/infobars/infobar_delegate.h" | |
| 9 #include "chrome/browser/browser_process.h" | |
| 5 #include "chrome/browser/infobars/infobar_tab_helper.h" | 10 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 11 #include "chrome/browser/notifications/balloon.h" | |
| 12 #include "chrome/browser/notifications/balloon_collection.h" | |
| 13 #include "chrome/browser/notifications/balloon_host.h" | |
| 14 #include "chrome/browser/notifications/desktop_notification_service.h" | |
| 15 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | |
| 16 #include "chrome/browser/notifications/notification.h" | |
| 17 #include "chrome/browser/notifications/notification_ui_manager.h" | |
| 18 #include "chrome/browser/profiles/profile.h" | |
| 19 #include "chrome/browser/sessions/session_tab_helper.h" | |
| 6 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/browser_commands.h" | |
| 7 #include "chrome/browser/ui/browser_tabstrip.h" | 22 #include "chrome/browser/ui/browser_tabstrip.h" |
| 23 #include "chrome/browser/ui/browser_window.h" | |
| 24 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
| 25 #include "chrome/common/chrome_notification_types.h" | |
| 8 #include "chrome/test/base/in_process_browser_test.h" | 26 #include "chrome/test/base/in_process_browser_test.h" |
| 9 #include "chrome/test/base/ui_test_utils.h" | 27 #include "chrome/test/base/ui_test_utils.h" |
| 28 #include "content/public/browser/navigation_controller.h" | |
| 29 #include "content/public/browser/notification_service.h" | |
| 30 #include "content/public/browser/notification_types.h" | |
| 10 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
| 11 #include "content/public/test/browser_test_utils.h" | 32 #include "content/public/test/browser_test_utils.h" |
| 33 #include "googleurl/src/gurl.h" | |
| 12 #include "net/base/net_util.h" | 34 #include "net/base/net_util.h" |
| 13 #include "net/test/test_server.h" | 35 #include "net/test/test_server.h" |
| 14 | 36 #include "webkit/glue/window_open_disposition.h" |
| 15 class NotificationsPermissionTest : public InProcessBrowserTest { | 37 |
| 38 #if defined(OS_WIN) | |
| 39 #include "base/win/windows_version.h" | |
| 40 #endif | |
| 41 | |
| 42 class NotificationsPermissionTest : public InProcessBrowserTest, | |
| 43 public content::NotificationObserver { | |
| 16 public: | 44 public: |
| 17 NotificationsPermissionTest() {} | 45 NotificationsPermissionTest(); |
| 46 | |
| 47 protected: | |
| 48 // InProcessBrowserTest | |
|
kkania
2012/11/13 23:38:21
The preferred style is:
Overriden from InProcessBr
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 49 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; | |
| 50 | |
| 51 // content::NotificationObserver | |
|
kkania
2012/11/13 23:38:21
same here
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 52 virtual void Observe(int type, | |
| 53 const content::NotificationSource& source, | |
| 54 const content::NotificationDetails& details) OVERRIDE; | |
|
kkania
2012/11/13 23:38:21
for chromium, we usually encourage you to include
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 55 | |
| 56 DesktopNotificationService* GetDesktopNotificationService(); | |
| 57 const std::deque<Balloon*>& GetActiveBalloons(); | |
| 58 | |
| 59 bool CloseNotificationAndWait(const Notification& notification); | |
| 60 bool WaitForInfoBarCount(Browser* browser, | |
| 61 const size_t count, | |
|
kkania
2012/11/13 23:38:21
don't put const for primitives
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 62 const int index, | |
| 63 bool increasing); | |
| 64 bool CheckNotificationCount(const size_t count); | |
| 65 Browser* CreateBrowserWindow(Browser* source_browser, bool is_incognito); | |
| 66 void CloseBrowserWindow(Browser* browser); | |
| 67 void CloseTab(Browser* browser, const int index); | |
| 68 void CrashTab(Browser* browser, const int index); | |
| 69 void CrashNotification(Balloon* balloon); | |
| 70 | |
| 71 void Debug(); | |
|
kkania
2012/11/13 23:38:21
remove?
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 72 | |
| 73 void SetDefaultPermissionSetting(ContentSetting setting); | |
| 74 ContentSetting GetDefaultPermissionSetting(); | |
| 75 void GetDeniedOrigins(ContentSettingsForOneType* settings); | |
| 76 void GetAllowedOrigins(ContentSettingsForOneType* settings); | |
| 77 void SetDeniedOrigins(const std::vector<GURL>& new_origins); | |
| 78 void SetAllowedOrigins(const std::vector<GURL>& new_origins); | |
| 79 void DenyOrigin(const GURL& origin); | |
| 80 void AllowOrigin(const GURL& origin); | |
| 81 void DropOriginPreference(const GURL& origin); | |
| 82 void AllowAllOrigins(); | |
| 83 | |
| 84 void VerifyInfobar(const Browser* browser, | |
| 85 const std::string& origin, | |
| 86 const int index); | |
| 87 std::string CreateNotification(Browser* browser, | |
| 88 const bool waiting, | |
|
kkania
2012/11/13 23:38:21
indent is wrong here
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 89 const std::string& icon, | |
| 90 const std::string& title, | |
| 91 const std::string& body, | |
| 92 const std::string& replace_id); | |
| 93 std::string CreateSimpleNotification(Browser* browser, const bool waiting); | |
| 94 std::string CreateHTMLNotification(const std::string& content_url, | |
| 95 Browser* browser, | |
| 96 const std::string& replace_id, | |
| 97 bool wait_for_display); | |
| 98 void RequestPermission(Browser* browser); | |
| 99 void CancelNotification(const std::string& notification_id, Browser* browser); | |
| 100 bool PerformActionOnInfobar(Browser* browser, | |
| 101 const std::string action, | |
| 102 const int infobar_index, | |
| 103 const int tab_index); | |
| 104 bool checkOriginInSetting(const ContentSettingsForOneType & settings, | |
| 105 const GURL & origin); | |
| 106 | |
| 107 static const ContentSetting kAllowAllSetting; | |
| 108 static const ContentSetting kDenyAllSetting; | |
| 109 GURL EMPTY_PAGE_URL; | |
| 110 GURL TEST_PAGE_URL; | |
| 111 | |
| 112 private: | |
| 113 size_t target_infobar_count_; | |
| 114 size_t target_balloon_count_; | |
| 115 static const std::string kReference; | |
|
kkania
2012/11/13 23:38:21
http://google-styleguide.googlecode.com/svn/trunk/
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 116 | |
| 117 void GetPrefsByContentSetting(ContentSetting setting, | |
| 118 ContentSettingsForOneType* settings); | |
| 119 void PrintSettings(const ContentSettingsForOneType & settings, | |
| 120 const std::string & message); | |
| 121 | |
| 122 class BrowserOpenedNotificationObserver : | |
|
kkania
2012/11/13 23:38:21
move this class out. put it in an unnamed namespac
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 123 public content::NotificationObserver { | |
| 124 public: | |
| 125 BrowserOpenedNotificationObserver(); | |
|
kkania
2012/11/13 23:38:21
indent is wrong
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 126 virtual ~BrowserOpenedNotificationObserver(); | |
| 127 | |
| 128 void Wait(); | |
| 129 Browser* new_browser() { return new_browser_; } | |
| 130 | |
| 131 virtual void Observe(int type, | |
| 132 const content::NotificationSource& source, | |
| 133 const content::NotificationDetails& details) OVERRIDE; | |
| 134 private: | |
| 135 content::NotificationRegistrar registrar_; | |
| 136 Browser* new_browser_; | |
| 137 int new_window_id_; | |
| 138 bool done_; | |
| 139 bool running_; | |
| 140 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(BrowserOpenedNotificationObserver); | |
| 143 }; | |
| 18 }; | 144 }; |
| 19 | 145 |
| 146 NotificationsPermissionTest::BrowserOpenedNotificationObserver | |
| 147 ::BrowserOpenedNotificationObserver() | |
| 148 : new_browser_(NULL), | |
| 149 new_window_id_(-1), | |
| 150 done_(false), | |
| 151 running_(false) { | |
| 152 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED, | |
| 153 content::NotificationService::AllBrowserContextsAndSources()); | |
| 154 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, | |
| 155 content::NotificationService::AllBrowserContextsAndSources()); | |
| 156 } | |
| 157 | |
| 158 NotificationsPermissionTest::BrowserOpenedNotificationObserver | |
| 159 ::~BrowserOpenedNotificationObserver() {} | |
| 160 | |
| 161 void NotificationsPermissionTest::BrowserOpenedNotificationObserver::Wait() { | |
| 162 if (done_) | |
| 163 return; | |
| 164 | |
| 165 running_ = true; | |
| 166 message_loop_runner_ = new content::MessageLoopRunner; | |
| 167 message_loop_runner_->Run(); | |
| 168 EXPECT_TRUE(done_); | |
| 169 } | |
| 170 | |
| 171 void NotificationsPermissionTest::BrowserOpenedNotificationObserver | |
| 172 ::Observe(int type, | |
| 173 const content::NotificationSource& source, | |
| 174 const content::NotificationDetails& details) { | |
| 175 if (type == chrome::NOTIFICATION_BROWSER_OPENED) { | |
| 176 // Store the new browser ID and continue waiting for a new tab within it | |
| 177 // to stop loading. | |
| 178 new_browser_ = content::Source<Browser>(source).ptr(); | |
| 179 new_window_id_ = new_browser_->session_id().id(); | |
| 180 } else { | |
| 181 DCHECK_EQ(content::NOTIFICATION_LOAD_STOP, type); | |
| 182 content::NavigationController* controller = | |
| 183 content::Source<content::NavigationController>(source).ptr(); | |
| 184 SessionTabHelper* session_tab_helper = | |
| 185 SessionTabHelper::FromWebContents(controller->GetWebContents()); | |
| 186 int window_id = session_tab_helper ? session_tab_helper->window_id().id() | |
| 187 : -1; | |
| 188 if (window_id == new_window_id_) { | |
| 189 registrar_.RemoveAll(); | |
| 190 done_ = true; | |
| 191 if (!running_) | |
| 192 return; | |
| 193 | |
| 194 message_loop_runner_->Quit(); | |
| 195 running_ = false; | |
| 196 } | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 const ContentSetting NotificationsPermissionTest::kAllowAllSetting | |
| 201 = CONTENT_SETTING_ALLOW; | |
| 202 | |
| 203 const ContentSetting NotificationsPermissionTest::kDenyAllSetting | |
| 204 = CONTENT_SETTING_BLOCK; | |
| 205 | |
| 206 const std::string NotificationsPermissionTest::kReference = "preference"; | |
| 207 | |
| 208 NotificationsPermissionTest::NotificationsPermissionTest() | |
| 209 : target_infobar_count_(0U), | |
| 210 target_balloon_count_(0U) { | |
| 211 } | |
| 212 | |
| 213 void NotificationsPermissionTest::SetUpInProcessBrowserTestFixture() { | |
| 214 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); | |
| 215 | |
| 216 ASSERT_TRUE(test_server()->Start()); | |
| 217 EMPTY_PAGE_URL = test_server()->GetURL("files/empty.html"); | |
| 218 TEST_PAGE_URL = test_server()->GetURL( | |
| 219 "files/notifications/notification_tester.html"); | |
| 220 } | |
| 221 | |
| 222 void NotificationsPermissionTest::Observe( | |
| 223 int type, | |
| 224 const content::NotificationSource& source, | |
| 225 const content::NotificationDetails& details) { | |
| 226 switch (type) { | |
| 227 case chrome::NOTIFICATION_BROWSER_CLOSED: { | |
| 228 LOG(ERROR) << "Got NOTIFICATION_BROWSER_CLOSED notification"; | |
| 229 MessageLoopForUI::current()->Quit(); | |
| 230 break; | |
| 231 } | |
| 232 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED: | |
| 233 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED: { | |
| 234 const InfoBarTabHelper* infoBarTabHelper = | |
| 235 content::Source<const InfoBarTabHelper>(source).ptr(); | |
| 236 if (infoBarTabHelper->GetInfoBarCount() == target_infobar_count_) { | |
| 237 target_infobar_count_ = 0U; | |
| 238 MessageLoopForUI::current()->Quit(); | |
| 239 } | |
| 240 break; | |
| 241 } | |
| 242 case chrome::NOTIFICATION_NOTIFY_BALLOON_CONNECTED: | |
| 243 case chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED: { | |
| 244 size_t current_balloon_count = GetActiveBalloons().size(); | |
| 245 LOG(ERROR) << "Got NOTIFICATION_NOTIFY_BALLOON_CONNECTED notification. " | |
| 246 << "Number of desktop notification: " | |
| 247 << current_balloon_count; | |
| 248 if (current_balloon_count == target_balloon_count_) { | |
| 249 target_balloon_count_ = 0U; | |
| 250 MessageLoopForUI::current()->Quit(); | |
| 251 } | |
| 252 break; | |
| 253 } | |
| 254 default: | |
| 255 break; | |
| 256 } | |
| 257 } | |
| 258 | |
| 259 DesktopNotificationService* | |
| 260 NotificationsPermissionTest::GetDesktopNotificationService() { | |
| 261 Profile* profile = browser()->profile(); | |
| 262 return DesktopNotificationServiceFactory::GetForProfile(profile); | |
| 263 } | |
| 264 | |
| 265 const std::deque<Balloon*>& NotificationsPermissionTest::GetActiveBalloons() { | |
| 266 NotificationUIManager* manager = g_browser_process->notification_ui_manager(); | |
| 267 return manager->balloon_collection()->GetActiveBalloons(); | |
| 268 } | |
| 269 | |
| 270 bool NotificationsPermissionTest::CloseNotificationAndWait( | |
| 271 const Notification& notification) { | |
| 272 LOG(ERROR) << "CLOSE AND WAIT"; | |
| 273 content::WindowedNotificationObserver signal( | |
| 274 chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED, | |
| 275 content::NotificationService::AllSources()); | |
| 276 NotificationUIManager* manager = g_browser_process->notification_ui_manager(); | |
| 277 LOG(ERROR) << "CLOSE"; | |
| 278 bool flag = manager->CancelById(notification.notification_id()); | |
| 279 signal.Wait(); | |
| 280 LOG(ERROR) << "DONE WAITING"; | |
| 281 return flag; | |
| 282 } | |
| 283 | |
| 284 bool NotificationsPermissionTest::WaitForInfoBarCount( | |
| 285 Browser* browser, | |
| 286 const size_t count, | |
| 287 const int index, | |
| 288 bool increasing) { | |
| 289 size_t current_count = InfoBarTabHelper::FromWebContents( | |
| 290 chrome::GetWebContentsAt(browser, 0))->GetInfoBarCount(); | |
| 291 if (count != current_count) { | |
| 292 target_infobar_count_ = count; | |
| 293 ui_test_utils::RegisterAndWait( | |
| 294 this, | |
| 295 increasing ? chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED | |
| 296 : chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
| 297 content::NotificationService::AllSources()); | |
| 298 } | |
| 299 | |
| 300 current_count = InfoBarTabHelper::FromWebContents( | |
| 301 chrome::GetWebContentsAt(browser, 0))->GetInfoBarCount(); | |
| 302 return count == current_count; | |
| 303 } | |
| 304 | |
| 305 bool NotificationsPermissionTest::CheckNotificationCount(const size_t count) { | |
| 306 NotificationUIManager* manager = g_browser_process->notification_ui_manager(); | |
| 307 return count == manager->balloon_collection()->GetActiveBalloons().size(); | |
| 308 } | |
| 309 | |
| 310 Browser* NotificationsPermissionTest::CreateBrowserWindow( | |
| 311 Browser* source_browser, | |
| 312 bool is_incognito) { | |
| 313 BrowserOpenedNotificationObserver signal; | |
|
kkania
2012/11/13 23:38:21
We should be able to do something like:
ui_test_u
chrisgao (Use stgao instead)
2012/11/14 21:38:28
I have tried the following three ways to create an
| |
| 314 if (is_incognito) | |
| 315 chrome::ExecuteCommand(source_browser, IDC_NEW_INCOGNITO_WINDOW); | |
| 316 else | |
| 317 chrome::ExecuteCommand(source_browser, IDC_NEW_WINDOW); | |
| 318 signal.Wait(); | |
| 319 return signal.new_browser(); | |
| 320 } | |
| 321 | |
| 322 void NotificationsPermissionTest::CloseBrowserWindow(Browser* browser) { | |
| 323 content::WindowedNotificationObserver signal( | |
|
kkania
2012/11/13 23:38:21
rename all instances of signal to observer; that f
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 324 chrome::NOTIFICATION_BROWSER_CLOSED, | |
| 325 content::Source<Browser>(browser)); | |
| 326 browser->window()->Close(); | |
| 327 signal.Wait(); | |
| 328 } | |
| 329 | |
| 330 void NotificationsPermissionTest::CloseTab(Browser* browser, const int index) { | |
|
kkania
2012/11/13 23:38:21
You only use this func in one place, so remove thi
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 331 content::WindowedNotificationObserver signal( | |
| 332 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
| 333 content::NotificationService::AllSources()); | |
| 334 chrome::CloseWebContents(browser, chrome::GetWebContentsAt(browser, index)); | |
| 335 signal.Wait(); | |
| 336 } | |
| 337 | |
| 338 void NotificationsPermissionTest::CrashTab(Browser* browser, const int index) { | |
| 339 content::CrashTab(chrome::GetWebContentsAt(browser, index)); | |
| 340 } | |
| 341 | |
| 342 void NotificationsPermissionTest::CrashNotification(Balloon* balloon) { | |
| 343 content::CrashTab(balloon->balloon_view()->GetHost()->web_contents()); | |
| 344 } | |
| 345 | |
| 346 void NotificationsPermissionTest::Debug() { | |
| 347 ContentSetting defaultSetting = GetDefaultPermissionSetting(); | |
| 348 LOG(INFO) << "DefaultContentSetting:" << defaultSetting; | |
| 349 | |
| 350 ContentSettingsForOneType settings; | |
| 351 // allowed | |
| 352 GetPrefsByContentSetting(CONTENT_SETTING_ALLOW, &settings); | |
| 353 PrintSettings(settings, "Allowed origins"); | |
| 354 // denied | |
| 355 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); | |
| 356 PrintSettings(settings, "Denied origins"); | |
| 357 } | |
| 358 | |
| 359 bool NotificationsPermissionTest::checkOriginInSetting( | |
|
kkania
2012/11/13 23:38:21
Check
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 360 const ContentSettingsForOneType & settings, | |
| 361 const GURL & origin) { | |
| 362 ContentSettingsPattern pattern = ContentSettingsPattern::FromURLNoWildcard( | |
| 363 origin); | |
| 364 for (ContentSettingsForOneType::const_iterator it = settings.begin(); | |
| 365 it != settings.end(); ++it) { | |
| 366 if (it->primary_pattern == pattern) { | |
| 367 return true; | |
| 368 } | |
| 369 } | |
| 370 return false; | |
| 371 } | |
| 372 | |
| 373 void NotificationsPermissionTest::SetDefaultPermissionSetting( | |
| 374 ContentSetting setting) { | |
| 375 DesktopNotificationService* service = GetDesktopNotificationService(); | |
| 376 service->SetDefaultContentSetting(setting); | |
| 377 } | |
| 378 | |
| 379 ContentSetting NotificationsPermissionTest::GetDefaultPermissionSetting() { | |
| 380 DesktopNotificationService* service = GetDesktopNotificationService(); | |
| 381 return service->GetDefaultContentSetting(NULL); | |
| 382 } | |
| 383 | |
| 384 void NotificationsPermissionTest::GetDeniedOrigins( | |
| 385 ContentSettingsForOneType* settings) { | |
| 386 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, settings); | |
| 387 } | |
| 388 | |
| 389 void NotificationsPermissionTest::GetAllowedOrigins( | |
| 390 ContentSettingsForOneType* settings) { | |
| 391 GetPrefsByContentSetting(CONTENT_SETTING_ALLOW, settings); | |
| 392 } | |
| 393 | |
| 394 void NotificationsPermissionTest::SetDeniedOrigins( | |
| 395 const std::vector<GURL>& new_origins) { | |
| 396 // clear original denied origin list | |
| 397 ContentSettingsForOneType old_origins; | |
| 398 GetDeniedOrigins(&old_origins); | |
| 399 for (ContentSettingsForOneType::const_iterator it = old_origins.begin(); | |
| 400 it != old_origins.end(); ++it) { | |
| 401 GetDesktopNotificationService()->ClearSetting(it->primary_pattern); | |
| 402 } | |
| 403 // set new origins | |
| 404 for (std::vector<GURL>::const_iterator it = new_origins.begin(); | |
| 405 it != new_origins.end(); ++it) { | |
| 406 DenyOrigin(*it); | |
| 407 } | |
| 408 } | |
| 409 void NotificationsPermissionTest::SetAllowedOrigins( | |
| 410 const std::vector<GURL>& new_origins) { | |
| 411 // clear original allowed origin list | |
| 412 ContentSettingsForOneType old_origins; | |
| 413 GetAllowedOrigins(&old_origins); | |
| 414 for (ContentSettingsForOneType::const_iterator it = old_origins.begin(); | |
| 415 it != old_origins.end(); ++it) { | |
| 416 GetDesktopNotificationService()->ClearSetting(it->primary_pattern); | |
| 417 } | |
| 418 // set new origins | |
| 419 for (std::vector<GURL>::const_iterator it = new_origins.begin(); | |
| 420 it != new_origins.end(); ++it) { | |
| 421 AllowOrigin(*it); | |
| 422 } | |
| 423 } | |
| 424 void NotificationsPermissionTest::DenyOrigin(const GURL& origin) { | |
| 425 DropOriginPreference(origin); | |
| 426 GetDesktopNotificationService()->DenyPermission(origin); | |
| 427 } | |
| 428 void NotificationsPermissionTest::AllowOrigin(const GURL& origin) { | |
| 429 DropOriginPreference(origin); | |
| 430 GetDesktopNotificationService()->GrantPermission(origin); | |
| 431 } | |
| 432 void NotificationsPermissionTest::DropOriginPreference(const GURL& origin) { | |
| 433 GetDesktopNotificationService()->ClearSetting( | |
| 434 ContentSettingsPattern::FromURLNoWildcard(origin)); | |
| 435 } | |
| 436 void NotificationsPermissionTest::AllowAllOrigins() { | |
| 437 GetDesktopNotificationService()->ResetAllOrigins(); | |
| 438 GetDesktopNotificationService()->SetDefaultContentSetting( | |
| 439 CONTENT_SETTING_ALLOW); | |
| 440 } | |
| 441 | |
| 442 void NotificationsPermissionTest::VerifyInfobar( | |
| 443 const Browser* browser, | |
| 444 const std::string& origin, | |
| 445 const int index) { | |
| 446 InfoBarTabHelper* infobar_helper = InfoBarTabHelper::FromWebContents( | |
| 447 chrome::GetWebContentsAt(browser, index)); | |
|
kkania
2012/11/13 23:38:21
indent
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 448 | |
| 449 ASSERT_EQ(1U, infobar_helper->GetInfoBarCount()); | |
| 450 InfoBarDelegate* infobar = infobar_helper->GetInfoBarDelegateAt(0); | |
| 451 ASSERT_TRUE(infobar->AsConfirmInfoBarDelegate()); | |
| 452 ConfirmInfoBarDelegate* confirm_infobar = infobar->AsConfirmInfoBarDelegate(); | |
| 453 int buttons = confirm_infobar->GetButtons(); | |
| 454 if (buttons & ConfirmInfoBarDelegate::BUTTON_OK) { | |
| 455 EXPECT_EQ( | |
| 456 ASCIIToUTF16("Allow"), | |
| 457 confirm_infobar->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK)); | |
| 458 } else { | |
| 459 EXPECT_TRUE(false); | |
| 460 } | |
| 461 if (buttons & ConfirmInfoBarDelegate::BUTTON_CANCEL) { | |
| 462 EXPECT_EQ( | |
| 463 ASCIIToUTF16("Deny"), | |
| 464 confirm_infobar->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL)); | |
| 465 } else { | |
| 466 EXPECT_TRUE(false); | |
| 467 } | |
| 468 std::string text("Allow "); | |
| 469 text.append(origin); | |
| 470 text.append(" to show desktop notifications?"); | |
| 471 EXPECT_EQ(UTF8ToUTF16(text), confirm_infobar->GetMessageText()); | |
| 472 } | |
| 473 | |
| 474 std::string NotificationsPermissionTest::CreateNotification( | |
| 475 Browser* browser, | |
| 476 const bool waiting, | |
| 477 const std::string& icon, | |
| 478 const std::string& title, | |
| 479 const std::string& body, | |
| 480 const std::string& replace_id) { | |
| 481 std::string result; | |
| 482 | |
| 483 std::string script("createNotification("); | |
|
kkania
2012/11/13 23:38:21
use base::StringPrintf
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 484 script += "'" + icon + "'"; | |
| 485 script += ",'" + title + "'"; | |
| 486 script += ",'" + body + "'"; | |
| 487 if (replace_id.length() > 0) { | |
| 488 script += ",'" + replace_id + "'"; | |
| 489 } | |
| 490 script += ");"; | |
| 491 | |
| 492 std::wstring tmp(script.length(), L' '); | |
| 493 UTF8ToWide(script.c_str(), script.length(), &tmp); | |
| 494 | |
| 495 bool flag = false; | |
| 496 if (!waiting) { | |
| 497 flag = content::ExecuteJavaScriptAndExtractString( | |
| 498 chrome::GetActiveWebContents(browser)->GetRenderViewHost(), | |
| 499 L"", | |
| 500 tmp, | |
| 501 &result); | |
| 502 } else { | |
| 503 LOG(ERROR) << "Create and Wait"; | |
|
kkania
2012/11/13 23:38:21
remove all logs
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 504 content::WindowedNotificationObserver signal( | |
| 505 chrome::NOTIFICATION_NOTIFY_BALLOON_CONNECTED, | |
| 506 content::NotificationService::AllSources()); | |
| 507 flag = content::ExecuteJavaScriptAndExtractString( | |
| 508 chrome::GetActiveWebContents(browser)->GetRenderViewHost(), | |
| 509 L"", | |
| 510 tmp, | |
| 511 &result); | |
| 512 signal.Wait(); | |
| 513 LOG(ERROR) << "Done Wait"; | |
| 514 } | |
| 515 EXPECT_TRUE(flag); | |
| 516 | |
| 517 content::RunAllPendingInMessageLoop(); | |
| 518 return result; | |
| 519 } | |
| 520 | |
| 521 std::string NotificationsPermissionTest::CreateSimpleNotification( | |
| 522 Browser* browser, const bool waiting) { | |
| 523 return CreateNotification( | |
| 524 browser, | |
| 525 waiting, | |
| 526 "no_such_file.png", | |
| 527 "My Title", | |
| 528 "My Body", | |
| 529 ""); | |
| 530 } | |
| 531 std::string NotificationsPermissionTest::CreateHTMLNotification( | |
| 532 const std::string& content_url, | |
| 533 Browser* browser, | |
| 534 const std::string& replace_id, | |
| 535 bool wait_for_display) { | |
| 536 std::string result; | |
| 537 | |
| 538 std::string script( | |
| 539 "window.domAutomationController.send(createHTMLNotification("); | |
| 540 script += "'" + content_url + "'"; | |
| 541 script += ",'" + replace_id + "'"; | |
| 542 if (wait_for_display) { | |
| 543 script += ", true"; | |
| 544 } else { | |
| 545 script += ", false"; | |
| 546 } | |
| 547 script += "));"; | |
| 548 | |
| 549 std::wstring tmp(script.length(), L' '); | |
| 550 UTF8ToWide(script.c_str(), script.length(), &tmp); | |
| 551 | |
| 552 bool flag = content::ExecuteJavaScriptAndExtractString( | |
| 553 chrome::GetActiveWebContents(browser)->GetRenderViewHost(), | |
| 554 L"", | |
| 555 tmp, | |
| 556 &result); | |
| 557 EXPECT_TRUE(flag); | |
| 558 | |
| 559 return result; | |
| 560 } | |
| 561 | |
| 562 void NotificationsPermissionTest::RequestPermission(Browser* browser) { | |
|
kkania
2012/11/13 23:38:21
return bool here and ASSERT_TRUE in the tests, so
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 563 std::string result; | |
| 564 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | |
| 565 chrome::GetActiveWebContents(browser)->GetRenderViewHost(), | |
| 566 L"", | |
| 567 L"requestPermission();", | |
| 568 &result)); | |
| 569 EXPECT_EQ(result, "1"); | |
| 570 } | |
| 571 | |
| 572 void NotificationsPermissionTest::CancelNotification( | |
| 573 const std::string& notification_id, | |
| 574 Browser* browser) { | |
| 575 std::string script("cancelNotification("); | |
|
kkania
2012/11/13 23:38:21
base::StringPrintf
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 576 script += "'" + notification_id + "'"; | |
| 577 script += ");"; | |
| 578 | |
| 579 std::wstring tmp(script.length(), L' '); | |
| 580 UTF8ToWide(script.c_str(), script.length(), &tmp); | |
|
kkania
2012/11/13 23:38:21
I think there's a version of UTF8ToWide that takes
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 581 | |
| 582 std::string result; | |
| 583 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | |
| 584 chrome::GetActiveWebContents(browser)->GetRenderViewHost(), | |
| 585 L"", | |
| 586 tmp, | |
| 587 &result)); | |
| 588 EXPECT_EQ(result, "1"); | |
| 589 } | |
| 590 | |
| 591 bool NotificationsPermissionTest::PerformActionOnInfobar( | |
|
kkania
2012/11/13 23:38:21
Change this to separate allow, deny, and dismiss f
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Changed to enum.
On 2012/11/13 23:38:21, kkania w
| |
| 592 Browser* browser, | |
| 593 const std::string action, | |
| 594 const int infobar_index, | |
| 595 const int tab_index) { | |
| 596 InfoBarTabHelper* infobar_helper = InfoBarTabHelper::FromWebContents( | |
| 597 chrome::GetWebContentsAt(browser, tab_index)); | |
| 598 | |
| 599 InfoBarDelegate* infobar = infobar_helper->GetInfoBarDelegateAt( | |
| 600 infobar_index); | |
| 601 if ("Dismiss" == action) { | |
| 602 infobar->InfoBarDismissed(); | |
| 603 infobar_helper->RemoveInfoBar(infobar); | |
| 604 return true; | |
| 605 } else if ("Allow" == action) { | |
| 606 ConfirmInfoBarDelegate* confirm_bar = infobar->AsConfirmInfoBarDelegate(); | |
| 607 if (confirm_bar->Accept()) { | |
| 608 infobar_helper->RemoveInfoBar(infobar); | |
| 609 return true; | |
| 610 } | |
| 611 } else if ("Deny" == action) { | |
| 612 ConfirmInfoBarDelegate* confirm_bar = infobar->AsConfirmInfoBarDelegate(); | |
| 613 if (confirm_bar->Cancel()) { | |
| 614 infobar_helper->RemoveInfoBar(infobar); | |
| 615 return true; | |
| 616 } | |
| 617 } | |
| 618 | |
| 619 return false; | |
| 620 } | |
| 621 | |
| 622 void NotificationsPermissionTest::GetPrefsByContentSetting( | |
| 623 ContentSetting setting, | |
| 624 ContentSettingsForOneType* settings) { | |
| 625 ASSERT_TRUE(settings != NULL); | |
| 626 DesktopNotificationService* service = GetDesktopNotificationService(); | |
| 627 service->GetNotificationsSettings(settings); | |
| 628 for (ContentSettingsForOneType::iterator it = settings->begin(); | |
| 629 it != settings->end();) { | |
| 630 if (it->setting != setting || it->source.compare(kReference) != 0) { | |
| 631 it = settings->erase(it); | |
| 632 } else { | |
| 633 ++it; | |
| 634 } | |
| 635 } | |
| 636 } | |
| 637 | |
| 638 void NotificationsPermissionTest::PrintSettings( | |
| 639 const ContentSettingsForOneType & settings, | |
| 640 const std::string & type) { | |
| 641 LOG(INFO) << type << ":"; | |
| 642 for (ContentSettingsForOneType::const_iterator it = settings.begin(); | |
| 643 it != settings.end(); ++it) { | |
| 644 LOG(INFO) << it->primary_pattern << "," << it->secondary_pattern << "," | |
| 645 << it->incognito; | |
| 646 } | |
| 647 } | |
| 648 | |
| 20 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428. | 649 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428. |
| 21 IN_PROC_BROWSER_TEST_F(NotificationsPermissionTest, TestUserGestureInfobar) { | 650 IN_PROC_BROWSER_TEST_F(NotificationsPermissionTest, TestUserGestureInfobar) { |
| 22 ASSERT_TRUE(test_server()->Start()); | |
| 23 | |
| 24 ui_test_utils::NavigateToURL( | 651 ui_test_utils::NavigateToURL( |
| 25 browser(), | 652 browser(), |
| 26 test_server()->GetURL( | 653 test_server()->GetURL( |
| 27 "files/notifications/notifications_request_function.html")); | 654 "files/notifications/notifications_request_function.html")); |
| 28 | 655 |
| 29 // Request permission by calling request() while eval'ing an inline script; | 656 // 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. | 657 // That's considered a user gesture to webkit, and should produce an infobar. |
| 31 bool result; | 658 bool result; |
| 32 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( | 659 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 33 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 660 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
| 34 L"", | 661 L"", |
| 35 L"window.domAutomationController.send(request());", | 662 L"window.domAutomationController.send(request());", |
| 36 &result)); | 663 &result)); |
| 37 EXPECT_TRUE(result); | 664 EXPECT_TRUE(result); |
| 38 | 665 |
| 39 EXPECT_EQ(1U, InfoBarTabHelper::FromWebContents( | 666 EXPECT_EQ(1U, InfoBarTabHelper::FromWebContents( |
| 40 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); | 667 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); |
| 41 } | 668 } |
| 42 | 669 |
| 43 // If this flakes, use http://crbug.com/62311. | 670 // If this flakes, use http://crbug.com/62311. |
| 44 IN_PROC_BROWSER_TEST_F(NotificationsPermissionTest, TestNoUserGestureInfobar) { | 671 IN_PROC_BROWSER_TEST_F(NotificationsPermissionTest, TestNoUserGestureInfobar) { |
| 45 ASSERT_TRUE(test_server()->Start()); | |
| 46 | |
| 47 // Load a page which just does a request; no user gesture should result | 672 // Load a page which just does a request; no user gesture should result |
| 48 // in no infobar. | 673 // in no infobar. |
| 49 ui_test_utils::NavigateToURL( | 674 ui_test_utils::NavigateToURL( |
| 50 browser(), | 675 browser(), |
| 51 test_server()->GetURL( | 676 test_server()->GetURL( |
| 52 "files/notifications/notifications_request_inline.html")); | 677 "files/notifications/notifications_request_inline.html")); |
| 53 | 678 |
| 54 EXPECT_EQ(0U, InfoBarTabHelper::FromWebContents( | 679 EXPECT_EQ(0U, InfoBarTabHelper::FromWebContents( |
| 55 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); | 680 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); |
| 56 } | 681 } |
| 682 | |
| 683 IN_PROC_BROWSER_TEST_F( | |
| 684 NotificationsPermissionTest, | |
| 685 TestCreateSimpleNotification) { | |
| 686 // Creates a simple notification. | |
| 687 AllowAllOrigins(); | |
| 688 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 689 | |
| 690 std::string result = CreateSimpleNotification(browser(), true); | |
| 691 EXPECT_NE(result, "-1"); | |
| 692 | |
| 693 const std::deque<Balloon*>& balloons = GetActiveBalloons(); | |
| 694 ASSERT_EQ(1U, balloons.size()); | |
| 695 Balloon* balloon = balloons[0]; | |
| 696 const Notification& notification = balloon->notification(); | |
| 697 GURL EXPECTED_ICON_URL = test_server()->GetURL( | |
| 698 "files/notifications/no_such_file.png"); | |
|
kkania
2012/11/13 23:38:21
indent 4 from left.
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 699 EXPECT_EQ(EXPECTED_ICON_URL, notification.icon_url()); | |
| 700 EXPECT_EQ(string16(ASCIIToUTF16("My Title")), notification.title()); | |
|
kkania
2012/11/13 23:38:21
string16 constructor is not needed
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 701 EXPECT_EQ(string16(ASCIIToUTF16("My Body")), notification.body()); | |
|
kkania
2012/11/13 23:38:21
here too
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 702 } | |
| 703 | |
| 704 IN_PROC_BROWSER_TEST_F( | |
| 705 NotificationsPermissionTest, | |
| 706 TestCreateHTMLNotification) { | |
| 707 // Creates an HTML notification using a fake url. | |
| 708 // Note: webkitNotifications.createHTMLNotification is deprecated | |
|
kkania
2012/11/13 23:38:21
. at end of comments
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 709 AllowAllOrigins(); | |
| 710 | |
| 711 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 712 | |
| 713 std::string result = CreateHTMLNotification( | |
|
kkania
2012/11/13 23:38:21
std::string result = CreateHTMLNotification(
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 714 "../empty.html", | |
| 715 browser(), | |
| 716 "", | |
| 717 true); | |
| 718 EXPECT_EQ(result, "-1"); | |
|
kkania
2012/11/13 23:38:21
the standard is EXPECT_EQ(expected, actual);
You s
kkania
2012/11/13 23:38:21
why is this supposed to be -1? Does the createHTML
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Accoring to http://www.w3.org/TR/notifications/, h
| |
| 719 ASSERT_TRUE(CheckNotificationCount(0)); | |
| 720 } | |
| 721 | |
| 722 IN_PROC_BROWSER_TEST_F(NotificationsPermissionTest, TestCloseNotification) { | |
| 723 // Creates a notification and closes it. | |
| 724 AllowAllOrigins(); | |
| 725 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 726 | |
| 727 std::string result = CreateSimpleNotification(browser(), true); | |
| 728 EXPECT_NE(result, "-1"); | |
| 729 | |
| 730 const std::deque<Balloon*>& balloons = GetActiveBalloons(); | |
| 731 ASSERT_EQ(1U, balloons.size()); | |
| 732 EXPECT_TRUE(CloseNotificationAndWait(balloons[0]->notification())); | |
| 733 ASSERT_TRUE(CheckNotificationCount(0)); | |
|
kkania
2012/11/13 23:38:21
change this function to GetNotificationCount(), so
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 734 } | |
| 735 | |
| 736 IN_PROC_BROWSER_TEST_F(NotificationsPermissionTest, TestCancelNotification) { | |
| 737 // Creates a notification and cancels it in the origin page. | |
| 738 AllowAllOrigins(); | |
| 739 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 740 | |
| 741 std::string note_id = CreateSimpleNotification(browser(), true); | |
| 742 EXPECT_NE(note_id, "-1"); | |
| 743 | |
| 744 ASSERT_TRUE(CheckNotificationCount(1)); | |
| 745 CancelNotification(note_id, browser()); | |
| 746 ASSERT_TRUE(CheckNotificationCount(0)); | |
| 747 } | |
| 748 | |
| 749 IN_PROC_BROWSER_TEST_F( | |
| 750 NotificationsPermissionTest, | |
| 751 TestPermissionInfobarAppears) { | |
| 752 // Requests notification privileges and verifies the infobar appears. | |
| 753 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 754 RequestPermission(browser()); | |
| 755 ASSERT_TRUE(WaitForInfoBarCount(browser(), 1, 0, true)); | |
| 756 | |
| 757 ASSERT_TRUE(CheckNotificationCount(0)); | |
| 758 VerifyInfobar(browser(), TEST_PAGE_URL.host(), 0); | |
| 759 } | |
| 760 | |
| 761 IN_PROC_BROWSER_TEST_F( | |
| 762 NotificationsPermissionTest, | |
| 763 TestAllowOnPermissionInfobar) { | |
| 764 // Tries to create a notification and clicks allow on the infobar. | |
| 765 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 766 // This notification should not be shown because we do not have permission. | |
| 767 CreateSimpleNotification(browser(), false); | |
| 768 ASSERT_TRUE(CheckNotificationCount(0)); | |
| 769 | |
| 770 RequestPermission(browser()); | |
| 771 ASSERT_TRUE(WaitForInfoBarCount(browser(), 1, 0, true)); | |
| 772 ASSERT_TRUE(PerformActionOnInfobar(browser(), "Allow", 0, 0)); | |
| 773 | |
| 774 CreateSimpleNotification(browser(), true); | |
| 775 EXPECT_TRUE(CheckNotificationCount(1)); | |
| 776 } | |
| 777 | |
| 778 IN_PROC_BROWSER_TEST_F( | |
| 779 NotificationsPermissionTest, | |
| 780 TestOriginPreferencesBasic) { | |
| 781 // Tests that we can allow and deny origins. | |
| 782 GURL altavista("http://www.altavista.com"); | |
| 783 GURL gmail("http://www.gmail.com"); | |
| 784 GURL yahoo("http://www.yahoo.com"); | |
| 785 | |
| 786 std::vector<GURL> origins; | |
| 787 ContentSettingsForOneType settings; | |
| 788 | |
| 789 origins.push_back(altavista); | |
| 790 origins.push_back(gmail); | |
| 791 | |
| 792 SetDeniedOrigins(origins); | |
| 793 GetDeniedOrigins(&settings); | |
| 794 EXPECT_EQ(2U, settings.size()); | |
| 795 EXPECT_TRUE(checkOriginInSetting(settings, altavista)); | |
| 796 EXPECT_TRUE(checkOriginInSetting(settings, gmail)); | |
| 797 DenyOrigin(yahoo); | |
| 798 GetDeniedOrigins(&settings); | |
| 799 EXPECT_EQ(3U, settings.size()); | |
| 800 EXPECT_TRUE(checkOriginInSetting(settings, yahoo)); | |
| 801 DropOriginPreference(gmail); | |
| 802 GetDeniedOrigins(&settings); | |
| 803 EXPECT_EQ(2U, settings.size()); | |
| 804 EXPECT_FALSE(checkOriginInSetting(settings, gmail)); | |
| 805 | |
| 806 AllowOrigin(yahoo); | |
| 807 GetDeniedOrigins(&settings); | |
| 808 EXPECT_EQ(1U, settings.size()); | |
| 809 EXPECT_FALSE(checkOriginInSetting(settings, yahoo)); | |
| 810 GetAllowedOrigins(&settings); | |
| 811 EXPECT_TRUE(checkOriginInSetting(settings, yahoo)); | |
| 812 | |
| 813 origins.clear(); | |
| 814 origins.push_back(altavista); | |
| 815 origins.push_back(gmail); | |
| 816 SetAllowedOrigins(origins); | |
| 817 origins.clear(); | |
| 818 SetDeniedOrigins(origins); | |
| 819 GetAllowedOrigins(&settings); | |
| 820 EXPECT_TRUE(checkOriginInSetting(settings, altavista)); | |
| 821 EXPECT_TRUE(checkOriginInSetting(settings, gmail)); | |
| 822 AllowOrigin(yahoo); | |
| 823 GetAllowedOrigins(&settings); | |
| 824 EXPECT_TRUE(checkOriginInSetting(settings, yahoo)); | |
| 825 EXPECT_EQ(3U, settings.size()); | |
| 826 DropOriginPreference(gmail); | |
| 827 GetAllowedOrigins(&settings); | |
| 828 EXPECT_EQ(2U, settings.size()); | |
| 829 EXPECT_FALSE(checkOriginInSetting(settings, gmail)); | |
| 830 | |
| 831 DenyOrigin(yahoo); | |
| 832 GetAllowedOrigins(&settings); | |
| 833 EXPECT_EQ(1U, settings.size()); | |
| 834 EXPECT_FALSE(checkOriginInSetting(settings, yahoo)); | |
| 835 GetDeniedOrigins(&settings); | |
| 836 EXPECT_TRUE(checkOriginInSetting(settings, yahoo)); | |
| 837 } | |
| 838 | |
| 839 IN_PROC_BROWSER_TEST_F( | |
| 840 NotificationsPermissionTest, | |
| 841 TestDenyOnPermissionInfobar) { | |
| 842 // Test that no notification is created | |
| 843 // when Deny is chosen from permission infobar. | |
| 844 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 845 RequestPermission(browser()); | |
| 846 ASSERT_TRUE(WaitForInfoBarCount(browser(), 1, 0, true)); | |
| 847 PerformActionOnInfobar(browser(), "Deny", 0, 0); | |
| 848 CreateSimpleNotification(browser(), false); | |
| 849 ASSERT_TRUE(CheckNotificationCount(0)); | |
| 850 ContentSettingsForOneType settings; | |
| 851 GetDeniedOrigins(&settings); | |
| 852 EXPECT_TRUE(checkOriginInSetting(settings, TEST_PAGE_URL)); | |
| 853 } | |
| 854 | |
| 855 IN_PROC_BROWSER_TEST_F( | |
| 856 NotificationsPermissionTest, | |
| 857 TestClosePermissionInfobar) { | |
| 858 // Test that no notification is created when permission infobar is dismissed. | |
| 859 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 860 RequestPermission(browser()); | |
| 861 ASSERT_TRUE(WaitForInfoBarCount(browser(), 1, 0, true)); | |
| 862 PerformActionOnInfobar(browser(), "Dimiss", 0, 0); | |
| 863 CreateSimpleNotification(browser(), false); | |
| 864 ASSERT_TRUE(CheckNotificationCount(0)); | |
| 865 ContentSettingsForOneType settings; | |
| 866 GetDeniedOrigins(&settings); | |
| 867 EXPECT_EQ(0U, settings.size()); | |
| 868 } | |
| 869 | |
| 870 IN_PROC_BROWSER_TEST_F( | |
| 871 NotificationsPermissionTest, | |
| 872 TestNotificationWithPropertyMissing) { | |
| 873 // Test that a notification can be created if one property is missing. | |
| 874 AllowAllOrigins(); | |
| 875 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 876 | |
| 877 std::string result = CreateSimpleNotification(browser(), true); | |
| 878 EXPECT_NE(result, "-1"); | |
| 879 | |
| 880 const std::deque<Balloon*>& balloons = GetActiveBalloons(); | |
| 881 ASSERT_EQ(1U, balloons.size()); | |
| 882 Balloon* balloon = balloons[0]; | |
| 883 const Notification& notification = balloon->notification(); | |
| 884 GURL EXPECTED_ICON_URL = test_server()->GetURL( | |
|
kkania
2012/11/13 23:38:21
kExpectedIconUrl
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 885 "files/notifications/no_such_file.png"); | |
| 886 EXPECT_EQ(EXPECTED_ICON_URL, notification.icon_url()); | |
| 887 EXPECT_EQ(string16(ASCIIToUTF16("My Title")), notification.title()); | |
|
kkania
2012/11/13 23:38:21
string16 constructor is not needed
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Done.
| |
| 888 } | |
| 889 | |
| 890 IN_PROC_BROWSER_TEST_F( | |
| 891 NotificationsPermissionTest, | |
| 892 TestAllowNotificationsFromAllSites) { | |
| 893 // Verify that all domains can be allowed to show notifications. | |
| 894 SetDefaultPermissionSetting(kAllowAllSetting); | |
| 895 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 896 | |
| 897 std::string result = CreateSimpleNotification(browser(), true); | |
| 898 EXPECT_NE(result, "-1"); | |
| 899 | |
| 900 ASSERT_TRUE(CheckNotificationCount(1)); | |
| 901 EXPECT_EQ(0U, InfoBarTabHelper::FromWebContents( | |
| 902 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); | |
| 903 } | |
| 904 | |
| 905 IN_PROC_BROWSER_TEST_F( | |
| 906 NotificationsPermissionTest, | |
| 907 TestDenyNotificationsFromAllSites) { | |
| 908 // Verify that no domain can show notifications. | |
| 909 SetDefaultPermissionSetting(kDenyAllSetting); | |
| 910 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 911 | |
| 912 std::string result = CreateSimpleNotification(browser(), false); | |
| 913 EXPECT_EQ(result, "-1"); | |
| 914 | |
| 915 ASSERT_TRUE(CheckNotificationCount(0)); | |
| 916 } | |
| 917 | |
| 918 IN_PROC_BROWSER_TEST_F(NotificationsPermissionTest, TestDenyDomainAndAllowAll) { | |
| 919 // Verify that denying a domain and allowing all shouldn't show | |
| 920 // notifications from the denied domain. | |
| 921 DenyOrigin(TEST_PAGE_URL.GetOrigin()); | |
| 922 SetDefaultPermissionSetting(kAllowAllSetting); | |
| 923 | |
| 924 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 925 | |
| 926 std::string result = CreateSimpleNotification(browser(), false); | |
| 927 EXPECT_EQ(result, "-1"); | |
| 928 | |
| 929 ASSERT_TRUE(CheckNotificationCount(0)); | |
| 930 } | |
| 931 | |
| 932 IN_PROC_BROWSER_TEST_F(NotificationsPermissionTest, TestAllowDomainAndDenyAll) { | |
| 933 // Verify that allowing a domain and denying all others should show | |
| 934 // notifications from the allowed domain. | |
| 935 AllowOrigin(TEST_PAGE_URL.GetOrigin()); | |
| 936 SetDefaultPermissionSetting(kDenyAllSetting); | |
| 937 | |
| 938 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 939 | |
| 940 std::string result = CreateSimpleNotification(browser(), true); | |
| 941 EXPECT_NE(result, "-1"); | |
| 942 | |
| 943 ASSERT_TRUE(CheckNotificationCount(1)); | |
| 944 } | |
| 945 | |
| 946 IN_PROC_BROWSER_TEST_F( | |
| 947 NotificationsPermissionTest, | |
| 948 TestDenyAndThenAllowDomain) { | |
| 949 // Verify that denying and again allowing should show notifications. | |
| 950 DenyOrigin(TEST_PAGE_URL.GetOrigin()); | |
| 951 | |
| 952 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 953 | |
| 954 std::string result = CreateSimpleNotification(browser(), false); | |
| 955 EXPECT_EQ(result, "-1"); | |
| 956 | |
| 957 ASSERT_TRUE(CheckNotificationCount(0)); | |
| 958 | |
| 959 AllowOrigin(TEST_PAGE_URL.GetOrigin()); | |
| 960 result = CreateSimpleNotification(browser(), true); | |
| 961 EXPECT_NE(result, "-1"); | |
| 962 | |
| 963 ASSERT_TRUE(CheckNotificationCount(1)); | |
| 964 EXPECT_EQ(0U, InfoBarTabHelper::FromWebContents( | |
| 965 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); | |
| 966 } | |
| 967 | |
| 968 IN_PROC_BROWSER_TEST_F( | |
| 969 NotificationsPermissionTest, | |
| 970 TestCreateDenyCloseNotifications) { | |
| 971 // Verify able to create, deny, and close the notification. | |
| 972 AllowAllOrigins(); | |
| 973 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 974 CreateSimpleNotification(browser(), true); | |
| 975 ASSERT_TRUE(CheckNotificationCount(1)); | |
| 976 | |
| 977 DenyOrigin(TEST_PAGE_URL.GetOrigin()); | |
| 978 ContentSettingsForOneType settings; | |
| 979 GetDeniedOrigins(&settings); | |
| 980 ASSERT_TRUE(checkOriginInSetting(settings, TEST_PAGE_URL.GetOrigin())); | |
| 981 | |
| 982 const std::deque<Balloon*>& balloons1 = GetActiveBalloons(); | |
| 983 EXPECT_EQ(1U, balloons1.size()); | |
| 984 ASSERT_TRUE(CloseNotificationAndWait(balloons1[0]->notification())); | |
| 985 ASSERT_TRUE(CheckNotificationCount(0)); | |
| 986 } | |
| 987 | |
| 988 // Crashes on Linux/Win. See http://crbug.com/160657. | |
| 989 IN_PROC_BROWSER_TEST_F( | |
| 990 NotificationsPermissionTest, | |
| 991 DISABLED_TestOriginPrefsNotSavedInIncognito) { | |
| 992 // Verify that allow/deny origin preferences are not saved in incognito. | |
| 993 Browser* incognito = CreateBrowserWindow(browser(), true); | |
| 994 ui_test_utils::NavigateToURL(incognito, TEST_PAGE_URL); | |
| 995 RequestPermission(incognito); | |
| 996 ASSERT_TRUE(WaitForInfoBarCount(incognito, 1, 0, true)); | |
| 997 PerformActionOnInfobar(incognito, "Deny", 0, 0); | |
| 998 CloseBrowserWindow(incognito); | |
| 999 | |
| 1000 incognito = CreateBrowserWindow(browser(), true); | |
| 1001 ui_test_utils::NavigateToURL(incognito, TEST_PAGE_URL); | |
| 1002 RequestPermission(incognito); | |
| 1003 ASSERT_TRUE(WaitForInfoBarCount(incognito, 1, 0, true)); | |
| 1004 PerformActionOnInfobar(incognito, "Allow", 0, 0); | |
| 1005 CreateSimpleNotification(incognito, true); | |
| 1006 ASSERT_TRUE(CheckNotificationCount(1)); | |
| 1007 CloseBrowserWindow(incognito); | |
| 1008 | |
| 1009 incognito = CreateBrowserWindow(browser(), true); | |
| 1010 ui_test_utils::NavigateToURL(incognito, TEST_PAGE_URL); | |
| 1011 RequestPermission(incognito); | |
| 1012 ASSERT_TRUE(WaitForInfoBarCount(incognito, 1, 0, true)); | |
| 1013 | |
| 1014 ContentSettingsForOneType settings; | |
| 1015 GetDeniedOrigins(&settings); | |
| 1016 EXPECT_EQ(0U, settings.size()); | |
| 1017 GetAllowedOrigins(&settings); | |
| 1018 EXPECT_EQ(0U, settings.size()); | |
| 1019 } | |
| 1020 | |
| 1021 IN_PROC_BROWSER_TEST_F( | |
| 1022 NotificationsPermissionTest, | |
| 1023 TestExitBrowserWithInfobar) { | |
| 1024 // Exit the browser window, when the infobar appears. | |
| 1025 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 1026 RequestPermission(browser()); | |
| 1027 ASSERT_TRUE(WaitForInfoBarCount(browser(), 1, 0, true)); | |
| 1028 } | |
| 1029 | |
| 1030 IN_PROC_BROWSER_TEST_F( | |
| 1031 NotificationsPermissionTest, | |
| 1032 TestCrashTabWithPermissionInfobar) { | |
| 1033 // Test crashing the tab with permission infobar doesn't crash Chrome. | |
| 1034 ui_test_utils::NavigateToURLWithDisposition( | |
| 1035 browser(), | |
| 1036 EMPTY_PAGE_URL, | |
| 1037 NEW_BACKGROUND_TAB, | |
| 1038 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | |
| 1039 chrome::ActivateTabAt(browser(), 0, true); | |
| 1040 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 1041 RequestPermission(browser()); | |
| 1042 ASSERT_TRUE(WaitForInfoBarCount(browser(), 1, 0, true)); | |
| 1043 CrashTab(browser(), 0); | |
| 1044 } | |
| 1045 | |
| 1046 IN_PROC_BROWSER_TEST_F( | |
| 1047 NotificationsPermissionTest, | |
| 1048 TestKillNotificationProcess) { | |
| 1049 // Test killing a notification doesn't crash Chrome. | |
| 1050 AllowAllOrigins(); | |
| 1051 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 1052 CreateSimpleNotification(browser(), true); | |
| 1053 ASSERT_TRUE(CheckNotificationCount(1)); | |
| 1054 | |
| 1055 const std::deque<Balloon*>& balloons = GetActiveBalloons(); | |
| 1056 ASSERT_EQ(1U, balloons.size()); | |
| 1057 CrashNotification(balloons[0]); | |
| 1058 ASSERT_TRUE(CheckNotificationCount(0)); | |
| 1059 } | |
| 1060 | |
| 1061 IN_PROC_BROWSER_TEST_F(NotificationsPermissionTest, TestIncognitoNotification) { | |
| 1062 // Test notifications in incognito window. | |
| 1063 Browser* browser = CreateIncognitoBrowser(); | |
| 1064 ui_test_utils::NavigateToURL(browser, TEST_PAGE_URL); | |
| 1065 chrome::ActivateTabAt(browser, 0, true); | |
| 1066 RequestPermission(browser); | |
| 1067 ASSERT_TRUE(WaitForInfoBarCount(browser, 1, 0, true)); | |
| 1068 PerformActionOnInfobar(browser, "Allow", 0, 0); | |
| 1069 CreateSimpleNotification(browser, true); | |
| 1070 ASSERT_TRUE(CheckNotificationCount(1)); | |
| 1071 } | |
| 1072 | |
| 1073 IN_PROC_BROWSER_TEST_F( | |
| 1074 NotificationsPermissionTest, | |
| 1075 TestSpecialURLNotification) { | |
| 1076 // Test a page cannot create a notification to a chrome: url. | |
| 1077 AllowAllOrigins(); | |
| 1078 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 1079 CreateHTMLNotification("chrome://settings", browser(), "", true); | |
| 1080 ASSERT_TRUE(CheckNotificationCount(0)); | |
| 1081 } | |
| 1082 | |
| 1083 IN_PROC_BROWSER_TEST_F( | |
| 1084 NotificationsPermissionTest, | |
| 1085 TestCloseTabWithPermissionInfobar) { | |
| 1086 // Test that user can close tab when infobar present. | |
| 1087 ui_test_utils::NavigateToURLWithDisposition( | |
| 1088 browser(), | |
| 1089 GURL("about:blank"), | |
| 1090 NEW_BACKGROUND_TAB, | |
| 1091 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | |
| 1092 chrome::ActivateTabAt(browser(), 0, true); | |
| 1093 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 1094 RequestPermission(browser()); | |
| 1095 ASSERT_TRUE(WaitForInfoBarCount(browser(), 1, 0, true)); | |
| 1096 CloseTab(browser(), 0); | |
| 1097 } | |
| 1098 | |
| 1099 IN_PROC_BROWSER_TEST_F( | |
| 1100 NotificationsPermissionTest, | |
| 1101 TestNavigateAwayWithPermissionInfobar) { | |
| 1102 // Test navigating away when an infobar is present, | |
| 1103 // then trying to create a notification from the same page. | |
| 1104 ui_test_utils::NavigateToURLWithDisposition( | |
| 1105 browser(), | |
| 1106 GURL("about:blank"), | |
| 1107 NEW_BACKGROUND_TAB, | |
| 1108 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | |
| 1109 chrome::ActivateTabAt(browser(), 0, true); | |
| 1110 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 1111 RequestPermission(browser()); | |
| 1112 ASSERT_TRUE(WaitForInfoBarCount(browser(), 1, 0, true)); | |
| 1113 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 1114 RequestPermission(browser()); | |
| 1115 ASSERT_TRUE(WaitForInfoBarCount(browser(), 1, 0, true)); | |
| 1116 PerformActionOnInfobar(browser(), "Allow", 0, 0); | |
| 1117 CreateSimpleNotification(browser(), true); | |
| 1118 ASSERT_TRUE(CheckNotificationCount(1)); | |
| 1119 } | |
| 1120 | |
| 1121 IN_PROC_BROWSER_TEST_F( | |
| 1122 NotificationsPermissionTest, | |
| 1123 TestCrashRendererNotificationRemain) { | |
| 1124 // Test crashing renderer does not close or crash notification. | |
| 1125 AllowAllOrigins(); | |
| 1126 ui_test_utils::NavigateToURLWithDisposition( | |
| 1127 browser(), | |
| 1128 GURL("about:blank"), | |
| 1129 NEW_BACKGROUND_TAB, | |
| 1130 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | |
| 1131 chrome::ActivateTabAt(browser(), 0, true); | |
| 1132 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 1133 CreateSimpleNotification(browser(), true); | |
| 1134 ASSERT_TRUE(CheckNotificationCount(1)); | |
| 1135 CrashTab(browser(), 0); | |
| 1136 ASSERT_TRUE(CheckNotificationCount(1)); | |
| 1137 } | |
| 1138 | |
| 1139 IN_PROC_BROWSER_TEST_F( | |
| 1140 NotificationsPermissionTest, | |
| 1141 TestNotificationOrderAfterClosingOne) { | |
| 1142 // Tests that closing a notification leaves the rest | |
| 1143 // of the notifications in the correct order. | |
| 1144 // check for Win7 | |
| 1145 #if defined(OS_WIN) | |
| 1146 if (base::win::GetVersion() == VERSION_WIN7) { | |
| 1147 return; // crbug.com/66072 | |
|
kkania
2012/11/13 23:38:21
this bug says the test is currently disabled on ma
chrisgao (Use stgao instead)
2012/11/14 21:38:28
Not sure about that.
Should we disable this testca
| |
| 1148 } | |
| 1149 #endif | |
| 1150 | |
| 1151 AllowAllOrigins(); | |
| 1152 | |
| 1153 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 1154 | |
| 1155 CreateNotification(browser(), true, "", "Title1", "", "1"); | |
| 1156 CreateNotification(browser(), true, "", "Title2", "", "2"); | |
| 1157 CreateNotification(browser(), true, "", "Title3", "", "3"); | |
| 1158 | |
| 1159 EXPECT_TRUE(CheckNotificationCount(3)); | |
| 1160 const std::deque<Balloon*>& old_balloons = GetActiveBalloons(); | |
| 1161 | |
| 1162 ASSERT_TRUE(CloseNotificationAndWait(old_balloons[1]->notification())); | |
| 1163 | |
| 1164 const std::deque<Balloon*>& new_balloons = GetActiveBalloons(); | |
| 1165 ASSERT_EQ(2U, new_balloons.size()); | |
| 1166 EXPECT_EQ(old_balloons[0]->notification().notification_id(), | |
| 1167 new_balloons[0]->notification().notification_id()); | |
| 1168 EXPECT_EQ(old_balloons[2]->notification().notification_id(), | |
| 1169 new_balloons[1]->notification().notification_id()); | |
| 1170 } | |
| 1171 | |
| 1172 IN_PROC_BROWSER_TEST_F( | |
| 1173 NotificationsPermissionTest, | |
| 1174 TestNotificationReplacement) { | |
| 1175 // Test that we can replace a notification using the replaceId. | |
| 1176 AllowAllOrigins(); | |
| 1177 | |
| 1178 ui_test_utils::NavigateToURL(browser(), TEST_PAGE_URL); | |
| 1179 | |
| 1180 std::string result = CreateNotification( | |
| 1181 browser(), | |
| 1182 true, | |
| 1183 "", | |
| 1184 "Title2", | |
| 1185 "", | |
| 1186 "chat"); | |
| 1187 EXPECT_NE(result, "-1"); | |
| 1188 | |
| 1189 ASSERT_TRUE(CheckNotificationCount(1)); | |
| 1190 | |
| 1191 result = CreateNotification( | |
| 1192 browser(), | |
| 1193 false, | |
| 1194 "no_such_file.png", | |
| 1195 "", | |
| 1196 "", | |
| 1197 "chat"); | |
| 1198 EXPECT_NE(result, "-1"); | |
| 1199 | |
| 1200 const std::deque<Balloon*>& balloons2 = GetActiveBalloons(); | |
| 1201 EXPECT_EQ(1U, balloons2.size()); | |
| 1202 GURL EXPECTED_ICON_URL = test_server()->GetURL( | |
| 1203 "files/notifications/no_such_file.png"); | |
| 1204 const Notification& notification = balloons2[0]->notification(); | |
| 1205 EXPECT_EQ(EXPECTED_ICON_URL, notification.icon_url()); | |
| 1206 } | |
| OLD | NEW |