blob: 8e0ceb34fb2dbbde35581be05a6c1fc976b485c4 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2022 The Chromium Authors
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:222// 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"
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:226#include "base/test/bind.h"
7#include "base/test/scoped_feature_list.h"
Emmanuel Arias Soto48c4dbe62023-01-30 12:19:258#include "base/test/test_future.h"
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:229#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 Lukaszewicz280919742022-08-30 03:07:1826#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 Willigers928fed22022-09-12 07:03:5430#include "chrome/browser/web_applications/web_app_provider.h"
31#include "chrome/browser/web_applications/web_app_registrar.h"
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:2232#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 Oshima2d455aa2c2022-10-16 23:29:3742#include "ui/display/screen.h"
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:2243
Thomas Lukaszewicz280919742022-08-30 03:07:1844namespace {
45
46web_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.keiling5a8e27f22023-01-01 17:55:4750 web_app_info->user_display_mode =
51 web_app::mojom::UserDisplayMode::kStandalone;
Thomas Lukaszewicz280919742022-08-30 03:07:1852 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 Lukaszewicz8fe9687d2022-08-29 17:56:2258class 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 Willigers928fed22022-09-12 07:03:5480 void TearDownOnMainThread() override {
81 if (!skip_uninstall_) {
82 UninstallWebApps();
83 }
84 InProcessBrowserTest::TearDownOnMainThread();
85 }
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:2286
87 void NewWindowSync(bool incognito, bool should_trigger_session_restore) {
Emmanuel Arias Soto48c4dbe62023-01-30 12:19:2588 base::test::TestFuture<void> new_window_future;
Mitsuru Oshima2d455aa2c2022-10-16 23:29:3789 browser_service()->NewWindow(
90 incognito, should_trigger_session_restore,
91 display::Screen::GetScreen()->GetDisplayForNewWindows().id(),
Emmanuel Arias Soto48c4dbe62023-01-30 12:19:2592 new_window_future.GetCallback());
93 ASSERT_TRUE(new_window_future.Wait())
94 << "NewWindow did not trigger the callback.";
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:2295 }
96
97 void DisableWelcomePages(const std::vector<Profile*>& profiles) {
98 for (Profile* profile : profiles)
99 profile->GetPrefs()->SetBoolean(prefs::kHasSeenWelcomePage, true);
100
101 // Also disable What's New.
102 PrefService* pref_service = g_browser_process->local_state();
103 pref_service->SetInteger(prefs::kLastWhatsNewVersion, CHROME_VERSION_MAJOR);
104 }
105
106 // Creates a new profile with a URLS startup preference and an open tab.
107 void SetupSingleProfileWithURLSPreference() {
108 ProfileManager* profile_manager = g_browser_process->profile_manager();
109
110 ASSERT_TRUE(embedded_test_server()->Start());
111
112 // Create two profiles.
113 base::FilePath dest_path = profile_manager->user_data_dir();
114
115 Profile* profile = nullptr;
116 {
117 base::ScopedAllowBlockingForTesting allow_blocking;
118 profile = profile_manager->GetProfile(
119 dest_path.Append(FILE_PATH_LITERAL("New Profile")));
120 ASSERT_TRUE(profile);
121 }
122 DisableWelcomePages({profile});
123
124 // Don't delete Profile too early.
125 ScopedProfileKeepAlive profile_keep_alive(
126 profile, ProfileKeepAliveOrigin::kBrowserWindow);
127
128 // Open some urls in the browser.
129 Browser* browser = Browser::Create(
130 Browser::CreateParams(Browser::TYPE_NORMAL, profile, true));
131 chrome::NewTab(browser);
132 ASSERT_TRUE(ui_test_utils::NavigateToURL(
133 browser, embedded_test_server()->GetURL("/empty.html")));
134
135 // Establish the startup preference.
136 std::vector<GURL> urls;
137 urls.push_back(ui_test_utils::GetTestUrl(
138 base::FilePath(base::FilePath::kCurrentDirectory),
139 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
140 urls.push_back(ui_test_utils::GetTestUrl(
141 base::FilePath(base::FilePath::kCurrentDirectory),
142 base::FilePath(FILE_PATH_LITERAL("title2.html"))));
143
144 // Set different startup preferences for the profile.
145 SessionStartupPref pref(SessionStartupPref::URLS);
146 pref.urls = urls;
147 SessionStartupPref::SetStartupPref(profile, pref);
148 profile->GetPrefs()->CommitPendingWrite();
149
150 // Ensure the session ends with the profile created above.
151 auto last_opened_profiles =
152 g_browser_process->profile_manager()->GetLastOpenedProfiles();
153 EXPECT_EQ(1u, last_opened_profiles.size());
154 EXPECT_TRUE(base::Contains(last_opened_profiles, profile));
155 }
156
157 BrowserServiceLacros* browser_service() const {
158 return browser_service_.get();
159 }
160
Eric Willigers928fed22022-09-12 07:03:54161 GURL GetWebAppStartUrl() const {
162 return GURL("https://lacros.example.com/example/index");
163 }
164
165 void SetSkipUninstall(bool value) { skip_uninstall_ = value; }
166
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:22167 private:
Eric Willigers928fed22022-09-12 07:03:54168 void UninstallWebApps() {
169 ProfileManager* profile_manager = g_browser_process->profile_manager();
170 auto* profile = profile_manager->GetProfile(
171 profile_manager->GetPrimaryUserProfilePath());
172
173 web_app::WebAppRegistrar& registrar =
Camden King849ff25b2022-12-08 20:17:55174 web_app::WebAppProvider::GetForTest(profile)->registrar_unsafe();
Eric Willigers928fed22022-09-12 07:03:54175 for (auto& app_id : registrar.GetAppIds()) {
176 web_app::test::UninstallWebApp(profile, app_id);
177 }
178 }
179
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:22180 std::unique_ptr<BrowserServiceLacros> browser_service_;
Eric Willigers928fed22022-09-12 07:03:54181 bool skip_uninstall_ = false;
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:22182};
183
184IN_PROC_BROWSER_TEST_F(BrowserLauncherTest, PRE_FullRestoreWithTwoProfiles) {
185 // Simulate a full restore by creating the profiles in a PRE_ test.
186 ProfileManager* profile_manager = g_browser_process->profile_manager();
187
188 ASSERT_TRUE(embedded_test_server()->Start());
189
190 // Create two profiles.
191 base::FilePath dest_path = profile_manager->user_data_dir();
192
193 Profile* profile1 = nullptr;
194 Profile* profile2 = nullptr;
195 {
196 base::ScopedAllowBlockingForTesting allow_blocking;
197 profile1 = profile_manager->GetProfile(
198 dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
199 ASSERT_TRUE(profile1);
200
201 profile2 = profile_manager->GetProfile(
202 dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
203 ASSERT_TRUE(profile2);
204 }
205 DisableWelcomePages({profile1, profile2});
206
207 // Don't delete Profiles too early.
208 ScopedProfileKeepAlive profile1_keep_alive(
209 profile1, ProfileKeepAliveOrigin::kBrowserWindow);
210 ScopedProfileKeepAlive profile2_keep_alive(
211 profile2, ProfileKeepAliveOrigin::kBrowserWindow);
212
213 // Open some urls with the browsers, and close them.
214 Browser* browser1 = Browser::Create(
215 Browser::CreateParams(Browser::TYPE_NORMAL, profile1, true));
216 chrome::NewTab(browser1);
217 ASSERT_TRUE(ui_test_utils::NavigateToURL(
218 browser1, embedded_test_server()->GetURL("/empty.html")));
219
220 Browser* browser2 = Browser::Create(
221 Browser::CreateParams(Browser::TYPE_NORMAL, profile2, true));
222 chrome::NewTab(browser2);
223 ASSERT_TRUE(ui_test_utils::NavigateToURL(
224 browser2, embedded_test_server()->GetURL("/form.html")));
225
226 // Set different startup preferences for the 2 profiles.
227 std::vector<GURL> urls1;
228 urls1.push_back(ui_test_utils::GetTestUrl(
229 base::FilePath(base::FilePath::kCurrentDirectory),
230 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
231 std::vector<GURL> urls2;
232 urls2.push_back(ui_test_utils::GetTestUrl(
233 base::FilePath(base::FilePath::kCurrentDirectory),
234 base::FilePath(FILE_PATH_LITERAL("title2.html"))));
235
236 // Set different startup preferences for the 2 profiles.
237 SessionStartupPref pref1(SessionStartupPref::URLS);
238 pref1.urls = urls1;
239 SessionStartupPref::SetStartupPref(profile1, pref1);
240 SessionStartupPref pref2(SessionStartupPref::URLS);
241 pref2.urls = urls2;
242 SessionStartupPref::SetStartupPref(profile2, pref2);
243
244 profile1->GetPrefs()->CommitPendingWrite();
245 profile2->GetPrefs()->CommitPendingWrite();
246
247 // Ensure the session ends with the above two profiles.
248 auto last_opened_profiles =
249 g_browser_process->profile_manager()->GetLastOpenedProfiles();
250 EXPECT_EQ(2u, last_opened_profiles.size());
251 EXPECT_TRUE(base::Contains(last_opened_profiles, profile1));
252 EXPECT_TRUE(base::Contains(last_opened_profiles, profile2));
253}
254
255IN_PROC_BROWSER_TEST_F(BrowserLauncherTest, FullRestoreWithTwoProfiles) {
256 // Browser launch should be suppressed with the kNoStartupWindow switch.
257 ASSERT_FALSE(browser());
258
259 ProfileManager* profile_manager = g_browser_process->profile_manager();
260
261 // Open the two profiles.
262 base::FilePath dest_path = profile_manager->user_data_dir();
263
264 Profile* profile1 = nullptr;
265 Profile* profile2 = nullptr;
266 {
267 base::ScopedAllowBlockingForTesting allow_blocking;
268 profile1 = profile_manager->GetProfile(
269 dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
270 ASSERT_TRUE(profile1);
271
272 profile2 = profile_manager->GetProfile(
273 dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
274 ASSERT_TRUE(profile2);
275 }
276
277 // The profiles to be restored should match those setup in the PRE_ test.
278 auto last_opened_profiles =
279 g_browser_process->profile_manager()->GetLastOpenedProfiles();
280 EXPECT_EQ(2u, last_opened_profiles.size());
281 EXPECT_TRUE(base::Contains(last_opened_profiles, profile1));
282 EXPECT_TRUE(base::Contains(last_opened_profiles, profile2));
283
284 // Trigger Lacros full restore.
Emmanuel Arias Soto48c4dbe62023-01-30 12:19:25285 base::test::TestFuture<void> restore_waiter_future;
286 testing::SessionsRestoredWaiter restore_waiter(
287 restore_waiter_future.GetCallback(), 2);
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:22288 browser_service()->OpenForFullRestore(/*skip_crash_restore=*/true);
Emmanuel Arias Soto48c4dbe62023-01-30 12:19:25289 ASSERT_TRUE(restore_waiter_future.Wait())
290 << "restore_waiter did not trigger the callback.";
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:22291
292 // The startup URLs are ignored, and instead the last open sessions are
293 // restored.
294 EXPECT_TRUE(profile1->restored_last_session());
295 EXPECT_TRUE(profile2->restored_last_session());
296
297 Browser* new_browser = nullptr;
298 ASSERT_EQ(1u, chrome::GetBrowserCount(profile1));
299 new_browser = chrome::FindBrowserWithProfile(profile1);
300 ASSERT_TRUE(new_browser);
301 TabStripModel* tab_strip = new_browser->tab_strip_model();
302 ASSERT_EQ(1, tab_strip->count());
303 EXPECT_EQ("/empty.html",
304 tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path());
305
306 ASSERT_EQ(1u, chrome::GetBrowserCount(profile2));
307 new_browser = chrome::FindBrowserWithProfile(profile2);
308 ASSERT_TRUE(new_browser);
309 tab_strip = new_browser->tab_strip_model();
310 ASSERT_EQ(1, tab_strip->count());
311 EXPECT_EQ("/form.html",
312 tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path());
313}
314
315IN_PROC_BROWSER_TEST_F(BrowserLauncherTest,
Thomas Lukaszewicz280919742022-08-30 03:07:18316 PRE_FullRestoreWillRestoreWebAppsIfPreviouslyOpen) {
317 // Browser launch should be suppressed with the kNoStartupWindow switch.
318 ASSERT_FALSE(browser());
319 ProfileManager* profile_manager = g_browser_process->profile_manager();
320 ASSERT_TRUE(embedded_test_server()->Start());
321
322 auto* profile =
323 profile_manager->GetProfile(profile_manager->GetPrimaryUserProfilePath());
324
325 // Don't delete the profile too early.
326 ScopedProfileKeepAlive profile_keep_alive(
327 profile, ProfileKeepAliveOrigin::kBrowserWindow);
328
329 // Set the startup pref to restore the last session.
330 SessionStartupPref pref(SessionStartupPref::LAST);
331 SessionStartupPref::SetStartupPref(profile, pref);
332 profile->GetPrefs()->CommitPendingWrite();
333
334 // Install and launch a PWA.
Eric Willigers928fed22022-09-12 07:03:54335 web_app::AppId app_id = InstallPWA(profile, GetWebAppStartUrl());
Thomas Lukaszewicz280919742022-08-30 03:07:18336 Browser* app_browser = web_app::LaunchWebAppBrowserAndWait(profile, app_id);
337 ASSERT_NE(app_browser, nullptr);
338 ASSERT_EQ(app_browser->type(), Browser::Type::TYPE_APP);
339 ASSERT_TRUE(web_app::AppBrowserController::IsForWebApp(app_browser, app_id));
340
341 // Launch a browser.
342 Browser* browser = Browser::Create(
343 Browser::CreateParams(Browser::TYPE_NORMAL, profile, true));
344 chrome::NewTab(browser);
345 ASSERT_TRUE(ui_test_utils::NavigateToURL(
346 browser, embedded_test_server()->GetURL("/empty.html")));
347
348 // Verify the state of the browser's tab strip.
349 TabStripModel* tab_strip = browser->tab_strip_model();
350 EXPECT_EQ(1, tab_strip->count());
351 EXPECT_EQ("/empty.html",
352 tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path());
353
354 // Ensure the session ends with the profile in last profiles.
355 auto last_opened_profiles =
356 g_browser_process->profile_manager()->GetLastOpenedProfiles();
357 EXPECT_EQ(1u, last_opened_profiles.size());
358 EXPECT_TRUE(base::Contains(last_opened_profiles, profile));
Eric Willigers928fed22022-09-12 07:03:54359
360 SetSkipUninstall(true);
Thomas Lukaszewicz280919742022-08-30 03:07:18361}
362
363IN_PROC_BROWSER_TEST_F(BrowserLauncherTest,
364 FullRestoreWillRestoreWebAppsIfPreviouslyOpen) {
365 // Browser launch should be suppressed with the kNoStartupWindow switch.
366 ASSERT_FALSE(browser());
367 ProfileManager* profile_manager = g_browser_process->profile_manager();
368 ASSERT_TRUE(embedded_test_server()->Start());
369
370 auto* profile =
371 profile_manager->GetProfile(profile_manager->GetPrimaryUserProfilePath());
372
373 // The profile should match the one set up in the PRE_ test.
374 auto last_opened_profiles =
375 g_browser_process->profile_manager()->GetLastOpenedProfiles();
376 EXPECT_EQ(1u, last_opened_profiles.size());
377 EXPECT_TRUE(base::Contains(last_opened_profiles, profile));
378
379 // Trigger Lacros full restore.
380 EXPECT_FALSE(profile->restored_last_session());
Emmanuel Arias Soto48c4dbe62023-01-30 12:19:25381 base::test::TestFuture<void> restore_waiter_future;
382 testing::SessionsRestoredWaiter restore_waiter(
383 restore_waiter_future.GetCallback(), 1);
Thomas Lukaszewicz280919742022-08-30 03:07:18384 browser_service()->OpenForFullRestore(/*skip_crash_restore=*/true);
Emmanuel Arias Soto48c4dbe62023-01-30 12:19:25385 ASSERT_TRUE(restore_waiter_future.Wait())
386 << "restore_waiter did not trigger the callback.";
Thomas Lukaszewicz280919742022-08-30 03:07:18387
388 // The last session should be logged as restored.
389 EXPECT_TRUE(profile->restored_last_session());
390
391 // A tabbed browser and app browser should have been restored.
392 ASSERT_EQ(2u, chrome::GetBrowserCount(profile));
393 Browser* tabbed_browser =
394 chrome::FindAllTabbedBrowsersWithProfile(profile)[0];
395 TabStripModel* tab_strip = tabbed_browser->tab_strip_model();
396 ASSERT_EQ(1, tab_strip->count());
397 EXPECT_EQ("/empty.html",
398 tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path());
399
400 Browser* app_browser = nullptr;
401 for (auto* browser : *BrowserList::GetInstance()) {
402 if (browser->type() != Browser::Type::TYPE_APP)
403 continue;
404 EXPECT_FALSE(app_browser);
405 app_browser = browser;
406 }
407 ASSERT_TRUE(app_browser);
408}
409
410// Lacros Apps should only be restored when launching for full restore. Ensure
411// Apps are not restored when performing a non-full-restore session restore for
412// the browser (i.e. if all lacros windows are closed and a browser window is
413// later re-opened we should trigger a session restore, but not restore any
414// previously open app windows).
415IN_PROC_BROWSER_TEST_F(
416 BrowserLauncherTest,
417 SessionRestoreDoesNotTriggerAppRestoreWhenOpeningNewWindows) {
418 // Browser launch should be suppressed with the kNoStartupWindow switch.
419 ASSERT_FALSE(browser());
420 ProfileManager* profile_manager = g_browser_process->profile_manager();
421 ASSERT_TRUE(embedded_test_server()->Start());
422
423 auto* profile =
424 profile_manager->GetProfile(profile_manager->GetPrimaryUserProfilePath());
425
426 // Keep the browser process running while the browsers are closed.
427 ScopedKeepAlive keep_alive(KeepAliveOrigin::BROWSER,
428 KeepAliveRestartOption::DISABLED);
429 ScopedProfileKeepAlive profile_keep_alive(
430 profile, ProfileKeepAliveOrigin::kBrowserWindow);
431
432 // Install and launch a PWA.
Eric Willigers928fed22022-09-12 07:03:54433 web_app::AppId app_id = InstallPWA(profile, GetWebAppStartUrl());
Thomas Lukaszewicz280919742022-08-30 03:07:18434 Browser* app_browser = web_app::LaunchWebAppBrowserAndWait(profile, app_id);
435 ASSERT_NE(app_browser, nullptr);
436 ASSERT_EQ(app_browser->type(), Browser::Type::TYPE_APP);
437 ASSERT_TRUE(web_app::AppBrowserController::IsForWebApp(app_browser, app_id));
438
439 // Launch a browser.
440 Browser* browser = Browser::Create(
441 Browser::CreateParams(Browser::TYPE_NORMAL, profile, true));
442 chrome::NewTab(browser);
443 ASSERT_TRUE(ui_test_utils::NavigateToURL(
444 browser, embedded_test_server()->GetURL("/empty.html")));
445
446 // Verify the state of the browser's tab strip.
447 TabStripModel* tab_strip = browser->tab_strip_model();
448 EXPECT_EQ(1, tab_strip->count());
449 EXPECT_EQ("/empty.html",
450 tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path());
451
452 // Close all browser windows and wait for the operation to complete.
453 size_t browser_count = chrome::GetTotalBrowserCount();
Miriam Polzer521da5e2022-09-30 14:03:58454 CloseAllBrowsers();
Thomas Lukaszewicz280919742022-08-30 03:07:18455 for (size_t i = 0; i < browser_count; ++i)
456 ui_test_utils::WaitForBrowserToClose();
457 ASSERT_EQ(0u, BrowserList::GetInstance()->size());
458
459 // Trigger a new window with session restore.
Emmanuel Arias Soto48c4dbe62023-01-30 12:19:25460 base::test::TestFuture<void> restore_waiter_future;
461 testing::SessionsRestoredWaiter restore_waiter(
462 restore_waiter_future.GetCallback(), 1);
Thomas Lukaszewicz280919742022-08-30 03:07:18463 NewWindowSync(/*incognito=*/false, /*should_trigger_session_restore=*/true);
Emmanuel Arias Soto48c4dbe62023-01-30 12:19:25464 ASSERT_TRUE(restore_waiter_future.Wait())
465 << "restore_waiter did not trigger the callback.";
Thomas Lukaszewicz280919742022-08-30 03:07:18466
467 // The browser window should be restored but app browser should not.
468 ASSERT_EQ(1u, chrome::GetBrowserCount(profile));
469 browser = chrome::FindAllTabbedBrowsersWithProfile(profile)[0];
470 tab_strip = browser->tab_strip_model();
471 ASSERT_EQ(1, tab_strip->count());
472 EXPECT_EQ("/empty.html",
473 tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path());
474}
475
476IN_PROC_BROWSER_TEST_F(BrowserLauncherTest,
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:22477 PRE_FullRestoreDoNotSkipCrashRestore) {
478 // Simulate a full restore by setting up a profile in a PRE_ test.
479 SetupSingleProfileWithURLSPreference();
480}
481
482IN_PROC_BROWSER_TEST_F(BrowserLauncherTest, FullRestoreDoNotSkipCrashRestore) {
483 // Browser launch should be suppressed with the kNoStartupWindow switch.
484 ASSERT_FALSE(browser());
485
486 ProfileManager* profile_manager = g_browser_process->profile_manager();
487
488 // Open the profile.
489 base::FilePath dest_path = profile_manager->user_data_dir();
490
491 Profile* profile = nullptr;
492 {
493 base::ScopedAllowBlockingForTesting allow_blocking;
494 profile = profile_manager->GetProfile(
495 dest_path.Append(FILE_PATH_LITERAL("New Profile")));
496 ASSERT_TRUE(profile);
497 }
498
499 // The profile to be restored should match the one in the PRE_ test.
500 auto last_opened_profiles =
501 g_browser_process->profile_manager()->GetLastOpenedProfiles();
502 EXPECT_EQ(1u, last_opened_profiles.size());
503 EXPECT_TRUE(base::Contains(last_opened_profiles, profile));
504
505 // Disable the profile picker and set the exit type to crashed.
506 g_browser_process->local_state()->SetInteger(
507 prefs::kBrowserProfilePickerAvailabilityOnStartup,
508 static_cast<int>(ProfilePicker::AvailabilityOnStartup::kDisabled));
509 ExitTypeService::GetInstanceForProfile(profile)
510 ->SetLastSessionExitTypeForTest(ExitType::kCrashed);
511
512 // Trigger Lacros full restore but do not skip crash restore prompts.
513 browser_service()->OpenForFullRestore(/*skip_crash_restore=*/false);
514
515 // The browser should not be restored but instead we should have the browser's
516 // crash restore bubble prompt.
517 Browser* new_browser = nullptr;
518 ASSERT_EQ(1u, chrome::GetBrowserCount(profile));
519 new_browser = chrome::FindBrowserWithProfile(profile);
520 ASSERT_TRUE(new_browser);
521
522 TabStripModel* tab_strip = new_browser->tab_strip_model();
523 ASSERT_EQ(1, tab_strip->count());
524 EXPECT_TRUE(content::WaitForLoadStop(tab_strip->GetWebContentsAt(0)));
525
526 views::BubbleDialogDelegate* crash_bubble_delegate =
527 SessionCrashedBubbleView::GetInstanceForTest();
528 EXPECT_TRUE(crash_bubble_delegate);
529}
530
531IN_PROC_BROWSER_TEST_F(BrowserLauncherTest, PRE_FullRestoreSkipCrashRestore) {
532 // Simulate a full restore by setting up a profile in a PRE_ test.
533 SetupSingleProfileWithURLSPreference();
534}
535
536IN_PROC_BROWSER_TEST_F(BrowserLauncherTest, FullRestoreSkipCrashRestore) {
537 // Browser launch should be suppressed with the kNoStartupWindow switch.
538 ASSERT_FALSE(browser());
539
540 ProfileManager* profile_manager = g_browser_process->profile_manager();
541
542 // Open the profile.
543 base::FilePath dest_path = profile_manager->user_data_dir();
544
545 Profile* profile = nullptr;
546 {
547 base::ScopedAllowBlockingForTesting allow_blocking;
548 profile = profile_manager->GetProfile(
549 dest_path.Append(FILE_PATH_LITERAL("New Profile")));
550 ASSERT_TRUE(profile);
551 }
552
553 // The profile to be restored should match the one in the PRE_ test.
554 auto last_opened_profiles =
555 g_browser_process->profile_manager()->GetLastOpenedProfiles();
556 EXPECT_EQ(1u, last_opened_profiles.size());
557 EXPECT_TRUE(base::Contains(last_opened_profiles, profile));
558
559 // Disable the profile picker and set the exit type to crashed.
560 g_browser_process->local_state()->SetInteger(
561 prefs::kBrowserProfilePickerAvailabilityOnStartup,
562 static_cast<int>(ProfilePicker::AvailabilityOnStartup::kDisabled));
563 ExitTypeService::GetInstanceForProfile(profile)
564 ->SetLastSessionExitTypeForTest(ExitType::kCrashed);
565
566 // Trigger Lacros full restore and skip crash restore prompts.
Emmanuel Arias Soto48c4dbe62023-01-30 12:19:25567 base::test::TestFuture<void> restore_waiter_future;
568 testing::SessionsRestoredWaiter restore_waiter(
569 restore_waiter_future.GetCallback(), 1);
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:22570 browser_service()->OpenForFullRestore(/*skip_crash_restore=*/true);
Emmanuel Arias Soto48c4dbe62023-01-30 12:19:25571 ASSERT_TRUE(restore_waiter_future.Wait())
572 << "restore_waiter did not trigger the callback.";
Thomas Lukaszewicz8fe9687d2022-08-29 17:56:22573
574 // The browser should be restored (ignoring startup preference).
575 Browser* new_browser = nullptr;
576 ASSERT_EQ(1u, chrome::GetBrowserCount(profile));
577 new_browser = chrome::FindBrowserWithProfile(profile);
578 ASSERT_TRUE(new_browser);
579
580 TabStripModel* tab_strip = new_browser->tab_strip_model();
581 ASSERT_EQ(1, tab_strip->count());
582 EXPECT_EQ("/empty.html",
583 tab_strip->GetWebContentsAt(0)->GetLastCommittedURL().path());
584}