| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include "chrome/browser/prerender/prerender_interceptor.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/callback.h" | |
| 10 #include "base/file_path.h" | |
| 11 #include "base/message_loop_proxy.h" | |
| 12 #include "base/scoped_ptr.h" | |
| 13 #include "chrome/browser/browser_thread.h" | |
| 14 #include "googleurl/src/gurl.h" | |
| 15 #include "net/base/load_flags.h" | |
| 16 #include "net/test/test_server.h" | |
| 17 #include "net/url_request/url_request_unittest.h" | |
| 18 | |
| 19 class PrerenderInterceptorTest : public testing::Test { | |
| 20 protected: | |
| 21 PrerenderInterceptorTest(); | |
| 22 | |
| 23 void MakeTestUrl(const std::string& base); | |
| 24 virtual void SetUp(); | |
| 25 | |
| 26 net::TestServer test_server_; | |
| 27 GURL gurl_; | |
| 28 GURL last_intercepted_gurl_; | |
| 29 scoped_ptr<net::URLRequest> req_; | |
| 30 | |
| 31 private: | |
| 32 void SetLastInterceptedGurl(const GURL& url); | |
| 33 | |
| 34 PrerenderInterceptor prerender_interceptor_; | |
| 35 MessageLoopForIO io_loop_; | |
| 36 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | |
| 37 BrowserThread ui_thread_; | |
| 38 TestDelegate delegate_for_req_; | |
| 39 }; | |
| 40 | |
| 41 PrerenderInterceptorTest::PrerenderInterceptorTest() | |
| 42 : test_server_(net::TestServer::TYPE_HTTP, | |
| 43 FilePath(FILE_PATH_LITERAL("chrome/test/data"))), | |
| 44 last_intercepted_gurl_("http://not.initialized/"), | |
| 45 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 46 prerender_interceptor_( | |
| 47 NewCallback(this, | |
| 48 &PrerenderInterceptorTest::SetLastInterceptedGurl))), | |
| 49 ui_thread_(BrowserThread::UI, &io_loop_) { | |
| 50 } | |
| 51 | |
| 52 void PrerenderInterceptorTest::SetUp() { | |
| 53 testing::Test::SetUp(); | |
| 54 last_intercepted_gurl_ = GURL("http://nothing.intercepted/"); | |
| 55 | |
| 56 io_message_loop_proxy_ = base::MessageLoopProxy::CreateForCurrentThread(); | |
| 57 ASSERT_TRUE(test_server_.Start()); | |
| 58 } | |
| 59 | |
| 60 void PrerenderInterceptorTest::MakeTestUrl(const std::string& base) { | |
| 61 gurl_ = test_server_.GetURL(base); | |
| 62 req_.reset(new TestURLRequest(gurl_, &delegate_for_req_)); | |
| 63 } | |
| 64 | |
| 65 void PrerenderInterceptorTest::SetLastInterceptedGurl(const GURL& url) { | |
| 66 last_intercepted_gurl_ = url; | |
| 67 } | |
| 68 | |
| 69 namespace { | |
| 70 | |
| 71 TEST_F(PrerenderInterceptorTest, Interception) { | |
| 72 MakeTestUrl("files/prerender/doc1.html"); | |
| 73 req_->set_load_flags(req_->load_flags() | net::LOAD_PREFETCH); | |
| 74 req_->Start(); | |
| 75 | |
| 76 MessageLoop::current()->Run(); | |
| 77 EXPECT_EQ(net::URLRequestStatus::SUCCESS, req_->status().status()); | |
| 78 EXPECT_EQ(gurl_, last_intercepted_gurl_); | |
| 79 } | |
| 80 | |
| 81 TEST_F(PrerenderInterceptorTest, NotAPrefetch) { | |
| 82 MakeTestUrl("files/prerender/doc2.html"); | |
| 83 req_->set_load_flags(req_->load_flags() & ~net::LOAD_PREFETCH); | |
| 84 req_->Start(); | |
| 85 | |
| 86 MessageLoop::current()->Run(); | |
| 87 EXPECT_EQ(net::URLRequestStatus::SUCCESS, req_->status().status()); | |
| 88 EXPECT_NE(gurl_, last_intercepted_gurl_); | |
| 89 } | |
| 90 | |
| 91 TEST_F(PrerenderInterceptorTest, WrongMimeType) { | |
| 92 MakeTestUrl("files/prerender/image.jpeg"); | |
| 93 req_->set_load_flags(req_->load_flags() | net::LOAD_PREFETCH); | |
| 94 req_->Start(); | |
| 95 | |
| 96 MessageLoop::current()->Run(); | |
| 97 EXPECT_EQ(net::URLRequestStatus::SUCCESS, req_->status().status()); | |
| 98 EXPECT_NE(gurl_, last_intercepted_gurl_); | |
| 99 } | |
| 100 | |
| 101 } // namespace | |
| OLD | NEW |