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/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"
16 #include "chrome/test/base/testing_browser_process.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/browser_test_utils.h"
21 #include "content/public/test/download_test_observer.h"
23 using content::BrowserContext
;
24 using content::DownloadManager
;
26 class PrefsFunctionalTest
: public InProcessBrowserTest
{
28 // Create a DownloadTestObserverTerminal that will wait for the
29 // specified number of downloads to finish.
30 scoped_ptr
<content::DownloadTestObserver
> CreateWaiter(Browser
* browser
,
32 DownloadManager
* download_manager
=
33 BrowserContext::GetDownloadManager(browser
->profile());
35 content::DownloadTestObserver
* downloads_observer
=
36 new content::DownloadTestObserverTerminal(
39 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL
);
40 return make_scoped_ptr(downloads_observer
);
44 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestDownloadDirPref
) {
45 ASSERT_TRUE(test_server()->Start());
46 base::ScopedTempDir new_download_dir
;
47 ASSERT_TRUE(new_download_dir
.CreateUniqueTempDir());
49 base::FilePath downloaded_pkg
=
50 new_download_dir
.path().AppendASCII("a_zip_file.zip");
52 // Set pref to download in new_download_dir.
53 browser()->profile()->GetPrefs()->SetFilePath(
54 prefs::kDownloadDefaultDirectory
, new_download_dir
.path());
56 // Create a downloads observer.
57 scoped_ptr
<content::DownloadTestObserver
> downloads_observer(
58 CreateWaiter(browser(), 1));
59 ui_test_utils::NavigateToURL(
61 test_server()->GetURL("files/downloads/a_zip_file.zip"));
62 // Waits for the download to complete.
63 downloads_observer
->WaitForFinished();
64 EXPECT_TRUE(base::PathExists(downloaded_pkg
));
67 // Verify image content settings show or hide images.
68 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestImageContentSettings
) {
69 ASSERT_TRUE(test_server()->Start());
71 ui_test_utils::NavigateToURL(
73 test_server()->GetURL("files/settings/image_page.html"));
77 "for (i=0; i < document.images.length; i++) {"
78 " if ((document.images[i].naturalWidth != 0) &&"
79 " (document.images[i].naturalHeight != 0)) {"
80 " window.domAutomationController.send(true);"
83 "window.domAutomationController.send(false);";
84 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
85 browser()->tab_strip_model()->GetActiveWebContents(),
90 base::DictionaryValue value
;
91 value
.SetInteger("images", 2);
92 browser()->profile()->GetPrefs()->Set(prefs::kDefaultContentSettings
,
95 ui_test_utils::NavigateToURL(
97 test_server()->GetURL("files/settings/image_page.html"));
100 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
101 browser()->tab_strip_model()->GetActiveWebContents(),
104 EXPECT_FALSE(result
);
107 // Verify that enabling/disabling Javascript in prefs works.
108 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestJavascriptEnableDisable
) {
109 ASSERT_TRUE(test_server()->Start());
111 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
112 prefs::kWebKitJavascriptEnabled
));
113 ui_test_utils::NavigateToURL(
115 test_server()->GetURL("files/javaScriptTitle.html"));
116 EXPECT_EQ(base::ASCIIToUTF16("Title from script javascript enabled"),
117 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
118 browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled
,
120 ui_test_utils::NavigateToURL(
122 test_server()->GetURL("files/javaScriptTitle.html"));
123 EXPECT_EQ(base::ASCIIToUTF16("This is html title"),
124 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
127 // Verify restore for bookmark bar visibility.
128 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
,
129 TestSessionRestoreShowBookmarkBar
) {
130 EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
131 bookmarks::prefs::kShowBookmarkBar
));
132 browser()->profile()->GetPrefs()->SetBoolean(
133 bookmarks::prefs::kShowBookmarkBar
, true);
134 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
135 bookmarks::prefs::kShowBookmarkBar
));
137 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
138 bookmarks::prefs::kShowBookmarkBar
));
139 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
142 // Verify images are not blocked in incognito mode.
143 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestImagesNotBlockedInIncognito
) {
144 ASSERT_TRUE(test_server()->Start());
145 GURL url
= test_server()->GetURL("files/settings/image_page.html");
146 Browser
* incognito_browser
= CreateIncognitoBrowser();
147 ui_test_utils::NavigateToURLWithDisposition(
151 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
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);"
161 "window.domAutomationController.send(false);";
162 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
163 incognito_browser
->tab_strip_model()->GetActiveWebContents(),
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
,
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
));
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");
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());
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");
198 PrefService
* prefs
= browser()->profile()->GetPrefs();
199 EXPECT_FALSE(prefs
->GetBoolean(prefs::kHomePageIsNewTabPage
));
200 EXPECT_EQ(home_page_url
.spec(), prefs
->GetString(prefs::kHomePage
));
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();
207 EXPECT_TRUE(prefs
->GetBoolean(prefs::kNetworkPredictionEnabled
));
208 prefs
->SetBoolean(prefs::kNetworkPredictionEnabled
, false);
210 EXPECT_TRUE(prefs
->GetBoolean(prefs::kSafeBrowsingEnabled
));
211 prefs
->SetBoolean(prefs::kSafeBrowsingEnabled
, false);
213 EXPECT_TRUE(prefs
->GetBoolean(prefs::kAlternateErrorPagesEnabled
));
214 prefs
->SetBoolean(prefs::kAlternateErrorPagesEnabled
, false);
216 EXPECT_TRUE(prefs
->GetBoolean(prefs::kSearchSuggestEnabled
));
217 prefs
->SetBoolean(prefs::kSearchSuggestEnabled
, false);
220 // Verify the security preference under privacy across restarts. Part2
221 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestPrivacySecurityPrefs
) {
222 PrefService
* prefs
= browser()->profile()->GetPrefs();
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
));
230 // Verify that we have some Local State prefs.
231 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestHaveLocalStatePrefs
) {
232 EXPECT_TRUE(g_browser_process
->local_state()->GetPreferenceValues().get());