Add GCMChannelStatusSyncer to schedule requests and enable/disable GCM
[chromium-blink-merge.git] / chrome / browser / prefs / pref_functional_browsertest.cc
blobdb5b8d75e7de67f4f76c4ad8e9703eb2df726fa7
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/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 {
28 protected:
29 // Create a DownloadTestObserverTerminal that will wait for the
30 // specified number of downloads to finish.
31 scoped_ptr<content::DownloadTestObserver> CreateWaiter(Browser* browser,
32 int num_downloads) {
33 DownloadManager* download_manager =
34 BrowserContext::GetDownloadManager(browser->profile());
36 content::DownloadTestObserver* downloads_observer =
37 new content::DownloadTestObserverTerminal(
38 download_manager,
39 num_downloads,
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());
47 base::ScopedTempDir new_download_dir;
48 ASSERT_TRUE(new_download_dir.CreateUniqueTempDir());
50 base::FilePath downloaded_pkg =
51 new_download_dir.path().AppendASCII("a_zip_file.zip");
53 // Set pref to download in new_download_dir.
54 browser()->profile()->GetPrefs()->SetFilePath(
55 prefs::kDownloadDefaultDirectory, new_download_dir.path());
57 // Create a downloads observer.
58 scoped_ptr<content::DownloadTestObserver> downloads_observer(
59 CreateWaiter(browser(), 1));
60 ui_test_utils::NavigateToURL(
61 browser(),
62 test_server()->GetURL("files/downloads/a_zip_file.zip"));
63 // Waits for the download to complete.
64 downloads_observer->WaitForFinished();
65 EXPECT_TRUE(base::PathExists(downloaded_pkg));
68 // Verify image content settings show or hide images.
69 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestImageContentSettings) {
70 ASSERT_TRUE(test_server()->Start());
72 ui_test_utils::NavigateToURL(
73 browser(),
74 test_server()->GetURL("files/settings/image_page.html"));
76 bool result = false;
77 std::string script =
78 "for (i=0; i < document.images.length; i++) {"
79 " if ((document.images[i].naturalWidth != 0) &&"
80 " (document.images[i].naturalHeight != 0)) {"
81 " window.domAutomationController.send(true);"
82 " }"
83 "}"
84 "window.domAutomationController.send(false);";
85 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
86 browser()->tab_strip_model()->GetActiveWebContents(),
87 script,
88 &result));
89 EXPECT_TRUE(result);
91 base::DictionaryValue value;
92 value.SetInteger("images", 2);
93 browser()->profile()->GetPrefs()->Set(prefs::kDefaultContentSettings,
94 value);
96 ui_test_utils::NavigateToURL(
97 browser(),
98 test_server()->GetURL("files/settings/image_page.html"));
100 result = false;
101 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
102 browser()->tab_strip_model()->GetActiveWebContents(),
103 script,
104 &result));
105 EXPECT_FALSE(result);
108 // Verify that enabling/disabling Javascript in prefs works.
109 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestJavascriptEnableDisable) {
110 ASSERT_TRUE(test_server()->Start());
112 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
113 prefs::kWebKitJavascriptEnabled));
114 ui_test_utils::NavigateToURL(
115 browser(),
116 test_server()->GetURL("files/javaScriptTitle.html"));
117 EXPECT_EQ(base::ASCIIToUTF16("Title from script javascript enabled"),
118 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
119 browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled,
120 false);
121 ui_test_utils::NavigateToURL(
122 browser(),
123 test_server()->GetURL("files/javaScriptTitle.html"));
124 EXPECT_EQ(base::ASCIIToUTF16("This is html title"),
125 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
128 // Verify DNS prefetching pref.
129 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestNetworkPredictionEnabledPref) {
130 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
131 prefs::kNetworkPredictionEnabled));
132 browser()->profile()->GetPrefs()->SetBoolean(prefs::kNetworkPredictionEnabled,
133 false);
134 EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
135 prefs::kNetworkPredictionEnabled));
138 // Verify restore for bookmark bar visibility.
139 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest,
140 TestSessionRestoreShowBookmarkBar) {
141 EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
142 bookmarks::prefs::kShowBookmarkBar));
143 browser()->profile()->GetPrefs()->SetBoolean(
144 bookmarks::prefs::kShowBookmarkBar, true);
145 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
146 bookmarks::prefs::kShowBookmarkBar));
148 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
149 bookmarks::prefs::kShowBookmarkBar));
150 EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
153 // Verify images are not blocked in incognito mode.
154 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestImagesNotBlockedInIncognito) {
155 ASSERT_TRUE(test_server()->Start());
156 GURL url = test_server()->GetURL("files/settings/image_page.html");
157 Browser* incognito_browser = CreateIncognitoBrowser();
158 ui_test_utils::NavigateToURLWithDisposition(
159 incognito_browser,
160 url,
161 CURRENT_TAB,
162 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
164 bool result = false;
165 std::string script =
166 "for (i=0; i < document.images.length; i++) {"
167 " if ((document.images[i].naturalWidth != 0) &&"
168 " (document.images[i].naturalHeight != 0)) {"
169 " window.domAutomationController.send(true);"
170 " }"
172 "window.domAutomationController.send(false);";
173 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
174 incognito_browser->tab_strip_model()->GetActiveWebContents(),
175 script,
176 &result));
177 EXPECT_TRUE(result);
180 // Verify setting homepage preference to newtabpage across restarts. Part1
181 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestHomepageNewTabpagePrefs) {
182 browser()->profile()->GetPrefs()->SetBoolean(prefs::kHomePageIsNewTabPage,
183 true);
186 // Verify setting homepage preference to newtabpage across restarts. Part2
187 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHomepageNewTabpagePrefs) {
188 EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
189 prefs::kHomePageIsNewTabPage));
192 // Verify setting homepage preference to specific url. Part1
193 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestHomepagePrefs) {
194 GURL home_page_url("http://www.google.com");
196 PrefService* prefs = browser()->profile()->GetPrefs();
197 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false);
198 const PrefService::Preference* pref =
199 prefs->FindPreference(prefs::kHomePage);
200 if (pref && !pref->IsManaged()) {
201 prefs->SetString(prefs::kHomePage, home_page_url.spec());
205 // Verify setting homepage preference to specific url. Part2
206 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHomepagePrefs) {
207 GURL home_page_url("http://www.google.com");
209 PrefService* prefs = browser()->profile()->GetPrefs();
210 EXPECT_FALSE(prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
211 EXPECT_EQ(home_page_url.spec(), prefs->GetString(prefs::kHomePage));
214 // Verify the security preference under privacy across restarts. Part1
215 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, PRE_TestPrivacySecurityPrefs) {
216 PrefService* prefs = browser()->profile()->GetPrefs();
218 EXPECT_TRUE(prefs->GetBoolean(prefs::kNetworkPredictionEnabled));
219 prefs->SetBoolean(prefs::kNetworkPredictionEnabled, false);
221 EXPECT_TRUE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled));
222 prefs->SetBoolean(prefs::kSafeBrowsingEnabled, false);
224 EXPECT_TRUE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled));
225 prefs->SetBoolean(prefs::kAlternateErrorPagesEnabled, false);
227 EXPECT_TRUE(prefs->GetBoolean(prefs::kSearchSuggestEnabled));
228 prefs->SetBoolean(prefs::kSearchSuggestEnabled, false);
231 // Verify the security preference under privacy across restarts. Part2
232 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestPrivacySecurityPrefs) {
233 PrefService* prefs = browser()->profile()->GetPrefs();
235 EXPECT_FALSE(prefs->GetBoolean(prefs::kNetworkPredictionEnabled));
236 EXPECT_FALSE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled));
237 EXPECT_FALSE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled));
238 EXPECT_FALSE(prefs->GetBoolean(prefs::kSearchSuggestEnabled));
241 // Verify that we have some Local State prefs.
242 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHaveLocalStatePrefs) {
243 EXPECT_TRUE(g_browser_process->local_state()->GetPreferenceValues().get());