OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/file_path.h" | 6 #include "base/file_path.h" |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" |
11 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/chromeos/enterprise_extension_observer.h" |
12 #include "chrome/browser/content_settings/cookie_settings.h" | 14 #include "chrome/browser/content_settings/cookie_settings.h" |
| 15 #include "chrome/browser/infobars/infobar_tab_helper.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/prefs/session_startup_pref.h" | 17 #include "chrome/browser/prefs/session_startup_pref.h" |
15 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/profiles/profile_impl.h" |
| 20 #include "chrome/browser/profiles/profile_manager.h" |
| 21 #include "chrome/browser/sessions/session_backend.h" |
| 22 #include "chrome/browser/sessions/session_service_factory.h" |
16 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
| 24 #include "chrome/browser/ui/browser_commands.h" |
17 #include "chrome/browser/ui/browser_list.h" | 25 #include "chrome/browser/ui/browser_list.h" |
18 #include "chrome/browser/ui/browser_tabstrip.h" | 26 #include "chrome/browser/ui/browser_tabstrip.h" |
| 27 #include "chrome/browser/ui/browser_window.h" |
| 28 #include "chrome/browser/ui/startup/startup_browser_creator.h" |
19 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
20 #include "chrome/common/content_settings.h" | 30 #include "chrome/common/content_settings.h" |
21 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
| 32 #include "chrome/common/url_constants.h" |
22 #include "chrome/test/base/in_process_browser_test.h" | 33 #include "chrome/test/base/in_process_browser_test.h" |
23 #include "chrome/test/base/ui_test_utils.h" | 34 #include "chrome/test/base/ui_test_utils.h" |
24 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
25 #include "content/public/common/url_constants.h" | 36 #include "content/public/common/url_constants.h" |
26 #include "content/public/test/browser_test_utils.h" | 37 #include "content/public/test/browser_test_utils.h" |
27 #include "net/base/net_util.h" | 38 #include "net/base/net_util.h" |
28 #include "net/base/upload_bytes_element_reader.h" | 39 #include "net/base/upload_bytes_element_reader.h" |
29 #include "net/base/upload_data_stream.h" | 40 #include "net/base/upload_data_stream.h" |
30 #include "net/url_request/url_request.h" | 41 #include "net/url_request/url_request.h" |
31 #include "net/url_request/url_request_filter.h" | 42 #include "net/url_request/url_request_filter.h" |
32 #include "net/url_request/url_request_test_job.h" | 43 #include "net/url_request/url_request_test_job.h" |
33 | 44 |
34 namespace { | 45 namespace { |
35 | 46 |
| 47 Browser* FindOneOtherBrowserForProfile(Profile* profile, |
| 48 Browser* not_this_browser) { |
| 49 for (BrowserList::const_iterator i = BrowserList::begin(); |
| 50 i != BrowserList::end(); ++i) { |
| 51 if (*i != not_this_browser && (*i)->profile() == profile) |
| 52 return *i; |
| 53 } |
| 54 return NULL; |
| 55 } |
| 56 |
36 // We need to serve the test files so that PRE_Test and Test can access the same | 57 // We need to serve the test files so that PRE_Test and Test can access the same |
37 // page using the same URL. In addition, perceived security origin of the page | 58 // page using the same URL. In addition, perceived security origin of the page |
38 // needs to stay the same, so e.g., redirecting the URL requests doesn't | 59 // needs to stay the same, so e.g., redirecting the URL requests doesn't |
39 // work. (If we used a test server, the PRE_Test and Test would have separate | 60 // work. (If we used a test server, the PRE_Test and Test would have separate |
40 // instances running on separate ports.) | 61 // instances running on separate ports.) |
41 | 62 |
42 base::LazyInstance<std::map<std::string, std::string> > g_file_contents = | 63 base::LazyInstance<std::map<std::string, std::string> > g_file_contents = |
43 LAZY_INSTANCE_INITIALIZER; | 64 LAZY_INSTANCE_INITIALIZER; |
44 | 65 |
45 net::URLRequestJob* URLRequestFaker( | 66 net::URLRequestJob* URLRequestFaker( |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 GURL(fake_server_address_ + test_path_ + *it), | 134 GURL(fake_server_address_ + test_path_ + *it), |
114 &URLRequestFaker); | 135 &URLRequestFaker); |
115 } | 136 } |
116 net::URLRequestFilter::GetInstance()->AddUrlHandler( | 137 net::URLRequestFilter::GetInstance()->AddUrlHandler( |
117 GURL(fake_server_address_ + test_path_ + "posted.php"), | 138 GURL(fake_server_address_ + test_path_ + "posted.php"), |
118 &URLRequestFakerForPostRequests); | 139 &URLRequestFakerForPostRequests); |
119 } | 140 } |
120 | 141 |
121 protected: | 142 protected: |
122 void StoreDataWithPage(const std::string& filename) { | 143 void StoreDataWithPage(const std::string& filename) { |
| 144 StoreDataWithPage(browser(), filename); |
| 145 } |
| 146 |
| 147 void StoreDataWithPage(Browser* browser, const std::string& filename) { |
123 content::WebContents* web_contents = | 148 content::WebContents* web_contents = |
124 chrome::GetActiveWebContents(browser()); | 149 chrome::GetActiveWebContents(browser); |
125 content::TitleWatcher title_watcher(web_contents, title_storing_); | 150 content::TitleWatcher title_watcher(web_contents, title_storing_); |
126 title_watcher.AlsoWaitForTitle(title_pass_); | 151 title_watcher.AlsoWaitForTitle(title_pass_); |
127 title_watcher.AlsoWaitForTitle(title_error_write_failed_); | 152 title_watcher.AlsoWaitForTitle(title_error_write_failed_); |
128 title_watcher.AlsoWaitForTitle(title_error_empty_); | 153 title_watcher.AlsoWaitForTitle(title_error_empty_); |
129 ui_test_utils::NavigateToURL( | 154 ui_test_utils::NavigateToURL( |
130 browser(), GURL(fake_server_address_ + test_path_ + filename)); | 155 browser, GURL(fake_server_address_ + test_path_ + filename)); |
131 string16 final_title = title_watcher.WaitAndGetTitle(); | 156 string16 final_title = title_watcher.WaitAndGetTitle(); |
132 EXPECT_EQ(title_storing_, final_title); | 157 EXPECT_EQ(title_storing_, final_title); |
133 } | 158 } |
134 | 159 |
135 void NavigateAndCheckStoredData(const std::string& filename) { | 160 void NavigateAndCheckStoredData(const std::string& filename) { |
| 161 NavigateAndCheckStoredData(browser(), filename); |
| 162 } |
| 163 |
| 164 void NavigateAndCheckStoredData(Browser* browser, |
| 165 const std::string& filename) { |
136 // Navigate to a page which has previously stored data; check that the | 166 // Navigate to a page which has previously stored data; check that the |
137 // stored data can be accessed. | 167 // stored data can be accessed. |
138 content::WebContents* web_contents = | 168 content::WebContents* web_contents = |
139 chrome::GetActiveWebContents(browser()); | 169 chrome::GetActiveWebContents(browser); |
140 content::TitleWatcher title_watcher(web_contents, title_pass_); | 170 content::TitleWatcher title_watcher(web_contents, title_pass_); |
141 title_watcher.AlsoWaitForTitle(title_storing_); | 171 title_watcher.AlsoWaitForTitle(title_storing_); |
142 title_watcher.AlsoWaitForTitle(title_error_write_failed_); | 172 title_watcher.AlsoWaitForTitle(title_error_write_failed_); |
143 title_watcher.AlsoWaitForTitle(title_error_empty_); | 173 title_watcher.AlsoWaitForTitle(title_error_empty_); |
144 ui_test_utils::NavigateToURL( | 174 ui_test_utils::NavigateToURL( |
145 browser(), GURL(fake_server_address_ + test_path_ + filename)); | 175 browser, GURL(fake_server_address_ + test_path_ + filename)); |
146 string16 final_title = title_watcher.WaitAndGetTitle(); | 176 string16 final_title = title_watcher.WaitAndGetTitle(); |
147 EXPECT_EQ(title_pass_, final_title); | 177 EXPECT_EQ(title_pass_, final_title); |
148 } | 178 } |
149 | 179 |
150 void CheckReloadedPageRestored() { | 180 void CheckReloadedPageRestored() { |
151 CheckTitle(title_pass_); | 181 CheckTitle(browser(), title_pass_); |
| 182 } |
| 183 |
| 184 void CheckReloadedPageRestored(Browser* browser) { |
| 185 CheckTitle(browser, title_pass_); |
152 } | 186 } |
153 | 187 |
154 void CheckReloadedPageNotRestored() { | 188 void CheckReloadedPageNotRestored() { |
155 CheckTitle(title_storing_); | 189 CheckTitle(browser(), title_storing_); |
156 } | 190 } |
157 | 191 |
158 void CheckTitle(const string16& expected_title) { | 192 void CheckTitle(Browser* browser, const string16& expected_title) { |
159 content::WebContents* web_contents = chrome::GetWebContentsAt(browser(), 0); | 193 content::WebContents* web_contents = chrome::GetWebContentsAt(browser, 0); |
160 content::TitleWatcher title_watcher(web_contents, expected_title); | 194 content::TitleWatcher title_watcher(web_contents, expected_title); |
161 title_watcher.AlsoWaitForTitle(title_pass_); | 195 title_watcher.AlsoWaitForTitle(title_pass_); |
162 title_watcher.AlsoWaitForTitle(title_storing_); | 196 title_watcher.AlsoWaitForTitle(title_storing_); |
163 title_watcher.AlsoWaitForTitle(title_error_write_failed_); | 197 title_watcher.AlsoWaitForTitle(title_error_write_failed_); |
164 title_watcher.AlsoWaitForTitle(title_error_empty_); | 198 title_watcher.AlsoWaitForTitle(title_error_empty_); |
165 // It's possible that the title was already the right one before | 199 // It's possible that the title was already the right one before |
166 // title_watcher was created. | 200 // title_watcher was created. |
167 string16 first_title = web_contents->GetTitle(); | 201 string16 first_title = web_contents->GetTitle(); |
168 if (first_title != title_pass_ && | 202 if (first_title != title_pass_ && |
169 first_title != title_storing_ && | 203 first_title != title_storing_ && |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") != | 249 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") != |
216 std::string::npos); | 250 std::string::npos); |
217 } else { | 251 } else { |
218 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-password") == | 252 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-password") == |
219 std::string::npos); | 253 std::string::npos); |
220 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") == | 254 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") == |
221 std::string::npos); | 255 std::string::npos); |
222 } | 256 } |
223 } | 257 } |
224 | 258 |
| 259 std::string fake_server_address() { |
| 260 return fake_server_address_; |
| 261 } |
| 262 |
| 263 std::string test_path() { |
| 264 return test_path_; |
| 265 } |
| 266 |
225 private: | 267 private: |
226 std::string fake_server_address_; | 268 const std::string fake_server_address_; |
227 std::string test_path_; | 269 const std::string test_path_; |
228 string16 title_pass_; | 270 const string16 title_pass_; |
229 string16 title_storing_; | 271 const string16 title_storing_; |
230 string16 title_error_write_failed_; | 272 const string16 title_error_write_failed_; |
231 string16 title_error_empty_; | 273 const string16 title_error_empty_; |
232 | 274 |
233 DISALLOW_COPY_AND_ASSIGN(BetterSessionRestoreTest); | 275 DISALLOW_COPY_AND_ASSIGN(BetterSessionRestoreTest); |
234 }; | 276 }; |
235 | 277 |
236 class ContinueWhereILeftOffTest : public BetterSessionRestoreTest { | 278 class ContinueWhereILeftOffTest : public BetterSessionRestoreTest { |
237 public: | 279 public: |
| 280 ContinueWhereILeftOffTest() { } |
| 281 |
238 virtual void SetUpOnMainThread() OVERRIDE { | 282 virtual void SetUpOnMainThread() OVERRIDE { |
| 283 BetterSessionRestoreTest::SetUpOnMainThread(); |
239 SessionStartupPref::SetStartupPref( | 284 SessionStartupPref::SetStartupPref( |
240 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST)); | 285 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST)); |
241 } | 286 } |
| 287 |
| 288 DISALLOW_COPY_AND_ASSIGN(ContinueWhereILeftOffTest); |
242 }; | 289 }; |
243 | 290 |
244 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PRE_SessionCookies) { | 291 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PRE_SessionCookies) { |
245 // Set the startup preference to "continue where I left off" and visit a page | 292 // Set the startup preference to "continue where I left off" and visit a page |
246 // which stores a session cookie. | 293 // which stores a session cookie. |
247 StoreDataWithPage("session_cookies.html"); | 294 StoreDataWithPage("session_cookies.html"); |
248 } | 295 } |
249 | 296 |
250 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, SessionCookies) { | 297 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, SessionCookies) { |
251 // The browsing session will be continued; just wait for the page to reload | 298 // The browsing session will be continued; just wait for the page to reload |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 | 446 |
400 IN_PROC_BROWSER_TEST_F(RestartTest, PostWithPassword) { | 447 IN_PROC_BROWSER_TEST_F(RestartTest, PostWithPassword) { |
401 // The form data contained passwords, so it's removed completely. | 448 // The form data contained passwords, so it's removed completely. |
402 CheckFormRestored(false, false); | 449 CheckFormRestored(false, false); |
403 } | 450 } |
404 | 451 |
405 // These tests ensure that the Better Session Restore features are not triggered | 452 // These tests ensure that the Better Session Restore features are not triggered |
406 // when they shouldn't be. | 453 // when they shouldn't be. |
407 class NoSessionRestoreTest : public BetterSessionRestoreTest { | 454 class NoSessionRestoreTest : public BetterSessionRestoreTest { |
408 public: | 455 public: |
| 456 NoSessionRestoreTest() { } |
| 457 |
409 virtual void SetUpOnMainThread() OVERRIDE { | 458 virtual void SetUpOnMainThread() OVERRIDE { |
| 459 BetterSessionRestoreTest::SetUpOnMainThread(); |
410 SessionStartupPref::SetStartupPref( | 460 SessionStartupPref::SetStartupPref( |
411 browser()->profile(), SessionStartupPref(SessionStartupPref::DEFAULT)); | 461 browser()->profile(), SessionStartupPref(SessionStartupPref::DEFAULT)); |
412 } | 462 } |
| 463 |
| 464 private: |
| 465 DISALLOW_COPY_AND_ASSIGN(NoSessionRestoreTest); |
413 }; | 466 }; |
414 | 467 |
415 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, PRE_SessionCookies) { | 468 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, PRE_SessionCookies) { |
416 StoreDataWithPage("session_cookies.html"); | 469 StoreDataWithPage("session_cookies.html"); |
417 } | 470 } |
418 | 471 |
419 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, SessionCookies) { | 472 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, SessionCookies) { |
420 content::WebContents* web_contents = chrome::GetActiveWebContents(browser()); | 473 content::WebContents* web_contents = chrome::GetActiveWebContents(browser()); |
421 EXPECT_EQ(std::string(chrome::kAboutBlankURL), web_contents->GetURL().spec()); | 474 EXPECT_EQ(std::string(chrome::kAboutBlankURL), web_contents->GetURL().spec()); |
422 // When we navigate to the page again, it doens't see the data previously | 475 // When we navigate to the page again, it doens't see the data previously |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 // ... but not if the content setting is set to clear on exit. | 521 // ... but not if the content setting is set to clear on exit. |
469 CookieSettings::Factory::GetForProfile(browser()->profile())-> | 522 CookieSettings::Factory::GetForProfile(browser()->profile())-> |
470 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY); | 523 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY); |
471 } | 524 } |
472 | 525 |
473 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, CookiesClearedOnExit) { | 526 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, CookiesClearedOnExit) { |
474 content::WebContents* web_contents = chrome::GetActiveWebContents(browser()); | 527 content::WebContents* web_contents = chrome::GetActiveWebContents(browser()); |
475 EXPECT_EQ(std::string(chrome::kAboutBlankURL), web_contents->GetURL().spec()); | 528 EXPECT_EQ(std::string(chrome::kAboutBlankURL), web_contents->GetURL().spec()); |
476 StoreDataWithPage("local_storage.html"); | 529 StoreDataWithPage("local_storage.html"); |
477 } | 530 } |
| 531 |
| 532 class BetterSessionRestoreCrashTest : public BetterSessionRestoreTest { |
| 533 public: |
| 534 BetterSessionRestoreCrashTest() { } |
| 535 |
| 536 virtual void SetUpOnMainThread() OVERRIDE { |
| 537 BetterSessionRestoreTest::SetUpOnMainThread(); |
| 538 SessionStartupPref::SetStartupPref( |
| 539 browser()->profile(), SessionStartupPref(SessionStartupPref::DEFAULT)); |
| 540 } |
| 541 |
| 542 protected: |
| 543 void CrashTestWithPage(const std::string& filename) { |
| 544 Profile* profile = browser()->profile(); |
| 545 Browser* browser_before_restore = browser(); |
| 546 ASSERT_TRUE(browser_before_restore); |
| 547 StoreDataWithPage(browser_before_restore, filename); |
| 548 |
| 549 // Session restore data is written lazily but we cannot restore data that |
| 550 // was not saved. Be less lazy for the test. |
| 551 SessionServiceFactory::GetForProfile(profile)->Save(); |
| 552 |
| 553 // Simulate a crash and a restart. |
| 554 browser_before_restore->window()->Close(); |
| 555 SessionServiceFactory::GetForProfile(profile)->backend()-> |
| 556 MoveCurrentSessionToLastSession(); |
| 557 ProfileImpl* profile_impl = static_cast<ProfileImpl*>(profile); |
| 558 profile_impl->last_session_exit_type_ = Profile::EXIT_CRASHED; |
| 559 #if defined(OS_CHROMEOS) |
| 560 profile_impl->chromeos_enterprise_extension_observer_.reset(NULL); |
| 561 #endif |
| 562 StartupBrowserCreator::ClearLaunchedProfilesForTesting(); |
| 563 |
| 564 CommandLine dummy(CommandLine::NO_PROGRAM); |
| 565 int return_code; |
| 566 StartupBrowserCreator browser_creator; |
| 567 std::vector<Profile*> last_opened_profiles(1, profile); |
| 568 browser_creator.Start(dummy, |
| 569 g_browser_process->profile_manager()->user_data_dir(), |
| 570 profile, last_opened_profiles, &return_code); |
| 571 |
| 572 // The browser displays an info bar, use it to restore the session. |
| 573 Browser* browser_after_restore = |
| 574 FindOneOtherBrowserForProfile(profile, browser_before_restore); |
| 575 ASSERT_TRUE(browser_after_restore); |
| 576 content::WebContents* web_contents = |
| 577 chrome::GetActiveWebContents(browser_after_restore); |
| 578 ASSERT_TRUE(web_contents); |
| 579 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL()); |
| 580 InfoBarTabHelper* info_bar_tab_helper = |
| 581 InfoBarTabHelper::FromWebContents(web_contents); |
| 582 EXPECT_EQ(1U, info_bar_tab_helper->GetInfoBarCount()); |
| 583 ConfirmInfoBarDelegate* info_bar_delegate = |
| 584 static_cast<ConfirmInfoBarDelegate*>( |
| 585 info_bar_tab_helper->GetInfoBarDelegateAt(0)); |
| 586 info_bar_delegate->Accept(); |
| 587 |
| 588 // Session restore is done ascynhronously. |
| 589 base::RunLoop loop; |
| 590 loop.RunUntilIdle(); |
| 591 |
| 592 // Check the restored page. |
| 593 web_contents = chrome::GetWebContentsAt(browser_after_restore, 0); |
| 594 ASSERT_TRUE(web_contents); |
| 595 EXPECT_EQ(GURL(fake_server_address() + test_path() + filename), |
| 596 web_contents->GetURL()); |
| 597 CheckReloadedPageRestored(browser_after_restore); |
| 598 } |
| 599 |
| 600 private: |
| 601 DISALLOW_COPY_AND_ASSIGN(BetterSessionRestoreCrashTest); |
| 602 }; |
| 603 |
| 604 IN_PROC_BROWSER_TEST_F(BetterSessionRestoreCrashTest, SessionCookies) { |
| 605 CrashTestWithPage("session_cookies.html"); |
| 606 } |
OLD | NEW |