| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/data_reduction_proxy/browser/data_reduction_proxy_usage_sta
ts.h" | 5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_sta
ts.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "net/base/host_port_pair.h" |
| 9 #include "net/base/request_priority.h" | 10 #include "net/base/request_priority.h" |
| 10 #include "net/url_request/url_request.h" | 11 #include "net/url_request/url_request.h" |
| 11 #include "net/url_request/url_request_status.h" | 12 #include "net/url_request/url_request_status.h" |
| 12 #include "net/url_request/url_request_test_util.h" | 13 #include "net/url_request/url_request_test_util.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 using base::MessageLoop; | |
| 17 using base::MessageLoopProxy; | |
| 18 using data_reduction_proxy::DataReductionProxyParams; | |
| 19 using net::TestDelegate; | |
| 20 using net::TestURLRequestContext; | |
| 21 using net::URLRequest; | |
| 22 using net::URLRequestStatus; | |
| 23 using testing::Return; | 17 using testing::Return; |
| 24 | 18 |
| 25 namespace { | 19 namespace { |
| 26 | 20 |
| 27 class DataReductionProxyParamsMock : public DataReductionProxyParams { | 21 class DataReductionProxyParamsMock : |
| 22 public data_reduction_proxy::DataReductionProxyParams { |
| 28 public: | 23 public: |
| 29 DataReductionProxyParamsMock() : DataReductionProxyParams(0) {} | 24 DataReductionProxyParamsMock() : |
| 25 data_reduction_proxy::DataReductionProxyParams(0) {} |
| 30 virtual ~DataReductionProxyParamsMock() {} | 26 virtual ~DataReductionProxyParamsMock() {} |
| 31 | 27 |
| 32 MOCK_METHOD1(IsDataReductionProxyEligible, bool(const net::URLRequest*)); | 28 MOCK_CONST_METHOD2( |
| 29 IsDataReductionProxy, |
| 30 bool(const net::HostPortPair& host_port_pair, |
| 31 data_reduction_proxy::DataReductionProxyTypeInfo* proxy_info)); |
| 33 MOCK_CONST_METHOD2( | 32 MOCK_CONST_METHOD2( |
| 34 WasDataReductionProxyUsed, | 33 WasDataReductionProxyUsed, |
| 35 bool(const net::URLRequest*, | 34 bool(const net::URLRequest*, |
| 36 data_reduction_proxy::DataReductionProxyTypeInfo* proxy_servers)); | 35 data_reduction_proxy::DataReductionProxyTypeInfo* proxy_info)); |
| 37 | 36 |
| 38 private: | 37 private: |
| 39 DISALLOW_COPY_AND_ASSIGN(DataReductionProxyParamsMock); | 38 DISALLOW_COPY_AND_ASSIGN(DataReductionProxyParamsMock); |
| 40 }; | 39 }; |
| 41 | 40 |
| 42 } // namespace | 41 } // namespace |
| 43 | 42 |
| 44 namespace data_reduction_proxy { | 43 namespace data_reduction_proxy { |
| 45 | 44 |
| 46 class DataReductionProxyUsageStatsTest : public testing::Test { | 45 class DataReductionProxyUsageStatsTest : public testing::Test { |
| 47 public: | 46 public: |
| 48 DataReductionProxyUsageStatsTest() | 47 DataReductionProxyUsageStatsTest() |
| 49 : loop_proxy_(MessageLoopProxy::current().get()), | 48 : loop_proxy_(base::MessageLoopProxy::current().get()), |
| 50 context_(true), | 49 context_(true), |
| 51 unavailable_(false) { | 50 unavailable_(false) { |
| 52 context_.Init(); | 51 context_.Init(); |
| 53 mock_url_request_ = context_.CreateRequest(GURL(), net::IDLE, &delegate_, | 52 mock_url_request_ = context_.CreateRequest(GURL(), net::IDLE, &delegate_, |
| 54 NULL); | 53 NULL); |
| 55 } | 54 } |
| 56 | 55 |
| 57 void NotifyUnavailable(bool unavailable) { | 56 void NotifyUnavailable(bool unavailable) { |
| 58 unavailable_ = unavailable; | 57 unavailable_ = unavailable; |
| 59 } | 58 } |
| 60 | 59 |
| 61 // Required for MessageLoopProxy::current(). | 60 // Required for base::MessageLoopProxy::current(). |
| 62 base::MessageLoopForUI loop_; | 61 base::MessageLoopForUI loop_; |
| 63 MessageLoopProxy* loop_proxy_; | 62 base::MessageLoopProxy* loop_proxy_; |
| 64 | 63 |
| 65 protected: | 64 protected: |
| 66 TestURLRequestContext context_; | 65 net::TestURLRequestContext context_; |
| 67 TestDelegate delegate_; | 66 net::TestDelegate delegate_; |
| 68 DataReductionProxyParamsMock mock_params_; | 67 DataReductionProxyParamsMock mock_params_; |
| 69 scoped_ptr<URLRequest> mock_url_request_; | 68 scoped_ptr<net::URLRequest> mock_url_request_; |
| 70 bool unavailable_; | 69 bool unavailable_; |
| 71 }; | 70 }; |
| 72 | 71 |
| 73 TEST_F(DataReductionProxyUsageStatsTest, IsDataReductionProxyUnreachable) { | 72 TEST_F(DataReductionProxyUsageStatsTest, IsDataReductionProxyUnreachable) { |
| 73 net::ProxyServer fallback_proxy_server = |
| 74 net::ProxyServer::FromURI("foo.com", net::ProxyServer::SCHEME_HTTP); |
| 75 data_reduction_proxy::DataReductionProxyTypeInfo proxy_info; |
| 74 struct TestCase { | 76 struct TestCase { |
| 75 bool is_proxy_eligible; | 77 bool fallback_proxy_server_is_data_reduction_proxy; |
| 76 bool was_proxy_used; | 78 bool was_proxy_used; |
| 77 bool is_unreachable; | 79 bool is_unreachable; |
| 78 }; | 80 }; |
| 79 const TestCase test_cases[] = { | 81 const TestCase test_cases[] = { |
| 80 { | 82 { |
| 81 false, | 83 false, |
| 82 false, | 84 false, |
| 83 false | 85 false |
| 84 }, | 86 }, |
| 85 { | 87 { |
| 88 false, |
| 89 true, |
| 90 false |
| 91 }, |
| 92 { |
| 86 true, | 93 true, |
| 87 true, | 94 true, |
| 88 false | 95 false |
| 89 }, | 96 }, |
| 90 { | 97 { |
| 91 true, | 98 true, |
| 92 false, | 99 false, |
| 93 true | 100 true |
| 94 } | 101 } |
| 95 }; | 102 }; |
| 96 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 103 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
| 97 TestCase test_case = test_cases[i]; | 104 TestCase test_case = test_cases[i]; |
| 98 | 105 |
| 99 EXPECT_CALL(mock_params_, | 106 EXPECT_CALL(mock_params_, IsDataReductionProxy(testing::_, testing::_)) |
| 100 IsDataReductionProxyEligible(mock_url_request_.get())) | 107 .WillRepeatedly(testing::Return( |
| 101 .WillRepeatedly(Return(test_case.is_proxy_eligible)); | 108 test_case.fallback_proxy_server_is_data_reduction_proxy)); |
| 102 EXPECT_CALL(mock_params_, | 109 EXPECT_CALL(mock_params_, |
| 103 WasDataReductionProxyUsed(mock_url_request_.get(), NULL)) | 110 WasDataReductionProxyUsed(mock_url_request_.get(), NULL)) |
| 104 .WillRepeatedly(Return(test_case.was_proxy_used)); | 111 .WillRepeatedly(testing::Return(test_case.was_proxy_used)); |
| 105 | 112 |
| 106 scoped_ptr<DataReductionProxyUsageStats> usage_stats( | 113 scoped_ptr<DataReductionProxyUsageStats> usage_stats( |
| 107 new DataReductionProxyUsageStats( | 114 new DataReductionProxyUsageStats( |
| 108 &mock_params_, loop_proxy_)); | 115 &mock_params_, loop_proxy_)); |
| 109 usage_stats->set_unavailable_callback( | 116 usage_stats->set_unavailable_callback( |
| 110 base::Bind(&DataReductionProxyUsageStatsTest::NotifyUnavailable, | 117 base::Bind(&DataReductionProxyUsageStatsTest::NotifyUnavailable, |
| 111 base::Unretained(this))); | 118 base::Unretained(this))); |
| 112 | 119 |
| 120 usage_stats->OnProxyFallback(fallback_proxy_server, |
| 121 net::ERR_PROXY_CONNECTION_FAILED); |
| 113 usage_stats->OnUrlRequestCompleted(mock_url_request_.get(), false); | 122 usage_stats->OnUrlRequestCompleted(mock_url_request_.get(), false); |
| 114 MessageLoop::current()->RunUntilIdle(); | 123 base::MessageLoop::current()->RunUntilIdle(); |
| 115 | 124 |
| 116 EXPECT_EQ(test_case.is_unreachable, unavailable_); | 125 EXPECT_EQ(test_case.is_unreachable, unavailable_); |
| 117 } | 126 } |
| 118 } | 127 } |
| 119 | 128 |
| 129 TEST_F(DataReductionProxyUsageStatsTest, ProxyUnreachableThenReachable) { |
| 130 net::ProxyServer fallback_proxy_server = |
| 131 net::ProxyServer::FromURI("foo.com", net::ProxyServer::SCHEME_HTTP); |
| 132 scoped_ptr<DataReductionProxyUsageStats> usage_stats( |
| 133 new DataReductionProxyUsageStats( |
| 134 &mock_params_, loop_proxy_)); |
| 135 usage_stats->set_unavailable_callback( |
| 136 base::Bind(&DataReductionProxyUsageStatsTest::NotifyUnavailable, |
| 137 base::Unretained(this))); |
| 138 |
| 139 EXPECT_CALL(mock_params_, IsDataReductionProxy(testing::_, testing::_)) |
| 140 .WillOnce(testing::Return(true)); |
| 141 EXPECT_CALL(mock_params_, |
| 142 WasDataReductionProxyUsed(mock_url_request_.get(), NULL)) |
| 143 .WillOnce(testing::Return(true)); |
| 144 |
| 145 // proxy falls back |
| 146 usage_stats->OnProxyFallback(fallback_proxy_server, |
| 147 net::ERR_PROXY_CONNECTION_FAILED); |
| 148 base::MessageLoop::current()->RunUntilIdle(); |
| 149 EXPECT_TRUE(unavailable_); |
| 150 |
| 151 // proxy succeeds |
| 152 usage_stats->OnUrlRequestCompleted(mock_url_request_.get(), false); |
| 153 base::MessageLoop::current()->RunUntilIdle(); |
| 154 EXPECT_FALSE(unavailable_); |
| 155 } |
| 156 |
| 157 TEST_F(DataReductionProxyUsageStatsTest, ProxyReachableThenUnreachable) { |
| 158 net::ProxyServer fallback_proxy_server = |
| 159 net::ProxyServer::FromURI("foo.com", net::ProxyServer::SCHEME_HTTP); |
| 160 scoped_ptr<DataReductionProxyUsageStats> usage_stats( |
| 161 new DataReductionProxyUsageStats( |
| 162 &mock_params_, loop_proxy_)); |
| 163 usage_stats->set_unavailable_callback( |
| 164 base::Bind(&DataReductionProxyUsageStatsTest::NotifyUnavailable, |
| 165 base::Unretained(this))); |
| 166 EXPECT_CALL(mock_params_, |
| 167 WasDataReductionProxyUsed(mock_url_request_.get(), NULL)) |
| 168 .WillOnce(testing::Return(true)); |
| 169 EXPECT_CALL(mock_params_, IsDataReductionProxy(testing::_, testing::_)) |
| 170 .WillRepeatedly(testing::Return(true)); |
| 171 |
| 172 // Proxy succeeds. |
| 173 usage_stats->OnUrlRequestCompleted(mock_url_request_.get(), false); |
| 174 base::MessageLoop::current()->RunUntilIdle(); |
| 175 EXPECT_FALSE(unavailable_); |
| 176 |
| 177 // Then proxy falls back indefinitely. |
| 178 usage_stats->OnProxyFallback(fallback_proxy_server, |
| 179 net::ERR_PROXY_CONNECTION_FAILED); |
| 180 usage_stats->OnProxyFallback(fallback_proxy_server, |
| 181 net::ERR_PROXY_CONNECTION_FAILED); |
| 182 usage_stats->OnProxyFallback(fallback_proxy_server, |
| 183 net::ERR_PROXY_CONNECTION_FAILED); |
| 184 usage_stats->OnProxyFallback(fallback_proxy_server, |
| 185 net::ERR_PROXY_CONNECTION_FAILED); |
| 186 base::MessageLoop::current()->RunUntilIdle(); |
| 187 EXPECT_TRUE(unavailable_); |
| 188 } |
| 189 |
| 120 } // namespace data_reduction_proxy | 190 } // namespace data_reduction_proxy |
| OLD | NEW |