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_backend.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
->element_readers();
85 for (size_t i
= 0; i
< readers
.size(); ++i
) {
86 const net::UploadBytesElementReader
* bytes_reader
=
87 readers
[i
]->AsBytesReader();
89 g_last_upload_bytes
.Get() +=
90 std::string(bytes_reader
->bytes(), bytes_reader
->length());
94 return new net::URLRequestTestJob(
95 request
, network_delegate
, net::URLRequestTestJob::test_headers(),
96 "<html><head><title>PASS</title></head><body>Data posted</body></html>",
100 class FakeBackgroundModeManager
: public BackgroundModeManager
{
102 FakeBackgroundModeManager()
103 : BackgroundModeManager(
104 CommandLine::ForCurrentProcess(),
105 &g_browser_process
->profile_manager()->GetProfileInfoCache()),
106 background_mode_active_(false) {}
108 void SetBackgroundModeActive(bool active
) {
109 background_mode_active_
= active
;
112 virtual bool IsBackgroundModeActive() OVERRIDE
{
113 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 virtual 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 virtual void SetUpOnMainThread() OVERRIDE
{
373 BetterSessionRestoreTest::SetUpOnMainThread();
374 SessionStartupPref::SetStartupPref(
375 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST
));
379 virtual Browser
* QuitBrowserAndRestore(Browser
* browser
,
380 bool close_all_windows
) OVERRIDE
{
381 content::WindowedNotificationObserver
session_restore_observer(
382 chrome::NOTIFICATION_SESSION_RESTORE_DONE
,
383 content::NotificationService::AllSources());
384 Browser
* new_browser
= BetterSessionRestoreTest::QuitBrowserAndRestore(
385 browser
, close_all_windows
);
386 session_restore_observer
.Wait();
390 DISALLOW_COPY_AND_ASSIGN(ContinueWhereILeftOffTest
);
393 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PRE_SessionCookies
) {
394 // Set the startup preference to "continue where I left off" and visit a page
395 // which stores a session cookie.
396 StoreDataWithPage("session_cookies.html");
399 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, SessionCookies
) {
400 // The browsing session will be continued; just wait for the page to reload
401 // and check the stored data.
402 CheckReloadedPageRestored();
405 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PRE_SessionStorage
) {
406 StoreDataWithPage("session_storage.html");
409 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, SessionStorage
) {
410 CheckReloadedPageRestored();
413 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
414 PRE_PRE_LocalStorageClearedOnExit
) {
415 StoreDataWithPage("local_storage.html");
418 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
419 PRE_LocalStorageClearedOnExit
) {
420 // Normally localStorage is restored.
421 CheckReloadedPageRestored();
422 // ... but not if it's set to clear on exit.
423 CookieSettings::Factory::GetForProfile(browser()->profile())->
424 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
427 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, LocalStorageClearedOnExit
) {
428 CheckReloadedPageNotRestored();
431 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
432 PRE_PRE_CookiesClearedOnExit
) {
433 StoreDataWithPage("cookies.html");
436 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PRE_CookiesClearedOnExit
) {
437 // Normally cookies are restored.
438 CheckReloadedPageRestored();
439 // ... but not if the content setting is set to clear on exit.
440 CookieSettings::Factory::GetForProfile(browser()->profile())->
441 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
444 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, CookiesClearedOnExit
) {
445 CheckReloadedPageNotRestored();
448 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PRE_Post
) {
449 PostFormWithPage("post.html", false);
452 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, Post
) {
453 CheckFormRestored(true, false);
456 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PRE_PostWithPassword
) {
457 PostFormWithPage("post_with_password.html", true);
460 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PostWithPassword
) {
461 CheckReloadedPageRestored();
462 // The form data contained passwords, so it's removed completely.
463 CheckFormRestored(false, false);
466 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, SessionCookiesBrowserClose
) {
467 // Set the startup preference to "continue where I left off" and visit a page
468 // which stores a session cookie.
469 StoreDataWithPage("session_cookies.html");
470 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
471 // The browsing session will be continued; just wait for the page to reload
472 // and check the stored data.
473 CheckReloadedPageRestored(new_browser
);
476 // Test that leaving a popup open will not prevent session restore.
477 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
478 SessionCookiesBrowserCloseWithPopupOpen
) {
479 // Set the startup preference to "continue where I left off" and visit a page
480 // which stores a session cookie.
481 StoreDataWithPage("session_cookies.html");
482 Browser
* popup
= new Browser(Browser::CreateParams(
484 browser()->profile(),
485 chrome::HOST_DESKTOP_TYPE_NATIVE
));
486 popup
->window()->Show();
488 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
489 // The browsing session will be continued; just wait for the page to reload
490 // and check the stored data.
491 CheckReloadedPageRestored(new_browser
);
493 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
494 CookiesClearedOnBrowserClose
) {
495 StoreDataWithPage("cookies.html");
496 // Normally cookies are restored.
497 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
498 CheckReloadedPageRestored(new_browser
);
499 // ... but not if the content setting is set to clear on exit.
500 CookieSettings::Factory::GetForProfile(new_browser
->profile())->
501 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
502 // ... unless background mode is active.
503 EnableBackgroundMode();
504 new_browser
= QuitBrowserAndRestore(new_browser
, false);
505 CheckReloadedPageRestored(new_browser
);
507 DisableBackgroundMode();
508 new_browser
= QuitBrowserAndRestore(new_browser
, false);
509 if (browser_defaults::kBrowserAliveWithNoWindows
)
510 CheckReloadedPageRestored(new_browser
);
512 CheckReloadedPageNotRestored(new_browser
);
515 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PostBrowserClose
) {
516 PostFormWithPage("post.html", false);
517 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
518 CheckFormRestored(new_browser
, true, false);
521 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
522 PostWithPasswordBrowserClose
) {
523 PostFormWithPage("post_with_password.html", true);
524 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
525 CheckReloadedPageRestored(new_browser
);
526 // The form data contained passwords, so it's removed completely.
527 CheckFormRestored(new_browser
, false, false);
530 // Check that session cookies are cleared on a wrench menu quit.
531 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
532 SessionCookiesCloseAllBrowsers
) {
533 // Set the startup preference to "continue where I left off" and visit a page
534 // which stores a session cookie.
535 StoreDataWithPage("session_cookies.html");
536 Browser
* new_browser
= QuitBrowserAndRestore(browser(), true);
537 // The browsing session will be continued; just wait for the page to reload
538 // and check the stored data.
539 CheckReloadedPageRestored(new_browser
);
542 // Check that cookies are cleared on a wrench menu quit only if cookies are set
543 // to current session only, regardless of whether background mode is enabled.
544 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
545 CookiesClearedOnCloseAllBrowsers
) {
546 StoreDataWithPage("cookies.html");
547 // Normally cookies are restored.
548 Browser
* new_browser
= QuitBrowserAndRestore(browser(), true);
549 CheckReloadedPageRestored(new_browser
);
550 // ... but not if the content setting is set to clear on exit.
551 CookieSettings::Factory::GetForProfile(new_browser
->profile())->
552 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
553 // ... even if background mode is active.
554 EnableBackgroundMode();
555 new_browser
= QuitBrowserAndRestore(new_browser
, true);
556 CheckReloadedPageNotRestored(new_browser
);
558 DisableBackgroundMode();
559 new_browser
= QuitBrowserAndRestore(new_browser
, true);
560 CheckReloadedPageNotRestored(new_browser
);
563 // Check that form data is restored after wrench menu quit.
564 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
, PostCloseAllBrowsers
) {
565 PostFormWithPage("post.html", false);
566 Browser
* new_browser
= QuitBrowserAndRestore(browser(), true);
567 CheckFormRestored(new_browser
, true, false);
570 // Check that form data with a password field is cleared after wrench menu quit.
571 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest
,
572 PostWithPasswordCloseAllBrowsers
) {
573 PostFormWithPage("post_with_password.html", true);
574 Browser
* new_browser
= QuitBrowserAndRestore(browser(), true);
575 CheckReloadedPageRestored(new_browser
);
576 // The form data contained passwords, so it's removed completely.
577 CheckFormRestored(new_browser
, false, false);
580 class RestartTest
: public BetterSessionRestoreTest
{
583 virtual ~RestartTest() { }
586 // Simluate restarting the browser, but let the test exit peacefully.
587 for (chrome::BrowserIterator it
; !it
.done(); it
.Next())
588 content::BrowserContext::SaveSessionState(it
->profile());
589 PrefService
* pref_service
= g_browser_process
->local_state();
590 pref_service
->SetBoolean(prefs::kWasRestarted
, true);
592 if (pref_service
->HasPrefPath(prefs::kRelaunchMode
))
593 pref_service
->ClearPref(prefs::kRelaunchMode
);
598 DISALLOW_COPY_AND_ASSIGN(RestartTest
);
601 IN_PROC_BROWSER_TEST_F(RestartTest
, PRE_SessionCookies
) {
602 StoreDataWithPage("session_cookies.html");
606 IN_PROC_BROWSER_TEST_F(RestartTest
, SessionCookies
) {
607 CheckReloadedPageRestored();
610 IN_PROC_BROWSER_TEST_F(RestartTest
, PRE_SessionStorage
) {
611 StoreDataWithPage("session_storage.html");
615 IN_PROC_BROWSER_TEST_F(RestartTest
, SessionStorage
) {
616 CheckReloadedPageRestored();
619 IN_PROC_BROWSER_TEST_F(RestartTest
, PRE_LocalStorageClearedOnExit
) {
620 StoreDataWithPage("local_storage.html");
621 CookieSettings::Factory::GetForProfile(browser()->profile())->
622 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
626 IN_PROC_BROWSER_TEST_F(RestartTest
, LocalStorageClearedOnExit
) {
627 CheckReloadedPageRestored();
630 IN_PROC_BROWSER_TEST_F(RestartTest
, PRE_CookiesClearedOnExit
) {
631 StoreDataWithPage("cookies.html");
632 CookieSettings::Factory::GetForProfile(browser()->profile())->
633 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
637 IN_PROC_BROWSER_TEST_F(RestartTest
, CookiesClearedOnExit
) {
638 CheckReloadedPageRestored();
641 IN_PROC_BROWSER_TEST_F(RestartTest
, PRE_Post
) {
642 PostFormWithPage("post.html", false);
646 IN_PROC_BROWSER_TEST_F(RestartTest
, Post
) {
647 CheckFormRestored(true, false);
650 IN_PROC_BROWSER_TEST_F(RestartTest
, PRE_PostWithPassword
) {
651 PostFormWithPage("post_with_password.html", true);
655 IN_PROC_BROWSER_TEST_F(RestartTest
, PostWithPassword
) {
656 // The form data contained passwords, so it's removed completely.
657 CheckFormRestored(false, false);
660 // These tests ensure that the Better Session Restore features are not triggered
661 // when they shouldn't be.
662 class NoSessionRestoreTest
: public BetterSessionRestoreTest
{
664 NoSessionRestoreTest() { }
666 virtual void SetUpOnMainThread() OVERRIDE
{
667 BetterSessionRestoreTest::SetUpOnMainThread();
668 SessionStartupPref::SetStartupPref(
669 browser()->profile(), SessionStartupPref(SessionStartupPref::DEFAULT
));
673 DISALLOW_COPY_AND_ASSIGN(NoSessionRestoreTest
);
676 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, PRE_SessionCookies
) {
677 StoreDataWithPage("session_cookies.html");
680 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, SessionCookies
) {
681 content::WebContents
* web_contents
=
682 browser()->tab_strip_model()->GetActiveWebContents();
683 EXPECT_EQ(std::string(url::kAboutBlankURL
), web_contents
->GetURL().spec());
684 // When we navigate to the page again, it doens't see the data previously
686 StoreDataWithPage("session_cookies.html");
689 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, PRE_SessionStorage
) {
690 StoreDataWithPage("session_storage.html");
693 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, SessionStorage
) {
694 content::WebContents
* web_contents
=
695 browser()->tab_strip_model()->GetActiveWebContents();
696 EXPECT_EQ(std::string(url::kAboutBlankURL
), web_contents
->GetURL().spec());
697 StoreDataWithPage("session_storage.html");
700 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
,
701 PRE_PRE_LocalStorageClearedOnExit
) {
702 StoreDataWithPage("local_storage.html");
705 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, PRE_LocalStorageClearedOnExit
) {
706 // Normally localStorage is persisted.
707 content::WebContents
* web_contents
=
708 browser()->tab_strip_model()->GetActiveWebContents();
709 EXPECT_EQ(std::string(url::kAboutBlankURL
), web_contents
->GetURL().spec());
710 NavigateAndCheckStoredData("local_storage.html");
711 // ... but not if it's set to clear on exit.
712 CookieSettings::Factory::GetForProfile(browser()->profile())->
713 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
716 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, LocalStorageClearedOnExit
) {
717 content::WebContents
* web_contents
=
718 browser()->tab_strip_model()->GetActiveWebContents();
719 EXPECT_EQ(std::string(url::kAboutBlankURL
), web_contents
->GetURL().spec());
720 StoreDataWithPage("local_storage.html");
723 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, PRE_PRE_CookiesClearedOnExit
) {
724 StoreDataWithPage("cookies.html");
727 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, PRE_CookiesClearedOnExit
) {
728 // Normally cookies are restored.
729 content::WebContents
* web_contents
=
730 browser()->tab_strip_model()->GetActiveWebContents();
731 EXPECT_EQ(std::string(url::kAboutBlankURL
), web_contents
->GetURL().spec());
732 NavigateAndCheckStoredData("cookies.html");
733 // ... but not if the content setting is set to clear on exit.
734 CookieSettings::Factory::GetForProfile(browser()->profile())->
735 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
738 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, CookiesClearedOnExit
) {
739 content::WebContents
* web_contents
=
740 browser()->tab_strip_model()->GetActiveWebContents();
741 EXPECT_EQ(std::string(url::kAboutBlankURL
), web_contents
->GetURL().spec());
742 StoreDataWithPage("local_storage.html");
745 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, SessionCookiesBrowserClose
) {
746 StoreDataWithPage("session_cookies.html");
747 EnableBackgroundMode();
748 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
749 NavigateAndCheckStoredData(new_browser
, "session_cookies.html");
750 DisableBackgroundMode();
751 new_browser
= QuitBrowserAndRestore(new_browser
, false);
752 if (browser_defaults::kBrowserAliveWithNoWindows
)
753 NavigateAndCheckStoredData(new_browser
, "session_cookies.html");
755 StoreDataWithPage(new_browser
, "session_cookies.html");
758 // Tests that session cookies are not cleared when only a popup window is open.
759 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
,
760 SessionCookiesBrowserCloseWithPopupOpen
) {
761 StoreDataWithPage("session_cookies.html");
762 Browser
* popup
= new Browser(Browser::CreateParams(
764 browser()->profile(),
765 chrome::HOST_DESKTOP_TYPE_NATIVE
));
766 popup
->window()->Show();
767 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
768 NavigateAndCheckStoredData(new_browser
, "session_cookies.html");
771 // Tests that session cookies are cleared if the last window to close is a
773 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
,
774 SessionCookiesBrowserClosePopupLast
) {
775 StoreDataWithPage("session_cookies.html");
776 Browser
* popup
= new Browser(Browser::CreateParams(
778 browser()->profile(),
779 chrome::HOST_DESKTOP_TYPE_NATIVE
));
780 popup
->window()->Show();
781 CloseBrowserSynchronously(browser(), false);
782 Browser
* new_browser
= QuitBrowserAndRestore(popup
, false);
783 if (browser_defaults::kBrowserAliveWithNoWindows
)
784 NavigateAndCheckStoredData(new_browser
, "session_cookies.html");
786 StoreDataWithPage(new_browser
, "session_cookies.html");
789 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, CookiesClearedOnBrowserClose
) {
790 StoreDataWithPage("cookies.html");
792 // Normally cookies are restored.
793 Browser
* new_browser
= QuitBrowserAndRestore(browser(), false);
794 NavigateAndCheckStoredData(new_browser
, "cookies.html");
796 // ... but not if the content setting is set to clear on exit.
797 CookieSettings::Factory::GetForProfile(new_browser
->profile())->
798 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
799 // ... unless background mode is active.
800 EnableBackgroundMode();
801 new_browser
= QuitBrowserAndRestore(new_browser
, false);
802 NavigateAndCheckStoredData(new_browser
, "cookies.html");
803 DisableBackgroundMode();
804 new_browser
= QuitBrowserAndRestore(new_browser
, false);
805 if (browser_defaults::kBrowserAliveWithNoWindows
)
806 NavigateAndCheckStoredData(new_browser
, "cookies.html");
808 StoreDataWithPage(new_browser
, "cookies.html");
811 // Check that session cookies are cleared on a wrench menu quit.
812 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, SessionCookiesCloseAllBrowsers
) {
813 StoreDataWithPage("session_cookies.html");
814 EnableBackgroundMode();
815 Browser
* new_browser
= QuitBrowserAndRestore(browser(), true);
816 StoreDataWithPage(new_browser
, "session_cookies.html");
817 DisableBackgroundMode();
818 new_browser
= QuitBrowserAndRestore(new_browser
, true);
819 StoreDataWithPage(new_browser
, "session_cookies.html");
822 // Check that cookies are cleared on a wrench menu quit only if cookies are set
823 // to current session only, regardless of whether background mode is enabled.
824 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest
, CookiesClearedOnCloseAllBrowsers
) {
825 StoreDataWithPage("cookies.html");
827 // Normally cookies are restored.
828 Browser
* new_browser
= QuitBrowserAndRestore(browser(), true);
829 NavigateAndCheckStoredData(new_browser
, "cookies.html");
831 // ... but not if the content setting is set to clear on exit.
832 CookieSettings::Factory::GetForProfile(new_browser
->profile())->
833 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
834 // ... even if background mode is active.
835 EnableBackgroundMode();
836 new_browser
= QuitBrowserAndRestore(new_browser
, true);
837 StoreDataWithPage(new_browser
, "cookies.html");
838 DisableBackgroundMode();
839 new_browser
= QuitBrowserAndRestore(new_browser
, true);
840 StoreDataWithPage(new_browser
, "cookies.html");