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