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 "base/test/test_simple_task_runner.h"
6 #include "base/thread_task_runner_handle.h"
7 #include "components/gcm_driver/gcm_channel_status_request.h"
8 #include "net/url_request/test_url_fetcher_factory.h"
9 #include "net/url_request/url_request_test_util.h"
10 #include "sync/protocol/experiment_status.pb.h"
11 #include "sync/protocol/experiments_specifics.pb.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 class GCMChannelStatusRequestTest
: public testing::Test
{
18 GCMChannelStatusRequestTest();
19 ~GCMChannelStatusRequestTest() override
;
29 void SetResponseStatusAndString(net::HttpStatusCode status_code
,
30 const std::string
& response_body
);
31 void SetResponseProtoData(GCMStatus status
, int poll_interval_seconds
);
33 void OnRequestCompleted(bool update_received
,
35 int poll_interval_seconds
);
37 scoped_ptr
<GCMChannelStatusRequest
> request_
;
38 scoped_refptr
<base::TestSimpleTaskRunner
> task_runner_
;
39 base::ThreadTaskRunnerHandle task_runner_handle_
;
40 net::TestURLFetcherFactory url_fetcher_factory_
;
41 scoped_refptr
<net::TestURLRequestContextGetter
> url_request_context_getter_
;
42 bool request_callback_invoked_
;
43 bool update_received_
;
45 int poll_interval_seconds_
;
48 GCMChannelStatusRequestTest::GCMChannelStatusRequestTest()
49 : task_runner_(new base::TestSimpleTaskRunner()),
50 task_runner_handle_(task_runner_
),
51 url_request_context_getter_(
52 new net::TestURLRequestContextGetter(task_runner_
)),
53 request_callback_invoked_(false),
54 update_received_(false),
56 poll_interval_seconds_(0) {
59 GCMChannelStatusRequestTest::~GCMChannelStatusRequestTest() {
62 void GCMChannelStatusRequestTest::StartRequest() {
63 request_
.reset(new GCMChannelStatusRequest(
64 url_request_context_getter_
.get(),
65 "http://channel.status.request.com/",
67 base::Bind(&GCMChannelStatusRequestTest::OnRequestCompleted
,
68 base::Unretained(this))));
72 void GCMChannelStatusRequestTest::SetResponseStatusAndString(
73 net::HttpStatusCode status_code
,
74 const std::string
& response_body
) {
75 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
77 fetcher
->set_response_code(status_code
);
78 fetcher
->SetResponseString(response_body
);
81 void GCMChannelStatusRequestTest::SetResponseProtoData(
82 GCMStatus status
, int poll_interval_seconds
) {
83 sync_pb::ExperimentStatusResponse response_proto
;
84 if (status
!= NOT_SPECIFIED
) {
85 sync_pb::ExperimentsSpecifics
* experiment_specifics
=
86 response_proto
.add_experiment();
87 experiment_specifics
->mutable_gcm_channel()->set_enabled(status
==
91 // Zero |poll_interval_seconds| means the optional field is not set.
92 if (poll_interval_seconds
)
93 response_proto
.set_poll_interval_seconds(poll_interval_seconds
);
95 std::string response_string
;
96 response_proto
.SerializeToString(&response_string
);
97 SetResponseStatusAndString(net::HTTP_OK
, response_string
);
100 void GCMChannelStatusRequestTest::CompleteFetch() {
101 request_callback_invoked_
= false;
102 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
103 ASSERT_TRUE(fetcher
);
104 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
107 void GCMChannelStatusRequestTest::OnRequestCompleted(
108 bool update_received
, bool enabled
, int poll_interval_seconds
) {
109 request_callback_invoked_
= true;
110 update_received_
= update_received
;
112 poll_interval_seconds_
= poll_interval_seconds
;
115 TEST_F(GCMChannelStatusRequestTest
, RequestData
) {
118 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
119 ASSERT_TRUE(fetcher
);
121 EXPECT_EQ(GURL(request_
->channel_status_request_url_
),
122 fetcher
->GetOriginalURL());
124 net::HttpRequestHeaders headers
;
125 fetcher
->GetExtraRequestHeaders(&headers
);
126 std::string user_agent_header
;
127 headers
.GetHeader("User-Agent", &user_agent_header
);
128 EXPECT_FALSE(user_agent_header
.empty());
129 EXPECT_EQ(request_
->user_agent_
, user_agent_header
);
131 std::string upload_data
= fetcher
->upload_data();
132 EXPECT_FALSE(upload_data
.empty());
133 sync_pb::ExperimentStatusRequest proto_data
;
134 proto_data
.ParseFromString(upload_data
);
135 EXPECT_EQ(1, proto_data
.experiment_name_size());
136 EXPECT_EQ("gcm_channel", proto_data
.experiment_name(0));
139 TEST_F(GCMChannelStatusRequestTest
, ResponseHttpStatusNotOK
) {
141 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED
, "");
144 EXPECT_FALSE(request_callback_invoked_
);
147 TEST_F(GCMChannelStatusRequestTest
, ResponseEmpty
) {
149 SetResponseStatusAndString(net::HTTP_OK
, "");
152 EXPECT_TRUE(request_callback_invoked_
);
153 EXPECT_FALSE(update_received_
);
156 TEST_F(GCMChannelStatusRequestTest
, ResponseNotInProtoFormat
) {
158 SetResponseStatusAndString(net::HTTP_OK
, "foo");
161 EXPECT_FALSE(request_callback_invoked_
);
164 TEST_F(GCMChannelStatusRequestTest
, ResponseEmptyProtoData
) {
166 SetResponseProtoData(NOT_SPECIFIED
, 0);
169 EXPECT_TRUE(request_callback_invoked_
);
170 EXPECT_FALSE(update_received_
);
173 TEST_F(GCMChannelStatusRequestTest
, ResponseWithDisabledStatus
) {
175 SetResponseProtoData(GCM_DISABLED
, 0);
178 EXPECT_TRUE(request_callback_invoked_
);
179 EXPECT_TRUE(update_received_
);
180 EXPECT_FALSE(enabled_
);
182 GCMChannelStatusRequest::default_poll_interval_seconds(),
183 poll_interval_seconds_
);
186 TEST_F(GCMChannelStatusRequestTest
, ResponseWithEnabledStatus
) {
188 SetResponseProtoData(GCM_ENABLED
, 0);
191 EXPECT_TRUE(request_callback_invoked_
);
192 EXPECT_TRUE(update_received_
);
193 EXPECT_TRUE(enabled_
);
195 GCMChannelStatusRequest::default_poll_interval_seconds(),
196 poll_interval_seconds_
);
199 TEST_F(GCMChannelStatusRequestTest
, ResponseWithPollInterval
) {
200 // Setting a poll interval 15 minutes longer than the minimum interval we
202 int poll_interval_seconds
=
203 GCMChannelStatusRequest::min_poll_interval_seconds() + 15 * 60;
205 SetResponseProtoData(NOT_SPECIFIED
, poll_interval_seconds
);
208 EXPECT_TRUE(request_callback_invoked_
);
209 EXPECT_TRUE(update_received_
);
210 EXPECT_TRUE(enabled_
);
211 EXPECT_EQ(poll_interval_seconds
, poll_interval_seconds_
);
214 TEST_F(GCMChannelStatusRequestTest
, ResponseWithShortPollInterval
) {
215 // Setting a poll interval 15 minutes shorter than the minimum interval we
217 int poll_interval_seconds
=
218 GCMChannelStatusRequest::min_poll_interval_seconds() - 15 * 60;
220 SetResponseProtoData(NOT_SPECIFIED
, poll_interval_seconds
);
223 EXPECT_TRUE(request_callback_invoked_
);
224 EXPECT_TRUE(update_received_
);
225 EXPECT_TRUE(enabled_
);
226 EXPECT_EQ(GCMChannelStatusRequest::min_poll_interval_seconds(),
227 poll_interval_seconds_
);
230 TEST_F(GCMChannelStatusRequestTest
, ResponseWithDisabledStatusAndPollInterval
) {
231 int poll_interval_seconds
=
232 GCMChannelStatusRequest::min_poll_interval_seconds() + 15 * 60;
234 SetResponseProtoData(GCM_DISABLED
, poll_interval_seconds
);
237 EXPECT_TRUE(request_callback_invoked_
);
238 EXPECT_TRUE(update_received_
);
239 EXPECT_FALSE(enabled_
);
240 EXPECT_EQ(poll_interval_seconds
, poll_interval_seconds_
);