blob: ec142dbb0153218b3728c4f487e5d3fd5210a0e9 [file] [log] [blame]
[email protected]6c491c62013-08-02 18:59:001// 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
dcheng4af48582016-04-19 00:29:357#include "base/memory/ptr_util.h"
[email protected]6c491c62013-08-02 18:59:008#include "base/strings/utf_string_conversions.h"
jam3f2d3932017-04-26 20:28:519#include "base/threading/thread_restrictions.h"
[email protected]6c491c62013-08-02 18:59:0010#include "chrome/browser/download/download_prefs.h"
bncf17f84b2015-09-14 22:13:2611#include "chrome/browser/net/prediction_options.h"
[email protected]6c491c62013-08-02 18:59:0012#include "chrome/browser/ui/browser.h"
13#include "chrome/browser/ui/browser_commands.h"
14#include "chrome/browser/ui/tabs/tab_strip_model.h"
15#include "chrome/common/chrome_paths.h"
16#include "chrome/common/pref_names.h"
17#include "chrome/test/base/in_process_browser_test.h"
[email protected]d3e9f3d2013-10-30 16:46:4118#include "chrome/test/base/testing_browser_process.h"
[email protected]6c491c62013-08-02 18:59:0019#include "chrome/test/base/testing_profile.h"
20#include "chrome/test/base/ui_test_utils.h"
brettwe1f0af8b2015-10-09 21:30:4621#include "components/bookmarks/common/bookmark_pref_names.h"
raymesbc7409e2015-08-06 01:57:4522#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"
brettwe1f0af8b2015-10-09 21:30:4625#include "components/content_settings/core/common/pref_names.h"
maxbogueea16ff412016-10-28 16:35:2926#include "components/sync_preferences/pref_service_syncable.h"
[email protected]6c491c62013-08-02 18:59:0027#include "content/public/browser/web_contents.h"
28#include "content/public/test/browser_test_utils.h"
29#include "content/public/test/download_test_observer.h"
svaldeza01f7d92015-11-18 17:47:5630#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]6c491c62013-08-02 18:59:0031
32using content::BrowserContext;
33using content::DownloadManager;
34
35class PrefsFunctionalTest : public InProcessBrowserTest {
36 protected:
37 // Create a DownloadTestObserverTerminal that will wait for the
38 // specified number of downloads to finish.
dcheng4af48582016-04-19 00:29:3539 std::unique_ptr<content::DownloadTestObserver> CreateWaiter(
40 Browser* browser,
41 int num_downloads) {
[email protected]6c491c62013-08-02 18:59:0042 DownloadManager* download_manager =
43 BrowserContext::GetDownloadManager(browser->profile());
44
45 content::DownloadTestObserver* downloads_observer =
46 new content::DownloadTestObserverTerminal(
47 download_manager,
48 num_downloads,
49 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
dcheng4af48582016-04-19 00:29:3550 return base::WrapUnique(downloads_observer);
[email protected]6c491c62013-08-02 18:59:0051 }
52};
53
54IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestDownloadDirPref) {
svaldeza01f7d92015-11-18 17:47:5655 ASSERT_TRUE(embedded_test_server()->Start());
jam3f2d3932017-04-26 20:28:5156 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]5079b2b2014-05-10 21:31:3257 base::ScopedTempDir new_download_dir;
58 ASSERT_TRUE(new_download_dir.CreateUniqueTempDir());
[email protected]6c491c62013-08-02 18:59:0059
[email protected]6c491c62013-08-02 18:59:0060 base::FilePath downloaded_pkg =
vabr8023d872016-09-15 08:12:2261 new_download_dir.GetPath().AppendASCII("a_zip_file.zip");
[email protected]6c491c62013-08-02 18:59:0062
[email protected]6c491c62013-08-02 18:59:0063 // Set pref to download in new_download_dir.
64 browser()->profile()->GetPrefs()->SetFilePath(
vabr8023d872016-09-15 08:12:2265 prefs::kDownloadDefaultDirectory, new_download_dir.GetPath());
[email protected]6c491c62013-08-02 18:59:0066
67 // Create a downloads observer.
dcheng4af48582016-04-19 00:29:3568 std::unique_ptr<content::DownloadTestObserver> downloads_observer(
[email protected]6c491c62013-08-02 18:59:0069 CreateWaiter(browser(), 1));
70 ui_test_utils::NavigateToURL(
svaldeza01f7d92015-11-18 17:47:5671 browser(), embedded_test_server()->GetURL("/downloads/a_zip_file.zip"));
[email protected]6c491c62013-08-02 18:59:0072 // Waits for the download to complete.
73 downloads_observer->WaitForFinished();
74 EXPECT_TRUE(base::PathExists(downloaded_pkg));
75}
76
77// Verify image content settings show or hide images.
78IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestImageContentSettings) {
svaldeza01f7d92015-11-18 17:47:5679 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]6c491c62013-08-02 18:59:0080
81 ui_test_utils::NavigateToURL(
svaldeza01f7d92015-11-18 17:47:5682 browser(), embedded_test_server()->GetURL("/settings/image_page.html"));
[email protected]6c491c62013-08-02 18:59:0083
84 bool result = false;
85 std::string script =
86 "for (i=0; i < document.images.length; i++) {"
87 " if ((document.images[i].naturalWidth != 0) &&"
88 " (document.images[i].naturalHeight != 0)) {"
89 " window.domAutomationController.send(true);"
90 " }"
91 "}"
92 "window.domAutomationController.send(false);";
93 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
94 browser()->tab_strip_model()->GetActiveWebContents(),
95 script,
96 &result));
97 EXPECT_TRUE(result);
98
msramek96812202015-07-27 16:00:0299 browser()->profile()->GetPrefs()->SetInteger(
raymesbc7409e2015-08-06 01:57:45100 content_settings::WebsiteSettingsRegistry::GetInstance()
101 ->Get(CONTENT_SETTINGS_TYPE_IMAGES)
102 ->default_value_pref_name(),
103 CONTENT_SETTING_BLOCK);
[email protected]6c491c62013-08-02 18:59:00104
105 ui_test_utils::NavigateToURL(
svaldeza01f7d92015-11-18 17:47:56106 browser(), embedded_test_server()->GetURL("/settings/image_page.html"));
[email protected]6c491c62013-08-02 18:59:00107
108 result = false;
109 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
110 browser()->tab_strip_model()->GetActiveWebContents(),
111 script,
112 &result));
113 EXPECT_FALSE(result);
114}
115
116// Verify that enabling/disabling Javascript in prefs works.
117IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestJavascriptEnableDisable) {
svaldeza01f7d92015-11-18 17:47:56118 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]6c491c62013-08-02 18:59:00119
120 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
121 prefs::kWebKitJavascriptEnabled));
122 ui_test_utils::NavigateToURL(
svaldeza01f7d92015-11-18 17:47:56123 browser(), embedded_test_server()->GetURL("/javaScriptTitle.html"));
[email protected]6778fed2013-12-24 20:09:37124 EXPECT_EQ(base::ASCIIToUTF16("Title from script javascript enabled"),
[email protected]6c491c62013-08-02 18:59:00125 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
126 browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled,
127 false);
128 ui_test_utils::NavigateToURL(
svaldeza01f7d92015-11-18 17:47:56129 browser(), embedded_test_server()->GetURL("/javaScriptTitle.html"));
[email protected]6778fed2013-12-24 20:09:37130 EXPECT_EQ(base::ASCIIToUTF16("This is html title"),
[email protected]6c491c62013-08-02 18:59:00131 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
132}
133
[email protected]6c491c62013-08-02 18:59:00134// Verify restore for bookmark bar visibility.
135IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest,
136 TestSessionRestoreShowBookmarkBar) {
137 EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
tfarina3bddbe112014-08-28 05:29:32138 bookmarks::prefs::kShowBookmarkBar));
139 browser()->profile()->GetPrefs()->SetBoolean(
140 bookmarks::prefs::kShowBookmarkBar, true);
[email protected]6c491c62013-08-02 18:59:00141 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
tfarina3bddbe112014-08-28 05:29:32142 bookmarks::prefs::kShowBookmarkBar));
[email protected]6c491c62013-08-02 18:59:00143
144 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
tfarina3bddbe112014-08-28 05:29:32145 bookmarks::prefs::kShowBookmarkBar));
[email protected]6c491c62013-08-02 18:59:00146 EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
147}
[email protected]d3e9f3d2013-10-30 16:46:41148
149// Verify images are not blocked in incognito mode.
150IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestImagesNotBlockedInIncognito) {
svaldeza01f7d92015-11-18 17:47:56151 ASSERT_TRUE(embedded_test_server()->Start());
152 GURL url = embedded_test_server()->GetURL("/settings/image_page.html");
[email protected]d3e9f3d2013-10-30 16:46:41153 Browser* incognito_browser = CreateIncognitoBrowser();
thestig53986dc2014-12-16 06:09:18154 ui_test_utils::NavigateToURL(incognito_browser, url);
[email protected]d3e9f3d2013-10-30 16:46:41155
156 bool result = false;
157 std::string script =
158 "for (i=0; i < document.images.length; i++) {"
159 " if ((document.images[i].naturalWidth != 0) &&"
160 " (document.images[i].naturalHeight != 0)) {"
161 " window.domAutomationController.send(true);"
162 " }"
163 "}"
164 "window.domAutomationController.send(false);";
165 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
166 incognito_browser->tab_strip_model()->GetActiveWebContents(),
167 script,
168 &result));
169 EXPECT_TRUE(result);
170}
171
172// Verify setting homepage preference to newtabpage across restarts. Part1
173IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestHomepageNewTabpagePrefs) {
174 browser()->profile()->GetPrefs()->SetBoolean(prefs::kHomePageIsNewTabPage,
175 true);
176}
177
178// Verify setting homepage preference to newtabpage across restarts. Part2
179IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHomepageNewTabpagePrefs) {
180 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
181 prefs::kHomePageIsNewTabPage));
182}
183
184// Verify setting homepage preference to specific url. Part1
185IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestHomepagePrefs) {
186 GURL home_page_url("http://www.google.com");
187
188 PrefService* prefs = browser()->profile()->GetPrefs();
189 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false);
190 const PrefService::Preference* pref =
191 prefs->FindPreference(prefs::kHomePage);
192 if (pref && !pref->IsManaged()) {
193 prefs->SetString(prefs::kHomePage, home_page_url.spec());
194 }
195}
196
197// Verify setting homepage preference to specific url. Part2
198IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHomepagePrefs) {
199 GURL home_page_url("http://www.google.com");
200
201 PrefService* prefs = browser()->profile()->GetPrefs();
202 EXPECT_FALSE(prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
203 EXPECT_EQ(home_page_url.spec(), prefs->GetString(prefs::kHomePage));
204}
205
206// Verify the security preference under privacy across restarts. Part1
207IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestPrivacySecurityPrefs) {
208 PrefService* prefs = browser()->profile()->GetPrefs();
209
bncf17f84b2015-09-14 22:13:26210 static_assert(chrome_browser_net::NETWORK_PREDICTION_DEFAULT !=
211 chrome_browser_net::NETWORK_PREDICTION_NEVER,
212 "PrefsFunctionalTest.TestPrivacySecurityPrefs relies on "
213 "predictive network actions being enabled by default.");
214 EXPECT_EQ(chrome_browser_net::NETWORK_PREDICTION_DEFAULT,
215 prefs->GetInteger(prefs::kNetworkPredictionOptions));
216 prefs->SetInteger(prefs::kNetworkPredictionOptions,
217 chrome_browser_net::NETWORK_PREDICTION_NEVER);
[email protected]d3e9f3d2013-10-30 16:46:41218
219 EXPECT_TRUE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled));
220 prefs->SetBoolean(prefs::kSafeBrowsingEnabled, false);
221
222 EXPECT_TRUE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled));
223 prefs->SetBoolean(prefs::kAlternateErrorPagesEnabled, false);
224
225 EXPECT_TRUE(prefs->GetBoolean(prefs::kSearchSuggestEnabled));
226 prefs->SetBoolean(prefs::kSearchSuggestEnabled, false);
227}
228
229// Verify the security preference under privacy across restarts. Part2
230IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestPrivacySecurityPrefs) {
231 PrefService* prefs = browser()->profile()->GetPrefs();
232
bncf17f84b2015-09-14 22:13:26233 EXPECT_EQ(chrome_browser_net::NETWORK_PREDICTION_NEVER,
234 prefs->GetInteger(prefs::kNetworkPredictionOptions));
[email protected]d3e9f3d2013-10-30 16:46:41235 EXPECT_FALSE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled));
236 EXPECT_FALSE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled));
237 EXPECT_FALSE(prefs->GetBoolean(prefs::kSearchSuggestEnabled));
238}
239
240// Verify that we have some Local State prefs.
241IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHaveLocalStatePrefs) {
brettwebf7184f2017-04-18 21:10:53242 EXPECT_TRUE(g_browser_process->local_state()
243 ->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS)
244 .get());
[email protected]d3e9f3d2013-10-30 16:46:41245}