OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "net/proxy/dhcp_proxy_script_fetcher_win.h" | 5 #include "net/proxy/dhcp_proxy_script_fetcher_win.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/perftimer.h" | 10 #include "base/perftimer.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 } | 42 } |
43 } | 43 } |
44 | 44 |
45 // Helper for RealFetch* tests below. | 45 // Helper for RealFetch* tests below. |
46 class RealFetchTester { | 46 class RealFetchTester { |
47 public: | 47 public: |
48 RealFetchTester() | 48 RealFetchTester() |
49 : context_((new TestURLRequestContext())), | 49 : context_((new TestURLRequestContext())), |
50 fetcher_(new DhcpProxyScriptFetcherWin(context_.get())), | 50 fetcher_(new DhcpProxyScriptFetcherWin(context_.get())), |
51 finished_(false), | 51 finished_(false), |
52 ALLOW_THIS_IN_INITIALIZER_LIST( | |
53 completion_callback_(this, &RealFetchTester::OnCompletion)), | |
54 on_completion_is_error_(false) { | 52 on_completion_is_error_(false) { |
55 // Make sure the test ends. | 53 // Make sure the test ends. |
56 timeout_.Start(FROM_HERE, | 54 timeout_.Start(FROM_HERE, |
57 base::TimeDelta::FromSeconds(5), this, &RealFetchTester::OnTimeout); | 55 base::TimeDelta::FromSeconds(5), this, &RealFetchTester::OnTimeout); |
58 } | 56 } |
59 | 57 |
60 void RunTest() { | 58 void RunTest() { |
61 int result = fetcher_->Fetch(&pac_text_, &completion_callback_); | 59 int result = fetcher_->Fetch( |
| 60 &pac_text_, |
| 61 base::Bind(&RealFetchTester::OnCompletion, base::Unretained(this))); |
62 if (result != ERR_IO_PENDING) | 62 if (result != ERR_IO_PENDING) |
63 finished_ = true; | 63 finished_ = true; |
64 } | 64 } |
65 | 65 |
66 void RunTestWithCancel() { | 66 void RunTestWithCancel() { |
67 RunTest(); | 67 RunTest(); |
68 fetcher_->Cancel(); | 68 fetcher_->Cancel(); |
69 } | 69 } |
70 | 70 |
71 void RunTestWithDeferredCancel() { | 71 void RunTestWithDeferredCancel() { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // do something a bit more clever to track worker threads even when the | 109 // do something a bit more clever to track worker threads even when the |
110 // DhcpProxyScriptFetcherWin state machine has finished. | 110 // DhcpProxyScriptFetcherWin state machine has finished. |
111 void FinishTestAllowCleanup() { | 111 void FinishTestAllowCleanup() { |
112 base::PlatformThread::Sleep(30); | 112 base::PlatformThread::Sleep(30); |
113 } | 113 } |
114 | 114 |
115 scoped_refptr<URLRequestContext> context_; | 115 scoped_refptr<URLRequestContext> context_; |
116 scoped_ptr<DhcpProxyScriptFetcherWin> fetcher_; | 116 scoped_ptr<DhcpProxyScriptFetcherWin> fetcher_; |
117 bool finished_; | 117 bool finished_; |
118 string16 pac_text_; | 118 string16 pac_text_; |
119 OldCompletionCallbackImpl<RealFetchTester> completion_callback_; | |
120 base::OneShotTimer<RealFetchTester> timeout_; | 119 base::OneShotTimer<RealFetchTester> timeout_; |
121 base::OneShotTimer<RealFetchTester> cancel_timer_; | 120 base::OneShotTimer<RealFetchTester> cancel_timer_; |
122 bool on_completion_is_error_; | 121 bool on_completion_is_error_; |
123 }; | 122 }; |
124 | 123 |
125 TEST(DhcpProxyScriptFetcherWin, RealFetch) { | 124 TEST(DhcpProxyScriptFetcherWin, RealFetch) { |
126 // This tests a call to Fetch() with no stubbing out of dependencies. | 125 // This tests a call to Fetch() with no stubbing out of dependencies. |
127 // | 126 // |
128 // We don't make assumptions about the environment this unit test is | 127 // We don't make assumptions about the environment this unit test is |
129 // running in, so it just exercises the code to make sure there | 128 // running in, so it just exercises the code to make sure there |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 // situations, with actual network access fully stubbed out. | 208 // situations, with actual network access fully stubbed out. |
210 | 209 |
211 class DummyDhcpProxyScriptAdapterFetcher | 210 class DummyDhcpProxyScriptAdapterFetcher |
212 : public DhcpProxyScriptAdapterFetcher { | 211 : public DhcpProxyScriptAdapterFetcher { |
213 public: | 212 public: |
214 DummyDhcpProxyScriptAdapterFetcher() | 213 DummyDhcpProxyScriptAdapterFetcher() |
215 : DhcpProxyScriptAdapterFetcher(new TestURLRequestContext()), | 214 : DhcpProxyScriptAdapterFetcher(new TestURLRequestContext()), |
216 did_finish_(false), | 215 did_finish_(false), |
217 result_(OK), | 216 result_(OK), |
218 pac_script_(L"bingo"), | 217 pac_script_(L"bingo"), |
219 fetch_delay_ms_(1), | 218 fetch_delay_ms_(1) { |
220 client_callback_(NULL) { | |
221 } | 219 } |
222 | 220 |
223 void Fetch(const std::string& adapter_name, | 221 void Fetch(const std::string& adapter_name, |
224 OldCompletionCallback* callback) OVERRIDE { | 222 const CompletionCallback& callback) OVERRIDE { |
225 client_callback_ = callback; | 223 callback_ = callback; |
226 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(fetch_delay_ms_), | 224 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(fetch_delay_ms_), |
227 this, &DummyDhcpProxyScriptAdapterFetcher::OnTimer); | 225 this, &DummyDhcpProxyScriptAdapterFetcher::OnTimer); |
228 } | 226 } |
229 | 227 |
230 void Cancel() OVERRIDE { | 228 void Cancel() OVERRIDE { |
231 timer_.Stop(); | 229 timer_.Stop(); |
232 } | 230 } |
233 | 231 |
234 bool DidFinish() const OVERRIDE { | 232 bool DidFinish() const OVERRIDE { |
235 return did_finish_; | 233 return did_finish_; |
236 } | 234 } |
237 | 235 |
238 int GetResult() const OVERRIDE { | 236 int GetResult() const OVERRIDE { |
239 return result_; | 237 return result_; |
240 } | 238 } |
241 | 239 |
242 string16 GetPacScript() const OVERRIDE { | 240 string16 GetPacScript() const OVERRIDE { |
243 return pac_script_; | 241 return pac_script_; |
244 } | 242 } |
245 | 243 |
246 void OnTimer() { | 244 void OnTimer() { |
247 client_callback_->Run(result_); | 245 callback_.Run(result_); |
248 } | 246 } |
249 | 247 |
250 void Configure( | 248 void Configure( |
251 bool did_finish, int result, string16 pac_script, int fetch_delay_ms) { | 249 bool did_finish, int result, string16 pac_script, int fetch_delay_ms) { |
252 did_finish_ = did_finish; | 250 did_finish_ = did_finish; |
253 result_ = result; | 251 result_ = result; |
254 pac_script_ = pac_script; | 252 pac_script_ = pac_script; |
255 fetch_delay_ms_ = fetch_delay_ms; | 253 fetch_delay_ms_ = fetch_delay_ms; |
256 } | 254 } |
257 | 255 |
258 private: | 256 private: |
259 bool did_finish_; | 257 bool did_finish_; |
260 int result_; | 258 int result_; |
261 string16 pac_script_; | 259 string16 pac_script_; |
262 int fetch_delay_ms_; | 260 int fetch_delay_ms_; |
263 OldCompletionCallback* client_callback_; | 261 CompletionCallback callback_; |
264 base::OneShotTimer<DummyDhcpProxyScriptAdapterFetcher> timer_; | 262 base::OneShotTimer<DummyDhcpProxyScriptAdapterFetcher> timer_; |
265 }; | 263 }; |
266 | 264 |
267 class MockDhcpProxyScriptFetcherWin : public DhcpProxyScriptFetcherWin { | 265 class MockDhcpProxyScriptFetcherWin : public DhcpProxyScriptFetcherWin { |
268 public: | 266 public: |
269 class MockAdapterQuery : public AdapterQuery { | 267 class MockAdapterQuery : public AdapterQuery { |
270 public: | 268 public: |
271 MockAdapterQuery() { | 269 MockAdapterQuery() { |
272 } | 270 } |
273 | 271 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 | 363 |
366 int max_wait_ms_; | 364 int max_wait_ms_; |
367 int num_fetchers_created_; | 365 int num_fetchers_created_; |
368 base::WaitableEvent worker_finished_event_; | 366 base::WaitableEvent worker_finished_event_; |
369 }; | 367 }; |
370 | 368 |
371 class FetcherClient { | 369 class FetcherClient { |
372 public: | 370 public: |
373 FetcherClient() | 371 FetcherClient() |
374 : finished_(false), | 372 : finished_(false), |
375 result_(ERR_UNEXPECTED), | 373 result_(ERR_UNEXPECTED) { |
376 ALLOW_THIS_IN_INITIALIZER_LIST( | |
377 completion_callback_(this, &FetcherClient::OnCompletion)) { | |
378 } | 374 } |
379 | 375 |
380 void RunTest() { | 376 void RunTest() { |
381 int result = fetcher_.Fetch(&pac_text_, &completion_callback_); | 377 int result = fetcher_.Fetch( |
| 378 &pac_text_, |
| 379 base::Bind(&FetcherClient::OnCompletion, base::Unretained(this))); |
382 ASSERT_EQ(ERR_IO_PENDING, result); | 380 ASSERT_EQ(ERR_IO_PENDING, result); |
383 } | 381 } |
384 | 382 |
385 void RunMessageLoopUntilComplete() { | 383 void RunMessageLoopUntilComplete() { |
386 while (!finished_) { | 384 while (!finished_) { |
387 MessageLoop::current()->RunAllPending(); | 385 MessageLoop::current()->RunAllPending(); |
388 } | 386 } |
389 MessageLoop::current()->RunAllPending(); | 387 MessageLoop::current()->RunAllPending(); |
390 } | 388 } |
391 | 389 |
(...skipping 14 matching lines...) Expand all Loading... |
406 finished_ = false; | 404 finished_ = false; |
407 result_ = ERR_UNEXPECTED; | 405 result_ = ERR_UNEXPECTED; |
408 pac_text_ = L""; | 406 pac_text_ = L""; |
409 fetcher_.ResetTestState(); | 407 fetcher_.ResetTestState(); |
410 } | 408 } |
411 | 409 |
412 MockDhcpProxyScriptFetcherWin fetcher_; | 410 MockDhcpProxyScriptFetcherWin fetcher_; |
413 bool finished_; | 411 bool finished_; |
414 int result_; | 412 int result_; |
415 string16 pac_text_; | 413 string16 pac_text_; |
416 OldCompletionCallbackImpl<FetcherClient> completion_callback_; | |
417 }; | 414 }; |
418 | 415 |
419 // We separate out each test's logic so that we can easily implement | 416 // We separate out each test's logic so that we can easily implement |
420 // the ReuseFetcher test at the bottom. | 417 // the ReuseFetcher test at the bottom. |
421 void TestNormalCaseURLConfiguredOneAdapter(FetcherClient* client) { | 418 void TestNormalCaseURLConfiguredOneAdapter(FetcherClient* client) { |
422 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( | 419 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( |
423 new DummyDhcpProxyScriptAdapterFetcher()); | 420 new DummyDhcpProxyScriptAdapterFetcher()); |
424 adapter_fetcher->Configure(true, OK, L"bingo", 1); | 421 adapter_fetcher->Configure(true, OK, L"bingo", 1); |
425 client->fetcher_.PushBackAdapter("a", adapter_fetcher.release()); | 422 client->fetcher_.PushBackAdapter("a", adapter_fetcher.release()); |
426 client->RunTest(); | 423 client->RunTest(); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 } | 617 } |
621 | 618 |
622 // Re-do the first test to make sure the last test that was run did | 619 // Re-do the first test to make sure the last test that was run did |
623 // not leave things in a bad state. | 620 // not leave things in a bad state. |
624 (*test_functions.begin())(&client); | 621 (*test_functions.begin())(&client); |
625 } | 622 } |
626 | 623 |
627 } // namespace | 624 } // namespace |
628 | 625 |
629 } // namespace net | 626 } // namespace net |
OLD | NEW |