Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sessions / better_session_restore_browsertest.cc
blob66f0f58aa7f21432092af310ed579d0f0e5e298f
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/file_util.h"
7 #include "base/files/file_path.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/confirm_infobar_delegate.h"
18 #include "chrome/browser/infobars/infobar_service.h"
19 #include "chrome/browser/lifetime/application_lifetime.h"
20 #include "chrome/browser/prefs/session_startup_pref.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/profiles/profile_impl.h"
23 #include "chrome/browser/profiles/profile_manager.h"
24 #include "chrome/browser/sessions/session_backend.h"
25 #include "chrome/browser/sessions/session_service_factory.h"
26 #include "chrome/browser/sessions/session_service_test_helper.h"
27 #include "chrome/browser/ui/browser.h"
28 #include "chrome/browser/ui/browser_commands.h"
29 #include "chrome/browser/ui/browser_iterator.h"
30 #include "chrome/browser/ui/browser_window.h"
31 #include "chrome/browser/ui/startup/startup_browser_creator.h"
32 #include "chrome/browser/ui/tabs/tab_strip_model.h"
33 #include "chrome/common/chrome_switches.h"
34 #include "chrome/common/content_settings.h"
35 #include "chrome/common/pref_names.h"
36 #include "chrome/common/url_constants.h"
37 #include "chrome/test/base/in_process_browser_test.h"
38 #include "chrome/test/base/ui_test_utils.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"
51 #endif
53 namespace {
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();
82 if (upload_data) {
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();
88 if (bytes_reader) {
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>",
97 true);
100 class FakeBackgroundModeManager : public BackgroundModeManager {
101 public:
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_;
116 private:
117 bool background_mode_active_;
121 } // namespace
123 class BetterSessionRestoreTest : public InProcessBrowserTest {
124 public:
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));
143 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),
154 &URLRequestFaker);
156 net::URLRequestFilter::GetInstance()->AddUrlHandler(
157 GURL(fake_server_address_ + test_path_ + "posted.php"),
158 &URLRequestFakerForPostRequests);
161 protected:
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);
241 } else {
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") !=
255 std::string::npos);
256 EXPECT_TRUE(g_last_upload_bytes.Get().find("text-entered") !=
257 std::string::npos);
258 if (password_present) {
259 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-password") !=
260 std::string::npos);
261 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") !=
262 std::string::npos);
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);
273 if (text_present) {
274 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-text") !=
275 std::string::npos);
276 EXPECT_TRUE(g_last_upload_bytes.Get().find("text-entered") !=
277 std::string::npos);
278 } else {
279 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-text") ==
280 std::string::npos);
281 EXPECT_TRUE(g_last_upload_bytes.Get().find("text-entered") ==
282 std::string::npos);
284 if (password_present) {
285 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-password") !=
286 std::string::npos);
287 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") !=
288 std::string::npos);
289 } else {
290 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-password") ==
291 std::string::npos);
292 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") ==
293 std::string::npos);
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();
303 else
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();
310 #endif
311 observer.Wait();
314 virtual Browser* QuitBrowserAndRestore(Browser* browser,
315 bool close_all_windows) {
316 Profile* profile = browser->profile();
318 // Close the browser.
319 chrome::StartKeepAlive();
320 CloseBrowserSynchronously(browser, close_all_windows);
322 SessionServiceTestHelper helper;
323 helper.SetService(
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::EndKeepAlive();
334 return new_browser;
337 std::string fake_server_address() {
338 return fake_server_address_;
341 std::string test_path() {
342 return 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);
357 private:
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 {
369 public:
370 ContinueWhereILeftOffTest() { }
372 virtual void SetUpOnMainThread() OVERRIDE {
373 BetterSessionRestoreTest::SetUpOnMainThread();
374 SessionStartupPref::SetStartupPref(
375 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
378 protected:
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();
387 return new_browser;
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 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
477 CookiesClearedOnBrowserClose) {
478 StoreDataWithPage("cookies.html");
479 // Normally cookies are restored.
480 Browser* new_browser = QuitBrowserAndRestore(browser(), false);
481 CheckReloadedPageRestored(new_browser);
482 // ... but not if the content setting is set to clear on exit.
483 CookieSettings::Factory::GetForProfile(new_browser->profile())->
484 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
485 // ... unless background mode is active.
486 EnableBackgroundMode();
487 new_browser = QuitBrowserAndRestore(new_browser, false);
488 CheckReloadedPageRestored(new_browser);
490 DisableBackgroundMode();
491 new_browser = QuitBrowserAndRestore(new_browser, false);
492 if (browser_defaults::kBrowserAliveWithNoWindows)
493 CheckReloadedPageRestored(new_browser);
494 else
495 CheckReloadedPageNotRestored(new_browser);
498 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PostBrowserClose) {
499 PostFormWithPage("post.html", false);
500 Browser* new_browser = QuitBrowserAndRestore(browser(), false);
501 CheckFormRestored(new_browser, true, false);
504 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
505 PostWithPasswordBrowserClose) {
506 PostFormWithPage("post_with_password.html", true);
507 Browser* new_browser = QuitBrowserAndRestore(browser(), false);
508 CheckReloadedPageRestored(new_browser);
509 // The form data contained passwords, so it's removed completely.
510 CheckFormRestored(new_browser, false, false);
513 // Check that session cookies are cleared on a wrench menu quit.
514 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
515 SessionCookiesCloseAllBrowsers) {
516 // Set the startup preference to "continue where I left off" and visit a page
517 // which stores a session cookie.
518 StoreDataWithPage("session_cookies.html");
519 Browser* new_browser = QuitBrowserAndRestore(browser(), true);
520 // The browsing session will be continued; just wait for the page to reload
521 // and check the stored data.
522 CheckReloadedPageRestored(new_browser);
525 // Check that cookies are cleared on a wrench menu quit only if cookies are set
526 // to current session only, regardless of whether background mode is enabled.
527 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
528 CookiesClearedOnCloseAllBrowsers) {
529 StoreDataWithPage("cookies.html");
530 // Normally cookies are restored.
531 Browser* new_browser = QuitBrowserAndRestore(browser(), true);
532 CheckReloadedPageRestored(new_browser);
533 // ... but not if the content setting is set to clear on exit.
534 CookieSettings::Factory::GetForProfile(new_browser->profile())->
535 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
536 // ... even if background mode is active.
537 EnableBackgroundMode();
538 new_browser = QuitBrowserAndRestore(new_browser, true);
539 CheckReloadedPageNotRestored(new_browser);
541 DisableBackgroundMode();
542 new_browser = QuitBrowserAndRestore(new_browser, true);
543 CheckReloadedPageNotRestored(new_browser);
546 // Check that form data is restored after wrench menu quit.
547 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PostCloseAllBrowsers) {
548 PostFormWithPage("post.html", false);
549 Browser* new_browser = QuitBrowserAndRestore(browser(), true);
550 CheckFormRestored(new_browser, true, false);
553 // Check that form data with a password field is cleared after wrench menu quit.
554 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
555 PostWithPasswordCloseAllBrowsers) {
556 PostFormWithPage("post_with_password.html", true);
557 Browser* new_browser = QuitBrowserAndRestore(browser(), true);
558 CheckReloadedPageRestored(new_browser);
559 // The form data contained passwords, so it's removed completely.
560 CheckFormRestored(new_browser, false, false);
563 class RestartTest : public BetterSessionRestoreTest {
564 public:
565 RestartTest() { }
566 virtual ~RestartTest() { }
567 protected:
568 void Restart() {
569 // Simluate restarting the browser, but let the test exit peacefully.
570 for (chrome::BrowserIterator it; !it.done(); it.Next())
571 content::BrowserContext::SaveSessionState(it->profile());
572 PrefService* pref_service = g_browser_process->local_state();
573 pref_service->SetBoolean(prefs::kWasRestarted, true);
574 #if defined(OS_WIN)
575 if (pref_service->HasPrefPath(prefs::kRelaunchMode))
576 pref_service->ClearPref(prefs::kRelaunchMode);
577 #endif
580 private:
581 DISALLOW_COPY_AND_ASSIGN(RestartTest);
584 IN_PROC_BROWSER_TEST_F(RestartTest, PRE_SessionCookies) {
585 StoreDataWithPage("session_cookies.html");
586 Restart();
589 IN_PROC_BROWSER_TEST_F(RestartTest, SessionCookies) {
590 CheckReloadedPageRestored();
593 IN_PROC_BROWSER_TEST_F(RestartTest, PRE_SessionStorage) {
594 StoreDataWithPage("session_storage.html");
595 Restart();
598 IN_PROC_BROWSER_TEST_F(RestartTest, SessionStorage) {
599 CheckReloadedPageRestored();
602 IN_PROC_BROWSER_TEST_F(RestartTest, PRE_LocalStorageClearedOnExit) {
603 StoreDataWithPage("local_storage.html");
604 CookieSettings::Factory::GetForProfile(browser()->profile())->
605 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
606 Restart();
609 IN_PROC_BROWSER_TEST_F(RestartTest, LocalStorageClearedOnExit) {
610 CheckReloadedPageRestored();
613 IN_PROC_BROWSER_TEST_F(RestartTest, PRE_CookiesClearedOnExit) {
614 StoreDataWithPage("cookies.html");
615 CookieSettings::Factory::GetForProfile(browser()->profile())->
616 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
617 Restart();
620 IN_PROC_BROWSER_TEST_F(RestartTest, CookiesClearedOnExit) {
621 CheckReloadedPageRestored();
624 IN_PROC_BROWSER_TEST_F(RestartTest, PRE_Post) {
625 PostFormWithPage("post.html", false);
626 Restart();
629 IN_PROC_BROWSER_TEST_F(RestartTest, Post) {
630 CheckFormRestored(true, false);
633 IN_PROC_BROWSER_TEST_F(RestartTest, PRE_PostWithPassword) {
634 PostFormWithPage("post_with_password.html", true);
635 Restart();
638 IN_PROC_BROWSER_TEST_F(RestartTest, PostWithPassword) {
639 // The form data contained passwords, so it's removed completely.
640 CheckFormRestored(false, false);
643 // These tests ensure that the Better Session Restore features are not triggered
644 // when they shouldn't be.
645 class NoSessionRestoreTest : public BetterSessionRestoreTest {
646 public:
647 NoSessionRestoreTest() { }
649 virtual void SetUpOnMainThread() OVERRIDE {
650 BetterSessionRestoreTest::SetUpOnMainThread();
651 SessionStartupPref::SetStartupPref(
652 browser()->profile(), SessionStartupPref(SessionStartupPref::DEFAULT));
655 private:
656 DISALLOW_COPY_AND_ASSIGN(NoSessionRestoreTest);
659 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, PRE_SessionCookies) {
660 StoreDataWithPage("session_cookies.html");
663 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, SessionCookies) {
664 content::WebContents* web_contents =
665 browser()->tab_strip_model()->GetActiveWebContents();
666 EXPECT_EQ(std::string(content::kAboutBlankURL),
667 web_contents->GetURL().spec());
668 // When we navigate to the page again, it doens't see the data previously
669 // stored.
670 StoreDataWithPage("session_cookies.html");
673 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, PRE_SessionStorage) {
674 StoreDataWithPage("session_storage.html");
677 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, SessionStorage) {
678 content::WebContents* web_contents =
679 browser()->tab_strip_model()->GetActiveWebContents();
680 EXPECT_EQ(std::string(content::kAboutBlankURL),
681 web_contents->GetURL().spec());
682 StoreDataWithPage("session_storage.html");
685 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest,
686 PRE_PRE_LocalStorageClearedOnExit) {
687 StoreDataWithPage("local_storage.html");
690 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, PRE_LocalStorageClearedOnExit) {
691 // Normally localStorage is persisted.
692 content::WebContents* web_contents =
693 browser()->tab_strip_model()->GetActiveWebContents();
694 EXPECT_EQ(std::string(content::kAboutBlankURL),
695 web_contents->GetURL().spec());
696 NavigateAndCheckStoredData("local_storage.html");
697 // ... but not if it's set to clear on exit.
698 CookieSettings::Factory::GetForProfile(browser()->profile())->
699 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
702 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, LocalStorageClearedOnExit) {
703 content::WebContents* web_contents =
704 browser()->tab_strip_model()->GetActiveWebContents();
705 EXPECT_EQ(std::string(content::kAboutBlankURL),
706 web_contents->GetURL().spec());
707 StoreDataWithPage("local_storage.html");
710 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, PRE_PRE_CookiesClearedOnExit) {
711 StoreDataWithPage("cookies.html");
714 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, PRE_CookiesClearedOnExit) {
715 // Normally cookies are restored.
716 content::WebContents* web_contents =
717 browser()->tab_strip_model()->GetActiveWebContents();
718 EXPECT_EQ(std::string(content::kAboutBlankURL),
719 web_contents->GetURL().spec());
720 NavigateAndCheckStoredData("cookies.html");
721 // ... but not if the content setting is set to clear on exit.
722 CookieSettings::Factory::GetForProfile(browser()->profile())->
723 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
726 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, CookiesClearedOnExit) {
727 content::WebContents* web_contents =
728 browser()->tab_strip_model()->GetActiveWebContents();
729 EXPECT_EQ(std::string(content::kAboutBlankURL),
730 web_contents->GetURL().spec());
731 StoreDataWithPage("local_storage.html");
734 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, SessionCookiesBrowserClose) {
735 StoreDataWithPage("session_cookies.html");
736 EnableBackgroundMode();
737 Browser* new_browser = QuitBrowserAndRestore(browser(), false);
738 NavigateAndCheckStoredData(new_browser, "session_cookies.html");
739 DisableBackgroundMode();
740 new_browser = QuitBrowserAndRestore(new_browser, false);
741 if (browser_defaults::kBrowserAliveWithNoWindows)
742 NavigateAndCheckStoredData(new_browser, "session_cookies.html");
743 else
744 StoreDataWithPage(new_browser, "session_cookies.html");
747 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, CookiesClearedOnBrowserClose) {
748 StoreDataWithPage("cookies.html");
750 // Normally cookies are restored.
751 Browser* new_browser = QuitBrowserAndRestore(browser(), false);
752 NavigateAndCheckStoredData(new_browser, "cookies.html");
754 // ... but not if the content setting is set to clear on exit.
755 CookieSettings::Factory::GetForProfile(new_browser->profile())->
756 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
757 // ... unless background mode is active.
758 EnableBackgroundMode();
759 new_browser = QuitBrowserAndRestore(new_browser, false);
760 NavigateAndCheckStoredData(new_browser, "cookies.html");
761 DisableBackgroundMode();
762 new_browser = QuitBrowserAndRestore(new_browser, false);
763 if (browser_defaults::kBrowserAliveWithNoWindows)
764 NavigateAndCheckStoredData(new_browser, "cookies.html");
765 else
766 StoreDataWithPage(new_browser, "cookies.html");
769 // Check that session cookies are cleared on a wrench menu quit.
770 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, SessionCookiesCloseAllBrowsers) {
771 StoreDataWithPage("session_cookies.html");
772 EnableBackgroundMode();
773 Browser* new_browser = QuitBrowserAndRestore(browser(), true);
774 StoreDataWithPage(new_browser, "session_cookies.html");
775 DisableBackgroundMode();
776 new_browser = QuitBrowserAndRestore(new_browser, true);
777 StoreDataWithPage(new_browser, "session_cookies.html");
780 // Check that cookies are cleared on a wrench menu quit only if cookies are set
781 // to current session only, regardless of whether background mode is enabled.
782 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, CookiesClearedOnCloseAllBrowsers) {
783 StoreDataWithPage("cookies.html");
785 // Normally cookies are restored.
786 Browser* new_browser = QuitBrowserAndRestore(browser(), true);
787 NavigateAndCheckStoredData(new_browser, "cookies.html");
789 // ... but not if the content setting is set to clear on exit.
790 CookieSettings::Factory::GetForProfile(new_browser->profile())->
791 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
792 // ... even if background mode is active.
793 EnableBackgroundMode();
794 new_browser = QuitBrowserAndRestore(new_browser, true);
795 StoreDataWithPage(new_browser, "cookies.html");
796 DisableBackgroundMode();
797 new_browser = QuitBrowserAndRestore(new_browser, true);
798 StoreDataWithPage(new_browser, "cookies.html");