Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / first_run / first_run_browsertest.cc
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.
4
5 #include <string>
6
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"
31
32 typedef InProcessBrowserTest FirstRunBrowserTest;
33
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));
65 }
66
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());
72 }
73
74 #if !defined(OS_CHROMEOS)
75 namespace {
76
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
80 // class.
81 class FirstRunMasterPrefsBrowserTestBase : public InProcessBrowserTest {
82  public:
83   FirstRunMasterPrefsBrowserTestBase() {}
84
85  protected:
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());
90
91     ASSERT_TRUE(base::CreateTemporaryFile(&prefs_file_));
92     EXPECT_TRUE(file_util::WriteFile(prefs_file_, text_->c_str(),
93                                      text_->size()));
94     first_run::SetMasterPrefsPathForTesting(prefs_file_);
95
96     // This invokes BrowserMain, and does the import, so must be done last.
97     InProcessBrowserTest::SetUp();
98   }
99
100   virtual void TearDown() OVERRIDE {
101     EXPECT_TRUE(base::DeleteFile(prefs_file_, false));
102     InProcessBrowserTest::TearDown();
103   }
104
105   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
106     InProcessBrowserTest::SetUpCommandLine(command_line);
107     command_line->AppendSwitch(switches::kForceFirstRun);
108     EXPECT_EQ(first_run::AUTO_IMPORT_NONE, first_run::auto_import_state());
109
110     extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
111   }
112
113   void SetMasterPreferencesForTest(const char text[]) {
114     text_.reset(new std::string(text));
115   }
116
117  private:
118   base::FilePath prefs_file_;
119   scoped_ptr<std::string> text_;
120
121   DISALLOW_COPY_AND_ASSIGN(FirstRunMasterPrefsBrowserTestBase);
122 };
123
124 template<const char Text[]>
125 class FirstRunMasterPrefsBrowserTestT
126     : public FirstRunMasterPrefsBrowserTestBase {
127  public:
128   FirstRunMasterPrefsBrowserTestT() {}
129
130  protected:
131   virtual void SetUp() OVERRIDE {
132     SetMasterPreferencesForTest(Text);
133     FirstRunMasterPrefsBrowserTestBase::SetUp();
134   }
135
136  private:
137   DISALLOW_COPY_AND_ASSIGN(FirstRunMasterPrefsBrowserTestT);
138 };
139
140 // Returns the true expected import state, derived from the original
141 // |expected_import_state|, for the current test machine's configuration. Some
142 // bot configurations do not have another profile (browser) to import from and
143 // thus the import must not be expected to have occurred.
144 int MaskExpectedImportState(int expected_import_state) {
145   scoped_refptr<ImporterList> importer_list(new ImporterList());
146   importer_list->DetectSourceProfilesHack(
147       g_browser_process->GetApplicationLocale(), false);
148   int source_profile_count = importer_list->count();
149 #if defined(OS_WIN)
150   // On Windows, the importer's DetectIEProfiles() will always add to the count.
151   // Internet Explorer always exists and always has something to import.
152   EXPECT_GT(source_profile_count, 0);
153 #endif
154   if (source_profile_count == 0)
155     return expected_import_state & ~first_run::AUTO_IMPORT_PROFILE_IMPORTED;
156
157   return expected_import_state;
158 }
159
160 }  // namespace
161
162 extern const char kImportDefault[] =
163     "{\n"
164     "}\n";
165 typedef FirstRunMasterPrefsBrowserTestT<kImportDefault>
166     FirstRunMasterPrefsImportDefault;
167 // http://crbug.com/314221
168 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_MACOSX) || defined(OS_LINUX))
169 #define MAYBE_ImportDefault DISABLED_ImportDefault
170 #else
171 #define MAYBE_ImportDefault ImportDefault
172 #endif
173 IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportDefault, MAYBE_ImportDefault) {
174   int auto_import_state = first_run::auto_import_state();
175   EXPECT_EQ(MaskExpectedImportState(first_run::AUTO_IMPORT_CALLED |
176                                     first_run::AUTO_IMPORT_PROFILE_IMPORTED),
177             auto_import_state);
178 }
179
180 // The bookmarks file doesn't actually need to exist for this integration test
181 // to trigger the interaction being tested.
182 extern const char kImportBookmarksFile[] =
183     "{\n"
184     "  \"distribution\": {\n"
185     "     \"import_bookmarks_from_file\": \"/foo/doesntexists.wtv\"\n"
186     "  }\n"
187     "}\n";
188 typedef FirstRunMasterPrefsBrowserTestT<kImportBookmarksFile>
189     FirstRunMasterPrefsImportBookmarksFile;
190 // http://crbug.com/314221
191 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_MACOSX) || defined(OS_LINUX))
192 #define MAYBE_ImportBookmarksFile DISABLED_ImportBookmarksFile
193 #else
194 #define MAYBE_ImportBookmarksFile ImportBookmarksFile
195 #endif
196 IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportBookmarksFile,
197                        MAYBE_ImportBookmarksFile) {
198   int auto_import_state = first_run::auto_import_state();
199   EXPECT_EQ(
200       MaskExpectedImportState(first_run::AUTO_IMPORT_CALLED |
201                               first_run::AUTO_IMPORT_PROFILE_IMPORTED |
202                               first_run::AUTO_IMPORT_BOOKMARKS_FILE_IMPORTED),
203       auto_import_state);
204 }
205
206 // Test an import with all import options disabled. This is a regression test
207 // for http://crbug.com/169984 where this would cause the import process to
208 // stay running, and the NTP to be loaded with no apps.
209 extern const char kImportNothing[] =
210     "{\n"
211     "  \"distribution\": {\n"
212     "    \"import_bookmarks\": false,\n"
213     "    \"import_history\": false,\n"
214     "    \"import_home_page\": false,\n"
215     "    \"import_search_engine\": false\n"
216     "  }\n"
217     "}\n";
218 typedef FirstRunMasterPrefsBrowserTestT<kImportNothing>
219     FirstRunMasterPrefsImportNothing;
220 // http://crbug.com/314221
221 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_MACOSX) || defined(OS_LINUX))
222 #define MAYBE_ImportNothingAndShowNewTabPage \
223     DISABLED_ImportNothingAndShowNewTabPage
224 #else
225 #define MAYBE_ImportNothingAndShowNewTabPage ImportNothingAndShowNewTabPage
226 #endif
227 IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportNothing,
228                        MAYBE_ImportNothingAndShowNewTabPage) {
229   EXPECT_EQ(first_run::AUTO_IMPORT_CALLED, first_run::auto_import_state());
230   ui_test_utils::NavigateToURLWithDisposition(
231       browser(), GURL(chrome::kChromeUINewTabURL), CURRENT_TAB,
232       ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
233   content::WebContents* tab = browser()->tab_strip_model()->GetWebContentsAt(0);
234   EXPECT_EQ(1, tab->GetMaxPageID());
235 }
236
237 // Test first run with some tracked preferences.
238 extern const char kWithTrackedPrefs[] =
239     "{\n"
240     "  \"homepage\": \"example.com\",\n"
241     "  \"homepage_is_newtabpage\": false\n"
242     "}\n";
243 // A test fixture that will run in a first run scenario with master_preferences
244 // set to kWithTrackedPrefs. Parameterizable on the SettingsEnforcement
245 // experiment to be forced.
246 class FirstRunMasterPrefsWithTrackedPreferences
247     : public FirstRunMasterPrefsBrowserTestT<kWithTrackedPrefs>,
248       public testing::WithParamInterface<std::string> {
249  public:
250   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
251     FirstRunMasterPrefsBrowserTestT::SetUpCommandLine(command_line);
252     command_line->AppendSwitchASCII(
253         switches::kForceFieldTrials,
254         std::string(chrome_prefs::internals::kSettingsEnforcementTrialName) +
255             "/" + GetParam() + "/");
256   }
257 };
258
259 // http://crbug.com/314221
260 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_MACOSX) || defined(OS_LINUX))
261 #define MAYBE_TrackedPreferencesSurviveFirstRun \
262     DISABLED_TrackedPreferencesSurviveFirstRun
263 #else
264 #define MAYBE_TrackedPreferencesSurviveFirstRun \
265     TrackedPreferencesSurviveFirstRun
266 #endif
267 IN_PROC_BROWSER_TEST_P(FirstRunMasterPrefsWithTrackedPreferences,
268                        MAYBE_TrackedPreferencesSurviveFirstRun) {
269   const PrefService* user_prefs = browser()->profile()->GetPrefs();
270   EXPECT_EQ("example.com", user_prefs->GetString(prefs::kHomePage));
271   EXPECT_FALSE(user_prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
272
273   // The test for kHomePageIsNewTabPage above relies on the fact that true is
274   // the default (hence false must be the user's pref); ensure this fact remains
275   // true.
276   const base::Value* default_homepage_is_ntp_value =
277       user_prefs->GetDefaultPrefValue(prefs::kHomePageIsNewTabPage);
278   ASSERT_TRUE(default_homepage_is_ntp_value != NULL);
279   bool default_homepage_is_ntp = false;
280   EXPECT_TRUE(
281       default_homepage_is_ntp_value->GetAsBoolean(&default_homepage_is_ntp));
282   EXPECT_TRUE(default_homepage_is_ntp);
283 }
284
285 INSTANTIATE_TEST_CASE_P(
286     FirstRunMasterPrefsWithTrackedPreferencesInstance,
287     FirstRunMasterPrefsWithTrackedPreferences,
288     testing::Values(
289         chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement,
290         chrome_prefs::internals::kSettingsEnforcementGroupEnforceOnload,
291         chrome_prefs::internals::kSettingsEnforcementGroupEnforceAlways));
292
293
294 #endif  // !defined(OS_CHROMEOS)