Enables compositing support for webview.
[chromium-blink-merge.git] / net / proxy / dhcp_proxy_script_adapter_fetcher_win_unittest.cc
blob480f2f675fe3e64996c4e9b365804f7170d97347
1 // Copyright (c) 2012 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 "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h"
7 #include "base/perftimer.h"
8 #include "base/synchronization/waitable_event.h"
9 #include "base/test/test_timeouts.h"
10 #include "base/timer.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/test_completion_callback.h"
13 #include "net/proxy/mock_proxy_script_fetcher.h"
14 #include "net/proxy/proxy_script_fetcher_impl.h"
15 #include "net/test/test_server.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace net {
21 namespace {
23 const char* const kPacUrl = "http://pacserver/script.pac";
25 // In net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc there are a few
26 // tests that exercise DhcpProxyScriptAdapterFetcher end-to-end along with
27 // DhcpProxyScriptFetcherWin, i.e. it tests the end-to-end usage of Win32
28 // APIs and the network. In this file we test only by stubbing out
29 // functionality.
31 // Version of DhcpProxyScriptAdapterFetcher that mocks out dependencies
32 // to allow unit testing.
33 class MockDhcpProxyScriptAdapterFetcher
34 : public DhcpProxyScriptAdapterFetcher {
35 public:
36 explicit MockDhcpProxyScriptAdapterFetcher(URLRequestContext* context)
37 : DhcpProxyScriptAdapterFetcher(context),
38 dhcp_delay_(base::TimeDelta::FromMilliseconds(1)),
39 timeout_(TestTimeouts::action_timeout()),
40 configured_url_(kPacUrl),
41 fetcher_delay_ms_(1),
42 fetcher_result_(OK),
43 pac_script_("bingo") {
46 void Cancel() {
47 DhcpProxyScriptAdapterFetcher::Cancel();
48 fetcher_ = NULL;
51 virtual ProxyScriptFetcher* ImplCreateScriptFetcher() OVERRIDE {
52 // We don't maintain ownership of the fetcher, it is transferred to
53 // the caller.
54 fetcher_ = new MockProxyScriptFetcher();
55 if (fetcher_delay_ms_ != -1) {
56 fetcher_timer_.Start(FROM_HERE,
57 base::TimeDelta::FromMilliseconds(fetcher_delay_ms_),
58 this, &MockDhcpProxyScriptAdapterFetcher::OnFetcherTimer);
60 return fetcher_;
63 class DelayingDhcpQuery : public DhcpQuery {
64 public:
65 explicit DelayingDhcpQuery()
66 : DhcpQuery(),
67 test_finished_event_(true, false) {
70 std::string ImplGetPacURLFromDhcp(
71 const std::string& adapter_name) OVERRIDE {
72 PerfTimer timer;
73 test_finished_event_.TimedWait(dhcp_delay_);
74 return configured_url_;
77 base::WaitableEvent test_finished_event_;
78 base::TimeDelta dhcp_delay_;
79 std::string configured_url_;
82 virtual DhcpQuery* ImplCreateDhcpQuery() OVERRIDE {
83 dhcp_query_ = new DelayingDhcpQuery();
84 dhcp_query_->dhcp_delay_ = dhcp_delay_;
85 dhcp_query_->configured_url_ = configured_url_;
86 return dhcp_query_;
89 // Use a shorter timeout so tests can finish more quickly.
90 virtual base::TimeDelta ImplGetTimeout() const OVERRIDE {
91 return timeout_;
94 void OnFetcherTimer() {
95 // Note that there is an assumption by this mock implementation that
96 // DhcpProxyScriptAdapterFetcher::Fetch will call ImplCreateScriptFetcher
97 // and call Fetch on the fetcher before the message loop is re-entered.
98 // This holds true today, but if you hit this DCHECK the problem can
99 // possibly be resolved by having a separate subclass of
100 // MockProxyScriptFetcher that adds the delay internally (instead of
101 // the simple approach currently used in ImplCreateScriptFetcher above).
102 DCHECK(fetcher_ && fetcher_->has_pending_request());
103 fetcher_->NotifyFetchCompletion(fetcher_result_, pac_script_);
104 fetcher_ = NULL;
107 bool IsWaitingForFetcher() const {
108 return state() == STATE_WAIT_URL;
111 bool WasCancelled() const {
112 return state() == STATE_CANCEL;
115 void FinishTest() {
116 DCHECK(dhcp_query_);
117 dhcp_query_->test_finished_event_.Signal();
120 base::TimeDelta dhcp_delay_;
121 base::TimeDelta timeout_;
122 std::string configured_url_;
123 int fetcher_delay_ms_;
124 int fetcher_result_;
125 std::string pac_script_;
126 MockProxyScriptFetcher* fetcher_;
127 base::OneShotTimer<MockDhcpProxyScriptAdapterFetcher> fetcher_timer_;
128 scoped_refptr<DelayingDhcpQuery> dhcp_query_;
131 class FetcherClient {
132 public:
133 FetcherClient()
134 : url_request_context_(new TestURLRequestContext()),
135 fetcher_(
136 new MockDhcpProxyScriptAdapterFetcher(url_request_context_.get())) {
139 void WaitForResult(int expected_error) {
140 EXPECT_EQ(expected_error, callback_.WaitForResult());
143 void RunTest() {
144 fetcher_->Fetch("adapter name", callback_.callback());
147 void FinishTestAllowCleanup() {
148 fetcher_->FinishTest();
149 MessageLoop::current()->RunUntilIdle();
152 TestCompletionCallback callback_;
153 scoped_ptr<URLRequestContext> url_request_context_;
154 scoped_ptr<MockDhcpProxyScriptAdapterFetcher> fetcher_;
155 string16 pac_text_;
158 TEST(DhcpProxyScriptAdapterFetcher, NormalCaseURLNotInDhcp) {
159 FetcherClient client;
160 client.fetcher_->configured_url_ = "";
161 client.RunTest();
162 client.WaitForResult(ERR_PAC_NOT_IN_DHCP);
163 ASSERT_TRUE(client.fetcher_->DidFinish());
164 EXPECT_EQ(ERR_PAC_NOT_IN_DHCP, client.fetcher_->GetResult());
165 EXPECT_EQ(string16(L""), client.fetcher_->GetPacScript());
168 TEST(DhcpProxyScriptAdapterFetcher, NormalCaseURLInDhcp) {
169 FetcherClient client;
170 client.RunTest();
171 client.WaitForResult(OK);
172 ASSERT_TRUE(client.fetcher_->DidFinish());
173 EXPECT_EQ(OK, client.fetcher_->GetResult());
174 EXPECT_EQ(string16(L"bingo"), client.fetcher_->GetPacScript());
175 EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL());
178 TEST(DhcpProxyScriptAdapterFetcher, TimeoutDuringDhcp) {
179 // Does a Fetch() with a long enough delay on accessing DHCP that the
180 // fetcher should time out. This is to test a case manual testing found,
181 // where under certain circumstances (e.g. adapter enabled for DHCP and
182 // needs to retrieve its configuration from DHCP, but no DHCP server
183 // present on the network) accessing DHCP can take on the order of tens
184 // of seconds.
185 FetcherClient client;
186 client.fetcher_->dhcp_delay_ = TestTimeouts::action_max_timeout();
187 client.fetcher_->timeout_ = base::TimeDelta::FromMilliseconds(25);
189 PerfTimer timer;
190 client.RunTest();
191 // An error different from this would be received if the timeout didn't
192 // kick in.
193 client.WaitForResult(ERR_TIMED_OUT);
195 ASSERT_TRUE(client.fetcher_->DidFinish());
196 EXPECT_EQ(ERR_TIMED_OUT, client.fetcher_->GetResult());
197 EXPECT_EQ(string16(L""), client.fetcher_->GetPacScript());
198 EXPECT_EQ(GURL(), client.fetcher_->GetPacURL());
199 client.FinishTestAllowCleanup();
202 TEST(DhcpProxyScriptAdapterFetcher, CancelWhileDhcp) {
203 FetcherClient client;
204 client.RunTest();
205 client.fetcher_->Cancel();
206 MessageLoop::current()->RunUntilIdle();
207 ASSERT_FALSE(client.fetcher_->DidFinish());
208 ASSERT_TRUE(client.fetcher_->WasCancelled());
209 EXPECT_EQ(ERR_ABORTED, client.fetcher_->GetResult());
210 EXPECT_EQ(string16(L""), client.fetcher_->GetPacScript());
211 EXPECT_EQ(GURL(), client.fetcher_->GetPacURL());
212 client.FinishTestAllowCleanup();
215 TEST(DhcpProxyScriptAdapterFetcher, CancelWhileFetcher) {
216 FetcherClient client;
217 // This causes the mock fetcher not to pretend the
218 // fetcher finishes after a timeout.
219 client.fetcher_->fetcher_delay_ms_ = -1;
220 client.RunTest();
221 int max_loops = 4;
222 while (!client.fetcher_->IsWaitingForFetcher() && max_loops--) {
223 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
224 MessageLoop::current()->RunUntilIdle();
226 client.fetcher_->Cancel();
227 MessageLoop::current()->RunUntilIdle();
228 ASSERT_FALSE(client.fetcher_->DidFinish());
229 ASSERT_TRUE(client.fetcher_->WasCancelled());
230 EXPECT_EQ(ERR_ABORTED, client.fetcher_->GetResult());
231 EXPECT_EQ(string16(L""), client.fetcher_->GetPacScript());
232 // GetPacURL() still returns the URL fetched in this case.
233 EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL());
234 client.FinishTestAllowCleanup();
237 TEST(DhcpProxyScriptAdapterFetcher, CancelAtCompletion) {
238 FetcherClient client;
239 client.RunTest();
240 client.WaitForResult(OK);
241 client.fetcher_->Cancel();
242 // Canceling after you're done should have no effect, so these
243 // are identical expectations to the NormalCaseURLInDhcp test.
244 ASSERT_TRUE(client.fetcher_->DidFinish());
245 EXPECT_EQ(OK, client.fetcher_->GetResult());
246 EXPECT_EQ(string16(L"bingo"), client.fetcher_->GetPacScript());
247 EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL());
248 client.FinishTestAllowCleanup();
251 // Does a real fetch on a mock DHCP configuration.
252 class MockDhcpRealFetchProxyScriptAdapterFetcher
253 : public MockDhcpProxyScriptAdapterFetcher {
254 public:
255 explicit MockDhcpRealFetchProxyScriptAdapterFetcher(
256 URLRequestContext* context)
257 : MockDhcpProxyScriptAdapterFetcher(context),
258 url_request_context_(context) {
261 // Returns a real proxy script fetcher.
262 ProxyScriptFetcher* ImplCreateScriptFetcher() OVERRIDE {
263 ProxyScriptFetcher* fetcher =
264 new ProxyScriptFetcherImpl(url_request_context_);
265 return fetcher;
268 URLRequestContext* url_request_context_;
271 TEST(DhcpProxyScriptAdapterFetcher, MockDhcpRealFetch) {
272 TestServer test_server(
273 TestServer::TYPE_HTTP,
274 net::TestServer::kLocalhost,
275 FilePath(FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest")));
276 ASSERT_TRUE(test_server.Start());
278 GURL configured_url = test_server.GetURL("files/downloadable.pac");
280 FetcherClient client;
281 TestURLRequestContext url_request_context;
282 client.fetcher_.reset(
283 new MockDhcpRealFetchProxyScriptAdapterFetcher(
284 &url_request_context));
285 client.fetcher_->configured_url_ = configured_url.spec();
286 client.RunTest();
287 client.WaitForResult(OK);
288 ASSERT_TRUE(client.fetcher_->DidFinish());
289 EXPECT_EQ(OK, client.fetcher_->GetResult());
290 EXPECT_EQ(string16(L"-downloadable.pac-\n"), client.fetcher_->GetPacScript());
291 EXPECT_EQ(configured_url,
292 client.fetcher_->GetPacURL());
295 } // namespace
297 } // namespace net