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 "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 #include "components/gcm_driver/gcm_channel_status_request.h" | 6 #include "components/gcm_driver/gcm_channel_status_request.h" |
7 #include "net/url_request/test_url_fetcher_factory.h" | 7 #include "net/url_request/test_url_fetcher_factory.h" |
8 #include "net/url_request/url_request_test_util.h" | 8 #include "net/url_request/url_request_test_util.h" |
9 #include "sync/protocol/experiment_status.pb.h" | 9 #include "sync/protocol/experiment_status.pb.h" |
10 #include "sync/protocol/experiments_specifics.pb.h" | 10 #include "sync/protocol/experiments_specifics.pb.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 NOT_SPECIFIED, | 22 NOT_SPECIFIED, |
23 GCM_ENABLED, | 23 GCM_ENABLED, |
24 GCM_DISABLED, | 24 GCM_DISABLED, |
25 }; | 25 }; |
26 | 26 |
27 void StartRequest(); | 27 void StartRequest(); |
28 void SetResponseStatusAndString(net::HttpStatusCode status_code, | 28 void SetResponseStatusAndString(net::HttpStatusCode status_code, |
29 const std::string& response_body); | 29 const std::string& response_body); |
30 void SetResponseProtoData(GCMStatus status, int poll_interval_seconds); | 30 void SetResponseProtoData(GCMStatus status, int poll_interval_seconds); |
31 void CompleteFetch(); | 31 void CompleteFetch(); |
32 void OnRequestCompleted(bool enabled, int poll_interval_seconds); | 32 void OnRequestCompleted(bool update_received, |
| 33 bool enabled, |
| 34 int poll_interval_seconds); |
33 | 35 |
34 scoped_ptr<GCMChannelStatusRequest> request_; | 36 scoped_ptr<GCMChannelStatusRequest> request_; |
35 base::MessageLoop message_loop_; | 37 base::MessageLoop message_loop_; |
36 net::TestURLFetcherFactory url_fetcher_factory_; | 38 net::TestURLFetcherFactory url_fetcher_factory_; |
37 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; | 39 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
38 bool request_callback_invoked_; | 40 bool request_callback_invoked_; |
| 41 bool update_received_; |
39 bool enabled_; | 42 bool enabled_; |
40 int poll_interval_seconds_; | 43 int poll_interval_seconds_; |
41 }; | 44 }; |
42 | 45 |
43 GCMChannelStatusRequestTest::GCMChannelStatusRequestTest() | 46 GCMChannelStatusRequestTest::GCMChannelStatusRequestTest() |
44 : url_request_context_getter_(new net::TestURLRequestContextGetter( | 47 : url_request_context_getter_(new net::TestURLRequestContextGetter( |
45 message_loop_.message_loop_proxy())), | 48 message_loop_.message_loop_proxy())), |
46 request_callback_invoked_(false), | 49 request_callback_invoked_(false), |
| 50 update_received_(false), |
47 enabled_(true), | 51 enabled_(true), |
48 poll_interval_seconds_(0) { | 52 poll_interval_seconds_(0) { |
49 } | 53 } |
50 | 54 |
51 GCMChannelStatusRequestTest::~GCMChannelStatusRequestTest() { | 55 GCMChannelStatusRequestTest::~GCMChannelStatusRequestTest() { |
52 } | 56 } |
53 | 57 |
54 void GCMChannelStatusRequestTest::StartRequest() { | 58 void GCMChannelStatusRequestTest::StartRequest() { |
55 request_.reset(new GCMChannelStatusRequest( | 59 request_.reset(new GCMChannelStatusRequest( |
56 url_request_context_getter_.get(), | 60 url_request_context_getter_.get(), |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 } | 94 } |
91 | 95 |
92 void GCMChannelStatusRequestTest::CompleteFetch() { | 96 void GCMChannelStatusRequestTest::CompleteFetch() { |
93 request_callback_invoked_ = false; | 97 request_callback_invoked_ = false; |
94 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 98 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
95 ASSERT_TRUE(fetcher); | 99 ASSERT_TRUE(fetcher); |
96 fetcher->delegate()->OnURLFetchComplete(fetcher); | 100 fetcher->delegate()->OnURLFetchComplete(fetcher); |
97 } | 101 } |
98 | 102 |
99 void GCMChannelStatusRequestTest::OnRequestCompleted( | 103 void GCMChannelStatusRequestTest::OnRequestCompleted( |
100 bool enabled, int poll_interval_seconds) { | 104 bool update_received, bool enabled, int poll_interval_seconds) { |
101 request_callback_invoked_ = true; | 105 request_callback_invoked_ = true; |
| 106 update_received_ = update_received; |
102 enabled_ = enabled; | 107 enabled_ = enabled; |
103 poll_interval_seconds_ = poll_interval_seconds; | 108 poll_interval_seconds_ = poll_interval_seconds; |
104 } | 109 } |
105 | 110 |
106 TEST_F(GCMChannelStatusRequestTest, ResponseHttpStatusNotOK) { | 111 TEST_F(GCMChannelStatusRequestTest, ResponseHttpStatusNotOK) { |
107 StartRequest(); | 112 StartRequest(); |
108 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, ""); | 113 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, ""); |
109 CompleteFetch(); | 114 CompleteFetch(); |
110 | 115 |
111 EXPECT_FALSE(request_callback_invoked_); | 116 EXPECT_FALSE(request_callback_invoked_); |
112 } | 117 } |
113 | 118 |
114 TEST_F(GCMChannelStatusRequestTest, ResponseEmpty) { | 119 TEST_F(GCMChannelStatusRequestTest, ResponseEmpty) { |
115 StartRequest(); | 120 StartRequest(); |
116 SetResponseStatusAndString(net::HTTP_OK, ""); | 121 SetResponseStatusAndString(net::HTTP_OK, ""); |
117 CompleteFetch(); | 122 CompleteFetch(); |
118 | 123 |
119 EXPECT_FALSE(request_callback_invoked_); | 124 EXPECT_TRUE(request_callback_invoked_); |
| 125 EXPECT_FALSE(update_received_); |
120 } | 126 } |
121 | 127 |
122 TEST_F(GCMChannelStatusRequestTest, ResponseNotInProtoFormat) { | 128 TEST_F(GCMChannelStatusRequestTest, ResponseNotInProtoFormat) { |
123 StartRequest(); | 129 StartRequest(); |
124 SetResponseStatusAndString(net::HTTP_OK, "foo"); | 130 SetResponseStatusAndString(net::HTTP_OK, "foo"); |
125 CompleteFetch(); | 131 CompleteFetch(); |
126 | 132 |
127 EXPECT_FALSE(request_callback_invoked_); | 133 EXPECT_FALSE(request_callback_invoked_); |
128 } | 134 } |
129 | 135 |
130 TEST_F(GCMChannelStatusRequestTest, ResponseEmptyProtoData) { | 136 TEST_F(GCMChannelStatusRequestTest, ResponseEmptyProtoData) { |
131 StartRequest(); | 137 StartRequest(); |
132 SetResponseProtoData(NOT_SPECIFIED, 0); | 138 SetResponseProtoData(NOT_SPECIFIED, 0); |
133 CompleteFetch(); | 139 CompleteFetch(); |
134 | 140 |
135 EXPECT_FALSE(request_callback_invoked_); | 141 EXPECT_TRUE(request_callback_invoked_); |
| 142 EXPECT_FALSE(update_received_); |
136 } | 143 } |
137 | 144 |
138 TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatus) { | 145 TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatus) { |
139 StartRequest(); | 146 StartRequest(); |
140 SetResponseProtoData(GCM_DISABLED, 0); | 147 SetResponseProtoData(GCM_DISABLED, 0); |
141 CompleteFetch(); | 148 CompleteFetch(); |
142 | 149 |
143 EXPECT_TRUE(request_callback_invoked_); | 150 EXPECT_TRUE(request_callback_invoked_); |
| 151 EXPECT_TRUE(update_received_); |
144 EXPECT_FALSE(enabled_); | 152 EXPECT_FALSE(enabled_); |
145 EXPECT_EQ( | 153 EXPECT_EQ( |
146 GCMChannelStatusRequest::default_poll_interval_seconds(), | 154 GCMChannelStatusRequest::default_poll_interval_seconds(), |
147 poll_interval_seconds_); | 155 poll_interval_seconds_); |
148 } | 156 } |
149 | 157 |
150 TEST_F(GCMChannelStatusRequestTest, ResponseWithEnabledStatus) { | 158 TEST_F(GCMChannelStatusRequestTest, ResponseWithEnabledStatus) { |
151 StartRequest(); | 159 StartRequest(); |
152 SetResponseProtoData(GCM_ENABLED, 0); | 160 SetResponseProtoData(GCM_ENABLED, 0); |
153 CompleteFetch(); | 161 CompleteFetch(); |
154 | 162 |
155 EXPECT_TRUE(request_callback_invoked_); | 163 EXPECT_TRUE(request_callback_invoked_); |
| 164 EXPECT_TRUE(update_received_); |
156 EXPECT_TRUE(enabled_); | 165 EXPECT_TRUE(enabled_); |
157 EXPECT_EQ( | 166 EXPECT_EQ( |
158 GCMChannelStatusRequest::default_poll_interval_seconds(), | 167 GCMChannelStatusRequest::default_poll_interval_seconds(), |
159 poll_interval_seconds_); | 168 poll_interval_seconds_); |
160 } | 169 } |
161 | 170 |
162 TEST_F(GCMChannelStatusRequestTest, ResponseWithPollInterval) { | 171 TEST_F(GCMChannelStatusRequestTest, ResponseWithPollInterval) { |
163 // Setting a poll interval 15 minutes longer than the minimum interval we | 172 // Setting a poll interval 15 minutes longer than the minimum interval we |
164 // enforce. | 173 // enforce. |
165 int poll_interval_seconds = | 174 int poll_interval_seconds = |
166 GCMChannelStatusRequest::min_poll_interval_seconds() + 15 * 60; | 175 GCMChannelStatusRequest::min_poll_interval_seconds() + 15 * 60; |
167 StartRequest(); | 176 StartRequest(); |
168 SetResponseProtoData(NOT_SPECIFIED, poll_interval_seconds); | 177 SetResponseProtoData(NOT_SPECIFIED, poll_interval_seconds); |
169 CompleteFetch(); | 178 CompleteFetch(); |
170 | 179 |
171 EXPECT_TRUE(request_callback_invoked_); | 180 EXPECT_TRUE(request_callback_invoked_); |
| 181 EXPECT_TRUE(update_received_); |
172 EXPECT_TRUE(enabled_); | 182 EXPECT_TRUE(enabled_); |
173 EXPECT_EQ(poll_interval_seconds, poll_interval_seconds_); | 183 EXPECT_EQ(poll_interval_seconds, poll_interval_seconds_); |
174 } | 184 } |
175 | 185 |
176 TEST_F(GCMChannelStatusRequestTest, ResponseWithShortPollInterval) { | 186 TEST_F(GCMChannelStatusRequestTest, ResponseWithShortPollInterval) { |
177 // Setting a poll interval 15 minutes shorter than the minimum interval we | 187 // Setting a poll interval 15 minutes shorter than the minimum interval we |
178 // enforce. | 188 // enforce. |
179 int poll_interval_seconds = | 189 int poll_interval_seconds = |
180 GCMChannelStatusRequest::min_poll_interval_seconds() - 15 * 60; | 190 GCMChannelStatusRequest::min_poll_interval_seconds() - 15 * 60; |
181 StartRequest(); | 191 StartRequest(); |
182 SetResponseProtoData(NOT_SPECIFIED, poll_interval_seconds); | 192 SetResponseProtoData(NOT_SPECIFIED, poll_interval_seconds); |
183 CompleteFetch(); | 193 CompleteFetch(); |
184 | 194 |
185 EXPECT_TRUE(request_callback_invoked_); | 195 EXPECT_TRUE(request_callback_invoked_); |
| 196 EXPECT_TRUE(update_received_); |
186 EXPECT_TRUE(enabled_); | 197 EXPECT_TRUE(enabled_); |
187 EXPECT_EQ(GCMChannelStatusRequest::min_poll_interval_seconds(), | 198 EXPECT_EQ(GCMChannelStatusRequest::min_poll_interval_seconds(), |
188 poll_interval_seconds_); | 199 poll_interval_seconds_); |
189 } | 200 } |
190 | 201 |
191 TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatusAndPollInterval) { | 202 TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatusAndPollInterval) { |
192 int poll_interval_seconds = | 203 int poll_interval_seconds = |
193 GCMChannelStatusRequest::min_poll_interval_seconds() + 15 * 60; | 204 GCMChannelStatusRequest::min_poll_interval_seconds() + 15 * 60; |
194 StartRequest(); | 205 StartRequest(); |
195 SetResponseProtoData(GCM_DISABLED, poll_interval_seconds); | 206 SetResponseProtoData(GCM_DISABLED, poll_interval_seconds); |
196 CompleteFetch(); | 207 CompleteFetch(); |
197 | 208 |
198 EXPECT_TRUE(request_callback_invoked_); | 209 EXPECT_TRUE(request_callback_invoked_); |
| 210 EXPECT_TRUE(update_received_); |
199 EXPECT_FALSE(enabled_); | 211 EXPECT_FALSE(enabled_); |
200 EXPECT_EQ(poll_interval_seconds, poll_interval_seconds_); | 212 EXPECT_EQ(poll_interval_seconds, poll_interval_seconds_); |
201 } | 213 } |
202 | 214 |
203 } // namespace gcm | 215 } // namespace gcm |
OLD | NEW |