blob: 877ffe9b6afb76b7b79611b5a965d9d6217ed122 [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
[email protected]6c491c62013-08-02 18:59:007#include "base/strings/utf_string_conversions.h"
8#include "chrome/browser/download/download_prefs.h"
bncf17f84b2015-09-14 22:13:269#include "chrome/browser/net/prediction_options.h"
[email protected]6c491c62013-08-02 18:59:0010#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"
[email protected]d3e9f3d2013-10-30 16:46:4117#include "chrome/test/base/testing_browser_process.h"
[email protected]6c491c62013-08-02 18:59:0018#include "chrome/test/base/testing_profile.h"
19#include "chrome/test/base/ui_test_utils.h"
raymesbc7409e2015-08-06 01:57:4520#include "components/content_settings/core/browser/website_settings_info.h"
21#include "components/content_settings/core/browser/website_settings_registry.h"
22#include "components/content_settings/core/common/content_settings_types.h"
[email protected]6c491c62013-08-02 18:59:0023#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"
26
27using content::BrowserContext;
28using content::DownloadManager;
29
30class 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());
38
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);
45 }
46};
47
48IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestDownloadDirPref) {
49 ASSERT_TRUE(test_server()->Start());
[email protected]5079b2b2014-05-10 21:31:3250 base::ScopedTempDir new_download_dir;
51 ASSERT_TRUE(new_download_dir.CreateUniqueTempDir());
[email protected]6c491c62013-08-02 18:59:0052
[email protected]6c491c62013-08-02 18:59:0053 base::FilePath downloaded_pkg =
[email protected]5079b2b2014-05-10 21:31:3254 new_download_dir.path().AppendASCII("a_zip_file.zip");
[email protected]6c491c62013-08-02 18:59:0055
[email protected]6c491c62013-08-02 18:59:0056 // Set pref to download in new_download_dir.
57 browser()->profile()->GetPrefs()->SetFilePath(
[email protected]5079b2b2014-05-10 21:31:3258 prefs::kDownloadDefaultDirectory, new_download_dir.path());
[email protected]6c491c62013-08-02 18:59:0059
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));
69}
70
71// Verify image content settings show or hide images.
72IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestImageContentSettings) {
73 ASSERT_TRUE(test_server()->Start());
74
75 ui_test_utils::NavigateToURL(
76 browser(),
77 test_server()->GetURL("files/settings/image_page.html"));
78
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);
93
msramek96812202015-07-27 16:00:0294 browser()->profile()->GetPrefs()->SetInteger(
raymesbc7409e2015-08-06 01:57:4595 content_settings::WebsiteSettingsRegistry::GetInstance()
96 ->Get(CONTENT_SETTINGS_TYPE_IMAGES)
97 ->default_value_pref_name(),
98 CONTENT_SETTING_BLOCK);
[email protected]6c491c62013-08-02 18:59:0099
100 ui_test_utils::NavigateToURL(
101 browser(),
102 test_server()->GetURL("files/settings/image_page.html"));
103
104 result = false;
105 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
106 browser()->tab_strip_model()->GetActiveWebContents(),
107 script,
108 &result));
109 EXPECT_FALSE(result);
110}
111
112// Verify that enabling/disabling Javascript in prefs works.
113IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestJavascriptEnableDisable) {
114 ASSERT_TRUE(test_server()->Start());
115
116 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
117 prefs::kWebKitJavascriptEnabled));
118 ui_test_utils::NavigateToURL(
119 browser(),
120 test_server()->GetURL("files/javaScriptTitle.html"));
[email protected]6778fed2013-12-24 20:09:37121 EXPECT_EQ(base::ASCIIToUTF16("Title from script javascript enabled"),
[email protected]6c491c62013-08-02 18:59:00122 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"));
[email protected]6778fed2013-12-24 20:09:37128 EXPECT_EQ(base::ASCIIToUTF16("This is html title"),
[email protected]6c491c62013-08-02 18:59:00129 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
130}
131
[email protected]6c491c62013-08-02 18:59:00132// Verify restore for bookmark bar visibility.
133IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest,
134 TestSessionRestoreShowBookmarkBar) {
135 EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
tfarina3bddbe112014-08-28 05:29:32136 bookmarks::prefs::kShowBookmarkBar));
137 browser()->profile()->GetPrefs()->SetBoolean(
138 bookmarks::prefs::kShowBookmarkBar, true);
[email protected]6c491c62013-08-02 18:59:00139 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
tfarina3bddbe112014-08-28 05:29:32140 bookmarks::prefs::kShowBookmarkBar));
[email protected]6c491c62013-08-02 18:59:00141
142 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
tfarina3bddbe112014-08-28 05:29:32143 bookmarks::prefs::kShowBookmarkBar));
[email protected]6c491c62013-08-02 18:59:00144 EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
145}
[email protected]d3e9f3d2013-10-30 16:46:41146
147// Verify images are not blocked in incognito mode.
148IN_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();
thestig53986dc2014-12-16 06:09:18152 ui_test_utils::NavigateToURL(incognito_browser, url);
[email protected]d3e9f3d2013-10-30 16:46:41153
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 " }"
161 "}"
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);
168}
169
170// Verify setting homepage preference to newtabpage across restarts. Part1
171IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestHomepageNewTabpagePrefs) {
172 browser()->profile()->GetPrefs()->SetBoolean(prefs::kHomePageIsNewTabPage,
173 true);
174}
175
176// Verify setting homepage preference to newtabpage across restarts. Part2
177IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHomepageNewTabpagePrefs) {
178 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
179 prefs::kHomePageIsNewTabPage));
180}
181
182// Verify setting homepage preference to specific url. Part1
183IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestHomepagePrefs) {
184 GURL home_page_url("http://www.google.com");
185
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());
192 }
193}
194
195// Verify setting homepage preference to specific url. Part2
196IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHomepagePrefs) {
197 GURL home_page_url("http://www.google.com");
198
199 PrefService* prefs = browser()->profile()->GetPrefs();
200 EXPECT_FALSE(prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
201 EXPECT_EQ(home_page_url.spec(), prefs->GetString(prefs::kHomePage));
202}
203
204// Verify the security preference under privacy across restarts. Part1
205IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestPrivacySecurityPrefs) {
206 PrefService* prefs = browser()->profile()->GetPrefs();
207
bncf17f84b2015-09-14 22:13:26208 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);
[email protected]d3e9f3d2013-10-30 16:46:41216
217 EXPECT_TRUE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled));
218 prefs->SetBoolean(prefs::kSafeBrowsingEnabled, false);
219
220 EXPECT_TRUE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled));
221 prefs->SetBoolean(prefs::kAlternateErrorPagesEnabled, false);
222
223 EXPECT_TRUE(prefs->GetBoolean(prefs::kSearchSuggestEnabled));
224 prefs->SetBoolean(prefs::kSearchSuggestEnabled, false);
225}
226
227// Verify the security preference under privacy across restarts. Part2
228IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestPrivacySecurityPrefs) {
229 PrefService* prefs = browser()->profile()->GetPrefs();
230
bncf17f84b2015-09-14 22:13:26231 EXPECT_EQ(chrome_browser_net::NETWORK_PREDICTION_NEVER,
232 prefs->GetInteger(prefs::kNetworkPredictionOptions));
[email protected]d3e9f3d2013-10-30 16:46:41233 EXPECT_FALSE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled));
234 EXPECT_FALSE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled));
235 EXPECT_FALSE(prefs->GetBoolean(prefs::kSearchSuggestEnabled));
236}
237
238// Verify that we have some Local State prefs.
239IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHaveLocalStatePrefs) {
[email protected]ddf421c32013-11-01 00:52:35240 EXPECT_TRUE(g_browser_process->local_state()->GetPreferenceValues().get());
[email protected]d3e9f3d2013-10-30 16:46:41241}
242