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.
7 #include "base/path_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/download/download_prefs.h"
10 #include "chrome/browser/prefs/pref_service_syncable.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_commands.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/test/browser_test_utils.h"
22 #include "content/public/test/download_test_observer.h"
24 using content::BrowserContext
;
25 using content::DownloadManager
;
27 class PrefsFunctionalTest
: public InProcessBrowserTest
{
29 // Create a DownloadTestObserverTerminal that will wait for the
30 // specified number of downloads to finish.
31 scoped_ptr
<content::DownloadTestObserver
> CreateWaiter(Browser
* browser
,
33 DownloadManager
* download_manager
=
34 BrowserContext::GetDownloadManager(browser
->profile());
36 content::DownloadTestObserver
* downloads_observer
=
37 new content::DownloadTestObserverTerminal(
40 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL
);
41 return make_scoped_ptr(downloads_observer
);
45 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestDownloadDirPref
) {
46 ASSERT_TRUE(test_server()->Start());
48 DownloadManager
* download_manager
=
49 BrowserContext::GetDownloadManager(browser()->profile());
50 base::FilePath download_dir
=
51 (DownloadPrefs::FromDownloadManager(download_manager
))->DownloadPath();
52 base::FilePath new_download_dir
= download_dir
.AppendASCII("my_downloads");
53 base::FilePath downloaded_pkg
=
54 new_download_dir
.AppendASCII("a_zip_file.zip");
56 // If the directory exists, delete it.
57 if (base::PathExists(new_download_dir
)) {
58 base::DeleteFile(new_download_dir
, true);
61 // Create the new downloads directory.
62 base::CreateDirectory(new_download_dir
);
63 // Set pref to download in new_download_dir.
64 browser()->profile()->GetPrefs()->SetFilePath(
65 prefs::kDownloadDefaultDirectory
,
68 // Create a downloads observer.
69 scoped_ptr
<content::DownloadTestObserver
> downloads_observer(
70 CreateWaiter(browser(), 1));
71 ui_test_utils::NavigateToURL(
73 test_server()->GetURL("files/downloads/a_zip_file.zip"));
74 // Waits for the download to complete.
75 downloads_observer
->WaitForFinished();
76 EXPECT_TRUE(base::PathExists(downloaded_pkg
));
79 // Verify image content settings show or hide images.
80 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestImageContentSettings
) {
81 ASSERT_TRUE(test_server()->Start());
83 ui_test_utils::NavigateToURL(
85 test_server()->GetURL("files/settings/image_page.html"));
89 "for (i=0; i < document.images.length; i++) {"
90 " if ((document.images[i].naturalWidth != 0) &&"
91 " (document.images[i].naturalHeight != 0)) {"
92 " window.domAutomationController.send(true);"
95 "window.domAutomationController.send(false);";
96 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
97 browser()->tab_strip_model()->GetActiveWebContents(),
102 base::DictionaryValue value
;
103 value
.SetInteger("images", 2);
104 browser()->profile()->GetPrefs()->Set(prefs::kDefaultContentSettings
,
107 ui_test_utils::NavigateToURL(
109 test_server()->GetURL("files/settings/image_page.html"));
112 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
113 browser()->tab_strip_model()->GetActiveWebContents(),
116 EXPECT_FALSE(result
);
119 // Verify that enabling/disabling Javascript in prefs works.
120 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestJavascriptEnableDisable
) {
121 ASSERT_TRUE(test_server()->Start());
123 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
124 prefs::kWebKitJavascriptEnabled
));
125 ui_test_utils::NavigateToURL(
127 test_server()->GetURL("files/javaScriptTitle.html"));
128 EXPECT_EQ(base::ASCIIToUTF16("Title from script javascript enabled"),
129 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
130 browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled
,
132 ui_test_utils::NavigateToURL(
134 test_server()->GetURL("files/javaScriptTitle.html"));
135 EXPECT_EQ(base::ASCIIToUTF16("This is html title"),
136 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
139 // Verify DNS prefetching pref.
140 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestNetworkPredictionEnabledPref
) {
141 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
142 prefs::kNetworkPredictionEnabled
));
143 browser()->profile()->GetPrefs()->SetBoolean(prefs::kNetworkPredictionEnabled
,
145 EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
146 prefs::kNetworkPredictionEnabled
));
149 // Verify restore for bookmark bar visibility.
150 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
,
151 TestSessionRestoreShowBookmarkBar
) {
152 EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
153 prefs::kShowBookmarkBar
));
154 browser()->profile()->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar
, true);
155 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
156 prefs::kShowBookmarkBar
));
158 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
159 prefs::kShowBookmarkBar
));
160 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
163 // Verify images are not blocked in incognito mode.
164 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestImagesNotBlockedInIncognito
) {
165 ASSERT_TRUE(test_server()->Start());
166 GURL url
= test_server()->GetURL("files/settings/image_page.html");
167 Browser
* incognito_browser
= CreateIncognitoBrowser();
168 ui_test_utils::NavigateToURLWithDisposition(
172 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
176 "for (i=0; i < document.images.length; i++) {"
177 " if ((document.images[i].naturalWidth != 0) &&"
178 " (document.images[i].naturalHeight != 0)) {"
179 " window.domAutomationController.send(true);"
182 "window.domAutomationController.send(false);";
183 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
184 incognito_browser
->tab_strip_model()->GetActiveWebContents(),
190 // Verify setting homepage preference to newtabpage across restarts. Part1
191 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, PRE_TestHomepageNewTabpagePrefs
) {
192 browser()->profile()->GetPrefs()->SetBoolean(prefs::kHomePageIsNewTabPage
,
196 // Verify setting homepage preference to newtabpage across restarts. Part2
197 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestHomepageNewTabpagePrefs
) {
198 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
199 prefs::kHomePageIsNewTabPage
));
202 // Verify setting homepage preference to specific url. Part1
203 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, PRE_TestHomepagePrefs
) {
204 GURL
home_page_url("http://www.google.com");
206 PrefService
* prefs
= browser()->profile()->GetPrefs();
207 prefs
->SetBoolean(prefs::kHomePageIsNewTabPage
, false);
208 const PrefService::Preference
* pref
=
209 prefs
->FindPreference(prefs::kHomePage
);
210 if (pref
&& !pref
->IsManaged()) {
211 prefs
->SetString(prefs::kHomePage
, home_page_url
.spec());
215 // Verify setting homepage preference to specific url. Part2
216 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestHomepagePrefs
) {
217 GURL
home_page_url("http://www.google.com");
219 PrefService
* prefs
= browser()->profile()->GetPrefs();
220 EXPECT_FALSE(prefs
->GetBoolean(prefs::kHomePageIsNewTabPage
));
221 EXPECT_EQ(home_page_url
.spec(), prefs
->GetString(prefs::kHomePage
));
224 // Verify the security preference under privacy across restarts. Part1
225 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, PRE_TestPrivacySecurityPrefs
) {
226 PrefService
* prefs
= browser()->profile()->GetPrefs();
228 EXPECT_TRUE(prefs
->GetBoolean(prefs::kNetworkPredictionEnabled
));
229 prefs
->SetBoolean(prefs::kNetworkPredictionEnabled
, false);
231 EXPECT_TRUE(prefs
->GetBoolean(prefs::kSafeBrowsingEnabled
));
232 prefs
->SetBoolean(prefs::kSafeBrowsingEnabled
, false);
234 EXPECT_TRUE(prefs
->GetBoolean(prefs::kAlternateErrorPagesEnabled
));
235 prefs
->SetBoolean(prefs::kAlternateErrorPagesEnabled
, false);
237 EXPECT_TRUE(prefs
->GetBoolean(prefs::kSearchSuggestEnabled
));
238 prefs
->SetBoolean(prefs::kSearchSuggestEnabled
, false);
241 // Verify the security preference under privacy across restarts. Part2
242 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestPrivacySecurityPrefs
) {
243 PrefService
* prefs
= browser()->profile()->GetPrefs();
245 EXPECT_FALSE(prefs
->GetBoolean(prefs::kNetworkPredictionEnabled
));
246 EXPECT_FALSE(prefs
->GetBoolean(prefs::kSafeBrowsingEnabled
));
247 EXPECT_FALSE(prefs
->GetBoolean(prefs::kAlternateErrorPagesEnabled
));
248 EXPECT_FALSE(prefs
->GetBoolean(prefs::kSearchSuggestEnabled
));
251 // Verify that we have some Local State prefs.
252 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestHaveLocalStatePrefs
) {
253 EXPECT_TRUE(g_browser_process
->local_state()->GetPreferenceValues().get());