startup_browser_creator_browsertest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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 "base/command_line.h" 6#include "base/files/file_path.h" 7#include "base/prefs/pref_service.h" 8#include "chrome/browser/browser_process.h" 9#include "chrome/browser/extensions/extension_browsertest.h" 10#include "chrome/browser/extensions/extension_service.h" 11#include "chrome/browser/extensions/extension_system.h" 12#include "chrome/browser/first_run/first_run.h" 13#include "chrome/browser/infobars/infobar_service.h" 14#include "chrome/browser/prefs/session_startup_pref.h" 15#include "chrome/browser/profiles/profile.h" 16#include "chrome/browser/profiles/profile_impl.h" 17#include "chrome/browser/profiles/profile_manager.h" 18#include "chrome/browser/sessions/session_restore.h" 19#include "chrome/browser/ui/browser.h" 20#include "chrome/browser/ui/browser_finder.h" 21#include "chrome/browser/ui/browser_iterator.h" 22#include "chrome/browser/ui/browser_list.h" 23#include "chrome/browser/ui/browser_list_observer.h" 24#include "chrome/browser/ui/browser_window.h" 25#include "chrome/browser/ui/host_desktop.h" 26#include "chrome/browser/ui/startup/startup_browser_creator.h" 27#include "chrome/browser/ui/startup/startup_browser_creator_impl.h" 28#include "chrome/browser/ui/tabs/tab_strip_model.h" 29#include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h" 30#include "chrome/common/chrome_switches.h" 31#include "chrome/common/pref_names.h" 32#include "chrome/common/url_constants.h" 33#include "chrome/test/base/in_process_browser_test.h" 34#include "chrome/test/base/ui_test_utils.h" 35#include "content/public/browser/web_contents.h" 36#include "testing/gtest/include/gtest/gtest.h" 37 38using extensions::Extension; 39 40class StartupBrowserCreatorTest : public ExtensionBrowserTest { 41 protected: 42 virtual bool SetUpUserDataDirectory() OVERRIDE { 43 // Make sure the first run sentinel file exists before running these tests, 44 // since some of them customize the session startup pref whose value can 45 // be different than the default during the first run. 46 // TODO(bauerb): set the first run flag instead of creating a sentinel file. 47 first_run::CreateSentinel(); 48 return ExtensionBrowserTest::SetUpUserDataDirectory(); 49 } 50 51 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 52 ExtensionBrowserTest::SetUpCommandLine(command_line); 53 command_line->AppendSwitch(switches::kEnablePanels); 54 command_line->AppendSwitchASCII(switches::kHomePage, 55 chrome::kAboutBlankURL); 56#if defined(OS_CHROMEOS) 57 // TODO(nkostylev): Investigate if we can remove this switch. 58 command_line->AppendSwitch(switches::kCreateBrowserOnStartupForTests); 59#endif 60 } 61 62 // Helper functions return void so that we can ASSERT*(). 63 // Use ASSERT_NO_FATAL_FAILURE around calls to these functions to stop the 64 // test if an assert fails. 65 void LoadApp(const std::string& app_name, 66 const Extension** out_app_extension) { 67 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(app_name.c_str()))); 68 69 ExtensionService* service = extensions::ExtensionSystem::Get( 70 browser()->profile())->extension_service(); 71 *out_app_extension = service->GetExtensionById( 72 last_loaded_extension_id_, false); 73 ASSERT_TRUE(*out_app_extension); 74 75 // Code that opens a new browser assumes we start with exactly one. 76 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(), 77 browser()->host_desktop_type())); 78 } 79 80 void SetAppLaunchPref(const std::string& app_id, 81 extensions::ExtensionPrefs::LaunchType launch_type) { 82 ExtensionService* service = extensions::ExtensionSystem::Get( 83 browser()->profile())->extension_service(); 84 service->extension_prefs()->SetLaunchType(app_id, launch_type); 85 } 86 87 // Check that there are two browsers. Find the one that is not |browser()|. 88 void FindOneOtherBrowser(Browser** out_other_browser) { 89 // There should only be one other browser. 90 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(), 91 browser()->host_desktop_type())); 92 93 // Find the new browser. 94 Browser* other_browser = NULL; 95 for (chrome::BrowserIterator it; !it.done() && !other_browser; it.Next()) { 96 if (*it != browser()) 97 other_browser = *it; 98 } 99 ASSERT_TRUE(other_browser); 100 ASSERT_TRUE(other_browser != browser()); 101 *out_other_browser = other_browser; 102 } 103 104 Browser* FindOneOtherBrowserForProfile(Profile* profile, 105 Browser* not_this_browser) { 106 for (chrome::BrowserIterator it; !it.done(); it.Next()) { 107 if (*it != not_this_browser && it->profile() == profile) 108 return *it; 109 } 110 return NULL; 111 } 112}; 113 114class OpenURLsPopupObserver : public chrome::BrowserListObserver { 115 public: 116 OpenURLsPopupObserver() : added_browser_(NULL) { } 117 118 virtual void OnBrowserAdded(Browser* browser) OVERRIDE { 119 added_browser_ = browser; 120 } 121 122 virtual void OnBrowserRemoved(Browser* browser) OVERRIDE { } 123 124 Browser* added_browser_; 125}; 126 127// Test that when there is a popup as the active browser any requests to 128// StartupBrowserCreatorImpl::OpenURLsInBrowser don't crash because there's no 129// explicit profile given. 130IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenURLsPopup) { 131 std::vector<GURL> urls; 132 urls.push_back(GURL("http://localhost")); 133 134 // Note that in our testing we do not ever query the BrowserList for the "last 135 // active" browser. That's because the browsers are set as "active" by 136 // platform UI toolkit messages, and those messages are not sent during unit 137 // testing sessions. 138 139 OpenURLsPopupObserver observer; 140 BrowserList::AddObserver(&observer); 141 142 Browser* popup = new Browser( 143 Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile(), 144 browser()->host_desktop_type())); 145 ASSERT_TRUE(popup->is_type_popup()); 146 ASSERT_EQ(popup, observer.added_browser_); 147 148 CommandLine dummy(CommandLine::NO_PROGRAM); 149 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ? 150 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN; 151 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run); 152 // This should create a new window, but re-use the profile from |popup|. If 153 // it used a NULL or invalid profile, it would crash. 154 launch.OpenURLsInBrowser(popup, false, urls); 155 ASSERT_NE(popup, observer.added_browser_); 156 BrowserList::RemoveObserver(&observer); 157} 158 159// We don't do non-process-startup browser launches on ChromeOS. 160// Session restore for process-startup browser launches is tested 161// in session_restore_uitest. 162#if !defined(OS_CHROMEOS) 163// Verify that startup URLs are honored when the process already exists but has 164// no tabbed browser windows (eg. as if the process is running only due to a 165// background application. 166IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, 167 StartupURLsOnNewWindowWithNoTabbedBrowsers) { 168 // Use a couple same-site HTTP URLs. 169 ASSERT_TRUE(test_server()->Start()); 170 std::vector<GURL> urls; 171 urls.push_back(test_server()->GetURL("files/title1.html")); 172 urls.push_back(test_server()->GetURL("files/title2.html")); 173 174 // Set the startup preference to open these URLs. 175 SessionStartupPref pref(SessionStartupPref::URLS); 176 pref.urls = urls; 177 SessionStartupPref::SetStartupPref(browser()->profile(), pref); 178 179 // Close the browser. 180 browser()->window()->Close(); 181 182 // Do a simple non-process-startup browser launch. 183 CommandLine dummy(CommandLine::NO_PROGRAM); 184 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ? 185 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN; 186 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run); 187 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); 188 189 // This should have created a new browser window. |browser()| is still 190 // around at this point, even though we've closed its window. 191 Browser* new_browser = NULL; 192 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); 193 194 // The new browser should have one tab for each URL. 195 TabStripModel* tab_strip = new_browser->tab_strip_model(); 196 ASSERT_EQ(static_cast<int>(urls.size()), tab_strip->count()); 197 for (size_t i=0; i < urls.size(); i++) { 198 EXPECT_EQ(urls[i], tab_strip->GetWebContentsAt(i)->GetURL()); 199 } 200 201 // The two tabs, despite having the same site, should be in different 202 // SiteInstances. 203 EXPECT_NE(tab_strip->GetWebContentsAt(0)->GetSiteInstance(), 204 tab_strip->GetWebContentsAt(1)->GetSiteInstance()); 205} 206 207// Verify that startup URLs aren't used when the process already exists 208// and has other tabbed browser windows. This is the common case of starting a 209// new browser. 210IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, 211 StartupURLsOnNewWindow) { 212 // Use a couple arbitrary URLs. 213 std::vector<GURL> urls; 214 urls.push_back(ui_test_utils::GetTestUrl( 215 base::FilePath(base::FilePath::kCurrentDirectory), 216 base::FilePath(FILE_PATH_LITERAL("title1.html")))); 217 urls.push_back(ui_test_utils::GetTestUrl( 218 base::FilePath(base::FilePath::kCurrentDirectory), 219 base::FilePath(FILE_PATH_LITERAL("title2.html")))); 220 221 // Set the startup preference to open these URLs. 222 SessionStartupPref pref(SessionStartupPref::URLS); 223 pref.urls = urls; 224 SessionStartupPref::SetStartupPref(browser()->profile(), pref); 225 226 // Do a simple non-process-startup browser launch. 227 CommandLine dummy(CommandLine::NO_PROGRAM); 228 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ? 229 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN; 230 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run); 231 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); 232 233 // This should have created a new browser window. 234 Browser* new_browser = NULL; 235 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); 236 237 // The new browser should have exactly one tab (not the startup URLs). 238 ASSERT_EQ(1, new_browser->tab_strip_model()->count()); 239} 240 241// App shortcuts are not implemented on mac os. 242#if !defined(OS_MACOSX) 243IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutNoPref) { 244 // Load an app with launch.container = 'tab'. 245 const Extension* extension_app = NULL; 246 ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app)); 247 248 // Add --app-id=<extension->id()> to the command line. 249 CommandLine command_line(CommandLine::NO_PROGRAM); 250 command_line.AppendSwitchASCII(switches::kAppId, extension_app->id()); 251 252 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ? 253 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN; 254 StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run); 255 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); 256 257 // No pref was set, so the app should have opened in a window. 258 // The launch should have created a new browser. 259 Browser* new_browser = NULL; 260 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); 261 262 // Expect an app window. 263 EXPECT_TRUE(new_browser->is_app()); 264 265 // The browser's app_name should include the app's ID. 266 EXPECT_NE( 267 new_browser->app_name_.find(extension_app->id()), 268 std::string::npos) << new_browser->app_name_; 269} 270 271IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutWindowPref) { 272 const Extension* extension_app = NULL; 273 ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app)); 274 275 // Set a pref indicating that the user wants to open this app in a window. 276 SetAppLaunchPref(extension_app->id(), 277 extensions::ExtensionPrefs::LAUNCH_WINDOW); 278 279 CommandLine command_line(CommandLine::NO_PROGRAM); 280 command_line.AppendSwitchASCII(switches::kAppId, extension_app->id()); 281 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ? 282 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN; 283 StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run); 284 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); 285 286 // Pref was set to open in a window, so the app should have opened in a 287 // window. The launch should have created a new browser. Find the new 288 // browser. 289 Browser* new_browser = NULL; 290 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); 291 292 // Expect an app window. 293 EXPECT_TRUE(new_browser->is_app()); 294 295 // The browser's app_name should include the app's ID. 296 EXPECT_NE( 297 new_browser->app_name_.find(extension_app->id()), 298 std::string::npos) << new_browser->app_name_; 299} 300 301IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutTabPref) { 302 // Load an app with launch.container = 'tab'. 303 const Extension* extension_app = NULL; 304 ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app)); 305 306 // Set a pref indicating that the user wants to open this app in a window. 307 SetAppLaunchPref(extension_app->id(), 308 extensions::ExtensionPrefs::LAUNCH_REGULAR); 309 310 CommandLine command_line(CommandLine::NO_PROGRAM); 311 command_line.AppendSwitchASCII(switches::kAppId, extension_app->id()); 312 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ? 313 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN; 314 StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run); 315 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); 316 317 // When an app shortcut is open and the pref indicates a tab should 318 // open, the tab is open in a new browser window. Expect a new window. 319 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(), 320 browser()->host_desktop_type())); 321 322 Browser* new_browser = NULL; 323 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); 324 325 // The tab should be in a tabbed window. 326 EXPECT_TRUE(new_browser->is_type_tabbed()); 327 328 // The browser's app_name should not include the app's ID: It is in a 329 // normal browser. 330 EXPECT_EQ( 331 new_browser->app_name_.find(extension_app->id()), 332 std::string::npos) << new_browser->app_name_; 333} 334 335#endif // !defined(OS_MACOSX) 336 337#endif // !defined(OS_CHROMEOS) 338 339IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, 340 ReadingWasRestartedAfterRestart) { 341 // Tests that StartupBrowserCreator::WasRestarted reads and resets the 342 // preference kWasRestarted correctly. 343 StartupBrowserCreator::was_restarted_read_ = false; 344 PrefService* pref_service = g_browser_process->local_state(); 345 pref_service->SetBoolean(prefs::kWasRestarted, true); 346 EXPECT_TRUE(StartupBrowserCreator::WasRestarted()); 347 EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted)); 348 EXPECT_TRUE(StartupBrowserCreator::WasRestarted()); 349} 350 351IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, 352 ReadingWasRestartedAfterNormalStart) { 353 // Tests that StartupBrowserCreator::WasRestarted reads and resets the 354 // preference kWasRestarted correctly. 355 StartupBrowserCreator::was_restarted_read_ = false; 356 PrefService* pref_service = g_browser_process->local_state(); 357 pref_service->SetBoolean(prefs::kWasRestarted, false); 358 EXPECT_FALSE(StartupBrowserCreator::WasRestarted()); 359 EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted)); 360 EXPECT_FALSE(StartupBrowserCreator::WasRestarted()); 361} 362 363IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, AddFirstRunTab) { 364 StartupBrowserCreator browser_creator; 365 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html")); 366 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title2.html")); 367 368 // Do a simple non-process-startup browser launch. 369 CommandLine dummy(CommandLine::NO_PROGRAM); 370 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator, 371 chrome::startup::IS_FIRST_RUN); 372 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); 373 374 // This should have created a new browser window. 375 Browser* new_browser = NULL; 376 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); 377 378 TabStripModel* tab_strip = new_browser->tab_strip_model(); 379 EXPECT_EQ(2, tab_strip->count()); 380 381 EXPECT_EQ("title1.html", 382 tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName()); 383 EXPECT_EQ("title2.html", 384 tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName()); 385} 386 387// Test hard-coded special first run tabs (defined in 388// StartupBrowserCreatorImpl::AddStartupURLs()). 389IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, AddCustomFirstRunTab) { 390 StartupBrowserCreator browser_creator; 391 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html")); 392 browser_creator.AddFirstRunTab(GURL("http://new_tab_page")); 393 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title2.html")); 394 browser_creator.AddFirstRunTab(GURL("http://welcome_page")); 395 396 // Do a simple non-process-startup browser launch. 397 CommandLine dummy(CommandLine::NO_PROGRAM); 398 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator, 399 chrome::startup::IS_FIRST_RUN); 400 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); 401 402 // This should have created a new browser window. 403 Browser* new_browser = NULL; 404 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); 405 406 TabStripModel* tab_strip = new_browser->tab_strip_model(); 407 EXPECT_EQ(4, tab_strip->count()); 408 409 EXPECT_EQ("title1.html", 410 tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName()); 411 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), 412 tab_strip->GetWebContentsAt(1)->GetURL()); 413 EXPECT_EQ("title2.html", 414 tab_strip->GetWebContentsAt(2)->GetURL().ExtractFileName()); 415 EXPECT_EQ(internals::GetWelcomePageURL(), 416 tab_strip->GetWebContentsAt(3)->GetURL()); 417} 418 419IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoNoWelcomePage) { 420 // Trick this test into thinking the promo has been shown for this profile; so 421 // that it will show it again (otherwise it skips showing it since 422 // --no-first-run is specified in browser tests). 423 SyncPromoUI::DidShowSyncPromoAtStartup(browser()->profile()); 424 425 // Do a simple non-process-startup browser launch. 426 CommandLine dummy(CommandLine::NO_PROGRAM); 427 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, 428 chrome::startup::IS_FIRST_RUN); 429 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); 430 431 // This should have created a new browser window. 432 Browser* new_browser = NULL; 433 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); 434 435 TabStripModel* tab_strip = new_browser->tab_strip_model(); 436 EXPECT_EQ(1, tab_strip->count()); 437 438 if (SyncPromoUI::ShouldShowSyncPromoAtStartup(browser()->profile(), true)) { 439 EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host()); 440 } else { 441 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), 442 tab_strip->GetWebContentsAt(0)->GetURL()); 443 } 444} 445 446IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoWithWelcomePage) { 447 // Trick this test into thinking the promo has been shown for this profile; so 448 // that it will show it again (otherwise it skips showing it since 449 // --no-first-run is specified in browser tests). 450 SyncPromoUI::DidShowSyncPromoAtStartup(browser()->profile()); 451 first_run::SetShouldShowWelcomePage(); 452 453 // Do a simple non-process-startup browser launch. 454 CommandLine dummy(CommandLine::NO_PROGRAM); 455 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, 456 chrome::startup::IS_FIRST_RUN); 457 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); 458 459 // This should have created a new browser window. 460 Browser* new_browser = NULL; 461 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); 462 463 TabStripModel* tab_strip = new_browser->tab_strip_model(); 464 EXPECT_EQ(2, tab_strip->count()); 465 466 if (SyncPromoUI::ShouldShowSyncPromoAtStartup(browser()->profile(), true)) { 467 EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host()); 468 } else { 469 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), 470 tab_strip->GetWebContentsAt(0)->GetURL()); 471 } 472 EXPECT_EQ(internals::GetWelcomePageURL(), 473 tab_strip->GetWebContentsAt(1)->GetURL()); 474} 475 476IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoWithFirstRunTabs) { 477 StartupBrowserCreator browser_creator; 478 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html")); 479 480 // Trick this test into thinking the promo has been shown for this profile; so 481 // that it will show it again (otherwise it skips showing it since 482 // --no-first-run is specified in browser tests). 483 SyncPromoUI::DidShowSyncPromoAtStartup(browser()->profile()); 484 485 // The welcome page should not be shown, even if 486 // first_run::ShouldShowWelcomePage() says so, when there are already 487 // more than 2 first run tabs. 488 first_run::SetShouldShowWelcomePage(); 489 490 // Do a simple non-process-startup browser launch. 491 CommandLine dummy(CommandLine::NO_PROGRAM); 492 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator, 493 chrome::startup::IS_FIRST_RUN); 494 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); 495 496 // This should have created a new browser window. 497 Browser* new_browser = NULL; 498 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); 499 500 TabStripModel* tab_strip = new_browser->tab_strip_model(); 501 if (SyncPromoUI::ShouldShowSyncPromoAtStartup(browser()->profile(), true)) { 502 EXPECT_EQ(2, tab_strip->count()); 503 EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host()); 504 EXPECT_EQ("title1.html", 505 tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName()); 506 } else { 507 EXPECT_EQ(1, tab_strip->count()); 508 EXPECT_EQ("title1.html", 509 tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName()); 510 } 511} 512 513// The welcome page should still be shown if there are more than 2 first run 514// tabs, but the welcome page was explcitly added to the first run tabs. 515IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, 516 SyncPromoWithFirstRunTabsIncludingWelcomePage) { 517 StartupBrowserCreator browser_creator; 518 browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html")); 519 browser_creator.AddFirstRunTab(GURL("http://welcome_page")); 520 521 // Trick this test into thinking the promo has been shown for this profile; so 522 // that it will show it again (otherwise it skips showing it since 523 // --no-first-run is specified in browser tests). 524 SyncPromoUI::DidShowSyncPromoAtStartup(browser()->profile()); 525 526 // Do a simple non-process-startup browser launch. 527 CommandLine dummy(CommandLine::NO_PROGRAM); 528 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator, 529 chrome::startup::IS_FIRST_RUN); 530 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); 531 532 // This should have created a new browser window. 533 Browser* new_browser = NULL; 534 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); 535 536 TabStripModel* tab_strip = new_browser->tab_strip_model(); 537 if (SyncPromoUI::ShouldShowSyncPromoAtStartup(browser()->profile(), true)) { 538 EXPECT_EQ(3, tab_strip->count()); 539 EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host()); 540 EXPECT_EQ("title1.html", 541 tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName()); 542 EXPECT_EQ(internals::GetWelcomePageURL(), 543 tab_strip->GetWebContentsAt(2)->GetURL()); 544 } else { 545 EXPECT_EQ(2, tab_strip->count()); 546 EXPECT_EQ("title1.html", 547 tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName()); 548 EXPECT_EQ(internals::GetWelcomePageURL(), 549 tab_strip->GetWebContentsAt(1)->GetURL()); 550 } 551} 552 553#if !defined(OS_CHROMEOS) 554IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, StartupURLsForTwoProfiles) { 555 Profile* default_profile = browser()->profile(); 556 557 ProfileManager* profile_manager = g_browser_process->profile_manager(); 558 // Create another profile. 559 base::FilePath dest_path = profile_manager->user_data_dir(); 560 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile 1")); 561 562 Profile* other_profile = profile_manager->GetProfile(dest_path); 563 ASSERT_TRUE(other_profile); 564 565 // Use a couple arbitrary URLs. 566 std::vector<GURL> urls1; 567 urls1.push_back(ui_test_utils::GetTestUrl( 568 base::FilePath(base::FilePath::kCurrentDirectory), 569 base::FilePath(FILE_PATH_LITERAL("title1.html")))); 570 std::vector<GURL> urls2; 571 urls2.push_back(ui_test_utils::GetTestUrl( 572 base::FilePath(base::FilePath::kCurrentDirectory), 573 base::FilePath(FILE_PATH_LITERAL("title2.html")))); 574 575 // Set different startup preferences for the 2 profiles. 576 SessionStartupPref pref1(SessionStartupPref::URLS); 577 pref1.urls = urls1; 578 SessionStartupPref::SetStartupPref(default_profile, pref1); 579 SessionStartupPref pref2(SessionStartupPref::URLS); 580 pref2.urls = urls2; 581 SessionStartupPref::SetStartupPref(other_profile, pref2); 582 583 // Close the browser. 584 browser()->window()->Close(); 585 586 // Do a simple non-process-startup browser launch. 587 CommandLine dummy(CommandLine::NO_PROGRAM); 588 589 int return_code; 590 StartupBrowserCreator browser_creator; 591 std::vector<Profile*> last_opened_profiles; 592 last_opened_profiles.push_back(default_profile); 593 last_opened_profiles.push_back(other_profile); 594 browser_creator.Start(dummy, profile_manager->user_data_dir(), 595 default_profile, last_opened_profiles, &return_code); 596 597 // urls1 were opened in a browser for default_profile, and urls2 were opened 598 // in a browser for other_profile. 599 Browser* new_browser = NULL; 600 // |browser()| is still around at this point, even though we've closed its 601 // window. Thus the browser count for default_profile is 2. 602 ASSERT_EQ(2u, chrome::GetBrowserCount(default_profile, 603 browser()->host_desktop_type())); 604 new_browser = FindOneOtherBrowserForProfile(default_profile, browser()); 605 ASSERT_TRUE(new_browser); 606 TabStripModel* tab_strip = new_browser->tab_strip_model(); 607 ASSERT_EQ(1, tab_strip->count()); 608 EXPECT_EQ(urls1[0], tab_strip->GetWebContentsAt(0)->GetURL()); 609 610 ASSERT_EQ(1u, chrome::GetBrowserCount(other_profile, 611 browser()->host_desktop_type())); 612 new_browser = FindOneOtherBrowserForProfile(other_profile, NULL); 613 ASSERT_TRUE(new_browser); 614 tab_strip = new_browser->tab_strip_model(); 615 ASSERT_EQ(1, tab_strip->count()); 616 EXPECT_EQ(urls2[0], tab_strip->GetWebContentsAt(0)->GetURL()); 617} 618 619IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, UpdateWithTwoProfiles) { 620 // Make StartupBrowserCreator::WasRestarted() return true. 621 StartupBrowserCreator::was_restarted_read_ = false; 622 PrefService* pref_service = g_browser_process->local_state(); 623 pref_service->SetBoolean(prefs::kWasRestarted, true); 624 625 ProfileManager* profile_manager = g_browser_process->profile_manager(); 626 627 // Create two profiles. 628 base::FilePath dest_path = profile_manager->user_data_dir(); 629 630 Profile* profile1 = profile_manager->GetProfile( 631 dest_path.Append(FILE_PATH_LITERAL("New Profile 1"))); 632 ASSERT_TRUE(profile1); 633 634 Profile* profile2 = profile_manager->GetProfile( 635 dest_path.Append(FILE_PATH_LITERAL("New Profile 2"))); 636 ASSERT_TRUE(profile2); 637 638 // Use a couple arbitrary URLs. 639 std::vector<GURL> urls1; 640 urls1.push_back(ui_test_utils::GetTestUrl( 641 base::FilePath(base::FilePath::kCurrentDirectory), 642 base::FilePath(FILE_PATH_LITERAL("title1.html")))); 643 std::vector<GURL> urls2; 644 urls2.push_back(ui_test_utils::GetTestUrl( 645 base::FilePath(base::FilePath::kCurrentDirectory), 646 base::FilePath(FILE_PATH_LITERAL("title2.html")))); 647 648 // Set different startup preferences for the 2 profiles. 649 SessionStartupPref pref1(SessionStartupPref::URLS); 650 pref1.urls = urls1; 651 SessionStartupPref::SetStartupPref(profile1, pref1); 652 SessionStartupPref pref2(SessionStartupPref::URLS); 653 pref2.urls = urls2; 654 SessionStartupPref::SetStartupPref(profile2, pref2); 655 656 // Simulate a launch after a browser update. 657 CommandLine dummy(CommandLine::NO_PROGRAM); 658 int return_code; 659 StartupBrowserCreator browser_creator; 660 std::vector<Profile*> last_opened_profiles; 661 last_opened_profiles.push_back(profile1); 662 last_opened_profiles.push_back(profile2); 663 browser_creator.Start(dummy, profile_manager->user_data_dir(), profile1, 664 last_opened_profiles, &return_code); 665 666 while (SessionRestore::IsRestoring(profile1) || 667 SessionRestore::IsRestoring(profile2)) 668 MessageLoop::current()->RunUntilIdle(); 669 670 // The startup URLs are ignored, and instead the last open sessions are 671 // restored. 672 EXPECT_TRUE(profile1->restored_last_session()); 673 EXPECT_TRUE(profile2->restored_last_session()); 674 675 Browser* new_browser = NULL; 676 ASSERT_EQ(1u, chrome::GetBrowserCount(profile1, 677 browser()->host_desktop_type())); 678 new_browser = FindOneOtherBrowserForProfile(profile1, NULL); 679 ASSERT_TRUE(new_browser); 680 TabStripModel* tab_strip = new_browser->tab_strip_model(); 681 ASSERT_EQ(1, tab_strip->count()); 682 EXPECT_EQ(GURL(chrome::kAboutBlankURL), 683 tab_strip->GetWebContentsAt(0)->GetURL()); 684 685 ASSERT_EQ(1u, chrome::GetBrowserCount(profile2, 686 browser()->host_desktop_type())); 687 new_browser = FindOneOtherBrowserForProfile(profile2, NULL); 688 ASSERT_TRUE(new_browser); 689 tab_strip = new_browser->tab_strip_model(); 690 ASSERT_EQ(1, tab_strip->count()); 691 EXPECT_EQ(GURL(chrome::kAboutBlankURL), 692 tab_strip->GetWebContentsAt(0)->GetURL()); 693} 694 695IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, 696 ProfilesWithoutPagesNotLaunched) { 697 Profile* default_profile = browser()->profile(); 698 699 ProfileManager* profile_manager = g_browser_process->profile_manager(); 700 701 // Create 4 more profiles. 702 base::FilePath dest_path1 = profile_manager->user_data_dir().Append( 703 FILE_PATH_LITERAL("New Profile 1")); 704 base::FilePath dest_path2 = profile_manager->user_data_dir().Append( 705 FILE_PATH_LITERAL("New Profile 2")); 706 base::FilePath dest_path3 = profile_manager->user_data_dir().Append( 707 FILE_PATH_LITERAL("New Profile 3")); 708 base::FilePath dest_path4 = profile_manager->user_data_dir().Append( 709 FILE_PATH_LITERAL("New Profile 4")); 710 711 Profile* profile_home1 = profile_manager->GetProfile(dest_path1); 712 ASSERT_TRUE(profile_home1); 713 Profile* profile_home2 = profile_manager->GetProfile(dest_path2); 714 ASSERT_TRUE(profile_home2); 715 Profile* profile_last = profile_manager->GetProfile(dest_path3); 716 ASSERT_TRUE(profile_last); 717 Profile* profile_urls = profile_manager->GetProfile(dest_path4); 718 ASSERT_TRUE(profile_urls); 719 720 // Set the profiles to open urls, open last visited pages or display the home 721 // page. 722 SessionStartupPref pref_home(SessionStartupPref::DEFAULT); 723 SessionStartupPref::SetStartupPref(profile_home1, pref_home); 724 SessionStartupPref::SetStartupPref(profile_home2, pref_home); 725 726 SessionStartupPref pref_last(SessionStartupPref::LAST); 727 SessionStartupPref::SetStartupPref(profile_last, pref_last); 728 729 std::vector<GURL> urls; 730 urls.push_back(ui_test_utils::GetTestUrl( 731 base::FilePath(base::FilePath::kCurrentDirectory), 732 base::FilePath(FILE_PATH_LITERAL("title1.html")))); 733 734 SessionStartupPref pref_urls(SessionStartupPref::URLS); 735 pref_urls.urls = urls; 736 SessionStartupPref::SetStartupPref(profile_urls, pref_urls); 737 738 // Close the browser. 739 chrome::HostDesktopType original_desktop_type = 740 browser()->host_desktop_type(); 741 browser()->window()->Close(); 742 743 // Do a simple non-process-startup browser launch. 744 CommandLine dummy(CommandLine::NO_PROGRAM); 745 746 int return_code; 747 StartupBrowserCreator browser_creator; 748 std::vector<Profile*> last_opened_profiles; 749 last_opened_profiles.push_back(profile_home1); 750 last_opened_profiles.push_back(profile_home2); 751 last_opened_profiles.push_back(profile_last); 752 last_opened_profiles.push_back(profile_urls); 753 browser_creator.Start(dummy, profile_manager->user_data_dir(), profile_home1, 754 last_opened_profiles, &return_code); 755 756 while (SessionRestore::IsRestoring(default_profile) || 757 SessionRestore::IsRestoring(profile_home1) || 758 SessionRestore::IsRestoring(profile_home2) || 759 SessionRestore::IsRestoring(profile_last) || 760 SessionRestore::IsRestoring(profile_urls)) 761 MessageLoop::current()->RunUntilIdle(); 762 763 Browser* new_browser = NULL; 764 // The last open profile (the profile_home1 in this case) will always be 765 // launched, even if it will open just the home page. 766 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home1, original_desktop_type)); 767 new_browser = FindOneOtherBrowserForProfile(profile_home1, NULL); 768 ASSERT_TRUE(new_browser); 769 TabStripModel* tab_strip = new_browser->tab_strip_model(); 770 ASSERT_EQ(1, tab_strip->count()); 771 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), 772 tab_strip->GetWebContentsAt(0)->GetURL()); 773 774 // profile_urls opened the urls. 775 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls, original_desktop_type)); 776 new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL); 777 ASSERT_TRUE(new_browser); 778 tab_strip = new_browser->tab_strip_model(); 779 ASSERT_EQ(1, tab_strip->count()); 780 EXPECT_EQ(urls[0], tab_strip->GetWebContentsAt(0)->GetURL()); 781 782 // profile_last opened the last open pages. 783 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last, original_desktop_type)); 784 new_browser = FindOneOtherBrowserForProfile(profile_last, NULL); 785 ASSERT_TRUE(new_browser); 786 tab_strip = new_browser->tab_strip_model(); 787 ASSERT_EQ(1, tab_strip->count()); 788 EXPECT_EQ(GURL(chrome::kAboutBlankURL), 789 tab_strip->GetWebContentsAt(0)->GetURL()); 790 791 // profile_home2 was not launched since it would've only opened the home page. 792 ASSERT_EQ(0u, chrome::GetBrowserCount(profile_home2, original_desktop_type)); 793} 794 795IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, ProfilesLaunchedAfterCrash) { 796 // After an unclean exit, all profiles will be launched. However, they won't 797 // open any pages automatically. 798 799 ProfileManager* profile_manager = g_browser_process->profile_manager(); 800 801 // Create 3 profiles. 802 base::FilePath dest_path1 = profile_manager->user_data_dir().Append( 803 FILE_PATH_LITERAL("New Profile 1")); 804 base::FilePath dest_path2 = profile_manager->user_data_dir().Append( 805 FILE_PATH_LITERAL("New Profile 2")); 806 base::FilePath dest_path3 = profile_manager->user_data_dir().Append( 807 FILE_PATH_LITERAL("New Profile 3")); 808 809 Profile* profile_home = profile_manager->GetProfile(dest_path1); 810 ASSERT_TRUE(profile_home); 811 Profile* profile_last = profile_manager->GetProfile(dest_path2); 812 ASSERT_TRUE(profile_last); 813 Profile* profile_urls = profile_manager->GetProfile(dest_path3); 814 ASSERT_TRUE(profile_urls); 815 816 // Set the profiles to open the home page, last visited pages or URLs. 817 SessionStartupPref pref_home(SessionStartupPref::DEFAULT); 818 SessionStartupPref::SetStartupPref(profile_home, pref_home); 819 820 SessionStartupPref pref_last(SessionStartupPref::LAST); 821 SessionStartupPref::SetStartupPref(profile_last, pref_last); 822 823 std::vector<GURL> urls; 824 urls.push_back(ui_test_utils::GetTestUrl( 825 base::FilePath(base::FilePath::kCurrentDirectory), 826 base::FilePath(FILE_PATH_LITERAL("title1.html")))); 827 828 SessionStartupPref pref_urls(SessionStartupPref::URLS); 829 pref_urls.urls = urls; 830 SessionStartupPref::SetStartupPref(profile_urls, pref_urls); 831 832 // Simulate a launch after an unclear exit. 833 browser()->window()->Close(); 834 static_cast<ProfileImpl*>(profile_home)->last_session_exit_type_ = 835 Profile::EXIT_CRASHED; 836 static_cast<ProfileImpl*>(profile_last)->last_session_exit_type_ = 837 Profile::EXIT_CRASHED; 838 static_cast<ProfileImpl*>(profile_urls)->last_session_exit_type_ = 839 Profile::EXIT_CRASHED; 840 841 CommandLine dummy(CommandLine::NO_PROGRAM); 842 dummy.AppendSwitchASCII(switches::kTestType, "browser"); 843 int return_code; 844 StartupBrowserCreator browser_creator; 845 std::vector<Profile*> last_opened_profiles; 846 last_opened_profiles.push_back(profile_home); 847 last_opened_profiles.push_back(profile_last); 848 last_opened_profiles.push_back(profile_urls); 849 browser_creator.Start(dummy, profile_manager->user_data_dir(), profile_home, 850 last_opened_profiles, &return_code); 851 852 // No profiles are getting restored, since they all display the crash info 853 // bar. 854 EXPECT_FALSE(SessionRestore::IsRestoring(profile_home)); 855 EXPECT_FALSE(SessionRestore::IsRestoring(profile_last)); 856 EXPECT_FALSE(SessionRestore::IsRestoring(profile_urls)); 857 858 // The profile which normally opens the home page displays the new tab page. 859 Browser* new_browser = NULL; 860 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home, 861 browser()->host_desktop_type())); 862 new_browser = FindOneOtherBrowserForProfile(profile_home, NULL); 863 ASSERT_TRUE(new_browser); 864 TabStripModel* tab_strip = new_browser->tab_strip_model(); 865 ASSERT_EQ(1, tab_strip->count()); 866 content::WebContents* web_contents = tab_strip->GetWebContentsAt(0); 867 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL()); 868 EXPECT_EQ(1U, 869 InfoBarService::FromWebContents(web_contents)->GetInfoBarCount()); 870 871 // The profile which normally opens last open pages displays the new tab page. 872 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last, 873 browser()->host_desktop_type())); 874 new_browser = FindOneOtherBrowserForProfile(profile_last, NULL); 875 ASSERT_TRUE(new_browser); 876 tab_strip = new_browser->tab_strip_model(); 877 ASSERT_EQ(1, tab_strip->count()); 878 web_contents = tab_strip->GetWebContentsAt(0); 879 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL()); 880 EXPECT_EQ(1U, 881 InfoBarService::FromWebContents(web_contents)->GetInfoBarCount()); 882 883 // The profile which normally opens URLs displays the new tab page. 884 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls, 885 browser()->host_desktop_type())); 886 new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL); 887 ASSERT_TRUE(new_browser); 888 tab_strip = new_browser->tab_strip_model(); 889 ASSERT_EQ(1, tab_strip->count()); 890 web_contents = tab_strip->GetWebContentsAt(0); 891 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL()); 892 EXPECT_EQ(1U, 893 InfoBarService::FromWebContents(web_contents)->GetInfoBarCount()); 894} 895#endif // !OS_CHROMEOS 896