| 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" | 10 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 11 #include "chrome/browser/autocomplete/autocomplete_result.h" | 11 #include "chrome/browser/autocomplete/autocomplete_result.h" |
| 12 #include "chrome/browser/content_settings/host_content_settings_map.h" | 12 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 13 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 13 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| 14 #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" | 15 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" |
| 16 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_commands.h" | 17 #include "chrome/browser/ui/browser_commands.h" |
| 18 #include "chrome/browser/ui/browser_finder.h" | 18 #include "chrome/browser/ui/browser_finder.h" |
| 19 #include "chrome/browser/ui/browser_tabstrip.h" | 19 #include "chrome/browser/ui/browser_tabstrip.h" |
| 20 #include "chrome/browser/ui/browser_window.h" | 20 #include "chrome/browser/ui/browser_window.h" |
| 21 #include "chrome/browser/ui/omnibox/location_bar.h" | 21 #include "chrome/browser/ui/omnibox/location_bar.h" |
| 22 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" | 22 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" |
| 23 #include "chrome/browser/ui/omnibox/omnibox_view.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" | 24 #include "chrome/common/chrome_notification_types.h" |
| 26 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
| 27 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
| 28 #include "chrome/test/base/in_process_browser_test.h" | 27 #include "chrome/test/base/in_process_browser_test.h" |
| 29 #include "chrome/test/base/ui_test_utils.h" | 28 #include "chrome/test/base/ui_test_utils.h" |
| 30 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
| 31 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
| 32 #include "content/public/common/url_constants.h" | 31 #include "content/public/common/url_constants.h" |
| 33 #include "content/public/test/browser_test_utils.h" | 32 #include "content/public/test/browser_test_utils.h" |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 35 | 34 |
| 36 using content::WebContents; | 35 using content::WebContents; |
| 37 | 36 |
| 38 namespace { | 37 namespace { |
| 39 | 38 |
| 40 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("popup_blocker"); | 39 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("popup_blocker"); |
| 41 | 40 |
| 42 class PopupBlockerBrowserTest : public InProcessBrowserTest { | 41 class PopupBlockerBrowserTest : public InProcessBrowserTest { |
| 43 public: | 42 public: |
| 44 PopupBlockerBrowserTest() {} | 43 PopupBlockerBrowserTest() {} |
| 45 | 44 |
| 46 // Returns a url that shows one popup. | 45 // Returns a url that shows one popup. |
| 47 GURL GetTestURL() { | 46 GURL GetTestURL() { |
| 48 return ui_test_utils::GetTestUrl( | 47 return ui_test_utils::GetTestUrl( |
| 49 FilePath(kTestDir), | 48 FilePath(kTestDir), |
| 50 FilePath(FILE_PATH_LITERAL("popup-blocked-to-post-blank.html"))); | 49 FilePath(FILE_PATH_LITERAL("popup-blocked-to-post-blank.html"))); |
| 51 } | 50 } |
| 52 | 51 |
| 53 std::vector<TabContents*> GetBlockedContents(Browser* browser) { | 52 std::vector<WebContents*> GetBlockedContents(Browser* browser) { |
| 54 // Do a round trip to the renderer first to flush any in-flight IPCs to | 53 // Do a round trip to the renderer first to flush any in-flight IPCs to |
| 55 // create a to-be-blocked window. | 54 // create a to-be-blocked window. |
| 56 TabContents* tab = chrome::GetActiveTabContents(browser); | 55 WebContents* tab = chrome::GetActiveWebContents(browser); |
| 57 CHECK(content::ExecuteJavaScript( | 56 CHECK(content::ExecuteJavaScript(tab->GetRenderViewHost(), L"", L"")); |
| 58 tab->web_contents()->GetRenderViewHost(), L"", L"")); | 57 BlockedContentTabHelper* blocked_content_tab_helper = |
| 59 BlockedContentTabHelper* blocked_content = | 58 BlockedContentTabHelper::FromWebContents(tab); |
| 60 tab->blocked_content_tab_helper(); | 59 std::vector<WebContents*> blocked_contents; |
| 61 std::vector<TabContents*> blocked_contents; | 60 blocked_content_tab_helper->GetBlockedContents(&blocked_contents); |
| 62 blocked_content->GetBlockedContents(&blocked_contents); | |
| 63 return blocked_contents; | 61 return blocked_contents; |
| 64 } | 62 } |
| 65 | 63 |
| 66 void NavigateAndCheckPopupShown(Browser* browser, const GURL& url) { | 64 void NavigateAndCheckPopupShown(Browser* browser, const GURL& url) { |
| 67 content::WindowedNotificationObserver observer( | 65 content::WindowedNotificationObserver observer( |
| 68 chrome::NOTIFICATION_TAB_ADDED, | 66 chrome::NOTIFICATION_TAB_ADDED, |
| 69 content::NotificationService::AllSources()); | 67 content::NotificationService::AllSources()); |
| 70 ui_test_utils::NavigateToURL(browser, url); | 68 ui_test_utils::NavigateToURL(browser, url); |
| 71 observer.Wait(); | 69 observer.Wait(); |
| 72 | 70 |
| 73 ASSERT_EQ(2u, browser::GetBrowserCount(browser->profile())); | 71 ASSERT_EQ(2u, browser::GetBrowserCount(browser->profile())); |
| 74 | 72 |
| 75 std::vector<TabContents*> blocked_contents = GetBlockedContents(browser); | 73 std::vector<WebContents*> blocked_contents = GetBlockedContents(browser); |
| 76 ASSERT_TRUE(blocked_contents.empty()); | 74 ASSERT_TRUE(blocked_contents.empty()); |
| 77 } | 75 } |
| 78 | 76 |
| 79 void BasicTest(Browser* browser) { | 77 void BasicTest(Browser* browser) { |
| 80 GURL url(GetTestURL()); | 78 GURL url(GetTestURL()); |
| 81 ui_test_utils::NavigateToURL(browser, url); | 79 ui_test_utils::NavigateToURL(browser, url); |
| 82 | 80 |
| 83 // If the popup blocker blocked the blank post, there should be only one | 81 // 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 | 82 // tab in only one browser window and the URL of current tab must be equal |
| 85 // to the original URL. | 83 // to the original URL. |
| 86 EXPECT_EQ(1u, browser::GetBrowserCount(browser->profile())); | 84 EXPECT_EQ(1u, browser::GetBrowserCount(browser->profile())); |
| 87 EXPECT_EQ(1, browser->tab_count()); | 85 EXPECT_EQ(1, browser->tab_count()); |
| 88 WebContents* web_contents = chrome::GetActiveWebContents(browser); | 86 WebContents* web_contents = chrome::GetActiveWebContents(browser); |
| 89 EXPECT_EQ(url, web_contents->GetURL()); | 87 EXPECT_EQ(url, web_contents->GetURL()); |
| 90 | 88 |
| 91 std::vector<TabContents*> blocked_contents = GetBlockedContents(browser); | 89 std::vector<WebContents*> blocked_contents = GetBlockedContents(browser); |
| 92 ASSERT_EQ(1u, blocked_contents.size()); | 90 ASSERT_EQ(1u, blocked_contents.size()); |
| 93 | 91 |
| 94 content::WindowedNotificationObserver observer( | 92 content::WindowedNotificationObserver observer( |
| 95 chrome::NOTIFICATION_TAB_ADDED, | 93 chrome::NOTIFICATION_TAB_ADDED, |
| 96 content::NotificationService::AllSources()); | 94 content::NotificationService::AllSources()); |
| 97 | 95 |
| 98 TabContents* tab_contents = TabContents::FromWebContents(web_contents); | 96 BlockedContentTabHelper* blocked_content_tab_helper = |
| 99 BlockedContentTabHelper* blocked_content = | 97 BlockedContentTabHelper::FromWebContents(web_contents); |
| 100 tab_contents->blocked_content_tab_helper(); | 98 blocked_content_tab_helper->LaunchForContents(blocked_contents[0]); |
| 101 blocked_content->LaunchForContents(blocked_contents[0]); | |
| 102 | 99 |
| 103 observer.Wait(); | 100 observer.Wait(); |
| 104 } | 101 } |
| 105 }; | 102 }; |
| 106 | 103 |
| 107 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, PopupBlockedPostBlank) { | 104 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, PopupBlockedPostBlank) { |
| 108 BasicTest(browser()); | 105 BasicTest(browser()); |
| 109 } | 106 } |
| 110 | 107 |
| 111 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, | 108 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, |
| 112 PopupBlockedPostBlankIncognito) { | 109 PopupBlockedPostBlankIncognito) { |
| 113 BasicTest(CreateIncognitoBrowser()); | 110 BasicTest(CreateIncognitoBrowser()); |
| 114 } | 111 } |
| 115 | 112 |
| 116 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MultiplePopups) { | 113 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MultiplePopups) { |
| 117 GURL url(ui_test_utils::GetTestUrl( | 114 GURL url(ui_test_utils::GetTestUrl( |
| 118 FilePath(kTestDir), FilePath(FILE_PATH_LITERAL("popup-many.html")))); | 115 FilePath(kTestDir), FilePath(FILE_PATH_LITERAL("popup-many.html")))); |
| 119 ui_test_utils::NavigateToURL(browser(), url); | 116 ui_test_utils::NavigateToURL(browser(), url); |
| 120 std::vector<TabContents*> blocked_contents = GetBlockedContents(browser()); | 117 std::vector<WebContents*> blocked_contents = GetBlockedContents(browser()); |
| 121 ASSERT_EQ(2u, blocked_contents.size()); | 118 ASSERT_EQ(2u, blocked_contents.size()); |
| 122 } | 119 } |
| 123 | 120 |
| 124 // Verify that popups are launched on browser back button. | 121 // Verify that popups are launched on browser back button. |
| 125 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, | 122 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, |
| 126 AllowPopupThroughContentSetting) { | 123 AllowPopupThroughContentSetting) { |
| 127 GURL url(GetTestURL()); | 124 GURL url(GetTestURL()); |
| 128 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( | 125 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( |
| 129 ContentSettingsPattern::FromURL(url), | 126 ContentSettingsPattern::FromURL(url), |
| 130 ContentSettingsPattern::Wildcard(), | 127 ContentSettingsPattern::Wildcard(), |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 ASSERT_EQ(url, history_urls[1]); | 162 ASSERT_EQ(url, history_urls[1]); |
| 166 | 163 |
| 167 LocationBar* location_bar = browser()->window()->GetLocationBar(); | 164 LocationBar* location_bar = browser()->window()->GetLocationBar(); |
| 168 ui_test_utils::SendToOmniboxAndSubmit(location_bar, search_string); | 165 ui_test_utils::SendToOmniboxAndSubmit(location_bar, search_string); |
| 169 OmniboxEditModel* model = location_bar->GetLocationEntry()->model(); | 166 OmniboxEditModel* model = location_bar->GetLocationEntry()->model(); |
| 170 EXPECT_EQ(GURL(search_string), model->CurrentMatch().destination_url); | 167 EXPECT_EQ(GURL(search_string), model->CurrentMatch().destination_url); |
| 171 EXPECT_EQ(ASCIIToUTF16(search_string), model->CurrentMatch().contents); | 168 EXPECT_EQ(ASCIIToUTF16(search_string), model->CurrentMatch().contents); |
| 172 } | 169 } |
| 173 | 170 |
| 174 } // namespace | 171 } // namespace |
| OLD | NEW |