Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / startup / startup_browser_creator_browsertest.cc
blob1e1a055a550f0f221d0f53349bfb6dc77de50fcb
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 <algorithm>
6 #include <string>
8 #include "base/command_line.h"
9 #include "base/files/file_path.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/test/histogram_tester.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/extensions/extension_browsertest.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/extension_util.h"
17 #include "chrome/browser/extensions/launch_util.h"
18 #include "chrome/browser/first_run/first_run.h"
19 #include "chrome/browser/infobars/infobar_service.h"
20 #include "chrome/browser/prefs/session_startup_pref.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/profiles/profile_impl.h"
23 #include "chrome/browser/profiles/profile_manager.h"
24 #include "chrome/browser/sessions/session_restore.h"
25 #include "chrome/browser/signin/signin_promo.h"
26 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_commands.h"
28 #include "chrome/browser/ui/browser_finder.h"
29 #include "chrome/browser/ui/browser_iterator.h"
30 #include "chrome/browser/ui/browser_list.h"
31 #include "chrome/browser/ui/browser_list_observer.h"
32 #include "chrome/browser/ui/browser_window.h"
33 #include "chrome/browser/ui/host_desktop.h"
34 #include "chrome/browser/ui/startup/startup_browser_creator.h"
35 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
36 #include "chrome/browser/ui/tabs/tab_strip_model.h"
37 #include "chrome/common/chrome_switches.h"
38 #include "chrome/common/extensions/extension_constants.h"
39 #include "chrome/common/pref_names.h"
40 #include "chrome/common/url_constants.h"
41 #include "chrome/test/base/in_process_browser_test.h"
42 #include "chrome/test/base/test_switches.h"
43 #include "chrome/test/base/ui_test_utils.h"
44 #include "content/public/browser/web_contents.h"
45 #include "extensions/browser/extension_system.h"
46 #include "testing/gtest/include/gtest/gtest.h"
47 #include "url/gurl.h"
49 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
50 #include "base/callback.h"
51 #include "base/run_loop.h"
52 #include "base/values.h"
53 #include "components/policy/core/browser/browser_policy_connector.h"
54 #include "components/policy/core/common/external_data_fetcher.h"
55 #include "components/policy/core/common/mock_configuration_policy_provider.h"
56 #include "components/policy/core/common/policy_map.h"
57 #include "components/policy/core/common/policy_types.h"
58 #include "policy/policy_constants.h"
59 #include "testing/gmock/include/gmock/gmock.h"
61 using testing::_;
62 using testing::Return;
63 #endif // defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
65 #if defined(ENABLE_SUPERVISED_USERS)
66 #include "chrome/browser/supervised_user/supervised_user_navigation_observer.h"
67 #include "chrome/browser/supervised_user/supervised_user_service.h"
68 #include "chrome/browser/supervised_user/supervised_user_service_factory.h"
69 #endif
71 using extensions::Extension;
73 namespace {
75 // Check that there are two browsers. Find the one that is not |browser|.
76 Browser* FindOneOtherBrowser(Browser* browser) {
77 // There should only be one other browser.
78 EXPECT_EQ(2u, chrome::GetBrowserCount(browser->profile(),
79 browser->host_desktop_type()));
81 // Find the new browser.
82 Browser* other_browser = NULL;
83 for (chrome::BrowserIterator it; !it.done() && !other_browser; it.Next()) {
84 if (*it != browser)
85 other_browser = *it;
87 return other_browser;
90 } // namespace
92 class StartupBrowserCreatorTest : public ExtensionBrowserTest {
93 protected:
94 StartupBrowserCreatorTest() {}
96 bool SetUpUserDataDirectory() override {
97 return ExtensionBrowserTest::SetUpUserDataDirectory();
100 void SetUpCommandLine(base::CommandLine* command_line) override {
101 ExtensionBrowserTest::SetUpCommandLine(command_line);
102 command_line->AppendSwitch(switches::kEnablePanels);
103 command_line->AppendSwitchASCII(switches::kHomePage, url::kAboutBlankURL);
104 #if defined(OS_CHROMEOS)
105 // TODO(nkostylev): Investigate if we can remove this switch.
106 command_line->AppendSwitch(switches::kCreateBrowserOnStartupForTests);
107 #endif
110 // Helper functions return void so that we can ASSERT*().
111 // Use ASSERT_NO_FATAL_FAILURE around calls to these functions to stop the
112 // test if an assert fails.
113 void LoadApp(const std::string& app_name,
114 const Extension** out_app_extension) {
115 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(app_name.c_str())));
117 ExtensionService* service = extensions::ExtensionSystem::Get(
118 browser()->profile())->extension_service();
119 *out_app_extension = service->GetExtensionById(
120 last_loaded_extension_id(), false);
121 ASSERT_TRUE(*out_app_extension);
123 // Code that opens a new browser assumes we start with exactly one.
124 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
125 browser()->host_desktop_type()));
128 void SetAppLaunchPref(const std::string& app_id,
129 extensions::LaunchType launch_type) {
130 extensions::SetLaunchType(browser()->profile(), app_id, launch_type);
133 Browser* FindOneOtherBrowserForProfile(Profile* profile,
134 Browser* not_this_browser) {
135 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
136 if (*it != not_this_browser && it->profile() == profile)
137 return *it;
139 return NULL;
142 // A helper function that checks the session restore UI (infobar) is shown
143 // when Chrome starts up after crash.
144 void EnsureRestoreUIWasShown(content::WebContents* web_contents) {
145 #if defined(OS_MACOSX)
146 InfoBarService* infobar_service =
147 InfoBarService::FromWebContents(web_contents);
148 EXPECT_EQ(1U, infobar_service->infobar_count());
149 #endif // defined(OS_MACOSX)
152 private:
153 DISALLOW_COPY_AND_ASSIGN(StartupBrowserCreatorTest);
156 class OpenURLsPopupObserver : public chrome::BrowserListObserver {
157 public:
158 OpenURLsPopupObserver() : added_browser_(NULL) { }
160 void OnBrowserAdded(Browser* browser) override { added_browser_ = browser; }
162 void OnBrowserRemoved(Browser* browser) override {}
164 Browser* added_browser_;
167 // Test that when there is a popup as the active browser any requests to
168 // StartupBrowserCreatorImpl::OpenURLsInBrowser don't crash because there's no
169 // explicit profile given.
170 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenURLsPopup) {
171 std::vector<GURL> urls;
172 urls.push_back(GURL("http://localhost"));
174 // Note that in our testing we do not ever query the BrowserList for the "last
175 // active" browser. That's because the browsers are set as "active" by
176 // platform UI toolkit messages, and those messages are not sent during unit
177 // testing sessions.
179 OpenURLsPopupObserver observer;
180 BrowserList::AddObserver(&observer);
182 Browser* popup = new Browser(
183 Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile(),
184 browser()->host_desktop_type()));
185 ASSERT_TRUE(popup->is_type_popup());
186 ASSERT_EQ(popup, observer.added_browser_);
188 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
189 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
190 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
191 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
192 // This should create a new window, but re-use the profile from |popup|. If
193 // it used a NULL or invalid profile, it would crash.
194 launch.OpenURLsInBrowser(popup, false, urls, chrome::GetActiveDesktop());
195 ASSERT_NE(popup, observer.added_browser_);
196 BrowserList::RemoveObserver(&observer);
199 // We don't do non-process-startup browser launches on ChromeOS.
200 // Session restore for process-startup browser launches is tested
201 // in session_restore_uitest.
202 #if !defined(OS_CHROMEOS)
203 // Verify that startup URLs are honored when the process already exists but has
204 // no tabbed browser windows (eg. as if the process is running only due to a
205 // background application.
206 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
207 StartupURLsOnNewWindowWithNoTabbedBrowsers) {
208 // Use a couple same-site HTTP URLs.
209 ASSERT_TRUE(test_server()->Start());
210 std::vector<GURL> urls;
211 urls.push_back(test_server()->GetURL("files/title1.html"));
212 urls.push_back(test_server()->GetURL("files/title2.html"));
214 // Set the startup preference to open these URLs.
215 SessionStartupPref pref(SessionStartupPref::URLS);
216 pref.urls = urls;
217 SessionStartupPref::SetStartupPref(browser()->profile(), pref);
219 // Close the browser.
220 browser()->window()->Close();
222 // Do a simple non-process-startup browser launch.
223 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
224 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
225 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
226 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
227 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
228 browser()->host_desktop_type()));
230 // This should have created a new browser window. |browser()| is still
231 // around at this point, even though we've closed its window.
232 Browser* new_browser = FindOneOtherBrowser(browser());
233 ASSERT_TRUE(new_browser);
235 // The new browser should have one tab for each URL.
236 TabStripModel* tab_strip = new_browser->tab_strip_model();
237 ASSERT_EQ(static_cast<int>(urls.size()), tab_strip->count());
238 for (size_t i=0; i < urls.size(); i++) {
239 EXPECT_EQ(urls[i], tab_strip->GetWebContentsAt(i)->GetURL());
242 // The two tabs, despite having the same site, should be in different
243 // SiteInstances.
244 EXPECT_NE(tab_strip->GetWebContentsAt(0)->GetSiteInstance(),
245 tab_strip->GetWebContentsAt(1)->GetSiteInstance());
248 // Verify that startup URLs aren't used when the process already exists
249 // and has other tabbed browser windows. This is the common case of starting a
250 // new browser.
251 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
252 StartupURLsOnNewWindow) {
253 // Use a couple arbitrary URLs.
254 std::vector<GURL> urls;
255 urls.push_back(ui_test_utils::GetTestUrl(
256 base::FilePath(base::FilePath::kCurrentDirectory),
257 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
258 urls.push_back(ui_test_utils::GetTestUrl(
259 base::FilePath(base::FilePath::kCurrentDirectory),
260 base::FilePath(FILE_PATH_LITERAL("title2.html"))));
262 // Set the startup preference to open these URLs.
263 SessionStartupPref pref(SessionStartupPref::URLS);
264 pref.urls = urls;
265 SessionStartupPref::SetStartupPref(browser()->profile(), pref);
267 // Do a simple non-process-startup browser launch.
268 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
269 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
270 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
271 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
272 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
273 browser()->host_desktop_type()));
275 // This should have created a new browser window.
276 Browser* new_browser = FindOneOtherBrowser(browser());
277 ASSERT_TRUE(new_browser);
279 // The new browser should have exactly one tab (not the startup URLs).
280 ASSERT_EQ(1, new_browser->tab_strip_model()->count());
283 // App shortcuts are not implemented on mac os.
284 #if !defined(OS_MACOSX)
285 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutNoPref) {
286 // Load an app with launch.container = 'tab'.
287 const Extension* extension_app = NULL;
288 ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app));
290 // Add --app-id=<extension->id()> to the command line.
291 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
292 command_line.AppendSwitchASCII(switches::kAppId, extension_app->id());
294 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
295 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
296 StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run);
297 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
298 browser()->host_desktop_type()));
300 // No pref was set, so the app should have opened in a tab in a new window.
301 // The launch should have created a new browser.
302 Browser* new_browser = FindOneOtherBrowser(browser());
303 ASSERT_TRUE(new_browser);
305 // If new bookmark apps are enabled, it should be a standard tabbed window,
306 // not an app window; otherwise the reverse should be true.
307 bool new_bookmark_apps_enabled = extensions::util::IsNewBookmarkAppsEnabled();
308 EXPECT_EQ(!new_bookmark_apps_enabled, new_browser->is_app());
309 EXPECT_EQ(new_bookmark_apps_enabled, new_browser->is_type_tabbed());
312 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutWindowPref) {
313 const Extension* extension_app = NULL;
314 ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app));
316 // Set a pref indicating that the user wants to open this app in a window.
317 SetAppLaunchPref(extension_app->id(), extensions::LAUNCH_TYPE_WINDOW);
319 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
320 command_line.AppendSwitchASCII(switches::kAppId, extension_app->id());
321 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
322 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
323 StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run);
324 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
325 browser()->host_desktop_type()));
327 // Pref was set to open in a window, so the app should have opened in a
328 // window. The launch should have created a new browser. Find the new
329 // browser.
330 Browser* new_browser = FindOneOtherBrowser(browser());
331 ASSERT_TRUE(new_browser);
333 // Expect an app window.
334 EXPECT_TRUE(new_browser->is_app());
336 // The browser's app_name should include the app's ID.
337 EXPECT_NE(
338 new_browser->app_name_.find(extension_app->id()),
339 std::string::npos) << new_browser->app_name_;
342 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutTabPref) {
343 // Load an app with launch.container = 'tab'.
344 const Extension* extension_app = NULL;
345 ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app));
347 // Set a pref indicating that the user wants to open this app in a window.
348 SetAppLaunchPref(extension_app->id(), extensions::LAUNCH_TYPE_REGULAR);
350 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
351 command_line.AppendSwitchASCII(switches::kAppId, extension_app->id());
352 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
353 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
354 StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run);
355 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
356 browser()->host_desktop_type()));
358 // When an app shortcut is open and the pref indicates a tab should
359 // open, the tab is open in a new browser window. Expect a new window.
360 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
361 browser()->host_desktop_type()));
363 Browser* new_browser = FindOneOtherBrowser(browser());
364 ASSERT_TRUE(new_browser);
366 // The tab should be in a tabbed window.
367 EXPECT_TRUE(new_browser->is_type_tabbed());
369 // The browser's app_name should not include the app's ID: It is in a
370 // normal browser.
371 EXPECT_EQ(
372 new_browser->app_name_.find(extension_app->id()),
373 std::string::npos) << new_browser->app_name_;
376 #endif // !defined(OS_MACOSX)
378 #endif // !defined(OS_CHROMEOS)
380 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
381 ReadingWasRestartedAfterRestart) {
382 // Tests that StartupBrowserCreator::WasRestarted reads and resets the
383 // preference kWasRestarted correctly.
384 StartupBrowserCreator::was_restarted_read_ = false;
385 PrefService* pref_service = g_browser_process->local_state();
386 pref_service->SetBoolean(prefs::kWasRestarted, true);
387 EXPECT_TRUE(StartupBrowserCreator::WasRestarted());
388 EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted));
389 EXPECT_TRUE(StartupBrowserCreator::WasRestarted());
392 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
393 ReadingWasRestartedAfterNormalStart) {
394 // Tests that StartupBrowserCreator::WasRestarted reads and resets the
395 // preference kWasRestarted correctly.
396 StartupBrowserCreator::was_restarted_read_ = false;
397 PrefService* pref_service = g_browser_process->local_state();
398 pref_service->SetBoolean(prefs::kWasRestarted, false);
399 EXPECT_FALSE(StartupBrowserCreator::WasRestarted());
400 EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted));
401 EXPECT_FALSE(StartupBrowserCreator::WasRestarted());
404 // Fails on official builds. See http://crbug.com/313856
405 #if defined(GOOGLE_CHROME_BUILD)
406 #define MAYBE_AddFirstRunTab DISABLED_AddFirstRunTab
407 #else
408 #define MAYBE_AddFirstRunTab AddFirstRunTab
409 #endif
410 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, MAYBE_AddFirstRunTab) {
411 StartupBrowserCreator browser_creator;
412 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
413 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title2.html"));
415 // Do a simple non-process-startup browser launch.
416 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
417 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
418 chrome::startup::IS_FIRST_RUN);
419 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
420 browser()->host_desktop_type()));
422 // This should have created a new browser window.
423 Browser* new_browser = FindOneOtherBrowser(browser());
424 ASSERT_TRUE(new_browser);
426 TabStripModel* tab_strip = new_browser->tab_strip_model();
427 EXPECT_EQ(2, tab_strip->count());
429 EXPECT_EQ("title1.html",
430 tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
431 EXPECT_EQ("title2.html",
432 tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
435 // Test hard-coded special first run tabs (defined in
436 // StartupBrowserCreatorImpl::AddStartupURLs()).
437 // Fails on official builds. See http://crbug.com/313856
438 #if defined(GOOGLE_CHROME_BUILD)
439 #define MAYBE_AddCustomFirstRunTab DISABLED_AddCustomFirstRunTab
440 #else
441 #define MAYBE_AddCustomFirstRunTab AddCustomFirstRunTab
442 #endif
443 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, MAYBE_AddCustomFirstRunTab) {
444 StartupBrowserCreator browser_creator;
445 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
446 browser_creator.AddFirstRunTab(GURL("http://new_tab_page"));
447 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title2.html"));
448 browser_creator.AddFirstRunTab(GURL("http://welcome_page"));
450 // Do a simple non-process-startup browser launch.
451 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
452 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
453 chrome::startup::IS_FIRST_RUN);
454 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
455 browser()->host_desktop_type()));
457 // This should have created a new browser window.
458 Browser* new_browser = FindOneOtherBrowser(browser());
459 ASSERT_TRUE(new_browser);
461 TabStripModel* tab_strip = new_browser->tab_strip_model();
462 EXPECT_EQ(4, tab_strip->count());
464 EXPECT_EQ("title1.html",
465 tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
466 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
467 tab_strip->GetWebContentsAt(1)->GetURL());
468 EXPECT_EQ("title2.html",
469 tab_strip->GetWebContentsAt(2)->GetURL().ExtractFileName());
470 EXPECT_EQ(internals::GetWelcomePageURL(),
471 tab_strip->GetWebContentsAt(3)->GetURL());
474 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoNoWelcomePage) {
475 // Do a simple non-process-startup browser launch.
476 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
477 StartupBrowserCreatorImpl launch(base::FilePath(), dummy,
478 chrome::startup::IS_FIRST_RUN);
479 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
480 browser()->host_desktop_type()));
482 // This should have created a new browser window.
483 Browser* new_browser = FindOneOtherBrowser(browser());
484 ASSERT_TRUE(new_browser);
486 TabStripModel* tab_strip = new_browser->tab_strip_model();
487 EXPECT_EQ(1, tab_strip->count());
489 if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
490 EXPECT_EQ(signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
491 tab_strip->GetWebContentsAt(0)->GetURL());
492 } else {
493 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
494 tab_strip->GetWebContentsAt(0)->GetURL());
498 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoWithWelcomePage) {
499 first_run::SetShouldShowWelcomePage();
501 // Do a simple non-process-startup browser launch.
502 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
503 StartupBrowserCreatorImpl launch(base::FilePath(), dummy,
504 chrome::startup::IS_FIRST_RUN);
505 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
506 browser()->host_desktop_type()));
508 // This should have created a new browser window.
509 Browser* new_browser = FindOneOtherBrowser(browser());
510 ASSERT_TRUE(new_browser);
512 TabStripModel* tab_strip = new_browser->tab_strip_model();
513 EXPECT_EQ(2, tab_strip->count());
515 if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
516 EXPECT_EQ(signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
517 tab_strip->GetWebContentsAt(0)->GetURL());
518 } else {
519 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
520 tab_strip->GetWebContentsAt(0)->GetURL());
522 EXPECT_EQ(internals::GetWelcomePageURL(),
523 tab_strip->GetWebContentsAt(1)->GetURL());
526 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoWithFirstRunTabs) {
527 StartupBrowserCreator browser_creator;
528 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
530 // The welcome page should not be shown, even if
531 // first_run::ShouldShowWelcomePage() says so, when there are already
532 // more than 2 first run tabs.
533 first_run::SetShouldShowWelcomePage();
535 // Do a simple non-process-startup browser launch.
536 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
537 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
538 chrome::startup::IS_FIRST_RUN);
539 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
540 browser()->host_desktop_type()));
542 // This should have created a new browser window.
543 Browser* new_browser = FindOneOtherBrowser(browser());
544 ASSERT_TRUE(new_browser);
546 TabStripModel* tab_strip = new_browser->tab_strip_model();
547 if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
548 EXPECT_EQ(2, tab_strip->count());
549 EXPECT_EQ(signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
550 tab_strip->GetWebContentsAt(0)->GetURL());
551 EXPECT_EQ("title1.html",
552 tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
553 } else {
554 EXPECT_EQ(1, tab_strip->count());
555 EXPECT_EQ("title1.html",
556 tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
560 // The welcome page should still be shown if there are more than 2 first run
561 // tabs, but the welcome page was explcitly added to the first run tabs.
562 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
563 SyncPromoWithFirstRunTabsIncludingWelcomePage) {
564 StartupBrowserCreator browser_creator;
565 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
566 browser_creator.AddFirstRunTab(GURL("http://welcome_page"));
568 // Do a simple non-process-startup browser launch.
569 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
570 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
571 chrome::startup::IS_FIRST_RUN);
572 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
573 browser()->host_desktop_type()));
575 // This should have created a new browser window.
576 Browser* new_browser = FindOneOtherBrowser(browser());
577 ASSERT_TRUE(new_browser);
579 TabStripModel* tab_strip = new_browser->tab_strip_model();
580 if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
581 EXPECT_EQ(3, tab_strip->count());
582 EXPECT_EQ(signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
583 tab_strip->GetWebContentsAt(0)->GetURL());
584 EXPECT_EQ("title1.html",
585 tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
586 EXPECT_EQ(internals::GetWelcomePageURL(),
587 tab_strip->GetWebContentsAt(2)->GetURL());
588 } else {
589 EXPECT_EQ(2, tab_strip->count());
590 EXPECT_EQ("title1.html",
591 tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
592 EXPECT_EQ(internals::GetWelcomePageURL(),
593 tab_strip->GetWebContentsAt(1)->GetURL());
597 #if !defined(OS_CHROMEOS)
598 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, StartupURLsForTwoProfiles) {
599 #if defined(OS_WIN) && defined(USE_ASH)
600 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
601 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
602 switches::kAshBrowserTests))
603 return;
604 #endif
606 Profile* default_profile = browser()->profile();
608 ProfileManager* profile_manager = g_browser_process->profile_manager();
609 // Create another profile.
610 base::FilePath dest_path = profile_manager->user_data_dir();
611 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile 1"));
613 Profile* other_profile = profile_manager->GetProfile(dest_path);
614 ASSERT_TRUE(other_profile);
616 // Use a couple arbitrary URLs.
617 std::vector<GURL> urls1;
618 urls1.push_back(ui_test_utils::GetTestUrl(
619 base::FilePath(base::FilePath::kCurrentDirectory),
620 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
621 std::vector<GURL> urls2;
622 urls2.push_back(ui_test_utils::GetTestUrl(
623 base::FilePath(base::FilePath::kCurrentDirectory),
624 base::FilePath(FILE_PATH_LITERAL("title2.html"))));
626 // Set different startup preferences for the 2 profiles.
627 SessionStartupPref pref1(SessionStartupPref::URLS);
628 pref1.urls = urls1;
629 SessionStartupPref::SetStartupPref(default_profile, pref1);
630 SessionStartupPref pref2(SessionStartupPref::URLS);
631 pref2.urls = urls2;
632 SessionStartupPref::SetStartupPref(other_profile, pref2);
634 // Close the browser.
635 browser()->window()->Close();
637 // Do a simple non-process-startup browser launch.
638 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
640 StartupBrowserCreator browser_creator;
641 std::vector<Profile*> last_opened_profiles;
642 last_opened_profiles.push_back(default_profile);
643 last_opened_profiles.push_back(other_profile);
644 browser_creator.Start(dummy, profile_manager->user_data_dir(),
645 default_profile, last_opened_profiles);
647 // urls1 were opened in a browser for default_profile, and urls2 were opened
648 // in a browser for other_profile.
649 Browser* new_browser = NULL;
650 // |browser()| is still around at this point, even though we've closed its
651 // window. Thus the browser count for default_profile is 2.
652 ASSERT_EQ(2u, chrome::GetBrowserCount(default_profile,
653 browser()->host_desktop_type()));
654 new_browser = FindOneOtherBrowserForProfile(default_profile, browser());
655 ASSERT_TRUE(new_browser);
656 TabStripModel* tab_strip = new_browser->tab_strip_model();
657 ASSERT_EQ(1, tab_strip->count());
658 EXPECT_EQ(urls1[0], tab_strip->GetWebContentsAt(0)->GetURL());
660 ASSERT_EQ(1u, chrome::GetBrowserCount(other_profile,
661 browser()->host_desktop_type()));
662 new_browser = FindOneOtherBrowserForProfile(other_profile, NULL);
663 ASSERT_TRUE(new_browser);
664 tab_strip = new_browser->tab_strip_model();
665 ASSERT_EQ(1, tab_strip->count());
666 EXPECT_EQ(urls2[0], tab_strip->GetWebContentsAt(0)->GetURL());
669 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, PRE_UpdateWithTwoProfiles) {
670 // Simulate a browser restart by creating the profiles in the PRE_ part.
671 ProfileManager* profile_manager = g_browser_process->profile_manager();
673 ASSERT_TRUE(test_server()->Start());
675 // Create two profiles.
676 base::FilePath dest_path = profile_manager->user_data_dir();
678 Profile* profile1 = profile_manager->GetProfile(
679 dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
680 ASSERT_TRUE(profile1);
682 Profile* profile2 = profile_manager->GetProfile(
683 dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
684 ASSERT_TRUE(profile2);
686 // Open some urls with the browsers, and close them.
687 Browser* browser1 = new Browser(
688 Browser::CreateParams(Browser::TYPE_TABBED, profile1,
689 browser()->host_desktop_type()));
690 chrome::NewTab(browser1);
691 ui_test_utils::NavigateToURL(browser1,
692 test_server()->GetURL("files/empty.html"));
693 browser1->window()->Close();
695 Browser* browser2 = new Browser(
696 Browser::CreateParams(Browser::TYPE_TABBED, profile2,
697 browser()->host_desktop_type()));
698 chrome::NewTab(browser2);
699 ui_test_utils::NavigateToURL(browser2,
700 test_server()->GetURL("files/form.html"));
701 browser2->window()->Close();
703 // Set different startup preferences for the 2 profiles.
704 std::vector<GURL> urls1;
705 urls1.push_back(ui_test_utils::GetTestUrl(
706 base::FilePath(base::FilePath::kCurrentDirectory),
707 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
708 std::vector<GURL> urls2;
709 urls2.push_back(ui_test_utils::GetTestUrl(
710 base::FilePath(base::FilePath::kCurrentDirectory),
711 base::FilePath(FILE_PATH_LITERAL("title2.html"))));
713 // Set different startup preferences for the 2 profiles.
714 SessionStartupPref pref1(SessionStartupPref::URLS);
715 pref1.urls = urls1;
716 SessionStartupPref::SetStartupPref(profile1, pref1);
717 SessionStartupPref pref2(SessionStartupPref::URLS);
718 pref2.urls = urls2;
719 SessionStartupPref::SetStartupPref(profile2, pref2);
721 profile1->GetPrefs()->CommitPendingWrite();
722 profile2->GetPrefs()->CommitPendingWrite();
725 // See crbug.com/376184 about improvements to this test on Mac.
726 // Disabled because it's flaky. http://crbug.com/379579
727 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
728 DISABLED_UpdateWithTwoProfiles) {
729 #if defined(OS_WIN) && defined(USE_ASH)
730 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
731 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
732 switches::kAshBrowserTests))
733 return;
734 #endif
736 // Make StartupBrowserCreator::WasRestarted() return true.
737 StartupBrowserCreator::was_restarted_read_ = false;
738 PrefService* pref_service = g_browser_process->local_state();
739 pref_service->SetBoolean(prefs::kWasRestarted, true);
741 ProfileManager* profile_manager = g_browser_process->profile_manager();
743 // Open the two profiles.
744 base::FilePath dest_path = profile_manager->user_data_dir();
746 Profile* profile1 = profile_manager->GetProfile(
747 dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
748 ASSERT_TRUE(profile1);
750 Profile* profile2 = profile_manager->GetProfile(
751 dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
752 ASSERT_TRUE(profile2);
754 // Simulate a launch after a browser update.
755 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
756 StartupBrowserCreator browser_creator;
757 std::vector<Profile*> last_opened_profiles;
758 last_opened_profiles.push_back(profile1);
759 last_opened_profiles.push_back(profile2);
760 browser_creator.Start(dummy, profile_manager->user_data_dir(), profile1,
761 last_opened_profiles);
763 while (SessionRestore::IsRestoring(profile1) ||
764 SessionRestore::IsRestoring(profile2))
765 base::MessageLoop::current()->RunUntilIdle();
767 // The startup URLs are ignored, and instead the last open sessions are
768 // restored.
769 EXPECT_TRUE(profile1->restored_last_session());
770 EXPECT_TRUE(profile2->restored_last_session());
772 Browser* new_browser = NULL;
773 ASSERT_EQ(1u, chrome::GetBrowserCount(profile1,
774 browser()->host_desktop_type()));
775 new_browser = FindOneOtherBrowserForProfile(profile1, NULL);
776 ASSERT_TRUE(new_browser);
777 TabStripModel* tab_strip = new_browser->tab_strip_model();
778 ASSERT_EQ(1, tab_strip->count());
779 EXPECT_EQ("/files/empty.html",
780 tab_strip->GetWebContentsAt(0)->GetURL().path());
782 ASSERT_EQ(1u, chrome::GetBrowserCount(profile2,
783 browser()->host_desktop_type()));
784 new_browser = FindOneOtherBrowserForProfile(profile2, NULL);
785 ASSERT_TRUE(new_browser);
786 tab_strip = new_browser->tab_strip_model();
787 ASSERT_EQ(1, tab_strip->count());
788 EXPECT_EQ("/files/form.html",
789 tab_strip->GetWebContentsAt(0)->GetURL().path());
792 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
793 ProfilesWithoutPagesNotLaunched) {
794 #if defined(OS_WIN) && defined(USE_ASH)
795 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
796 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
797 switches::kAshBrowserTests))
798 return;
799 #endif
801 Profile* default_profile = browser()->profile();
803 ProfileManager* profile_manager = g_browser_process->profile_manager();
805 // Create 4 more profiles.
806 base::FilePath dest_path1 = profile_manager->user_data_dir().Append(
807 FILE_PATH_LITERAL("New Profile 1"));
808 base::FilePath dest_path2 = profile_manager->user_data_dir().Append(
809 FILE_PATH_LITERAL("New Profile 2"));
810 base::FilePath dest_path3 = profile_manager->user_data_dir().Append(
811 FILE_PATH_LITERAL("New Profile 3"));
812 base::FilePath dest_path4 = profile_manager->user_data_dir().Append(
813 FILE_PATH_LITERAL("New Profile 4"));
815 Profile* profile_home1 = profile_manager->GetProfile(dest_path1);
816 ASSERT_TRUE(profile_home1);
817 Profile* profile_home2 = profile_manager->GetProfile(dest_path2);
818 ASSERT_TRUE(profile_home2);
819 Profile* profile_last = profile_manager->GetProfile(dest_path3);
820 ASSERT_TRUE(profile_last);
821 Profile* profile_urls = profile_manager->GetProfile(dest_path4);
822 ASSERT_TRUE(profile_urls);
824 // Set the profiles to open urls, open last visited pages or display the home
825 // page.
826 SessionStartupPref pref_home(SessionStartupPref::DEFAULT);
827 SessionStartupPref::SetStartupPref(profile_home1, pref_home);
828 SessionStartupPref::SetStartupPref(profile_home2, pref_home);
830 SessionStartupPref pref_last(SessionStartupPref::LAST);
831 SessionStartupPref::SetStartupPref(profile_last, pref_last);
833 std::vector<GURL> urls;
834 urls.push_back(ui_test_utils::GetTestUrl(
835 base::FilePath(base::FilePath::kCurrentDirectory),
836 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
838 SessionStartupPref pref_urls(SessionStartupPref::URLS);
839 pref_urls.urls = urls;
840 SessionStartupPref::SetStartupPref(profile_urls, pref_urls);
842 // Open a page with profile_last.
843 Browser* browser_last = new Browser(
844 Browser::CreateParams(Browser::TYPE_TABBED, profile_last,
845 browser()->host_desktop_type()));
846 chrome::NewTab(browser_last);
847 ui_test_utils::NavigateToURL(browser_last,
848 test_server()->GetURL("files/empty.html"));
849 browser_last->window()->Close();
851 // Close the main browser.
852 chrome::HostDesktopType original_desktop_type =
853 browser()->host_desktop_type();
854 browser()->window()->Close();
856 // Do a simple non-process-startup browser launch.
857 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
859 StartupBrowserCreator browser_creator;
860 std::vector<Profile*> last_opened_profiles;
861 last_opened_profiles.push_back(profile_home1);
862 last_opened_profiles.push_back(profile_home2);
863 last_opened_profiles.push_back(profile_last);
864 last_opened_profiles.push_back(profile_urls);
865 browser_creator.Start(dummy, profile_manager->user_data_dir(), profile_home1,
866 last_opened_profiles);
868 while (SessionRestore::IsRestoring(default_profile) ||
869 SessionRestore::IsRestoring(profile_home1) ||
870 SessionRestore::IsRestoring(profile_home2) ||
871 SessionRestore::IsRestoring(profile_last) ||
872 SessionRestore::IsRestoring(profile_urls))
873 base::MessageLoop::current()->RunUntilIdle();
875 Browser* new_browser = NULL;
876 // The last open profile (the profile_home1 in this case) will always be
877 // launched, even if it will open just the home page.
878 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home1, original_desktop_type));
879 new_browser = FindOneOtherBrowserForProfile(profile_home1, NULL);
880 ASSERT_TRUE(new_browser);
881 TabStripModel* tab_strip = new_browser->tab_strip_model();
882 ASSERT_EQ(1, tab_strip->count());
883 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
884 tab_strip->GetWebContentsAt(0)->GetURL());
886 // profile_urls opened the urls.
887 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls, original_desktop_type));
888 new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL);
889 ASSERT_TRUE(new_browser);
890 tab_strip = new_browser->tab_strip_model();
891 ASSERT_EQ(1, tab_strip->count());
892 EXPECT_EQ(urls[0], tab_strip->GetWebContentsAt(0)->GetURL());
894 // profile_last opened the last open pages.
895 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last, original_desktop_type));
896 new_browser = FindOneOtherBrowserForProfile(profile_last, NULL);
897 ASSERT_TRUE(new_browser);
898 tab_strip = new_browser->tab_strip_model();
899 ASSERT_EQ(1, tab_strip->count());
900 EXPECT_EQ("/files/empty.html",
901 tab_strip->GetWebContentsAt(0)->GetURL().path());
903 // profile_home2 was not launched since it would've only opened the home page.
904 ASSERT_EQ(0u, chrome::GetBrowserCount(profile_home2, original_desktop_type));
907 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, ProfilesLaunchedAfterCrash) {
908 #if defined(OS_WIN) && defined(USE_ASH)
909 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
910 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
911 switches::kAshBrowserTests))
912 return;
913 #endif
915 // After an unclean exit, all profiles will be launched. However, they won't
916 // open any pages automatically.
918 ProfileManager* profile_manager = g_browser_process->profile_manager();
920 // Create 3 profiles.
921 base::FilePath dest_path1 = profile_manager->user_data_dir().Append(
922 FILE_PATH_LITERAL("New Profile 1"));
923 base::FilePath dest_path2 = profile_manager->user_data_dir().Append(
924 FILE_PATH_LITERAL("New Profile 2"));
925 base::FilePath dest_path3 = profile_manager->user_data_dir().Append(
926 FILE_PATH_LITERAL("New Profile 3"));
928 Profile* profile_home = profile_manager->GetProfile(dest_path1);
929 ASSERT_TRUE(profile_home);
930 Profile* profile_last = profile_manager->GetProfile(dest_path2);
931 ASSERT_TRUE(profile_last);
932 Profile* profile_urls = profile_manager->GetProfile(dest_path3);
933 ASSERT_TRUE(profile_urls);
935 // Set the profiles to open the home page, last visited pages or URLs.
936 SessionStartupPref pref_home(SessionStartupPref::DEFAULT);
937 SessionStartupPref::SetStartupPref(profile_home, pref_home);
939 SessionStartupPref pref_last(SessionStartupPref::LAST);
940 SessionStartupPref::SetStartupPref(profile_last, pref_last);
942 std::vector<GURL> urls;
943 urls.push_back(ui_test_utils::GetTestUrl(
944 base::FilePath(base::FilePath::kCurrentDirectory),
945 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
947 SessionStartupPref pref_urls(SessionStartupPref::URLS);
948 pref_urls.urls = urls;
949 SessionStartupPref::SetStartupPref(profile_urls, pref_urls);
951 // Simulate a launch after an unclear exit.
952 browser()->window()->Close();
953 static_cast<ProfileImpl*>(profile_home)->last_session_exit_type_ =
954 Profile::EXIT_CRASHED;
955 static_cast<ProfileImpl*>(profile_last)->last_session_exit_type_ =
956 Profile::EXIT_CRASHED;
957 static_cast<ProfileImpl*>(profile_urls)->last_session_exit_type_ =
958 Profile::EXIT_CRASHED;
960 #if !defined(OS_MACOSX) && !defined(GOOGLE_CHROME_BUILD)
961 // Use HistogramTester to make sure a bubble is shown when it's not on
962 // platform Mac OS X and it's not official Chrome build.
964 // On Mac OS X, an infobar is shown to restore the previous session, which
965 // is tested by function EnsureRestoreUIWasShown.
967 // Under a Google Chrome build, it is not tested because a task is posted to
968 // the file thread before the bubble is shown. It is difficult to make sure
969 // that the histogram check runs after all threads have finished their tasks.
970 base::HistogramTester histogram_tester;
971 #endif // !defined(OS_MACOSX) && !defined(GOOGLE_CHROME_BUILD)
973 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
974 dummy.AppendSwitchASCII(switches::kTestType, "browser");
975 StartupBrowserCreator browser_creator;
976 std::vector<Profile*> last_opened_profiles;
977 last_opened_profiles.push_back(profile_home);
978 last_opened_profiles.push_back(profile_last);
979 last_opened_profiles.push_back(profile_urls);
980 browser_creator.Start(dummy, profile_manager->user_data_dir(), profile_home,
981 last_opened_profiles);
983 // No profiles are getting restored, since they all display the crash info
984 // bar.
985 EXPECT_FALSE(SessionRestore::IsRestoring(profile_home));
986 EXPECT_FALSE(SessionRestore::IsRestoring(profile_last));
987 EXPECT_FALSE(SessionRestore::IsRestoring(profile_urls));
989 // The profile which normally opens the home page displays the new tab page.
990 Browser* new_browser = NULL;
991 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home,
992 browser()->host_desktop_type()));
993 new_browser = FindOneOtherBrowserForProfile(profile_home, NULL);
994 ASSERT_TRUE(new_browser);
995 TabStripModel* tab_strip = new_browser->tab_strip_model();
996 ASSERT_EQ(1, tab_strip->count());
997 content::WebContents* web_contents = tab_strip->GetWebContentsAt(0);
998 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
999 EnsureRestoreUIWasShown(web_contents);
1001 // The profile which normally opens last open pages displays the new tab page.
1002 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last,
1003 browser()->host_desktop_type()));
1004 new_browser = FindOneOtherBrowserForProfile(profile_last, NULL);
1005 ASSERT_TRUE(new_browser);
1006 tab_strip = new_browser->tab_strip_model();
1007 ASSERT_EQ(1, tab_strip->count());
1008 web_contents = tab_strip->GetWebContentsAt(0);
1009 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
1010 EnsureRestoreUIWasShown(web_contents);
1012 // The profile which normally opens URLs displays the new tab page.
1013 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls,
1014 browser()->host_desktop_type()));
1015 new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL);
1016 ASSERT_TRUE(new_browser);
1017 tab_strip = new_browser->tab_strip_model();
1018 ASSERT_EQ(1, tab_strip->count());
1019 web_contents = tab_strip->GetWebContentsAt(0);
1020 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
1021 EnsureRestoreUIWasShown(web_contents);
1023 #if !defined(OS_MACOSX) && !defined(GOOGLE_CHROME_BUILD)
1024 // Each profile should have one session restore bubble shown, so we should
1025 // observe count 3 in bucket 0 (which represents bubble shown).
1026 histogram_tester.ExpectBucketCount("SessionCrashed.Bubble", 0, 3);
1027 #endif // !defined(OS_MACOSX) && !defined(GOOGLE_CHROME_BUILD)
1030 class SupervisedUserBrowserCreatorTest : public InProcessBrowserTest {
1031 protected:
1032 void SetUpCommandLine(base::CommandLine* command_line) override {
1033 InProcessBrowserTest::SetUpCommandLine(command_line);
1034 command_line->AppendSwitchASCII(switches::kSupervisedUserId, "asdf");
1038 IN_PROC_BROWSER_TEST_F(SupervisedUserBrowserCreatorTest,
1039 StartupSupervisedUserProfile) {
1040 StartupBrowserCreator browser_creator;
1042 // Do a simple non-process-startup browser launch.
1043 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
1044 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1045 chrome::startup::IS_FIRST_RUN);
1046 content::WindowedNotificationObserver observer(
1047 content::NOTIFICATION_LOAD_STOP,
1048 content::NotificationService::AllSources());
1049 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
1050 browser()->host_desktop_type()));
1052 // This should have created a new browser window.
1053 Browser* new_browser = FindOneOtherBrowser(browser());
1054 ASSERT_TRUE(new_browser);
1056 TabStripModel* tab_strip = new_browser->tab_strip_model();
1057 // There should be only one tab.
1058 EXPECT_EQ(1, tab_strip->count());
1061 #endif // !defined(OS_CHROMEOS)
1063 // These tests are not applicable to Chrome OS as neither master_preferences nor
1064 // the sync promo exist there.
1065 #if !defined(OS_CHROMEOS)
1067 // On a branded Linux build, policy is required to suppress the first-run
1068 // dialog.
1069 #if !defined(OS_LINUX) || !defined(GOOGLE_CHROME_BUILD) || \
1070 defined(ENABLE_CONFIGURATION_POLICY)
1072 class StartupBrowserCreatorFirstRunTest : public InProcessBrowserTest {
1073 protected:
1074 void SetUpCommandLine(base::CommandLine* command_line) override;
1075 void SetUpInProcessBrowserTestFixture() override;
1077 #if defined(ENABLE_CONFIGURATION_POLICY)
1078 policy::MockConfigurationPolicyProvider provider_;
1079 policy::PolicyMap policy_map_;
1080 #endif // defined(ENABLE_CONFIGURATION_POLICY)
1083 void StartupBrowserCreatorFirstRunTest::SetUpCommandLine(
1084 base::CommandLine* command_line) {
1085 command_line->AppendSwitch(switches::kForceFirstRun);
1088 void StartupBrowserCreatorFirstRunTest::SetUpInProcessBrowserTestFixture() {
1089 #if defined(ENABLE_CONFIGURATION_POLICY)
1090 #if defined(OS_LINUX) && defined(GOOGLE_CHROME_BUILD)
1091 // Set a policy that prevents the first-run dialog from being shown.
1092 policy_map_.Set(policy::key::kMetricsReportingEnabled,
1093 policy::POLICY_LEVEL_MANDATORY,
1094 policy::POLICY_SCOPE_USER,
1095 new base::FundamentalValue(false),
1096 NULL);
1097 provider_.UpdateChromePolicy(policy_map_);
1098 #endif // defined(OS_LINUX) && defined(GOOGLE_CHROME_BUILD)
1100 EXPECT_CALL(provider_, IsInitializationComplete(_))
1101 .WillRepeatedly(Return(true));
1102 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
1103 #endif // defined(ENABLE_CONFIGURATION_POLICY)
1106 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1107 // http://crbug.com/314819
1108 #define MAYBE_SyncPromoForbidden DISABLED_SyncPromoForbidden
1109 #else
1110 #define MAYBE_SyncPromoForbidden SyncPromoForbidden
1111 #endif
1112 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1113 MAYBE_SyncPromoForbidden) {
1114 // Consistently enable the welcome page on all platforms.
1115 first_run::SetShouldShowWelcomePage();
1117 // Simulate the following master_preferences:
1118 // {
1119 // "sync_promo": {
1120 // "show_on_first_run_allowed": false
1121 // }
1122 // }
1123 StartupBrowserCreator browser_creator;
1124 browser()->profile()->GetPrefs()->SetBoolean(
1125 prefs::kSignInPromoShowOnFirstRunAllowed, false);
1127 // Do a process-startup browser launch.
1128 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
1129 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1130 chrome::startup::IS_FIRST_RUN);
1131 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1132 browser()->host_desktop_type()));
1134 // This should have created a new browser window.
1135 Browser* new_browser = FindOneOtherBrowser(browser());
1136 ASSERT_TRUE(new_browser);
1138 // Verify that the NTP and the welcome page are shown.
1139 TabStripModel* tab_strip = new_browser->tab_strip_model();
1140 ASSERT_EQ(2, tab_strip->count());
1141 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
1142 tab_strip->GetWebContentsAt(0)->GetURL());
1143 EXPECT_EQ(internals::GetWelcomePageURL(),
1144 tab_strip->GetWebContentsAt(1)->GetURL());
1147 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1148 // http://crbug.com/314819
1149 #define MAYBE_SyncPromoAllowed DISABLED_SyncPromoAllowed
1150 #else
1151 #define MAYBE_SyncPromoAllowed SyncPromoAllowed
1152 #endif
1153 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1154 MAYBE_SyncPromoAllowed) {
1155 // Consistently enable the welcome page on all platforms.
1156 first_run::SetShouldShowWelcomePage();
1158 // Simulate the following master_preferences:
1159 // {
1160 // "sync_promo": {
1161 // "show_on_first_run_allowed": true
1162 // }
1163 // }
1164 StartupBrowserCreator browser_creator;
1165 browser()->profile()->GetPrefs()->SetBoolean(
1166 prefs::kSignInPromoShowOnFirstRunAllowed, true);
1168 // Do a process-startup browser launch.
1169 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
1170 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1171 chrome::startup::IS_FIRST_RUN);
1172 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1173 browser()->host_desktop_type()));
1175 // This should have created a new browser window.
1176 Browser* new_browser = FindOneOtherBrowser(browser());
1177 ASSERT_TRUE(new_browser);
1179 // Verify that the sync promo and the welcome page are shown.
1180 TabStripModel* tab_strip = new_browser->tab_strip_model();
1181 ASSERT_EQ(2, tab_strip->count());
1182 EXPECT_EQ(signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
1183 tab_strip->GetWebContentsAt(0)->GetURL());
1184 EXPECT_EQ(internals::GetWelcomePageURL(),
1185 tab_strip->GetWebContentsAt(1)->GetURL());
1188 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1189 // http://crbug.com/314819
1190 #define MAYBE_FirstRunTabsPromoAllowed DISABLED_FirstRunTabsPromoAllowed
1191 #else
1192 #define MAYBE_FirstRunTabsPromoAllowed FirstRunTabsPromoAllowed
1193 #endif
1194 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1195 MAYBE_FirstRunTabsPromoAllowed) {
1196 // Simulate the following master_preferences:
1197 // {
1198 // "first_run_tabs" : [
1199 // "files/title1.html"
1200 // ],
1201 // "sync_promo": {
1202 // "show_on_first_run_allowed": true
1203 // }
1204 // }
1205 StartupBrowserCreator browser_creator;
1206 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1207 browser()->profile()->GetPrefs()->SetBoolean(
1208 prefs::kSignInPromoShowOnFirstRunAllowed, true);
1210 // Do a process-startup browser launch.
1211 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
1212 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1213 chrome::startup::IS_FIRST_RUN);
1214 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1215 browser()->host_desktop_type()));
1217 // This should have created a new browser window.
1218 Browser* new_browser = FindOneOtherBrowser(browser());
1219 ASSERT_TRUE(new_browser);
1221 // Verify that the first-run tab is shown and the sync promo has been added.
1222 TabStripModel* tab_strip = new_browser->tab_strip_model();
1223 ASSERT_EQ(2, tab_strip->count());
1224 EXPECT_EQ(signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
1225 tab_strip->GetWebContentsAt(0)->GetURL());
1226 EXPECT_EQ("title1.html",
1227 tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
1230 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1231 // http://crbug.com/314819
1232 #define MAYBE_FirstRunTabsContainSyncPromo \
1233 DISABLED_FirstRunTabsContainSyncPromo
1234 #else
1235 #define MAYBE_FirstRunTabsContainSyncPromo FirstRunTabsContainSyncPromo
1236 #endif
1237 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1238 MAYBE_FirstRunTabsContainSyncPromo) {
1239 // Simulate the following master_preferences:
1240 // {
1241 // "first_run_tabs" : [
1242 // "files/title1.html",
1243 // "chrome://signin/?source=0&next_page=chrome%3A%2F%2Fnewtab%2F"
1244 // ],
1245 // "sync_promo": {
1246 // "show_on_first_run_allowed": true
1247 // }
1248 // }
1249 ASSERT_TRUE(test_server()->Start());
1250 StartupBrowserCreator browser_creator;
1251 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1252 browser_creator.AddFirstRunTab(
1253 signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false));
1254 browser()->profile()->GetPrefs()->SetBoolean(
1255 prefs::kSignInPromoShowOnFirstRunAllowed, true);
1257 // Do a process-startup browser launch.
1258 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
1259 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1260 chrome::startup::IS_FIRST_RUN);
1261 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1262 browser()->host_desktop_type()));
1264 // This should have created a new browser window.
1265 Browser* new_browser = FindOneOtherBrowser(browser());
1266 ASSERT_TRUE(new_browser);
1268 // Verify that the first-run tabs are shown and no sync promo has been added
1269 // as the first-run tabs contain it already.
1270 TabStripModel* tab_strip = new_browser->tab_strip_model();
1271 ASSERT_EQ(2, tab_strip->count());
1272 EXPECT_EQ("title1.html",
1273 tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
1274 EXPECT_EQ(signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
1275 tab_strip->GetWebContentsAt(1)->GetURL());
1278 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1279 // http://crbug.com/314819
1280 #define MAYBE_FirstRunTabsContainNTPSyncPromoAllowed \
1281 DISABLED_FirstRunTabsContainNTPSyncPromoAllowed
1282 #else
1283 #define MAYBE_FirstRunTabsContainNTPSyncPromoAllowed \
1284 FirstRunTabsContainNTPSyncPromoAllowed
1285 #endif
1286 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1287 MAYBE_FirstRunTabsContainNTPSyncPromoAllowed) {
1288 // Simulate the following master_preferences:
1289 // {
1290 // "first_run_tabs" : [
1291 // "new_tab_page",
1292 // "files/title1.html"
1293 // ],
1294 // "sync_promo": {
1295 // "show_on_first_run_allowed": true
1296 // }
1297 // }
1298 StartupBrowserCreator browser_creator;
1299 browser_creator.AddFirstRunTab(GURL("http://new_tab_page"));
1300 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1301 browser()->profile()->GetPrefs()->SetBoolean(
1302 prefs::kSignInPromoShowOnFirstRunAllowed, true);
1304 // Do a process-startup browser launch.
1305 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
1306 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1307 chrome::startup::IS_FIRST_RUN);
1308 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1309 browser()->host_desktop_type()));
1311 // This should have created a new browser window.
1312 Browser* new_browser = FindOneOtherBrowser(browser());
1313 ASSERT_TRUE(new_browser);
1315 // Verify that the first-run tabs are shown but the NTP that they contain has
1316 // been replaced by the sync promo.
1317 TabStripModel* tab_strip = new_browser->tab_strip_model();
1318 ASSERT_EQ(2, tab_strip->count());
1319 EXPECT_EQ(signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
1320 tab_strip->GetWebContentsAt(0)->GetURL());
1321 EXPECT_EQ("title1.html",
1322 tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
1325 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1326 // http://crbug.com/314819
1327 #define MAYBE_FirstRunTabsContainNTPSyncPromoForbidden \
1328 DISABLED_FirstRunTabsContainNTPSyncPromoForbidden
1329 #else
1330 #define MAYBE_FirstRunTabsContainNTPSyncPromoForbidden \
1331 FirstRunTabsContainNTPSyncPromoForbidden
1332 #endif
1333 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1334 MAYBE_FirstRunTabsContainNTPSyncPromoForbidden) {
1335 // Simulate the following master_preferences:
1336 // {
1337 // "first_run_tabs" : [
1338 // "new_tab_page",
1339 // "files/title1.html"
1340 // ],
1341 // "sync_promo": {
1342 // "show_on_first_run_allowed": false
1343 // }
1344 // }
1345 StartupBrowserCreator browser_creator;
1346 browser_creator.AddFirstRunTab(GURL("http://new_tab_page"));
1347 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1348 browser()->profile()->GetPrefs()->SetBoolean(
1349 prefs::kSignInPromoShowOnFirstRunAllowed, false);
1351 // Do a process-startup browser launch.
1352 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
1353 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1354 chrome::startup::IS_FIRST_RUN);
1355 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1356 browser()->host_desktop_type()));
1358 // This should have created a new browser window.
1359 Browser* new_browser = FindOneOtherBrowser(browser());
1360 ASSERT_TRUE(new_browser);
1362 // Verify that the first-run tabs are shown, the NTP that they contain has not
1363 // not been replaced by the sync promo and no sync promo has been added.
1364 TabStripModel* tab_strip = new_browser->tab_strip_model();
1365 ASSERT_EQ(2, tab_strip->count());
1366 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
1367 tab_strip->GetWebContentsAt(0)->GetURL());
1368 EXPECT_EQ("title1.html",
1369 tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
1372 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1373 // http://crbug.com/314819
1374 #define MAYBE_FirstRunTabsSyncPromoForbidden \
1375 DISABLED_FirstRunTabsSyncPromoForbidden
1376 #else
1377 #define MAYBE_FirstRunTabsSyncPromoForbidden FirstRunTabsSyncPromoForbidden
1378 #endif
1379 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1380 MAYBE_FirstRunTabsSyncPromoForbidden) {
1381 // Simulate the following master_preferences:
1382 // {
1383 // "first_run_tabs" : [
1384 // "files/title1.html"
1385 // ],
1386 // "sync_promo": {
1387 // "show_on_first_run_allowed": false
1388 // }
1389 // }
1390 StartupBrowserCreator browser_creator;
1391 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1392 browser()->profile()->GetPrefs()->SetBoolean(
1393 prefs::kSignInPromoShowOnFirstRunAllowed, false);
1395 // Do a process-startup browser launch.
1396 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
1397 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1398 chrome::startup::IS_FIRST_RUN);
1399 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1400 browser()->host_desktop_type()));
1402 // This should have created a new browser window.
1403 Browser* new_browser = FindOneOtherBrowser(browser());
1404 ASSERT_TRUE(new_browser);
1406 // Verify that the first-run tab is shown and no sync promo has been added.
1407 TabStripModel* tab_strip = new_browser->tab_strip_model();
1408 ASSERT_EQ(1, tab_strip->count());
1409 EXPECT_EQ("title1.html",
1410 tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
1413 #if defined(ENABLE_CONFIGURATION_POLICY)
1414 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1415 // http://crbug.com/314819
1416 #define MAYBE_RestoreOnStartupURLsPolicySpecified \
1417 DISABLED_RestoreOnStartupURLsPolicySpecified
1418 #else
1419 #define MAYBE_RestoreOnStartupURLsPolicySpecified \
1420 RestoreOnStartupURLsPolicySpecified
1421 #endif
1422 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1423 MAYBE_RestoreOnStartupURLsPolicySpecified) {
1424 // Simulate the following master_preferences:
1425 // {
1426 // "sync_promo": {
1427 // "show_on_first_run_allowed": true
1428 // }
1429 // }
1430 StartupBrowserCreator browser_creator;
1431 browser()->profile()->GetPrefs()->SetBoolean(
1432 prefs::kSignInPromoShowOnFirstRunAllowed, true);
1434 // Set the following user policies:
1435 // * RestoreOnStartup = RestoreOnStartupIsURLs
1436 // * RestoreOnStartupURLs = [ "files/title1.html" ]
1437 policy_map_.Set(
1438 policy::key::kRestoreOnStartup,
1439 policy::POLICY_LEVEL_MANDATORY,
1440 policy::POLICY_SCOPE_USER,
1441 new base::FundamentalValue(SessionStartupPref::kPrefValueURLs),
1442 NULL);
1443 base::ListValue startup_urls;
1444 startup_urls.Append(
1445 new base::StringValue(test_server()->GetURL("files/title1.html").spec()));
1446 policy_map_.Set(policy::key::kRestoreOnStartupURLs,
1447 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
1448 startup_urls.DeepCopy(), NULL);
1449 provider_.UpdateChromePolicy(policy_map_);
1450 base::RunLoop().RunUntilIdle();
1452 // Do a process-startup browser launch.
1453 base::CommandLine dummy(base::CommandLine::NO_PROGRAM);
1454 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1455 chrome::startup::IS_FIRST_RUN);
1456 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1457 browser()->host_desktop_type()));
1459 // This should have created a new browser window.
1460 Browser* new_browser = FindOneOtherBrowser(browser());
1461 ASSERT_TRUE(new_browser);
1463 // Verify that the URL specified through policy is shown and no sync promo has
1464 // been added.
1465 TabStripModel* tab_strip = new_browser->tab_strip_model();
1466 ASSERT_EQ(1, tab_strip->count());
1467 EXPECT_EQ("title1.html",
1468 tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
1470 #endif // defined(ENABLE_CONFIGURATION_POLICY)
1472 #endif // !defined(OS_LINUX) || !defined(GOOGLE_CHROME_BUILD) ||
1473 // defined(ENABLE_CONFIGURATION_POLICY)
1475 #endif // !defined(OS_CHROMEOS)