ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / sessions / better_session_restore_browsertest.cc
blob7b15d5ca60bae65a55edd5677938c55daf258c66
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 <string>
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/lazy_instance.h"
11 #include "base/path_service.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/background/background_mode_manager.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/content_settings/cookie_settings_factory.h"
17 #include "chrome/browser/defaults.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_restore_test_helper.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/pref_names.h"
35 #include "chrome/common/url_constants.h"
36 #include "chrome/test/base/in_process_browser_test.h"
37 #include "chrome/test/base/ui_test_utils.h"
38 #include "components/content_settings/core/browser/cookie_settings.h"
39 #include "components/content_settings/core/common/content_settings.h"
40 #include "components/infobars/core/confirm_infobar_delegate.h"
41 #include "content/public/browser/web_contents.h"
42 #include "content/public/common/url_constants.h"
43 #include "content/public/test/browser_test_utils.h"
44 #include "net/base/net_util.h"
45 #include "net/base/upload_bytes_element_reader.h"
46 #include "net/base/upload_data_stream.h"
47 #include "net/url_request/url_request.h"
48 #include "net/url_request/url_request_filter.h"
49 #include "net/url_request/url_request_interceptor.h"
50 #include "net/url_request/url_request_test_job.h"
52 #if defined(OS_MACOSX)
53 #include "base/mac/scoped_nsautorelease_pool.h"
54 #endif
56 namespace {
58 // We need to serve the test files so that PRE_Test and Test can access the same
59 // page using the same URL. In addition, perceived security origin of the page
60 // needs to stay the same, so e.g., redirecting the URL requests doesn't
61 // work. (If we used a test server, the PRE_Test and Test would have separate
62 // instances running on separate ports.)
64 class URLRequestFakerInterceptor : public net::URLRequestInterceptor {
65 public:
66 // |response_contents| are returned by URLRequestJob's created by
67 // MaybeInterceptRequests.
68 explicit URLRequestFakerInterceptor(const std::string& response_contents)
69 : response_contents_(response_contents) {}
70 ~URLRequestFakerInterceptor() override {}
72 // URLRequestInterceptor implementation:
73 net::URLRequestJob* MaybeInterceptRequest(
74 net::URLRequest* request,
75 net::NetworkDelegate* network_delegate) const override {
76 return new net::URLRequestTestJob(request, network_delegate,
77 net::URLRequestTestJob::test_headers(),
78 response_contents_, true);
81 private:
82 const std::string response_contents_;
84 DISALLOW_COPY_AND_ASSIGN(URLRequestFakerInterceptor);
87 class URLRequestFakerForPostRequestsInterceptor
88 : public net::URLRequestInterceptor {
89 public:
90 URLRequestFakerForPostRequestsInterceptor() {}
91 ~URLRequestFakerForPostRequestsInterceptor() override {}
93 // URLRequestInterceptor implementation:
94 net::URLRequestJob* MaybeInterceptRequest(
95 net::URLRequest* request,
96 net::NetworkDelegate* network_delegate) const override {
97 // Read the uploaded data and store it to last_upload_bytes.
98 const net::UploadDataStream* upload_data = request->get_upload();
99 last_upload_bytes_.clear();
100 if (upload_data) {
101 const ScopedVector<net::UploadElementReader>* readers =
102 upload_data->GetElementReaders();
103 if (readers) {
104 for (size_t i = 0; i < readers->size(); ++i) {
105 const net::UploadBytesElementReader* bytes_reader =
106 (*readers)[i]->AsBytesReader();
107 if (bytes_reader) {
108 last_upload_bytes_ +=
109 std::string(bytes_reader->bytes(), bytes_reader->length());
114 return new net::URLRequestTestJob(
115 request, network_delegate, net::URLRequestTestJob::test_headers(),
116 "<html><head><title>PASS</title></head><body>Data posted</body></html>",
117 true);
120 // Did the last intercepted upload data contain |search_string|?
121 // This method is not thread-safe. It's called on the UI thread, though
122 // the intercept takes place on the IO thread. It must not be called while an
123 // upload is in progress.
124 bool DidLastUploadContain(const std::string& search_string) {
125 return last_upload_bytes_.find(search_string) != std::string::npos;
128 private:
129 mutable std::string last_upload_bytes_;
131 DISALLOW_COPY_AND_ASSIGN(URLRequestFakerForPostRequestsInterceptor);
134 class FakeBackgroundModeManager : public BackgroundModeManager {
135 public:
136 FakeBackgroundModeManager()
137 : BackgroundModeManager(
138 *base::CommandLine::ForCurrentProcess(),
139 &g_browser_process->profile_manager()->GetProfileInfoCache()),
140 background_mode_active_(false) {}
142 void SetBackgroundModeActive(bool active) {
143 background_mode_active_ = active;
146 bool IsBackgroundModeActive() override { return background_mode_active_; }
148 private:
149 bool background_mode_active_;
153 } // namespace
155 class BetterSessionRestoreTest : public InProcessBrowserTest {
156 public:
157 BetterSessionRestoreTest()
158 : fake_server_address_("http://www.test.com/"),
159 test_path_("session_restore/"),
160 title_pass_(base::ASCIIToUTF16("PASS")),
161 title_storing_(base::ASCIIToUTF16("STORING")),
162 title_error_write_failed_(base::ASCIIToUTF16("ERROR_WRITE_FAILED")),
163 title_error_empty_(base::ASCIIToUTF16("ERROR_EMPTY")) {
164 // Set up the URL request filtering.
165 std::vector<std::string> test_files;
166 test_files.push_back("common.js");
167 test_files.push_back("cookies.html");
168 test_files.push_back("local_storage.html");
169 test_files.push_back("post.html");
170 test_files.push_back("post_with_password.html");
171 test_files.push_back("session_cookies.html");
172 test_files.push_back("session_storage.html");
173 base::FilePath test_file_dir;
174 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &test_file_dir));
175 test_file_dir =
176 test_file_dir.AppendASCII("chrome/test/data").AppendASCII(test_path_);
178 for (std::vector<std::string>::const_iterator it = test_files.begin();
179 it != test_files.end(); ++it) {
180 base::FilePath path = test_file_dir.AppendASCII(*it);
181 std::string contents;
182 CHECK(base::ReadFileToString(path, &contents));
183 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
184 GURL(fake_server_address_ + test_path_ + *it),
185 scoped_ptr<net::URLRequestInterceptor>(
186 new URLRequestFakerInterceptor(contents)));
188 post_interceptor_ = new URLRequestFakerForPostRequestsInterceptor();
189 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
190 GURL(fake_server_address_ + test_path_ + "posted.php"),
191 scoped_ptr<net::URLRequestInterceptor>(post_interceptor_));
194 protected:
195 void SetUpOnMainThread() override {
196 SessionServiceTestHelper helper(
197 SessionServiceFactory::GetForProfile(browser()->profile()));
198 helper.SetForceBrowserNotAliveWithNoWindows(true);
199 helper.ReleaseService();
200 g_browser_process->set_background_mode_manager_for_test(
201 scoped_ptr<BackgroundModeManager>(new FakeBackgroundModeManager));
204 void StoreDataWithPage(const std::string& filename) {
205 StoreDataWithPage(browser(), filename);
208 void StoreDataWithPage(Browser* browser, const std::string& filename) {
209 content::WebContents* web_contents =
210 browser->tab_strip_model()->GetActiveWebContents();
211 content::TitleWatcher title_watcher(web_contents, title_storing_);
212 title_watcher.AlsoWaitForTitle(title_pass_);
213 title_watcher.AlsoWaitForTitle(title_error_write_failed_);
214 title_watcher.AlsoWaitForTitle(title_error_empty_);
215 ui_test_utils::NavigateToURL(
216 browser, GURL(fake_server_address_ + test_path_ + filename));
217 base::string16 final_title = title_watcher.WaitAndGetTitle();
218 EXPECT_EQ(title_storing_, final_title);
221 void NavigateAndCheckStoredData(const std::string& filename) {
222 NavigateAndCheckStoredData(browser(), filename);
225 void NavigateAndCheckStoredData(Browser* browser,
226 const std::string& filename) {
227 // Navigate to a page which has previously stored data; check that the
228 // stored data can be accessed.
229 content::WebContents* web_contents =
230 browser->tab_strip_model()->GetActiveWebContents();
231 content::TitleWatcher title_watcher(web_contents, title_pass_);
232 title_watcher.AlsoWaitForTitle(title_storing_);
233 title_watcher.AlsoWaitForTitle(title_error_write_failed_);
234 title_watcher.AlsoWaitForTitle(title_error_empty_);
235 ui_test_utils::NavigateToURL(
236 browser, GURL(fake_server_address_ + test_path_ + filename));
237 base::string16 final_title = title_watcher.WaitAndGetTitle();
238 EXPECT_EQ(title_pass_, final_title);
241 void CheckReloadedPageRestored() {
242 CheckTitle(browser(), title_pass_);
245 void CheckReloadedPageRestored(Browser* browser) {
246 CheckTitle(browser, title_pass_);
249 void CheckReloadedPageNotRestored() {
250 CheckReloadedPageNotRestored(browser());
253 void CheckReloadedPageNotRestored(Browser* browser) {
254 CheckTitle(browser, title_storing_);
257 void CheckTitle(Browser* browser, const base::string16& expected_title) {
258 content::WebContents* web_contents =
259 browser->tab_strip_model()->GetWebContentsAt(0);
260 content::TitleWatcher title_watcher(web_contents, expected_title);
261 title_watcher.AlsoWaitForTitle(title_pass_);
262 title_watcher.AlsoWaitForTitle(title_storing_);
263 title_watcher.AlsoWaitForTitle(title_error_write_failed_);
264 title_watcher.AlsoWaitForTitle(title_error_empty_);
265 // It's possible that the title was already the right one before
266 // title_watcher was created.
267 base::string16 first_title = web_contents->GetTitle();
268 if (first_title != title_pass_ &&
269 first_title != title_storing_ &&
270 first_title != title_error_write_failed_ &&
271 first_title != title_error_empty_) {
272 base::string16 final_title = title_watcher.WaitAndGetTitle();
273 EXPECT_EQ(expected_title, final_title);
274 } else {
275 EXPECT_EQ(expected_title, first_title);
279 void PostFormWithPage(const std::string& filename, bool password_present) {
280 content::WebContents* web_contents =
281 browser()->tab_strip_model()->GetActiveWebContents();
282 content::TitleWatcher title_watcher(web_contents, title_pass_);
283 ui_test_utils::NavigateToURL(
284 browser(), GURL(fake_server_address_ + test_path_ + filename));
285 base::string16 final_title = title_watcher.WaitAndGetTitle();
286 EXPECT_EQ(title_pass_, final_title);
287 EXPECT_TRUE(post_interceptor_->DidLastUploadContain("posted-text"));
288 EXPECT_TRUE(post_interceptor_->DidLastUploadContain("text-entered"));
289 if (password_present) {
290 EXPECT_TRUE(post_interceptor_->DidLastUploadContain("posted-password"));
291 EXPECT_TRUE(post_interceptor_->DidLastUploadContain("password-entered"));
295 void CheckFormRestored(bool text_present, bool password_present) {
296 CheckFormRestored(browser(), text_present, password_present);
299 void CheckFormRestored(
300 Browser* browser, bool text_present, bool password_present) {
301 CheckReloadedPageRestored(browser);
302 EXPECT_EQ(text_present,
303 post_interceptor_->DidLastUploadContain("posted-text"));
304 EXPECT_EQ(text_present,
305 post_interceptor_->DidLastUploadContain("text-entered"));
306 EXPECT_EQ(password_present,
307 post_interceptor_->DidLastUploadContain("posted-password"));
308 EXPECT_EQ(password_present,
309 post_interceptor_->DidLastUploadContain("password-entered"));
312 virtual Browser* QuitBrowserAndRestore(Browser* browser,
313 bool close_all_windows) {
314 Profile* profile = browser->profile();
316 // Close the browser.
317 chrome::IncrementKeepAliveCount();
318 if (close_all_windows)
319 CloseAllBrowsers();
320 else
321 CloseBrowserSynchronously(browser);
323 SessionServiceTestHelper helper;
324 helper.SetService(
325 SessionServiceFactory::GetForProfileForSessionRestore(profile));
326 helper.SetForceBrowserNotAliveWithNoWindows(true);
327 helper.ReleaseService();
329 // Create a new window, which should trigger session restore.
330 ui_test_utils::BrowserAddedObserver window_observer;
331 chrome::NewEmptyWindow(profile, chrome::HOST_DESKTOP_TYPE_NATIVE);
332 Browser* new_browser = window_observer.WaitForSingleNewBrowser();
333 chrome::DecrementKeepAliveCount();
335 return new_browser;
338 std::string fake_server_address() {
339 return fake_server_address_;
342 std::string test_path() {
343 return test_path_;
346 void EnableBackgroundMode() {
347 static_cast<FakeBackgroundModeManager*>(
348 g_browser_process->background_mode_manager())->
349 SetBackgroundModeActive(true);
352 void DisableBackgroundMode() {
353 static_cast<FakeBackgroundModeManager*>(
354 g_browser_process->background_mode_manager())->
355 SetBackgroundModeActive(false);
358 private:
359 const std::string fake_server_address_;
360 const std::string test_path_;
361 const base::string16 title_pass_;
362 const base::string16 title_storing_;
363 const base::string16 title_error_write_failed_;
364 const base::string16 title_error_empty_;
366 // Interceptor is owned by URLRequestFilter and lives on IO thread; this is
367 // just a reference.
368 URLRequestFakerForPostRequestsInterceptor* post_interceptor_;
370 DISALLOW_COPY_AND_ASSIGN(BetterSessionRestoreTest);
373 class ContinueWhereILeftOffTest : public BetterSessionRestoreTest {
374 public:
375 ContinueWhereILeftOffTest() { }
377 void SetUpOnMainThread() override {
378 BetterSessionRestoreTest::SetUpOnMainThread();
379 SessionStartupPref::SetStartupPref(
380 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
383 protected:
384 Browser* QuitBrowserAndRestore(Browser* browser,
385 bool close_all_windows) override {
386 SessionRestoreTestHelper session_restore_observer;
387 Browser* new_browser = BetterSessionRestoreTest::QuitBrowserAndRestore(
388 browser, close_all_windows);
389 session_restore_observer.Wait();
390 return new_browser;
393 DISALLOW_COPY_AND_ASSIGN(ContinueWhereILeftOffTest);
396 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PRE_SessionCookies) {
397 // Set the startup preference to "continue where I left off" and visit a page
398 // which stores a session cookie.
399 StoreDataWithPage("session_cookies.html");
402 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, SessionCookies) {
403 // The browsing session will be continued; just wait for the page to reload
404 // and check the stored data.
405 CheckReloadedPageRestored();
408 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PRE_SessionStorage) {
409 StoreDataWithPage("session_storage.html");
412 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, SessionStorage) {
413 CheckReloadedPageRestored();
416 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
417 PRE_PRE_LocalStorageClearedOnExit) {
418 StoreDataWithPage("local_storage.html");
421 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
422 PRE_LocalStorageClearedOnExit) {
423 // Normally localStorage is restored.
424 CheckReloadedPageRestored();
425 // ... but not if it's set to clear on exit.
426 CookieSettingsFactory::GetForProfile(browser()->profile())
427 ->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
430 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, LocalStorageClearedOnExit) {
431 CheckReloadedPageNotRestored();
434 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
435 PRE_PRE_CookiesClearedOnExit) {
436 StoreDataWithPage("cookies.html");
439 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PRE_CookiesClearedOnExit) {
440 // Normally cookies are restored.
441 CheckReloadedPageRestored();
442 // ... but not if the content setting is set to clear on exit.
443 CookieSettingsFactory::GetForProfile(browser()->profile())
444 ->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
447 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, CookiesClearedOnExit) {
448 CheckReloadedPageNotRestored();
451 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PRE_Post) {
452 PostFormWithPage("post.html", false);
455 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, Post) {
456 CheckFormRestored(true, false);
459 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PRE_PostWithPassword) {
460 PostFormWithPage("post_with_password.html", true);
463 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PostWithPassword) {
464 CheckReloadedPageRestored();
465 // The form data contained passwords, so it's removed completely.
466 CheckFormRestored(false, false);
469 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, SessionCookiesBrowserClose) {
470 // Set the startup preference to "continue where I left off" and visit a page
471 // which stores a session cookie.
472 StoreDataWithPage("session_cookies.html");
473 Browser* new_browser = QuitBrowserAndRestore(browser(), false);
474 // The browsing session will be continued; just wait for the page to reload
475 // and check the stored data.
476 CheckReloadedPageRestored(new_browser);
479 // Test that leaving a popup open will not prevent session restore.
480 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
481 SessionCookiesBrowserCloseWithPopupOpen) {
482 // Set the startup preference to "continue where I left off" and visit a page
483 // which stores a session cookie.
484 StoreDataWithPage("session_cookies.html");
485 Browser* popup = new Browser(Browser::CreateParams(
486 Browser::TYPE_POPUP,
487 browser()->profile(),
488 chrome::HOST_DESKTOP_TYPE_NATIVE));
489 popup->window()->Show();
491 Browser* new_browser = QuitBrowserAndRestore(browser(), false);
492 // The browsing session will be continued; just wait for the page to reload
493 // and check the stored data.
494 CheckReloadedPageRestored(new_browser);
496 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
497 CookiesClearedOnBrowserClose) {
498 StoreDataWithPage("cookies.html");
499 // Normally cookies are restored.
500 Browser* new_browser = QuitBrowserAndRestore(browser(), false);
501 CheckReloadedPageRestored(new_browser);
502 // ... but not if the content setting is set to clear on exit.
503 CookieSettingsFactory::GetForProfile(new_browser->profile())
504 ->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
505 // ... unless background mode is active.
506 EnableBackgroundMode();
507 new_browser = QuitBrowserAndRestore(new_browser, false);
508 CheckReloadedPageRestored(new_browser);
510 DisableBackgroundMode();
511 new_browser = QuitBrowserAndRestore(new_browser, false);
512 if (browser_defaults::kBrowserAliveWithNoWindows)
513 CheckReloadedPageRestored(new_browser);
514 else
515 CheckReloadedPageNotRestored(new_browser);
518 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PostBrowserClose) {
519 PostFormWithPage("post.html", false);
520 Browser* new_browser = QuitBrowserAndRestore(browser(), false);
521 CheckFormRestored(new_browser, true, false);
524 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
525 PostWithPasswordBrowserClose) {
526 PostFormWithPage("post_with_password.html", true);
527 Browser* new_browser = QuitBrowserAndRestore(browser(), false);
528 CheckReloadedPageRestored(new_browser);
529 // The form data contained passwords, so it's removed completely.
530 CheckFormRestored(new_browser, false, false);
533 // Check that session cookies are cleared on a wrench menu quit.
534 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
535 SessionCookiesCloseAllBrowsers) {
536 // Set the startup preference to "continue where I left off" and visit a page
537 // which stores a session cookie.
538 StoreDataWithPage("session_cookies.html");
539 Browser* new_browser = QuitBrowserAndRestore(browser(), true);
540 // The browsing session will be continued; just wait for the page to reload
541 // and check the stored data.
542 CheckReloadedPageRestored(new_browser);
545 // Check that cookies are cleared on a wrench menu quit only if cookies are set
546 // to current session only, regardless of whether background mode is enabled.
547 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
548 CookiesClearedOnCloseAllBrowsers) {
549 StoreDataWithPage("cookies.html");
550 // Normally cookies are restored.
551 Browser* new_browser = QuitBrowserAndRestore(browser(), true);
552 CheckReloadedPageRestored(new_browser);
553 // ... but not if the content setting is set to clear on exit.
554 CookieSettingsFactory::GetForProfile(new_browser->profile())
555 ->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
556 // ... even if background mode is active.
557 EnableBackgroundMode();
558 new_browser = QuitBrowserAndRestore(new_browser, true);
559 CheckReloadedPageNotRestored(new_browser);
561 DisableBackgroundMode();
562 new_browser = QuitBrowserAndRestore(new_browser, true);
563 CheckReloadedPageNotRestored(new_browser);
566 // Check that form data is restored after wrench menu quit.
567 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PostCloseAllBrowsers) {
568 PostFormWithPage("post.html", false);
569 Browser* new_browser = QuitBrowserAndRestore(browser(), true);
570 CheckFormRestored(new_browser, true, false);
573 // Check that form data with a password field is cleared after wrench menu quit.
574 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
575 PostWithPasswordCloseAllBrowsers) {
576 PostFormWithPage("post_with_password.html", true);
577 Browser* new_browser = QuitBrowserAndRestore(browser(), true);
578 CheckReloadedPageRestored(new_browser);
579 // The form data contained passwords, so it's removed completely.
580 CheckFormRestored(new_browser, false, false);
583 // ChromeOS does not override the SessionStartupPreference upon controlled
584 // system restart.
585 #if !defined(OS_CHROMEOS)
586 class RestartTest : public BetterSessionRestoreTest {
587 public:
588 RestartTest() { }
589 ~RestartTest() override {}
591 protected:
592 void Restart() {
593 // Simulate restarting the browser, but let the test exit peacefully.
594 for (chrome::BrowserIterator it; !it.done(); it.Next())
595 content::BrowserContext::SaveSessionState(it->profile());
596 PrefService* pref_service = g_browser_process->local_state();
597 pref_service->SetBoolean(prefs::kWasRestarted, true);
598 #if defined(OS_WIN)
599 if (pref_service->HasPrefPath(prefs::kRelaunchMode))
600 pref_service->ClearPref(prefs::kRelaunchMode);
601 #endif
604 private:
605 DISALLOW_COPY_AND_ASSIGN(RestartTest);
608 IN_PROC_BROWSER_TEST_F(RestartTest, PRE_SessionCookies) {
609 StoreDataWithPage("session_cookies.html");
610 Restart();
613 IN_PROC_BROWSER_TEST_F(RestartTest, SessionCookies) {
614 CheckReloadedPageRestored();
617 IN_PROC_BROWSER_TEST_F(RestartTest, PRE_SessionStorage) {
618 StoreDataWithPage("session_storage.html");
619 Restart();
622 IN_PROC_BROWSER_TEST_F(RestartTest, SessionStorage) {
623 CheckReloadedPageRestored();
626 IN_PROC_BROWSER_TEST_F(RestartTest, PRE_LocalStorageClearedOnExit) {
627 StoreDataWithPage("local_storage.html");
628 CookieSettingsFactory::GetForProfile(browser()->profile())
629 ->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
630 Restart();
633 IN_PROC_BROWSER_TEST_F(RestartTest, LocalStorageClearedOnExit) {
634 CheckReloadedPageRestored();
637 IN_PROC_BROWSER_TEST_F(RestartTest, PRE_CookiesClearedOnExit) {
638 StoreDataWithPage("cookies.html");
639 CookieSettingsFactory::GetForProfile(browser()->profile())
640 ->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
641 Restart();
644 IN_PROC_BROWSER_TEST_F(RestartTest, CookiesClearedOnExit) {
645 CheckReloadedPageRestored();
648 IN_PROC_BROWSER_TEST_F(RestartTest, PRE_Post) {
649 PostFormWithPage("post.html", false);
650 Restart();
653 IN_PROC_BROWSER_TEST_F(RestartTest, Post) {
654 CheckFormRestored(true, false);
657 IN_PROC_BROWSER_TEST_F(RestartTest, PRE_PostWithPassword) {
658 PostFormWithPage("post_with_password.html", true);
659 Restart();
662 IN_PROC_BROWSER_TEST_F(RestartTest, PostWithPassword) {
663 // The form data contained passwords, so it's removed completely.
664 CheckFormRestored(false, false);
666 #endif
668 // These tests ensure that the Better Session Restore features are not triggered
669 // when they shouldn't be.
670 class NoSessionRestoreTest : public BetterSessionRestoreTest {
671 public:
672 NoSessionRestoreTest() { }
674 void SetUpOnMainThread() override {
675 BetterSessionRestoreTest::SetUpOnMainThread();
676 SessionStartupPref::SetStartupPref(
677 browser()->profile(), SessionStartupPref(SessionStartupPref::DEFAULT));
680 private:
681 DISALLOW_COPY_AND_ASSIGN(NoSessionRestoreTest);
684 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, PRE_SessionCookies) {
685 StoreDataWithPage("session_cookies.html");
688 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, SessionCookies) {
689 content::WebContents* web_contents =
690 browser()->tab_strip_model()->GetActiveWebContents();
691 EXPECT_EQ(std::string(url::kAboutBlankURL), web_contents->GetURL().spec());
692 // When we navigate to the page again, it doens't see the data previously
693 // stored.
694 StoreDataWithPage("session_cookies.html");
697 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, PRE_SessionStorage) {
698 StoreDataWithPage("session_storage.html");
701 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, SessionStorage) {
702 content::WebContents* web_contents =
703 browser()->tab_strip_model()->GetActiveWebContents();
704 EXPECT_EQ(std::string(url::kAboutBlankURL), web_contents->GetURL().spec());
705 StoreDataWithPage("session_storage.html");
708 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest,
709 PRE_PRE_LocalStorageClearedOnExit) {
710 StoreDataWithPage("local_storage.html");
713 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, PRE_LocalStorageClearedOnExit) {
714 // Normally localStorage is persisted.
715 content::WebContents* web_contents =
716 browser()->tab_strip_model()->GetActiveWebContents();
717 EXPECT_EQ(std::string(url::kAboutBlankURL), web_contents->GetURL().spec());
718 NavigateAndCheckStoredData("local_storage.html");
719 // ... but not if it's set to clear on exit.
720 CookieSettingsFactory::GetForProfile(browser()->profile())
721 ->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
724 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, LocalStorageClearedOnExit) {
725 content::WebContents* web_contents =
726 browser()->tab_strip_model()->GetActiveWebContents();
727 EXPECT_EQ(std::string(url::kAboutBlankURL), web_contents->GetURL().spec());
728 StoreDataWithPage("local_storage.html");
731 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, PRE_PRE_CookiesClearedOnExit) {
732 StoreDataWithPage("cookies.html");
735 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, PRE_CookiesClearedOnExit) {
736 // Normally cookies are restored.
737 content::WebContents* web_contents =
738 browser()->tab_strip_model()->GetActiveWebContents();
739 EXPECT_EQ(std::string(url::kAboutBlankURL), web_contents->GetURL().spec());
740 NavigateAndCheckStoredData("cookies.html");
741 // ... but not if the content setting is set to clear on exit.
742 CookieSettingsFactory::GetForProfile(browser()->profile())
743 ->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
746 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, CookiesClearedOnExit) {
747 content::WebContents* web_contents =
748 browser()->tab_strip_model()->GetActiveWebContents();
749 EXPECT_EQ(std::string(url::kAboutBlankURL), web_contents->GetURL().spec());
750 StoreDataWithPage("local_storage.html");
753 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, SessionCookiesBrowserClose) {
754 StoreDataWithPage("session_cookies.html");
755 EnableBackgroundMode();
756 Browser* new_browser = QuitBrowserAndRestore(browser(), false);
757 NavigateAndCheckStoredData(new_browser, "session_cookies.html");
758 DisableBackgroundMode();
759 new_browser = QuitBrowserAndRestore(new_browser, false);
760 if (browser_defaults::kBrowserAliveWithNoWindows)
761 NavigateAndCheckStoredData(new_browser, "session_cookies.html");
762 else
763 StoreDataWithPage(new_browser, "session_cookies.html");
766 // Tests that session cookies are not cleared when only a popup window is open.
767 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest,
768 SessionCookiesBrowserCloseWithPopupOpen) {
769 StoreDataWithPage("session_cookies.html");
770 Browser* popup = new Browser(Browser::CreateParams(
771 Browser::TYPE_POPUP,
772 browser()->profile(),
773 chrome::HOST_DESKTOP_TYPE_NATIVE));
774 popup->window()->Show();
775 Browser* new_browser = QuitBrowserAndRestore(browser(), false);
776 NavigateAndCheckStoredData(new_browser, "session_cookies.html");
779 // Tests that session cookies are cleared if the last window to close is a
780 // popup.
781 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest,
782 SessionCookiesBrowserClosePopupLast) {
783 StoreDataWithPage("session_cookies.html");
784 Browser* popup = new Browser(Browser::CreateParams(
785 Browser::TYPE_POPUP,
786 browser()->profile(),
787 chrome::HOST_DESKTOP_TYPE_NATIVE));
788 popup->window()->Show();
789 CloseBrowserSynchronously(browser());
790 Browser* new_browser = QuitBrowserAndRestore(popup, false);
791 if (browser_defaults::kBrowserAliveWithNoWindows)
792 NavigateAndCheckStoredData(new_browser, "session_cookies.html");
793 else
794 StoreDataWithPage(new_browser, "session_cookies.html");
797 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, CookiesClearedOnBrowserClose) {
798 StoreDataWithPage("cookies.html");
800 // Normally cookies are restored.
801 Browser* new_browser = QuitBrowserAndRestore(browser(), false);
802 NavigateAndCheckStoredData(new_browser, "cookies.html");
804 // ... but not if the content setting is set to clear on exit.
805 CookieSettingsFactory::GetForProfile(new_browser->profile())
806 ->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
807 // ... unless background mode is active.
808 EnableBackgroundMode();
809 new_browser = QuitBrowserAndRestore(new_browser, false);
810 NavigateAndCheckStoredData(new_browser, "cookies.html");
811 DisableBackgroundMode();
812 new_browser = QuitBrowserAndRestore(new_browser, false);
813 if (browser_defaults::kBrowserAliveWithNoWindows)
814 NavigateAndCheckStoredData(new_browser, "cookies.html");
815 else
816 StoreDataWithPage(new_browser, "cookies.html");
819 // Check that session cookies are cleared on a wrench menu quit.
820 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, SessionCookiesCloseAllBrowsers) {
821 StoreDataWithPage("session_cookies.html");
822 EnableBackgroundMode();
823 Browser* new_browser = QuitBrowserAndRestore(browser(), true);
824 StoreDataWithPage(new_browser, "session_cookies.html");
825 DisableBackgroundMode();
826 new_browser = QuitBrowserAndRestore(new_browser, true);
827 StoreDataWithPage(new_browser, "session_cookies.html");
830 // Check that cookies are cleared on a wrench menu quit only if cookies are set
831 // to current session only, regardless of whether background mode is enabled.
832 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, CookiesClearedOnCloseAllBrowsers) {
833 StoreDataWithPage("cookies.html");
835 // Normally cookies are restored.
836 Browser* new_browser = QuitBrowserAndRestore(browser(), true);
837 NavigateAndCheckStoredData(new_browser, "cookies.html");
839 // ... but not if the content setting is set to clear on exit.
840 CookieSettingsFactory::GetForProfile(new_browser->profile())
841 ->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
842 // ... even if background mode is active.
843 EnableBackgroundMode();
844 new_browser = QuitBrowserAndRestore(new_browser, true);
845 StoreDataWithPage(new_browser, "cookies.html");
846 DisableBackgroundMode();
847 new_browser = QuitBrowserAndRestore(new_browser, true);
848 StoreDataWithPage(new_browser, "cookies.html");