Further cleanup of some assets
[chromium-blink-merge.git] / content / browser / resolve_proxy_msg_helper_unittest.cc
blob9dab2939cdf45879dd306276064e3e19bee810cd
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.
5 #include "content/browser/resolve_proxy_msg_helper.h"
7 #include "content/browser/browser_thread_impl.h"
8 #include "content/common/view_messages.h"
9 #include "ipc/ipc_test_sink.h"
10 #include "net/base/net_errors.h"
11 #include "net/proxy/mock_proxy_resolver.h"
12 #include "net/proxy/proxy_config_service.h"
13 #include "net/proxy/proxy_service.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace content {
18 // This ProxyConfigService always returns "http://pac" as the PAC url to use.
19 class MockProxyConfigService : public net::ProxyConfigService {
20 public:
21 void AddObserver(Observer* observer) override {}
22 void RemoveObserver(Observer* observer) override {}
23 ConfigAvailability GetLatestProxyConfig(net::ProxyConfig* results) override {
24 *results = net::ProxyConfig::CreateFromCustomPacURL(GURL("http://pac"));
25 return CONFIG_VALID;
29 class TestResolveProxyMsgHelper : public ResolveProxyMsgHelper {
30 public:
31 TestResolveProxyMsgHelper(
32 net::ProxyService* proxy_service,
33 IPC::Listener* listener)
34 : ResolveProxyMsgHelper(proxy_service),
35 listener_(listener) {}
36 bool Send(IPC::Message* message) override {
37 listener_->OnMessageReceived(*message);
38 delete message;
39 return true;
42 protected:
43 ~TestResolveProxyMsgHelper() override {}
45 IPC::Listener* listener_;
48 class ResolveProxyMsgHelperTest : public testing::Test, public IPC::Listener {
49 public:
50 struct PendingResult {
51 PendingResult(bool result,
52 const std::string& proxy_list)
53 : result(result), proxy_list(proxy_list) {
56 bool result;
57 std::string proxy_list;
60 ResolveProxyMsgHelperTest()
61 : resolver_factory_(new net::MockAsyncProxyResolverFactory(false)),
62 service_(new net::ProxyService(new MockProxyConfigService,
63 make_scoped_ptr(resolver_factory_),
64 NULL)),
65 helper_(new TestResolveProxyMsgHelper(service_.get(), this)),
66 io_thread_(BrowserThread::IO, &message_loop_) {
67 test_sink_.AddFilter(this);
70 protected:
71 const PendingResult* pending_result() const { return pending_result_.get(); }
73 void clear_pending_result() {
74 pending_result_.reset();
77 IPC::Message* GenerateReply() {
78 bool temp_bool;
79 std::string temp_string;
80 ViewHostMsg_ResolveProxy message(GURL(), &temp_bool, &temp_string);
81 return IPC::SyncMessage::GenerateReply(&message);
84 net::MockAsyncProxyResolverFactory* resolver_factory_;
85 net::MockAsyncProxyResolver resolver_;
86 scoped_ptr<net::ProxyService> service_;
87 scoped_refptr<ResolveProxyMsgHelper> helper_;
88 scoped_ptr<PendingResult> pending_result_;
90 private:
91 bool OnMessageReceived(const IPC::Message& msg) override {
92 base::TupleTypes<ViewHostMsg_ResolveProxy::ReplyParam>::ValueTuple
93 reply_data;
94 EXPECT_TRUE(ViewHostMsg_ResolveProxy::ReadReplyParam(&msg, &reply_data));
95 DCHECK(!pending_result_.get());
96 pending_result_.reset(
97 new PendingResult(base::get<0>(reply_data), base::get<1>(reply_data)));
98 test_sink_.ClearMessages();
99 return true;
102 base::MessageLoopForIO message_loop_;
103 BrowserThreadImpl io_thread_;
104 IPC::TestSink test_sink_;
107 // Issue three sequential requests -- each should succeed.
108 TEST_F(ResolveProxyMsgHelperTest, Sequential) {
109 GURL url1("http://www.google1.com/");
110 GURL url2("http://www.google2.com/");
111 GURL url3("http://www.google3.com/");
113 // Messages are deleted by the sink.
114 IPC::Message* msg1 = GenerateReply();
115 IPC::Message* msg2 = GenerateReply();
116 IPC::Message* msg3 = GenerateReply();
118 // Execute each request sequentially (so there are never 2 requests
119 // outstanding at the same time).
121 helper_->OnResolveProxy(url1, msg1);
123 // Finish ProxyService's initialization.
124 ASSERT_EQ(1u, resolver_factory_->pending_requests().size());
125 resolver_factory_->pending_requests()[0]->CompleteNowWithForwarder(
126 net::OK, &resolver_);
128 ASSERT_EQ(1u, resolver_.pending_requests().size());
129 EXPECT_EQ(url1, resolver_.pending_requests()[0]->url());
130 resolver_.pending_requests()[0]->results()->UseNamedProxy("result1:80");
131 resolver_.pending_requests()[0]->CompleteNow(net::OK);
133 // Check result.
134 EXPECT_EQ(true, pending_result()->result);
135 EXPECT_EQ("PROXY result1:80", pending_result()->proxy_list);
136 clear_pending_result();
138 helper_->OnResolveProxy(url2, msg2);
140 ASSERT_EQ(1u, resolver_.pending_requests().size());
141 EXPECT_EQ(url2, resolver_.pending_requests()[0]->url());
142 resolver_.pending_requests()[0]->results()->UseNamedProxy("result2:80");
143 resolver_.pending_requests()[0]->CompleteNow(net::OK);
145 // Check result.
146 EXPECT_EQ(true, pending_result()->result);
147 EXPECT_EQ("PROXY result2:80", pending_result()->proxy_list);
148 clear_pending_result();
150 helper_->OnResolveProxy(url3, msg3);
152 ASSERT_EQ(1u, resolver_.pending_requests().size());
153 EXPECT_EQ(url3, resolver_.pending_requests()[0]->url());
154 resolver_.pending_requests()[0]->results()->UseNamedProxy("result3:80");
155 resolver_.pending_requests()[0]->CompleteNow(net::OK);
157 // Check result.
158 EXPECT_EQ(true, pending_result()->result);
159 EXPECT_EQ("PROXY result3:80", pending_result()->proxy_list);
160 clear_pending_result();
163 // Issue a request while one is already in progress -- should be queued.
164 TEST_F(ResolveProxyMsgHelperTest, QueueRequests) {
165 GURL url1("http://www.google1.com/");
166 GURL url2("http://www.google2.com/");
167 GURL url3("http://www.google3.com/");
169 IPC::Message* msg1 = GenerateReply();
170 IPC::Message* msg2 = GenerateReply();
171 IPC::Message* msg3 = GenerateReply();
173 // Start three requests. Since the proxy resolver is async, all the
174 // requests will be pending.
176 helper_->OnResolveProxy(url1, msg1);
178 // Finish ProxyService's initialization.
179 ASSERT_EQ(1u, resolver_factory_->pending_requests().size());
180 resolver_factory_->pending_requests()[0]->CompleteNowWithForwarder(
181 net::OK, &resolver_);
183 helper_->OnResolveProxy(url2, msg2);
184 helper_->OnResolveProxy(url3, msg3);
186 // ResolveProxyHelper only keeps 1 request outstanding in ProxyService
187 // at a time.
188 ASSERT_EQ(1u, resolver_.pending_requests().size());
189 EXPECT_EQ(url1, resolver_.pending_requests()[0]->url());
191 resolver_.pending_requests()[0]->results()->UseNamedProxy("result1:80");
192 resolver_.pending_requests()[0]->CompleteNow(net::OK);
194 // Check result.
195 EXPECT_EQ(true, pending_result()->result);
196 EXPECT_EQ("PROXY result1:80", pending_result()->proxy_list);
197 clear_pending_result();
199 ASSERT_EQ(1u, resolver_.pending_requests().size());
200 EXPECT_EQ(url2, resolver_.pending_requests()[0]->url());
202 resolver_.pending_requests()[0]->results()->UseNamedProxy("result2:80");
203 resolver_.pending_requests()[0]->CompleteNow(net::OK);
205 // Check result.
206 EXPECT_EQ(true, pending_result()->result);
207 EXPECT_EQ("PROXY result2:80", pending_result()->proxy_list);
208 clear_pending_result();
210 ASSERT_EQ(1u, resolver_.pending_requests().size());
211 EXPECT_EQ(url3, resolver_.pending_requests()[0]->url());
213 resolver_.pending_requests()[0]->results()->UseNamedProxy("result3:80");
214 resolver_.pending_requests()[0]->CompleteNow(net::OK);
216 // Check result.
217 EXPECT_EQ(true, pending_result()->result);
218 EXPECT_EQ("PROXY result3:80", pending_result()->proxy_list);
219 clear_pending_result();
222 // Delete the helper while a request is in progress, and others are pending.
223 TEST_F(ResolveProxyMsgHelperTest, CancelPendingRequests) {
224 GURL url1("http://www.google1.com/");
225 GURL url2("http://www.google2.com/");
226 GURL url3("http://www.google3.com/");
228 // They will be deleted by the request's cancellation.
229 IPC::Message* msg1 = GenerateReply();
230 IPC::Message* msg2 = GenerateReply();
231 IPC::Message* msg3 = GenerateReply();
233 // Start three requests. Since the proxy resolver is async, all the
234 // requests will be pending.
236 helper_->OnResolveProxy(url1, msg1);
238 // Finish ProxyService's initialization.
239 ASSERT_EQ(1u, resolver_factory_->pending_requests().size());
240 resolver_factory_->pending_requests()[0]->CompleteNowWithForwarder(
241 net::OK, &resolver_);
243 helper_->OnResolveProxy(url2, msg2);
244 helper_->OnResolveProxy(url3, msg3);
246 // ResolveProxyHelper only keeps 1 request outstanding in ProxyService
247 // at a time.
248 ASSERT_EQ(1u, resolver_.pending_requests().size());
249 EXPECT_EQ(url1, resolver_.pending_requests()[0]->url());
251 // Delete the underlying ResolveProxyMsgHelper -- this should cancel all
252 // the requests which are outstanding.
253 helper_ = NULL;
255 // The pending requests sent to the proxy resolver should have been cancelled.
257 EXPECT_EQ(0u, resolver_.pending_requests().size());
259 EXPECT_TRUE(pending_result() == NULL);
261 // It should also be the case that msg1, msg2, msg3 were deleted by the
262 // cancellation. (Else will show up as a leak in Valgrind).
265 } // namespace content