OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/json/json_writer.h" |
| 6 #include "base/values.h" |
| 7 #include "chrome/browser/extensions/api/copresence/copresence_api.h" |
| 8 #include "chrome/browser/extensions/extension_api_unittest.h" |
| 9 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 10 #include "components/copresence/proto/data.pb.h" |
| 11 #include "components/copresence/proto/rpcs.pb.h" |
| 12 #include "components/copresence/public/copresence_manager.h" |
| 13 |
| 14 using base::ListValue; |
| 15 using copresence::BROADCAST_ONLY; |
| 16 using copresence::CopresenceDelegate; |
| 17 using copresence::CopresenceManager; |
| 18 using copresence::FAIL; |
| 19 using copresence::PublishedMessage; |
| 20 using copresence::ReportRequest; |
| 21 using copresence::SCAN_ONLY; |
| 22 using copresence::Subscription; |
| 23 using google::protobuf::RepeatedPtrField; |
| 24 |
| 25 namespace test_utils = extension_function_test_utils; |
| 26 |
| 27 namespace extensions { |
| 28 |
| 29 using api::copresence::Message; |
| 30 using api::copresence::Operation; |
| 31 using api::copresence::PublishOperation; |
| 32 using api::copresence::Strategy; |
| 33 using api::copresence::SubscribeOperation; |
| 34 using api::copresence::UnpublishOperation; |
| 35 using api::copresence::UnsubscribeOperation; |
| 36 |
| 37 |
| 38 PublishOperation* CreatePublish(const std::string& id) { |
| 39 PublishOperation* publish = new PublishOperation; |
| 40 |
| 41 publish->id = id; |
| 42 publish->time_to_live_millis.reset(new int(1000)); |
| 43 publish->message.type = "joke"; |
| 44 publish->message.payload = "Knock Knock!"; |
| 45 |
| 46 return publish; |
| 47 } |
| 48 |
| 49 SubscribeOperation* CreateSubscribe(const std::string& id) { |
| 50 SubscribeOperation* subscribe = new SubscribeOperation; |
| 51 |
| 52 subscribe->id = id; |
| 53 subscribe->time_to_live_millis.reset(new int(1000)); |
| 54 subscribe->filter.type = "joke"; |
| 55 |
| 56 return subscribe; |
| 57 } |
| 58 |
| 59 template <typename T> |
| 60 bool GetOnly(const RepeatedPtrField<T>& things, T* out) { |
| 61 if (things.size() != 1) |
| 62 return false; |
| 63 |
| 64 *out = things.Get(0); |
| 65 return true; |
| 66 } |
| 67 |
| 68 class MockCopresenceManager : public CopresenceManager { |
| 69 public: |
| 70 explicit MockCopresenceManager(CopresenceDelegate* delegate) |
| 71 : delegate_(delegate) {} |
| 72 virtual ~MockCopresenceManager() {} |
| 73 |
| 74 virtual void ExecuteReportRequest( |
| 75 ReportRequest request, |
| 76 const std::string& app_id, |
| 77 const copresence::StatusCallback& status_callback) OVERRIDE { |
| 78 request_ = request; |
| 79 app_id_ = app_id; |
| 80 status_callback.Run(copresence::SUCCESS); |
| 81 } |
| 82 |
| 83 CopresenceDelegate* delegate_; |
| 84 |
| 85 ReportRequest request_; |
| 86 std::string app_id_; |
| 87 }; |
| 88 |
| 89 class CopresenceApiUnittest : public ExtensionApiUnittest { |
| 90 public: |
| 91 CopresenceApiUnittest() {} |
| 92 virtual ~CopresenceApiUnittest() {} |
| 93 |
| 94 virtual void SetUp() OVERRIDE { |
| 95 ExtensionApiUnittest::SetUp(); |
| 96 |
| 97 CopresenceService* service = |
| 98 CopresenceService::GetFactoryInstance()->Get(profile()); |
| 99 copresence_manager_ = new MockCopresenceManager(service); |
| 100 service->set_manager_for_testing( |
| 101 make_scoped_ptr<CopresenceManager>(copresence_manager_)); |
| 102 } |
| 103 |
| 104 // Takes ownership of the operation_list. |
| 105 bool ExecuteOperations(ListValue* operation_list) { |
| 106 scoped_ptr<ListValue> args_list(new ListValue); |
| 107 args_list->Append(operation_list); |
| 108 |
| 109 scoped_refptr<UIThreadExtensionFunction> function = |
| 110 new CopresenceExecuteFunction; |
| 111 function->set_extension(extension()); |
| 112 function->set_browser_context(profile()); |
| 113 function->set_has_callback(true); |
| 114 test_utils::RunFunction( |
| 115 function.get(), args_list.Pass(), browser(), test_utils::NONE); |
| 116 return function->GetResultList(); |
| 117 } |
| 118 |
| 119 bool ExecuteOperation(scoped_ptr<Operation> operation) { |
| 120 ListValue* operation_list = new ListValue; |
| 121 operation_list->Append(operation->ToValue().release()); |
| 122 return ExecuteOperations(operation_list); |
| 123 } |
| 124 |
| 125 const ReportRequest& request_sent() const { |
| 126 return copresence_manager_->request_; |
| 127 } |
| 128 |
| 129 const std::string& app_id_sent() const { |
| 130 return copresence_manager_->app_id_; |
| 131 } |
| 132 |
| 133 void clear_app_id() { |
| 134 copresence_manager_->app_id_ = ""; |
| 135 } |
| 136 |
| 137 CopresenceDelegate* delegate() { |
| 138 return copresence_manager_->delegate_; |
| 139 } |
| 140 |
| 141 protected: |
| 142 MockCopresenceManager* copresence_manager_; |
| 143 }; |
| 144 |
| 145 TEST_F(CopresenceApiUnittest, Publish) { |
| 146 scoped_ptr<PublishOperation> publish(CreatePublish("pub")); |
| 147 publish->strategies.reset(new Strategy); |
| 148 publish->strategies->only_broadcast.reset(new bool(true)); // Default |
| 149 |
| 150 scoped_ptr<Operation> operation(new Operation); |
| 151 operation->publish = publish.Pass(); |
| 152 |
| 153 clear_app_id(); |
| 154 EXPECT_TRUE(ExecuteOperation(operation.Pass())); |
| 155 EXPECT_EQ(extension()->id(), app_id_sent()); |
| 156 |
| 157 PublishedMessage message; |
| 158 ASSERT_TRUE(GetOnly( |
| 159 request_sent().manage_messages_request().message_to_publish(), &message)); |
| 160 EXPECT_EQ("pub", message.id()); |
| 161 EXPECT_EQ(1000, message.access_policy().ttl_millis()); |
| 162 EXPECT_EQ(copresence::NO_ACL_CHECK, message.access_policy().acl().acl_type()); |
| 163 EXPECT_EQ("joke", message.message().type().type()); |
| 164 EXPECT_EQ("Knock Knock!", message.message().payload()); |
| 165 EXPECT_EQ(BROADCAST_ONLY, |
| 166 message.token_exchange_strategy().broadcast_scan_configuration()); |
| 167 } |
| 168 |
| 169 TEST_F(CopresenceApiUnittest, Subscribe) { |
| 170 scoped_ptr<SubscribeOperation> subscribe(CreateSubscribe("sub")); |
| 171 subscribe->strategies.reset(new Strategy); |
| 172 subscribe->strategies->only_broadcast.reset(new bool(true)); // Not default |
| 173 |
| 174 scoped_ptr<Operation> operation(new Operation); |
| 175 operation->subscribe = subscribe.Pass(); |
| 176 |
| 177 clear_app_id(); |
| 178 EXPECT_TRUE(ExecuteOperation(operation.Pass())); |
| 179 EXPECT_EQ(extension()->id(), app_id_sent()); |
| 180 |
| 181 Subscription subscription; |
| 182 ASSERT_TRUE(GetOnly( |
| 183 request_sent().manage_subscriptions_request().subscription(), |
| 184 &subscription)); |
| 185 EXPECT_EQ("sub", subscription.id()); |
| 186 EXPECT_EQ(1000, subscription.ttl_millis()); |
| 187 EXPECT_EQ("joke", subscription.message_type().type()); |
| 188 copresence::BroadcastScanConfiguration strategy = |
| 189 subscription.token_exchange_strategy().broadcast_scan_configuration(); |
| 190 EXPECT_EQ(BROADCAST_ONLY, strategy); |
| 191 } |
| 192 |
| 193 TEST_F(CopresenceApiUnittest, DefaultStrategies) { |
| 194 scoped_ptr<Operation> publish_operation(new Operation); |
| 195 publish_operation->publish.reset(CreatePublish("pub")); |
| 196 |
| 197 scoped_ptr<Operation> subscribe_operation(new Operation); |
| 198 subscribe_operation->subscribe.reset(CreateSubscribe("sub")); |
| 199 |
| 200 ListValue* operation_list = new ListValue; |
| 201 operation_list->Append(publish_operation->ToValue().release()); |
| 202 operation_list->Append(subscribe_operation->ToValue().release()); |
| 203 EXPECT_TRUE(ExecuteOperations(operation_list)); |
| 204 |
| 205 EXPECT_EQ(BROADCAST_ONLY, |
| 206 request_sent().manage_messages_request().message_to_publish(0) |
| 207 .token_exchange_strategy().broadcast_scan_configuration()); |
| 208 EXPECT_EQ(SCAN_ONLY, |
| 209 request_sent().manage_subscriptions_request().subscription(0) |
| 210 .token_exchange_strategy().broadcast_scan_configuration()); |
| 211 } |
| 212 |
| 213 TEST_F(CopresenceApiUnittest, UnPubSub) { |
| 214 // First we need to create a publish and a subscribe to cancel. |
| 215 scoped_ptr<Operation> publish_operation(new Operation); |
| 216 scoped_ptr<Operation> subscribe_operation(new Operation); |
| 217 publish_operation->publish.reset(CreatePublish("pub")); |
| 218 subscribe_operation->subscribe.reset(CreateSubscribe("sub")); |
| 219 ListValue* operation_list = new ListValue; |
| 220 operation_list->Append(publish_operation->ToValue().release()); |
| 221 operation_list->Append(subscribe_operation->ToValue().release()); |
| 222 EXPECT_TRUE(ExecuteOperations(operation_list)); |
| 223 |
| 224 scoped_ptr<Operation> unpublish_operation(new Operation); |
| 225 unpublish_operation->unpublish.reset(new UnpublishOperation); |
| 226 unpublish_operation->unpublish->unpublish_id = "pub"; |
| 227 |
| 228 scoped_ptr<Operation> unsubscribe_operation(new Operation); |
| 229 unsubscribe_operation->unsubscribe.reset(new UnsubscribeOperation); |
| 230 unsubscribe_operation->unsubscribe->unsubscribe_id = "sub"; |
| 231 |
| 232 operation_list = new ListValue; |
| 233 operation_list->Append(unpublish_operation->ToValue().release()); |
| 234 operation_list->Append(unsubscribe_operation->ToValue().release()); |
| 235 EXPECT_TRUE(ExecuteOperations(operation_list)); |
| 236 |
| 237 std::string unpublish_id; |
| 238 ASSERT_TRUE(GetOnly( |
| 239 request_sent().manage_messages_request().id_to_unpublish(), |
| 240 &unpublish_id)); |
| 241 EXPECT_EQ("pub", unpublish_id); |
| 242 |
| 243 std::string unsubscribe_id; |
| 244 ASSERT_TRUE(GetOnly( |
| 245 request_sent().manage_subscriptions_request().id_to_unsubscribe(), |
| 246 &unsubscribe_id)); |
| 247 EXPECT_EQ("sub", unsubscribe_id); |
| 248 } |
| 249 |
| 250 TEST_F(CopresenceApiUnittest, BadId) { |
| 251 scoped_ptr<Operation> unsubscribe_operation(new Operation); |
| 252 unsubscribe_operation->unsubscribe.reset(new UnsubscribeOperation); |
| 253 unsubscribe_operation->unsubscribe->unsubscribe_id = "invalid id"; |
| 254 |
| 255 EXPECT_FALSE(ExecuteOperation(unsubscribe_operation.Pass())); |
| 256 } |
| 257 |
| 258 TEST_F(CopresenceApiUnittest, MultipleOperations) { |
| 259 scoped_ptr<Operation> multi_operation(new Operation); |
| 260 multi_operation->publish.reset(CreatePublish("pub")); |
| 261 multi_operation->subscribe.reset(CreateSubscribe("sub")); |
| 262 |
| 263 EXPECT_FALSE(ExecuteOperation(multi_operation.Pass())); |
| 264 } |
| 265 |
| 266 } // namespace extensions |
OLD | NEW |