| 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/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "components/copresence/handlers/directive_handler.h" | 15 #include "components/copresence/handlers/directive_handler.h" |
| 16 #include "components/copresence/mediums/audio/audio_manager.h" | 16 #include "components/copresence/mediums/audio/audio_manager.h" |
| 17 #include "components/copresence/proto/data.pb.h" | 17 #include "components/copresence/proto/data.pb.h" |
| 18 #include "components/copresence/proto/enums.pb.h" | 18 #include "components/copresence/proto/enums.pb.h" |
| 19 #include "components/copresence/proto/rpcs.pb.h" | 19 #include "components/copresence/proto/rpcs.pb.h" |
| 20 #include "components/copresence/test/fake_directive_handler.h" |
| 20 #include "components/copresence/test/stub_whispernet_client.h" | 21 #include "components/copresence/test/stub_whispernet_client.h" |
| 21 #include "net/http/http_status_code.h" | 22 #include "net/http/http_status_code.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 23 | 24 |
| 24 using google::protobuf::MessageLite; | 25 using google::protobuf::MessageLite; |
| 25 using google::protobuf::RepeatedPtrField; | 26 using google::protobuf::RepeatedPtrField; |
| 26 | 27 |
| 27 using testing::ElementsAre; | 28 using testing::ElementsAre; |
| 28 using testing::Property; | 29 using testing::Property; |
| 29 using testing::SizeIs; | 30 using testing::SizeIs; |
| 30 | 31 |
| 31 namespace copresence { | 32 namespace copresence { |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 const char kChromeVersion[] = "Chrome Version String"; | 36 const char kChromeVersion[] = "Chrome Version String"; |
| 36 | 37 |
| 37 void CreateSubscribedMessage(const std::vector<std::string>& subscription_ids, | 38 void CreateSubscribedMessage(const std::vector<std::string>& subscription_ids, |
| 38 const std::string& message_string, | 39 const std::string& message_string, |
| 39 SubscribedMessage* message_proto) { | 40 SubscribedMessage* message_proto) { |
| 40 message_proto->mutable_published_message()->set_payload(message_string); | 41 message_proto->mutable_published_message()->set_payload(message_string); |
| 41 for (const std::string& subscription_id : subscription_ids) { | 42 for (const std::string& subscription_id : subscription_ids) { |
| 42 message_proto->add_subscription_id(subscription_id); | 43 message_proto->add_subscription_id(subscription_id); |
| 43 } | 44 } |
| 44 } | 45 } |
| 45 | 46 |
| 46 // TODO(ckehoe): Make DirectiveHandler an interface. | |
| 47 class FakeDirectiveHandler final : public DirectiveHandler { | |
| 48 public: | |
| 49 FakeDirectiveHandler() : DirectiveHandler(nullptr) {} | |
| 50 | |
| 51 const std::vector<std::string>& added_directives() const { | |
| 52 return added_directives_; | |
| 53 } | |
| 54 | |
| 55 const std::vector<std::string>& removed_directives() const { | |
| 56 return removed_directives_; | |
| 57 } | |
| 58 | |
| 59 void Start(WhispernetClient* /* whispernet_client */, | |
| 60 const TokensCallback& /* tokens_cb */) override { | |
| 61 NOTREACHED(); | |
| 62 } | |
| 63 | |
| 64 void AddDirective(const Directive& directive) override { | |
| 65 added_directives_.push_back(directive.subscription_id()); | |
| 66 } | |
| 67 | |
| 68 void RemoveDirectives(const std::string& op_id) override { | |
| 69 removed_directives_.push_back(op_id); | |
| 70 } | |
| 71 | |
| 72 const std::string GetCurrentAudioToken(AudioType type) const override { | |
| 73 return type == AUDIBLE ? "current audible" : "current inaudible"; | |
| 74 } | |
| 75 | |
| 76 bool IsAudioTokenHeard(AudioType type) const override { return true; } | |
| 77 | |
| 78 private: | |
| 79 std::vector<std::string> added_directives_; | |
| 80 std::vector<std::string> removed_directives_; | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(FakeDirectiveHandler); | |
| 83 }; | |
| 84 | |
| 85 } // namespace | 47 } // namespace |
| 86 | 48 |
| 87 class RpcHandlerTest : public testing::Test, public CopresenceDelegate { | 49 class RpcHandlerTest : public testing::Test, public CopresenceDelegate { |
| 88 public: | 50 public: |
| 89 RpcHandlerTest() | 51 RpcHandlerTest() |
| 90 : whispernet_client_(new StubWhispernetClient), | 52 : whispernet_client_(new StubWhispernetClient), |
| 91 rpc_handler_(this, | 53 rpc_handler_(this, |
| 92 &directive_handler_, | 54 &directive_handler_, |
| 55 nullptr, |
| 93 base::Bind(&RpcHandlerTest::CaptureHttpPost, | 56 base::Bind(&RpcHandlerTest::CaptureHttpPost, |
| 94 base::Unretained(this))), | 57 base::Unretained(this))), |
| 95 status_(SUCCESS) {} | 58 status_(SUCCESS) {} |
| 96 | 59 |
| 97 // CopresenceDelegate implementation | 60 // CopresenceDelegate implementation |
| 98 | 61 |
| 99 void HandleMessages(const std::string& /* app_id */, | 62 void HandleMessages(const std::string& /* app_id */, |
| 100 const std::string& subscription_id, | 63 const std::string& subscription_id, |
| 101 const std::vector<Message>& messages) override { | 64 const std::vector<Message>& messages) override { |
| 102 // app_id is unused for now, pending a server fix. | 65 // app_id is unused for now, pending a server fix. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 116 } | 79 } |
| 117 | 80 |
| 118 const std::string GetAPIKey(const std::string& app_id) const override { | 81 const std::string GetAPIKey(const std::string& app_id) const override { |
| 119 return app_id + " API Key"; | 82 return app_id + " API Key"; |
| 120 } | 83 } |
| 121 | 84 |
| 122 WhispernetClient* GetWhispernetClient() override { | 85 WhispernetClient* GetWhispernetClient() override { |
| 123 return whispernet_client_.get(); | 86 return whispernet_client_.get(); |
| 124 } | 87 } |
| 125 | 88 |
| 89 // TODO(ckehoe): Add GCM tests. |
| 90 gcm::GCMDriver* GetGCMDriver() override { |
| 91 return nullptr; |
| 92 } |
| 93 |
| 126 protected: | 94 protected: |
| 127 | 95 |
| 128 // Send test input to RpcHandler | 96 // Send test input to RpcHandler |
| 129 | 97 |
| 130 void RegisterForToken(const std::string& auth_token) { | 98 void RegisterForToken(const std::string& auth_token) { |
| 131 rpc_handler_.RegisterForToken(auth_token); | 99 rpc_handler_.RegisterForToken(auth_token); |
| 132 } | 100 } |
| 133 | 101 |
| 134 void SendRegisterResponse(const std::string& auth_token, | 102 void SendRegisterResponse(const std::string& auth_token, |
| 135 const std::string& device_id) { | 103 const std::string& device_id) { |
| 136 RegisterDeviceResponse response; | 104 RegisterDeviceResponse response; |
| 137 response.set_registered_device_id(device_id); | 105 response.set_registered_device_id(device_id); |
| 138 response.mutable_header()->mutable_status()->set_code(OK); | 106 response.mutable_header()->mutable_status()->set_code(OK); |
| 139 | 107 |
| 140 std::string serialized_response; | 108 std::string serialized_response; |
| 141 response.SerializeToString(&serialized_response); | 109 response.SerializeToString(&serialized_response); |
| 142 rpc_handler_.RegisterResponseHandler( | 110 rpc_handler_.RegisterResponseHandler( |
| 143 auth_token, nullptr, net::HTTP_OK, serialized_response); | 111 auth_token, false, nullptr, net::HTTP_OK, serialized_response); |
| 144 } | 112 } |
| 145 | 113 |
| 146 void SendReport(scoped_ptr<ReportRequest> request, | 114 void SendReport(scoped_ptr<ReportRequest> request, |
| 147 const std::string& app_id, | 115 const std::string& app_id, |
| 148 const std::string& auth_token) { | 116 const std::string& auth_token) { |
| 149 rpc_handler_.SendReportRequest( | 117 rpc_handler_.SendReportRequest( |
| 150 request.Pass(), app_id, auth_token, StatusCallback()); | 118 request.Pass(), app_id, auth_token, StatusCallback()); |
| 151 } | 119 } |
| 152 | 120 |
| 153 void SendReportResponse(int status_code, | 121 void SendReportResponse(int status_code, |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 EXPECT_TRUE(TokenIsInvalid("bad token")); | 349 EXPECT_TRUE(TokenIsInvalid("bad token")); |
| 382 EXPECT_THAT(messages_by_subscription_["Subscription 1"], | 350 EXPECT_THAT(messages_by_subscription_["Subscription 1"], |
| 383 ElementsAre("Message A", "Message C")); | 351 ElementsAre("Message A", "Message C")); |
| 384 EXPECT_THAT(messages_by_subscription_["Subscription 2"], | 352 EXPECT_THAT(messages_by_subscription_["Subscription 2"], |
| 385 ElementsAre("Message B", "Message C")); | 353 ElementsAre("Message B", "Message C")); |
| 386 EXPECT_THAT(directive_handler_.added_directives(), | 354 EXPECT_THAT(directive_handler_.added_directives(), |
| 387 ElementsAre("Subscription 1", "Subscription 2")); | 355 ElementsAre("Subscription 1", "Subscription 2")); |
| 388 } | 356 } |
| 389 | 357 |
| 390 } // namespace copresence | 358 } // namespace copresence |
| OLD | NEW |