Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 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 | |
Matt Menke | f6f308fe | 2020-10-28 05:18:16 | [diff] [blame] | 7 | #include "base/files/file_util.h" |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 8 | #include "base/memory/ptr_util.h" |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
jam | 3f2d393 | 2017-04-26 20:28:51 | [diff] [blame] | 10 | #include "base/threading/thread_restrictions.h" |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 11 | #include "chrome/browser/download/download_prefs.h" |
Simon Pelchat | e74bbb6 | 2021-12-03 23:57:55 | [diff] [blame] | 12 | #include "chrome/browser/prefetch/prefetch_prefs.h" |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 13 | #include "chrome/browser/ui/browser.h" |
| 14 | #include "chrome/browser/ui/browser_commands.h" |
| 15 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 16 | #include "chrome/common/pref_names.h" |
| 17 | #include "chrome/test/base/in_process_browser_test.h" |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 18 | #include "chrome/test/base/testing_browser_process.h" |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 19 | #include "chrome/test/base/testing_profile.h" |
| 20 | #include "chrome/test/base/ui_test_utils.h" |
brettw | e1f0af8b | 2015-10-09 21:30:46 | [diff] [blame] | 21 | #include "components/bookmarks/common/bookmark_pref_names.h" |
raymes | bc7409e | 2015-08-06 01:57:45 | [diff] [blame] | 22 | #include "components/content_settings/core/browser/website_settings_info.h" |
| 23 | #include "components/content_settings/core/browser/website_settings_registry.h" |
| 24 | #include "components/content_settings/core/common/content_settings_types.h" |
brettw | e1f0af8b | 2015-10-09 21:30:46 | [diff] [blame] | 25 | #include "components/content_settings/core/common/pref_names.h" |
Colin Blundell | ab45f55 | 2020-01-22 15:08:40 | [diff] [blame] | 26 | #include "components/embedder_support/pref_names.h" |
Ali Juma | fb3dc1f | 2020-01-07 17:33:47 | [diff] [blame] | 27 | #include "components/safe_browsing/core/common/safe_browsing_prefs.h" |
maxbogue | ea16ff41 | 2016-10-28 16:35:29 | [diff] [blame] | 28 | #include "components/sync_preferences/pref_service_syncable.h" |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 29 | #include "content/public/browser/web_contents.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 30 | #include "content/public/test/browser_test.h" |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 31 | #include "content/public/test/browser_test_utils.h" |
| 32 | #include "content/public/test/download_test_observer.h" |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 33 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 34 | |
| 35 | using content::BrowserContext; |
| 36 | using content::DownloadManager; |
| 37 | |
| 38 | class PrefsFunctionalTest : public InProcessBrowserTest { |
| 39 | protected: |
| 40 | // Create a DownloadTestObserverTerminal that will wait for the |
| 41 | // specified number of downloads to finish. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 42 | std::unique_ptr<content::DownloadTestObserver> CreateWaiter( |
| 43 | Browser* browser, |
| 44 | int num_downloads) { |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 45 | DownloadManager* download_manager = |
Lukasz Anforowicz | 48d8345 | 2021-05-12 02:58:20 | [diff] [blame] | 46 | browser->profile()->GetDownloadManager(); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 47 | |
| 48 | content::DownloadTestObserver* downloads_observer = |
| 49 | new content::DownloadTestObserverTerminal( |
| 50 | download_manager, |
| 51 | num_downloads, |
| 52 | content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 53 | return base::WrapUnique(downloads_observer); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 54 | } |
| 55 | }; |
| 56 | |
| 57 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestDownloadDirPref) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 58 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 59 | |
Mattias Nissler | 441cc65 | 2018-04-05 00:08:56 | [diff] [blame] | 60 | base::FilePath new_download_dir = |
| 61 | DownloadPrefs(browser()->profile()).DownloadPath().AppendASCII("subdir"); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 62 | base::FilePath downloaded_pkg = |
Mattias Nissler | 441cc65 | 2018-04-05 00:08:56 | [diff] [blame] | 63 | new_download_dir.AppendASCII("a_zip_file.zip"); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 64 | |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 65 | // Set pref to download in new_download_dir. |
| 66 | browser()->profile()->GetPrefs()->SetFilePath( |
Mattias Nissler | 441cc65 | 2018-04-05 00:08:56 | [diff] [blame] | 67 | prefs::kDownloadDefaultDirectory, new_download_dir); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 68 | |
| 69 | // Create a downloads observer. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 70 | std::unique_ptr<content::DownloadTestObserver> downloads_observer( |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 71 | CreateWaiter(browser(), 1)); |
Lukasz Anforowicz | b78290c | 2021-09-08 04:31:38 | [diff] [blame] | 72 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 73 | browser(), embedded_test_server()->GetURL("/downloads/a_zip_file.zip"))); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 74 | // Waits for the download to complete. |
| 75 | downloads_observer->WaitForFinished(); |
Mattias Nissler | 441cc65 | 2018-04-05 00:08:56 | [diff] [blame] | 76 | |
| 77 | base::ScopedAllowBlockingForTesting allow_blocking; |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 78 | EXPECT_TRUE(base::PathExists(downloaded_pkg)); |
| 79 | } |
| 80 | |
| 81 | // Verify image content settings show or hide images. |
| 82 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestImageContentSettings) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 83 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 84 | |
Lukasz Anforowicz | b78290c | 2021-09-08 04:31:38 | [diff] [blame] | 85 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 86 | browser(), embedded_test_server()->GetURL("/settings/image_page.html"))); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 87 | |
| 88 | bool result = false; |
| 89 | std::string script = |
| 90 | "for (i=0; i < document.images.length; i++) {" |
| 91 | " if ((document.images[i].naturalWidth != 0) &&" |
| 92 | " (document.images[i].naturalHeight != 0)) {" |
| 93 | " window.domAutomationController.send(true);" |
| 94 | " }" |
| 95 | "}" |
| 96 | "window.domAutomationController.send(false);"; |
| 97 | EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 98 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 99 | script, |
| 100 | &result)); |
| 101 | EXPECT_TRUE(result); |
| 102 | |
msramek | 9681220 | 2015-07-27 16:00:02 | [diff] [blame] | 103 | browser()->profile()->GetPrefs()->SetInteger( |
raymes | bc7409e | 2015-08-06 01:57:45 | [diff] [blame] | 104 | content_settings::WebsiteSettingsRegistry::GetInstance() |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 105 | ->Get(ContentSettingsType::IMAGES) |
raymes | bc7409e | 2015-08-06 01:57:45 | [diff] [blame] | 106 | ->default_value_pref_name(), |
| 107 | CONTENT_SETTING_BLOCK); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 108 | |
Lukasz Anforowicz | b78290c | 2021-09-08 04:31:38 | [diff] [blame] | 109 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 110 | browser(), embedded_test_server()->GetURL("/settings/image_page.html"))); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 111 | |
| 112 | result = false; |
| 113 | EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 114 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 115 | script, |
| 116 | &result)); |
| 117 | EXPECT_FALSE(result); |
| 118 | } |
| 119 | |
| 120 | // Verify that enabling/disabling Javascript in prefs works. |
| 121 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestJavascriptEnableDisable) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 122 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 123 | |
| 124 | EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean( |
| 125 | prefs::kWebKitJavascriptEnabled)); |
Lukasz Anforowicz | b78290c | 2021-09-08 04:31:38 | [diff] [blame] | 126 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 127 | browser(), embedded_test_server()->GetURL("/javaScriptTitle.html"))); |
Jan Wilken Dörrie | 78e88d82e | 2021-03-23 15:24:22 | [diff] [blame] | 128 | EXPECT_EQ(u"Title from script javascript enabled", |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 129 | browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); |
| 130 | browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled, |
| 131 | false); |
Lukasz Anforowicz | b78290c | 2021-09-08 04:31:38 | [diff] [blame] | 132 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 133 | browser(), embedded_test_server()->GetURL("/javaScriptTitle.html"))); |
Jan Wilken Dörrie | 78e88d82e | 2021-03-23 15:24:22 | [diff] [blame] | 134 | EXPECT_EQ(u"This is html title", |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 135 | browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); |
| 136 | } |
| 137 | |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 138 | // Verify restore for bookmark bar visibility. |
| 139 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, |
| 140 | TestSessionRestoreShowBookmarkBar) { |
| 141 | EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean( |
tfarina | 3bddbe11 | 2014-08-28 05:29:32 | [diff] [blame] | 142 | bookmarks::prefs::kShowBookmarkBar)); |
| 143 | browser()->profile()->GetPrefs()->SetBoolean( |
| 144 | bookmarks::prefs::kShowBookmarkBar, true); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 145 | EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean( |
tfarina | 3bddbe11 | 2014-08-28 05:29:32 | [diff] [blame] | 146 | bookmarks::prefs::kShowBookmarkBar)); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 147 | |
| 148 | EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean( |
tfarina | 3bddbe11 | 2014-08-28 05:29:32 | [diff] [blame] | 149 | bookmarks::prefs::kShowBookmarkBar)); |
[email protected] | 6c491c6 | 2013-08-02 18:59:00 | [diff] [blame] | 150 | EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state()); |
| 151 | } |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 152 | |
| 153 | // Verify images are not blocked in incognito mode. |
| 154 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestImagesNotBlockedInIncognito) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 155 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 156 | GURL url = embedded_test_server()->GetURL("/settings/image_page.html"); |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 157 | Browser* incognito_browser = CreateIncognitoBrowser(); |
Lukasz Anforowicz | b78290c | 2021-09-08 04:31:38 | [diff] [blame] | 158 | ASSERT_TRUE(ui_test_utils::NavigateToURL(incognito_browser, url)); |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 159 | |
| 160 | bool result = false; |
| 161 | std::string script = |
| 162 | "for (i=0; i < document.images.length; i++) {" |
| 163 | " if ((document.images[i].naturalWidth != 0) &&" |
| 164 | " (document.images[i].naturalHeight != 0)) {" |
| 165 | " window.domAutomationController.send(true);" |
| 166 | " }" |
| 167 | "}" |
| 168 | "window.domAutomationController.send(false);"; |
| 169 | EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 170 | incognito_browser->tab_strip_model()->GetActiveWebContents(), |
| 171 | script, |
| 172 | &result)); |
| 173 | EXPECT_TRUE(result); |
| 174 | } |
| 175 | |
| 176 | // Verify setting homepage preference to newtabpage across restarts. Part1 |
| 177 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestHomepageNewTabpagePrefs) { |
| 178 | browser()->profile()->GetPrefs()->SetBoolean(prefs::kHomePageIsNewTabPage, |
| 179 | true); |
| 180 | } |
| 181 | |
| 182 | // Verify setting homepage preference to newtabpage across restarts. Part2 |
| 183 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHomepageNewTabpagePrefs) { |
| 184 | EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean( |
| 185 | prefs::kHomePageIsNewTabPage)); |
| 186 | } |
| 187 | |
| 188 | // Verify setting homepage preference to specific url. Part1 |
| 189 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestHomepagePrefs) { |
| 190 | GURL home_page_url("http://www.google.com"); |
| 191 | |
| 192 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 193 | prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false); |
| 194 | const PrefService::Preference* pref = |
| 195 | prefs->FindPreference(prefs::kHomePage); |
| 196 | if (pref && !pref->IsManaged()) { |
| 197 | prefs->SetString(prefs::kHomePage, home_page_url.spec()); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // Verify setting homepage preference to specific url. Part2 |
| 202 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHomepagePrefs) { |
| 203 | GURL home_page_url("http://www.google.com"); |
| 204 | |
| 205 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 206 | EXPECT_FALSE(prefs->GetBoolean(prefs::kHomePageIsNewTabPage)); |
| 207 | EXPECT_EQ(home_page_url.spec(), prefs->GetString(prefs::kHomePage)); |
| 208 | } |
| 209 | |
| 210 | // Verify the security preference under privacy across restarts. Part1 |
| 211 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestPrivacySecurityPrefs) { |
| 212 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 213 | |
Simon Pelchat | e74bbb6 | 2021-12-03 23:57:55 | [diff] [blame] | 214 | static_assert(prefetch::NetworkPredictionOptions::kDefault != |
| 215 | prefetch::NetworkPredictionOptions::kDisabled, |
bnc | f17f84b | 2015-09-14 22:13:26 | [diff] [blame] | 216 | "PrefsFunctionalTest.TestPrivacySecurityPrefs relies on " |
| 217 | "predictive network actions being enabled by default."); |
Simon Pelchat | e74bbb6 | 2021-12-03 23:57:55 | [diff] [blame] | 218 | EXPECT_EQ(prefetch::PreloadPagesState::kStandardPreloading, |
| 219 | prefetch::GetPreloadPagesState(*prefs)); |
| 220 | prefetch::SetPreloadPagesState(prefs, |
| 221 | prefetch::PreloadPagesState::kNoPreloading); |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 222 | |
| 223 | EXPECT_TRUE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled)); |
| 224 | prefs->SetBoolean(prefs::kSafeBrowsingEnabled, false); |
| 225 | |
Colin Blundell | ab45f55 | 2020-01-22 15:08:40 | [diff] [blame] | 226 | EXPECT_TRUE(prefs->GetBoolean(embedder_support::kAlternateErrorPagesEnabled)); |
| 227 | prefs->SetBoolean(embedder_support::kAlternateErrorPagesEnabled, false); |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 228 | |
| 229 | EXPECT_TRUE(prefs->GetBoolean(prefs::kSearchSuggestEnabled)); |
| 230 | prefs->SetBoolean(prefs::kSearchSuggestEnabled, false); |
| 231 | } |
| 232 | |
| 233 | // Verify the security preference under privacy across restarts. Part2 |
Dominic Battre | 30e25c1 | 2020-06-19 09:32:40 | [diff] [blame] | 234 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestPrivacySecurityPrefs) { |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 235 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 236 | |
Simon Pelchat | e74bbb6 | 2021-12-03 23:57:55 | [diff] [blame] | 237 | EXPECT_EQ(prefetch::PreloadPagesState::kNoPreloading, |
| 238 | prefetch::GetPreloadPagesState(*prefs)); |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 239 | EXPECT_FALSE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled)); |
Colin Blundell | ab45f55 | 2020-01-22 15:08:40 | [diff] [blame] | 240 | EXPECT_FALSE( |
| 241 | prefs->GetBoolean(embedder_support::kAlternateErrorPagesEnabled)); |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 242 | EXPECT_FALSE(prefs->GetBoolean(prefs::kSearchSuggestEnabled)); |
| 243 | } |
| 244 | |
| 245 | // Verify that we have some Local State prefs. |
| 246 | IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHaveLocalStatePrefs) { |
Lei Zhang | fc0abb9 | 2020-10-19 23:01:24 | [diff] [blame] | 247 | base::Value prefs = g_browser_process->local_state()->GetPreferenceValues( |
| 248 | PrefService::INCLUDE_DEFAULTS); |
| 249 | EXPECT_TRUE(prefs.is_dict()); |
[email protected] | d3e9f3d | 2013-10-30 16:46:41 | [diff] [blame] | 250 | } |