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 "chrome/browser/extensions/api/copresence/copresence_api.h" | 5 #include "chrome/browser/extensions/api/copresence/copresence_api.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/memory/ptr_util.h" |
10 #include "base/values.h" | 11 #include "base/values.h" |
11 #include "chrome/browser/extensions/extension_api_unittest.h" | 12 #include "chrome/browser/extensions/extension_api_unittest.h" |
12 #include "chrome/browser/extensions/extension_function_test_utils.h" | 13 #include "chrome/browser/extensions/extension_function_test_utils.h" |
13 #include "components/copresence/proto/data.pb.h" | 14 #include "components/copresence/proto/data.pb.h" |
14 #include "components/copresence/proto/rpcs.pb.h" | 15 #include "components/copresence/proto/rpcs.pb.h" |
15 #include "components/copresence/public/copresence_manager.h" | 16 #include "components/copresence/public/copresence_manager.h" |
16 | 17 |
17 using base::ListValue; | 18 using base::ListValue; |
18 using copresence::AUDIO_CONFIGURATION_AUDIBLE; | 19 using copresence::AUDIO_CONFIGURATION_AUDIBLE; |
19 using copresence::AUDIO_CONFIGURATION_UNKNOWN; | 20 using copresence::AUDIO_CONFIGURATION_UNKNOWN; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 CopresenceApiUnittest() {} | 104 CopresenceApiUnittest() {} |
104 ~CopresenceApiUnittest() override {} | 105 ~CopresenceApiUnittest() override {} |
105 | 106 |
106 void SetUp() override { | 107 void SetUp() override { |
107 ExtensionApiUnittest::SetUp(); | 108 ExtensionApiUnittest::SetUp(); |
108 | 109 |
109 CopresenceService* service = | 110 CopresenceService* service = |
110 CopresenceService::GetFactoryInstance()->Get(profile()); | 111 CopresenceService::GetFactoryInstance()->Get(profile()); |
111 copresence_manager_ = new FakeCopresenceManager(service); | 112 copresence_manager_ = new FakeCopresenceManager(service); |
112 service->set_manager_for_testing( | 113 service->set_manager_for_testing( |
113 make_scoped_ptr<CopresenceManager>(copresence_manager_)); | 114 base::WrapUnique<CopresenceManager>(copresence_manager_)); |
114 } | 115 } |
115 | 116 |
116 // Takes ownership of the operation_list. | 117 // Takes ownership of the operation_list. |
117 bool ExecuteOperations(ListValue* operation_list) { | 118 bool ExecuteOperations(ListValue* operation_list) { |
118 scoped_ptr<ListValue> args_list(new ListValue); | 119 std::unique_ptr<ListValue> args_list(new ListValue); |
119 args_list->Append(operation_list); | 120 args_list->Append(operation_list); |
120 | 121 |
121 scoped_refptr<UIThreadExtensionFunction> function = | 122 scoped_refptr<UIThreadExtensionFunction> function = |
122 new CopresenceExecuteFunction; | 123 new CopresenceExecuteFunction; |
123 function->set_extension(extension()); | 124 function->set_extension(extension()); |
124 function->set_browser_context(profile()); | 125 function->set_browser_context(profile()); |
125 function->set_has_callback(true); | 126 function->set_has_callback(true); |
126 test_utils::RunFunction(function.get(), std::move(args_list), browser(), | 127 test_utils::RunFunction(function.get(), std::move(args_list), browser(), |
127 test_utils::NONE); | 128 test_utils::NONE); |
128 return function->GetResultList(); | 129 return function->GetResultList(); |
129 } | 130 } |
130 | 131 |
131 bool ExecuteOperation(scoped_ptr<Operation> operation) { | 132 bool ExecuteOperation(std::unique_ptr<Operation> operation) { |
132 ListValue* operation_list = new ListValue; | 133 ListValue* operation_list = new ListValue; |
133 operation_list->Append(operation->ToValue().release()); | 134 operation_list->Append(operation->ToValue().release()); |
134 return ExecuteOperations(operation_list); | 135 return ExecuteOperations(operation_list); |
135 } | 136 } |
136 | 137 |
137 const ReportRequest& request_sent() const { | 138 const ReportRequest& request_sent() const { |
138 return copresence_manager_->request_; | 139 return copresence_manager_->request_; |
139 } | 140 } |
140 | 141 |
141 const std::string& app_id_sent() const { | 142 const std::string& app_id_sent() const { |
142 return copresence_manager_->app_id_; | 143 return copresence_manager_->app_id_; |
143 } | 144 } |
144 | 145 |
145 void clear_app_id() { | 146 void clear_app_id() { |
146 copresence_manager_->app_id_.clear(); | 147 copresence_manager_->app_id_.clear(); |
147 } | 148 } |
148 | 149 |
149 CopresenceDelegate* delegate() { | 150 CopresenceDelegate* delegate() { |
150 return copresence_manager_->delegate_; | 151 return copresence_manager_->delegate_; |
151 } | 152 } |
152 | 153 |
153 protected: | 154 protected: |
154 FakeCopresenceManager* copresence_manager_; | 155 FakeCopresenceManager* copresence_manager_; |
155 }; | 156 }; |
156 | 157 |
157 TEST_F(CopresenceApiUnittest, Publish) { | 158 TEST_F(CopresenceApiUnittest, Publish) { |
158 scoped_ptr<PublishOperation> publish(CreatePublish("pub")); | 159 std::unique_ptr<PublishOperation> publish(CreatePublish("pub")); |
159 publish->strategies.reset(new Strategy); | 160 publish->strategies.reset(new Strategy); |
160 publish->strategies->only_broadcast.reset(new bool(true)); // Default | 161 publish->strategies->only_broadcast.reset(new bool(true)); // Default |
161 | 162 |
162 scoped_ptr<Operation> operation(new Operation); | 163 std::unique_ptr<Operation> operation(new Operation); |
163 operation->publish = std::move(publish); | 164 operation->publish = std::move(publish); |
164 | 165 |
165 clear_app_id(); | 166 clear_app_id(); |
166 EXPECT_TRUE(ExecuteOperation(std::move(operation))); | 167 EXPECT_TRUE(ExecuteOperation(std::move(operation))); |
167 EXPECT_EQ(extension()->id(), app_id_sent()); | 168 EXPECT_EQ(extension()->id(), app_id_sent()); |
168 | 169 |
169 PublishedMessage message; | 170 PublishedMessage message; |
170 ASSERT_TRUE(GetOnly( | 171 ASSERT_TRUE(GetOnly( |
171 request_sent().manage_messages_request().message_to_publish(), &message)); | 172 request_sent().manage_messages_request().message_to_publish(), &message)); |
172 EXPECT_EQ("pub", message.id()); | 173 EXPECT_EQ("pub", message.id()); |
173 EXPECT_EQ(1000, message.access_policy().ttl_millis()); | 174 EXPECT_EQ(1000, message.access_policy().ttl_millis()); |
174 EXPECT_EQ(copresence::NO_ACL_CHECK, message.access_policy().acl().acl_type()); | 175 EXPECT_EQ(copresence::NO_ACL_CHECK, message.access_policy().acl().acl_type()); |
175 EXPECT_EQ("joke", message.message().type().type()); | 176 EXPECT_EQ("joke", message.message().type().type()); |
176 EXPECT_EQ("Knock Knock!", message.message().payload()); | 177 EXPECT_EQ("Knock Knock!", message.message().payload()); |
177 EXPECT_EQ(BROADCAST_ONLY, | 178 EXPECT_EQ(BROADCAST_ONLY, |
178 message.token_exchange_strategy().broadcast_scan_configuration()); | 179 message.token_exchange_strategy().broadcast_scan_configuration()); |
179 EXPECT_EQ(AUDIO_CONFIGURATION_UNKNOWN, | 180 EXPECT_EQ(AUDIO_CONFIGURATION_UNKNOWN, |
180 message.token_exchange_strategy().audio_configuration()); | 181 message.token_exchange_strategy().audio_configuration()); |
181 } | 182 } |
182 | 183 |
183 TEST_F(CopresenceApiUnittest, Subscribe) { | 184 TEST_F(CopresenceApiUnittest, Subscribe) { |
184 scoped_ptr<SubscribeOperation> subscribe(CreateSubscribe("sub")); | 185 std::unique_ptr<SubscribeOperation> subscribe(CreateSubscribe("sub")); |
185 subscribe->strategies.reset(new Strategy); | 186 subscribe->strategies.reset(new Strategy); |
186 subscribe->strategies->only_broadcast.reset(new bool(true)); // Not default | 187 subscribe->strategies->only_broadcast.reset(new bool(true)); // Not default |
187 subscribe->strategies->audible.reset(new bool(true)); // Not default | 188 subscribe->strategies->audible.reset(new bool(true)); // Not default |
188 | 189 |
189 scoped_ptr<Operation> operation(new Operation); | 190 std::unique_ptr<Operation> operation(new Operation); |
190 operation->subscribe = std::move(subscribe); | 191 operation->subscribe = std::move(subscribe); |
191 | 192 |
192 clear_app_id(); | 193 clear_app_id(); |
193 EXPECT_TRUE(ExecuteOperation(std::move(operation))); | 194 EXPECT_TRUE(ExecuteOperation(std::move(operation))); |
194 EXPECT_EQ(extension()->id(), app_id_sent()); | 195 EXPECT_EQ(extension()->id(), app_id_sent()); |
195 | 196 |
196 Subscription subscription; | 197 Subscription subscription; |
197 ASSERT_TRUE(GetOnly( | 198 ASSERT_TRUE(GetOnly( |
198 request_sent().manage_subscriptions_request().subscription(), | 199 request_sent().manage_subscriptions_request().subscription(), |
199 &subscription)); | 200 &subscription)); |
200 EXPECT_EQ("sub", subscription.id()); | 201 EXPECT_EQ("sub", subscription.id()); |
201 EXPECT_EQ(1000, subscription.ttl_millis()); | 202 EXPECT_EQ(1000, subscription.ttl_millis()); |
202 EXPECT_EQ("joke", subscription.message_type().type()); | 203 EXPECT_EQ("joke", subscription.message_type().type()); |
203 copresence::BroadcastScanConfiguration broadcast_scan = | 204 copresence::BroadcastScanConfiguration broadcast_scan = |
204 subscription.token_exchange_strategy().broadcast_scan_configuration(); | 205 subscription.token_exchange_strategy().broadcast_scan_configuration(); |
205 EXPECT_EQ(BROADCAST_ONLY, broadcast_scan); | 206 EXPECT_EQ(BROADCAST_ONLY, broadcast_scan); |
206 EXPECT_EQ(AUDIO_CONFIGURATION_AUDIBLE, | 207 EXPECT_EQ(AUDIO_CONFIGURATION_AUDIBLE, |
207 subscription.token_exchange_strategy().audio_configuration()); | 208 subscription.token_exchange_strategy().audio_configuration()); |
208 } | 209 } |
209 | 210 |
210 TEST_F(CopresenceApiUnittest, DefaultStrategies) { | 211 TEST_F(CopresenceApiUnittest, DefaultStrategies) { |
211 scoped_ptr<Operation> publish_operation(new Operation); | 212 std::unique_ptr<Operation> publish_operation(new Operation); |
212 publish_operation->publish.reset(CreatePublish("pub")); | 213 publish_operation->publish.reset(CreatePublish("pub")); |
213 | 214 |
214 scoped_ptr<Operation> subscribe_operation(new Operation); | 215 std::unique_ptr<Operation> subscribe_operation(new Operation); |
215 subscribe_operation->subscribe.reset(CreateSubscribe("sub")); | 216 subscribe_operation->subscribe.reset(CreateSubscribe("sub")); |
216 | 217 |
217 ListValue* operation_list = new ListValue; | 218 ListValue* operation_list = new ListValue; |
218 operation_list->Append(publish_operation->ToValue().release()); | 219 operation_list->Append(publish_operation->ToValue().release()); |
219 operation_list->Append(subscribe_operation->ToValue().release()); | 220 operation_list->Append(subscribe_operation->ToValue().release()); |
220 EXPECT_TRUE(ExecuteOperations(operation_list)); | 221 EXPECT_TRUE(ExecuteOperations(operation_list)); |
221 | 222 |
222 EXPECT_EQ(BROADCAST_ONLY, | 223 EXPECT_EQ(BROADCAST_ONLY, |
223 request_sent().manage_messages_request().message_to_publish(0) | 224 request_sent().manage_messages_request().message_to_publish(0) |
224 .token_exchange_strategy().broadcast_scan_configuration()); | 225 .token_exchange_strategy().broadcast_scan_configuration()); |
225 EXPECT_EQ(SCAN_ONLY, | 226 EXPECT_EQ(SCAN_ONLY, |
226 request_sent().manage_subscriptions_request().subscription(0) | 227 request_sent().manage_subscriptions_request().subscription(0) |
227 .token_exchange_strategy().broadcast_scan_configuration()); | 228 .token_exchange_strategy().broadcast_scan_configuration()); |
228 } | 229 } |
229 | 230 |
230 TEST_F(CopresenceApiUnittest, LowPowerStrategy) { | 231 TEST_F(CopresenceApiUnittest, LowPowerStrategy) { |
231 scoped_ptr<Operation> subscribe_operation(new Operation); | 232 std::unique_ptr<Operation> subscribe_operation(new Operation); |
232 subscribe_operation->subscribe.reset(CreateSubscribe("sub")); | 233 subscribe_operation->subscribe.reset(CreateSubscribe("sub")); |
233 subscribe_operation->subscribe->strategies.reset(new Strategy); | 234 subscribe_operation->subscribe->strategies.reset(new Strategy); |
234 subscribe_operation->subscribe->strategies->low_power.reset(new bool(true)); | 235 subscribe_operation->subscribe->strategies->low_power.reset(new bool(true)); |
235 | 236 |
236 ListValue* operation_list = new ListValue; | 237 ListValue* operation_list = new ListValue; |
237 operation_list->Append(subscribe_operation->ToValue().release()); | 238 operation_list->Append(subscribe_operation->ToValue().release()); |
238 EXPECT_TRUE(ExecuteOperations(operation_list)); | 239 EXPECT_TRUE(ExecuteOperations(operation_list)); |
239 | 240 |
240 EXPECT_EQ(copresence::BROADCAST_SCAN_CONFIGURATION_UNKNOWN, | 241 EXPECT_EQ(copresence::BROADCAST_SCAN_CONFIGURATION_UNKNOWN, |
241 request_sent().manage_subscriptions_request().subscription(0) | 242 request_sent().manage_subscriptions_request().subscription(0) |
242 .token_exchange_strategy().broadcast_scan_configuration()); | 243 .token_exchange_strategy().broadcast_scan_configuration()); |
243 } | 244 } |
244 | 245 |
245 TEST_F(CopresenceApiUnittest, UnPubSub) { | 246 TEST_F(CopresenceApiUnittest, UnPubSub) { |
246 // First we need to create a publish and a subscribe to cancel. | 247 // First we need to create a publish and a subscribe to cancel. |
247 scoped_ptr<Operation> publish_operation(new Operation); | 248 std::unique_ptr<Operation> publish_operation(new Operation); |
248 scoped_ptr<Operation> subscribe_operation(new Operation); | 249 std::unique_ptr<Operation> subscribe_operation(new Operation); |
249 publish_operation->publish.reset(CreatePublish("pub")); | 250 publish_operation->publish.reset(CreatePublish("pub")); |
250 subscribe_operation->subscribe.reset(CreateSubscribe("sub")); | 251 subscribe_operation->subscribe.reset(CreateSubscribe("sub")); |
251 ListValue* operation_list = new ListValue; | 252 ListValue* operation_list = new ListValue; |
252 operation_list->Append(publish_operation->ToValue().release()); | 253 operation_list->Append(publish_operation->ToValue().release()); |
253 operation_list->Append(subscribe_operation->ToValue().release()); | 254 operation_list->Append(subscribe_operation->ToValue().release()); |
254 EXPECT_TRUE(ExecuteOperations(operation_list)); | 255 EXPECT_TRUE(ExecuteOperations(operation_list)); |
255 | 256 |
256 scoped_ptr<Operation> unpublish_operation(new Operation); | 257 std::unique_ptr<Operation> unpublish_operation(new Operation); |
257 unpublish_operation->unpublish.reset(new UnpublishOperation); | 258 unpublish_operation->unpublish.reset(new UnpublishOperation); |
258 unpublish_operation->unpublish->unpublish_id = "pub"; | 259 unpublish_operation->unpublish->unpublish_id = "pub"; |
259 | 260 |
260 scoped_ptr<Operation> unsubscribe_operation(new Operation); | 261 std::unique_ptr<Operation> unsubscribe_operation(new Operation); |
261 unsubscribe_operation->unsubscribe.reset(new UnsubscribeOperation); | 262 unsubscribe_operation->unsubscribe.reset(new UnsubscribeOperation); |
262 unsubscribe_operation->unsubscribe->unsubscribe_id = "sub"; | 263 unsubscribe_operation->unsubscribe->unsubscribe_id = "sub"; |
263 | 264 |
264 operation_list = new ListValue; | 265 operation_list = new ListValue; |
265 operation_list->Append(unpublish_operation->ToValue().release()); | 266 operation_list->Append(unpublish_operation->ToValue().release()); |
266 operation_list->Append(unsubscribe_operation->ToValue().release()); | 267 operation_list->Append(unsubscribe_operation->ToValue().release()); |
267 EXPECT_TRUE(ExecuteOperations(operation_list)); | 268 EXPECT_TRUE(ExecuteOperations(operation_list)); |
268 | 269 |
269 std::string unpublish_id; | 270 std::string unpublish_id; |
270 ASSERT_TRUE(GetOnly( | 271 ASSERT_TRUE(GetOnly( |
271 request_sent().manage_messages_request().id_to_unpublish(), | 272 request_sent().manage_messages_request().id_to_unpublish(), |
272 &unpublish_id)); | 273 &unpublish_id)); |
273 EXPECT_EQ("pub", unpublish_id); | 274 EXPECT_EQ("pub", unpublish_id); |
274 | 275 |
275 std::string unsubscribe_id; | 276 std::string unsubscribe_id; |
276 ASSERT_TRUE(GetOnly( | 277 ASSERT_TRUE(GetOnly( |
277 request_sent().manage_subscriptions_request().id_to_unsubscribe(), | 278 request_sent().manage_subscriptions_request().id_to_unsubscribe(), |
278 &unsubscribe_id)); | 279 &unsubscribe_id)); |
279 EXPECT_EQ("sub", unsubscribe_id); | 280 EXPECT_EQ("sub", unsubscribe_id); |
280 } | 281 } |
281 | 282 |
282 TEST_F(CopresenceApiUnittest, BadId) { | 283 TEST_F(CopresenceApiUnittest, BadId) { |
283 scoped_ptr<Operation> unsubscribe_operation(new Operation); | 284 std::unique_ptr<Operation> unsubscribe_operation(new Operation); |
284 unsubscribe_operation->unsubscribe.reset(new UnsubscribeOperation); | 285 unsubscribe_operation->unsubscribe.reset(new UnsubscribeOperation); |
285 unsubscribe_operation->unsubscribe->unsubscribe_id = "invalid id"; | 286 unsubscribe_operation->unsubscribe->unsubscribe_id = "invalid id"; |
286 | 287 |
287 EXPECT_FALSE(ExecuteOperation(std::move(unsubscribe_operation))); | 288 EXPECT_FALSE(ExecuteOperation(std::move(unsubscribe_operation))); |
288 } | 289 } |
289 | 290 |
290 TEST_F(CopresenceApiUnittest, MultipleOperations) { | 291 TEST_F(CopresenceApiUnittest, MultipleOperations) { |
291 scoped_ptr<Operation> multi_operation(new Operation); | 292 std::unique_ptr<Operation> multi_operation(new Operation); |
292 multi_operation->publish.reset(CreatePublish("pub")); | 293 multi_operation->publish.reset(CreatePublish("pub")); |
293 multi_operation->subscribe.reset(CreateSubscribe("sub")); | 294 multi_operation->subscribe.reset(CreateSubscribe("sub")); |
294 | 295 |
295 EXPECT_FALSE(ExecuteOperation(std::move(multi_operation))); | 296 EXPECT_FALSE(ExecuteOperation(std::move(multi_operation))); |
296 } | 297 } |
297 | 298 |
298 } // namespace extensions | 299 } // namespace extensions |
299 | 300 |
300 // TODO(ckehoe): add tests for auth tokens and api key functionality | 301 // TODO(ckehoe): add tests for auth tokens and api key functionality |
OLD | NEW |