Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats_unittest.cc

Issue 568893002: Trigger data reduction proxy unreachable message via on proxy fall back. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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::MessageLoop;
17 using base::MessageLoopProxy; 18 using base::MessageLoopProxy;
18 using data_reduction_proxy::DataReductionProxyParams; 19 using data_reduction_proxy::DataReductionProxyParams;
20 using data_reduction_proxy::DataReductionProxyTypeInfo;
21 using net::HostPortPair;
bengr 2014/09/16 18:48:57 If you have this line here, then you don't need to
Not at Google. Contact bengr 2014/09/16 19:34:49 Done.
22 using net::ProxyServer;
19 using net::TestDelegate; 23 using net::TestDelegate;
20 using net::TestURLRequestContext; 24 using net::TestURLRequestContext;
21 using net::URLRequest; 25 using net::URLRequest;
22 using net::URLRequestStatus; 26 using net::URLRequestStatus;
27 using testing::_;
23 using testing::Return; 28 using testing::Return;
24 29
25 namespace { 30 namespace {
26 31
27 class DataReductionProxyParamsMock : public DataReductionProxyParams { 32 class DataReductionProxyParamsMock : public DataReductionProxyParams {
28 public: 33 public:
29 DataReductionProxyParamsMock() : DataReductionProxyParams(0) {} 34 DataReductionProxyParamsMock() : DataReductionProxyParams(0) {}
30 virtual ~DataReductionProxyParamsMock() {} 35 virtual ~DataReductionProxyParamsMock() {}
31 36
32 MOCK_METHOD1(IsDataReductionProxyEligible, bool(const net::URLRequest*)); 37 MOCK_CONST_METHOD2(
38 IsDataReductionProxy,
39 bool(const net::HostPortPair& host_port_pair,
40 data_reduction_proxy::DataReductionProxyTypeInfo* proxy_info));
33 MOCK_CONST_METHOD2( 41 MOCK_CONST_METHOD2(
34 WasDataReductionProxyUsed, 42 WasDataReductionProxyUsed,
35 bool(const net::URLRequest*, 43 bool(const net::URLRequest*,
36 data_reduction_proxy::DataReductionProxyTypeInfo* proxy_servers)); 44 data_reduction_proxy::DataReductionProxyTypeInfo* proxy_info));
37 45
38 private: 46 private:
39 DISALLOW_COPY_AND_ASSIGN(DataReductionProxyParamsMock); 47 DISALLOW_COPY_AND_ASSIGN(DataReductionProxyParamsMock);
40 }; 48 };
41 49
42 } // namespace 50 } // namespace
43 51
44 namespace data_reduction_proxy { 52 namespace data_reduction_proxy {
45 53
54
46 class DataReductionProxyUsageStatsTest : public testing::Test { 55 class DataReductionProxyUsageStatsTest : public testing::Test {
47 public: 56 public:
48 DataReductionProxyUsageStatsTest() 57 DataReductionProxyUsageStatsTest()
49 : loop_proxy_(MessageLoopProxy::current().get()), 58 : loop_proxy_(MessageLoopProxy::current().get()),
50 context_(true), 59 context_(true),
51 unavailable_(false) { 60 unavailable_(false) {
52 context_.Init(); 61 context_.Init();
53 mock_url_request_ = context_.CreateRequest(GURL(), net::IDLE, &delegate_, 62 mock_url_request_ = context_.CreateRequest(GURL(), net::IDLE, &delegate_,
54 NULL); 63 NULL);
55 } 64 }
56 65
57 void NotifyUnavailable(bool unavailable) { 66 void NotifyUnavailable(bool unavailable) {
58 unavailable_ = unavailable; 67 unavailable_ = unavailable;
59 } 68 }
60 69
61 // Required for MessageLoopProxy::current(). 70 // Required for MessageLoopProxy::current().
62 base::MessageLoopForUI loop_; 71 base::MessageLoopForUI loop_;
63 MessageLoopProxy* loop_proxy_; 72 MessageLoopProxy* loop_proxy_;
64 73
65 protected: 74 protected:
66 TestURLRequestContext context_; 75 TestURLRequestContext context_;
67 TestDelegate delegate_; 76 TestDelegate delegate_;
68 DataReductionProxyParamsMock mock_params_; 77 DataReductionProxyParamsMock mock_params_;
69 scoped_ptr<URLRequest> mock_url_request_; 78 scoped_ptr<URLRequest> mock_url_request_;
70 bool unavailable_; 79 bool unavailable_;
71 }; 80 };
72 81
73 TEST_F(DataReductionProxyUsageStatsTest, IsDataReductionProxyUnreachable) { 82 TEST_F(DataReductionProxyUsageStatsTest, IsDataReductionProxyUnreachable) {
83 ProxyServer fallback_proxy_server =
84 ProxyServer::FromURI("foo.com", ProxyServer::SCHEME_HTTP);
85 DataReductionProxyTypeInfo proxy_info;
74 struct TestCase { 86 struct TestCase {
75 bool is_proxy_eligible; 87 bool fallback_proxy_server_is_data_reduction_proxy;
76 bool was_proxy_used; 88 bool was_proxy_used;
77 bool is_unreachable; 89 bool is_unreachable;
78 }; 90 };
79 const TestCase test_cases[] = { 91 const TestCase test_cases[] = {
80 { 92 {
81 false, 93 false,
82 false, 94 false,
83 false 95 false
84 }, 96 },
85 { 97 {
98 false,
99 true,
100 false
101 },
102 {
86 true, 103 true,
87 true, 104 true,
88 false 105 false
89 }, 106 },
90 { 107 {
91 true, 108 true,
92 false, 109 false,
93 true 110 true
94 } 111 }
95 }; 112 };
96 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 113 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
97 TestCase test_case = test_cases[i]; 114 TestCase test_case = test_cases[i];
98 115
99 EXPECT_CALL(mock_params_, 116 EXPECT_CALL(mock_params_, IsDataReductionProxy(_, _))
100 IsDataReductionProxyEligible(mock_url_request_.get())) 117 .WillRepeatedly(
101 .WillRepeatedly(Return(test_case.is_proxy_eligible)); 118 Return(test_case.fallback_proxy_server_is_data_reduction_proxy));
102 EXPECT_CALL(mock_params_, 119 EXPECT_CALL(mock_params_,
103 WasDataReductionProxyUsed(mock_url_request_.get(), NULL)) 120 WasDataReductionProxyUsed(mock_url_request_.get(), NULL))
104 .WillRepeatedly(Return(test_case.was_proxy_used)); 121 .WillRepeatedly(Return(test_case.was_proxy_used));
105 122
106 scoped_ptr<DataReductionProxyUsageStats> usage_stats( 123 scoped_ptr<DataReductionProxyUsageStats> usage_stats(
107 new DataReductionProxyUsageStats( 124 new DataReductionProxyUsageStats(
108 &mock_params_, loop_proxy_)); 125 &mock_params_, loop_proxy_));
109 usage_stats->set_unavailable_callback( 126 usage_stats->set_unavailable_callback(
110 base::Bind(&DataReductionProxyUsageStatsTest::NotifyUnavailable, 127 base::Bind(&DataReductionProxyUsageStatsTest::NotifyUnavailable,
111 base::Unretained(this))); 128 base::Unretained(this)));
112 129
130 usage_stats->OnProxyFallback(fallback_proxy_server,
131 net::ERR_PROXY_CONNECTION_FAILED);
113 usage_stats->OnUrlRequestCompleted(mock_url_request_.get(), false); 132 usage_stats->OnUrlRequestCompleted(mock_url_request_.get(), false);
114 MessageLoop::current()->RunUntilIdle(); 133 MessageLoop::current()->RunUntilIdle();
115 134
116 EXPECT_EQ(test_case.is_unreachable, unavailable_); 135 EXPECT_EQ(test_case.is_unreachable, unavailable_);
117 } 136 }
118 } 137 }
119 138
120 } // namespace data_reduction_proxy 139 } // namespace data_reduction_proxy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698