[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <string> |
| 6 | |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 7 | #include "base/strings/utf_string_conversions.h" |
| 8 | #include "chrome/browser/download/download_prefs.h" |
| 9 | #include "chrome/browser/prefs/pref_service_syncable.h" |
| 10 | #include "chrome/browser/ui/browser.h" |
| 11 | #include "chrome/browser/ui/browser_commands.h" |
| 12 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 13 | #include "chrome/common/chrome_paths.h" |
| 14 | #include "chrome/common/pref_names.h" |
| 15 | #include "chrome/test/base/in_process_browser_test.h" |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 16 | #include "chrome/test/base/testing_browser_process.h" |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 17 | #include "chrome/test/base/testing_profile.h" |
| 18 | #include "chrome/test/base/ui_test_utils.h" |
raymes | bc7409e | 2015-08-06 01:57:45 | [diff] [blame] | 19 | #include "components/content_settings/core/browser/website_settings_info.h" |
| 20 | #include "components/content_settings/core/browser/website_settings_registry.h" |
| 21 | #include "components/content_settings/core/common/content_settings_types.h" |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 22 | #include "content/public/browser/web_contents.h" |
| 23 | #include "content/public/test/browser_test_utils.h" |
| 24 | #include "content/public/test/download_test_observer.h" |
| 25 | |
| 26 | using content::BrowserContext; |
| 27 | using content::DownloadManager; |
| 28 | |
| 29 | class PrefsFunctionalTest : public InProcessBrowserTest { |
| 30 | protected: |
| 31 | // Create a DownloadTestObserverTerminal that will wait for the |
| 32 | // specified number of downloads to finish. |
| 33 | scoped_ptr<content::DownloadTestObserver> CreateWaiter(Browser* browser, |
| 34 | int num_downloads) { |
| 35 | DownloadManager* download_manager = |
| 36 | BrowserContext::GetDownloadManager(browser->profile()); |
| 37 | |
| 38 | content::DownloadTestObserver* downloads_observer = |
| 39 | new content::DownloadTestObserverTerminal( |
| 40 | download_manager, |
| 41 | num_downloads, |
| 42 | content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); |
| 43 | return make_scoped_ptr(downloads_observer); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestDownloadDirPref) { |
| 48 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 5079b2b | 2014-05-10 21:31:32 | [diff] [blame] | 49 | base::ScopedTempDir new_download_dir; |
| 50 | ASSERT_TRUE(new_download_dir.CreateUniqueTempDir()); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 51 | |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 52 | base::FilePath downloaded_pkg = |
[email protected] | 5079b2b | 2014-05-10 21:31:32 | [diff] [blame] | 53 | new_download_dir.path().AppendASCII("a_zip_file.zip"); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 54 | |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 55 | // Set pref to download in new_download_dir. |
| 56 | browser()->profile()->GetPrefs()->SetFilePath( |
[email protected] | 5079b2b | 2014-05-10 21:31:32 | [diff] [blame] | 57 | prefs::kDownloadDefaultDirectory, new_download_dir.path()); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 58 | |
| 59 | // Create a downloads observer. |
| 60 | scoped_ptr<content::DownloadTestObserver> downloads_observer( |
| 61 | CreateWaiter(browser(), 1)); |
| 62 | ui_test_utils::NavigateToURL( |
| 63 | browser(), |
| 64 | test_server()->GetURL("files/downloads/a_zip_file.zip")); |
| 65 | // Waits for the download to complete. |
| 66 | downloads_observer->WaitForFinished(); |
| 67 | EXPECT_TRUE(base::PathExists(downloaded_pkg)); |
| 68 | } |
| 69 | |
| 70 | // Verify image content settings show or hide images. |
| 71 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestImageContentSettings) { |
| 72 | ASSERT_TRUE(test_server()->Start()); |
| 73 | |
| 74 | ui_test_utils::NavigateToURL( |
| 75 | browser(), |
| 76 | test_server()->GetURL("files/settings/image_page.html")); |
| 77 | |
| 78 | bool result = false; |
| 79 | std::string script = |
| 80 | "for (i=0; i < document.images.length; i++) {" |
| 81 | " if ((document.images[i].naturalWidth != 0) &&" |
| 82 | " (document.images[i].naturalHeight != 0)) {" |
| 83 | " window.domAutomationController.send(true);" |
| 84 | " }" |
| 85 | "}" |
| 86 | "window.domAutomationController.send(false);"; |
| 87 | EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 88 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 89 | script, |
| 90 | &result)); |
| 91 | EXPECT_TRUE(result); |
| 92 | |
msramek | 9681220 | 2015-07-27 16:00:02 | [diff] [blame] | 93 | browser()->profile()->GetPrefs()->SetInteger( |
raymes | bc7409e | 2015-08-06 01:57:45 | [diff] [blame] | 94 | content_settings::WebsiteSettingsRegistry::GetInstance() |
| 95 | ->Get(CONTENT_SETTINGS_TYPE_IMAGES) |
| 96 | ->default_value_pref_name(), |
| 97 | CONTENT_SETTING_BLOCK); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 98 | |
| 99 | ui_test_utils::NavigateToURL( |
| 100 | browser(), |
| 101 | test_server()->GetURL("files/settings/image_page.html")); |
| 102 | |
| 103 | result = false; |
| 104 | EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 105 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 106 | script, |
| 107 | &result)); |
| 108 | EXPECT_FALSE(result); |
| 109 | } |
| 110 | |
| 111 | // Verify that enabling/disabling Javascript in prefs works. |
| 112 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestJavascriptEnableDisable) { |
| 113 | ASSERT_TRUE(test_server()->Start()); |
| 114 | |
| 115 | EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean( |
| 116 | prefs::kWebKitJavascriptEnabled)); |
| 117 | ui_test_utils::NavigateToURL( |
| 118 | browser(), |
| 119 | test_server()->GetURL("files/javaScriptTitle.html")); |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 120 | EXPECT_EQ(base::ASCIIToUTF16("Title from script javascript enabled"), |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 121 | browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); |
| 122 | browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled, |
| 123 | false); |
| 124 | ui_test_utils::NavigateToURL( |
| 125 | browser(), |
| 126 | test_server()->GetURL("files/javaScriptTitle.html")); |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 127 | EXPECT_EQ(base::ASCIIToUTF16("This is html title"), |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 128 | browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); |
| 129 | } |
| 130 | |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 131 | // Verify restore for bookmark bar visibility. |
| 132 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, |
| 133 | TestSessionRestoreShowBookmarkBar) { |
| 134 | EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean( |
tfarina | 3bddbe11 | 2014-08-28 05:29:32 | [diff] [blame] | 135 | bookmarks::prefs::kShowBookmarkBar)); |
| 136 | browser()->profile()->GetPrefs()->SetBoolean( |
| 137 | bookmarks::prefs::kShowBookmarkBar, true); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 138 | EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean( |
tfarina | 3bddbe11 | 2014-08-28 05:29:32 | [diff] [blame] | 139 | bookmarks::prefs::kShowBookmarkBar)); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 140 | |
| 141 | EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean( |
tfarina | 3bddbe11 | 2014-08-28 05:29:32 | [diff] [blame] | 142 | bookmarks::prefs::kShowBookmarkBar)); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 143 | EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state()); |
| 144 | } |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 145 | |
| 146 | // Verify images are not blocked in incognito mode. |
| 147 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestImagesNotBlockedInIncognito) { |
| 148 | ASSERT_TRUE(test_server()->Start()); |
| 149 | GURL url = test_server()->GetURL("files/settings/image_page.html"); |
| 150 | Browser* incognito_browser = CreateIncognitoBrowser(); |
thestig | 53986dc | 2014-12-16 06:09:18 | [diff] [blame] | 151 | ui_test_utils::NavigateToURL(incognito_browser, url); |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 152 | |
| 153 | bool result = false; |
| 154 | std::string script = |
| 155 | "for (i=0; i < document.images.length; i++) {" |
| 156 | " if ((document.images[i].naturalWidth != 0) &&" |
| 157 | " (document.images[i].naturalHeight != 0)) {" |
| 158 | " window.domAutomationController.send(true);" |
| 159 | " }" |
| 160 | "}" |
| 161 | "window.domAutomationController.send(false);"; |
| 162 | EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 163 | incognito_browser->tab_strip_model()->GetActiveWebContents(), |
| 164 | script, |
| 165 | &result)); |
| 166 | EXPECT_TRUE(result); |
| 167 | } |
| 168 | |
| 169 | // Verify setting homepage preference to newtabpage across restarts. Part1 |
| 170 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestHomepageNewTabpagePrefs) { |
| 171 | browser()->profile()->GetPrefs()->SetBoolean(prefs::kHomePageIsNewTabPage, |
| 172 | true); |
| 173 | } |
| 174 | |
| 175 | // Verify setting homepage preference to newtabpage across restarts. Part2 |
| 176 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHomepageNewTabpagePrefs) { |
| 177 | EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean( |
| 178 | prefs::kHomePageIsNewTabPage)); |
| 179 | } |
| 180 | |
| 181 | // Verify setting homepage preference to specific url. Part1 |
| 182 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestHomepagePrefs) { |
| 183 | GURL home_page_url("http://www.google.com"); |
| 184 | |
| 185 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 186 | prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false); |
| 187 | const PrefService::Preference* pref = |
| 188 | prefs->FindPreference(prefs::kHomePage); |
| 189 | if (pref && !pref->IsManaged()) { |
| 190 | prefs->SetString(prefs::kHomePage, home_page_url.spec()); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // Verify setting homepage preference to specific url. Part2 |
| 195 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHomepagePrefs) { |
| 196 | GURL home_page_url("http://www.google.com"); |
| 197 | |
| 198 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 199 | EXPECT_FALSE(prefs->GetBoolean(prefs::kHomePageIsNewTabPage)); |
| 200 | EXPECT_EQ(home_page_url.spec(), prefs->GetString(prefs::kHomePage)); |
| 201 | } |
| 202 | |
| 203 | // Verify the security preference under privacy across restarts. Part1 |
| 204 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestPrivacySecurityPrefs) { |
| 205 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 206 | |
| 207 | EXPECT_TRUE(prefs->GetBoolean(prefs::kNetworkPredictionEnabled)); |
| 208 | prefs->SetBoolean(prefs::kNetworkPredictionEnabled, false); |
| 209 | |
| 210 | EXPECT_TRUE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled)); |
| 211 | prefs->SetBoolean(prefs::kSafeBrowsingEnabled, false); |
| 212 | |
| 213 | EXPECT_TRUE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)); |
| 214 | prefs->SetBoolean(prefs::kAlternateErrorPagesEnabled, false); |
| 215 | |
| 216 | EXPECT_TRUE(prefs->GetBoolean(prefs::kSearchSuggestEnabled)); |
| 217 | prefs->SetBoolean(prefs::kSearchSuggestEnabled, false); |
| 218 | } |
| 219 | |
| 220 | // Verify the security preference under privacy across restarts. Part2 |
| 221 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestPrivacySecurityPrefs) { |
| 222 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 223 | |
| 224 | EXPECT_FALSE(prefs->GetBoolean(prefs::kNetworkPredictionEnabled)); |
| 225 | EXPECT_FALSE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled)); |
| 226 | EXPECT_FALSE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)); |
| 227 | EXPECT_FALSE(prefs->GetBoolean(prefs::kSearchSuggestEnabled)); |
| 228 | } |
| 229 | |
| 230 | // Verify that we have some Local State prefs. |
| 231 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHaveLocalStatePrefs) { |
[email protected] | ddf421c3 | 2013-11-01 00:52:35 | [diff] [blame] | 232 | EXPECT_TRUE(g_browser_process->local_state()->GetPreferenceValues().get()); |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 233 | } |
| 234 | |