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/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/file_path.h" | 6 #include "base/file_path.h" |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 11 #include "chrome/browser/autocomplete/autocomplete_result.h" |
| 12 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 13 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
10 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" |
11 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_commands.h" |
12 #include "chrome/browser/ui/browser_finder.h" | 18 #include "chrome/browser/ui/browser_finder.h" |
13 #include "chrome/browser/ui/browser_tabstrip.h" | 19 #include "chrome/browser/ui/browser_tabstrip.h" |
14 #include "chrome/browser/ui/browser_window.h" | 20 #include "chrome/browser/ui/browser_window.h" |
| 21 #include "chrome/browser/ui/omnibox/location_bar.h" |
| 22 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" |
| 23 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
| 24 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 25 #include "chrome/common/chrome_notification_types.h" |
15 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
| 27 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/test/base/in_process_browser_test.h" | 28 #include "chrome/test/base/in_process_browser_test.h" |
17 #include "chrome/test/base/ui_test_utils.h" | 29 #include "chrome/test/base/ui_test_utils.h" |
18 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 31 #include "content/public/browser/notification_service.h" |
| 32 #include "content/public/common/url_constants.h" |
| 33 #include "content/public/test/browser_test_utils.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
20 | 35 |
21 using content::WebContents; | 36 using content::WebContents; |
22 | 37 |
23 namespace { | 38 namespace { |
24 | 39 |
25 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("popup_blocker"); | 40 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("popup_blocker"); |
26 | 41 |
27 typedef InProcessBrowserTest PopupBlockerBrowserTest; | 42 class PopupBlockerBrowserTest : public InProcessBrowserTest { |
| 43 public: |
| 44 PopupBlockerBrowserTest() {} |
| 45 |
| 46 // Returns a url that shows one popup. |
| 47 GURL GetTestURL() { |
| 48 return ui_test_utils::GetTestUrl( |
| 49 FilePath(kTestDir), |
| 50 FilePath(FILE_PATH_LITERAL("popup-blocked-to-post-blank.html"))); |
| 51 } |
| 52 |
| 53 std::vector<TabContents*> GetBlockedContents(Browser* browser) { |
| 54 // Do a round trip to the renderer first to flush any in-flight IPCs to |
| 55 // create a to-be-blocked window. |
| 56 TabContents* tab = chrome::GetActiveTabContents(browser); |
| 57 CHECK(content::ExecuteJavaScript( |
| 58 tab->web_contents()->GetRenderViewHost(), L"", L"")); |
| 59 BlockedContentTabHelper* blocked_content = |
| 60 tab->blocked_content_tab_helper(); |
| 61 std::vector<TabContents*> blocked_contents; |
| 62 blocked_content->GetBlockedContents(&blocked_contents); |
| 63 return blocked_contents; |
| 64 } |
| 65 |
| 66 void NavigateAndCheckPopupShown(Browser* browser, const GURL& url) { |
| 67 content::WindowedNotificationObserver observer( |
| 68 chrome::NOTIFICATION_TAB_ADDED, |
| 69 content::NotificationService::AllSources()); |
| 70 ui_test_utils::NavigateToURL(browser, url); |
| 71 observer.Wait(); |
| 72 |
| 73 ASSERT_EQ(2u, browser::GetBrowserCount(browser->profile())); |
| 74 |
| 75 std::vector<TabContents*> blocked_contents = GetBlockedContents(browser); |
| 76 ASSERT_TRUE(blocked_contents.empty()); |
| 77 } |
| 78 |
| 79 void BasicTest(Browser* browser) { |
| 80 GURL url(GetTestURL()); |
| 81 ui_test_utils::NavigateToURL(browser, url); |
| 82 |
| 83 // If the popup blocker blocked the blank post, there should be only one |
| 84 // tab in only one browser window and the URL of current tab must be equal |
| 85 // to the original URL. |
| 86 EXPECT_EQ(1u, browser::GetBrowserCount(browser->profile())); |
| 87 EXPECT_EQ(1, browser->tab_count()); |
| 88 WebContents* web_contents = chrome::GetActiveWebContents(browser); |
| 89 EXPECT_EQ(url, web_contents->GetURL()); |
| 90 |
| 91 std::vector<TabContents*> blocked_contents = GetBlockedContents(browser); |
| 92 ASSERT_EQ(1u, blocked_contents.size()); |
| 93 |
| 94 content::WindowedNotificationObserver observer( |
| 95 chrome::NOTIFICATION_TAB_ADDED, |
| 96 content::NotificationService::AllSources()); |
| 97 |
| 98 TabContents* tab_contents = TabContents::FromWebContents(web_contents); |
| 99 BlockedContentTabHelper* blocked_content = |
| 100 tab_contents->blocked_content_tab_helper(); |
| 101 blocked_content->LaunchForContents(blocked_contents[0]); |
| 102 |
| 103 observer.Wait(); |
| 104 } |
| 105 }; |
28 | 106 |
29 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, PopupBlockedPostBlank) { | 107 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, PopupBlockedPostBlank) { |
30 FilePath file_name(FILE_PATH_LITERAL("popup-blocked-to-post-blank.html")); | 108 BasicTest(browser()); |
31 FilePath test_dir(kTestDir); | 109 } |
32 GURL url(ui_test_utils::GetTestUrl(test_dir, file_name)); | 110 |
| 111 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, |
| 112 PopupBlockedPostBlankIncognito) { |
| 113 BasicTest(CreateIncognitoBrowser()); |
| 114 } |
| 115 |
| 116 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MultiplePopups) { |
| 117 GURL url(ui_test_utils::GetTestUrl( |
| 118 FilePath(kTestDir), FilePath(FILE_PATH_LITERAL("popup-many.html")))); |
| 119 ui_test_utils::NavigateToURL(browser(), url); |
| 120 std::vector<TabContents*> blocked_contents = GetBlockedContents(browser()); |
| 121 ASSERT_EQ(2u, blocked_contents.size()); |
| 122 } |
| 123 |
| 124 // Verify that popups are launched on browser back button. |
| 125 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, |
| 126 AllowPopupThroughContentSetting) { |
| 127 GURL url(GetTestURL()); |
| 128 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( |
| 129 ContentSettingsPattern::FromURL(url), |
| 130 ContentSettingsPattern::Wildcard(), |
| 131 CONTENT_SETTINGS_TYPE_POPUPS, |
| 132 "", |
| 133 CONTENT_SETTING_ALLOW); |
| 134 |
| 135 NavigateAndCheckPopupShown(browser(), url); |
| 136 } |
| 137 |
| 138 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, PopupsLaunchWhenTabIsClosed) { |
| 139 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 140 switches::kDisablePopupBlocking); |
| 141 GURL url = ui_test_utils::GetTestUrl( |
| 142 FilePath(kTestDir), |
| 143 FilePath(FILE_PATH_LITERAL("popup-on-unload.html"))); |
33 ui_test_utils::NavigateToURL(browser(), url); | 144 ui_test_utils::NavigateToURL(browser(), url); |
34 | 145 |
35 // If the popup blocker blocked the blank post, there should be only one | 146 NavigateAndCheckPopupShown(browser(), GURL(chrome::kAboutBlankURL)); |
36 // tab in only one browser window and the URL of current tab must be equal | 147 } |
37 // to the original URL. | 148 |
38 EXPECT_EQ(1u, browser::GetBrowserCount(browser()->profile())); | 149 // Verify that when you unblock popup, the popup shows in history and omnibox. |
39 EXPECT_EQ(1, browser()->tab_count()); | 150 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, |
40 WebContents* cur_tab = chrome::GetActiveWebContents(browser()); | 151 UnblockedPopupShowsInHistoryAndOmnibox) { |
41 ASSERT_TRUE(cur_tab); | 152 CommandLine::ForCurrentProcess()->AppendSwitch( |
42 EXPECT_EQ(url, cur_tab->GetURL()); | 153 switches::kDisablePopupBlocking); |
| 154 GURL url(GetTestURL()); |
| 155 NavigateAndCheckPopupShown(browser(), url); |
| 156 |
| 157 std::string search_string = |
| 158 "data:text/html,<title>Popup Success!</title>you should not see this " |
| 159 "message if popup blocker is enabled"; |
| 160 |
| 161 ui_test_utils::HistoryEnumerator history(browser()->profile()); |
| 162 std::vector<GURL>& history_urls = history.urls(); |
| 163 ASSERT_EQ(2u, history_urls.size()); |
| 164 ASSERT_EQ(GURL(search_string), history_urls[0]); |
| 165 ASSERT_EQ(url, history_urls[1]); |
| 166 |
| 167 LocationBar* location_bar = browser()->window()->GetLocationBar(); |
| 168 ui_test_utils::SendToOmniboxAndSubmit(location_bar, search_string); |
| 169 OmniboxEditModel* model = location_bar->GetLocationEntry()->model(); |
| 170 EXPECT_EQ(GURL(search_string), model->CurrentMatch().destination_url); |
| 171 EXPECT_EQ(ASCIIToUTF16(search_string), model->CurrentMatch().contents); |
43 } | 172 } |
44 | 173 |
45 } // namespace | 174 } // namespace |
OLD | NEW |