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 browser()->profile()->GetPrefs()->SetInteger(
91 prefs::kDefaultImagesSetting
, CONTENT_SETTING_BLOCK
);
93 ui_test_utils::NavigateToURL(
95 test_server()->GetURL("files/settings/image_page.html"));
98 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
99 browser()->tab_strip_model()->GetActiveWebContents(),
102 EXPECT_FALSE(result
);
105 // Verify that enabling/disabling Javascript in prefs works.
106 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestJavascriptEnableDisable
) {
107 ASSERT_TRUE(test_server()->Start());
109 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
110 prefs::kWebKitJavascriptEnabled
));
111 ui_test_utils::NavigateToURL(
113 test_server()->GetURL("files/javaScriptTitle.html"));
114 EXPECT_EQ(base::ASCIIToUTF16("Title from script javascript enabled"),
115 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
116 browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled
,
118 ui_test_utils::NavigateToURL(
120 test_server()->GetURL("files/javaScriptTitle.html"));
121 EXPECT_EQ(base::ASCIIToUTF16("This is html title"),
122 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
125 // Verify restore for bookmark bar visibility.
126 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
,
127 TestSessionRestoreShowBookmarkBar
) {
128 EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
129 bookmarks::prefs::kShowBookmarkBar
));
130 browser()->profile()->GetPrefs()->SetBoolean(
131 bookmarks::prefs::kShowBookmarkBar
, true);
132 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
133 bookmarks::prefs::kShowBookmarkBar
));
135 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
136 bookmarks::prefs::kShowBookmarkBar
));
137 EXPECT_EQ(BookmarkBar::SHOW
, browser()->bookmark_bar_state());
140 // Verify images are not blocked in incognito mode.
141 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestImagesNotBlockedInIncognito
) {
142 ASSERT_TRUE(test_server()->Start());
143 GURL url
= test_server()->GetURL("files/settings/image_page.html");
144 Browser
* incognito_browser
= CreateIncognitoBrowser();
145 ui_test_utils::NavigateToURL(incognito_browser
, url
);
149 "for (i=0; i < document.images.length; i++) {"
150 " if ((document.images[i].naturalWidth != 0) &&"
151 " (document.images[i].naturalHeight != 0)) {"
152 " window.domAutomationController.send(true);"
155 "window.domAutomationController.send(false);";
156 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
157 incognito_browser
->tab_strip_model()->GetActiveWebContents(),
163 // Verify setting homepage preference to newtabpage across restarts. Part1
164 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, PRE_TestHomepageNewTabpagePrefs
) {
165 browser()->profile()->GetPrefs()->SetBoolean(prefs::kHomePageIsNewTabPage
,
169 // Verify setting homepage preference to newtabpage across restarts. Part2
170 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestHomepageNewTabpagePrefs
) {
171 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
172 prefs::kHomePageIsNewTabPage
));
175 // Verify setting homepage preference to specific url. Part1
176 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, PRE_TestHomepagePrefs
) {
177 GURL
home_page_url("http://www.google.com");
179 PrefService
* prefs
= browser()->profile()->GetPrefs();
180 prefs
->SetBoolean(prefs::kHomePageIsNewTabPage
, false);
181 const PrefService::Preference
* pref
=
182 prefs
->FindPreference(prefs::kHomePage
);
183 if (pref
&& !pref
->IsManaged()) {
184 prefs
->SetString(prefs::kHomePage
, home_page_url
.spec());
188 // Verify setting homepage preference to specific url. Part2
189 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestHomepagePrefs
) {
190 GURL
home_page_url("http://www.google.com");
192 PrefService
* prefs
= browser()->profile()->GetPrefs();
193 EXPECT_FALSE(prefs
->GetBoolean(prefs::kHomePageIsNewTabPage
));
194 EXPECT_EQ(home_page_url
.spec(), prefs
->GetString(prefs::kHomePage
));
197 // Verify the security preference under privacy across restarts. Part1
198 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, PRE_TestPrivacySecurityPrefs
) {
199 PrefService
* prefs
= browser()->profile()->GetPrefs();
201 EXPECT_TRUE(prefs
->GetBoolean(prefs::kNetworkPredictionEnabled
));
202 prefs
->SetBoolean(prefs::kNetworkPredictionEnabled
, false);
204 EXPECT_TRUE(prefs
->GetBoolean(prefs::kSafeBrowsingEnabled
));
205 prefs
->SetBoolean(prefs::kSafeBrowsingEnabled
, false);
207 EXPECT_TRUE(prefs
->GetBoolean(prefs::kAlternateErrorPagesEnabled
));
208 prefs
->SetBoolean(prefs::kAlternateErrorPagesEnabled
, false);
210 EXPECT_TRUE(prefs
->GetBoolean(prefs::kSearchSuggestEnabled
));
211 prefs
->SetBoolean(prefs::kSearchSuggestEnabled
, false);
214 // Verify the security preference under privacy across restarts. Part2
215 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestPrivacySecurityPrefs
) {
216 PrefService
* prefs
= browser()->profile()->GetPrefs();
218 EXPECT_FALSE(prefs
->GetBoolean(prefs::kNetworkPredictionEnabled
));
219 EXPECT_FALSE(prefs
->GetBoolean(prefs::kSafeBrowsingEnabled
));
220 EXPECT_FALSE(prefs
->GetBoolean(prefs::kAlternateErrorPagesEnabled
));
221 EXPECT_FALSE(prefs
->GetBoolean(prefs::kSearchSuggestEnabled
));
224 // Verify that we have some Local State prefs.
225 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest
, TestHaveLocalStatePrefs
) {
226 EXPECT_TRUE(g_browser_process
->local_state()->GetPreferenceValues().get());