Index: chrome/browser/prerender/prerender_resource_handler_unittest.cc |
diff --git a/chrome/browser/prerender/prerender_resource_handler_unittest.cc b/chrome/browser/prerender/prerender_resource_handler_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c7437126e61545ac1dd5889f34eb20438a7a5920 |
--- /dev/null |
+++ b/chrome/browser/prerender/prerender_resource_handler_unittest.cc |
@@ -0,0 +1,159 @@ |
+// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/prerender/prerender_resource_handler.h" |
+#include "chrome/common/resource_response.h" |
+#include "net/http/http_response_headers.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+class MockResourceHandler : public ResourceHandler { |
+ public: |
+ MockResourceHandler() {} |
+ |
+ virtual bool OnUploadProgress(int request_id, |
+ uint64 position, |
+ uint64 size) { |
+ return true; |
+ } |
+ |
+ virtual bool OnRequestRedirected(int request_id, const GURL& url, |
+ ResourceResponse* response, |
+ bool* defer) { |
+ *defer = false; |
+ return true; |
+ } |
+ |
+ virtual bool OnResponseStarted(int request_id, |
+ ResourceResponse* response) { |
+ return true; |
+ } |
+ |
+ virtual bool OnWillStart(int request_id, const GURL& url, bool* defer) { |
+ *defer = false; |
+ return true; |
+ } |
+ |
+ virtual bool OnWillRead(int request_id, |
+ net::IOBuffer** buf, |
+ int* buf_size, |
+ int min_size) { |
+ return true; |
+ } |
+ |
+ virtual bool OnReadCompleted(int request_id, int* bytes_read) { |
+ return true; |
+ } |
+ |
+ virtual bool OnResponseCompleted(int request_id, |
+ const URLRequestStatus& status, |
+ const std::string& security_info) { |
+ return true; |
+ } |
+ |
+ virtual void OnRequestClosed() { |
+ } |
+ |
+ virtual void OnDataDownloaded(int request_id, int bytes_downloaded) {} |
+}; |
+ |
+} // namespace |
+ |
+class PrerenderResourceHandlerTest : public testing::Test { |
+ protected: |
+ PrerenderResourceHandlerTest() |
+ : mock_handler_(new MockResourceHandler()), |
+ ALLOW_THIS_IN_INITIALIZER_LIST( |
+ pre_handler_(new PrerenderResourceHandler( |
+ mock_handler_, |
+ NewCallback( |
+ this, |
+ &PrerenderResourceHandlerTest::SetLastHandledURL)))), |
+ ui_thread_(BrowserThread::UI, &loop_), |
+ default_url_("http://www.prerender.com") { |
+ } |
+ |
+ void SetLastHandledURL(const GURL& url) { |
+ last_handled_url_ = url; |
+ } |
+ |
+ // Common logic shared by many of the tests |
+ void StartPrerendering(const std::string& mime_type, |
+ const std::string& headers) { |
+ int request_id = 1; |
+ bool defer = false; |
+ EXPECT_TRUE(pre_handler_->OnWillStart(request_id, default_url_, &defer)); |
+ EXPECT_FALSE(defer); |
+ scoped_refptr<ResourceResponse> response(new ResourceResponse); |
+ response->response_head.mime_type = mime_type; |
+ response->response_head.headers = new net::HttpResponseHeaders(headers); |
+ EXPECT_TRUE(last_handled_url_.is_empty()); |
+ |
+ // Start the response. If it is able to prerender, a task will |
+ // be posted to loop_ (masquerading as the UI thread), and |
+ // |SetLastHandledURL| will be called. |
+ EXPECT_TRUE(pre_handler_->OnResponseStarted(request_id, response)); |
+ loop_.RunAllPending(); |
+ } |
+ |
+ scoped_refptr<MockResourceHandler> mock_handler_; |
+ scoped_refptr<PrerenderResourceHandler> pre_handler_; |
+ MessageLoop loop_; |
+ BrowserThread ui_thread_; |
+ GURL last_handled_url_; |
+ GURL default_url_; |
+}; |
+ |
+namespace { |
+ |
+TEST_F(PrerenderResourceHandlerTest, NoOp) { |
+} |
+ |
+// Tests that a valid HTML resource will correctly get diverted |
+// to the PrerenderManager. |
+TEST_F(PrerenderResourceHandlerTest, Prerender) { |
+ StartPrerendering("text/html", "HTTP/1.1 200 OK\r\n\r\n"); |
+ EXPECT_EQ(default_url_, last_handled_url_); |
+} |
+ |
+// Tests that a resource with the wrong MIME type (a GIF in this example) |
+// will not be diverted to the PrerenderManager. |
+TEST_F(PrerenderResourceHandlerTest, PrerenderWrongMimeType) { |
+ StartPrerendering("image/gif", "HTTP/1.1 200 OK\r\n\r\n"); |
+ EXPECT_TRUE(last_handled_url_.is_empty()); |
+} |
+ |
+// Tests that a resource with a non-200 response will not be diverted |
+// to the PrerenderManager |
+TEST_F(PrerenderResourceHandlerTest, PrerenderBadResponseCode) { |
+ StartPrerendering("text/html", "HTTP/1.1 403 Forbidden\r\n\r\n"); |
+ EXPECT_TRUE(last_handled_url_.is_empty()); |
+} |
+ |
+// Tests that the final request in a redirect chain will |
+// get diverted to the PrerenderManager. |
+TEST_F(PrerenderResourceHandlerTest, PrerenderRedirect) { |
+ int request_id = 1; |
+ GURL url_redirect("http://www.redirect.com"); |
+ bool defer = false; |
+ EXPECT_TRUE(pre_handler_->OnWillStart(request_id, default_url_, &defer)); |
+ EXPECT_FALSE(defer); |
+ EXPECT_TRUE(pre_handler_->OnRequestRedirected(request_id, |
+ url_redirect, |
+ NULL, |
+ &defer)); |
+ EXPECT_FALSE(defer); |
+ scoped_refptr<ResourceResponse> response(new ResourceResponse); |
+ response->response_head.mime_type = "text/html"; |
+ response->response_head.headers = new net::HttpResponseHeaders( |
+ "HTTP/1.1 200 OK\r\n\r\n"); |
+ EXPECT_TRUE(pre_handler_->OnResponseStarted(request_id, response)); |
+ EXPECT_TRUE(last_handled_url_.is_empty()); |
+ loop_.RunAllPending(); |
+ EXPECT_EQ(url_redirect, last_handled_url_); |
+} |
+ |
+} |
willchan no longer on Chromium
2010/12/17 21:45:17
// namespace
|
+ |