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

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 android 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 void StartRequest();
21 void SetResponseStatusAndString(net::HttpStatusCode status_code,
22 const std::string& response_body);
23 void SetResponseProtoData(bool enabled_set,
24 bool enabled,
25 int poll_interval_seconds);
26 void CompleteFetch();
27 void OnRequestCompleted(bool enabled, const base::TimeDelta& poll_interval);
28
29 scoped_ptr<GCMChannelStatusRequest> request_;
30 base::MessageLoop message_loop_;
31 net::TestURLFetcherFactory url_fetcher_factory_;
32 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
33 bool request_callback_invoked_;
34 bool enabled_;
35 base::TimeDelta poll_interval_;
36 };
37
38 GCMChannelStatusRequestTest::GCMChannelStatusRequestTest()
39 : url_request_context_getter_(new net::TestURLRequestContextGetter(
40 message_loop_.message_loop_proxy())),
41 request_callback_invoked_(false),
42 enabled_(false) {
43 }
44
45 GCMChannelStatusRequestTest::~GCMChannelStatusRequestTest() {
46 }
47
48 void GCMChannelStatusRequestTest::StartRequest() {
49 request_.reset(new GCMChannelStatusRequest(
50 "",
51 url_request_context_getter_.get(),
52 base::Bind(&GCMChannelStatusRequestTest::OnRequestCompleted,
53 base::Unretained(this))));
54 request_->Start();
55 }
56
57 void GCMChannelStatusRequestTest::SetResponseStatusAndString(
58 net::HttpStatusCode status_code,
59 const std::string& response_body) {
60 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
61 ASSERT_TRUE(fetcher);
62 fetcher->set_response_code(status_code);
63 fetcher->SetResponseString(response_body);
64 }
65
66 void GCMChannelStatusRequestTest::SetResponseProtoData(
67 bool enabled_set, bool enabled, int poll_interval_seconds) {
fgorski 2014/09/03 20:42:16 you could use a "test scenario" enum instead: {
jianli 2014/09/03 21:17:55 Done.
68 gcm_proto::ExperimentStatusResponse response_proto;
69 if (enabled_set)
70 response_proto.mutable_gcm_channel()->set_enabled(enabled);
71
72 // Zero |poll_interval_seconds| means the optional field is not set.
73 if (poll_interval_seconds)
74 response_proto.set_poll_interval_seconds(poll_interval_seconds);
75
76 std::string response_string;
77 response_proto.SerializeToString(&response_string);
78 SetResponseStatusAndString(net::HTTP_OK, response_string);
79 }
80
81 void GCMChannelStatusRequestTest::CompleteFetch() {
82 request_callback_invoked_ = false;
83 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
84 ASSERT_TRUE(fetcher);
85 fetcher->delegate()->OnURLFetchComplete(fetcher);
86 }
87
88 void GCMChannelStatusRequestTest::OnRequestCompleted(
89 bool enabled, const base::TimeDelta& poll_interval) {
90 request_callback_invoked_ = true;
91 enabled_ = enabled;
92 poll_interval_ = poll_interval;
93 }
94
95 TEST_F(GCMChannelStatusRequestTest, ResponseHttpStatusNotOK) {
96 StartRequest();
97 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "");
98 CompleteFetch();
99
100 EXPECT_FALSE(request_callback_invoked_);
101 }
102
103 TEST_F(GCMChannelStatusRequestTest, ResponseEmpty) {
104 StartRequest();
105 SetResponseStatusAndString(net::HTTP_OK, "");
106 CompleteFetch();
107
108 EXPECT_FALSE(request_callback_invoked_);
109 }
110
111 TEST_F(GCMChannelStatusRequestTest, ResponseNotInProtoFormat) {
112 StartRequest();
113 SetResponseStatusAndString(net::HTTP_OK, "foo");
114 CompleteFetch();
115
116 EXPECT_FALSE(request_callback_invoked_);
117 }
118
119 TEST_F(GCMChannelStatusRequestTest, ResponseEmptyProtoData) {
120 StartRequest();
121 SetResponseProtoData(false, false, 0);
122 CompleteFetch();
123
124 EXPECT_FALSE(request_callback_invoked_);
125 }
126
127 TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatus) {
128 StartRequest();
129 SetResponseProtoData(true, false, 0);
130 CompleteFetch();
131
132 EXPECT_TRUE(request_callback_invoked_);
133 EXPECT_FALSE(enabled_);
134 EXPECT_EQ(GCMChannelStatusRequest::default_poll_interval_for_testing(),
135 poll_interval_);
136 }
137
138 TEST_F(GCMChannelStatusRequestTest, ResponseWithEnabledStatus) {
139 StartRequest();
140 SetResponseProtoData(true, true, 0);
141 CompleteFetch();
142
143 EXPECT_TRUE(request_callback_invoked_);
144 EXPECT_TRUE(enabled_);
145 EXPECT_EQ(GCMChannelStatusRequest::default_poll_interval_for_testing(),
146 poll_interval_);
147 }
148
149 TEST_F(GCMChannelStatusRequestTest, ResponseWithPollInterval) {
150 // Setting a poll interval 15 minutes longer than the minimum interval we
151 // enforce.
152 base::TimeDelta poll_interval =
153 GCMChannelStatusRequest::min_poll_interval_for_testing() +
154 base::TimeDelta::FromMinutes(15);
155 StartRequest();
156 SetResponseProtoData(false, false, poll_interval.InSeconds());
157 CompleteFetch();
158
159 EXPECT_TRUE(request_callback_invoked_);
160 EXPECT_TRUE(enabled_);
161 EXPECT_EQ(poll_interval, poll_interval_);
162 }
163
164 TEST_F(GCMChannelStatusRequestTest, ResponseWithShortPollInterval) {
165 // Setting a poll interval 15 minutes shorter than the minimum interval we
166 // enforce.
fgorski 2014/09/03 20:42:16 There is an assumption here that min poll interval
jianli 2014/09/03 21:17:55 I think this still works if the min poll interval
167 base::TimeDelta poll_interval =
168 GCMChannelStatusRequest::min_poll_interval_for_testing() -
169 base::TimeDelta::FromMinutes(15);
170 StartRequest();
171 SetResponseProtoData(false, false, poll_interval.InSeconds());
172 CompleteFetch();
173
174 EXPECT_TRUE(request_callback_invoked_);
175 EXPECT_TRUE(enabled_);
176 EXPECT_EQ(GCMChannelStatusRequest::min_poll_interval_for_testing(),
177 poll_interval_);
178 }
179
180 TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatusAndPollInterval) {
181 base::TimeDelta poll_interval =
182 GCMChannelStatusRequest::min_poll_interval_for_testing() +
183 base::TimeDelta::FromMinutes(15);
184 StartRequest();
185 SetResponseProtoData(true, false, poll_interval.InSeconds());
186 CompleteFetch();
187
188 EXPECT_TRUE(request_callback_invoked_);
189 EXPECT_FALSE(enabled_);
190 EXPECT_EQ(poll_interval, poll_interval_);
191 }
192
193 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698