Fix AfterTyping experiment for Android.
[chromium-blink-merge.git] / components / gcm_driver / gcm_channel_status_request_unittest.cc
blob4bb3c69fb8d007693f6a3d37d77b95a85696a26d
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"
12 namespace gcm {
14 class GCMChannelStatusRequestTest : public testing::Test {
15 public:
16 GCMChannelStatusRequestTest();
17 ~GCMChannelStatusRequestTest() override;
19 protected:
20 enum GCMStatus {
21 NOT_SPECIFIED,
22 GCM_ENABLED,
23 GCM_DISABLED,
26 void StartRequest();
27 void SetResponseStatusAndString(net::HttpStatusCode status_code,
28 const std::string& response_body);
29 void SetResponseProtoData(GCMStatus status, int poll_interval_seconds);
30 void CompleteFetch();
31 void OnRequestCompleted(bool update_received,
32 bool enabled,
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_;
41 bool enabled_;
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),
50 enabled_(true),
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/",
61 "user agent string",
62 base::Bind(&GCMChannelStatusRequestTest::OnRequestCompleted,
63 base::Unretained(this))));
64 request_->Start();
67 void GCMChannelStatusRequestTest::SetResponseStatusAndString(
68 net::HttpStatusCode status_code,
69 const std::string& response_body) {
70 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
71 ASSERT_TRUE(fetcher);
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 ==
83 GCM_ENABLED);
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);
98 ASSERT_TRUE(fetcher);
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;
106 enabled_ = enabled;
107 poll_interval_seconds_ = poll_interval_seconds;
110 TEST_F(GCMChannelStatusRequestTest, RequestData) {
111 StartRequest();
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) {
135 StartRequest();
136 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "");
137 CompleteFetch();
139 EXPECT_FALSE(request_callback_invoked_);
142 TEST_F(GCMChannelStatusRequestTest, ResponseEmpty) {
143 StartRequest();
144 SetResponseStatusAndString(net::HTTP_OK, "");
145 CompleteFetch();
147 EXPECT_TRUE(request_callback_invoked_);
148 EXPECT_FALSE(update_received_);
151 TEST_F(GCMChannelStatusRequestTest, ResponseNotInProtoFormat) {
152 StartRequest();
153 SetResponseStatusAndString(net::HTTP_OK, "foo");
154 CompleteFetch();
156 EXPECT_FALSE(request_callback_invoked_);
159 TEST_F(GCMChannelStatusRequestTest, ResponseEmptyProtoData) {
160 StartRequest();
161 SetResponseProtoData(NOT_SPECIFIED, 0);
162 CompleteFetch();
164 EXPECT_TRUE(request_callback_invoked_);
165 EXPECT_FALSE(update_received_);
168 TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatus) {
169 StartRequest();
170 SetResponseProtoData(GCM_DISABLED, 0);
171 CompleteFetch();
173 EXPECT_TRUE(request_callback_invoked_);
174 EXPECT_TRUE(update_received_);
175 EXPECT_FALSE(enabled_);
176 EXPECT_EQ(
177 GCMChannelStatusRequest::default_poll_interval_seconds(),
178 poll_interval_seconds_);
181 TEST_F(GCMChannelStatusRequestTest, ResponseWithEnabledStatus) {
182 StartRequest();
183 SetResponseProtoData(GCM_ENABLED, 0);
184 CompleteFetch();
186 EXPECT_TRUE(request_callback_invoked_);
187 EXPECT_TRUE(update_received_);
188 EXPECT_TRUE(enabled_);
189 EXPECT_EQ(
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
196 // enforce.
197 int poll_interval_seconds =
198 GCMChannelStatusRequest::min_poll_interval_seconds() + 15 * 60;
199 StartRequest();
200 SetResponseProtoData(NOT_SPECIFIED, poll_interval_seconds);
201 CompleteFetch();
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
211 // enforce.
212 int poll_interval_seconds =
213 GCMChannelStatusRequest::min_poll_interval_seconds() - 15 * 60;
214 StartRequest();
215 SetResponseProtoData(NOT_SPECIFIED, poll_interval_seconds);
216 CompleteFetch();
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;
228 StartRequest();
229 SetResponseProtoData(GCM_DISABLED, poll_interval_seconds);
230 CompleteFetch();
232 EXPECT_TRUE(request_callback_invoked_);
233 EXPECT_TRUE(update_received_);
234 EXPECT_FALSE(enabled_);
235 EXPECT_EQ(poll_interval_seconds, poll_interval_seconds_);
238 } // namespace gcm