Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1963)

Unified Diff: chrome/browser/notifications/notification_interactive_uitest.cc

Issue 2729613006: When navigation focuses a web contents, also activate its window. (Closed)
Patch Set: Copyright + nits Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/notifications/notification_interactive_uitest_mac.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/notifications/notification_interactive_uitest.cc
diff --git a/chrome/browser/notifications/notification_interactive_uitest.cc b/chrome/browser/notifications/notification_interactive_uitest.cc
index 483c74ed923b571bc535e31d5607e9d3e0ee51de..52e0d2597fa16303f9b1e9a5756963b3eb75581b 100644
--- a/chrome/browser/notifications/notification_interactive_uitest.cc
+++ b/chrome/browser/notifications/notification_interactive_uitest.cc
@@ -11,22 +11,17 @@
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
-#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
-#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "base/time/clock.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
-#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/infobars/infobar_service.h"
-#include "chrome/browser/notifications/desktop_notification_profile_util.h"
#include "chrome/browser/notifications/notification.h"
+#include "chrome/browser/notifications/notification_interactive_uitest_support.h"
#include "chrome/browser/notifications/notification_test_util.h"
-#include "chrome/browser/notifications/web_notification_delegate.h"
-#include "chrome/browser/permissions/permission_request_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
@@ -34,12 +29,8 @@
#include "chrome/browser/ui/exclusive_access/exclusive_access_context.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
-#include "chrome/common/chrome_features.h"
-#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
-#include "components/content_settings/core/browser/host_content_settings_map.h"
-#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
@@ -97,327 +88,8 @@ namespace {
const char kExpectedIconUrl[] = "/notifications/no_such_file.png";
-class NotificationChangeObserver {
-public:
- virtual ~NotificationChangeObserver() {}
- virtual bool Wait() = 0;
-};
-
-class MessageCenterChangeObserver
- : public message_center::MessageCenterObserver,
- public NotificationChangeObserver {
- public:
- MessageCenterChangeObserver()
- : notification_received_(false) {
- message_center::MessageCenter::Get()->AddObserver(this);
- }
-
- ~MessageCenterChangeObserver() override {
- message_center::MessageCenter::Get()->RemoveObserver(this);
- }
-
- // NotificationChangeObserver:
- bool Wait() override {
- if (notification_received_)
- return true;
-
- message_loop_runner_ = new content::MessageLoopRunner;
- message_loop_runner_->Run();
- return notification_received_;
- }
-
- // message_center::MessageCenterObserver:
- void OnNotificationAdded(const std::string& notification_id) override {
- OnMessageCenterChanged();
- }
-
- void OnNotificationRemoved(const std::string& notification_id,
- bool by_user) override {
- OnMessageCenterChanged();
- }
-
- void OnNotificationUpdated(const std::string& notification_id) override {
- OnMessageCenterChanged();
- }
-
- void OnMessageCenterChanged() {
- notification_received_ = true;
- if (message_loop_runner_.get())
- message_loop_runner_->Quit();
- }
-
- bool notification_received_;
- scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
-
- DISALLOW_COPY_AND_ASSIGN(MessageCenterChangeObserver);
-};
-
-// Used to observe the creation of permission prompt without responding.
-class PermissionRequestObserver : public PermissionRequestManager::Observer {
- public:
- explicit PermissionRequestObserver(content::WebContents* web_contents)
- : request_manager_(
- PermissionRequestManager::FromWebContents(web_contents)),
- request_shown_(false),
- message_loop_runner_(new content::MessageLoopRunner) {
- request_manager_->AddObserver(this);
- }
- ~PermissionRequestObserver() override {
- // Safe to remove twice if it happens.
- request_manager_->RemoveObserver(this);
- }
-
- void Wait() { message_loop_runner_->Run(); }
-
- bool request_shown() { return request_shown_; }
-
- private:
- // PermissionRequestManager::Observer
- void OnBubbleAdded() override {
- request_shown_ = true;
- request_manager_->RemoveObserver(this);
- message_loop_runner_->Quit();
- }
-
- PermissionRequestManager* request_manager_;
- bool request_shown_;
- scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
-
- DISALLOW_COPY_AND_ASSIGN(PermissionRequestObserver);
-};
-
} // namespace
-class NotificationsTest : public InProcessBrowserTest {
- public:
- NotificationsTest() {}
-
- protected:
- int GetNotificationCount();
- int GetNotificationPopupCount();
-
- void CloseBrowserWindow(Browser* browser);
- void CrashTab(Browser* browser, int index);
-
- void DenyOrigin(const GURL& origin);
- void AllowOrigin(const GURL& origin);
- void AllowAllOrigins();
- void SetDefaultContentSetting(ContentSetting setting);
-
- std::string CreateNotification(Browser* browser,
- bool wait_for_new_balloon,
- const char* icon,
- const char* title,
- const char* body,
- const char* replace_id);
- std::string CreateSimpleNotification(Browser* browser,
- bool wait_for_new_balloon);
- bool RequestAndAcceptPermission(Browser* browser);
- bool RequestAndDenyPermission(Browser* browser);
- bool RequestAndDismissPermission(Browser* browser);
- bool RequestPermissionAndWait(Browser* browser);
- bool CancelNotification(const char* notification_id, Browser* browser);
- void GetPrefsByContentSetting(ContentSetting setting,
- ContentSettingsForOneType* settings);
- bool CheckOriginInSetting(const ContentSettingsForOneType& settings,
- const GURL& origin);
-
- GURL GetTestPageURLForFile(const std::string& file) const {
- return embedded_test_server()->GetURL(
- std::string("/notifications/") + file);
- }
-
- GURL GetTestPageURL() const {
- return GetTestPageURLForFile("notification_tester.html");
- }
-
- content::WebContents* GetActiveWebContents(Browser* browser) {
- return browser->tab_strip_model()->GetActiveWebContents();
- }
-
- protected:
- void EnableFullscreenNotifications() {
- feature_list_.InitWithFeatures({
- features::kPreferHtmlOverPlugins,
- features::kAllowFullscreenWebNotificationsFeature}, {});
- }
-
- void DisableFullscreenNotifications() {
- feature_list_.InitWithFeatures(
- {features::kPreferHtmlOverPlugins},
- {features::kAllowFullscreenWebNotificationsFeature});
- }
-
- private:
- void DropOriginPreference(const GURL& origin);
- std::string RequestAndRespondToPermission(
- Browser* browser,
- PermissionRequestManager::AutoResponseType bubble_response);
-
- base::test::ScopedFeatureList feature_list_;
-};
-
-int NotificationsTest::GetNotificationCount() {
- return message_center::MessageCenter::Get()->NotificationCount();
-}
-
-int NotificationsTest::GetNotificationPopupCount() {
- return message_center::MessageCenter::Get()->GetPopupNotifications().size();
-}
-
-void NotificationsTest::CloseBrowserWindow(Browser* browser) {
- content::WindowedNotificationObserver observer(
- chrome::NOTIFICATION_BROWSER_CLOSED,
- content::Source<Browser>(browser));
- browser->window()->Close();
- observer.Wait();
-}
-
-void NotificationsTest::CrashTab(Browser* browser, int index) {
- content::CrashTab(browser->tab_strip_model()->GetWebContentsAt(index));
-}
-
-void NotificationsTest::DenyOrigin(const GURL& origin) {
- DropOriginPreference(origin);
- DesktopNotificationProfileUtil::DenyPermission(browser()->profile(), origin);
-}
-
-void NotificationsTest::AllowOrigin(const GURL& origin) {
- DropOriginPreference(origin);
- DesktopNotificationProfileUtil::GrantPermission(browser()->profile(), origin);
-}
-
-void NotificationsTest::AllowAllOrigins() {
- // Reset all origins
- HostContentSettingsMapFactory::GetForProfile(browser()->profile())
- ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
- SetDefaultContentSetting(CONTENT_SETTING_ALLOW);
- }
-
-void NotificationsTest::SetDefaultContentSetting(ContentSetting setting) {
- HostContentSettingsMapFactory::GetForProfile(browser()->profile())
- ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting);
-}
-
-std::string NotificationsTest::CreateNotification(
- Browser* browser,
- bool wait_for_new_balloon,
- const char* icon,
- const char* title,
- const char* body,
- const char* replace_id) {
- std::string script = base::StringPrintf(
- "createNotification('%s', '%s', '%s', '%s');",
- icon, title, body, replace_id);
-
- MessageCenterChangeObserver observer;
- std::string result;
- bool success = content::ExecuteScriptAndExtractString(
- GetActiveWebContents(browser), script, &result);
- if (success && result != "-1" && wait_for_new_balloon)
- success = observer.Wait();
- EXPECT_TRUE(success);
-
- return result;
-}
-
-std::string NotificationsTest::CreateSimpleNotification(
- Browser* browser,
- bool wait_for_new_balloon) {
- return CreateNotification(
- browser, wait_for_new_balloon,
- "no_such_file.png", "My Title", "My Body", "");
-}
-
-std::string NotificationsTest::RequestAndRespondToPermission(
- Browser* browser,
- PermissionRequestManager::AutoResponseType bubble_response) {
- std::string result;
- content::WebContents* web_contents = GetActiveWebContents(browser);
- PermissionRequestManager::FromWebContents(web_contents)
- ->set_auto_response_for_test(bubble_response);
- EXPECT_TRUE(content::ExecuteScriptAndExtractString(
- web_contents, "requestPermission();", &result));
- return result;
-}
-
-bool NotificationsTest::RequestAndAcceptPermission(Browser* browser) {
- std::string result = RequestAndRespondToPermission(
- browser, PermissionRequestManager::ACCEPT_ALL);
- return "request-callback-granted" == result;
-}
-
-bool NotificationsTest::RequestAndDenyPermission(Browser* browser) {
- std::string result = RequestAndRespondToPermission(
- browser, PermissionRequestManager::DENY_ALL);
- return "request-callback-denied" == result;
-}
-
-bool NotificationsTest::RequestAndDismissPermission(Browser* browser) {
- std::string result =
- RequestAndRespondToPermission(browser, PermissionRequestManager::DISMISS);
- return "request-callback-default" == result;
-}
-
-bool NotificationsTest::RequestPermissionAndWait(Browser* browser) {
- content::WebContents* web_contents = GetActiveWebContents(browser);
- ui_test_utils::NavigateToURL(browser, GetTestPageURL());
- PermissionRequestObserver observer(web_contents);
- std::string result;
- EXPECT_TRUE(content::ExecuteScriptAndExtractString(
- web_contents, "requestPermissionAndRespond();", &result));
- EXPECT_EQ("requested", result);
- observer.Wait();
- return observer.request_shown();
-}
-
-bool NotificationsTest::CancelNotification(
- const char* notification_id,
- Browser* browser) {
- std::string script = base::StringPrintf(
- "cancelNotification('%s');",
- notification_id);
-
- MessageCenterChangeObserver observer;
- std::string result;
- bool success = content::ExecuteScriptAndExtractString(
- GetActiveWebContents(browser), script, &result);
- if (!success || result != "1")
- return false;
- return observer.Wait();
-}
-
-void NotificationsTest::GetPrefsByContentSetting(
- ContentSetting setting,
- ContentSettingsForOneType* settings) {
- DesktopNotificationProfileUtil::GetNotificationsSettings(
- browser()->profile(), settings);
- for (ContentSettingsForOneType::iterator it = settings->begin();
- it != settings->end(); ) {
- if (it->setting != setting || it->source.compare("preference") != 0)
- it = settings->erase(it);
- else
- ++it;
- }
-}
-
-bool NotificationsTest::CheckOriginInSetting(
- const ContentSettingsForOneType& settings,
- const GURL& origin) {
- ContentSettingsPattern pattern =
- ContentSettingsPattern::FromURLNoWildcard(origin);
- for (ContentSettingsForOneType::const_iterator it = settings.begin();
- it != settings.end(); ++it) {
- if (it->primary_pattern == pattern)
- return true;
- }
- return false;
-}
-
-void NotificationsTest::DropOriginPreference(const GURL& origin) {
- DesktopNotificationProfileUtil::ClearSetting(browser()->profile(), origin);
-}
-
// Flaky on Windows, Mac, Linux: http://crbug.com/437414.
IN_PROC_BROWSER_TEST_F(NotificationsTest, DISABLED_TestUserGestureInfobar) {
ASSERT_TRUE(embedded_test_server()->Start());
« no previous file with comments | « no previous file | chrome/browser/notifications/notification_interactive_uitest_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698