Index: chrome/browser/popup_blocker_browsertest.cc |
=================================================================== |
--- chrome/browser/popup_blocker_browsertest.cc (revision 154448) |
+++ chrome/browser/popup_blocker_browsertest.cc (working copy) |
@@ -7,15 +7,30 @@ |
#include "base/file_util.h" |
#include "base/message_loop.h" |
#include "base/path_service.h" |
+#include "chrome/browser/autocomplete/autocomplete_match.h" |
+#include "chrome/browser/autocomplete/autocomplete_result.h" |
+#include "chrome/browser/content_settings/host_content_settings_map.h" |
+#include "chrome/browser/content_settings/tab_specific_content_settings.h" |
#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" |
#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/browser_commands.h" |
#include "chrome/browser/ui/browser_finder.h" |
#include "chrome/browser/ui/browser_tabstrip.h" |
#include "chrome/browser/ui/browser_window.h" |
+#include "chrome/browser/ui/omnibox/location_bar.h" |
+#include "chrome/browser/ui/omnibox/omnibox_edit_model.h" |
+#include "chrome/browser/ui/omnibox/omnibox_view.h" |
+#include "chrome/browser/ui/tab_contents/tab_contents.h" |
+#include "chrome/common/chrome_notification_types.h" |
#include "chrome/common/chrome_paths.h" |
+#include "chrome/common/chrome_switches.h" |
#include "chrome/test/base/in_process_browser_test.h" |
#include "chrome/test/base/ui_test_utils.h" |
#include "content/public/browser/web_contents.h" |
+#include "content/public/browser/notification_service.h" |
+#include "content/public/common/url_constants.h" |
+#include "content/public/test/browser_test_utils.h" |
#include "testing/gtest/include/gtest/gtest.h" |
using content::WebContents; |
@@ -24,22 +39,136 @@ |
static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("popup_blocker"); |
-typedef InProcessBrowserTest PopupBlockerBrowserTest; |
+class PopupBlockerBrowserTest : public InProcessBrowserTest { |
+ public: |
+ PopupBlockerBrowserTest() {} |
+ // Returns a url that shows one popup. |
+ GURL GetTestURL() { |
+ return ui_test_utils::GetTestUrl( |
+ FilePath(kTestDir), |
+ FilePath(FILE_PATH_LITERAL("popup-blocked-to-post-blank.html"))); |
+ } |
+ |
+ std::vector<TabContents*> GetBlockedContents(Browser* browser) { |
+ // Do a round trip to the renderer first to flush any in-flight IPCs to |
+ // create a to-be-blocked window. |
+ TabContents* tab = chrome::GetActiveTabContents(browser); |
+ CHECK(content::ExecuteJavaScript( |
+ tab->web_contents()->GetRenderViewHost(), L"", L"")); |
+ BlockedContentTabHelper* blocked_content = |
+ tab->blocked_content_tab_helper(); |
+ std::vector<TabContents*> blocked_contents; |
+ blocked_content->GetBlockedContents(&blocked_contents); |
+ return blocked_contents; |
+ } |
+ |
+ void NavigateAndCheckPopupShown(Browser* browser, const GURL& url) { |
+ content::WindowedNotificationObserver observer( |
+ chrome::NOTIFICATION_TAB_ADDED, |
+ content::NotificationService::AllSources()); |
+ ui_test_utils::NavigateToURL(browser, url); |
+ observer.Wait(); |
+ |
+ ASSERT_EQ(2u, browser::GetBrowserCount(browser->profile())); |
+ |
+ std::vector<TabContents*> blocked_contents = GetBlockedContents(browser); |
+ ASSERT_TRUE(blocked_contents.empty()); |
+ } |
+ |
+ void BasicTest(Browser* browser) { |
+ GURL url(GetTestURL()); |
+ ui_test_utils::NavigateToURL(browser, url); |
+ |
+ // If the popup blocker blocked the blank post, there should be only one |
+ // tab in only one browser window and the URL of current tab must be equal |
+ // to the original URL. |
+ EXPECT_EQ(1u, browser::GetBrowserCount(browser->profile())); |
+ EXPECT_EQ(1, browser->tab_count()); |
+ WebContents* web_contents = chrome::GetActiveWebContents(browser); |
+ EXPECT_EQ(url, web_contents->GetURL()); |
+ |
+ std::vector<TabContents*> blocked_contents = GetBlockedContents(browser); |
+ ASSERT_EQ(1u, blocked_contents.size()); |
+ |
+ content::WindowedNotificationObserver observer( |
+ chrome::NOTIFICATION_TAB_ADDED, |
+ content::NotificationService::AllSources()); |
+ |
+ TabContents* tab_contents = TabContents::FromWebContents(web_contents); |
+ BlockedContentTabHelper* blocked_content = |
+ tab_contents->blocked_content_tab_helper(); |
+ blocked_content->LaunchForContents(blocked_contents[0]); |
+ |
+ observer.Wait(); |
+ } |
+}; |
+ |
IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, PopupBlockedPostBlank) { |
- FilePath file_name(FILE_PATH_LITERAL("popup-blocked-to-post-blank.html")); |
- FilePath test_dir(kTestDir); |
- GURL url(ui_test_utils::GetTestUrl(test_dir, file_name)); |
+ BasicTest(browser()); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, |
+ PopupBlockedPostBlankIncognito) { |
+ BasicTest(CreateIncognitoBrowser()); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MultiplePopups) { |
+ GURL url(ui_test_utils::GetTestUrl( |
+ FilePath(kTestDir), FilePath(FILE_PATH_LITERAL("popup-many.html")))); |
ui_test_utils::NavigateToURL(browser(), url); |
+ std::vector<TabContents*> blocked_contents = GetBlockedContents(browser()); |
+ ASSERT_EQ(2u, blocked_contents.size()); |
+} |
- // If the popup blocker blocked the blank post, there should be only one |
- // tab in only one browser window and the URL of current tab must be equal |
- // to the original URL. |
- EXPECT_EQ(1u, browser::GetBrowserCount(browser()->profile())); |
- EXPECT_EQ(1, browser()->tab_count()); |
- WebContents* cur_tab = chrome::GetActiveWebContents(browser()); |
- ASSERT_TRUE(cur_tab); |
- EXPECT_EQ(url, cur_tab->GetURL()); |
+// Verify that popups are launched on browser back button. |
+IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, |
+ AllowPopupThroughContentSetting) { |
+ GURL url(GetTestURL()); |
+ browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( |
+ ContentSettingsPattern::FromURL(url), |
+ ContentSettingsPattern::Wildcard(), |
+ CONTENT_SETTINGS_TYPE_POPUPS, |
+ "", |
+ CONTENT_SETTING_ALLOW); |
+ |
+ NavigateAndCheckPopupShown(browser(), url); |
} |
+IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, PopupsLaunchWhenTabIsClosed) { |
+ CommandLine::ForCurrentProcess()->AppendSwitch( |
+ switches::kDisablePopupBlocking); |
+ GURL url = ui_test_utils::GetTestUrl( |
+ FilePath(kTestDir), |
+ FilePath(FILE_PATH_LITERAL("popup-on-unload.html"))); |
+ ui_test_utils::NavigateToURL(browser(), url); |
+ |
+ NavigateAndCheckPopupShown(browser(), GURL(chrome::kAboutBlankURL)); |
+} |
+ |
+// Verify that when you unblock popup, the popup shows in history and omnibox. |
+IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, |
+ UnblockedPopupShowsInHistoryAndOmnibox) { |
+ CommandLine::ForCurrentProcess()->AppendSwitch( |
+ switches::kDisablePopupBlocking); |
+ GURL url(GetTestURL()); |
+ NavigateAndCheckPopupShown(browser(), url); |
+ |
+ std::string search_string = |
+ "data:text/html,<title>Popup Success!</title>you should not see this " |
+ "message if popup blocker is enabled"; |
+ |
+ ui_test_utils::HistoryEnumerator history(browser()->profile()); |
+ std::vector<GURL>& history_urls = history.urls(); |
+ ASSERT_EQ(2u, history_urls.size()); |
+ ASSERT_EQ(GURL(search_string), history_urls[0]); |
+ ASSERT_EQ(url, history_urls[1]); |
+ |
+ LocationBar* location_bar = browser()->window()->GetLocationBar(); |
+ ui_test_utils::SendToOmniboxAndSubmit(location_bar, search_string); |
+ OmniboxEditModel* model = location_bar->GetLocationEntry()->model(); |
+ EXPECT_EQ(GURL(search_string), model->CurrentMatch().destination_url); |
+ EXPECT_EQ(ASCIIToUTF16(search_string), model->CurrentMatch().contents); |
+} |
+ |
} // namespace |