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/request_priority.h" | 9 #include "net/base/request_priority.h" |
10 #include "net/url_request/url_request.h" | 10 #include "net/url_request/url_request.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 | 41 |
42 } // namespace | 42 } // namespace |
43 | 43 |
44 namespace data_reduction_proxy { | 44 namespace data_reduction_proxy { |
45 | 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_(MessageLoopProxy::current().get()), |
50 context_(true), | 50 context_(true), |
51 mock_url_request_(GURL(), net::IDLE, &delegate_, &context_), | |
52 unavailable_(false) { | 51 unavailable_(false) { |
53 context_.Init(); | 52 context_.Init(); |
| 53 mock_url_request_ = context_.CreateRequest(GURL(), net::IDLE, &delegate_, |
| 54 NULL); |
54 } | 55 } |
55 | 56 |
56 void NotifyUnavailable(bool unavailable) { | 57 void NotifyUnavailable(bool unavailable) { |
57 unavailable_ = unavailable; | 58 unavailable_ = unavailable; |
58 } | 59 } |
59 | 60 |
60 // Required for MessageLoopProxy::current(). | 61 // Required for MessageLoopProxy::current(). |
61 base::MessageLoopForUI loop_; | 62 base::MessageLoopForUI loop_; |
62 MessageLoopProxy* loop_proxy_; | 63 MessageLoopProxy* loop_proxy_; |
63 | 64 |
64 protected: | 65 protected: |
65 TestURLRequestContext context_; | 66 TestURLRequestContext context_; |
66 TestDelegate delegate_; | 67 TestDelegate delegate_; |
67 DataReductionProxyParamsMock mock_params_; | 68 DataReductionProxyParamsMock mock_params_; |
68 URLRequest mock_url_request_; | 69 scoped_ptr<URLRequest> mock_url_request_; |
69 bool unavailable_; | 70 bool unavailable_; |
70 }; | 71 }; |
71 | 72 |
72 TEST_F(DataReductionProxyUsageStatsTest, IsDataReductionProxyUnreachable) { | 73 TEST_F(DataReductionProxyUsageStatsTest, IsDataReductionProxyUnreachable) { |
73 struct TestCase { | 74 struct TestCase { |
74 bool is_proxy_eligible; | 75 bool is_proxy_eligible; |
75 bool was_proxy_used; | 76 bool was_proxy_used; |
76 bool is_unreachable; | 77 bool is_unreachable; |
77 }; | 78 }; |
78 const TestCase test_cases[] = { | 79 const TestCase test_cases[] = { |
79 { | 80 { |
80 false, | 81 false, |
81 false, | 82 false, |
82 false | 83 false |
83 }, | 84 }, |
84 { | 85 { |
85 true, | 86 true, |
86 true, | 87 true, |
87 false | 88 false |
88 }, | 89 }, |
89 { | 90 { |
90 true, | 91 true, |
91 false, | 92 false, |
92 true | 93 true |
93 } | 94 } |
94 }; | 95 }; |
95 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 96 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
96 TestCase test_case = test_cases[i]; | 97 TestCase test_case = test_cases[i]; |
97 | 98 |
98 EXPECT_CALL(mock_params_, IsDataReductionProxyEligible(&mock_url_request_)) | |
99 .WillRepeatedly(Return(test_case.is_proxy_eligible)); | |
100 EXPECT_CALL(mock_params_, | 99 EXPECT_CALL(mock_params_, |
101 WasDataReductionProxyUsed(&mock_url_request_, NULL)) | 100 IsDataReductionProxyEligible(mock_url_request_.get())) |
| 101 .WillRepeatedly(Return(test_case.is_proxy_eligible)); |
| 102 EXPECT_CALL(mock_params_, |
| 103 WasDataReductionProxyUsed(mock_url_request_.get(), NULL)) |
102 .WillRepeatedly(Return(test_case.was_proxy_used)); | 104 .WillRepeatedly(Return(test_case.was_proxy_used)); |
103 | 105 |
104 scoped_ptr<DataReductionProxyUsageStats> usage_stats( | 106 scoped_ptr<DataReductionProxyUsageStats> usage_stats( |
105 new DataReductionProxyUsageStats( | 107 new DataReductionProxyUsageStats( |
106 &mock_params_, loop_proxy_)); | 108 &mock_params_, loop_proxy_)); |
107 usage_stats->set_unavailable_callback( | 109 usage_stats->set_unavailable_callback( |
108 base::Bind(&DataReductionProxyUsageStatsTest::NotifyUnavailable, | 110 base::Bind(&DataReductionProxyUsageStatsTest::NotifyUnavailable, |
109 base::Unretained(this))); | 111 base::Unretained(this))); |
110 | 112 |
111 usage_stats->OnUrlRequestCompleted(&mock_url_request_, false); | 113 usage_stats->OnUrlRequestCompleted(mock_url_request_.get(), false); |
112 MessageLoop::current()->RunUntilIdle(); | 114 MessageLoop::current()->RunUntilIdle(); |
113 | 115 |
114 EXPECT_EQ(test_case.is_unreachable, unavailable_); | 116 EXPECT_EQ(test_case.is_unreachable, unavailable_); |
115 } | 117 } |
116 } | 118 } |
117 | 119 |
118 } // namespace data_reduction_proxy | 120 } // namespace data_reduction_proxy |
OLD | NEW |