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" |
12 #include "chrome/browser/content_settings/cookie_settings.h" | 13 #include "chrome/browser/content_settings/cookie_settings.h" |
14 #include "chrome/browser/infobars/infobar_tab_helper.h" | |
13 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/prefs/session_startup_pref.h" | 16 #include "chrome/browser/prefs/session_startup_pref.h" |
15 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/profiles/profile_impl.h" | |
19 #include "chrome/browser/profiles/profile_manager.h" | |
20 #include "chrome/browser/sessions/session_backend.h" | |
21 #include "chrome/browser/sessions/session_service_factory.h" | |
16 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
23 #include "chrome/browser/ui/browser_commands.h" | |
17 #include "chrome/browser/ui/browser_list.h" | 24 #include "chrome/browser/ui/browser_list.h" |
18 #include "chrome/browser/ui/browser_tabstrip.h" | 25 #include "chrome/browser/ui/browser_tabstrip.h" |
26 #include "chrome/browser/ui/browser_window.h" | |
27 #include "chrome/browser/ui/startup/startup_browser_creator.h" | |
19 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
20 #include "chrome/common/content_settings.h" | 29 #include "chrome/common/content_settings.h" |
21 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
31 #include "chrome/common/url_constants.h" | |
22 #include "chrome/test/base/in_process_browser_test.h" | 32 #include "chrome/test/base/in_process_browser_test.h" |
23 #include "chrome/test/base/ui_test_utils.h" | 33 #include "chrome/test/base/ui_test_utils.h" |
24 #include "content/public/browser/web_contents.h" | 34 #include "content/public/browser/web_contents.h" |
25 #include "content/public/common/url_constants.h" | 35 #include "content/public/common/url_constants.h" |
26 #include "content/public/test/browser_test_utils.h" | 36 #include "content/public/test/browser_test_utils.h" |
27 #include "net/base/net_util.h" | 37 #include "net/base/net_util.h" |
28 #include "net/base/upload_bytes_element_reader.h" | 38 #include "net/base/upload_bytes_element_reader.h" |
29 #include "net/base/upload_data_stream.h" | 39 #include "net/base/upload_data_stream.h" |
30 #include "net/url_request/url_request.h" | 40 #include "net/url_request/url_request.h" |
31 #include "net/url_request/url_request_filter.h" | 41 #include "net/url_request/url_request_filter.h" |
32 #include "net/url_request/url_request_test_job.h" | 42 #include "net/url_request/url_request_test_job.h" |
33 | 43 |
34 namespace { | 44 namespace { |
35 | 45 |
46 Browser* FindOneOtherBrowserForProfile(Profile* profile, | |
47 Browser* not_this_browser) { | |
48 for (BrowserList::const_iterator i = BrowserList::begin(); | |
49 i != BrowserList::end(); ++i) { | |
50 if (*i != not_this_browser && (*i)->profile() == profile) | |
51 return *i; | |
52 } | |
53 return NULL; | |
54 } | |
55 | |
36 // We need to serve the test files so that PRE_Test and Test can access the same | 56 // 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 | 57 // 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 | 58 // 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 | 59 // work. (If we used a test server, the PRE_Test and Test would have separate |
40 // instances running on separate ports.) | 60 // instances running on separate ports.) |
41 | 61 |
42 base::LazyInstance<std::map<std::string, std::string> > g_file_contents = | 62 base::LazyInstance<std::map<std::string, std::string> > g_file_contents = |
43 LAZY_INSTANCE_INITIALIZER; | 63 LAZY_INSTANCE_INITIALIZER; |
44 | 64 |
45 net::URLRequestJob* URLRequestFaker( | 65 net::URLRequestJob* URLRequestFaker( |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 GURL(fake_server_address_ + test_path_ + *it), | 133 GURL(fake_server_address_ + test_path_ + *it), |
114 &URLRequestFaker); | 134 &URLRequestFaker); |
115 } | 135 } |
116 net::URLRequestFilter::GetInstance()->AddUrlHandler( | 136 net::URLRequestFilter::GetInstance()->AddUrlHandler( |
117 GURL(fake_server_address_ + test_path_ + "posted.php"), | 137 GURL(fake_server_address_ + test_path_ + "posted.php"), |
118 &URLRequestFakerForPostRequests); | 138 &URLRequestFakerForPostRequests); |
119 } | 139 } |
120 | 140 |
121 protected: | 141 protected: |
122 void StoreDataWithPage(const std::string& filename) { | 142 void StoreDataWithPage(const std::string& filename) { |
143 StoreDataWithPage(browser(), filename); | |
144 } | |
145 | |
146 void StoreDataWithPage(Browser* browser, const std::string& filename) { | |
123 content::WebContents* web_contents = | 147 content::WebContents* web_contents = |
124 chrome::GetActiveWebContents(browser()); | 148 chrome::GetActiveWebContents(browser); |
125 content::TitleWatcher title_watcher(web_contents, title_storing_); | 149 content::TitleWatcher title_watcher(web_contents, title_storing_); |
126 title_watcher.AlsoWaitForTitle(title_pass_); | 150 title_watcher.AlsoWaitForTitle(title_pass_); |
127 title_watcher.AlsoWaitForTitle(title_error_write_failed_); | 151 title_watcher.AlsoWaitForTitle(title_error_write_failed_); |
128 title_watcher.AlsoWaitForTitle(title_error_empty_); | 152 title_watcher.AlsoWaitForTitle(title_error_empty_); |
129 ui_test_utils::NavigateToURL( | 153 ui_test_utils::NavigateToURL( |
130 browser(), GURL(fake_server_address_ + test_path_ + filename)); | 154 browser, GURL(fake_server_address_ + test_path_ + filename)); |
131 string16 final_title = title_watcher.WaitAndGetTitle(); | 155 string16 final_title = title_watcher.WaitAndGetTitle(); |
132 EXPECT_EQ(title_storing_, final_title); | 156 EXPECT_EQ(title_storing_, final_title); |
133 } | 157 } |
134 | 158 |
135 void NavigateAndCheckStoredData(const std::string& filename) { | 159 void NavigateAndCheckStoredData(const std::string& filename) { |
160 NavigateAndCheckStoredData(browser(), filename); | |
161 } | |
162 | |
163 void NavigateAndCheckStoredData(Browser* browser, | |
164 const std::string& filename) { | |
136 // Navigate to a page which has previously stored data; check that the | 165 // Navigate to a page which has previously stored data; check that the |
137 // stored data can be accessed. | 166 // stored data can be accessed. |
138 content::WebContents* web_contents = | 167 content::WebContents* web_contents = |
139 chrome::GetActiveWebContents(browser()); | 168 chrome::GetActiveWebContents(browser); |
140 content::TitleWatcher title_watcher(web_contents, title_pass_); | 169 content::TitleWatcher title_watcher(web_contents, title_pass_); |
141 title_watcher.AlsoWaitForTitle(title_storing_); | 170 title_watcher.AlsoWaitForTitle(title_storing_); |
142 title_watcher.AlsoWaitForTitle(title_error_write_failed_); | 171 title_watcher.AlsoWaitForTitle(title_error_write_failed_); |
143 title_watcher.AlsoWaitForTitle(title_error_empty_); | 172 title_watcher.AlsoWaitForTitle(title_error_empty_); |
144 ui_test_utils::NavigateToURL( | 173 ui_test_utils::NavigateToURL( |
145 browser(), GURL(fake_server_address_ + test_path_ + filename)); | 174 browser, GURL(fake_server_address_ + test_path_ + filename)); |
146 string16 final_title = title_watcher.WaitAndGetTitle(); | 175 string16 final_title = title_watcher.WaitAndGetTitle(); |
147 EXPECT_EQ(title_pass_, final_title); | 176 EXPECT_EQ(title_pass_, final_title); |
148 } | 177 } |
149 | 178 |
150 void CheckReloadedPageRestored() { | 179 void CheckReloadedPageRestored() { |
151 CheckTitle(title_pass_); | 180 CheckTitle(browser(), title_pass_); |
181 } | |
182 | |
183 void CheckReloadedPageRestored(Browser* browser) { | |
184 CheckTitle(browser, title_pass_); | |
152 } | 185 } |
153 | 186 |
154 void CheckReloadedPageNotRestored() { | 187 void CheckReloadedPageNotRestored() { |
155 CheckTitle(title_storing_); | 188 CheckTitle(browser(), title_storing_); |
156 } | 189 } |
157 | 190 |
158 void CheckTitle(const string16& expected_title) { | 191 void CheckTitle(Browser* browser, const string16& expected_title) { |
159 content::WebContents* web_contents = chrome::GetWebContentsAt(browser(), 0); | 192 content::WebContents* web_contents = chrome::GetWebContentsAt(browser, 0); |
160 content::TitleWatcher title_watcher(web_contents, expected_title); | 193 content::TitleWatcher title_watcher(web_contents, expected_title); |
161 title_watcher.AlsoWaitForTitle(title_pass_); | 194 title_watcher.AlsoWaitForTitle(title_pass_); |
162 title_watcher.AlsoWaitForTitle(title_storing_); | 195 title_watcher.AlsoWaitForTitle(title_storing_); |
163 title_watcher.AlsoWaitForTitle(title_error_write_failed_); | 196 title_watcher.AlsoWaitForTitle(title_error_write_failed_); |
164 title_watcher.AlsoWaitForTitle(title_error_empty_); | 197 title_watcher.AlsoWaitForTitle(title_error_empty_); |
165 // It's possible that the title was already the right one before | 198 // It's possible that the title was already the right one before |
166 // title_watcher was created. | 199 // title_watcher was created. |
167 string16 first_title = web_contents->GetTitle(); | 200 string16 first_title = web_contents->GetTitle(); |
168 if (first_title != title_pass_ && | 201 if (first_title != title_pass_ && |
169 first_title != title_storing_ && | 202 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") != | 248 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") != |
216 std::string::npos); | 249 std::string::npos); |
217 } else { | 250 } else { |
218 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-password") == | 251 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-password") == |
219 std::string::npos); | 252 std::string::npos); |
220 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") == | 253 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") == |
221 std::string::npos); | 254 std::string::npos); |
222 } | 255 } |
223 } | 256 } |
224 | 257 |
225 private: | 258 protected: |
226 std::string fake_server_address_; | 259 std::string fake_server_address_; |
jochen (gone - plz use gerrit)
2012/12/17 16:21:20
since you're only reading these values, it would b
marja
2012/12/17 16:45:58
Done.
| |
227 std::string test_path_; | 260 std::string test_path_; |
261 | |
262 private: | |
228 string16 title_pass_; | 263 string16 title_pass_; |
229 string16 title_storing_; | 264 string16 title_storing_; |
230 string16 title_error_write_failed_; | 265 string16 title_error_write_failed_; |
231 string16 title_error_empty_; | 266 string16 title_error_empty_; |
232 | 267 |
233 DISALLOW_COPY_AND_ASSIGN(BetterSessionRestoreTest); | 268 DISALLOW_COPY_AND_ASSIGN(BetterSessionRestoreTest); |
234 }; | 269 }; |
235 | 270 |
236 class ContinueWhereILeftOffTest : public BetterSessionRestoreTest { | 271 class ContinueWhereILeftOffTest : public BetterSessionRestoreTest { |
237 public: | 272 public: |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
468 // ... but not if the content setting is set to clear on exit. | 503 // ... but not if the content setting is set to clear on exit. |
469 CookieSettings::Factory::GetForProfile(browser()->profile())-> | 504 CookieSettings::Factory::GetForProfile(browser()->profile())-> |
470 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY); | 505 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY); |
471 } | 506 } |
472 | 507 |
473 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, CookiesClearedOnExit) { | 508 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, CookiesClearedOnExit) { |
474 content::WebContents* web_contents = chrome::GetActiveWebContents(browser()); | 509 content::WebContents* web_contents = chrome::GetActiveWebContents(browser()); |
475 EXPECT_EQ(std::string(chrome::kAboutBlankURL), web_contents->GetURL().spec()); | 510 EXPECT_EQ(std::string(chrome::kAboutBlankURL), web_contents->GetURL().spec()); |
476 StoreDataWithPage("local_storage.html"); | 511 StoreDataWithPage("local_storage.html"); |
477 } | 512 } |
513 | |
514 class BetterSessionRestoreCrashTest : public BetterSessionRestoreTest { | |
515 public: | |
516 virtual void SetUpOnMainThread() OVERRIDE { | |
jochen (gone - plz use gerrit)
2012/12/17 16:21:20
you should invoke the BetterSessionRestoreTest::Se
marja
2012/12/17 16:45:58
Done.
| |
517 SessionStartupPref::SetStartupPref( | |
518 browser()->profile(), SessionStartupPref(SessionStartupPref::DEFAULT)); | |
519 } | |
jochen (gone - plz use gerrit)
2012/12/17 16:21:20
nit empty line after this
marja
2012/12/17 16:45:58
Done.
| |
520 protected: | |
521 void CrashTestWithPage(const std::string& filename) { | |
522 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
523 FilePath profile_path = profile_manager->user_data_dir().Append( | |
524 FILE_PATH_LITERAL("test_profile")); | |
525 Profile* profile = profile_manager->GetProfile(profile_path); | |
526 ASSERT_TRUE(profile); | |
527 chrome::NewEmptyWindow(profile); | |
528 Browser* browser1 = FindOneOtherBrowserForProfile(profile, NULL); | |
jochen (gone - plz use gerrit)
2012/12/17 16:21:20
can you pick a more descriptive name?
marja
2012/12/17 16:45:58
Done.
| |
529 ASSERT_TRUE(browser1); | |
530 StoreDataWithPage(browser1, filename); | |
531 | |
532 // Session restore data is written lazily but we cannot restore data that | |
533 // was not saved. Be less lazy for the test. | |
534 SessionServiceFactory::GetForProfile(profile)->Save(); | |
535 | |
536 // Simulate a crash and a restart. | |
537 browser()->window()->Close(); | |
538 browser1->window()->Close(); | |
539 SessionServiceFactory::GetForProfile(profile)->backend()-> | |
540 MoveCurrentSessionToLastSession(); | |
541 static_cast<ProfileImpl*>(profile)->last_session_exit_type_ = | |
542 Profile::EXIT_CRASHED; | |
543 | |
544 CommandLine dummy(CommandLine::NO_PROGRAM); | |
545 int return_code; | |
546 StartupBrowserCreator browser_creator; | |
547 std::vector<Profile*> last_opened_profiles(1, profile); | |
548 browser_creator.Start(dummy, profile_manager->user_data_dir(), profile, | |
549 last_opened_profiles, &return_code); | |
550 | |
551 // The browser displays an info bar, use it to restore the session. | |
552 Browser* browser2 = FindOneOtherBrowserForProfile(profile, browser1); | |
jochen (gone - plz use gerrit)
2012/12/17 16:21:20
same here
marja
2012/12/17 16:45:58
Done.
| |
553 ASSERT_TRUE(browser2); | |
554 content::WebContents* web_contents = chrome::GetWebContentsAt(browser2, 0); | |
555 ASSERT_TRUE(web_contents); | |
556 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL()); | |
557 InfoBarTabHelper* info_bar_tab_helper = | |
558 InfoBarTabHelper::FromWebContents(web_contents); | |
559 EXPECT_EQ(1U, info_bar_tab_helper->GetInfoBarCount()); | |
560 ConfirmInfoBarDelegate* info_bar_delegate = | |
561 static_cast<ConfirmInfoBarDelegate*>( | |
562 info_bar_tab_helper->GetInfoBarDelegateAt(0)); | |
563 info_bar_delegate->Accept(); | |
564 | |
565 // Session restore is done ascynhronously. | |
566 base::RunLoop loop; | |
567 loop.RunUntilIdle(); | |
568 | |
569 // Check the restored page. | |
570 web_contents = chrome::GetWebContentsAt(browser2, 0); | |
571 ASSERT_TRUE(web_contents); | |
572 EXPECT_EQ(GURL(fake_server_address_ + test_path_ + filename), | |
573 web_contents->GetURL()); | |
574 CheckReloadedPageRestored(browser2); | |
575 } | |
576 }; | |
jochen (gone - plz use gerrit)
2012/12/17 16:21:20
DISALLOW_COPY_AND_ASSIGN
marja
2012/12/17 16:45:58
Done.
| |
577 | |
578 IN_PROC_BROWSER_TEST_F(BetterSessionRestoreCrashTest, SessionCookies) { | |
579 CrashTestWithPage("session_cookies.html"); | |
580 } | |
OLD | NEW |