Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: components/gcm_driver/gcm_channel_status_request_unittest.cc

Issue 530253002: Add GCMChannelStatusRequest to talk with server for all users (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix gn build Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "base/message_loop/message_loop.h"
6 #include "components/gcm_driver/gcm_channel_status_request.h"
7 #include "components/gcm_driver/proto/gcm_channel_status.pb.h"
8 #include "net/url_request/test_url_fetcher_factory.h"
9 #include "net/url_request/url_request_test_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace gcm {
13
14 class GCMChannelStatusRequestTest : public testing::Test {
15 public:
16 GCMChannelStatusRequestTest();
17 virtual ~GCMChannelStatusRequestTest();
18
19 protected:
20 enum GCMStatus {
21 NOT_SPECIFIED,
22 GCM_ENABLED,
23 GCM_DISABLED,
24 };
25
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 enabled, int poll_interval_seconds);
32
33 scoped_ptr<GCMChannelStatusRequest> request_;
34 base::MessageLoop message_loop_;
35 net::TestURLFetcherFactory url_fetcher_factory_;
36 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
37 bool request_callback_invoked_;
38 bool enabled_;
39 int poll_interval_seconds_;
40 };
41
42 GCMChannelStatusRequestTest::GCMChannelStatusRequestTest()
43 : url_request_context_getter_(new net::TestURLRequestContextGetter(
44 message_loop_.message_loop_proxy())),
45 request_callback_invoked_(false),
46 enabled_(true),
47 poll_interval_seconds_(0) {
48 }
49
50 GCMChannelStatusRequestTest::~GCMChannelStatusRequestTest() {
51 }
52
53 void GCMChannelStatusRequestTest::StartRequest() {
54 request_.reset(new GCMChannelStatusRequest(
55 url_request_context_getter_.get(),
56 base::Bind(&GCMChannelStatusRequestTest::OnRequestCompleted,
57 base::Unretained(this))));
58 request_->Start();
59 }
60
61 void GCMChannelStatusRequestTest::SetResponseStatusAndString(
62 net::HttpStatusCode status_code,
63 const std::string& response_body) {
64 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
65 ASSERT_TRUE(fetcher);
66 fetcher->set_response_code(status_code);
67 fetcher->SetResponseString(response_body);
68 }
69
70 void GCMChannelStatusRequestTest::SetResponseProtoData(
71 GCMStatus status, int poll_interval_seconds) {
72 gcm_proto::ExperimentStatusResponse response_proto;
73 if (status != NOT_SPECIFIED)
74 response_proto.mutable_gcm_channel()->set_enabled(status == GCM_ENABLED);
75
76 // Zero |poll_interval_seconds| means the optional field is not set.
77 if (poll_interval_seconds)
78 response_proto.set_poll_interval_seconds(poll_interval_seconds);
79
80 std::string response_string;
81 response_proto.SerializeToString(&response_string);
82 SetResponseStatusAndString(net::HTTP_OK, response_string);
83 }
84
85 void GCMChannelStatusRequestTest::CompleteFetch() {
86 request_callback_invoked_ = false;
87 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
88 ASSERT_TRUE(fetcher);
89 fetcher->delegate()->OnURLFetchComplete(fetcher);
90 }
91
92 void GCMChannelStatusRequestTest::OnRequestCompleted(
93 bool enabled, int poll_interval_seconds) {
94 request_callback_invoked_ = true;
95 enabled_ = enabled;
96 poll_interval_seconds_ = poll_interval_seconds;
97 }
98
99 TEST_F(GCMChannelStatusRequestTest, ResponseHttpStatusNotOK) {
100 StartRequest();
101 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "");
102 CompleteFetch();
103
104 EXPECT_FALSE(request_callback_invoked_);
105 }
106
107 TEST_F(GCMChannelStatusRequestTest, ResponseEmpty) {
108 StartRequest();
109 SetResponseStatusAndString(net::HTTP_OK, "");
110 CompleteFetch();
111
112 EXPECT_FALSE(request_callback_invoked_);
113 }
114
115 TEST_F(GCMChannelStatusRequestTest, ResponseNotInProtoFormat) {
116 StartRequest();
117 SetResponseStatusAndString(net::HTTP_OK, "foo");
118 CompleteFetch();
119
120 EXPECT_FALSE(request_callback_invoked_);
121 }
122
123 TEST_F(GCMChannelStatusRequestTest, ResponseEmptyProtoData) {
124 StartRequest();
125 SetResponseProtoData(NOT_SPECIFIED, 0);
126 CompleteFetch();
127
128 EXPECT_FALSE(request_callback_invoked_);
129 }
130
131 TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatus) {
132 StartRequest();
133 SetResponseProtoData(GCM_DISABLED, 0);
134 CompleteFetch();
135
136 EXPECT_TRUE(request_callback_invoked_);
137 EXPECT_FALSE(enabled_);
138 EXPECT_EQ(
139 GCMChannelStatusRequest::default_poll_interval_seconds_for_testing(),
140 poll_interval_seconds_);
141 }
142
143 TEST_F(GCMChannelStatusRequestTest, ResponseWithEnabledStatus) {
144 StartRequest();
145 SetResponseProtoData(GCM_ENABLED, 0);
146 CompleteFetch();
147
148 EXPECT_TRUE(request_callback_invoked_);
149 EXPECT_TRUE(enabled_);
150 EXPECT_EQ(
151 GCMChannelStatusRequest::default_poll_interval_seconds_for_testing(),
152 poll_interval_seconds_);
153 }
154
155 TEST_F(GCMChannelStatusRequestTest, ResponseWithPollInterval) {
156 // Setting a poll interval 15 minutes longer than the minimum interval we
157 // enforce.
158 int poll_interval_seconds =
159 GCMChannelStatusRequest::min_poll_interval_seconds_for_testing() +
160 15 * 60;
161 StartRequest();
162 SetResponseProtoData(NOT_SPECIFIED, poll_interval_seconds);
163 CompleteFetch();
164
165 EXPECT_TRUE(request_callback_invoked_);
166 EXPECT_TRUE(enabled_);
167 EXPECT_EQ(poll_interval_seconds, poll_interval_seconds_);
168 }
169
170 TEST_F(GCMChannelStatusRequestTest, ResponseWithShortPollInterval) {
171 // Setting a poll interval 15 minutes shorter than the minimum interval we
172 // enforce.
173 int poll_interval_seconds =
174 GCMChannelStatusRequest::min_poll_interval_seconds_for_testing() -
175 15 * 60;
176 StartRequest();
177 SetResponseProtoData(NOT_SPECIFIED, poll_interval_seconds);
178 CompleteFetch();
179
180 EXPECT_TRUE(request_callback_invoked_);
181 EXPECT_TRUE(enabled_);
182 EXPECT_EQ(GCMChannelStatusRequest::min_poll_interval_seconds_for_testing(),
183 poll_interval_seconds_);
184 }
185
186 TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatusAndPollInterval) {
187 int poll_interval_seconds =
188 GCMChannelStatusRequest::min_poll_interval_seconds_for_testing() +
189 15 * 60;
190 StartRequest();
191 SetResponseProtoData(GCM_DISABLED, poll_interval_seconds);
192 CompleteFetch();
193
194 EXPECT_TRUE(request_callback_invoked_);
195 EXPECT_FALSE(enabled_);
196 EXPECT_EQ(poll_interval_seconds, poll_interval_seconds_);
197 }
198
199 } // namespace gcm
OLDNEW
« no previous file with comments | « components/gcm_driver/gcm_channel_status_request.cc ('k') | components/gcm_driver/gcm_client_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698