| 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"; |
| 29 |
| 28 void AddMessageWithStrategy(ReportRequest* report, | 30 void AddMessageWithStrategy(ReportRequest* report, |
| 29 BroadcastScanConfiguration strategy) { | 31 BroadcastScanConfiguration strategy) { |
| 30 report->mutable_manage_messages_request()->add_message_to_publish() | 32 report->mutable_manage_messages_request()->add_message_to_publish() |
| 31 ->mutable_token_exchange_strategy()->set_broadcast_scan_configuration( | 33 ->mutable_token_exchange_strategy()->set_broadcast_scan_configuration( |
| 32 strategy); | 34 strategy); |
| 33 } | 35 } |
| 34 | 36 |
| 35 void AddSubscriptionWithStrategy(ReportRequest* report, | 37 void AddSubscriptionWithStrategy(ReportRequest* report, |
| 36 BroadcastScanConfiguration strategy) { | 38 BroadcastScanConfiguration strategy) { |
| 37 report->mutable_manage_subscriptions_request()->add_subscription() | 39 report->mutable_manage_subscriptions_request()->add_subscription() |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 private: | 78 private: |
| 77 std::vector<Directive> added_directives_; | 79 std::vector<Directive> added_directives_; |
| 78 | 80 |
| 79 DISALLOW_COPY_AND_ASSIGN(FakeDirectiveHandler); | 81 DISALLOW_COPY_AND_ASSIGN(FakeDirectiveHandler); |
| 80 }; | 82 }; |
| 81 | 83 |
| 82 } // namespace | 84 } // namespace |
| 83 | 85 |
| 84 class RpcHandlerTest : public testing::Test, public CopresenceClientDelegate { | 86 class RpcHandlerTest : public testing::Test, public CopresenceClientDelegate { |
| 85 public: | 87 public: |
| 86 RpcHandlerTest() : rpc_handler_(this), status_(SUCCESS) { | 88 RpcHandlerTest() : rpc_handler_(this), status_(SUCCESS), api_key_("API key") { |
| 87 rpc_handler_.server_post_callback_ = | 89 rpc_handler_.server_post_callback_ = |
| 88 base::Bind(&RpcHandlerTest::CaptureHttpPost, base::Unretained(this)); | 90 base::Bind(&RpcHandlerTest::CaptureHttpPost, base::Unretained(this)); |
| 89 rpc_handler_.device_id_ = "Device ID"; | 91 rpc_handler_.device_id_ = "Device ID"; |
| 90 } | 92 } |
| 91 | 93 |
| 92 void CaptureHttpPost( | 94 void CaptureHttpPost( |
| 93 net::URLRequestContextGetter* url_context_getter, | 95 net::URLRequestContextGetter* url_context_getter, |
| 94 const std::string& rpc_name, | 96 const std::string& rpc_name, |
| 95 scoped_ptr<MessageLite> request_proto, | 97 scoped_ptr<MessageLite> request_proto, |
| 96 const RpcHandler::PostCleanupCallback& response_callback) { | 98 const RpcHandler::PostCleanupCallback& response_callback) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 const std::vector<Message>& messages) OVERRIDE { | 163 const std::vector<Message>& messages) OVERRIDE { |
| 162 // app_id is unused for now, pending a server fix. | 164 // app_id is unused for now, pending a server fix. |
| 163 messages_by_subscription_[subscription_id] = messages; | 165 messages_by_subscription_[subscription_id] = messages; |
| 164 } | 166 } |
| 165 | 167 |
| 166 virtual net::URLRequestContextGetter* GetRequestContext() const OVERRIDE { | 168 virtual net::URLRequestContextGetter* GetRequestContext() const OVERRIDE { |
| 167 return NULL; | 169 return NULL; |
| 168 } | 170 } |
| 169 | 171 |
| 170 virtual const std::string GetPlatformVersionString() const OVERRIDE { | 172 virtual const std::string GetPlatformVersionString() const OVERRIDE { |
| 171 return "Version String"; | 173 return kChromeVersion; |
| 174 } |
| 175 |
| 176 virtual const std::string GetAPIKey() const OVERRIDE { |
| 177 return api_key_; |
| 172 } | 178 } |
| 173 | 179 |
| 174 virtual WhispernetClient* GetWhispernetClient() OVERRIDE { | 180 virtual WhispernetClient* GetWhispernetClient() OVERRIDE { |
| 175 return NULL; | 181 return NULL; |
| 176 } | 182 } |
| 177 | 183 |
| 178 protected: | 184 protected: |
| 179 // For rpc_handler_.invalid_audio_token_cache_ | 185 // For rpc_handler_.invalid_audio_token_cache_ |
| 180 base::MessageLoop message_loop_; | 186 base::MessageLoop message_loop_; |
| 181 | 187 |
| 182 RpcHandler rpc_handler_; | 188 RpcHandler rpc_handler_; |
| 189 CopresenceStatus status_; |
| 190 std::string api_key_; |
| 183 | 191 |
| 184 std::string rpc_name_; | 192 std::string rpc_name_; |
| 185 scoped_ptr<MessageLite> request_proto_; | 193 scoped_ptr<MessageLite> request_proto_; |
| 186 CopresenceStatus status_; | |
| 187 std::map<std::string, std::vector<Message> > messages_by_subscription_; | 194 std::map<std::string, std::vector<Message> > messages_by_subscription_; |
| 188 }; | 195 }; |
| 189 | 196 |
| 190 TEST_F(RpcHandlerTest, Initialize) { | 197 TEST_F(RpcHandlerTest, Initialize) { |
| 191 SetDeviceId(""); | 198 SetDeviceId(""); |
| 192 rpc_handler_.Initialize(RpcHandler::SuccessCallback()); | 199 rpc_handler_.Initialize(RpcHandler::SuccessCallback()); |
| 193 RegisterDeviceRequest* registration = | 200 RegisterDeviceRequest* registration = |
| 194 static_cast<RegisterDeviceRequest*>(request_proto_.get()); | 201 static_cast<RegisterDeviceRequest*>(request_proto_.get()); |
| 195 Identity identity = registration->device_identifiers().registrant(); | 202 Identity identity = registration->device_identifiers().registrant(); |
| 196 EXPECT_EQ(CHROME, identity.type()); | 203 EXPECT_EQ(CHROME, identity.type()); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 EXPECT_EQ(OPTED_OUT, subscription_filter.allowed_opt_in_state(0)); | 287 EXPECT_EQ(OPTED_OUT, subscription_filter.allowed_opt_in_state(0)); |
| 281 } | 288 } |
| 282 | 289 |
| 283 TEST_F(RpcHandlerTest, CreateRequestHeader) { | 290 TEST_F(RpcHandlerTest, CreateRequestHeader) { |
| 284 SetDeviceId("CreateRequestHeader Device ID"); | 291 SetDeviceId("CreateRequestHeader Device ID"); |
| 285 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest), | 292 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest), |
| 286 "CreateRequestHeader App ID", | 293 "CreateRequestHeader App ID", |
| 287 StatusCallback()); | 294 StatusCallback()); |
| 288 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); | 295 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); |
| 289 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); | 296 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); |
| 290 EXPECT_TRUE(report->header().has_framework_version()); | 297 EXPECT_EQ(kChromeVersion, |
| 298 report->header().framework_version().version_name()); |
| 291 EXPECT_EQ("CreateRequestHeader App ID", | 299 EXPECT_EQ("CreateRequestHeader App ID", |
| 292 report->header().client_version().client()); | 300 report->header().client_version().client()); |
| 293 EXPECT_EQ("CreateRequestHeader Device ID", | 301 EXPECT_EQ("CreateRequestHeader Device ID", |
| 294 report->header().registered_device_id()); | 302 report->header().registered_device_id()); |
| 303 EXPECT_EQ(CHROME_PLATFORM_TYPE, |
| 304 report->header().device_fingerprint().type()); |
| 295 } | 305 } |
| 296 | 306 |
| 297 // TODO(ckehoe): Renable these after https://codereview.chromium.org/453203002/ | 307 // TODO(ckehoe): Renable these after https://codereview.chromium.org/453203002/ |
| 298 // lands. | 308 // lands. |
| 299 #define MAYBE_ReportTokens DISABLED_ReportTokens | 309 #define MAYBE_ReportTokens DISABLED_ReportTokens |
| 300 | 310 |
| 301 TEST_F(RpcHandlerTest, MAYBE_ReportTokens) { | 311 TEST_F(RpcHandlerTest, MAYBE_ReportTokens) { |
| 302 std::vector<AudioToken> test_tokens; | 312 std::vector<AudioToken> test_tokens; |
| 303 test_tokens.push_back(AudioToken("token 1", false)); | 313 test_tokens.push_back(AudioToken("token 1", false)); |
| 304 test_tokens.push_back(AudioToken("token 2", true)); | 314 test_tokens.push_back(AudioToken("token 2", true)); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 messages_by_subscription_["Subscription 2"][1].payload()); | 380 messages_by_subscription_["Subscription 2"][1].payload()); |
| 371 | 381 |
| 372 ASSERT_EQ(2U, directive_handler->added_directives().size()); | 382 ASSERT_EQ(2U, directive_handler->added_directives().size()); |
| 373 EXPECT_EQ("Subscription 1", | 383 EXPECT_EQ("Subscription 1", |
| 374 directive_handler->added_directives()[0].subscription_id()); | 384 directive_handler->added_directives()[0].subscription_id()); |
| 375 EXPECT_EQ("Subscription 2", | 385 EXPECT_EQ("Subscription 2", |
| 376 directive_handler->added_directives()[1].subscription_id()); | 386 directive_handler->added_directives()[1].subscription_id()); |
| 377 } | 387 } |
| 378 | 388 |
| 379 } // namespace copresence | 389 } // namespace copresence |
| OLD | NEW |