[Media Router] Add integration tests and e2e tests for media router and presentation...
[chromium-blink-merge.git] / chrome / browser / first_run / first_run_browsertest.cc
blob25897c129e90263da4e61ad59cc2a49adcecdea2
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.
5 #include <string>
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)
77 namespace {
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
82 // class.
83 class FirstRunMasterPrefsBrowserTestBase : public InProcessBrowserTest {
84 public:
85 FirstRunMasterPrefsBrowserTestBase() {}
87 protected:
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));
118 private:
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 {
128 public:
129 FirstRunMasterPrefsBrowserTestT() {}
131 protected:
132 void SetUp() override {
133 SetMasterPreferencesForTest(Text);
134 FirstRunMasterPrefsBrowserTestBase::SetUp();
137 private:
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());
152 run_loop.Run();
153 int source_profile_count = importer_list->count();
154 #if defined(OS_WIN)
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);
158 #endif
159 if (source_profile_count == 0)
160 return expected_import_state & ~first_run::AUTO_IMPORT_PROFILE_IMPORTED;
161 return expected_import_state;
164 } // namespace
166 extern const char kImportDefault[] =
167 "{\n"
168 "}\n";
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
174 #else
175 #define MAYBE_ImportDefault ImportDefault
176 #endif
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),
181 auto_import_state);
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[] =
187 "{\n"
188 " \"distribution\": {\n"
189 " \"import_bookmarks_from_file\": \"/foo/doesntexists.wtv\"\n"
190 " }\n"
191 "}\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
197 #else
198 #define MAYBE_ImportBookmarksFile ImportBookmarksFile
199 #endif
200 IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportBookmarksFile,
201 MAYBE_ImportBookmarksFile) {
202 int auto_import_state = first_run::auto_import_state();
203 EXPECT_EQ(
204 MaskExpectedImportState(first_run::AUTO_IMPORT_CALLED |
205 first_run::AUTO_IMPORT_PROFILE_IMPORTED |
206 first_run::AUTO_IMPORT_BOOKMARKS_FILE_IMPORTED),
207 auto_import_state);
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[] =
214 "{\n"
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"
220 " }\n"
221 "}\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
228 #else
229 #define MAYBE_ImportNothingAndShowNewTabPage ImportNothingAndShowNewTabPage
230 #endif
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[] =
241 "{\n"
242 " \"homepage\": \"example.com\",\n"
243 " \"homepage_is_newtabpage\": false\n"
244 "}\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> {
251 public:
252 FirstRunMasterPrefsWithTrackedPreferences() {}
254 protected:
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();
271 private:
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
279 #else
280 #define MAYBE_TrackedPreferencesSurviveFirstRun \
281 TrackedPreferencesSurviveFirstRun
282 #endif
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
291 // true.
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;
296 EXPECT_TRUE(
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,
304 testing::Values(
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)