| 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_adapter_fetcher_win.h" | 5 #include "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h" |
| 6 | 6 |
| 7 #include "base/perftimer.h" | 7 #include "base/perftimer.h" |
| 8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "base/test/test_timeouts.h" | 9 #include "base/test/test_timeouts.h" |
| 10 #include "base/timer.h" | 10 #include "base/timer.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 // the caller. | 53 // the caller. |
| 54 fetcher_ = new MockProxyScriptFetcher(); | 54 fetcher_ = new MockProxyScriptFetcher(); |
| 55 if (fetcher_delay_ms_ != -1) { | 55 if (fetcher_delay_ms_ != -1) { |
| 56 fetcher_timer_.Start(FROM_HERE, | 56 fetcher_timer_.Start(FROM_HERE, |
| 57 base::TimeDelta::FromMilliseconds(fetcher_delay_ms_), | 57 base::TimeDelta::FromMilliseconds(fetcher_delay_ms_), |
| 58 this, &MockDhcpProxyScriptAdapterFetcher::OnFetcherTimer); | 58 this, &MockDhcpProxyScriptAdapterFetcher::OnFetcherTimer); |
| 59 } | 59 } |
| 60 return fetcher_; | 60 return fetcher_; |
| 61 } | 61 } |
| 62 | 62 |
| 63 class DelayingWorkerThread : public WorkerThread { | 63 class DelayingDhcpQuery : public DhcpQuery { |
| 64 public: | 64 public: |
| 65 explicit DelayingWorkerThread( | 65 explicit DelayingDhcpQuery() |
| 66 const base::WeakPtr<DhcpProxyScriptAdapterFetcher>& owner) | 66 : DhcpQuery(), |
| 67 : WorkerThread(owner), | |
| 68 test_finished_event_(true, false) { | 67 test_finished_event_(true, false) { |
| 69 } | 68 } |
| 70 | 69 |
| 71 std::string ImplGetPacURLFromDhcp( | 70 std::string ImplGetPacURLFromDhcp( |
| 72 const std::string& adapter_name) OVERRIDE { | 71 const std::string& adapter_name) OVERRIDE { |
| 73 PerfTimer timer; | 72 PerfTimer timer; |
| 74 test_finished_event_.TimedWait(dhcp_delay_); | 73 test_finished_event_.TimedWait(dhcp_delay_); |
| 75 return configured_url_; | 74 return configured_url_; |
| 76 } | 75 } |
| 77 | 76 |
| 78 base::WaitableEvent test_finished_event_; | 77 base::WaitableEvent test_finished_event_; |
| 79 TimeDelta dhcp_delay_; | 78 TimeDelta dhcp_delay_; |
| 80 std::string configured_url_; | 79 std::string configured_url_; |
| 81 }; | 80 }; |
| 82 | 81 |
| 83 virtual WorkerThread* ImplCreateWorkerThread( | 82 virtual DhcpQuery* ImplCreateDhcpQuery() OVERRIDE { |
| 84 const base::WeakPtr<DhcpProxyScriptAdapterFetcher>& owner) OVERRIDE { | 83 dhcp_query_ = new DelayingDhcpQuery(); |
| 85 worker_thread_ = new DelayingWorkerThread(owner); | 84 dhcp_query_->dhcp_delay_ = TimeDelta::FromMilliseconds(dhcp_delay_ms_); |
| 86 worker_thread_->dhcp_delay_ = TimeDelta::FromMilliseconds(dhcp_delay_ms_); | 85 dhcp_query_->configured_url_ = configured_url_; |
| 87 worker_thread_->configured_url_ = configured_url_; | 86 return dhcp_query_; |
| 88 return worker_thread_; | |
| 89 } | 87 } |
| 90 | 88 |
| 91 // Use a shorter timeout so tests can finish more quickly. | 89 // Use a shorter timeout so tests can finish more quickly. |
| 92 virtual base::TimeDelta ImplGetTimeout() const OVERRIDE { | 90 virtual base::TimeDelta ImplGetTimeout() const OVERRIDE { |
| 93 return base::TimeDelta::FromMilliseconds(timeout_ms_); | 91 return base::TimeDelta::FromMilliseconds(timeout_ms_); |
| 94 } | 92 } |
| 95 | 93 |
| 96 void OnFetcherTimer() { | 94 void OnFetcherTimer() { |
| 97 // Note that there is an assumption by this mock implementation that | 95 // Note that there is an assumption by this mock implementation that |
| 98 // DhcpProxyScriptAdapterFetcher::Fetch will call ImplCreateScriptFetcher | 96 // DhcpProxyScriptAdapterFetcher::Fetch will call ImplCreateScriptFetcher |
| 99 // and call Fetch on the fetcher before the message loop is re-entered. | 97 // and call Fetch on the fetcher before the message loop is re-entered. |
| 100 // This holds true today, but if you hit this DCHECK the problem can | 98 // This holds true today, but if you hit this DCHECK the problem can |
| 101 // possibly be resolved by having a separate subclass of | 99 // possibly be resolved by having a separate subclass of |
| 102 // MockProxyScriptFetcher that adds the delay internally (instead of | 100 // MockProxyScriptFetcher that adds the delay internally (instead of |
| 103 // the simple approach currently used in ImplCreateScriptFetcher above). | 101 // the simple approach currently used in ImplCreateScriptFetcher above). |
| 104 DCHECK(fetcher_ && fetcher_->has_pending_request()); | 102 DCHECK(fetcher_ && fetcher_->has_pending_request()); |
| 105 fetcher_->NotifyFetchCompletion(fetcher_result_, pac_script_); | 103 fetcher_->NotifyFetchCompletion(fetcher_result_, pac_script_); |
| 106 fetcher_ = NULL; | 104 fetcher_ = NULL; |
| 107 } | 105 } |
| 108 | 106 |
| 109 bool IsWaitingForFetcher() const { | 107 bool IsWaitingForFetcher() const { |
| 110 return state() == STATE_WAIT_URL; | 108 return state() == STATE_WAIT_URL; |
| 111 } | 109 } |
| 112 | 110 |
| 113 bool WasCancelled() const { | 111 bool WasCancelled() const { |
| 114 return state() == STATE_CANCEL; | 112 return state() == STATE_CANCEL; |
| 115 } | 113 } |
| 116 | 114 |
| 117 void FinishTest() { | 115 void FinishTest() { |
| 118 DCHECK(worker_thread_); | 116 DCHECK(dhcp_query_); |
| 119 worker_thread_->test_finished_event_.Signal(); | 117 dhcp_query_->test_finished_event_.Signal(); |
| 120 } | 118 } |
| 121 | 119 |
| 122 int dhcp_delay_ms_; | 120 int dhcp_delay_ms_; |
| 123 int timeout_ms_; | 121 int timeout_ms_; |
| 124 std::string configured_url_; | 122 std::string configured_url_; |
| 125 int fetcher_delay_ms_; | 123 int fetcher_delay_ms_; |
| 126 int fetcher_result_; | 124 int fetcher_result_; |
| 127 std::string pac_script_; | 125 std::string pac_script_; |
| 128 MockProxyScriptFetcher* fetcher_; | 126 MockProxyScriptFetcher* fetcher_; |
| 129 base::OneShotTimer<MockDhcpProxyScriptAdapterFetcher> fetcher_timer_; | 127 base::OneShotTimer<MockDhcpProxyScriptAdapterFetcher> fetcher_timer_; |
| 130 scoped_refptr<DelayingWorkerThread> worker_thread_; | 128 scoped_refptr<DelayingDhcpQuery> dhcp_query_; |
| 131 }; | 129 }; |
| 132 | 130 |
| 133 class FetcherClient { | 131 class FetcherClient { |
| 134 public: | 132 public: |
| 135 FetcherClient() | 133 FetcherClient() |
| 136 : url_request_context_(new TestURLRequestContext()), | 134 : url_request_context_(new TestURLRequestContext()), |
| 137 fetcher_( | 135 fetcher_( |
| 138 new MockDhcpProxyScriptAdapterFetcher(url_request_context_.get())) { | 136 new MockDhcpProxyScriptAdapterFetcher(url_request_context_.get())) { |
| 139 } | 137 } |
| 140 | 138 |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 ASSERT_TRUE(client.fetcher_->DidFinish()); | 288 ASSERT_TRUE(client.fetcher_->DidFinish()); |
| 291 EXPECT_EQ(OK, client.fetcher_->GetResult()); | 289 EXPECT_EQ(OK, client.fetcher_->GetResult()); |
| 292 EXPECT_EQ(string16(L"-downloadable.pac-\n"), client.fetcher_->GetPacScript()); | 290 EXPECT_EQ(string16(L"-downloadable.pac-\n"), client.fetcher_->GetPacScript()); |
| 293 EXPECT_EQ(configured_url, | 291 EXPECT_EQ(configured_url, |
| 294 client.fetcher_->GetPacURL()); | 292 client.fetcher_->GetPacURL()); |
| 295 } | 293 } |
| 296 | 294 |
| 297 } // namespace | 295 } // namespace |
| 298 | 296 |
| 299 } // namespace net | 297 } // namespace net |
| OLD | NEW |