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