1 // Copyright (c) 2012 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/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/extensions/component_loader.h"
15 #include "chrome/browser/first_run/first_run.h"
16 #include "chrome/browser/importer/importer_list.h"
17 #include "chrome/browser/prefs/chrome_pref_service_factory.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/common/url_constants.h"
24 #include "chrome/test/base/in_process_browser_test.h"
25 #include "chrome/test/base/ui_test_utils.h"
26 #include "components/user_prefs/user_prefs.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/common/content_switches.h"
29 #include "content/public/test/test_launcher.h"
30 #include "testing/gtest/include/gtest/gtest.h"
32 typedef InProcessBrowserTest FirstRunBrowserTest;
34 IN_PROC_BROWSER_TEST_F(FirstRunBrowserTest, SetShowFirstRunBubblePref) {
35 EXPECT_TRUE(g_browser_process->local_state()->FindPreference(
36 prefs::kShowFirstRunBubbleOption));
37 EXPECT_EQ(first_run::FIRST_RUN_BUBBLE_DONT_SHOW,
38 g_browser_process->local_state()->GetInteger(
39 prefs::kShowFirstRunBubbleOption));
40 EXPECT_TRUE(first_run::SetShowFirstRunBubblePref(
41 first_run::FIRST_RUN_BUBBLE_SHOW));
42 ASSERT_TRUE(g_browser_process->local_state()->FindPreference(
43 prefs::kShowFirstRunBubbleOption));
44 EXPECT_EQ(first_run::FIRST_RUN_BUBBLE_SHOW,
45 g_browser_process->local_state()->GetInteger(
46 prefs::kShowFirstRunBubbleOption));
47 // Test that toggling the value works in either direction after it's been set.
48 EXPECT_TRUE(first_run::SetShowFirstRunBubblePref(
49 first_run::FIRST_RUN_BUBBLE_DONT_SHOW));
50 EXPECT_EQ(first_run::FIRST_RUN_BUBBLE_DONT_SHOW,
51 g_browser_process->local_state()->GetInteger(
52 prefs::kShowFirstRunBubbleOption));
53 // Test that the value can't be set to FIRST_RUN_BUBBLE_SHOW after it has been
54 // set to FIRST_RUN_BUBBLE_SUPPRESS.
55 EXPECT_TRUE(first_run::SetShowFirstRunBubblePref(
56 first_run::FIRST_RUN_BUBBLE_SUPPRESS));
57 EXPECT_EQ(first_run::FIRST_RUN_BUBBLE_SUPPRESS,
58 g_browser_process->local_state()->GetInteger(
59 prefs::kShowFirstRunBubbleOption));
60 EXPECT_TRUE(first_run::SetShowFirstRunBubblePref(
61 first_run::FIRST_RUN_BUBBLE_SHOW));
62 EXPECT_EQ(first_run::FIRST_RUN_BUBBLE_SUPPRESS,
63 g_browser_process->local_state()->GetInteger(
64 prefs::kShowFirstRunBubbleOption));
67 IN_PROC_BROWSER_TEST_F(FirstRunBrowserTest, SetShouldShowWelcomePage) {
68 EXPECT_FALSE(first_run::ShouldShowWelcomePage());
69 first_run::SetShouldShowWelcomePage();
70 EXPECT_TRUE(first_run::ShouldShowWelcomePage());
71 EXPECT_FALSE(first_run::ShouldShowWelcomePage());
74 #if !defined(OS_CHROMEOS)
77 // A generic test class to be subclassed by test classes testing specific
78 // master_preferences. All subclasses must call SetMasterPreferencesForTest()
79 // from their SetUp() method before deferring the remainder of Setup() to this
81 class FirstRunMasterPrefsBrowserTestBase : public InProcessBrowserTest {
83 FirstRunMasterPrefsBrowserTestBase() {}
86 virtual void SetUp() OVERRIDE {
87 // All users of this test class need to call SetMasterPreferencesForTest()
88 // before this class' SetUp() is invoked.
89 ASSERT_TRUE(text_.get());
91 ASSERT_TRUE(base::CreateTemporaryFile(&prefs_file_));
92 EXPECT_TRUE(base::WriteFile(prefs_file_, text_->c_str(), text_->size()));
93 first_run::SetMasterPrefsPathForTesting(prefs_file_);
95 // This invokes BrowserMain, and does the import, so must be done last.
96 InProcessBrowserTest::SetUp();
99 virtual void TearDown() OVERRIDE {
100 EXPECT_TRUE(base::DeleteFile(prefs_file_, false));
101 InProcessBrowserTest::TearDown();
104 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
105 InProcessBrowserTest::SetUpCommandLine(command_line);
106 command_line->AppendSwitch(switches::kForceFirstRun);
107 EXPECT_EQ(first_run::AUTO_IMPORT_NONE, first_run::auto_import_state());
109 extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
112 void SetMasterPreferencesForTest(const char text[]) {
113 text_.reset(new std::string(text));
117 base::FilePath prefs_file_;
118 scoped_ptr<std::string> text_;
120 DISALLOW_COPY_AND_ASSIGN(FirstRunMasterPrefsBrowserTestBase);
123 template<const char Text[]>
124 class FirstRunMasterPrefsBrowserTestT
125 : public FirstRunMasterPrefsBrowserTestBase {
127 FirstRunMasterPrefsBrowserTestT() {}
130 virtual void SetUp() OVERRIDE {
131 SetMasterPreferencesForTest(Text);
132 FirstRunMasterPrefsBrowserTestBase::SetUp();
136 DISALLOW_COPY_AND_ASSIGN(FirstRunMasterPrefsBrowserTestT);
139 // Returns the true expected import state, derived from the original
140 // |expected_import_state|, for the current test machine's configuration. Some
141 // bot configurations do not have another profile (browser) to import from and
142 // thus the import must not be expected to have occurred.
143 int MaskExpectedImportState(int expected_import_state) {
144 scoped_refptr<ImporterList> importer_list(new ImporterList());
145 importer_list->DetectSourceProfilesHack(
146 g_browser_process->GetApplicationLocale(), false);
147 int source_profile_count = importer_list->count();
149 // On Windows, the importer's DetectIEProfiles() will always add to the count.
150 // Internet Explorer always exists and always has something to import.
151 EXPECT_GT(source_profile_count, 0);
153 if (source_profile_count == 0)
154 return expected_import_state & ~first_run::AUTO_IMPORT_PROFILE_IMPORTED;
156 return expected_import_state;
161 extern const char kImportDefault[] =
164 typedef FirstRunMasterPrefsBrowserTestT<kImportDefault>
165 FirstRunMasterPrefsImportDefault;
166 // http://crbug.com/314221
167 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_MACOSX) || defined(OS_LINUX))
168 #define MAYBE_ImportDefault DISABLED_ImportDefault
170 #define MAYBE_ImportDefault ImportDefault
172 IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportDefault, MAYBE_ImportDefault) {
173 int auto_import_state = first_run::auto_import_state();
174 EXPECT_EQ(MaskExpectedImportState(first_run::AUTO_IMPORT_CALLED |
175 first_run::AUTO_IMPORT_PROFILE_IMPORTED),
179 // The bookmarks file doesn't actually need to exist for this integration test
180 // to trigger the interaction being tested.
181 extern const char kImportBookmarksFile[] =
183 " \"distribution\": {\n"
184 " \"import_bookmarks_from_file\": \"/foo/doesntexists.wtv\"\n"
187 typedef FirstRunMasterPrefsBrowserTestT<kImportBookmarksFile>
188 FirstRunMasterPrefsImportBookmarksFile;
189 // http://crbug.com/314221
190 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_MACOSX) || defined(OS_LINUX))
191 #define MAYBE_ImportBookmarksFile DISABLED_ImportBookmarksFile
193 #define MAYBE_ImportBookmarksFile ImportBookmarksFile
195 IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportBookmarksFile,
196 MAYBE_ImportBookmarksFile) {
197 int auto_import_state = first_run::auto_import_state();
199 MaskExpectedImportState(first_run::AUTO_IMPORT_CALLED |
200 first_run::AUTO_IMPORT_PROFILE_IMPORTED |
201 first_run::AUTO_IMPORT_BOOKMARKS_FILE_IMPORTED),
205 // Test an import with all import options disabled. This is a regression test
206 // for http://crbug.com/169984 where this would cause the import process to
207 // stay running, and the NTP to be loaded with no apps.
208 extern const char kImportNothing[] =
210 " \"distribution\": {\n"
211 " \"import_bookmarks\": false,\n"
212 " \"import_history\": false,\n"
213 " \"import_home_page\": false,\n"
214 " \"import_search_engine\": false\n"
217 typedef FirstRunMasterPrefsBrowserTestT<kImportNothing>
218 FirstRunMasterPrefsImportNothing;
219 // http://crbug.com/314221
220 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_MACOSX) || defined(OS_LINUX))
221 #define MAYBE_ImportNothingAndShowNewTabPage \
222 DISABLED_ImportNothingAndShowNewTabPage
224 #define MAYBE_ImportNothingAndShowNewTabPage ImportNothingAndShowNewTabPage
226 IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportNothing,
227 MAYBE_ImportNothingAndShowNewTabPage) {
228 EXPECT_EQ(first_run::AUTO_IMPORT_CALLED, first_run::auto_import_state());
229 ui_test_utils::NavigateToURLWithDisposition(
230 browser(), GURL(chrome::kChromeUINewTabURL), CURRENT_TAB,
231 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
232 content::WebContents* tab = browser()->tab_strip_model()->GetWebContentsAt(0);
233 EXPECT_EQ(1, tab->GetMaxPageID());
236 // Test first run with some tracked preferences.
237 extern const char kWithTrackedPrefs[] =
239 " \"homepage\": \"example.com\",\n"
240 " \"homepage_is_newtabpage\": false\n"
242 // A test fixture that will run in a first run scenario with master_preferences
243 // set to kWithTrackedPrefs. Parameterizable on the SettingsEnforcement
244 // experiment to be forced.
245 class FirstRunMasterPrefsWithTrackedPreferences
246 : public FirstRunMasterPrefsBrowserTestT<kWithTrackedPrefs>,
247 public testing::WithParamInterface<std::string> {
249 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
250 FirstRunMasterPrefsBrowserTestT::SetUpCommandLine(command_line);
251 command_line->AppendSwitchASCII(
252 switches::kForceFieldTrials,
253 std::string(chrome_prefs::internals::kSettingsEnforcementTrialName) +
254 "/" + GetParam() + "/");
258 // http://crbug.com/314221
259 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_MACOSX) || defined(OS_LINUX))
260 #define MAYBE_TrackedPreferencesSurviveFirstRun \
261 DISABLED_TrackedPreferencesSurviveFirstRun
263 #define MAYBE_TrackedPreferencesSurviveFirstRun \
264 TrackedPreferencesSurviveFirstRun
266 IN_PROC_BROWSER_TEST_P(FirstRunMasterPrefsWithTrackedPreferences,
267 MAYBE_TrackedPreferencesSurviveFirstRun) {
268 const PrefService* user_prefs = browser()->profile()->GetPrefs();
269 EXPECT_EQ("example.com", user_prefs->GetString(prefs::kHomePage));
270 EXPECT_FALSE(user_prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
272 // The test for kHomePageIsNewTabPage above relies on the fact that true is
273 // the default (hence false must be the user's pref); ensure this fact remains
275 const base::Value* default_homepage_is_ntp_value =
276 user_prefs->GetDefaultPrefValue(prefs::kHomePageIsNewTabPage);
277 ASSERT_TRUE(default_homepage_is_ntp_value != NULL);
278 bool default_homepage_is_ntp = false;
280 default_homepage_is_ntp_value->GetAsBoolean(&default_homepage_is_ntp));
281 EXPECT_TRUE(default_homepage_is_ntp);
284 INSTANTIATE_TEST_CASE_P(
285 FirstRunMasterPrefsWithTrackedPreferencesInstance,
286 FirstRunMasterPrefsWithTrackedPreferences,
288 chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement,
289 chrome_prefs::internals::kSettingsEnforcementGroupEnforceOnload,
290 chrome_prefs::internals::kSettingsEnforcementGroupEnforceAlways,
291 chrome_prefs::internals::
292 kSettingsEnforcementGroupEnforceAlwaysWithExtensions));
294 #endif // !defined(OS_CHROMEOS)