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
3 // found in the LICENSE file.
5 #include "components/gcm_driver/gcm_channel_status_request.h"
6 #include "net/url_request/test_url_fetcher_factory.h"
7 #include "net/url_request/url_request_test_util.h"
8 #include "sync/protocol/experiment_status.pb.h"
9 #include "sync/protocol/experiments_specifics.pb.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 class GCMChannelStatusRequestTest
: public testing::Test
{
16 GCMChannelStatusRequestTest();
17 ~GCMChannelStatusRequestTest() override
;
27 void SetResponseStatusAndString(net::HttpStatusCode status_code
,
28 const std::string
& response_body
);
29 void SetResponseProtoData(GCMStatus status
, int poll_interval_seconds
);
31 void OnRequestCompleted(bool update_received
,
33 int poll_interval_seconds
);
35 scoped_ptr
<GCMChannelStatusRequest
> request_
;
36 base::MessageLoop message_loop_
;
37 net::TestURLFetcherFactory url_fetcher_factory_
;
38 scoped_refptr
<net::TestURLRequestContextGetter
> url_request_context_getter_
;
39 bool request_callback_invoked_
;
40 bool update_received_
;
42 int poll_interval_seconds_
;
45 GCMChannelStatusRequestTest::GCMChannelStatusRequestTest()
46 : url_request_context_getter_(
47 new net::TestURLRequestContextGetter(message_loop_
.task_runner())),
48 request_callback_invoked_(false),
49 update_received_(false),
51 poll_interval_seconds_(0) {
54 GCMChannelStatusRequestTest::~GCMChannelStatusRequestTest() {
57 void GCMChannelStatusRequestTest::StartRequest() {
58 request_
.reset(new GCMChannelStatusRequest(
59 url_request_context_getter_
.get(),
60 "http://channel.status.request.com/",
62 base::Bind(&GCMChannelStatusRequestTest::OnRequestCompleted
,
63 base::Unretained(this))));
67 void GCMChannelStatusRequestTest::SetResponseStatusAndString(
68 net::HttpStatusCode status_code
,
69 const std::string
& response_body
) {
70 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
72 fetcher
->set_response_code(status_code
);
73 fetcher
->SetResponseString(response_body
);
76 void GCMChannelStatusRequestTest::SetResponseProtoData(
77 GCMStatus status
, int poll_interval_seconds
) {
78 sync_pb::ExperimentStatusResponse response_proto
;
79 if (status
!= NOT_SPECIFIED
) {
80 sync_pb::ExperimentsSpecifics
* experiment_specifics
=
81 response_proto
.add_experiment();
82 experiment_specifics
->mutable_gcm_channel()->set_enabled(status
==
86 // Zero |poll_interval_seconds| means the optional field is not set.
87 if (poll_interval_seconds
)
88 response_proto
.set_poll_interval_seconds(poll_interval_seconds
);
90 std::string response_string
;
91 response_proto
.SerializeToString(&response_string
);
92 SetResponseStatusAndString(net::HTTP_OK
, response_string
);
95 void GCMChannelStatusRequestTest::CompleteFetch() {
96 request_callback_invoked_
= false;
97 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
99 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
102 void GCMChannelStatusRequestTest::OnRequestCompleted(
103 bool update_received
, bool enabled
, int poll_interval_seconds
) {
104 request_callback_invoked_
= true;
105 update_received_
= update_received
;
107 poll_interval_seconds_
= poll_interval_seconds
;
110 TEST_F(GCMChannelStatusRequestTest
, RequestData
) {
113 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
114 ASSERT_TRUE(fetcher
);
116 EXPECT_EQ(GURL(request_
->channel_status_request_url_
),
117 fetcher
->GetOriginalURL());
119 net::HttpRequestHeaders headers
;
120 fetcher
->GetExtraRequestHeaders(&headers
);
121 std::string user_agent_header
;
122 headers
.GetHeader("User-Agent", &user_agent_header
);
123 EXPECT_FALSE(user_agent_header
.empty());
124 EXPECT_EQ(request_
->user_agent_
, user_agent_header
);
126 std::string upload_data
= fetcher
->upload_data();
127 EXPECT_FALSE(upload_data
.empty());
128 sync_pb::ExperimentStatusRequest proto_data
;
129 proto_data
.ParseFromString(upload_data
);
130 EXPECT_EQ(1, proto_data
.experiment_name_size());
131 EXPECT_EQ("gcm_channel", proto_data
.experiment_name(0));
134 TEST_F(GCMChannelStatusRequestTest
, ResponseHttpStatusNotOK
) {
136 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED
, "");
139 EXPECT_FALSE(request_callback_invoked_
);
142 TEST_F(GCMChannelStatusRequestTest
, ResponseEmpty
) {
144 SetResponseStatusAndString(net::HTTP_OK
, "");
147 EXPECT_TRUE(request_callback_invoked_
);
148 EXPECT_FALSE(update_received_
);
151 TEST_F(GCMChannelStatusRequestTest
, ResponseNotInProtoFormat
) {
153 SetResponseStatusAndString(net::HTTP_OK
, "foo");
156 EXPECT_FALSE(request_callback_invoked_
);
159 TEST_F(GCMChannelStatusRequestTest
, ResponseEmptyProtoData
) {
161 SetResponseProtoData(NOT_SPECIFIED
, 0);
164 EXPECT_TRUE(request_callback_invoked_
);
165 EXPECT_FALSE(update_received_
);
168 TEST_F(GCMChannelStatusRequestTest
, ResponseWithDisabledStatus
) {
170 SetResponseProtoData(GCM_DISABLED
, 0);
173 EXPECT_TRUE(request_callback_invoked_
);
174 EXPECT_TRUE(update_received_
);
175 EXPECT_FALSE(enabled_
);
177 GCMChannelStatusRequest::default_poll_interval_seconds(),
178 poll_interval_seconds_
);
181 TEST_F(GCMChannelStatusRequestTest
, ResponseWithEnabledStatus
) {
183 SetResponseProtoData(GCM_ENABLED
, 0);
186 EXPECT_TRUE(request_callback_invoked_
);
187 EXPECT_TRUE(update_received_
);
188 EXPECT_TRUE(enabled_
);
190 GCMChannelStatusRequest::default_poll_interval_seconds(),
191 poll_interval_seconds_
);
194 TEST_F(GCMChannelStatusRequestTest
, ResponseWithPollInterval
) {
195 // Setting a poll interval 15 minutes longer than the minimum interval we
197 int poll_interval_seconds
=
198 GCMChannelStatusRequest::min_poll_interval_seconds() + 15 * 60;
200 SetResponseProtoData(NOT_SPECIFIED
, poll_interval_seconds
);
203 EXPECT_TRUE(request_callback_invoked_
);
204 EXPECT_TRUE(update_received_
);
205 EXPECT_TRUE(enabled_
);
206 EXPECT_EQ(poll_interval_seconds
, poll_interval_seconds_
);
209 TEST_F(GCMChannelStatusRequestTest
, ResponseWithShortPollInterval
) {
210 // Setting a poll interval 15 minutes shorter than the minimum interval we
212 int poll_interval_seconds
=
213 GCMChannelStatusRequest::min_poll_interval_seconds() - 15 * 60;
215 SetResponseProtoData(NOT_SPECIFIED
, poll_interval_seconds
);
218 EXPECT_TRUE(request_callback_invoked_
);
219 EXPECT_TRUE(update_received_
);
220 EXPECT_TRUE(enabled_
);
221 EXPECT_EQ(GCMChannelStatusRequest::min_poll_interval_seconds(),
222 poll_interval_seconds_
);
225 TEST_F(GCMChannelStatusRequestTest
, ResponseWithDisabledStatusAndPollInterval
) {
226 int poll_interval_seconds
=
227 GCMChannelStatusRequest::min_poll_interval_seconds() + 15 * 60;
229 SetResponseProtoData(GCM_DISABLED
, poll_interval_seconds
);
232 EXPECT_TRUE(request_callback_invoked_
);
233 EXPECT_TRUE(update_received_
);
234 EXPECT_FALSE(enabled_
);
235 EXPECT_EQ(poll_interval_seconds
, poll_interval_seconds_
);