| 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 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 private: | 61 private: |
| 62 std::vector<Directive> added_directives_; | 62 std::vector<Directive> added_directives_; |
| 63 | 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(FakeDirectiveHandler); | 64 DISALLOW_COPY_AND_ASSIGN(FakeDirectiveHandler); |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 } // namespace | 67 } // namespace |
| 68 | 68 |
| 69 class RpcHandlerTest : public testing::Test, public CopresenceDelegate { | 69 class RpcHandlerTest : public testing::Test, public CopresenceDelegate { |
| 70 public: | 70 public: |
| 71 RpcHandlerTest() : rpc_handler_(this), status_(SUCCESS), api_key_("API key") { | 71 RpcHandlerTest() : rpc_handler_(this), status_(SUCCESS) { |
| 72 rpc_handler_.server_post_callback_ = | 72 rpc_handler_.server_post_callback_ = |
| 73 base::Bind(&RpcHandlerTest::CaptureHttpPost, base::Unretained(this)); | 73 base::Bind(&RpcHandlerTest::CaptureHttpPost, base::Unretained(this)); |
| 74 rpc_handler_.device_id_ = "Device ID"; | 74 rpc_handler_.device_id_ = "Device ID"; |
| 75 } | 75 } |
| 76 | 76 |
| 77 void CaptureHttpPost( | 77 void CaptureHttpPost( |
| 78 net::URLRequestContextGetter* url_context_getter, | 78 net::URLRequestContextGetter* url_context_getter, |
| 79 const std::string& rpc_name, | 79 const std::string& rpc_name, |
| 80 const std::string& app_id, |
| 80 scoped_ptr<MessageLite> request_proto, | 81 scoped_ptr<MessageLite> request_proto, |
| 81 const RpcHandler::PostCleanupCallback& response_callback) { | 82 const RpcHandler::PostCleanupCallback& response_callback) { |
| 82 rpc_name_ = rpc_name; | 83 rpc_name_ = rpc_name; |
| 84 app_id_ = app_id; |
| 83 request_proto_ = request_proto.Pass(); | 85 request_proto_ = request_proto.Pass(); |
| 84 } | 86 } |
| 85 | 87 |
| 86 void CaptureStatus(CopresenceStatus status) { | 88 void CaptureStatus(CopresenceStatus status) { |
| 87 status_ = status; | 89 status_ = status; |
| 88 } | 90 } |
| 89 | 91 |
| 90 inline const ReportRequest* GetReportSent() { | 92 inline const ReportRequest* GetReportSent() { |
| 91 return static_cast<ReportRequest*>(request_proto_.get()); | 93 return static_cast<ReportRequest*>(request_proto_.get()); |
| 92 } | 94 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 } | 148 } |
| 147 | 149 |
| 148 virtual net::URLRequestContextGetter* GetRequestContext() const override { | 150 virtual net::URLRequestContextGetter* GetRequestContext() const override { |
| 149 return NULL; | 151 return NULL; |
| 150 } | 152 } |
| 151 | 153 |
| 152 virtual const std::string GetPlatformVersionString() const override { | 154 virtual const std::string GetPlatformVersionString() const override { |
| 153 return kChromeVersion; | 155 return kChromeVersion; |
| 154 } | 156 } |
| 155 | 157 |
| 156 virtual const std::string GetAPIKey() const override { | 158 virtual const std::string GetAPIKey(const std::string& app_id) const |
| 157 return api_key_; | 159 override { |
| 160 NOTREACHED(); |
| 161 return ""; |
| 162 } |
| 163 |
| 164 virtual const std::string GetAuthToken(const std::string& app_id) const |
| 165 override { |
| 166 NOTREACHED(); |
| 167 return ""; |
| 158 } | 168 } |
| 159 | 169 |
| 160 virtual WhispernetClient* GetWhispernetClient() override { | 170 virtual WhispernetClient* GetWhispernetClient() override { |
| 161 return NULL; | 171 return NULL; |
| 162 } | 172 } |
| 163 | 173 |
| 164 protected: | 174 protected: |
| 165 // For rpc_handler_.invalid_audio_token_cache_ | 175 // For rpc_handler_.invalid_audio_token_cache_ |
| 166 base::MessageLoop message_loop_; | 176 base::MessageLoop message_loop_; |
| 167 | 177 |
| 168 RpcHandler rpc_handler_; | 178 RpcHandler rpc_handler_; |
| 169 CopresenceStatus status_; | 179 CopresenceStatus status_; |
| 170 std::string api_key_; | |
| 171 | 180 |
| 172 std::string rpc_name_; | 181 std::string rpc_name_; |
| 182 std::string app_id_; |
| 173 scoped_ptr<MessageLite> request_proto_; | 183 scoped_ptr<MessageLite> request_proto_; |
| 174 std::map<std::string, std::vector<Message>> messages_by_subscription_; | 184 std::map<std::string, std::vector<Message>> messages_by_subscription_; |
| 175 }; | 185 }; |
| 176 | 186 |
| 177 TEST_F(RpcHandlerTest, Initialize) { | 187 TEST_F(RpcHandlerTest, Initialize) { |
| 178 SetDeviceId(""); | 188 SetDeviceId(""); |
| 179 rpc_handler_.Initialize(RpcHandler::SuccessCallback()); | 189 rpc_handler_.Initialize(RpcHandler::SuccessCallback()); |
| 180 RegisterDeviceRequest* registration = | 190 RegisterDeviceRequest* registration = |
| 181 static_cast<RegisterDeviceRequest*>(request_proto_.get()); | 191 static_cast<RegisterDeviceRequest*>(request_proto_.get()); |
| 182 Identity identity = registration->device_identifiers().registrant(); | 192 Identity identity = registration->device_identifiers().registrant(); |
| 183 EXPECT_EQ(CHROME, identity.type()); | 193 EXPECT_EQ(CHROME, identity.type()); |
| 184 EXPECT_FALSE(identity.chrome_id().empty()); | 194 EXPECT_FALSE(identity.chrome_id().empty()); |
| 185 } | 195 } |
| 186 | 196 |
| 187 TEST_F(RpcHandlerTest, CreateRequestHeader) { | 197 TEST_F(RpcHandlerTest, CreateRequestHeader) { |
| 188 SetDeviceId("CreateRequestHeader Device ID"); | 198 SetDeviceId("CreateRequestHeader Device ID"); |
| 189 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest), | 199 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest), |
| 190 "CreateRequestHeader App ID", | 200 "CreateRequestHeader App ID", |
| 191 StatusCallback()); | 201 StatusCallback()); |
| 192 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); | 202 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); |
| 203 EXPECT_EQ("CreateRequestHeader App ID", app_id_); |
| 193 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); | 204 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); |
| 194 EXPECT_EQ(kChromeVersion, | 205 EXPECT_EQ(kChromeVersion, |
| 195 report->header().framework_version().version_name()); | 206 report->header().framework_version().version_name()); |
| 196 EXPECT_EQ("CreateRequestHeader App ID", | 207 EXPECT_EQ("CreateRequestHeader App ID", |
| 197 report->header().client_version().client()); | 208 report->header().client_version().client()); |
| 198 EXPECT_EQ("CreateRequestHeader Device ID", | 209 EXPECT_EQ("CreateRequestHeader Device ID", |
| 199 report->header().registered_device_id()); | 210 report->header().registered_device_id()); |
| 200 EXPECT_EQ(CHROME_PLATFORM_TYPE, | 211 EXPECT_EQ(CHROME_PLATFORM_TYPE, |
| 201 report->header().device_fingerprint().type()); | 212 report->header().device_fingerprint().type()); |
| 202 } | 213 } |
| 203 | 214 |
| 204 TEST_F(RpcHandlerTest, ReportTokens) { | 215 TEST_F(RpcHandlerTest, ReportTokens) { |
| 205 std::vector<AudioToken> test_tokens; | 216 std::vector<AudioToken> test_tokens; |
| 206 test_tokens.push_back(AudioToken("token 1", false)); | 217 test_tokens.push_back(AudioToken("token 1", false)); |
| 207 test_tokens.push_back(AudioToken("token 2", true)); | 218 test_tokens.push_back(AudioToken("token 2", true)); |
| 208 test_tokens.push_back(AudioToken("token 3", false)); | 219 test_tokens.push_back(AudioToken("token 3", false)); |
| 209 AddInvalidToken("token 2"); | 220 AddInvalidToken("token 2"); |
| 210 | 221 |
| 211 rpc_handler_.ReportTokens(test_tokens); | 222 rpc_handler_.ReportTokens(test_tokens); |
| 212 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); | 223 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); |
| 224 EXPECT_EQ("", app_id_); |
| 213 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); | 225 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); |
| 214 RepeatedPtrField<TokenObservation> tokens_sent = | 226 RepeatedPtrField<TokenObservation> tokens_sent = |
| 215 report->update_signals_request().token_observation(); | 227 report->update_signals_request().token_observation(); |
| 216 ASSERT_EQ(2, tokens_sent.size()); | 228 ASSERT_EQ(2, tokens_sent.size()); |
| 217 EXPECT_EQ("token 1", tokens_sent.Get(0).token_id()); | 229 EXPECT_EQ("token 1", tokens_sent.Get(0).token_id()); |
| 218 EXPECT_EQ("token 3", tokens_sent.Get(1).token_id()); | 230 EXPECT_EQ("token 3", tokens_sent.Get(1).token_id()); |
| 219 } | 231 } |
| 220 | 232 |
| 221 TEST_F(RpcHandlerTest, ReportResponseHandler) { | 233 TEST_F(RpcHandlerTest, ReportResponseHandler) { |
| 222 // Fail on HTTP status != 200. | 234 // Fail on HTTP status != 200. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 messages_by_subscription_["Subscription 2"][1].payload()); | 285 messages_by_subscription_["Subscription 2"][1].payload()); |
| 274 | 286 |
| 275 ASSERT_EQ(2U, directive_handler->added_directives().size()); | 287 ASSERT_EQ(2U, directive_handler->added_directives().size()); |
| 276 EXPECT_EQ("Subscription 1", | 288 EXPECT_EQ("Subscription 1", |
| 277 directive_handler->added_directives()[0].subscription_id()); | 289 directive_handler->added_directives()[0].subscription_id()); |
| 278 EXPECT_EQ("Subscription 2", | 290 EXPECT_EQ("Subscription 2", |
| 279 directive_handler->added_directives()[1].subscription_id()); | 291 directive_handler->added_directives()[1].subscription_id()); |
| 280 } | 292 } |
| 281 | 293 |
| 282 } // namespace copresence | 294 } // namespace copresence |
| OLD | NEW |