Modernize CJK default font prefs on Mac/Windows/Linux
[chromium-blink-merge.git] / content / browser / resolve_proxy_msg_helper_unittest.cc
blob1a19018e711d8941eb1c276d4f2dd19581500863
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_(new net::MockAsyncProxyResolver),
62 service_(
63 new net::ProxyService(new MockProxyConfigService, resolver_, NULL)),
64 helper_(new TestResolveProxyMsgHelper(service_.get(), this)),
65 io_thread_(BrowserThread::IO, &message_loop_) {
66 test_sink_.AddFilter(this);
69 protected:
70 const PendingResult* pending_result() const { return pending_result_.get(); }
72 void clear_pending_result() {
73 pending_result_.reset();
76 IPC::Message* GenerateReply() {
77 bool temp_bool;
78 std::string temp_string;
79 ViewHostMsg_ResolveProxy message(GURL(), &temp_bool, &temp_string);
80 return IPC::SyncMessage::GenerateReply(&message);
83 net::MockAsyncProxyResolver* resolver_;
84 scoped_ptr<net::ProxyService> service_;
85 scoped_refptr<ResolveProxyMsgHelper> helper_;
86 scoped_ptr<PendingResult> pending_result_;
88 private:
89 bool OnMessageReceived(const IPC::Message& msg) override {
90 TupleTypes<ViewHostMsg_ResolveProxy::ReplyParam>::ValueTuple reply_data;
91 EXPECT_TRUE(ViewHostMsg_ResolveProxy::ReadReplyParam(&msg, &reply_data));
92 DCHECK(!pending_result_.get());
93 pending_result_.reset(new PendingResult(reply_data.a, reply_data.b));
94 test_sink_.ClearMessages();
95 return true;
98 base::MessageLoopForIO message_loop_;
99 BrowserThreadImpl io_thread_;
100 IPC::TestSink test_sink_;
103 // Issue three sequential requests -- each should succeed.
104 TEST_F(ResolveProxyMsgHelperTest, Sequential) {
105 GURL url1("http://www.google1.com/");
106 GURL url2("http://www.google2.com/");
107 GURL url3("http://www.google3.com/");
109 // Messages are deleted by the sink.
110 IPC::Message* msg1 = GenerateReply();
111 IPC::Message* msg2 = GenerateReply();
112 IPC::Message* msg3 = GenerateReply();
114 // Execute each request sequentially (so there are never 2 requests
115 // outstanding at the same time).
117 helper_->OnResolveProxy(url1, msg1);
119 // Finish ProxyService's initialization.
120 resolver_->pending_set_pac_script_request()->CompleteNow(net::OK);
122 ASSERT_EQ(1u, resolver_->pending_requests().size());
123 EXPECT_EQ(url1, resolver_->pending_requests()[0]->url());
124 resolver_->pending_requests()[0]->results()->UseNamedProxy("result1:80");
125 resolver_->pending_requests()[0]->CompleteNow(net::OK);
127 // Check result.
128 EXPECT_EQ(true, pending_result()->result);
129 EXPECT_EQ("PROXY result1:80", pending_result()->proxy_list);
130 clear_pending_result();
132 helper_->OnResolveProxy(url2, msg2);
134 ASSERT_EQ(1u, resolver_->pending_requests().size());
135 EXPECT_EQ(url2, resolver_->pending_requests()[0]->url());
136 resolver_->pending_requests()[0]->results()->UseNamedProxy("result2:80");
137 resolver_->pending_requests()[0]->CompleteNow(net::OK);
139 // Check result.
140 EXPECT_EQ(true, pending_result()->result);
141 EXPECT_EQ("PROXY result2:80", pending_result()->proxy_list);
142 clear_pending_result();
144 helper_->OnResolveProxy(url3, msg3);
146 ASSERT_EQ(1u, resolver_->pending_requests().size());
147 EXPECT_EQ(url3, resolver_->pending_requests()[0]->url());
148 resolver_->pending_requests()[0]->results()->UseNamedProxy("result3:80");
149 resolver_->pending_requests()[0]->CompleteNow(net::OK);
151 // Check result.
152 EXPECT_EQ(true, pending_result()->result);
153 EXPECT_EQ("PROXY result3:80", pending_result()->proxy_list);
154 clear_pending_result();
157 // Issue a request while one is already in progress -- should be queued.
158 TEST_F(ResolveProxyMsgHelperTest, QueueRequests) {
159 GURL url1("http://www.google1.com/");
160 GURL url2("http://www.google2.com/");
161 GURL url3("http://www.google3.com/");
163 IPC::Message* msg1 = GenerateReply();
164 IPC::Message* msg2 = GenerateReply();
165 IPC::Message* msg3 = GenerateReply();
167 // Start three requests. Since the proxy resolver is async, all the
168 // requests will be pending.
170 helper_->OnResolveProxy(url1, msg1);
172 // Finish ProxyService's initialization.
173 resolver_->pending_set_pac_script_request()->CompleteNow(net::OK);
175 helper_->OnResolveProxy(url2, msg2);
176 helper_->OnResolveProxy(url3, msg3);
178 // ResolveProxyHelper only keeps 1 request outstanding in ProxyService
179 // at a time.
180 ASSERT_EQ(1u, resolver_->pending_requests().size());
181 EXPECT_EQ(url1, resolver_->pending_requests()[0]->url());
183 resolver_->pending_requests()[0]->results()->UseNamedProxy("result1:80");
184 resolver_->pending_requests()[0]->CompleteNow(net::OK);
186 // Check result.
187 EXPECT_EQ(true, pending_result()->result);
188 EXPECT_EQ("PROXY result1:80", pending_result()->proxy_list);
189 clear_pending_result();
191 ASSERT_EQ(1u, resolver_->pending_requests().size());
192 EXPECT_EQ(url2, resolver_->pending_requests()[0]->url());
194 resolver_->pending_requests()[0]->results()->UseNamedProxy("result2:80");
195 resolver_->pending_requests()[0]->CompleteNow(net::OK);
197 // Check result.
198 EXPECT_EQ(true, pending_result()->result);
199 EXPECT_EQ("PROXY result2:80", pending_result()->proxy_list);
200 clear_pending_result();
202 ASSERT_EQ(1u, resolver_->pending_requests().size());
203 EXPECT_EQ(url3, resolver_->pending_requests()[0]->url());
205 resolver_->pending_requests()[0]->results()->UseNamedProxy("result3:80");
206 resolver_->pending_requests()[0]->CompleteNow(net::OK);
208 // Check result.
209 EXPECT_EQ(true, pending_result()->result);
210 EXPECT_EQ("PROXY result3:80", pending_result()->proxy_list);
211 clear_pending_result();
214 // Delete the helper while a request is in progress, and others are pending.
215 TEST_F(ResolveProxyMsgHelperTest, CancelPendingRequests) {
216 GURL url1("http://www.google1.com/");
217 GURL url2("http://www.google2.com/");
218 GURL url3("http://www.google3.com/");
220 // They will be deleted by the request's cancellation.
221 IPC::Message* msg1 = GenerateReply();
222 IPC::Message* msg2 = GenerateReply();
223 IPC::Message* msg3 = GenerateReply();
225 // Start three requests. Since the proxy resolver is async, all the
226 // requests will be pending.
228 helper_->OnResolveProxy(url1, msg1);
230 // Finish ProxyService's initialization.
231 resolver_->pending_set_pac_script_request()->CompleteNow(net::OK);
233 helper_->OnResolveProxy(url2, msg2);
234 helper_->OnResolveProxy(url3, msg3);
236 // ResolveProxyHelper only keeps 1 request outstanding in ProxyService
237 // at a time.
238 ASSERT_EQ(1u, resolver_->pending_requests().size());
239 EXPECT_EQ(url1, resolver_->pending_requests()[0]->url());
241 // Delete the underlying ResolveProxyMsgHelper -- this should cancel all
242 // the requests which are outstanding.
243 helper_ = NULL;
245 // The pending requests sent to the proxy resolver should have been cancelled.
247 EXPECT_EQ(0u, resolver_->pending_requests().size());
249 EXPECT_TRUE(pending_result() == NULL);
251 // It should also be the case that msg1, msg2, msg3 were deleted by the
252 // cancellation. (Else will show up as a leak in Valgrind).
255 } // namespace content