Upstream version 7.35.139.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(base::WriteFile(prefs_file_, text_->c_str(), text_->size()));
93     first_run::SetMasterPrefsPathForTesting(prefs_file_);
94
95     // This invokes BrowserMain, and does the import, so must be done last.
96     InProcessBrowserTest::SetUp();
97   }
98
99   virtual void TearDown() OVERRIDE {
100     EXPECT_TRUE(base::DeleteFile(prefs_file_, false));
101     InProcessBrowserTest::TearDown();
102   }
103
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());
108
109     extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
110   }
111
112   void SetMasterPreferencesForTest(const char text[]) {
113     text_.reset(new std::string(text));
114   }
115
116  private:
117   base::FilePath prefs_file_;
118   scoped_ptr<std::string> text_;
119
120   DISALLOW_COPY_AND_ASSIGN(FirstRunMasterPrefsBrowserTestBase);
121 };
122
123 template<const char Text[]>
124 class FirstRunMasterPrefsBrowserTestT
125     : public FirstRunMasterPrefsBrowserTestBase {
126  public:
127   FirstRunMasterPrefsBrowserTestT() {}
128
129  protected:
130   virtual void SetUp() OVERRIDE {
131     SetMasterPreferencesForTest(Text);
132     FirstRunMasterPrefsBrowserTestBase::SetUp();
133   }
134
135  private:
136   DISALLOW_COPY_AND_ASSIGN(FirstRunMasterPrefsBrowserTestT);
137 };
138
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();
148 #if defined(OS_WIN)
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);
152 #endif
153   if (source_profile_count == 0)
154     return expected_import_state & ~first_run::AUTO_IMPORT_PROFILE_IMPORTED;
155
156   return expected_import_state;
157 }
158
159 }  // namespace
160
161 extern const char kImportDefault[] =
162     "{\n"
163     "}\n";
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
169 #else
170 #define MAYBE_ImportDefault ImportDefault
171 #endif
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),
176             auto_import_state);
177 }
178
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[] =
182     "{\n"
183     "  \"distribution\": {\n"
184     "     \"import_bookmarks_from_file\": \"/foo/doesntexists.wtv\"\n"
185     "  }\n"
186     "}\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
192 #else
193 #define MAYBE_ImportBookmarksFile ImportBookmarksFile
194 #endif
195 IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportBookmarksFile,
196                        MAYBE_ImportBookmarksFile) {
197   int auto_import_state = first_run::auto_import_state();
198   EXPECT_EQ(
199       MaskExpectedImportState(first_run::AUTO_IMPORT_CALLED |
200                               first_run::AUTO_IMPORT_PROFILE_IMPORTED |
201                               first_run::AUTO_IMPORT_BOOKMARKS_FILE_IMPORTED),
202       auto_import_state);
203 }
204
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[] =
209     "{\n"
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"
215     "  }\n"
216     "}\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
223 #else
224 #define MAYBE_ImportNothingAndShowNewTabPage ImportNothingAndShowNewTabPage
225 #endif
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());
234 }
235
236 // Test first run with some tracked preferences.
237 extern const char kWithTrackedPrefs[] =
238     "{\n"
239     "  \"homepage\": \"example.com\",\n"
240     "  \"homepage_is_newtabpage\": false\n"
241     "}\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> {
248  public:
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() + "/");
255   }
256 };
257
258 // http://crbug.com/314221
259 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_MACOSX) || defined(OS_LINUX))
260 #define MAYBE_TrackedPreferencesSurviveFirstRun \
261     DISABLED_TrackedPreferencesSurviveFirstRun
262 #else
263 #define MAYBE_TrackedPreferencesSurviveFirstRun \
264     TrackedPreferencesSurviveFirstRun
265 #endif
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));
271
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
274   // true.
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;
279   EXPECT_TRUE(
280       default_homepage_is_ntp_value->GetAsBoolean(&default_homepage_is_ntp));
281   EXPECT_TRUE(default_homepage_is_ntp);
282 }
283
284 INSTANTIATE_TEST_CASE_P(
285     FirstRunMasterPrefsWithTrackedPreferencesInstance,
286     FirstRunMasterPrefsWithTrackedPreferences,
287     testing::Values(
288         chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement,
289         chrome_prefs::internals::kSettingsEnforcementGroupEnforceOnload,
290         chrome_prefs::internals::kSettingsEnforcementGroupEnforceAlways,
291         chrome_prefs::internals::
292             kSettingsEnforcementGroupEnforceAlwaysWithExtensions));
293
294 #endif  // !defined(OS_CHROMEOS)