Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / prefs / pref_functional_browsertest.cc
blob0127db2055df2d0c522e25c5eacf3db9b9ea7ade
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.
5 #include <string>
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/download/download_prefs.h"
9 #include "chrome/browser/net/prediction_options.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 "components/content_settings/core/browser/website_settings_info.h"
20 #include "components/content_settings/core/browser/website_settings_registry.h"
21 #include "components/content_settings/core/common/content_settings_types.h"
22 #include "components/syncable_prefs/pref_service_syncable.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/test/browser_test_utils.h"
25 #include "content/public/test/download_test_observer.h"
27 using content::BrowserContext;
28 using content::DownloadManager;
30 class PrefsFunctionalTest : public InProcessBrowserTest {
31 protected:
32 // Create a DownloadTestObserverTerminal that will wait for the
33 // specified number of downloads to finish.
34 scoped_ptr<content::DownloadTestObserver> CreateWaiter(Browser* browser,
35 int num_downloads) {
36 DownloadManager* download_manager =
37 BrowserContext::GetDownloadManager(browser->profile());
39 content::DownloadTestObserver* downloads_observer =
40 new content::DownloadTestObserverTerminal(
41 download_manager,
42 num_downloads,
43 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
44 return make_scoped_ptr(downloads_observer);
48 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestDownloadDirPref) {
49 ASSERT_TRUE(test_server()->Start());
50 base::ScopedTempDir new_download_dir;
51 ASSERT_TRUE(new_download_dir.CreateUniqueTempDir());
53 base::FilePath downloaded_pkg =
54 new_download_dir.path().AppendASCII("a_zip_file.zip");
56 // Set pref to download in new_download_dir.
57 browser()->profile()->GetPrefs()->SetFilePath(
58 prefs::kDownloadDefaultDirectory, new_download_dir.path());
60 // Create a downloads observer.
61 scoped_ptr<content::DownloadTestObserver> downloads_observer(
62 CreateWaiter(browser(), 1));
63 ui_test_utils::NavigateToURL(
64 browser(),
65 test_server()->GetURL("files/downloads/a_zip_file.zip"));
66 // Waits for the download to complete.
67 downloads_observer->WaitForFinished();
68 EXPECT_TRUE(base::PathExists(downloaded_pkg));
71 // Verify image content settings show or hide images.
72 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestImageContentSettings) {
73 ASSERT_TRUE(test_server()->Start());
75 ui_test_utils::NavigateToURL(
76 browser(),
77 test_server()->GetURL("files/settings/image_page.html"));
79 bool result = false;
80 std::string script =
81 "for (i=0; i < document.images.length; i++) {"
82 " if ((document.images[i].naturalWidth != 0) &&"
83 " (document.images[i].naturalHeight != 0)) {"
84 " window.domAutomationController.send(true);"
85 " }"
86 "}"
87 "window.domAutomationController.send(false);";
88 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
89 browser()->tab_strip_model()->GetActiveWebContents(),
90 script,
91 &result));
92 EXPECT_TRUE(result);
94 browser()->profile()->GetPrefs()->SetInteger(
95 content_settings::WebsiteSettingsRegistry::GetInstance()
96 ->Get(CONTENT_SETTINGS_TYPE_IMAGES)
97 ->default_value_pref_name(),
98 CONTENT_SETTING_BLOCK);
100 ui_test_utils::NavigateToURL(
101 browser(),
102 test_server()->GetURL("files/settings/image_page.html"));
104 result = false;
105 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
106 browser()->tab_strip_model()->GetActiveWebContents(),
107 script,
108 &result));
109 EXPECT_FALSE(result);
112 // Verify that enabling/disabling Javascript in prefs works.
113 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestJavascriptEnableDisable) {
114 ASSERT_TRUE(test_server()->Start());
116 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
117 prefs::kWebKitJavascriptEnabled));
118 ui_test_utils::NavigateToURL(
119 browser(),
120 test_server()->GetURL("files/javaScriptTitle.html"));
121 EXPECT_EQ(base::ASCIIToUTF16("Title from script javascript enabled"),
122 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
123 browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled,
124 false);
125 ui_test_utils::NavigateToURL(
126 browser(),
127 test_server()->GetURL("files/javaScriptTitle.html"));
128 EXPECT_EQ(base::ASCIIToUTF16("This is html title"),
129 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
132 // Verify restore for bookmark bar visibility.
133 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest,
134 TestSessionRestoreShowBookmarkBar) {
135 EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
136 bookmarks::prefs::kShowBookmarkBar));
137 browser()->profile()->GetPrefs()->SetBoolean(
138 bookmarks::prefs::kShowBookmarkBar, true);
139 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
140 bookmarks::prefs::kShowBookmarkBar));
142 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
143 bookmarks::prefs::kShowBookmarkBar));
144 EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
147 // Verify images are not blocked in incognito mode.
148 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestImagesNotBlockedInIncognito) {
149 ASSERT_TRUE(test_server()->Start());
150 GURL url = test_server()->GetURL("files/settings/image_page.html");
151 Browser* incognito_browser = CreateIncognitoBrowser();
152 ui_test_utils::NavigateToURL(incognito_browser, url);
154 bool result = false;
155 std::string script =
156 "for (i=0; i < document.images.length; i++) {"
157 " if ((document.images[i].naturalWidth != 0) &&"
158 " (document.images[i].naturalHeight != 0)) {"
159 " window.domAutomationController.send(true);"
160 " }"
162 "window.domAutomationController.send(false);";
163 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
164 incognito_browser->tab_strip_model()->GetActiveWebContents(),
165 script,
166 &result));
167 EXPECT_TRUE(result);
170 // Verify setting homepage preference to newtabpage across restarts. Part1
171 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestHomepageNewTabpagePrefs) {
172 browser()->profile()->GetPrefs()->SetBoolean(prefs::kHomePageIsNewTabPage,
173 true);
176 // Verify setting homepage preference to newtabpage across restarts. Part2
177 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHomepageNewTabpagePrefs) {
178 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
179 prefs::kHomePageIsNewTabPage));
182 // Verify setting homepage preference to specific url. Part1
183 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestHomepagePrefs) {
184 GURL home_page_url("http://www.google.com");
186 PrefService* prefs = browser()->profile()->GetPrefs();
187 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false);
188 const PrefService::Preference* pref =
189 prefs->FindPreference(prefs::kHomePage);
190 if (pref && !pref->IsManaged()) {
191 prefs->SetString(prefs::kHomePage, home_page_url.spec());
195 // Verify setting homepage preference to specific url. Part2
196 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHomepagePrefs) {
197 GURL home_page_url("http://www.google.com");
199 PrefService* prefs = browser()->profile()->GetPrefs();
200 EXPECT_FALSE(prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
201 EXPECT_EQ(home_page_url.spec(), prefs->GetString(prefs::kHomePage));
204 // Verify the security preference under privacy across restarts. Part1
205 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestPrivacySecurityPrefs) {
206 PrefService* prefs = browser()->profile()->GetPrefs();
208 static_assert(chrome_browser_net::NETWORK_PREDICTION_DEFAULT !=
209 chrome_browser_net::NETWORK_PREDICTION_NEVER,
210 "PrefsFunctionalTest.TestPrivacySecurityPrefs relies on "
211 "predictive network actions being enabled by default.");
212 EXPECT_EQ(chrome_browser_net::NETWORK_PREDICTION_DEFAULT,
213 prefs->GetInteger(prefs::kNetworkPredictionOptions));
214 prefs->SetInteger(prefs::kNetworkPredictionOptions,
215 chrome_browser_net::NETWORK_PREDICTION_NEVER);
217 EXPECT_TRUE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled));
218 prefs->SetBoolean(prefs::kSafeBrowsingEnabled, false);
220 EXPECT_TRUE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled));
221 prefs->SetBoolean(prefs::kAlternateErrorPagesEnabled, false);
223 EXPECT_TRUE(prefs->GetBoolean(prefs::kSearchSuggestEnabled));
224 prefs->SetBoolean(prefs::kSearchSuggestEnabled, false);
227 // Verify the security preference under privacy across restarts. Part2
228 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestPrivacySecurityPrefs) {
229 PrefService* prefs = browser()->profile()->GetPrefs();
231 EXPECT_EQ(chrome_browser_net::NETWORK_PREDICTION_NEVER,
232 prefs->GetInteger(prefs::kNetworkPredictionOptions));
233 EXPECT_FALSE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled));
234 EXPECT_FALSE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled));
235 EXPECT_FALSE(prefs->GetBoolean(prefs::kSearchSuggestEnabled));
238 // Verify that we have some Local State prefs.
239 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHaveLocalStatePrefs) {
240 EXPECT_TRUE(g_browser_process->local_state()->GetPreferenceValues().get());