| OLD | NEW |
| (Empty) |
| 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/file_path.h" | |
| 7 #include "base/utf_string_conversions.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/first_run/first_run.h" | |
| 12 #include "chrome/browser/infobars/infobar_tab_helper.h" | |
| 13 #include "chrome/browser/prefs/pref_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_init.h" | |
| 21 #include "chrome/browser/ui/browser_list.h" | |
| 22 #include "chrome/browser/ui/browser_window.h" | |
| 23 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
| 24 #include "chrome/common/chrome_switches.h" | |
| 25 #include "chrome/common/pref_names.h" | |
| 26 #include "chrome/common/url_constants.h" | |
| 27 #include "chrome/test/base/in_process_browser_test.h" | |
| 28 #include "chrome/test/base/ui_test_utils.h" | |
| 29 #include "content/public/browser/web_contents.h" | |
| 30 #include "testing/gtest/include/gtest/gtest.h" | |
| 31 | |
| 32 class BrowserInitTest : public ExtensionBrowserTest { | |
| 33 protected: | |
| 34 virtual bool SetUpUserDataDirectory() OVERRIDE { | |
| 35 // Make sure the first run sentinel file exists before running these tests, | |
| 36 // since some of them customize the session startup pref whose value can | |
| 37 // be different than the default during the first run. | |
| 38 // TODO(bauerb): set the first run flag instead of creating a sentinel file. | |
| 39 first_run::CreateSentinel(); | |
| 40 return ExtensionBrowserTest::SetUpUserDataDirectory(); | |
| 41 } | |
| 42 | |
| 43 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
| 44 ExtensionBrowserTest::SetUpCommandLine(command_line); | |
| 45 command_line->AppendSwitch(switches::kEnablePanels); | |
| 46 } | |
| 47 | |
| 48 // Helper functions return void so that we can ASSERT*(). | |
| 49 // Use ASSERT_NO_FATAL_FAILURE around calls to these functions to stop the | |
| 50 // test if an assert fails. | |
| 51 void LoadApp(const std::string& app_name, | |
| 52 const Extension** out_app_extension) { | |
| 53 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(app_name.c_str()))); | |
| 54 | |
| 55 ExtensionService* service = browser()->profile()->GetExtensionService(); | |
| 56 *out_app_extension = service->GetExtensionById( | |
| 57 last_loaded_extension_id_, false); | |
| 58 ASSERT_TRUE(*out_app_extension); | |
| 59 | |
| 60 // Code that opens a new browser assumes we start with exactly one. | |
| 61 ASSERT_EQ(1u, BrowserList::GetBrowserCount(browser()->profile())); | |
| 62 } | |
| 63 | |
| 64 void SetAppLaunchPref(const std::string& app_id, | |
| 65 ExtensionPrefs::LaunchType launch_type) { | |
| 66 ExtensionService* service = browser()->profile()->GetExtensionService(); | |
| 67 service->extension_prefs()->SetLaunchType(app_id, launch_type); | |
| 68 } | |
| 69 | |
| 70 // Check that there are two browsers. Find the one that is not |browser()|. | |
| 71 void FindOneOtherBrowser(Browser** out_other_browser) { | |
| 72 // There should only be one other browser. | |
| 73 ASSERT_EQ(2u, BrowserList::GetBrowserCount(browser()->profile())); | |
| 74 | |
| 75 // Find the new browser. | |
| 76 Browser* other_browser = NULL; | |
| 77 for (BrowserList::const_iterator i = BrowserList::begin(); | |
| 78 i != BrowserList::end() && !other_browser; ++i) { | |
| 79 if (*i != browser()) | |
| 80 other_browser = *i; | |
| 81 } | |
| 82 ASSERT_TRUE(other_browser); | |
| 83 ASSERT_TRUE(other_browser != browser()); | |
| 84 *out_other_browser = other_browser; | |
| 85 } | |
| 86 | |
| 87 Browser* FindOneOtherBrowserForProfile(Profile* profile, | |
| 88 Browser* not_this_browser) { | |
| 89 for (BrowserList::const_iterator i = BrowserList::begin(); | |
| 90 i != BrowserList::end(); ++i) { | |
| 91 if (*i != not_this_browser && (*i)->profile() == profile) | |
| 92 return *i; | |
| 93 } | |
| 94 return NULL; | |
| 95 } | |
| 96 }; | |
| 97 | |
| 98 class OpenURLsPopupObserver : public BrowserList::Observer { | |
| 99 public: | |
| 100 OpenURLsPopupObserver() : added_browser_(NULL) { } | |
| 101 | |
| 102 virtual void OnBrowserAdded(const Browser* browser) { | |
| 103 added_browser_ = browser; | |
| 104 } | |
| 105 | |
| 106 virtual void OnBrowserRemoved(const Browser* browser) { } | |
| 107 | |
| 108 const Browser* added_browser_; | |
| 109 }; | |
| 110 | |
| 111 // Test that when there is a popup as the active browser any requests to | |
| 112 // BrowserInit::LaunchWithProfile::OpenURLsInBrowser don't crash because | |
| 113 // there's no explicit profile given. | |
| 114 IN_PROC_BROWSER_TEST_F(BrowserInitTest, OpenURLsPopup) { | |
| 115 std::vector<GURL> urls; | |
| 116 urls.push_back(GURL("http://localhost")); | |
| 117 | |
| 118 // Note that in our testing we do not ever query the BrowserList for the "last | |
| 119 // active" browser. That's because the browsers are set as "active" by | |
| 120 // platform UI toolkit messages, and those messages are not sent during unit | |
| 121 // testing sessions. | |
| 122 | |
| 123 OpenURLsPopupObserver observer; | |
| 124 BrowserList::AddObserver(&observer); | |
| 125 | |
| 126 Browser* popup = Browser::CreateWithParams( | |
| 127 Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile())); | |
| 128 ASSERT_TRUE(popup->is_type_popup()); | |
| 129 ASSERT_EQ(popup, observer.added_browser_); | |
| 130 | |
| 131 CommandLine dummy(CommandLine::NO_PROGRAM); | |
| 132 BrowserInit::IsFirstRun first_run = first_run::IsChromeFirstRun() ? | |
| 133 BrowserInit::IS_FIRST_RUN : BrowserInit::IS_NOT_FIRST_RUN; | |
| 134 BrowserInit::LaunchWithProfile launch(FilePath(), dummy, first_run); | |
| 135 // This should create a new window, but re-use the profile from |popup|. If | |
| 136 // it used a NULL or invalid profile, it would crash. | |
| 137 launch.OpenURLsInBrowser(popup, false, urls); | |
| 138 ASSERT_NE(popup, observer.added_browser_); | |
| 139 BrowserList::RemoveObserver(&observer); | |
| 140 } | |
| 141 | |
| 142 // We don't do non-process-startup browser launches on ChromeOS. | |
| 143 // Session restore for process-startup browser launches is tested | |
| 144 // in session_restore_uitest. | |
| 145 #if !defined(OS_CHROMEOS) | |
| 146 // Verify that startup URLs are honored when the process already exists but has | |
| 147 // no tabbed browser windows (eg. as if the process is running only due to a | |
| 148 // background application. | |
| 149 IN_PROC_BROWSER_TEST_F(BrowserInitTest, | |
| 150 StartupURLsOnNewWindowWithNoTabbedBrowsers) { | |
| 151 // Use a couple same-site HTTP URLs. | |
| 152 ASSERT_TRUE(test_server()->Start()); | |
| 153 std::vector<GURL> urls; | |
| 154 urls.push_back(test_server()->GetURL("files/title1.html")); | |
| 155 urls.push_back(test_server()->GetURL("files/title2.html")); | |
| 156 | |
| 157 // Set the startup preference to open these URLs. | |
| 158 SessionStartupPref pref(SessionStartupPref::URLS); | |
| 159 pref.urls = urls; | |
| 160 SessionStartupPref::SetStartupPref(browser()->profile(), pref); | |
| 161 | |
| 162 // Close the browser. | |
| 163 browser()->window()->Close(); | |
| 164 | |
| 165 // Do a simple non-process-startup browser launch. | |
| 166 CommandLine dummy(CommandLine::NO_PROGRAM); | |
| 167 BrowserInit::IsFirstRun first_run = first_run::IsChromeFirstRun() ? | |
| 168 BrowserInit::IS_FIRST_RUN : BrowserInit::IS_NOT_FIRST_RUN; | |
| 169 BrowserInit::LaunchWithProfile launch(FilePath(), dummy, first_run); | |
| 170 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); | |
| 171 | |
| 172 // This should have created a new browser window. |browser()| is still | |
| 173 // around at this point, even though we've closed its window. | |
| 174 Browser* new_browser = NULL; | |
| 175 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); | |
| 176 | |
| 177 // The new browser should have one tab for each URL. | |
| 178 ASSERT_EQ(static_cast<int>(urls.size()), new_browser->tab_count()); | |
| 179 for (size_t i=0; i < urls.size(); i++) { | |
| 180 EXPECT_EQ(urls[i], new_browser->GetWebContentsAt(i)->GetURL()); | |
| 181 } | |
| 182 | |
| 183 // The two tabs, despite having the same site, should be in different | |
| 184 // SiteInstances. | |
| 185 EXPECT_NE(new_browser->GetWebContentsAt(0)->GetSiteInstance(), | |
| 186 new_browser->GetWebContentsAt(1)->GetSiteInstance()); | |
| 187 } | |
| 188 | |
| 189 // Verify that startup URLs aren't used when the process already exists | |
| 190 // and has other tabbed browser windows. This is the common case of starting a | |
| 191 // new browser. | |
| 192 IN_PROC_BROWSER_TEST_F(BrowserInitTest, | |
| 193 StartupURLsOnNewWindow) { | |
| 194 // Use a couple arbitrary URLs. | |
| 195 std::vector<GURL> urls; | |
| 196 urls.push_back(ui_test_utils::GetTestUrl( | |
| 197 FilePath(FilePath::kCurrentDirectory), | |
| 198 FilePath(FILE_PATH_LITERAL("title1.html")))); | |
| 199 urls.push_back(ui_test_utils::GetTestUrl( | |
| 200 FilePath(FilePath::kCurrentDirectory), | |
| 201 FilePath(FILE_PATH_LITERAL("title2.html")))); | |
| 202 | |
| 203 // Set the startup preference to open these URLs. | |
| 204 SessionStartupPref pref(SessionStartupPref::URLS); | |
| 205 pref.urls = urls; | |
| 206 SessionStartupPref::SetStartupPref(browser()->profile(), pref); | |
| 207 | |
| 208 // Do a simple non-process-startup browser launch. | |
| 209 CommandLine dummy(CommandLine::NO_PROGRAM); | |
| 210 BrowserInit::IsFirstRun first_run = first_run::IsChromeFirstRun() ? | |
| 211 BrowserInit::IS_FIRST_RUN : BrowserInit::IS_NOT_FIRST_RUN; | |
| 212 BrowserInit::LaunchWithProfile launch(FilePath(), dummy, first_run); | |
| 213 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); | |
| 214 | |
| 215 // This should have created a new browser window. | |
| 216 Browser* new_browser = NULL; | |
| 217 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); | |
| 218 | |
| 219 // The new browser should have exactly one tab (not the startup URLs). | |
| 220 ASSERT_EQ(1, new_browser->tab_count()); | |
| 221 } | |
| 222 | |
| 223 // App shortcuts are not implemented on mac os. | |
| 224 #if !defined(OS_MACOSX) | |
| 225 IN_PROC_BROWSER_TEST_F(BrowserInitTest, OpenAppShortcutNoPref) { | |
| 226 // Load an app with launch.container = 'tab'. | |
| 227 const Extension* extension_app = NULL; | |
| 228 ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app)); | |
| 229 | |
| 230 // Add --app-id=<extension->id()> to the command line. | |
| 231 CommandLine command_line(CommandLine::NO_PROGRAM); | |
| 232 command_line.AppendSwitchASCII(switches::kAppId, extension_app->id()); | |
| 233 | |
| 234 BrowserInit::IsFirstRun first_run = first_run::IsChromeFirstRun() ? | |
| 235 BrowserInit::IS_FIRST_RUN : BrowserInit::IS_NOT_FIRST_RUN; | |
| 236 BrowserInit::LaunchWithProfile launch(FilePath(), command_line, first_run); | |
| 237 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); | |
| 238 | |
| 239 // No pref was set, so the app should have opened in a window. | |
| 240 // The launch should have created a new browser. | |
| 241 Browser* new_browser = NULL; | |
| 242 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); | |
| 243 | |
| 244 // Expect an app window. | |
| 245 EXPECT_TRUE(new_browser->is_app()); | |
| 246 | |
| 247 // The browser's app_name should include the app's ID. | |
| 248 EXPECT_NE( | |
| 249 new_browser->app_name_.find(extension_app->id()), | |
| 250 std::string::npos) << new_browser->app_name_; | |
| 251 } | |
| 252 | |
| 253 IN_PROC_BROWSER_TEST_F(BrowserInitTest, OpenAppShortcutWindowPref) { | |
| 254 const Extension* extension_app = NULL; | |
| 255 ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app)); | |
| 256 | |
| 257 // Set a pref indicating that the user wants to open this app in a window. | |
| 258 SetAppLaunchPref(extension_app->id(), ExtensionPrefs::LAUNCH_WINDOW); | |
| 259 | |
| 260 CommandLine command_line(CommandLine::NO_PROGRAM); | |
| 261 command_line.AppendSwitchASCII(switches::kAppId, extension_app->id()); | |
| 262 BrowserInit::IsFirstRun first_run = first_run::IsChromeFirstRun() ? | |
| 263 BrowserInit::IS_FIRST_RUN : BrowserInit::IS_NOT_FIRST_RUN; | |
| 264 BrowserInit::LaunchWithProfile launch(FilePath(), command_line, first_run); | |
| 265 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); | |
| 266 | |
| 267 // Pref was set to open in a window, so the app should have opened in a | |
| 268 // window. The launch should have created a new browser. Find the new | |
| 269 // browser. | |
| 270 Browser* new_browser = NULL; | |
| 271 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); | |
| 272 | |
| 273 // Expect an app window. | |
| 274 EXPECT_TRUE(new_browser->is_app()); | |
| 275 | |
| 276 // The browser's app_name should include the app's ID. | |
| 277 EXPECT_NE( | |
| 278 new_browser->app_name_.find(extension_app->id()), | |
| 279 std::string::npos) << new_browser->app_name_; | |
| 280 } | |
| 281 | |
| 282 IN_PROC_BROWSER_TEST_F(BrowserInitTest, OpenAppShortcutTabPref) { | |
| 283 // Load an app with launch.container = 'tab'. | |
| 284 const Extension* extension_app = NULL; | |
| 285 ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app)); | |
| 286 | |
| 287 // Set a pref indicating that the user wants to open this app in a window. | |
| 288 SetAppLaunchPref(extension_app->id(), ExtensionPrefs::LAUNCH_REGULAR); | |
| 289 | |
| 290 CommandLine command_line(CommandLine::NO_PROGRAM); | |
| 291 command_line.AppendSwitchASCII(switches::kAppId, extension_app->id()); | |
| 292 BrowserInit::IsFirstRun first_run = first_run::IsChromeFirstRun() ? | |
| 293 BrowserInit::IS_FIRST_RUN : BrowserInit::IS_NOT_FIRST_RUN; | |
| 294 BrowserInit::LaunchWithProfile launch(FilePath(), command_line, first_run); | |
| 295 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); | |
| 296 | |
| 297 // When an app shortcut is open and the pref indicates a tab should | |
| 298 // open, the tab is open in a new browser window. Expect a new window. | |
| 299 ASSERT_EQ(2u, BrowserList::GetBrowserCount(browser()->profile())); | |
| 300 | |
| 301 Browser* new_browser = NULL; | |
| 302 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); | |
| 303 | |
| 304 // The tab should be in a tabbed window. | |
| 305 EXPECT_TRUE(new_browser->is_type_tabbed()); | |
| 306 | |
| 307 // The browser's app_name should not include the app's ID: It is in a | |
| 308 // normal browser. | |
| 309 EXPECT_EQ( | |
| 310 new_browser->app_name_.find(extension_app->id()), | |
| 311 std::string::npos) << new_browser->app_name_; | |
| 312 } | |
| 313 | |
| 314 IN_PROC_BROWSER_TEST_F(BrowserInitTest, OpenAppShortcutPanel) { | |
| 315 // Load an app with launch.container = 'panel'. | |
| 316 const Extension* extension_app = NULL; | |
| 317 ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_panel_container", &extension_app)); | |
| 318 | |
| 319 CommandLine command_line(CommandLine::NO_PROGRAM); | |
| 320 command_line.AppendSwitchASCII(switches::kAppId, extension_app->id()); | |
| 321 BrowserInit::IsFirstRun first_run = first_run::IsChromeFirstRun() ? | |
| 322 BrowserInit::IS_FIRST_RUN : BrowserInit::IS_NOT_FIRST_RUN; | |
| 323 BrowserInit::LaunchWithProfile launch(FilePath(), command_line, first_run); | |
| 324 ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false)); | |
| 325 | |
| 326 // The launch should have created a new browser, with a panel type. | |
| 327 Browser* new_browser = NULL; | |
| 328 ASSERT_NO_FATAL_FAILURE(FindOneOtherBrowser(&new_browser)); | |
| 329 | |
| 330 // Expect an app panel. | |
| 331 EXPECT_TRUE(new_browser->is_type_panel() && new_browser->is_app()); | |
| 332 | |
| 333 // The new browser's app_name should include the app's ID. | |
| 334 EXPECT_NE( | |
| 335 new_browser->app_name_.find(extension_app->id()), | |
| 336 std::string::npos) << new_browser->app_name_; | |
| 337 } | |
| 338 | |
| 339 #endif // !defined(OS_MACOSX) | |
| 340 | |
| 341 #endif // !defined(OS_CHROMEOS) | |
| 342 | |
| 343 IN_PROC_BROWSER_TEST_F(BrowserInitTest, ReadingWasRestartedAfterRestart) { | |
| 344 // Tests that BrowserInit::WasRestarted reads and resets the preference | |
| 345 // kWasRestarted correctly. | |
| 346 BrowserInit::was_restarted_read_ = false; | |
| 347 PrefService* pref_service = g_browser_process->local_state(); | |
| 348 pref_service->SetBoolean(prefs::kWasRestarted, true); | |
| 349 EXPECT_TRUE(BrowserInit::WasRestarted()); | |
| 350 EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted)); | |
| 351 EXPECT_TRUE(BrowserInit::WasRestarted()); | |
| 352 } | |
| 353 | |
| 354 IN_PROC_BROWSER_TEST_F(BrowserInitTest, ReadingWasRestartedAfterNormalStart) { | |
| 355 // Tests that BrowserInit::WasRestarted reads and resets the preference | |
| 356 // kWasRestarted correctly. | |
| 357 BrowserInit::was_restarted_read_ = false; | |
| 358 PrefService* pref_service = g_browser_process->local_state(); | |
| 359 pref_service->SetBoolean(prefs::kWasRestarted, false); | |
| 360 EXPECT_FALSE(BrowserInit::WasRestarted()); | |
| 361 EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted)); | |
| 362 EXPECT_FALSE(BrowserInit::WasRestarted()); | |
| 363 } | |
| 364 | |
| 365 #if !defined(OS_CHROMEOS) | |
| 366 IN_PROC_BROWSER_TEST_F(BrowserInitTest, StartupURLsForTwoProfiles) { | |
| 367 Profile* default_profile = browser()->profile(); | |
| 368 | |
| 369 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 370 // Create another profile. | |
| 371 FilePath dest_path = profile_manager->user_data_dir(); | |
| 372 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile 1")); | |
| 373 | |
| 374 Profile* other_profile = profile_manager->GetProfile(dest_path); | |
| 375 ASSERT_TRUE(other_profile); | |
| 376 | |
| 377 // Use a couple arbitrary URLs. | |
| 378 std::vector<GURL> urls1; | |
| 379 urls1.push_back(ui_test_utils::GetTestUrl( | |
| 380 FilePath(FilePath::kCurrentDirectory), | |
| 381 FilePath(FILE_PATH_LITERAL("title1.html")))); | |
| 382 std::vector<GURL> urls2; | |
| 383 urls2.push_back(ui_test_utils::GetTestUrl( | |
| 384 FilePath(FilePath::kCurrentDirectory), | |
| 385 FilePath(FILE_PATH_LITERAL("title2.html")))); | |
| 386 | |
| 387 // Set different startup preferences for the 2 profiles. | |
| 388 SessionStartupPref pref1(SessionStartupPref::URLS); | |
| 389 pref1.urls = urls1; | |
| 390 SessionStartupPref::SetStartupPref(default_profile, pref1); | |
| 391 SessionStartupPref pref2(SessionStartupPref::URLS); | |
| 392 pref2.urls = urls2; | |
| 393 SessionStartupPref::SetStartupPref(other_profile, pref2); | |
| 394 | |
| 395 // Close the browser. | |
| 396 browser()->window()->Close(); | |
| 397 | |
| 398 // Do a simple non-process-startup browser launch. | |
| 399 CommandLine dummy(CommandLine::NO_PROGRAM); | |
| 400 | |
| 401 int return_code; | |
| 402 BrowserInit browser_init; | |
| 403 std::vector<Profile*> last_opened_profiles; | |
| 404 last_opened_profiles.push_back(default_profile); | |
| 405 last_opened_profiles.push_back(other_profile); | |
| 406 browser_init.Start(dummy, profile_manager->user_data_dir(), default_profile, | |
| 407 last_opened_profiles, &return_code); | |
| 408 | |
| 409 // urls1 were opened in a browser for default_profile, and urls2 were opened | |
| 410 // in a browser for other_profile. | |
| 411 Browser* new_browser = NULL; | |
| 412 // |browser()| is still around at this point, even though we've closed its | |
| 413 // window. Thus the browser count for default_profile is 2. | |
| 414 ASSERT_EQ(2u, BrowserList::GetBrowserCount(default_profile)); | |
| 415 new_browser = FindOneOtherBrowserForProfile(default_profile, browser()); | |
| 416 ASSERT_TRUE(new_browser); | |
| 417 ASSERT_EQ(1, new_browser->tab_count()); | |
| 418 EXPECT_EQ(urls1[0], new_browser->GetWebContentsAt(0)->GetURL()); | |
| 419 | |
| 420 ASSERT_EQ(1u, BrowserList::GetBrowserCount(other_profile)); | |
| 421 new_browser = FindOneOtherBrowserForProfile(other_profile, NULL); | |
| 422 ASSERT_TRUE(new_browser); | |
| 423 ASSERT_EQ(1, new_browser->tab_count()); | |
| 424 EXPECT_EQ(urls2[0], new_browser->GetWebContentsAt(0)->GetURL()); | |
| 425 } | |
| 426 | |
| 427 IN_PROC_BROWSER_TEST_F(BrowserInitTest, UpdateWithTwoProfiles) { | |
| 428 // Make BrowserInit::WasRestarted() return true. | |
| 429 BrowserInit::was_restarted_read_ = false; | |
| 430 PrefService* pref_service = g_browser_process->local_state(); | |
| 431 pref_service->SetBoolean(prefs::kWasRestarted, true); | |
| 432 | |
| 433 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 434 | |
| 435 // Create two profiles. | |
| 436 FilePath dest_path = profile_manager->user_data_dir(); | |
| 437 | |
| 438 Profile* profile1 = profile_manager->GetProfile( | |
| 439 dest_path.Append(FILE_PATH_LITERAL("New Profile 1"))); | |
| 440 ASSERT_TRUE(profile1); | |
| 441 | |
| 442 Profile* profile2 = profile_manager->GetProfile( | |
| 443 dest_path.Append(FILE_PATH_LITERAL("New Profile 2"))); | |
| 444 ASSERT_TRUE(profile2); | |
| 445 | |
| 446 // Use a couple arbitrary URLs. | |
| 447 std::vector<GURL> urls1; | |
| 448 urls1.push_back(ui_test_utils::GetTestUrl( | |
| 449 FilePath(FilePath::kCurrentDirectory), | |
| 450 FilePath(FILE_PATH_LITERAL("title1.html")))); | |
| 451 std::vector<GURL> urls2; | |
| 452 urls2.push_back(ui_test_utils::GetTestUrl( | |
| 453 FilePath(FilePath::kCurrentDirectory), | |
| 454 FilePath(FILE_PATH_LITERAL("title2.html")))); | |
| 455 | |
| 456 // Set different startup preferences for the 2 profiles. | |
| 457 SessionStartupPref pref1(SessionStartupPref::URLS); | |
| 458 pref1.urls = urls1; | |
| 459 SessionStartupPref::SetStartupPref(profile1, pref1); | |
| 460 SessionStartupPref pref2(SessionStartupPref::URLS); | |
| 461 pref2.urls = urls2; | |
| 462 SessionStartupPref::SetStartupPref(profile2, pref2); | |
| 463 | |
| 464 // Simulate a launch after a browser update. | |
| 465 CommandLine dummy(CommandLine::NO_PROGRAM); | |
| 466 int return_code; | |
| 467 BrowserInit browser_init; | |
| 468 std::vector<Profile*> last_opened_profiles; | |
| 469 last_opened_profiles.push_back(profile1); | |
| 470 last_opened_profiles.push_back(profile2); | |
| 471 browser_init.Start(dummy, profile_manager->user_data_dir(), profile1, | |
| 472 last_opened_profiles, &return_code); | |
| 473 | |
| 474 while (SessionRestore::IsRestoring(profile1) || | |
| 475 SessionRestore::IsRestoring(profile2)) | |
| 476 MessageLoop::current()->RunAllPending(); | |
| 477 | |
| 478 // The startup URLs are ignored, and instead the last open sessions are | |
| 479 // restored. | |
| 480 EXPECT_TRUE(profile1->restored_last_session()); | |
| 481 EXPECT_TRUE(profile2->restored_last_session()); | |
| 482 | |
| 483 Browser* new_browser = NULL; | |
| 484 ASSERT_EQ(1u, BrowserList::GetBrowserCount(profile1)); | |
| 485 new_browser = FindOneOtherBrowserForProfile(profile1, NULL); | |
| 486 ASSERT_TRUE(new_browser); | |
| 487 ASSERT_EQ(1, new_browser->tab_count()); | |
| 488 EXPECT_EQ(GURL(chrome::kAboutBlankURL), | |
| 489 new_browser->GetWebContentsAt(0)->GetURL()); | |
| 490 | |
| 491 ASSERT_EQ(1u, BrowserList::GetBrowserCount(profile2)); | |
| 492 new_browser = FindOneOtherBrowserForProfile(profile2, NULL); | |
| 493 ASSERT_TRUE(new_browser); | |
| 494 ASSERT_EQ(1, new_browser->tab_count()); | |
| 495 EXPECT_EQ(GURL(chrome::kAboutBlankURL), | |
| 496 new_browser->GetWebContentsAt(0)->GetURL()); | |
| 497 } | |
| 498 | |
| 499 IN_PROC_BROWSER_TEST_F(BrowserInitTest, ProfilesWithoutPagesNotLaunched) { | |
| 500 Profile* default_profile = browser()->profile(); | |
| 501 | |
| 502 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 503 | |
| 504 // Create 4 more profiles. | |
| 505 FilePath dest_path1 = profile_manager->user_data_dir().Append( | |
| 506 FILE_PATH_LITERAL("New Profile 1")); | |
| 507 FilePath dest_path2 = profile_manager->user_data_dir().Append( | |
| 508 FILE_PATH_LITERAL("New Profile 2")); | |
| 509 FilePath dest_path3 = profile_manager->user_data_dir().Append( | |
| 510 FILE_PATH_LITERAL("New Profile 3")); | |
| 511 FilePath dest_path4 = profile_manager->user_data_dir().Append( | |
| 512 FILE_PATH_LITERAL("New Profile 4")); | |
| 513 | |
| 514 Profile* profile_home1 = profile_manager->GetProfile(dest_path1); | |
| 515 ASSERT_TRUE(profile_home1); | |
| 516 Profile* profile_home2 = profile_manager->GetProfile(dest_path2); | |
| 517 ASSERT_TRUE(profile_home2); | |
| 518 Profile* profile_last = profile_manager->GetProfile(dest_path3); | |
| 519 ASSERT_TRUE(profile_last); | |
| 520 Profile* profile_urls = profile_manager->GetProfile(dest_path4); | |
| 521 ASSERT_TRUE(profile_urls); | |
| 522 | |
| 523 // Set the profiles to open urls, open last visited pages or display the home | |
| 524 // page. | |
| 525 SessionStartupPref pref_home(SessionStartupPref::DEFAULT); | |
| 526 SessionStartupPref::SetStartupPref(profile_home1, pref_home); | |
| 527 SessionStartupPref::SetStartupPref(profile_home2, pref_home); | |
| 528 | |
| 529 SessionStartupPref pref_last(SessionStartupPref::LAST); | |
| 530 SessionStartupPref::SetStartupPref(profile_last, pref_last); | |
| 531 | |
| 532 std::vector<GURL> urls; | |
| 533 urls.push_back(ui_test_utils::GetTestUrl( | |
| 534 FilePath(FilePath::kCurrentDirectory), | |
| 535 FilePath(FILE_PATH_LITERAL("title1.html")))); | |
| 536 | |
| 537 SessionStartupPref pref_urls(SessionStartupPref::URLS); | |
| 538 pref_urls.urls = urls; | |
| 539 SessionStartupPref::SetStartupPref(profile_urls, pref_urls); | |
| 540 | |
| 541 // Close the browser. | |
| 542 browser()->window()->Close(); | |
| 543 | |
| 544 // Do a simple non-process-startup browser launch. | |
| 545 CommandLine dummy(CommandLine::NO_PROGRAM); | |
| 546 | |
| 547 int return_code; | |
| 548 BrowserInit browser_init; | |
| 549 std::vector<Profile*> last_opened_profiles; | |
| 550 last_opened_profiles.push_back(profile_home1); | |
| 551 last_opened_profiles.push_back(profile_home2); | |
| 552 last_opened_profiles.push_back(profile_last); | |
| 553 last_opened_profiles.push_back(profile_urls); | |
| 554 browser_init.Start(dummy, profile_manager->user_data_dir(), profile_home1, | |
| 555 last_opened_profiles, &return_code); | |
| 556 | |
| 557 | |
| 558 while (SessionRestore::IsRestoring(default_profile) || | |
| 559 SessionRestore::IsRestoring(profile_home1) || | |
| 560 SessionRestore::IsRestoring(profile_home2) || | |
| 561 SessionRestore::IsRestoring(profile_last) || | |
| 562 SessionRestore::IsRestoring(profile_urls)) | |
| 563 MessageLoop::current()->RunAllPending(); | |
| 564 | |
| 565 Browser* new_browser = NULL; | |
| 566 // The last open profile (the profile_home1 in this case) will always be | |
| 567 // launched, even if it will open just the home page. | |
| 568 ASSERT_EQ(1u, BrowserList::GetBrowserCount(profile_home1)); | |
| 569 new_browser = FindOneOtherBrowserForProfile(profile_home1, NULL); | |
| 570 ASSERT_TRUE(new_browser); | |
| 571 ASSERT_EQ(1, new_browser->tab_count()); | |
| 572 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), | |
| 573 new_browser->GetWebContentsAt(0)->GetURL()); | |
| 574 | |
| 575 // profile_urls opened the urls. | |
| 576 ASSERT_EQ(1u, BrowserList::GetBrowserCount(profile_urls)); | |
| 577 new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL); | |
| 578 ASSERT_TRUE(new_browser); | |
| 579 ASSERT_EQ(1, new_browser->tab_count()); | |
| 580 EXPECT_EQ(urls[0], new_browser->GetWebContentsAt(0)->GetURL()); | |
| 581 | |
| 582 // profile_last opened the last open pages. | |
| 583 ASSERT_EQ(1u, BrowserList::GetBrowserCount(profile_last)); | |
| 584 new_browser = FindOneOtherBrowserForProfile(profile_last, NULL); | |
| 585 ASSERT_TRUE(new_browser); | |
| 586 ASSERT_EQ(1, new_browser->tab_count()); | |
| 587 EXPECT_EQ(GURL(chrome::kAboutBlankURL), | |
| 588 new_browser->GetWebContentsAt(0)->GetURL()); | |
| 589 | |
| 590 // profile_home2 was not launched since it would've only opened the home page. | |
| 591 ASSERT_EQ(0u, BrowserList::GetBrowserCount(profile_home2)); | |
| 592 } | |
| 593 | |
| 594 IN_PROC_BROWSER_TEST_F(BrowserInitTest, ProfilesLaunchedAfterCrash) { | |
| 595 // After an unclean exit, all profiles will be launched. However, they won't | |
| 596 // open any pages automatically. | |
| 597 | |
| 598 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 599 | |
| 600 // Create 3 profiles. | |
| 601 FilePath dest_path1 = profile_manager->user_data_dir().Append( | |
| 602 FILE_PATH_LITERAL("New Profile 1")); | |
| 603 FilePath dest_path2 = profile_manager->user_data_dir().Append( | |
| 604 FILE_PATH_LITERAL("New Profile 2")); | |
| 605 FilePath dest_path3 = profile_manager->user_data_dir().Append( | |
| 606 FILE_PATH_LITERAL("New Profile 3")); | |
| 607 | |
| 608 Profile* profile_home = profile_manager->GetProfile(dest_path1); | |
| 609 ASSERT_TRUE(profile_home); | |
| 610 Profile* profile_last = profile_manager->GetProfile(dest_path2); | |
| 611 ASSERT_TRUE(profile_last); | |
| 612 Profile* profile_urls = profile_manager->GetProfile(dest_path3); | |
| 613 ASSERT_TRUE(profile_urls); | |
| 614 | |
| 615 // Set the profiles to open the home page, last visited pages or URLs. | |
| 616 SessionStartupPref pref_home(SessionStartupPref::DEFAULT); | |
| 617 SessionStartupPref::SetStartupPref(profile_home, pref_home); | |
| 618 | |
| 619 SessionStartupPref pref_last(SessionStartupPref::LAST); | |
| 620 SessionStartupPref::SetStartupPref(profile_last, pref_last); | |
| 621 | |
| 622 std::vector<GURL> urls; | |
| 623 urls.push_back(ui_test_utils::GetTestUrl( | |
| 624 FilePath(FilePath::kCurrentDirectory), | |
| 625 FilePath(FILE_PATH_LITERAL("title1.html")))); | |
| 626 | |
| 627 SessionStartupPref pref_urls(SessionStartupPref::URLS); | |
| 628 pref_urls.urls = urls; | |
| 629 SessionStartupPref::SetStartupPref(profile_urls, pref_urls); | |
| 630 | |
| 631 // Simulate a launch after an unclear exit. | |
| 632 browser()->window()->Close(); | |
| 633 static_cast<ProfileImpl*>(profile_home)->last_session_exited_cleanly_ = false; | |
| 634 static_cast<ProfileImpl*>(profile_last)->last_session_exited_cleanly_ = false; | |
| 635 static_cast<ProfileImpl*>(profile_urls)->last_session_exited_cleanly_ = false; | |
| 636 | |
| 637 CommandLine dummy(CommandLine::NO_PROGRAM); | |
| 638 int return_code; | |
| 639 BrowserInit browser_init; | |
| 640 std::vector<Profile*> last_opened_profiles; | |
| 641 last_opened_profiles.push_back(profile_home); | |
| 642 last_opened_profiles.push_back(profile_last); | |
| 643 last_opened_profiles.push_back(profile_urls); | |
| 644 browser_init.Start(dummy, profile_manager->user_data_dir(), profile_home, | |
| 645 last_opened_profiles, &return_code); | |
| 646 | |
| 647 // No profiles are getting restored, since they all display the crash info | |
| 648 // bar. | |
| 649 EXPECT_FALSE(SessionRestore::IsRestoring(profile_home)); | |
| 650 EXPECT_FALSE(SessionRestore::IsRestoring(profile_last)); | |
| 651 EXPECT_FALSE(SessionRestore::IsRestoring(profile_urls)); | |
| 652 | |
| 653 // The profile which normally opens the home page displays the new tab page. | |
| 654 Browser* new_browser = NULL; | |
| 655 ASSERT_EQ(1u, BrowserList::GetBrowserCount(profile_home)); | |
| 656 new_browser = FindOneOtherBrowserForProfile(profile_home, NULL); | |
| 657 ASSERT_TRUE(new_browser); | |
| 658 ASSERT_EQ(1, new_browser->tab_count()); | |
| 659 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), | |
| 660 new_browser->GetWebContentsAt(0)->GetURL()); | |
| 661 EXPECT_EQ(1U, new_browser->GetTabContentsWrapperAt(0)->infobar_tab_helper()-> | |
| 662 infobar_count()); | |
| 663 | |
| 664 // The profile which normally opens last open pages displays the new tab page. | |
| 665 ASSERT_EQ(1u, BrowserList::GetBrowserCount(profile_last)); | |
| 666 new_browser = FindOneOtherBrowserForProfile(profile_last, NULL); | |
| 667 ASSERT_TRUE(new_browser); | |
| 668 ASSERT_EQ(1, new_browser->tab_count()); | |
| 669 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), | |
| 670 new_browser->GetWebContentsAt(0)->GetURL()); | |
| 671 EXPECT_EQ(1U, new_browser->GetTabContentsWrapperAt(0)->infobar_tab_helper()-> | |
| 672 infobar_count()); | |
| 673 | |
| 674 // The profile which normally opens URLs displays the new tab page. | |
| 675 ASSERT_EQ(1u, BrowserList::GetBrowserCount(profile_urls)); | |
| 676 new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL); | |
| 677 ASSERT_TRUE(new_browser); | |
| 678 ASSERT_EQ(1, new_browser->tab_count()); | |
| 679 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), | |
| 680 new_browser->GetWebContentsAt(0)->GetURL()); | |
| 681 EXPECT_EQ(1U, new_browser->GetTabContentsWrapperAt(0)->infobar_tab_helper()-> | |
| 682 infobar_count()); | |
| 683 } | |
| 684 #endif // !OS_CHROMEOS | |
| OLD | NEW |