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