| 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/stub_whispernet_client.h" |
| 20 #include "net/http/http_status_code.h" | 21 #include "net/http/http_status_code.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 22 | 23 |
| 23 using google::protobuf::MessageLite; | 24 using google::protobuf::MessageLite; |
| 24 using google::protobuf::RepeatedPtrField; | 25 using google::protobuf::RepeatedPtrField; |
| 25 | 26 |
| 27 using testing::ElementsAre; |
| 26 using testing::Property; | 28 using testing::Property; |
| 27 using testing::SizeIs; | 29 using testing::SizeIs; |
| 28 using testing::ElementsAre; | |
| 29 | 30 |
| 30 namespace copresence { | 31 namespace copresence { |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 const char kChromeVersion[] = "Chrome Version String"; | 35 const char kChromeVersion[] = "Chrome Version String"; |
| 35 | 36 |
| 36 void CreateSubscribedMessage(const std::vector<std::string>& subscription_ids, | 37 void CreateSubscribedMessage(const std::vector<std::string>& subscription_ids, |
| 37 const std::string& message_string, | 38 const std::string& message_string, |
| 38 SubscribedMessage* message_proto) { | 39 SubscribedMessage* message_proto) { |
| 39 message_proto->mutable_published_message()->set_payload(message_string); | 40 message_proto->mutable_published_message()->set_payload(message_string); |
| 40 for (const std::string& subscription_id : subscription_ids) { | 41 for (const std::string& subscription_id : subscription_ids) { |
| 41 message_proto->add_subscription_id(subscription_id); | 42 message_proto->add_subscription_id(subscription_id); |
| 42 } | 43 } |
| 43 } | 44 } |
| 44 | 45 |
| 45 // TODO(ckehoe): Make DirectiveHandler an interface. | 46 // TODO(ckehoe): Make DirectiveHandler an interface. |
| 46 class FakeDirectiveHandler : public DirectiveHandler { | 47 class FakeDirectiveHandler final : public DirectiveHandler { |
| 47 public: | 48 public: |
| 48 FakeDirectiveHandler() {} | 49 FakeDirectiveHandler() {} |
| 49 ~FakeDirectiveHandler() override {} | |
| 50 | 50 |
| 51 const std::vector<Directive>& added_directives() const { | 51 const std::vector<std::string>& added_directives() const { |
| 52 return added_directives_; | 52 return added_directives_; |
| 53 } | 53 } |
| 54 | 54 |
| 55 void Start(WhispernetClient* whispernet_client) override { | 55 void Start(WhispernetClient* /* whispernet_client */) override { |
| 56 NOTREACHED(); | 56 NOTREACHED(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 void AddDirective(const Directive& directive) override { | 59 void AddDirective(const Directive& directive) override { |
| 60 added_directives_.push_back(directive); | 60 added_directives_.push_back(directive.subscription_id()); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void RemoveDirectives(const std::string& op_id) override { | 63 void RemoveDirectives(const std::string& op_id) override { |
| 64 NOTREACHED(); | 64 NOTREACHED(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 const std::string GetCurrentAudioToken(AudioType type) const override { | 67 const std::string GetCurrentAudioToken(AudioType type) const override { |
| 68 return type == AUDIBLE ? "current audible" : "current inaudible"; | 68 return type == AUDIBLE ? "current audible" : "current inaudible"; |
| 69 } | 69 } |
| 70 | 70 |
| 71 private: | 71 private: |
| 72 std::vector<Directive> added_directives_; | 72 std::vector<std::string> added_directives_; |
| 73 | 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(FakeDirectiveHandler); | 74 DISALLOW_COPY_AND_ASSIGN(FakeDirectiveHandler); |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 } // namespace | 77 } // namespace |
| 78 | 78 |
| 79 class RpcHandlerTest : public testing::Test, public CopresenceDelegate { | 79 class RpcHandlerTest : public testing::Test, public CopresenceDelegate { |
| 80 public: | 80 public: |
| 81 RpcHandlerTest() : rpc_handler_(this, &directive_handler_), status_(SUCCESS) { | 81 RpcHandlerTest() |
| 82 rpc_handler_.server_post_callback_ = | 82 : whispernet_client_(new StubWhispernetClient), |
| 83 base::Bind(&RpcHandlerTest::CaptureHttpPost, base::Unretained(this)); | 83 rpc_handler_(this, |
| 84 } | 84 &directive_handler_, |
| 85 base::Bind(&RpcHandlerTest::CaptureHttpPost, |
| 86 base::Unretained(this))), |
| 87 status_(SUCCESS) {} |
| 85 | 88 |
| 86 // CopresenceDelegate implementation | 89 // CopresenceDelegate implementation |
| 87 | 90 |
| 88 void HandleMessages(const std::string& app_id, | 91 void HandleMessages(const std::string& /* app_id */, |
| 89 const std::string& subscription_id, | 92 const std::string& subscription_id, |
| 90 const std::vector<Message>& messages) override { | 93 const std::vector<Message>& messages) override { |
| 91 // app_id is unused for now, pending a server fix. | 94 // app_id is unused for now, pending a server fix. |
| 92 messages_by_subscription_[subscription_id] = messages; | 95 for (const Message& message : messages) { |
| 96 messages_by_subscription_[subscription_id].push_back(message.payload()); |
| 97 } |
| 93 } | 98 } |
| 94 | 99 |
| 95 net::URLRequestContextGetter* GetRequestContext() const override { | 100 net::URLRequestContextGetter* GetRequestContext() const override { |
| 96 return nullptr; | 101 return nullptr; |
| 97 } | 102 } |
| 98 | 103 |
| 99 const std::string GetPlatformVersionString() const override { | 104 const std::string GetPlatformVersionString() const override { |
| 100 return kChromeVersion; | 105 return kChromeVersion; |
| 101 } | 106 } |
| 102 | 107 |
| 103 const std::string GetAPIKey(const std::string& app_id) const override { | 108 const std::string GetAPIKey(const std::string& app_id) const override { |
| 104 return app_id + " API Key"; | 109 return app_id + " API Key"; |
| 105 } | 110 } |
| 106 | 111 |
| 107 const std::string GetAuthToken() const override { | 112 const std::string GetAuthToken() const override { |
| 108 return auth_token_; | 113 return auth_token_; |
| 109 } | 114 } |
| 110 | 115 |
| 111 WhispernetClient* GetWhispernetClient() override { return nullptr; } | 116 WhispernetClient* GetWhispernetClient() override { |
| 117 return whispernet_client_.get(); |
| 118 } |
| 112 | 119 |
| 113 protected: | 120 protected: |
| 114 void InvokeReportResponseHandler(int status_code, | 121 void InvokeReportResponseHandler(int status_code, |
| 115 const std::string& response) { | 122 const std::string& response) { |
| 116 rpc_handler_.ReportResponseHandler( | 123 rpc_handler_.ReportResponseHandler( |
| 117 base::Bind(&RpcHandlerTest::CaptureStatus, base::Unretained(this)), | 124 base::Bind(&RpcHandlerTest::CaptureStatus, base::Unretained(this)), |
| 118 nullptr, | 125 nullptr, |
| 119 status_code, | 126 status_code, |
| 120 response); | 127 response); |
| 121 } | 128 } |
| 122 | 129 |
| 123 void SetDeviceIdAndAuthToken(const std::string& device_id, | 130 void SetDeviceIdAndAuthToken(const std::string& device_id, |
| 124 const std::string& auth_token) { | 131 const std::string& auth_token) { |
| 125 rpc_handler_.device_id_by_auth_token_[auth_token] = device_id; | 132 rpc_handler_.device_id_by_auth_token_[auth_token] = device_id; |
| 126 auth_token_ = auth_token; | 133 auth_token_ = auth_token; |
| 127 } | 134 } |
| 128 | 135 |
| 129 void AddInvalidToken(const std::string& token) { | 136 void AddInvalidToken(const std::string& token) { |
| 130 rpc_handler_.invalid_audio_token_cache_.Add(token, true); | 137 rpc_handler_.invalid_audio_token_cache_.Add(token, true); |
| 131 } | 138 } |
| 132 | 139 |
| 133 bool TokenIsInvalid(const std::string& token) { | 140 bool TokenIsInvalid(const std::string& token) { |
| 134 return rpc_handler_.invalid_audio_token_cache_.HasKey(token); | 141 return rpc_handler_.invalid_audio_token_cache_.HasKey(token); |
| 135 } | 142 } |
| 136 | 143 |
| 137 // For rpc_handler_.invalid_audio_token_cache_ | 144 // For rpc_handler_.invalid_audio_token_cache_ |
| 138 base::MessageLoop message_loop_; | 145 base::MessageLoop message_loop_; |
| 139 | 146 |
| 147 scoped_ptr<WhispernetClient> whispernet_client_; |
| 140 FakeDirectiveHandler directive_handler_; | 148 FakeDirectiveHandler directive_handler_; |
| 141 RpcHandler rpc_handler_; | 149 RpcHandler rpc_handler_; |
| 150 |
| 142 CopresenceStatus status_; | 151 CopresenceStatus status_; |
| 143 | |
| 144 std::string rpc_name_; | 152 std::string rpc_name_; |
| 145 std::string api_key_; | 153 std::string api_key_; |
| 146 std::string auth_token_; | 154 std::string auth_token_; |
| 147 ScopedVector<MessageLite> request_protos_; | 155 ScopedVector<MessageLite> request_protos_; |
| 148 std::map<std::string, std::vector<Message>> messages_by_subscription_; | 156 std::map<std::string, std::vector<std::string>> messages_by_subscription_; |
| 149 | 157 |
| 150 private: | 158 private: |
| 151 void CaptureHttpPost( | 159 void CaptureHttpPost( |
| 152 net::URLRequestContextGetter* url_context_getter, | 160 net::URLRequestContextGetter* url_context_getter, |
| 153 const std::string& rpc_name, | 161 const std::string& rpc_name, |
| 154 const std::string& api_key, | 162 const std::string& api_key, |
| 155 const std::string& auth_token, | 163 const std::string& auth_token, |
| 156 scoped_ptr<MessageLite> request_proto, | 164 scoped_ptr<MessageLite> request_proto, |
| 157 const RpcHandler::PostCleanupCallback& response_callback) { | 165 const RpcHandler::PostCleanupCallback& response_callback) { |
| 158 rpc_name_ = rpc_name; | 166 rpc_name_ = rpc_name; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 | 273 |
| 266 messages_by_subscription_.clear(); | 274 messages_by_subscription_.clear(); |
| 267 std::string serialized_proto; | 275 std::string serialized_proto; |
| 268 ASSERT_TRUE(test_response.SerializeToString(&serialized_proto)); | 276 ASSERT_TRUE(test_response.SerializeToString(&serialized_proto)); |
| 269 status_ = FAIL; | 277 status_ = FAIL; |
| 270 InvokeReportResponseHandler(net::HTTP_OK, serialized_proto); | 278 InvokeReportResponseHandler(net::HTTP_OK, serialized_proto); |
| 271 | 279 |
| 272 EXPECT_EQ(SUCCESS, status_); | 280 EXPECT_EQ(SUCCESS, status_); |
| 273 EXPECT_TRUE(TokenIsInvalid("bad token")); | 281 EXPECT_TRUE(TokenIsInvalid("bad token")); |
| 274 | 282 |
| 275 EXPECT_THAT(messages_by_subscription_["Subscription 1"], ElementsAre( | 283 EXPECT_THAT(messages_by_subscription_["Subscription 1"], |
| 276 Property(&Message::payload, "Message A"), | 284 ElementsAre("Message A", "Message C")); |
| 277 Property(&Message::payload, "Message C"))); | 285 EXPECT_THAT(messages_by_subscription_["Subscription 2"], |
| 286 ElementsAre("Message B", "Message C")); |
| 278 | 287 |
| 279 EXPECT_THAT(messages_by_subscription_["Subscription 2"], ElementsAre( | 288 EXPECT_THAT(directive_handler_.added_directives(), |
| 280 Property(&Message::payload, "Message B"), | 289 ElementsAre("Subscription 1", "Subscription 2")); |
| 281 Property(&Message::payload, "Message C"))); | |
| 282 | |
| 283 EXPECT_THAT(directive_handler_.added_directives(), ElementsAre( | |
| 284 Property(&Directive::subscription_id, "Subscription 1"), | |
| 285 Property(&Directive::subscription_id, "Subscription 2"))); | |
| 286 } | 290 } |
| 287 | 291 |
| 288 } // namespace copresence | 292 } // namespace copresence |
| OLD | NEW |