Update mojo sdk to rev c02a28868825edfa57ab77947b8cb15e741c5598
[chromium-blink-merge.git] / components / gcm_driver / gcm_channel_status_request_unittest.cc
blob3190345d58a1a641403efa8c5929f25ea4de8b7e
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"
14 namespace gcm {
16 class GCMChannelStatusRequestTest : public testing::Test {
17 public:
18 GCMChannelStatusRequestTest();
19 ~GCMChannelStatusRequestTest() override;
21 protected:
22 enum GCMStatus {
23 NOT_SPECIFIED,
24 GCM_ENABLED,
25 GCM_DISABLED,
28 void StartRequest();
29 void SetResponseStatusAndString(net::HttpStatusCode status_code,
30 const std::string& response_body);
31 void SetResponseProtoData(GCMStatus status, int poll_interval_seconds);
32 void CompleteFetch();
33 void OnRequestCompleted(bool update_received,
34 bool enabled,
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_;
44 bool enabled_;
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),
55 enabled_(true),
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/",
66 "user agent string",
67 base::Bind(&GCMChannelStatusRequestTest::OnRequestCompleted,
68 base::Unretained(this))));
69 request_->Start();
72 void GCMChannelStatusRequestTest::SetResponseStatusAndString(
73 net::HttpStatusCode status_code,
74 const std::string& response_body) {
75 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
76 ASSERT_TRUE(fetcher);
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 ==
88 GCM_ENABLED);
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;
111 enabled_ = enabled;
112 poll_interval_seconds_ = poll_interval_seconds;
115 TEST_F(GCMChannelStatusRequestTest, RequestData) {
116 StartRequest();
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) {
140 StartRequest();
141 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "");
142 CompleteFetch();
144 EXPECT_FALSE(request_callback_invoked_);
147 TEST_F(GCMChannelStatusRequestTest, ResponseEmpty) {
148 StartRequest();
149 SetResponseStatusAndString(net::HTTP_OK, "");
150 CompleteFetch();
152 EXPECT_TRUE(request_callback_invoked_);
153 EXPECT_FALSE(update_received_);
156 TEST_F(GCMChannelStatusRequestTest, ResponseNotInProtoFormat) {
157 StartRequest();
158 SetResponseStatusAndString(net::HTTP_OK, "foo");
159 CompleteFetch();
161 EXPECT_FALSE(request_callback_invoked_);
164 TEST_F(GCMChannelStatusRequestTest, ResponseEmptyProtoData) {
165 StartRequest();
166 SetResponseProtoData(NOT_SPECIFIED, 0);
167 CompleteFetch();
169 EXPECT_TRUE(request_callback_invoked_);
170 EXPECT_FALSE(update_received_);
173 TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatus) {
174 StartRequest();
175 SetResponseProtoData(GCM_DISABLED, 0);
176 CompleteFetch();
178 EXPECT_TRUE(request_callback_invoked_);
179 EXPECT_TRUE(update_received_);
180 EXPECT_FALSE(enabled_);
181 EXPECT_EQ(
182 GCMChannelStatusRequest::default_poll_interval_seconds(),
183 poll_interval_seconds_);
186 TEST_F(GCMChannelStatusRequestTest, ResponseWithEnabledStatus) {
187 StartRequest();
188 SetResponseProtoData(GCM_ENABLED, 0);
189 CompleteFetch();
191 EXPECT_TRUE(request_callback_invoked_);
192 EXPECT_TRUE(update_received_);
193 EXPECT_TRUE(enabled_);
194 EXPECT_EQ(
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
201 // enforce.
202 int poll_interval_seconds =
203 GCMChannelStatusRequest::min_poll_interval_seconds() + 15 * 60;
204 StartRequest();
205 SetResponseProtoData(NOT_SPECIFIED, poll_interval_seconds);
206 CompleteFetch();
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
216 // enforce.
217 int poll_interval_seconds =
218 GCMChannelStatusRequest::min_poll_interval_seconds() - 15 * 60;
219 StartRequest();
220 SetResponseProtoData(NOT_SPECIFIED, poll_interval_seconds);
221 CompleteFetch();
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;
233 StartRequest();
234 SetResponseProtoData(GCM_DISABLED, poll_interval_seconds);
235 CompleteFetch();
237 EXPECT_TRUE(request_callback_invoked_);
238 EXPECT_TRUE(update_received_);
239 EXPECT_FALSE(enabled_);
240 EXPECT_EQ(poll_interval_seconds, poll_interval_seconds_);
243 } // namespace gcm