| 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 |
| 45 |
| 46 class DataReductionProxyUsageStatsTest : public testing::Test { | 46 class DataReductionProxyUsageStatsTest : public testing::Test { |
| 47 public: | 47 public: |
| 48 DataReductionProxyUsageStatsTest() | 48 DataReductionProxyUsageStatsTest() |
| 49 : loop_proxy_(MessageLoopProxy::current().get()), | 49 : loop_proxy_(base::MessageLoopProxy::current().get()), |
| 50 context_(true), | 50 context_(true), |
| 51 unavailable_(false) { | 51 unavailable_(false) { |
| 52 context_.Init(); | 52 context_.Init(); |
| 53 mock_url_request_ = context_.CreateRequest(GURL(), net::IDLE, &delegate_, | 53 mock_url_request_ = context_.CreateRequest(GURL(), net::IDLE, &delegate_, |
| 54 NULL); | 54 NULL); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void NotifyUnavailable(bool unavailable) { | 57 void NotifyUnavailable(bool unavailable) { |
| 58 unavailable_ = unavailable; | 58 unavailable_ = unavailable; |
| 59 } | 59 } |
| 60 | 60 |
| 61 // Required for MessageLoopProxy::current(). | 61 // Required for base::MessageLoopProxy::current(). |
| 62 base::MessageLoopForUI loop_; | 62 base::MessageLoopForUI loop_; |
| 63 MessageLoopProxy* loop_proxy_; | 63 base::MessageLoopProxy* loop_proxy_; |
| 64 | 64 |
| 65 protected: | 65 protected: |
| 66 TestURLRequestContext context_; | 66 net::TestURLRequestContext context_; |
| 67 TestDelegate delegate_; | 67 net::TestDelegate delegate_; |
| 68 DataReductionProxyParamsMock mock_params_; | 68 DataReductionProxyParamsMock mock_params_; |
| 69 scoped_ptr<URLRequest> mock_url_request_; | 69 scoped_ptr<net::URLRequest> mock_url_request_; |
| 70 bool unavailable_; | 70 bool unavailable_; |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 TEST_F(DataReductionProxyUsageStatsTest, IsDataReductionProxyUnreachable) { | 73 TEST_F(DataReductionProxyUsageStatsTest, IsDataReductionProxyUnreachable) { |
| 74 net::ProxyServer fallback_proxy_server = |
| 75 net::ProxyServer::FromURI("foo.com", net::ProxyServer::SCHEME_HTTP); |
| 76 data_reduction_proxy::DataReductionProxyTypeInfo proxy_info; |
| 74 struct TestCase { | 77 struct TestCase { |
| 75 bool is_proxy_eligible; | 78 bool fallback_proxy_server_is_data_reduction_proxy; |
| 76 bool was_proxy_used; | 79 bool was_proxy_used; |
| 77 bool is_unreachable; | 80 bool is_unreachable; |
| 78 }; | 81 }; |
| 79 const TestCase test_cases[] = { | 82 const TestCase test_cases[] = { |
| 80 { | 83 { |
| 81 false, | 84 false, |
| 82 false, | 85 false, |
| 83 false | 86 false |
| 84 }, | 87 }, |
| 85 { | 88 { |
| 89 false, |
| 90 true, |
| 91 false |
| 92 }, |
| 93 { |
| 86 true, | 94 true, |
| 87 true, | 95 true, |
| 88 false | 96 false |
| 89 }, | 97 }, |
| 90 { | 98 { |
| 91 true, | 99 true, |
| 92 false, | 100 false, |
| 93 true | 101 true |
| 94 } | 102 } |
| 95 }; | 103 }; |
| 96 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 104 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
| 97 TestCase test_case = test_cases[i]; | 105 TestCase test_case = test_cases[i]; |
| 98 | 106 |
| 99 EXPECT_CALL(mock_params_, | 107 EXPECT_CALL(mock_params_, IsDataReductionProxy(testing::_, testing::_)) |
| 100 IsDataReductionProxyEligible(mock_url_request_.get())) | 108 .WillRepeatedly(testing::Return( |
| 101 .WillRepeatedly(Return(test_case.is_proxy_eligible)); | 109 test_case.fallback_proxy_server_is_data_reduction_proxy)); |
| 102 EXPECT_CALL(mock_params_, | 110 EXPECT_CALL(mock_params_, |
| 103 WasDataReductionProxyUsed(mock_url_request_.get(), NULL)) | 111 WasDataReductionProxyUsed(mock_url_request_.get(), NULL)) |
| 104 .WillRepeatedly(Return(test_case.was_proxy_used)); | 112 .WillRepeatedly(testing::Return(test_case.was_proxy_used)); |
| 105 | 113 |
| 106 scoped_ptr<DataReductionProxyUsageStats> usage_stats( | 114 scoped_ptr<DataReductionProxyUsageStats> usage_stats( |
| 107 new DataReductionProxyUsageStats( | 115 new DataReductionProxyUsageStats( |
| 108 &mock_params_, loop_proxy_)); | 116 &mock_params_, loop_proxy_)); |
| 109 usage_stats->set_unavailable_callback( | 117 usage_stats->set_unavailable_callback( |
| 110 base::Bind(&DataReductionProxyUsageStatsTest::NotifyUnavailable, | 118 base::Bind(&DataReductionProxyUsageStatsTest::NotifyUnavailable, |
| 111 base::Unretained(this))); | 119 base::Unretained(this))); |
| 112 | 120 |
| 121 usage_stats->OnProxyFallback(fallback_proxy_server, |
| 122 net::ERR_PROXY_CONNECTION_FAILED); |
| 113 usage_stats->OnUrlRequestCompleted(mock_url_request_.get(), false); | 123 usage_stats->OnUrlRequestCompleted(mock_url_request_.get(), false); |
| 114 MessageLoop::current()->RunUntilIdle(); | 124 base::MessageLoop::current()->RunUntilIdle(); |
| 115 | 125 |
| 116 EXPECT_EQ(test_case.is_unreachable, unavailable_); | 126 EXPECT_EQ(test_case.is_unreachable, unavailable_); |
| 117 } | 127 } |
| 118 } | 128 } |
| 119 | 129 |
| 120 } // namespace data_reduction_proxy | 130 } // namespace data_reduction_proxy |
| OLD | NEW |