diff options
-rw-r--r-- | chrome/browser/sessions/better_session_restore_browsertest.cc | 83 |
1 files changed, 54 insertions, 29 deletions
diff --git a/chrome/browser/sessions/better_session_restore_browsertest.cc b/chrome/browser/sessions/better_session_restore_browsertest.cc index 82f17ea..474ffbd 100644 --- a/chrome/browser/sessions/better_session_restore_browsertest.cc +++ b/chrome/browser/sessions/better_session_restore_browsertest.cc @@ -5,6 +5,8 @@ #include "base/command_line.h" #include "base/file_path.h" #include "base/file_util.h" +#include "base/lazy_instance.h" +#include "base/path_service.h" #include "base/utf_string_conversions.h" #include "chrome/browser/prefs/session_startup_pref.h" #include "chrome/browser/ui/browser.h" @@ -15,50 +17,74 @@ #include "content/public/browser/web_contents.h" #include "content/public/test/browser_test_utils.h" #include "net/base/net_util.h" +#include "net/url_request/url_request.h" +#include "net/url_request/url_request_filter.h" +#include "net/url_request/url_request_test_job.h" #include "webkit/dom_storage/dom_storage_area.h" namespace { -// The tests server is started separately for each test function (including PRE_ -// functions). We need a test server which always uses the same port, so that -// the pages can be accessed with the same URLs after restoring the browser -// session. -class FixedPortTestServer : public net::LocalTestServer { - public: - explicit FixedPortTestServer(uint16 port) - : LocalTestServer( - net::TestServer::TYPE_HTTP, - net::TestServer::kLocalhost, - FilePath().AppendASCII("chrome/test/data")) { - BaseTestServer::SetPort(port); - } +// We need to serve the test files so that PRE_Test and Test can access the same +// page using the same URL. In addition, perceived security origin of the page +// needs to stay the same, so e.g., redirecting the URL requests doesn't +// work. (If we used a test server, the PRE_Test and Test would have separate +// instances running on separate ports.) - private: - DISALLOW_COPY_AND_ASSIGN(FixedPortTestServer); -}; +static base::LazyInstance<std::map<std::string, std::string> > file_contents = + LAZY_INSTANCE_INITIALIZER; + +net::URLRequestJob* URLRequestFaker( + net::URLRequest* request, + net::NetworkDelegate* network_delegate, + const std::string& scheme) { + return new net::URLRequestTestJob( + request, network_delegate, net::URLRequestTestJob::test_headers(), + file_contents.Get()[request->url().path()], true); +} } // namespace class BetterSessionRestoreTest : public InProcessBrowserTest { public: BetterSessionRestoreTest() - : test_server_(8001), - title_pass_(ASCIIToUTF16("PASS")), + : title_pass_(ASCIIToUTF16("PASS")), title_storing_(ASCIIToUTF16("STORING")), title_error_write_failed_(ASCIIToUTF16("ERROR_WRITE_FAILED")), - title_error_empty_(ASCIIToUTF16("ERROR_EMPTY")) { - CHECK(test_server_.Start()); + title_error_empty_(ASCIIToUTF16("ERROR_EMPTY")), + fake_server_address_("http://www.test.com/"), + test_path_("session_restore/") { + // Set up the URL request filtering. + std::vector<std::string> test_files; + test_files.push_back("common.js"); + test_files.push_back("session_cookies.html"); + test_files.push_back("session_storage.html"); + FilePath test_file_dir; + CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &test_file_dir)); + test_file_dir = + test_file_dir.AppendASCII("chrome/test/data").AppendASCII(test_path_); + + for (std::vector<std::string>::const_iterator it = test_files.begin(); + it != test_files.end(); ++it) { + FilePath path = test_file_dir.AppendASCII(*it); + std::string contents; + CHECK(file_util::ReadFileToString(path, &contents)); + file_contents.Get()["/" + test_path_ + *it] = contents; + net::URLRequestFilter::GetInstance()->AddUrlHandler( + GURL(fake_server_address_ + test_path_ + *it), + &URLRequestFaker); + } } + protected: void StoreDataWithPage(const std::string& filename) { - GURL url = test_server_.GetURL("files/session_restore/" + filename); content::WebContents* web_contents = chrome::GetActiveWebContents(browser()); content::TitleWatcher title_watcher(web_contents, title_storing_); title_watcher.AlsoWaitForTitle(title_pass_); title_watcher.AlsoWaitForTitle(title_error_write_failed_); title_watcher.AlsoWaitForTitle(title_error_empty_); - ui_test_utils::NavigateToURL(browser(), url); + ui_test_utils::NavigateToURL( + browser(), GURL(fake_server_address_ + test_path_ + filename)); string16 final_title = title_watcher.WaitAndGetTitle(); EXPECT_EQ(title_storing_, final_title); } @@ -78,11 +104,12 @@ class BetterSessionRestoreTest : public InProcessBrowserTest { } private: - FixedPortTestServer test_server_; string16 title_pass_; string16 title_storing_; string16 title_error_write_failed_; string16 title_error_empty_; + std::string fake_server_address_; + std::string test_path_; DISALLOW_COPY_AND_ASSIGN(BetterSessionRestoreTest); }; @@ -95,12 +122,10 @@ class BetterSessionRestoreTest : public InProcessBrowserTest { # define MAYBE_PRE_SessionStorage DISABLED_PRE_SessionStorage # define MAYBE_SessionStorage DISABLED_SessionStorage #else -// SessionCookies and SessionStorage are failing consistently on webkit canary -// builders. http://crbug.com/156444. -# define MAYBE_PRE_SessionCookies DISABLED_PRE_SessionCookies -# define MAYBE_SessionCookies DISABLED_SessionCookies -# define MAYBE_PRE_SessionStorage DISABLED_PRE_SessionStorage -# define MAYBE_SessionStorage DISABLED_SessionStorage +# define MAYBE_PRE_SessionCookies PRE_SessionCookies +# define MAYBE_SessionCookies SessionCookies +# define MAYBE_PRE_SessionStorage PRE_SessionStorage +# define MAYBE_SessionStorage SessionStorage #endif // crbug.com/156981 |