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/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/extensions/component_loader.h"
16 #include "chrome/browser/first_run/first_run.h"
17 #include "chrome/browser/importer/importer_list.h"
18 #include "chrome/browser/prefs/chrome_pref_service_factory.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/common/url_constants.h"
25 #include "chrome/test/base/in_process_browser_test.h"
26 #include "chrome/test/base/ui_test_utils.h"
27 #include "components/user_prefs/user_prefs.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/common/content_switches.h"
30 #include "content/public/test/browser_test_utils.h"
31 #include "content/public/test/test_launcher.h"
32 #include "testing/gtest/include/gtest/gtest.h"
34 typedef InProcessBrowserTest FirstRunBrowserTest
;
36 IN_PROC_BROWSER_TEST_F(FirstRunBrowserTest
, SetShowFirstRunBubblePref
) {
37 EXPECT_TRUE(g_browser_process
->local_state()->FindPreference(
38 prefs::kShowFirstRunBubbleOption
));
39 EXPECT_EQ(first_run::FIRST_RUN_BUBBLE_DONT_SHOW
,
40 g_browser_process
->local_state()->GetInteger(
41 prefs::kShowFirstRunBubbleOption
));
42 EXPECT_TRUE(first_run::SetShowFirstRunBubblePref(
43 first_run::FIRST_RUN_BUBBLE_SHOW
));
44 ASSERT_TRUE(g_browser_process
->local_state()->FindPreference(
45 prefs::kShowFirstRunBubbleOption
));
46 EXPECT_EQ(first_run::FIRST_RUN_BUBBLE_SHOW
,
47 g_browser_process
->local_state()->GetInteger(
48 prefs::kShowFirstRunBubbleOption
));
49 // Test that toggling the value works in either direction after it's been set.
50 EXPECT_TRUE(first_run::SetShowFirstRunBubblePref(
51 first_run::FIRST_RUN_BUBBLE_DONT_SHOW
));
52 EXPECT_EQ(first_run::FIRST_RUN_BUBBLE_DONT_SHOW
,
53 g_browser_process
->local_state()->GetInteger(
54 prefs::kShowFirstRunBubbleOption
));
55 // Test that the value can't be set to FIRST_RUN_BUBBLE_SHOW after it has been
56 // set to FIRST_RUN_BUBBLE_SUPPRESS.
57 EXPECT_TRUE(first_run::SetShowFirstRunBubblePref(
58 first_run::FIRST_RUN_BUBBLE_SUPPRESS
));
59 EXPECT_EQ(first_run::FIRST_RUN_BUBBLE_SUPPRESS
,
60 g_browser_process
->local_state()->GetInteger(
61 prefs::kShowFirstRunBubbleOption
));
62 EXPECT_TRUE(first_run::SetShowFirstRunBubblePref(
63 first_run::FIRST_RUN_BUBBLE_SHOW
));
64 EXPECT_EQ(first_run::FIRST_RUN_BUBBLE_SUPPRESS
,
65 g_browser_process
->local_state()->GetInteger(
66 prefs::kShowFirstRunBubbleOption
));
69 IN_PROC_BROWSER_TEST_F(FirstRunBrowserTest
, SetShouldShowWelcomePage
) {
70 EXPECT_FALSE(first_run::ShouldShowWelcomePage());
71 first_run::SetShouldShowWelcomePage();
72 EXPECT_TRUE(first_run::ShouldShowWelcomePage());
73 EXPECT_FALSE(first_run::ShouldShowWelcomePage());
76 #if !defined(OS_CHROMEOS)
79 // A generic test class to be subclassed by test classes testing specific
80 // master_preferences. All subclasses must call SetMasterPreferencesForTest()
81 // from their SetUp() method before deferring the remainder of Setup() to this
83 class FirstRunMasterPrefsBrowserTestBase
: public InProcessBrowserTest
{
85 FirstRunMasterPrefsBrowserTestBase() {}
88 void SetUp() override
{
89 // All users of this test class need to call SetMasterPreferencesForTest()
90 // before this class' SetUp() is invoked.
91 ASSERT_TRUE(text_
.get());
93 ASSERT_TRUE(base::CreateTemporaryFile(&prefs_file_
));
94 EXPECT_TRUE(base::WriteFile(prefs_file_
, text_
->c_str(), text_
->size()));
95 first_run::SetMasterPrefsPathForTesting(prefs_file_
);
97 // This invokes BrowserMain, and does the import, so must be done last.
98 InProcessBrowserTest::SetUp();
101 void TearDown() override
{
102 EXPECT_TRUE(base::DeleteFile(prefs_file_
, false));
103 InProcessBrowserTest::TearDown();
106 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
107 InProcessBrowserTest::SetUpCommandLine(command_line
);
108 command_line
->AppendSwitch(switches::kForceFirstRun
);
109 EXPECT_EQ(first_run::AUTO_IMPORT_NONE
, first_run::auto_import_state());
111 extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
114 void SetMasterPreferencesForTest(const char text
[]) {
115 text_
.reset(new std::string(text
));
119 base::FilePath prefs_file_
;
120 scoped_ptr
<std::string
> text_
;
122 DISALLOW_COPY_AND_ASSIGN(FirstRunMasterPrefsBrowserTestBase
);
125 template<const char Text
[]>
126 class FirstRunMasterPrefsBrowserTestT
127 : public FirstRunMasterPrefsBrowserTestBase
{
129 FirstRunMasterPrefsBrowserTestT() {}
132 void SetUp() override
{
133 SetMasterPreferencesForTest(Text
);
134 FirstRunMasterPrefsBrowserTestBase::SetUp();
138 DISALLOW_COPY_AND_ASSIGN(FirstRunMasterPrefsBrowserTestT
);
141 // Returns the true expected import state, derived from the original
142 // |expected_import_state|, for the current test machine's configuration. Some
143 // bot configurations do not have another profile (browser) to import from and
144 // thus the import must not be expected to have occurred.
145 int MaskExpectedImportState(int expected_import_state
) {
146 scoped_ptr
<ImporterList
> importer_list(new ImporterList());
147 base::RunLoop run_loop
;
148 importer_list
->DetectSourceProfiles(
149 g_browser_process
->GetApplicationLocale(),
150 false, // include_interactive_profiles?
151 run_loop
.QuitClosure());
153 int source_profile_count
= importer_list
->count();
155 // On Windows, the importer's DetectIEProfiles() will always add to the count.
156 // Internet Explorer always exists and always has something to import.
157 EXPECT_GT(source_profile_count
, 0);
159 if (source_profile_count
== 0)
160 return expected_import_state
& ~first_run::AUTO_IMPORT_PROFILE_IMPORTED
;
161 return expected_import_state
;
166 extern const char kImportDefault
[] =
169 typedef FirstRunMasterPrefsBrowserTestT
<kImportDefault
>
170 FirstRunMasterPrefsImportDefault
;
171 // http://crbug.com/314221
172 #if defined(OS_MACOSX) || (defined(GOOGLE_CHROME_BUILD) && defined(OS_LINUX))
173 #define MAYBE_ImportDefault DISABLED_ImportDefault
175 #define MAYBE_ImportDefault ImportDefault
177 IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportDefault
, MAYBE_ImportDefault
) {
178 int auto_import_state
= first_run::auto_import_state();
179 EXPECT_EQ(MaskExpectedImportState(first_run::AUTO_IMPORT_CALLED
|
180 first_run::AUTO_IMPORT_PROFILE_IMPORTED
),
184 // The bookmarks file doesn't actually need to exist for this integration test
185 // to trigger the interaction being tested.
186 extern const char kImportBookmarksFile
[] =
188 " \"distribution\": {\n"
189 " \"import_bookmarks_from_file\": \"/foo/doesntexists.wtv\"\n"
192 typedef FirstRunMasterPrefsBrowserTestT
<kImportBookmarksFile
>
193 FirstRunMasterPrefsImportBookmarksFile
;
194 // http://crbug.com/314221
195 #if (defined(GOOGLE_CHROME_BUILD) && defined(OS_LINUX)) || defined(OS_MACOSX)
196 #define MAYBE_ImportBookmarksFile DISABLED_ImportBookmarksFile
198 #define MAYBE_ImportBookmarksFile ImportBookmarksFile
200 IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportBookmarksFile
,
201 MAYBE_ImportBookmarksFile
) {
202 int auto_import_state
= first_run::auto_import_state();
204 MaskExpectedImportState(first_run::AUTO_IMPORT_CALLED
|
205 first_run::AUTO_IMPORT_PROFILE_IMPORTED
|
206 first_run::AUTO_IMPORT_BOOKMARKS_FILE_IMPORTED
),
210 // Test an import with all import options disabled. This is a regression test
211 // for http://crbug.com/169984 where this would cause the import process to
212 // stay running, and the NTP to be loaded with no apps.
213 extern const char kImportNothing
[] =
215 " \"distribution\": {\n"
216 " \"import_bookmarks\": false,\n"
217 " \"import_history\": false,\n"
218 " \"import_home_page\": false,\n"
219 " \"import_search_engine\": false\n"
222 typedef FirstRunMasterPrefsBrowserTestT
<kImportNothing
>
223 FirstRunMasterPrefsImportNothing
;
224 // http://crbug.com/314221
225 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_MACOSX) || defined(OS_LINUX))
226 #define MAYBE_ImportNothingAndShowNewTabPage \
227 DISABLED_ImportNothingAndShowNewTabPage
229 #define MAYBE_ImportNothingAndShowNewTabPage ImportNothingAndShowNewTabPage
231 IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportNothing
,
232 MAYBE_ImportNothingAndShowNewTabPage
) {
233 EXPECT_EQ(first_run::AUTO_IMPORT_CALLED
, first_run::auto_import_state());
234 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL
));
235 content::WebContents
* tab
= browser()->tab_strip_model()->GetWebContentsAt(0);
236 EXPECT_TRUE(WaitForLoadStop(tab
));
239 // Test first run with some tracked preferences.
240 extern const char kWithTrackedPrefs
[] =
242 " \"homepage\": \"example.com\",\n"
243 " \"homepage_is_newtabpage\": false\n"
245 // A test fixture that will run in a first run scenario with master_preferences
246 // set to kWithTrackedPrefs. Parameterizable on the SettingsEnforcement
247 // experiment to be forced.
248 class FirstRunMasterPrefsWithTrackedPreferences
249 : public FirstRunMasterPrefsBrowserTestT
<kWithTrackedPrefs
>,
250 public testing::WithParamInterface
<std::string
> {
252 FirstRunMasterPrefsWithTrackedPreferences() {}
255 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
256 FirstRunMasterPrefsBrowserTestT::SetUpCommandLine(command_line
);
257 command_line
->AppendSwitchASCII(
258 switches::kForceFieldTrials
,
259 std::string(chrome_prefs::internals::kSettingsEnforcementTrialName
) +
260 "/" + GetParam() + "/");
263 void SetUpInProcessBrowserTestFixture() override
{
264 FirstRunMasterPrefsBrowserTestT::SetUpInProcessBrowserTestFixture();
266 // Bots are on a domain, turn off the domain check for settings hardening in
267 // order to be able to test all SettingsEnforcement groups.
268 chrome_prefs::DisableDomainCheckForTesting();
272 DISALLOW_COPY_AND_ASSIGN(FirstRunMasterPrefsWithTrackedPreferences
);
275 // http://crbug.com/314221
276 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_MACOSX) || defined(OS_LINUX))
277 #define MAYBE_TrackedPreferencesSurviveFirstRun \
278 DISABLED_TrackedPreferencesSurviveFirstRun
280 #define MAYBE_TrackedPreferencesSurviveFirstRun \
281 TrackedPreferencesSurviveFirstRun
283 IN_PROC_BROWSER_TEST_P(FirstRunMasterPrefsWithTrackedPreferences
,
284 MAYBE_TrackedPreferencesSurviveFirstRun
) {
285 const PrefService
* user_prefs
= browser()->profile()->GetPrefs();
286 EXPECT_EQ("example.com", user_prefs
->GetString(prefs::kHomePage
));
287 EXPECT_FALSE(user_prefs
->GetBoolean(prefs::kHomePageIsNewTabPage
));
289 // The test for kHomePageIsNewTabPage above relies on the fact that true is
290 // the default (hence false must be the user's pref); ensure this fact remains
292 const base::Value
* default_homepage_is_ntp_value
=
293 user_prefs
->GetDefaultPrefValue(prefs::kHomePageIsNewTabPage
);
294 ASSERT_TRUE(default_homepage_is_ntp_value
!= NULL
);
295 bool default_homepage_is_ntp
= false;
297 default_homepage_is_ntp_value
->GetAsBoolean(&default_homepage_is_ntp
));
298 EXPECT_TRUE(default_homepage_is_ntp
);
301 INSTANTIATE_TEST_CASE_P(
302 FirstRunMasterPrefsWithTrackedPreferencesInstance
,
303 FirstRunMasterPrefsWithTrackedPreferences
,
305 chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement
,
306 chrome_prefs::internals::kSettingsEnforcementGroupEnforceAlways
,
307 chrome_prefs::internals::
308 kSettingsEnforcementGroupEnforceAlwaysWithDSE
,
309 chrome_prefs::internals::
310 kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE
));
312 #endif // !defined(OS_CHROMEOS)