1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/files/file_path.h"
7 #include "base/files/file_util.h"
8 #include "base/lazy_instance.h"
9 #include "base/path_service.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/background/background_mode_manager.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/content_settings/cookie_settings.h"
16 #include "chrome/browser/defaults.h"
17 #include "chrome/browser/infobars/infobar_service.h"
18 #include "chrome/browser/lifetime/application_lifetime.h"
19 #include "chrome/browser/prefs/session_startup_pref.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile_impl.h"
22 #include "chrome/browser/profiles/profile_manager.h"
23 #include "chrome/browser/sessions/session_restore_test_helper.h"
24 #include "chrome/browser/sessions/session_service_factory.h"
25 #include "chrome/browser/sessions/session_service_test_helper.h"
26 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_commands.h"
28 #include "chrome/browser/ui/browser_iterator.h"
29 #include "chrome/browser/ui/browser_window.h"
30 #include "chrome/browser/ui/startup/startup_browser_creator.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/pref_names.h"
34 #include "chrome/common/url_constants.h"
35 #include "chrome/test/base/in_process_browser_test.h"
36 #include "chrome/test/base/ui_test_utils.h"
37 #include "components/content_settings/core/common/content_settings.h"
38 #include "components/infobars/core/confirm_infobar_delegate.h"
39 #include "content/public/browser/web_contents.h"
40 #include "content/public/common/url_constants.h"
41 #include "content/public/test/browser_test_utils.h"
42 #include "net/base/net_util.h"
43 #include "net/base/upload_bytes_element_reader.h"
44 #include "net/base/upload_data_stream.h"
45 #include "net/url_request/url_request.h"
46 #include "net/url_request/url_request_filter.h"
47 #include "net/url_request/url_request_test_job.h"
49 #if defined(OS_MACOSX)
50 #include "base/mac/scoped_nsautorelease_pool.h"
55 // We need to serve the test files so that PRE_Test and Test can access the same
56 // page using the same URL. In addition, perceived security origin of the page
57 // needs to stay the same, so e.g., redirecting the URL requests doesn't
58 // work. (If we used a test server, the PRE_Test and Test would have separate
59 // instances running on separate ports.)
61 base::LazyInstance
<std::map
<std::string
, std::string
> > g_file_contents
=
62 LAZY_INSTANCE_INITIALIZER
;
64 net::URLRequestJob
* URLRequestFaker(
65 net::URLRequest
* request
,
66 net::NetworkDelegate
* network_delegate
,
67 const std::string
& scheme
) {
68 return new net::URLRequestTestJob(
69 request
, network_delegate
, net::URLRequestTestJob::test_headers(),
70 g_file_contents
.Get()[request
->url().path()], true);
73 base::LazyInstance
<std::string
> g_last_upload_bytes
= LAZY_INSTANCE_INITIALIZER
;
75 net::URLRequestJob
* URLRequestFakerForPostRequests(
76 net::URLRequest
* request
,
77 net::NetworkDelegate
* network_delegate
,
78 const std::string
& scheme
) {
79 // Read the uploaded data and store it to g_last_upload_bytes.
80 const net::UploadDataStream
* upload_data
= request
->get_upload();
81 g_last_upload_bytes
.Get().clear();
83 const ScopedVector
<net::UploadElementReader
>* readers
=
84 upload_data
->GetElementReaders();
86 for (size_t i
= 0; i
< readers
->size(); ++i
) {
87 const net::UploadBytesElementReader
* bytes_reader
=
88 (*readers
)[i
]->AsBytesReader();
90 g_last_upload_bytes
.Get() +=
91 std::string(bytes_reader
->bytes(), bytes_reader
->length());
96 return new net::URLRequestTestJob(
97 request
, network_delegate
, net::URLRequestTestJob::test_headers(),
98 "<html><head><title>PASS</title></head><body>Data posted</body></html>",
102 class FakeBackgroundModeManager
: public BackgroundModeManager
{
104 FakeBackgroundModeManager()
105 : BackgroundModeManager(
106 *base::CommandLine::ForCurrentProcess(),
107 &g_browser_process
->profile_manager()->GetProfileInfoCache()),
108 background_mode_active_(false) {}
110 void SetBackgroundModeActive(bool active
) {
111 background_mode_active_
= active
;
114 bool IsBackgroundModeActive() override
{ return background_mode_active_
; }
117 bool background_mode_active_
;
123 class BetterSessionRestoreTest
: public InProcessBrowserTest
{
125 BetterSessionRestoreTest()
126 : fake_server_address_("http://www.test.com/"),
127 test_path_("session_restore/"),
128 title_pass_(base::ASCIIToUTF16("PASS")),
129 title_storing_(base::ASCIIToUTF16("STORING")),
130 title_error_write_failed_(base::ASCIIToUTF16("ERROR_WRITE_FAILED")),
131 title_error_empty_(base::ASCIIToUTF16("ERROR_EMPTY")) {
132 // Set up the URL request filtering.
133 std::vector
<std::string
> test_files
;
134 test_files
.push_back("common.js");
135 test_files
.push_back("cookies.html");
136 test_files
.push_back("local_storage.html");
137 test_files
.push_back("post.html");
138 test_files
.push_back("post_with_password.html");
139 test_files
.push_back("session_cookies.html");
140 test_files
.push_back("session_storage.html");
141 base::FilePath test_file_dir
;
142 CHECK(PathService::Get(base::DIR_SOURCE_ROOT
, &test_file_dir
));
144 test_file_dir
.AppendASCII("chrome/test/data").AppendASCII(test_path_
);
146 for (std::vector
<std::string
>::const_iterator it
= test_files
.begin();
147 it
!= test_files
.end(); ++it
) {
148 base::FilePath path
= test_file_dir
.AppendASCII(*it
);
149 std::string contents
;
150 CHECK(base::ReadFileToString(path
, &contents
));
151 g_file_contents
.Get()["/" + test_path_
+ *it
] = contents
;
152 net::URLRequestFilter::GetInstance()->AddUrlHandler(
153 GURL(fake_server_address_
+ test_path_
+ *it
),
156 net::URLRequestFilter::GetInstance()->AddUrlHandler(
157 GURL(fake_server_address_
+ test_path_
+ "posted.php"),
158 &URLRequestFakerForPostRequests
);
162 void SetUpOnMainThread() override
{
163 SessionServiceTestHelper
helper(
164 SessionServiceFactory::GetForProfile(browser()->profile()));
165 helper
.SetForceBrowserNotAliveWithNoWindows(true);
166 helper
.ReleaseService();
167 g_browser_process
->set_background_mode_manager_for_test(
168 scoped_ptr
<BackgroundModeManager
>(new FakeBackgroundModeManager
));
171 void StoreDataWithPage(const std::string
& filename
) {
172 StoreDataWithPage(browser(), filename
);
175 void StoreDataWithPage(Browser
* browser
, const std::string
& filename
) {
176 content::WebContents
* web_contents
=
177 browser
->tab_strip_model()->GetActiveWebContents();
178 content::TitleWatcher
title_watcher(web_contents
, title_storing_
);
179 title_watcher
.AlsoWaitForTitle(title_pass_
);
180 title_watcher
.AlsoWaitForTitle(title_error_write_failed_
);
181 title_watcher
.AlsoWaitForTitle(title_error_empty_
);
182 ui_test_utils::NavigateToURL(
183 browser
, GURL(fake_server_address_
+ test_path_
+ filename
));
184 base::string16 final_title
= title_watcher
.WaitAndGetTitle();
185 EXPECT_EQ(title_storing_
, final_title
);
188 void NavigateAndCheckStoredData(const std::string
& filename
) {
189 NavigateAndCheckStoredData(browser(), filename
);
192 void NavigateAndCheckStoredData(Browser
* browser
,
193 const std::string
& filename
) {
194 // Navigate to a page which has previously stored data; check that the
195 // stored data can be accessed.
196 content::WebContents
* web_contents
=
197 browser
->tab_strip_model()->GetActiveWebContents();
198 content::TitleWatcher
title_watcher(web_contents
, title_pass_
);
199 title_watcher
.AlsoWaitForTitle(title_storing_
);
200 title_watcher
.AlsoWaitForTitle(title_error_write_failed_
);
201 title_watcher
.AlsoWaitForTitle(title_error_empty_
);
202 ui_test_utils::NavigateToURL(
203 browser
, GURL(fake_server_address_
+ test_path_
+ filename
));
204 base::string16 final_title
= title_watcher
.WaitAndGetTitle();
205 EXPECT_EQ(title_pass_
, final_title
);
208 void CheckReloadedPageRestored() {
209 CheckTitle(browser(), title_pass_
);
212 void CheckReloadedPageRestored(Browser
* browser
) {
213 CheckTitle(browser
, title_pass_
);
216 void CheckReloadedPageNotRestored() {
217 CheckReloadedPageNotRestored(browser());
220 void CheckReloadedPageNotRestored(Browser
* browser
) {
221 CheckTitle(browser
, title_storing_
);
224 void CheckTitle(Browser
* browser
, const base::string16
& expected_title
) {
225 content::WebContents
* web_contents
=
226 browser
->tab_strip_model()->GetWebContentsAt(0);
227 content::TitleWatcher
title_watcher(web_contents
, expected_title
);
228 title_watcher
.AlsoWaitForTitle(title_pass_
);
229 title_watcher
.AlsoWaitForTitle(title_storing_
);
230 title_watcher
.AlsoWaitForTitle(title_error_write_failed_
);
231 title_watcher
.AlsoWaitForTitle(title_error_empty_
);
232 // It's possible that the title was already the right one before
233 // title_watcher was created.
234 base::string16 first_title
= web_contents
->GetTitle();
235 if (first_title
!= title_pass_
&&
236 first_title
!= title_storing_
&&
237 first_title
!= title_error_write_failed_
&&
238 first_title
!= title_error_empty_
) {
239 base::string16 final_title
= title_watcher
.WaitAndGetTitle();
240 EXPECT_EQ(expected_title
, final_title
);
242 EXPECT_EQ(expected_title
, first_title
);
246 void PostFormWithPage(const std::string
& filename
, bool password_present
) {
247 content::WebContents
* web_contents
=
248 browser()->tab_strip_model()->GetActiveWebContents();
249 content::TitleWatcher
title_watcher(web_contents
, title_pass_
);
250 ui_test_utils::NavigateToURL(
251 browser(), GURL(fake_server_address_
+ test_path_
+ filename
));
252 base::string16 final_title
= title_watcher
.WaitAndGetTitle();
253 EXPECT_EQ(title_pass_
, final_title
);
254 EXPECT_TRUE(g_last_upload_bytes
.Get().find("posted-text") !=
256 EXPECT_TRUE(g_last_upload_bytes
.Get().find("text-entered") !=
258 if (password_present
) {
259 EXPECT_TRUE(g_last_upload_bytes
.Get().find("posted-password") !=
261 EXPECT_TRUE(g_last_upload_bytes
.Get().find("password-entered") !=
266 void CheckFormRestored(bool text_present
, bool password_present
) {
267 CheckFormRestored(browser(), text_present
, password_present
);
270 void CheckFormRestored(
271 Browser
* browser
, bool text_present
, bool password_present
) {
272 CheckReloadedPageRestored(browser
);
274 EXPECT_TRUE(g_last_upload_bytes
.Get().find("posted-text") !=
276 EXPECT_TRUE(g_last_upload_bytes
.Get().find("text-entered") !=
279 EXPECT_TRUE(g_last_upload_bytes
.Get().find("posted-text") ==
281 EXPECT_TRUE(g_last_upload_bytes
.Get().find("text-entered") ==
284 if (password_present
) {
285 EXPECT_TRUE(g_last_upload_bytes
.Get().find("posted-password") !=
287 EXPECT_TRUE(g_last_upload_bytes
.Get().find("password-entered") !=
290 EXPECT_TRUE(g_last_upload_bytes
.Get().find("posted-password") ==
292 EXPECT_TRUE(g_last_upload_bytes
.Get().find("password-entered") ==
297 void CloseBrowserSynchronously(Browser
* browser
, bool close_all_windows
) {
298 content::WindowedNotificationObserver
observer(
299 chrome::NOTIFICATION_BROWSER_CLOSED
,
300 content::NotificationService::AllSources());
301 if (close_all_windows
)
302 chrome::CloseAllBrowsers();
304 browser
->window()->Close();
305 #if defined(OS_MACOSX)
306 // BrowserWindowController depends on the auto release pool being recycled
307 // in the message loop to delete itself, which frees the Browser object
308 // which fires this event.
309 AutoreleasePool()->Recycle();
314 virtual Browser
* QuitBrowserAndRestore(Browser
* browser
,
315 bool close_all_windows
) {
316 Profile
* profile
= browser
->profile();
318 // Close the browser.
319 chrome::IncrementKeepAliveCount();
320 CloseBrowserSynchronously(browser
, close_all_windows
);
322 SessionServiceTestHelper helper
;
324 SessionServiceFactory::GetForProfileForSessionRestore(profile
));
325 helper
.SetForceBrowserNotAliveWithNoWindows(true);
326 helper
.ReleaseService();
328 // Create a new window, which should trigger session restore.
329 ui_test_utils::BrowserAddedObserver window_observer
;
330 chrome::NewEmptyWindow(profile
, chrome::HOST_DESKTOP_TYPE_NATIVE
);
331 Browser
* new_browser
= window_observer
.WaitForSingleNewBrowser();
332 chrome::DecrementKeepAliveCount();
337 std::string
fake_server_address() {
338 return fake_server_address_
;
341 std::string
test_path() {
345 void EnableBackgroundMode() {
346 static_cast<FakeBackgroundModeManager
*>(
347 g_browser_process
->background_mode_manager())->
348 SetBackgroundModeActive(true);
351 void DisableBackgroundMode() {
352 static_cast<FakeBackgroundModeManager
*>(
353 g_browser_process
->background_mode_manager())->
354 SetBackgroundModeActive(false);
358 const std::string fake_server_address_
;
359 const std::string test_path_
;
360 const base::string16 title_pass_
;
361 const base::string16 title_storing_
;
362 const base::string16 title_error_write_failed_
;
363 const base::string16 title_error_empty_
;
365 DISALLOW_COPY_AND_ASSIGN(BetterSessionRestoreTest
);
368 class ContinueWhereILeftOffTest
: public BetterSessionRestoreTest
{
370 ContinueWhereILeftOffTest() { }
372 void SetUpOnMainThread() override
{
373 BetterSessionRestoreTest::SetUpOnMainThread();
374 SessionStartupPref::SetStartupPref(
375 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST
));
379 Browser
* QuitBrowserAndRestore(Browser
* browser
,
380 bool close_all_windows
) override
{
381 SessionRestoreTestHelper session_restore_observer
;
382 Browser
* new_browser
= BetterSessionRestoreTest::QuitBrowserAndRestore(
383 browser
, close_all_windows
);
384 session_restore_observer
.Wait();
388 DISALLOW_COPY_AND_ASSIGN(ContinueWhereILeftOffTest
);
391 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PRE_SessionCookies
) {
392 // Set the startup preference to "continue where I left off" and visit a page
393 // which stores a session cookie.
394 StoreDataWithPage("session_cookies.html");
397 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, SessionCookies
) {
398 // The browsing session will be continued; just wait for the page to reload
399 // and check the stored data.
400 CheckReloadedPageRestored();
403 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PRE_SessionStorage
) {
404 StoreDataWithPage("session_storage.html");
407 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, SessionStorage
) {
408 CheckReloadedPageRestored();
411 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
412 PRE_PRE_LocalStorageClearedOnExit
) {
413 StoreDataWithPage("local_storage.html");
416 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
417 PRE_LocalStorageClearedOnExit
) {
418 // Normally localStorage is restored.
419 CheckReloadedPageRestored();
420 // ... but not if it's set to clear on exit.
421 CookieSettings::Factory::GetForProfile(browser()->profile())->
422 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
425 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, LocalStorageClearedOnExit
) {
426 CheckReloadedPageNotRestored();
429 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
430 PRE_PRE_CookiesClearedOnExit
) {
431 StoreDataWithPage("cookies.html");
434 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PRE_CookiesClearedOnExit
) {
435 // Normally cookies are restored.
436 CheckReloadedPageRestored();
437 // ... but not if the content setting is set to clear on exit.
438 CookieSettings::Factory::GetForProfile(browser()->profile())->
439 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
442 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, CookiesClearedOnExit
) {
443 CheckReloadedPageNotRestored();
446 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PRE_Post
) {
447 PostFormWithPage("post.html", false);
450 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, Post
) {
451 CheckFormRestored(true, false);
454 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PRE_PostWithPassword
) {
455 PostFormWithPage("post_with_password.html", true);
458 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PostWithPassword
) {
459 CheckReloadedPageRestored();
460 // The form data contained passwords, so it's removed completely.
461 CheckFormRestored(false, false);
464 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, SessionCookiesBrowserClose
) {
465 // Set the startup preference to "continue where I left off" and visit a page
466 // which stores a session cookie.
467 StoreDataWithPage("session_cookies.html");
468 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
469 // The browsing session will be continued; just wait for the page to reload
470 // and check the stored data.
471 CheckReloadedPageRestored(new_browser
);
474 // Test that leaving a popup open will not prevent session restore.
475 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
476 SessionCookiesBrowserCloseWithPopupOpen
) {
477 // Set the startup preference to "continue where I left off" and visit a page
478 // which stores a session cookie.
479 StoreDataWithPage("session_cookies.html");
480 Browser
* popup
= new Browser(Browser::CreateParams(
482 browser()->profile(),
483 chrome::HOST_DESKTOP_TYPE_NATIVE
));
484 popup
->window()->Show();
486 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
487 // The browsing session will be continued; just wait for the page to reload
488 // and check the stored data.
489 CheckReloadedPageRestored(new_browser
);
491 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
492 CookiesClearedOnBrowserClose
) {
493 StoreDataWithPage("cookies.html");
494 // Normally cookies are restored.
495 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
496 CheckReloadedPageRestored(new_browser
);
497 // ... but not if the content setting is set to clear on exit.
498 CookieSettings::Factory::GetForProfile(new_browser
->profile())->
499 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
500 // ... unless background mode is active.
501 EnableBackgroundMode();
502 new_browser
= QuitBrowserAndRestore(new_browser
, false);
503 CheckReloadedPageRestored(new_browser
);
505 DisableBackgroundMode();
506 new_browser
= QuitBrowserAndRestore(new_browser
, false);
507 if (browser_defaults::kBrowserAliveWithNoWindows
)
508 CheckReloadedPageRestored(new_browser
);
510 CheckReloadedPageNotRestored(new_browser
);
513 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PostBrowserClose
) {
514 PostFormWithPage("post.html", false);
515 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
516 CheckFormRestored(new_browser
, true, false);
519 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
520 PostWithPasswordBrowserClose
) {
521 PostFormWithPage("post_with_password.html", true);
522 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
523 CheckReloadedPageRestored(new_browser
);
524 // The form data contained passwords, so it's removed completely.
525 CheckFormRestored(new_browser
, false, false);
528 // Check that session cookies are cleared on a wrench menu quit.
529 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
530 SessionCookiesCloseAllBrowsers
) {
531 // Set the startup preference to "continue where I left off" and visit a page
532 // which stores a session cookie.
533 StoreDataWithPage("session_cookies.html");
534 Browser
* new_browser
= QuitBrowserAndRestore(browser(), true);
535 // The browsing session will be continued; just wait for the page to reload
536 // and check the stored data.
537 CheckReloadedPageRestored(new_browser
);
540 // Check that cookies are cleared on a wrench menu quit only if cookies are set
541 // to current session only, regardless of whether background mode is enabled.
542 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
543 CookiesClearedOnCloseAllBrowsers
) {
544 StoreDataWithPage("cookies.html");
545 // Normally cookies are restored.
546 Browser
* new_browser
= QuitBrowserAndRestore(browser(), true);
547 CheckReloadedPageRestored(new_browser
);
548 // ... but not if the content setting is set to clear on exit.
549 CookieSettings::Factory::GetForProfile(new_browser
->profile())->
550 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
551 // ... even if background mode is active.
552 EnableBackgroundMode();
553 new_browser
= QuitBrowserAndRestore(new_browser
, true);
554 CheckReloadedPageNotRestored(new_browser
);
556 DisableBackgroundMode();
557 new_browser
= QuitBrowserAndRestore(new_browser
, true);
558 CheckReloadedPageNotRestored(new_browser
);
561 // Check that form data is restored after wrench menu quit.
562 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PostCloseAllBrowsers
) {
563 PostFormWithPage("post.html", false);
564 Browser
* new_browser
= QuitBrowserAndRestore(browser(), true);
565 CheckFormRestored(new_browser
, true, false);
568 // Check that form data with a password field is cleared after wrench menu quit.
569 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
570 PostWithPasswordCloseAllBrowsers
) {
571 PostFormWithPage("post_with_password.html", true);
572 Browser
* new_browser
= QuitBrowserAndRestore(browser(), true);
573 CheckReloadedPageRestored(new_browser
);
574 // The form data contained passwords, so it's removed completely.
575 CheckFormRestored(new_browser
, false, false);
578 // ChromeOS does not override the SessionStartupPreference upon controlled
580 #if !defined(OS_CHROMEOS)
581 class RestartTest
: public BetterSessionRestoreTest
{
584 ~RestartTest() override
{}
588 // Simulate restarting the browser, but let the test exit peacefully.
589 for (chrome::BrowserIterator it
; !it
.done(); it
.Next())
590 content::BrowserContext::SaveSessionState(it
->profile());
591 PrefService
* pref_service
= g_browser_process
->local_state();
592 pref_service
->SetBoolean(prefs::kWasRestarted
, true);
594 if (pref_service
->HasPrefPath(prefs::kRelaunchMode
))
595 pref_service
->ClearPref(prefs::kRelaunchMode
);
600 DISALLOW_COPY_AND_ASSIGN(RestartTest
);
603 IN_PROC_BROWSER_TEST_F(RestartTest
, PRE_SessionCookies
) {
604 StoreDataWithPage("session_cookies.html");
608 IN_PROC_BROWSER_TEST_F(RestartTest
, SessionCookies
) {
609 CheckReloadedPageRestored();
612 IN_PROC_BROWSER_TEST_F(RestartTest
, PRE_SessionStorage
) {
613 StoreDataWithPage("session_storage.html");
617 IN_PROC_BROWSER_TEST_F(RestartTest
, SessionStorage
) {
618 CheckReloadedPageRestored();
621 IN_PROC_BROWSER_TEST_F(RestartTest
, PRE_LocalStorageClearedOnExit
) {
622 StoreDataWithPage("local_storage.html");
623 CookieSettings::Factory::GetForProfile(browser()->profile())->
624 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
628 IN_PROC_BROWSER_TEST_F(RestartTest
, LocalStorageClearedOnExit
) {
629 CheckReloadedPageRestored();
632 IN_PROC_BROWSER_TEST_F(RestartTest
, PRE_CookiesClearedOnExit
) {
633 StoreDataWithPage("cookies.html");
634 CookieSettings::Factory::GetForProfile(browser()->profile())->
635 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
639 IN_PROC_BROWSER_TEST_F(RestartTest
, CookiesClearedOnExit
) {
640 CheckReloadedPageRestored();
643 IN_PROC_BROWSER_TEST_F(RestartTest
, PRE_Post
) {
644 PostFormWithPage("post.html", false);
648 IN_PROC_BROWSER_TEST_F(RestartTest
, Post
) {
649 CheckFormRestored(true, false);
652 IN_PROC_BROWSER_TEST_F(RestartTest
, PRE_PostWithPassword
) {
653 PostFormWithPage("post_with_password.html", true);
657 IN_PROC_BROWSER_TEST_F(RestartTest
, PostWithPassword
) {
658 // The form data contained passwords, so it's removed completely.
659 CheckFormRestored(false, false);
663 // These tests ensure that the Better Session Restore features are not triggered
664 // when they shouldn't be.
665 class NoSessionRestoreTest
: public BetterSessionRestoreTest
{
667 NoSessionRestoreTest() { }
669 void SetUpOnMainThread() override
{
670 BetterSessionRestoreTest::SetUpOnMainThread();
671 SessionStartupPref::SetStartupPref(
672 browser()->profile(), SessionStartupPref(SessionStartupPref::DEFAULT
));
676 DISALLOW_COPY_AND_ASSIGN(NoSessionRestoreTest
);
679 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, PRE_SessionCookies
) {
680 StoreDataWithPage("session_cookies.html");
683 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, SessionCookies
) {
684 content::WebContents
* web_contents
=
685 browser()->tab_strip_model()->GetActiveWebContents();
686 EXPECT_EQ(std::string(url::kAboutBlankURL
), web_contents
->GetURL().spec());
687 // When we navigate to the page again, it doens't see the data previously
689 StoreDataWithPage("session_cookies.html");
692 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, PRE_SessionStorage
) {
693 StoreDataWithPage("session_storage.html");
696 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, SessionStorage
) {
697 content::WebContents
* web_contents
=
698 browser()->tab_strip_model()->GetActiveWebContents();
699 EXPECT_EQ(std::string(url::kAboutBlankURL
), web_contents
->GetURL().spec());
700 StoreDataWithPage("session_storage.html");
703 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
,
704 PRE_PRE_LocalStorageClearedOnExit
) {
705 StoreDataWithPage("local_storage.html");
708 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, PRE_LocalStorageClearedOnExit
) {
709 // Normally localStorage is persisted.
710 content::WebContents
* web_contents
=
711 browser()->tab_strip_model()->GetActiveWebContents();
712 EXPECT_EQ(std::string(url::kAboutBlankURL
), web_contents
->GetURL().spec());
713 NavigateAndCheckStoredData("local_storage.html");
714 // ... but not if it's set to clear on exit.
715 CookieSettings::Factory::GetForProfile(browser()->profile())->
716 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
719 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, LocalStorageClearedOnExit
) {
720 content::WebContents
* web_contents
=
721 browser()->tab_strip_model()->GetActiveWebContents();
722 EXPECT_EQ(std::string(url::kAboutBlankURL
), web_contents
->GetURL().spec());
723 StoreDataWithPage("local_storage.html");
726 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, PRE_PRE_CookiesClearedOnExit
) {
727 StoreDataWithPage("cookies.html");
730 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, PRE_CookiesClearedOnExit
) {
731 // Normally cookies are restored.
732 content::WebContents
* web_contents
=
733 browser()->tab_strip_model()->GetActiveWebContents();
734 EXPECT_EQ(std::string(url::kAboutBlankURL
), web_contents
->GetURL().spec());
735 NavigateAndCheckStoredData("cookies.html");
736 // ... but not if the content setting is set to clear on exit.
737 CookieSettings::Factory::GetForProfile(browser()->profile())->
738 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
741 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, CookiesClearedOnExit
) {
742 content::WebContents
* web_contents
=
743 browser()->tab_strip_model()->GetActiveWebContents();
744 EXPECT_EQ(std::string(url::kAboutBlankURL
), web_contents
->GetURL().spec());
745 StoreDataWithPage("local_storage.html");
748 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, SessionCookiesBrowserClose
) {
749 StoreDataWithPage("session_cookies.html");
750 EnableBackgroundMode();
751 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
752 NavigateAndCheckStoredData(new_browser
, "session_cookies.html");
753 DisableBackgroundMode();
754 new_browser
= QuitBrowserAndRestore(new_browser
, false);
755 if (browser_defaults::kBrowserAliveWithNoWindows
)
756 NavigateAndCheckStoredData(new_browser
, "session_cookies.html");
758 StoreDataWithPage(new_browser
, "session_cookies.html");
761 // Tests that session cookies are not cleared when only a popup window is open.
762 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
,
763 SessionCookiesBrowserCloseWithPopupOpen
) {
764 StoreDataWithPage("session_cookies.html");
765 Browser
* popup
= new Browser(Browser::CreateParams(
767 browser()->profile(),
768 chrome::HOST_DESKTOP_TYPE_NATIVE
));
769 popup
->window()->Show();
770 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
771 NavigateAndCheckStoredData(new_browser
, "session_cookies.html");
774 // Tests that session cookies are cleared if the last window to close is a
776 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
,
777 SessionCookiesBrowserClosePopupLast
) {
778 StoreDataWithPage("session_cookies.html");
779 Browser
* popup
= new Browser(Browser::CreateParams(
781 browser()->profile(),
782 chrome::HOST_DESKTOP_TYPE_NATIVE
));
783 popup
->window()->Show();
784 CloseBrowserSynchronously(browser(), false);
785 Browser
* new_browser
= QuitBrowserAndRestore(popup
, false);
786 if (browser_defaults::kBrowserAliveWithNoWindows
)
787 NavigateAndCheckStoredData(new_browser
, "session_cookies.html");
789 StoreDataWithPage(new_browser
, "session_cookies.html");
792 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, CookiesClearedOnBrowserClose
) {
793 StoreDataWithPage("cookies.html");
795 // Normally cookies are restored.
796 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
797 NavigateAndCheckStoredData(new_browser
, "cookies.html");
799 // ... but not if the content setting is set to clear on exit.
800 CookieSettings::Factory::GetForProfile(new_browser
->profile())->
801 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
802 // ... unless background mode is active.
803 EnableBackgroundMode();
804 new_browser
= QuitBrowserAndRestore(new_browser
, false);
805 NavigateAndCheckStoredData(new_browser
, "cookies.html");
806 DisableBackgroundMode();
807 new_browser
= QuitBrowserAndRestore(new_browser
, false);
808 if (browser_defaults::kBrowserAliveWithNoWindows
)
809 NavigateAndCheckStoredData(new_browser
, "cookies.html");
811 StoreDataWithPage(new_browser
, "cookies.html");
814 // Check that session cookies are cleared on a wrench menu quit.
815 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, SessionCookiesCloseAllBrowsers
) {
816 StoreDataWithPage("session_cookies.html");
817 EnableBackgroundMode();
818 Browser
* new_browser
= QuitBrowserAndRestore(browser(), true);
819 StoreDataWithPage(new_browser
, "session_cookies.html");
820 DisableBackgroundMode();
821 new_browser
= QuitBrowserAndRestore(new_browser
, true);
822 StoreDataWithPage(new_browser
, "session_cookies.html");
825 // Check that cookies are cleared on a wrench menu quit only if cookies are set
826 // to current session only, regardless of whether background mode is enabled.
827 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, CookiesClearedOnCloseAllBrowsers
) {
828 StoreDataWithPage("cookies.html");
830 // Normally cookies are restored.
831 Browser
* new_browser
= QuitBrowserAndRestore(browser(), true);
832 NavigateAndCheckStoredData(new_browser
, "cookies.html");
834 // ... but not if the content setting is set to clear on exit.
835 CookieSettings::Factory::GetForProfile(new_browser
->profile())->
836 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
837 // ... even if background mode is active.
838 EnableBackgroundMode();
839 new_browser
= QuitBrowserAndRestore(new_browser
, true);
840 StoreDataWithPage(new_browser
, "cookies.html");
841 DisableBackgroundMode();
842 new_browser
= QuitBrowserAndRestore(new_browser
, true);
843 StoreDataWithPage(new_browser
, "cookies.html");