OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/copresence/rpc/rpc_handler.h" | 5 #include "components/copresence/rpc/rpc_handler.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "components/copresence/handlers/directive_handler.h" | 14 #include "components/copresence/handlers/directive_handler.h" |
15 #include "components/copresence/proto/data.pb.h" | 15 #include "components/copresence/proto/data.pb.h" |
16 #include "components/copresence/proto/enums.pb.h" | 16 #include "components/copresence/proto/enums.pb.h" |
17 #include "components/copresence/proto/rpcs.pb.h" | 17 #include "components/copresence/proto/rpcs.pb.h" |
18 #include "net/http/http_status_code.h" | 18 #include "net/http/http_status_code.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 using google::protobuf::MessageLite; | 21 using google::protobuf::MessageLite; |
22 using google::protobuf::RepeatedPtrField; | 22 using google::protobuf::RepeatedPtrField; |
23 | 23 |
24 namespace copresence { | 24 namespace copresence { |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 const char kChromeVersion[] = "Chrome Version String"; | 28 const char kChromeVersion[] = "Chrome Version String"; |
29 | 29 |
30 void AddMessageWithStrategy(ReportRequest* report, | |
31 BroadcastScanConfiguration strategy) { | |
32 report->mutable_manage_messages_request()->add_message_to_publish() | |
33 ->mutable_token_exchange_strategy()->set_broadcast_scan_configuration( | |
34 strategy); | |
35 } | |
36 | |
37 void AddSubscriptionWithStrategy(ReportRequest* report, | |
38 BroadcastScanConfiguration strategy) { | |
39 report->mutable_manage_subscriptions_request()->add_subscription() | |
40 ->mutable_token_exchange_strategy()->set_broadcast_scan_configuration( | |
41 strategy); | |
42 } | |
43 | |
44 void CreateSubscribedMessage(const std::vector<std::string>& subscription_ids, | 30 void CreateSubscribedMessage(const std::vector<std::string>& subscription_ids, |
45 const std::string& message_string, | 31 const std::string& message_string, |
46 SubscribedMessage* message_proto) { | 32 SubscribedMessage* message_proto) { |
47 message_proto->mutable_published_message()->set_payload(message_string); | 33 message_proto->mutable_published_message()->set_payload(message_string); |
48 for (std::vector<std::string>::const_iterator subscription_id = | 34 for (std::vector<std::string>::const_iterator subscription_id = |
49 subscription_ids.begin(); | 35 subscription_ids.begin(); |
50 subscription_id != subscription_ids.end(); | 36 subscription_id != subscription_ids.end(); |
51 ++subscription_id) { | 37 ++subscription_id) { |
52 message_proto->add_subscription_id(*subscription_id); | 38 message_proto->add_subscription_id(*subscription_id); |
53 } | 39 } |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 TEST_F(RpcHandlerTest, Initialize) { | 180 TEST_F(RpcHandlerTest, Initialize) { |
195 SetDeviceId(""); | 181 SetDeviceId(""); |
196 rpc_handler_.Initialize(RpcHandler::SuccessCallback()); | 182 rpc_handler_.Initialize(RpcHandler::SuccessCallback()); |
197 RegisterDeviceRequest* registration = | 183 RegisterDeviceRequest* registration = |
198 static_cast<RegisterDeviceRequest*>(request_proto_.get()); | 184 static_cast<RegisterDeviceRequest*>(request_proto_.get()); |
199 Identity identity = registration->device_identifiers().registrant(); | 185 Identity identity = registration->device_identifiers().registrant(); |
200 EXPECT_EQ(CHROME, identity.type()); | 186 EXPECT_EQ(CHROME, identity.type()); |
201 EXPECT_FALSE(identity.chrome_id().empty()); | 187 EXPECT_FALSE(identity.chrome_id().empty()); |
202 } | 188 } |
203 | 189 |
204 TEST_F(RpcHandlerTest, GetDeviceCapabilities) { | |
205 // Empty request. | |
206 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest)); | |
207 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); | |
208 const TokenTechnology* token_technology = &GetTokenTechnologyFromReport(); | |
209 EXPECT_EQ(AUDIO_ULTRASOUND_PASSBAND, token_technology->medium()); | |
210 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0)); | |
211 EXPECT_EQ(RECEIVE, token_technology->instruction_type(1)); | |
212 | |
213 // Request with broadcast only. | |
214 scoped_ptr<ReportRequest> report(new ReportRequest); | |
215 AddMessageWithStrategy(report.get(), BROADCAST_ONLY); | |
216 rpc_handler_.SendReportRequest(report.Pass()); | |
217 token_technology = &GetTokenTechnologyFromReport(); | |
218 EXPECT_EQ(1, token_technology->instruction_type_size()); | |
219 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0)); | |
220 EXPECT_FALSE(GetReportSent()->has_manage_subscriptions_request()); | |
221 | |
222 // Request with scan only. | |
223 report.reset(new ReportRequest); | |
224 AddSubscriptionWithStrategy(report.get(), SCAN_ONLY); | |
225 AddSubscriptionWithStrategy(report.get(), SCAN_ONLY); | |
226 rpc_handler_.SendReportRequest(report.Pass()); | |
227 token_technology = &GetTokenTechnologyFromReport(); | |
228 EXPECT_EQ(1, token_technology->instruction_type_size()); | |
229 EXPECT_EQ(RECEIVE, token_technology->instruction_type(0)); | |
230 EXPECT_FALSE(GetReportSent()->has_manage_messages_request()); | |
231 | |
232 // Request with both scan and broadcast only (conflict). | |
233 report.reset(new ReportRequest); | |
234 AddMessageWithStrategy(report.get(), SCAN_ONLY); | |
235 AddMessageWithStrategy(report.get(), BROADCAST_ONLY); | |
236 AddSubscriptionWithStrategy(report.get(), BROADCAST_ONLY); | |
237 rpc_handler_.SendReportRequest(report.Pass()); | |
238 token_technology = &GetTokenTechnologyFromReport(); | |
239 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0)); | |
240 EXPECT_EQ(RECEIVE, token_technology->instruction_type(1)); | |
241 | |
242 // Request with broadcast and scan. | |
243 report.reset(new ReportRequest); | |
244 AddMessageWithStrategy(report.get(), SCAN_ONLY); | |
245 AddSubscriptionWithStrategy(report.get(), BROADCAST_AND_SCAN); | |
246 rpc_handler_.SendReportRequest(report.Pass()); | |
247 token_technology = &GetTokenTechnologyFromReport(); | |
248 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0)); | |
249 EXPECT_EQ(RECEIVE, token_technology->instruction_type(1)); | |
250 } | |
251 | |
252 TEST_F(RpcHandlerTest, CreateRequestHeader) { | 190 TEST_F(RpcHandlerTest, CreateRequestHeader) { |
253 SetDeviceId("CreateRequestHeader Device ID"); | 191 SetDeviceId("CreateRequestHeader Device ID"); |
254 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest), | 192 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest), |
255 "CreateRequestHeader App ID", | 193 "CreateRequestHeader App ID", |
256 StatusCallback()); | 194 StatusCallback()); |
257 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); | 195 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); |
258 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); | 196 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); |
259 EXPECT_EQ(kChromeVersion, | 197 EXPECT_EQ(kChromeVersion, |
260 report->header().framework_version().version_name()); | 198 report->header().framework_version().version_name()); |
261 EXPECT_EQ("CreateRequestHeader App ID", | 199 EXPECT_EQ("CreateRequestHeader App ID", |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 messages_by_subscription_["Subscription 2"][1].payload()); | 276 messages_by_subscription_["Subscription 2"][1].payload()); |
339 | 277 |
340 ASSERT_EQ(2U, directive_handler->added_directives().size()); | 278 ASSERT_EQ(2U, directive_handler->added_directives().size()); |
341 EXPECT_EQ("Subscription 1", | 279 EXPECT_EQ("Subscription 1", |
342 directive_handler->added_directives()[0].subscription_id()); | 280 directive_handler->added_directives()[0].subscription_id()); |
343 EXPECT_EQ("Subscription 2", | 281 EXPECT_EQ("Subscription 2", |
344 directive_handler->added_directives()[1].subscription_id()); | 282 directive_handler->added_directives()[1].subscription_id()); |
345 } | 283 } |
346 | 284 |
347 } // namespace copresence | 285 } // namespace copresence |
OLD | NEW |