Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Thomas Lukaszewicz | 8fe9687d | 2022-08-29 17:56:22 | [diff] [blame] | 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/containers/contains.h" |
| 6 | #include "base/run_loop.h" |
| 7 | #include "base/test/bind.h" |
| 8 | #include "base/test/scoped_feature_list.h" |
| 9 | #include "chrome/browser/lacros/browser_service_lacros.h" |
| 10 | #include "chrome/browser/lifetime/application_lifetime.h" |
| 11 | #include "chrome/browser/prefs/session_startup_pref.h" |
| 12 | #include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.h" |
| 13 | #include "chrome/browser/profiles/keep_alive/scoped_profile_keep_alive.h" |
| 14 | #include "chrome/browser/profiles/profile_manager.h" |
| 15 | #include "chrome/browser/profiles/profile_test_util.h" |
| 16 | #include "chrome/browser/sessions/exit_type_service.h" |
| 17 | #include "chrome/browser/sessions/session_restore.h" |
| 18 | #include "chrome/browser/sessions/session_restore_test_utils.h" |
| 19 | #include "chrome/browser/ui/browser_commands.h" |
| 20 | #include "chrome/browser/ui/browser_finder.h" |
| 21 | #include "chrome/browser/ui/browser_list.h" |
| 22 | #include "chrome/browser/ui/browser_window.h" |
| 23 | #include "chrome/browser/ui/profile_picker.h" |
| 24 | #include "chrome/browser/ui/profile_ui_test_utils.h" |
| 25 | #include "chrome/browser/ui/views/session_crashed_bubble_view.h" |
Thomas Lukaszewicz | 28091974 | 2022-08-30 03:07:18 | [diff] [blame] | 26 | #include "chrome/browser/ui/web_applications/app_browser_controller.h" |
| 27 | #include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h" |
| 28 | #include "chrome/browser/web_applications/test/web_app_install_test_utils.h" |
| 29 | #include "chrome/browser/web_applications/web_app_install_info.h" |
Eric Willigers | 928fed2 | 2022-09-12 07:03:54 | [diff] [blame] | 30 | #include "chrome/browser/web_applications/web_app_provider.h" |
| 31 | #include "chrome/browser/web_applications/web_app_registrar.h" |
Thomas Lukaszewicz | 8fe9687d | 2022-08-29 17:56:22 | [diff] [blame] | 32 | #include "chrome/common/chrome_switches.h" |
| 33 | #include "chrome/common/pref_names.h" |
| 34 | #include "chrome/test/base/in_process_browser_test.h" |
| 35 | #include "chrome/test/base/testing_browser_process.h" |
| 36 | #include "chrome/test/base/ui_test_utils.h" |
| 37 | #include "components/keep_alive_registry/keep_alive_types.h" |
| 38 | #include "components/keep_alive_registry/scoped_keep_alive.h" |
| 39 | #include "content/public/test/browser_test.h" |
| 40 | #include "content/public/test/browser_test_utils.h" |
| 41 | #include "testing/gtest/include/gtest/gtest.h" |
Mitsuru Oshima | 2d455aa2c | 2022-10-16 23:29:37 | [diff] [blame] | 42 | #include "ui/display/screen.h" |
Thomas Lukaszewicz | 8fe9687d | 2022-08-29 17:56:22 | [diff] [blame] | 43 | |
Thomas Lukaszewicz | 28091974 | 2022-08-30 03:07:18 | [diff] [blame] | 44 | namespace { |
| 45 | |
| 46 | web_app::AppId InstallPWA(Profile* profile, const GURL& start_url) { |
| 47 | auto web_app_info = std::make_unique<WebAppInstallInfo>(); |
| 48 | web_app_info->start_url = start_url; |
| 49 | web_app_info->scope = start_url.GetWithoutFilename(); |
liqining.keiling | 5a8e27f2 | 2023-01-01 17:55:47 | [diff] [blame^] | 50 | web_app_info->user_display_mode = |
| 51 | web_app::mojom::UserDisplayMode::kStandalone; |
Thomas Lukaszewicz | 28091974 | 2022-08-30 03:07:18 | [diff] [blame] | 52 | web_app_info->title = u"A Web App"; |
| 53 | return web_app::test::InstallWebApp(profile, std::move(web_app_info)); |
| 54 | } |
| 55 | |
| 56 | } // namespace |
| 57 | |
Thomas Lukaszewicz | 8fe9687d | 2022-08-29 17:56:22 | [diff] [blame] | 58 | class BrowserLauncherTest : public InProcessBrowserTest { |
| 59 | public: |
| 60 | BrowserLauncherTest() { |
| 61 | // Suppress the test-created about blank tab to be more representative of |
| 62 | // the startup and launch environment for testing. |
| 63 | set_open_about_blank_on_browser_launch(false); |
| 64 | } |
| 65 | BrowserLauncherTest(const BrowserLauncherTest&) = delete; |
| 66 | BrowserLauncherTest& operator=(const BrowserLauncherTest&) = delete; |
| 67 | ~BrowserLauncherTest() override = default; |
| 68 | |
| 69 | // InProcessBrowserTest: |
| 70 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 71 | InProcessBrowserTest::SetUpCommandLine(command_line); |
| 72 | // The kNoStartupWindow is applied when launching lacros-chrome with the |
| 73 | // kDoNotOpenWindow initial browser action. |
| 74 | command_line->AppendSwitch(switches::kNoStartupWindow); |
| 75 | } |
| 76 | void SetUpOnMainThread() override { |
| 77 | InProcessBrowserTest::SetUpOnMainThread(); |
| 78 | browser_service_ = std::make_unique<BrowserServiceLacros>(); |
| 79 | } |
Eric Willigers | 928fed2 | 2022-09-12 07:03:54 | [diff] [blame] | 80 | void TearDownOnMainThread() override { |
| 81 | if (!skip_uninstall_) { |
| 82 | UninstallWebApps(); |
| 83 | } |
| 84 | InProcessBrowserTest::TearDownOnMainThread(); |
| 85 | } |
Thomas Lukaszewicz | 8fe9687d | 2022-08-29 17:56:22 | [diff] [blame] | 86 | |
| 87 | void NewWindowSync(bool incognito, bool should_trigger_session_restore) { |
| 88 | base::RunLoop run_loop; |
Mitsuru Oshima | 2d455aa2c | 2022-10-16 23:29:37 | [diff] [blame] | 89 | browser_service()->NewWindow( |
| 90 | incognito, should_trigger_session_restore, |
| 91 | display::Screen::GetScreen()->GetDisplayForNewWindows().id(), |
| 92 | run_loop.QuitClosure()); |
Thomas Lukaszewicz | 8fe9687d | 2022-08-29 17:56:22 | [diff] [blame] | 93 | run_loop.Run(); |
| 94 | } |
| 95 | |
| 96 | void DisableWelcomePages(const std::vector<Profile*>& profiles) { |
| 97 | for (Profile* profile : profiles) |
| 98 | profile->GetPrefs()->SetBoolean(prefs::kHasSeenWelcomePage, true); |
| 99 | |
| 100 | // Also disable What's New. |
| 101 | PrefService* pref_service = g_browser_process->local_state(); |
| 102 | pref_service->SetInteger(prefs::kLastWhatsNewVersion, CHROME_VERSION_MAJOR); |
| 103 | } |
| 104 | |
| 105 | // Creates a new profile with a URLS startup preference and an open tab. |
| 106 | void SetupSingleProfileWithURLSPreference() { |
| 107 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 108 | |
| 109 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 110 | |
| 111 | // Create two profiles. |
| 112 | base::FilePath dest_path = profile_manager->user_data_dir(); |
| 113 | |
| 114 | Profile* profile = nullptr; |
| 115 | { |
| 116 | base::ScopedAllowBlockingForTesting allow_blocking; |
| 117 | profile = profile_manager->GetProfile( |
| 118 | dest_path.Append(FILE_PATH_LITERAL("New Profile"))); |
| 119 | ASSERT_TRUE(profile); |
| 120 | } |
| 121 | DisableWelcomePages({profile}); |
| 122 | |
| 123 | // Don't delete Profile too early. |
| 124 | ScopedProfileKeepAlive profile_keep_alive( |
| 125 | profile, ProfileKeepAliveOrigin::kBrowserWindow); |
| 126 | |
| 127 | // Open some urls in the browser. |
| 128 | Browser* browser = Browser::Create( |
| 129 | Browser::CreateParams(Browser::TYPE_NORMAL, profile, true)); |
| 130 | chrome::NewTab(browser); |
| 131 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 132 | browser, embedded_test_server()->GetURL("/empty.html"))); |
| 133 | |
| 134 | // Establish the startup preference. |
| 135 | std::vector<GURL> urls; |
| 136 | urls.push_back(ui_test_utils::GetTestUrl( |
| 137 | base::FilePath(base::FilePath::kCurrentDirectory), |
| 138 | base::FilePath(FILE_PATH_LITERAL("title1.html")))); |
| 139 | urls.push_back(ui_test_utils::GetTestUrl( |
| 140 | base::FilePath(base::FilePath::kCurrentDirectory), |
| 141 | base::FilePath(FILE_PATH_LITERAL("title2.html")))); |
| 142 | |
| 143 | // Set different startup preferences for the profile. |
| 144 | SessionStartupPref pref(SessionStartupPref::URLS); |
| 145 | pref.urls = urls; |
| 146 | SessionStartupPref::SetStartupPref(profile, pref); |
| 147 | profile->GetPrefs()->CommitPendingWrite(); |
| 148 | |
| 149 | // Ensure the session ends with the profile created above. |
| 150 | auto last_opened_profiles = |
| 151 | g_browser_process->profile_manager()->GetLastOpenedProfiles(); |
| 152 | EXPECT_EQ(1u, last_opened_profiles.size()); |
| 153 | EXPECT_TRUE(base::Contains(last_opened_profiles, profile)); |
| 154 | } |
| 155 | |
| 156 | BrowserServiceLacros* browser_service() const { |
| 157 | return browser_service_.get(); |
| 158 | } |
| 159 | |
Eric Willigers | 928fed2 | 2022-09-12 07:03:54 | [diff] [blame] | 160 | GURL GetWebAppStartUrl() const { |
| 161 | return GURL("https://lacros.example.com/example/index"); |
| 162 | } |
| 163 | |
| 164 | void SetSkipUninstall(bool value) { skip_uninstall_ = value; } |
| 165 | |
Thomas Lukaszewicz | 8fe9687d | 2022-08-29 17:56:22 | [diff] [blame] | 166 | private: |
Eric Willigers | 928fed2 | 2022-09-12 07:03:54 | [diff] [blame] | 167 | void UninstallWebApps() { |
| 168 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 169 | auto* profile = profile_manager->GetProfile( |
| 170 | profile_manager->GetPrimaryUserProfilePath()); |
| 171 | |
| 172 | web_app::WebAppRegistrar& registrar = |
Camden King | 849ff25b | 2022-12-08 20:17:55 | [diff] [blame] | 173 | web_app::WebAppProvider::GetForTest(profile)->registrar_unsafe(); |
Eric Willigers | 928fed2 | 2022-09-12 07:03:54 | [diff] [blame] | 174 | for (auto& app_id : registrar.GetAppIds()) { |
| 175 | web_app::test::UninstallWebApp(profile, app_id); |
| 176 | } |
| 177 | } |
| 178 | |
Thomas Lukaszewicz | 8fe9687d | 2022-08-29 17:56:22 | [diff] [blame] | 179 | std::unique_ptr<BrowserServiceLacros> browser_service_; |
Eric Willigers | 928fed2 | 2022-09-12 07:03:54 | [diff] [blame] | 180 | bool skip_uninstall_ = false; |
Thomas Lukaszewicz | 8fe9687d | 2022-08-29 17:56:22 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | IN_PROC_BROWSER_TEST_F(BrowserLauncherTest, PRE_FullRestoreWithTwoProfiles) { |
| 184 | // Simulate a full restore by creating the profiles in a PRE_ test. |
| 185 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 186 | |
| 187 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 188 | |
| 189 | // Create two profiles. |
| 190 | base::FilePath dest_path = profile_manager->user_data_dir(); |
| 191 | |
| 192 | Profile* profile1 = nullptr; |
| 193 | Profile* profile2 = nullptr; |
| 194 | { |
| 195 | base::ScopedAllowBlockingForTesting allow_blocking; |
| 196 | profile1 = profile_manager->GetProfile( |
| 197 | dest_path.Append(FILE_PATH_LITERAL("New Profile 1"))); |
| 198 | ASSERT_TRUE(profile1); |
| 199 | |
| 200 | profile2 = profile_manager->GetProfile( |
| 201 | dest_path.Append(FILE_PATH_LITERAL("New Profile 2"))); |
| 202 | ASSERT_TRUE(profile2); |
| 203 | } |
| 204 | DisableWelcomePages({profile1, profile2}); |
| 205 | |
| 206 | // Don't delete Profiles too early. |
| 207 | ScopedProfileKeepAlive profile1_keep_alive( |
| 208 | profile1, ProfileKeepAliveOrigin::kBrowserWindow); |
| 209 | ScopedProfileKeepAlive profile2_keep_alive( |
| 210 | profile2, ProfileKeepAliveOrigin::kBrowserWindow); |
| 211 | |
| 212 | // Open some urls with the browsers, and close them. |
| 213 | Browser* browser1 = Browser::Create( |
| 214 | Browser::CreateParams(Browser::TYPE_NORMAL, profile1, true)); |
| 215 | chrome::NewTab(browser1); |
| 216 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 217 | browser1, embedded_test_server()->GetURL("/empty.html"))); |
| 218 | |
| 219 | Browser* browser2 = Browser::Create( |
| 220 | Browser::CreateParams(Browser::TYPE_NORMAL, profile2, true)); |
| 221 | chrome::NewTab(browser2); |
| 222 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 223 | browser2, embedded_test_server()->GetURL("/form.html"))); |
| 224 | |
| 225 | // Set different startup preferences for the 2 profiles. |
| 226 | std::vector<GURL> urls1; |
| 227 | urls1.push_back(ui_test_utils::GetTestUrl( |
| 228 | base::FilePath(base::FilePath::kCurrentDirectory), |
| 229 | base::FilePath(FILE_PATH_LITERAL("title1.html")))); |
| 230 | std::vector<GURL> urls2; |
| 231 | urls2.push_back(ui_test_utils::GetTestUrl( |
| 232 | base::FilePath(base::FilePath::kCurrentDirectory), |
| 233 | base::FilePath(FILE_PATH_LITERAL("title2.html")))); |
| 234 | |
| 235 | // Set different startup preferences for the 2 profiles. |
| 236 | SessionStartupPref pref1(SessionStartupPref::URLS); |
| 237 | pref1.urls = urls1; |
| 238 | SessionStartupPref::SetStartupPref(profile1, pref1); |
| 239 | SessionStartupPref pref2(SessionStartupPref::URLS); |
| 240 | pref2.urls = urls2; |
| 241 | SessionStartupPref::SetStartupPref(profile2, pref2); |
| 242 | |
| 243 | profile1->GetPrefs()->CommitPendingWrite(); |
| 244 | profile2->GetPrefs()->CommitPendingWrite(); |
| 245 | |
| 246 | // Ensure the session ends with the above two profiles. |
| 247 | auto last_opened_profiles = |
| 248 | g_browser_process->profile_manager()->GetLastOpenedProfiles(); |
| 249 | EXPECT_EQ(2u, last_opened_profiles.size()); |
| 250 | EXPECT_TRUE(base::Contains(last_opened_profiles, profile1)); |
| 251 | EXPECT_TRUE(base::Contains(last_opened_profiles, profile2)); |
| 252 | } |
| 253 | |
| 254 | IN_PROC_BROWSER_TEST_F(BrowserLauncherTest, FullRestoreWithTwoProfiles) { |
| 255 | // Browser launch should be suppressed with the kNoStartupWindow switch. |
| 256 | ASSERT_FALSE(browser()); |
| 257 | |
| 258 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 259 | |
| 260 | // Open the two profiles. |
| 261 | base::FilePath dest_path = profile_manager->user_data_dir(); |
| 262 | |
| 263 | Profile* profile1 = nullptr; |
| 264 | Profile* profile2 = nullptr; |
| 265 | { |
| 266 | base::ScopedAllowBlockingForTesting allow_blocking; |
| 267 | profile1 = profile_manager->GetProfile( |
| 268 | dest_path.Append(FILE_PATH_LITERAL("New Profile 1"))); |
| 269 | ASSERT_TRUE(profile1); |
| 270 | |
| 271 | profile2 = profile_manager->GetProfile( |
| 272 | dest_path.Append(FILE_PATH_LITERAL("New Profile 2"))); |
| 273 | ASSERT_TRUE(profile2); |
| 274 | } |
| 275 | |
| 276 | // The profiles to be restored should match those setup in the PRE_ test. |
| 277 | auto last_opened_profiles = |
| 278 | g_browser_process->profile_manager()->GetLastOpenedProfiles(); |
| 279 | EXPECT_EQ(2u, last_opened_profiles.size()); |
| 280 | EXPECT_TRUE(base::Contains(last_opened_profiles, profile1)); |
| 281 | EXPECT_TRUE(base::Contains(last_opened_profiles, profile2)); |
| 282 | |
| 283 | // Trigger Lacros full restore. |
| 284 | base::RunLoop run_loop; |
| 285 | testing::SessionsRestoredWaiter restore_waiter(run_loop.QuitClosure(), 2); |
| 286 | browser_service()->OpenForFullRestore(/*skip_crash_restore=*/true); |
| 287 | run_loop.Run(); |
| 288 | |
| 289 | // The startup URLs are ignored, and instead the last open sessions are |
| 290 | // restored. |
| 291 | EXPECT_TRUE(profile1->restored_last_session()); |
| 292 | EXPECT_TRUE(profile2->restored_last_session()); |
| 293 | |
| 294 | Browser* new_browser = nullptr; |
| 295 | ASSERT_EQ(1u, chrome::GetBrowserCount(profile1)); |
| 296 | new_browser = chrome::FindBrowserWithProfile(profile1); |
| 297 | ASSERT_TRUE(new_browser); |
| 298 | TabStripModel* tab_strip = new_browser->tab_strip_model(); |
| 299 | ASSERT_EQ(1, tab_strip->count()); |
| 300 | EXPECT_EQ("/empty.html", |
| 301 | tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path()); |
| 302 | |
| 303 | ASSERT_EQ(1u, chrome::GetBrowserCount(profile2)); |
| 304 | new_browser = chrome::FindBrowserWithProfile(profile2); |
| 305 | ASSERT_TRUE(new_browser); |
| 306 | tab_strip = new_browser->tab_strip_model(); |
| 307 | ASSERT_EQ(1, tab_strip->count()); |
| 308 | EXPECT_EQ("/form.html", |
| 309 | tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path()); |
| 310 | } |
| 311 | |
| 312 | IN_PROC_BROWSER_TEST_F(BrowserLauncherTest, |
Thomas Lukaszewicz | 28091974 | 2022-08-30 03:07:18 | [diff] [blame] | 313 | PRE_FullRestoreWillRestoreWebAppsIfPreviouslyOpen) { |
| 314 | // Browser launch should be suppressed with the kNoStartupWindow switch. |
| 315 | ASSERT_FALSE(browser()); |
| 316 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 317 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 318 | |
| 319 | auto* profile = |
| 320 | profile_manager->GetProfile(profile_manager->GetPrimaryUserProfilePath()); |
| 321 | |
| 322 | // Don't delete the profile too early. |
| 323 | ScopedProfileKeepAlive profile_keep_alive( |
| 324 | profile, ProfileKeepAliveOrigin::kBrowserWindow); |
| 325 | |
| 326 | // Set the startup pref to restore the last session. |
| 327 | SessionStartupPref pref(SessionStartupPref::LAST); |
| 328 | SessionStartupPref::SetStartupPref(profile, pref); |
| 329 | profile->GetPrefs()->CommitPendingWrite(); |
| 330 | |
| 331 | // Install and launch a PWA. |
Eric Willigers | 928fed2 | 2022-09-12 07:03:54 | [diff] [blame] | 332 | web_app::AppId app_id = InstallPWA(profile, GetWebAppStartUrl()); |
Thomas Lukaszewicz | 28091974 | 2022-08-30 03:07:18 | [diff] [blame] | 333 | Browser* app_browser = web_app::LaunchWebAppBrowserAndWait(profile, app_id); |
| 334 | ASSERT_NE(app_browser, nullptr); |
| 335 | ASSERT_EQ(app_browser->type(), Browser::Type::TYPE_APP); |
| 336 | ASSERT_TRUE(web_app::AppBrowserController::IsForWebApp(app_browser, app_id)); |
| 337 | |
| 338 | // Launch a browser. |
| 339 | Browser* browser = Browser::Create( |
| 340 | Browser::CreateParams(Browser::TYPE_NORMAL, profile, true)); |
| 341 | chrome::NewTab(browser); |
| 342 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 343 | browser, embedded_test_server()->GetURL("/empty.html"))); |
| 344 | |
| 345 | // Verify the state of the browser's tab strip. |
| 346 | TabStripModel* tab_strip = browser->tab_strip_model(); |
| 347 | EXPECT_EQ(1, tab_strip->count()); |
| 348 | EXPECT_EQ("/empty.html", |
| 349 | tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path()); |
| 350 | |
| 351 | // Ensure the session ends with the profile in last profiles. |
| 352 | auto last_opened_profiles = |
| 353 | g_browser_process->profile_manager()->GetLastOpenedProfiles(); |
| 354 | EXPECT_EQ(1u, last_opened_profiles.size()); |
| 355 | EXPECT_TRUE(base::Contains(last_opened_profiles, profile)); |
Eric Willigers | 928fed2 | 2022-09-12 07:03:54 | [diff] [blame] | 356 | |
| 357 | SetSkipUninstall(true); |
Thomas Lukaszewicz | 28091974 | 2022-08-30 03:07:18 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | IN_PROC_BROWSER_TEST_F(BrowserLauncherTest, |
| 361 | FullRestoreWillRestoreWebAppsIfPreviouslyOpen) { |
| 362 | // Browser launch should be suppressed with the kNoStartupWindow switch. |
| 363 | ASSERT_FALSE(browser()); |
| 364 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 365 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 366 | |
| 367 | auto* profile = |
| 368 | profile_manager->GetProfile(profile_manager->GetPrimaryUserProfilePath()); |
| 369 | |
| 370 | // The profile should match the one set up in the PRE_ test. |
| 371 | auto last_opened_profiles = |
| 372 | g_browser_process->profile_manager()->GetLastOpenedProfiles(); |
| 373 | EXPECT_EQ(1u, last_opened_profiles.size()); |
| 374 | EXPECT_TRUE(base::Contains(last_opened_profiles, profile)); |
| 375 | |
| 376 | // Trigger Lacros full restore. |
| 377 | EXPECT_FALSE(profile->restored_last_session()); |
| 378 | base::RunLoop run_loop; |
| 379 | testing::SessionsRestoredWaiter restore_waiter(run_loop.QuitClosure(), 1); |
| 380 | browser_service()->OpenForFullRestore(/*skip_crash_restore=*/true); |
| 381 | run_loop.Run(); |
| 382 | |
| 383 | // The last session should be logged as restored. |
| 384 | EXPECT_TRUE(profile->restored_last_session()); |
| 385 | |
| 386 | // A tabbed browser and app browser should have been restored. |
| 387 | ASSERT_EQ(2u, chrome::GetBrowserCount(profile)); |
| 388 | Browser* tabbed_browser = |
| 389 | chrome::FindAllTabbedBrowsersWithProfile(profile)[0]; |
| 390 | TabStripModel* tab_strip = tabbed_browser->tab_strip_model(); |
| 391 | ASSERT_EQ(1, tab_strip->count()); |
| 392 | EXPECT_EQ("/empty.html", |
| 393 | tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path()); |
| 394 | |
| 395 | Browser* app_browser = nullptr; |
| 396 | for (auto* browser : *BrowserList::GetInstance()) { |
| 397 | if (browser->type() != Browser::Type::TYPE_APP) |
| 398 | continue; |
| 399 | EXPECT_FALSE(app_browser); |
| 400 | app_browser = browser; |
| 401 | } |
| 402 | ASSERT_TRUE(app_browser); |
| 403 | } |
| 404 | |
| 405 | // Lacros Apps should only be restored when launching for full restore. Ensure |
| 406 | // Apps are not restored when performing a non-full-restore session restore for |
| 407 | // the browser (i.e. if all lacros windows are closed and a browser window is |
| 408 | // later re-opened we should trigger a session restore, but not restore any |
| 409 | // previously open app windows). |
| 410 | IN_PROC_BROWSER_TEST_F( |
| 411 | BrowserLauncherTest, |
| 412 | SessionRestoreDoesNotTriggerAppRestoreWhenOpeningNewWindows) { |
| 413 | // Browser launch should be suppressed with the kNoStartupWindow switch. |
| 414 | ASSERT_FALSE(browser()); |
| 415 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 416 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 417 | |
| 418 | auto* profile = |
| 419 | profile_manager->GetProfile(profile_manager->GetPrimaryUserProfilePath()); |
| 420 | |
| 421 | // Keep the browser process running while the browsers are closed. |
| 422 | ScopedKeepAlive keep_alive(KeepAliveOrigin::BROWSER, |
| 423 | KeepAliveRestartOption::DISABLED); |
| 424 | ScopedProfileKeepAlive profile_keep_alive( |
| 425 | profile, ProfileKeepAliveOrigin::kBrowserWindow); |
| 426 | |
| 427 | // Install and launch a PWA. |
Eric Willigers | 928fed2 | 2022-09-12 07:03:54 | [diff] [blame] | 428 | web_app::AppId app_id = InstallPWA(profile, GetWebAppStartUrl()); |
Thomas Lukaszewicz | 28091974 | 2022-08-30 03:07:18 | [diff] [blame] | 429 | Browser* app_browser = web_app::LaunchWebAppBrowserAndWait(profile, app_id); |
| 430 | ASSERT_NE(app_browser, nullptr); |
| 431 | ASSERT_EQ(app_browser->type(), Browser::Type::TYPE_APP); |
| 432 | ASSERT_TRUE(web_app::AppBrowserController::IsForWebApp(app_browser, app_id)); |
| 433 | |
| 434 | // Launch a browser. |
| 435 | Browser* browser = Browser::Create( |
| 436 | Browser::CreateParams(Browser::TYPE_NORMAL, profile, true)); |
| 437 | chrome::NewTab(browser); |
| 438 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 439 | browser, embedded_test_server()->GetURL("/empty.html"))); |
| 440 | |
| 441 | // Verify the state of the browser's tab strip. |
| 442 | TabStripModel* tab_strip = browser->tab_strip_model(); |
| 443 | EXPECT_EQ(1, tab_strip->count()); |
| 444 | EXPECT_EQ("/empty.html", |
| 445 | tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path()); |
| 446 | |
| 447 | // Close all browser windows and wait for the operation to complete. |
| 448 | size_t browser_count = chrome::GetTotalBrowserCount(); |
Miriam Polzer | 521da5e | 2022-09-30 14:03:58 | [diff] [blame] | 449 | CloseAllBrowsers(); |
Thomas Lukaszewicz | 28091974 | 2022-08-30 03:07:18 | [diff] [blame] | 450 | for (size_t i = 0; i < browser_count; ++i) |
| 451 | ui_test_utils::WaitForBrowserToClose(); |
| 452 | ASSERT_EQ(0u, BrowserList::GetInstance()->size()); |
| 453 | |
| 454 | // Trigger a new window with session restore. |
| 455 | base::RunLoop run_loop; |
| 456 | testing::SessionsRestoredWaiter restore_waiter(run_loop.QuitClosure(), 1); |
| 457 | NewWindowSync(/*incognito=*/false, /*should_trigger_session_restore=*/true); |
| 458 | run_loop.Run(); |
| 459 | |
| 460 | // The browser window should be restored but app browser should not. |
| 461 | ASSERT_EQ(1u, chrome::GetBrowserCount(profile)); |
| 462 | browser = chrome::FindAllTabbedBrowsersWithProfile(profile)[0]; |
| 463 | tab_strip = browser->tab_strip_model(); |
| 464 | ASSERT_EQ(1, tab_strip->count()); |
| 465 | EXPECT_EQ("/empty.html", |
| 466 | tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path()); |
| 467 | } |
| 468 | |
| 469 | IN_PROC_BROWSER_TEST_F(BrowserLauncherTest, |
Thomas Lukaszewicz | 8fe9687d | 2022-08-29 17:56:22 | [diff] [blame] | 470 | PRE_FullRestoreDoNotSkipCrashRestore) { |
| 471 | // Simulate a full restore by setting up a profile in a PRE_ test. |
| 472 | SetupSingleProfileWithURLSPreference(); |
| 473 | } |
| 474 | |
| 475 | IN_PROC_BROWSER_TEST_F(BrowserLauncherTest, FullRestoreDoNotSkipCrashRestore) { |
| 476 | // Browser launch should be suppressed with the kNoStartupWindow switch. |
| 477 | ASSERT_FALSE(browser()); |
| 478 | |
| 479 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 480 | |
| 481 | // Open the profile. |
| 482 | base::FilePath dest_path = profile_manager->user_data_dir(); |
| 483 | |
| 484 | Profile* profile = nullptr; |
| 485 | { |
| 486 | base::ScopedAllowBlockingForTesting allow_blocking; |
| 487 | profile = profile_manager->GetProfile( |
| 488 | dest_path.Append(FILE_PATH_LITERAL("New Profile"))); |
| 489 | ASSERT_TRUE(profile); |
| 490 | } |
| 491 | |
| 492 | // The profile to be restored should match the one in the PRE_ test. |
| 493 | auto last_opened_profiles = |
| 494 | g_browser_process->profile_manager()->GetLastOpenedProfiles(); |
| 495 | EXPECT_EQ(1u, last_opened_profiles.size()); |
| 496 | EXPECT_TRUE(base::Contains(last_opened_profiles, profile)); |
| 497 | |
| 498 | // Disable the profile picker and set the exit type to crashed. |
| 499 | g_browser_process->local_state()->SetInteger( |
| 500 | prefs::kBrowserProfilePickerAvailabilityOnStartup, |
| 501 | static_cast<int>(ProfilePicker::AvailabilityOnStartup::kDisabled)); |
| 502 | ExitTypeService::GetInstanceForProfile(profile) |
| 503 | ->SetLastSessionExitTypeForTest(ExitType::kCrashed); |
| 504 | |
| 505 | // Trigger Lacros full restore but do not skip crash restore prompts. |
| 506 | browser_service()->OpenForFullRestore(/*skip_crash_restore=*/false); |
| 507 | |
| 508 | // The browser should not be restored but instead we should have the browser's |
| 509 | // crash restore bubble prompt. |
| 510 | Browser* new_browser = nullptr; |
| 511 | ASSERT_EQ(1u, chrome::GetBrowserCount(profile)); |
| 512 | new_browser = chrome::FindBrowserWithProfile(profile); |
| 513 | ASSERT_TRUE(new_browser); |
| 514 | |
| 515 | TabStripModel* tab_strip = new_browser->tab_strip_model(); |
| 516 | ASSERT_EQ(1, tab_strip->count()); |
| 517 | EXPECT_TRUE(content::WaitForLoadStop(tab_strip->GetWebContentsAt(0))); |
| 518 | |
| 519 | views::BubbleDialogDelegate* crash_bubble_delegate = |
| 520 | SessionCrashedBubbleView::GetInstanceForTest(); |
| 521 | EXPECT_TRUE(crash_bubble_delegate); |
| 522 | } |
| 523 | |
| 524 | IN_PROC_BROWSER_TEST_F(BrowserLauncherTest, PRE_FullRestoreSkipCrashRestore) { |
| 525 | // Simulate a full restore by setting up a profile in a PRE_ test. |
| 526 | SetupSingleProfileWithURLSPreference(); |
| 527 | } |
| 528 | |
| 529 | IN_PROC_BROWSER_TEST_F(BrowserLauncherTest, FullRestoreSkipCrashRestore) { |
| 530 | // Browser launch should be suppressed with the kNoStartupWindow switch. |
| 531 | ASSERT_FALSE(browser()); |
| 532 | |
| 533 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 534 | |
| 535 | // Open the profile. |
| 536 | base::FilePath dest_path = profile_manager->user_data_dir(); |
| 537 | |
| 538 | Profile* profile = nullptr; |
| 539 | { |
| 540 | base::ScopedAllowBlockingForTesting allow_blocking; |
| 541 | profile = profile_manager->GetProfile( |
| 542 | dest_path.Append(FILE_PATH_LITERAL("New Profile"))); |
| 543 | ASSERT_TRUE(profile); |
| 544 | } |
| 545 | |
| 546 | // The profile to be restored should match the one in the PRE_ test. |
| 547 | auto last_opened_profiles = |
| 548 | g_browser_process->profile_manager()->GetLastOpenedProfiles(); |
| 549 | EXPECT_EQ(1u, last_opened_profiles.size()); |
| 550 | EXPECT_TRUE(base::Contains(last_opened_profiles, profile)); |
| 551 | |
| 552 | // Disable the profile picker and set the exit type to crashed. |
| 553 | g_browser_process->local_state()->SetInteger( |
| 554 | prefs::kBrowserProfilePickerAvailabilityOnStartup, |
| 555 | static_cast<int>(ProfilePicker::AvailabilityOnStartup::kDisabled)); |
| 556 | ExitTypeService::GetInstanceForProfile(profile) |
| 557 | ->SetLastSessionExitTypeForTest(ExitType::kCrashed); |
| 558 | |
| 559 | // Trigger Lacros full restore and skip crash restore prompts. |
| 560 | base::RunLoop run_loop; |
| 561 | testing::SessionsRestoredWaiter restore_waiter(run_loop.QuitClosure(), 1); |
| 562 | browser_service()->OpenForFullRestore(/*skip_crash_restore=*/true); |
| 563 | run_loop.Run(); |
| 564 | |
| 565 | // The browser should be restored (ignoring startup preference). |
| 566 | Browser* new_browser = nullptr; |
| 567 | ASSERT_EQ(1u, chrome::GetBrowserCount(profile)); |
| 568 | new_browser = chrome::FindBrowserWithProfile(profile); |
| 569 | ASSERT_TRUE(new_browser); |
| 570 | |
| 571 | TabStripModel* tab_strip = new_browser->tab_strip_model(); |
| 572 | ASSERT_EQ(1, tab_strip->count()); |
| 573 | EXPECT_EQ("/empty.html", |
| 574 | tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path()); |
| 575 | } |