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