OLD | NEW |
---|---|
(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, const base::TimeDelta& poll_interval); | |
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 base::TimeDelta poll_interval_; | |
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_(false) { | |
Nicolas Zea
2014/09/04 23:57:50
Presumably we'll normally be enabled by default ri
jianli
2014/09/05 18:41:55
Done.
| |
47 } | |
48 | |
49 GCMChannelStatusRequestTest::~GCMChannelStatusRequestTest() { | |
50 } | |
51 | |
52 void GCMChannelStatusRequestTest::StartRequest() { | |
53 request_.reset(new GCMChannelStatusRequest( | |
54 "", | |
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, const base::TimeDelta& poll_interval) { | |
94 request_callback_invoked_ = true; | |
95 enabled_ = enabled; | |
96 poll_interval_ = poll_interval; | |
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(GCMChannelStatusRequest::default_poll_interval_for_testing(), | |
139 poll_interval_); | |
140 } | |
141 | |
142 TEST_F(GCMChannelStatusRequestTest, ResponseWithEnabledStatus) { | |
143 StartRequest(); | |
144 SetResponseProtoData(GCM_ENABLED, 0); | |
145 CompleteFetch(); | |
146 | |
147 EXPECT_TRUE(request_callback_invoked_); | |
148 EXPECT_TRUE(enabled_); | |
149 EXPECT_EQ(GCMChannelStatusRequest::default_poll_interval_for_testing(), | |
150 poll_interval_); | |
151 } | |
152 | |
153 TEST_F(GCMChannelStatusRequestTest, ResponseWithPollInterval) { | |
154 // Setting a poll interval 15 minutes longer than the minimum interval we | |
155 // enforce. | |
156 base::TimeDelta poll_interval = | |
157 GCMChannelStatusRequest::min_poll_interval_for_testing() + | |
158 base::TimeDelta::FromMinutes(15); | |
Nicolas Zea
2014/09/04 23:57:50
Isn't the minimum 30 minutes?
jianli
2014/09/05 18:41:55
Yes, the minimum is 30 minutes. Here we're setting
| |
159 StartRequest(); | |
160 SetResponseProtoData(NOT_SPECIFIED, poll_interval.InSeconds()); | |
161 CompleteFetch(); | |
162 | |
163 EXPECT_TRUE(request_callback_invoked_); | |
164 EXPECT_TRUE(enabled_); | |
165 EXPECT_EQ(poll_interval, poll_interval_); | |
166 } | |
167 | |
168 TEST_F(GCMChannelStatusRequestTest, ResponseWithShortPollInterval) { | |
169 // Setting a poll interval 15 minutes shorter than the minimum interval we | |
170 // enforce. | |
171 base::TimeDelta poll_interval = | |
172 GCMChannelStatusRequest::min_poll_interval_for_testing() - | |
173 base::TimeDelta::FromMinutes(15); | |
174 StartRequest(); | |
175 SetResponseProtoData(NOT_SPECIFIED, poll_interval.InSeconds()); | |
176 CompleteFetch(); | |
177 | |
178 EXPECT_TRUE(request_callback_invoked_); | |
179 EXPECT_TRUE(enabled_); | |
180 EXPECT_EQ(GCMChannelStatusRequest::min_poll_interval_for_testing(), | |
181 poll_interval_); | |
182 } | |
183 | |
184 TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatusAndPollInterval) { | |
185 base::TimeDelta poll_interval = | |
186 GCMChannelStatusRequest::min_poll_interval_for_testing() + | |
187 base::TimeDelta::FromMinutes(15); | |
188 StartRequest(); | |
189 SetResponseProtoData(GCM_DISABLED, poll_interval.InSeconds()); | |
190 CompleteFetch(); | |
191 | |
192 EXPECT_TRUE(request_callback_invoked_); | |
193 EXPECT_FALSE(enabled_); | |
194 EXPECT_EQ(poll_interval, poll_interval_); | |
195 } | |
196 | |
197 } // namespace gcm | |
OLD | NEW |