OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
not at google - send to devlin
2014/08/15 22:17:03
2014.
And this file should probably be "unittest"
Charlie
2014/08/15 23:46:40
Done.
| |
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_apitest.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::CopresenceManager; | |
17 using copresence::CopresenceManagerDelegate; | |
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(CopresenceManagerDelegate* 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 CopresenceManagerDelegate* delegate_; | |
84 | |
85 ReportRequest request_; | |
86 std::string app_id_; | |
87 }; | |
88 | |
89 class CopresenceApiTest : public ExtensionApiTest { | |
not at google - send to devlin
2014/08/15 22:17:03
ApiTest is still a browser tests which requires sp
Charlie
2014/08/15 23:46:40
Done. And they run much faster now! Thanks.
| |
90 public: | |
91 CopresenceApiTest() {} | |
92 virtual ~CopresenceApiTest() {} | |
93 | |
94 virtual void SetUpOnMainThread() OVERRIDE { | |
95 test_extension_ = test_utils::CreateEmptyExtension(); | |
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 void ExecuteOperations(ListValue* operation_list, bool expect_error) { | |
106 ListValue args_list; | |
107 std::string json_args; | |
108 args_list.Append(operation_list); | |
109 base::JSONWriter::Write(&args_list, &json_args); | |
110 | |
111 scoped_refptr<UIThreadExtensionFunction> function = | |
112 new CopresenceExecuteFunction; | |
113 function->set_extension(test_extension_.get()); | |
114 function->set_browser_context(profile()); | |
115 if (expect_error) { | |
116 test_utils::RunFunctionAndReturnError( | |
117 function.get(), json_args, browser()); | |
118 } else { | |
119 test_utils::RunFunction( | |
120 function.get(), json_args, browser(), test_utils::NONE); | |
121 } | |
122 } | |
123 | |
124 void ExecuteOperation(scoped_ptr<Operation> operation, bool expect_error) { | |
125 ListValue* operation_list = new ListValue; | |
126 operation_list->Append(operation->ToValue().release()); | |
127 ExecuteOperations(operation_list, expect_error); | |
128 } | |
129 | |
130 const ReportRequest& request_sent() const { | |
131 return copresence_manager_->request_; | |
132 } | |
133 | |
134 const std::string& app_id_sent() const { | |
135 return copresence_manager_->app_id_; | |
136 } | |
137 | |
138 void clear_app_id() { | |
139 copresence_manager_->app_id_ = ""; | |
140 } | |
141 | |
142 CopresenceManagerDelegate* delegate() { | |
143 return copresence_manager_->delegate_; | |
144 } | |
145 | |
146 protected: | |
147 scoped_refptr<Extension> test_extension_; | |
148 MockCopresenceManager* copresence_manager_; | |
149 }; | |
150 | |
151 IN_PROC_BROWSER_TEST_F(CopresenceApiTest, Publish) { | |
152 scoped_ptr<PublishOperation> publish(CreatePublish("pub")); | |
153 publish->strategies.reset(new Strategy); | |
154 publish->strategies->only_broadcast.reset(new bool(true)); // Default | |
155 | |
156 scoped_ptr<Operation> operation(new Operation); | |
157 operation->publish = publish.Pass(); | |
158 | |
159 clear_app_id(); | |
160 ExecuteOperation(operation.Pass(), false); | |
161 EXPECT_EQ(test_extension_->id(), app_id_sent()); | |
162 | |
163 PublishedMessage message; | |
164 ASSERT_TRUE(GetOnly( | |
165 request_sent().manage_messages_request().message_to_publish(), &message)); | |
166 EXPECT_EQ("pub", message.id()); | |
167 EXPECT_EQ(1000, message.access_policy().ttl_millis()); | |
168 EXPECT_EQ(copresence::NO_ACL_CHECK, message.access_policy().acl().acl_type()); | |
169 EXPECT_EQ("joke", message.message().type().type()); | |
170 EXPECT_EQ("Knock Knock!", message.message().payload()); | |
171 EXPECT_EQ(BROADCAST_ONLY, | |
172 message.token_exchange_strategy().broadcast_scan_configuration()); | |
173 } | |
174 | |
175 IN_PROC_BROWSER_TEST_F(CopresenceApiTest, Subscribe) { | |
176 scoped_ptr<SubscribeOperation> subscribe(CreateSubscribe("sub")); | |
177 subscribe->strategies.reset(new Strategy); | |
178 subscribe->strategies->only_broadcast.reset(new bool(true)); // Not default | |
179 | |
180 scoped_ptr<Operation> operation(new Operation); | |
181 operation->subscribe = subscribe.Pass(); | |
182 | |
183 clear_app_id(); | |
184 ExecuteOperation(operation.Pass(), false); | |
185 EXPECT_EQ(test_extension_->id(), app_id_sent()); | |
186 | |
187 Subscription subscription; | |
188 ASSERT_TRUE(GetOnly( | |
189 request_sent().manage_subscriptions_request().subscription(), | |
190 &subscription)); | |
191 EXPECT_EQ("sub", subscription.id()); | |
192 EXPECT_EQ(1000, subscription.ttl_millis()); | |
193 EXPECT_EQ("joke", subscription.message_type().type()); | |
194 copresence::BroadcastScanConfiguration strategy = | |
195 subscription.token_exchange_strategy().broadcast_scan_configuration(); | |
196 EXPECT_EQ(BROADCAST_ONLY, strategy); | |
197 } | |
198 | |
199 IN_PROC_BROWSER_TEST_F(CopresenceApiTest, DefaultStrategies) { | |
200 scoped_ptr<Operation> publishOperation(new Operation); | |
201 publishOperation->publish.reset(CreatePublish("pub")); | |
202 | |
203 scoped_ptr<Operation> subscribeOperation(new Operation); | |
204 subscribeOperation->subscribe.reset(CreateSubscribe("sub")); | |
205 | |
206 ListValue* operation_list = new ListValue; | |
207 operation_list->Append(publishOperation->ToValue().release()); | |
208 operation_list->Append(subscribeOperation->ToValue().release()); | |
209 ExecuteOperations(operation_list, false); | |
210 | |
211 EXPECT_EQ(BROADCAST_ONLY, | |
212 request_sent().manage_messages_request().message_to_publish(0) | |
213 .token_exchange_strategy().broadcast_scan_configuration()); | |
214 EXPECT_EQ(SCAN_ONLY, | |
215 request_sent().manage_subscriptions_request().subscription(0) | |
216 .token_exchange_strategy().broadcast_scan_configuration()); | |
217 } | |
218 | |
219 IN_PROC_BROWSER_TEST_F(CopresenceApiTest, UnPubSub) { | |
220 // First we need to create a publish and a subscribe to cancel. | |
221 scoped_ptr<Operation> publishOperation(new Operation); | |
222 scoped_ptr<Operation> subscribeOperation(new Operation); | |
223 publishOperation->publish.reset(CreatePublish("pub")); | |
224 subscribeOperation->subscribe.reset(CreateSubscribe("sub")); | |
225 ListValue* operation_list = new ListValue; | |
226 operation_list->Append(publishOperation->ToValue().release()); | |
227 operation_list->Append(subscribeOperation->ToValue().release()); | |
228 ExecuteOperations(operation_list, false); | |
229 | |
230 scoped_ptr<Operation> unpublishOperation(new Operation); | |
231 unpublishOperation->unpublish.reset(new UnpublishOperation); | |
232 unpublishOperation->unpublish->unpublish_id = "pub"; | |
233 | |
234 scoped_ptr<Operation> unsubscribeOperation(new Operation); | |
235 unsubscribeOperation->unsubscribe.reset(new UnsubscribeOperation); | |
236 unsubscribeOperation->unsubscribe->unsubscribe_id = "sub"; | |
237 | |
238 operation_list = new ListValue; | |
239 operation_list->Append(unpublishOperation->ToValue().release()); | |
240 operation_list->Append(unsubscribeOperation->ToValue().release()); | |
241 ExecuteOperations(operation_list, false); | |
242 | |
243 std::string unpublish_id; | |
244 ASSERT_TRUE(GetOnly( | |
245 request_sent().manage_messages_request().id_to_unpublish(), | |
246 &unpublish_id)); | |
247 EXPECT_EQ("pub", unpublish_id); | |
248 | |
249 std::string unsubscribe_id; | |
250 ASSERT_TRUE(GetOnly( | |
251 request_sent().manage_subscriptions_request().id_to_unsubscribe(), | |
252 &unsubscribe_id)); | |
253 EXPECT_EQ("sub", unsubscribe_id); | |
254 } | |
255 | |
256 IN_PROC_BROWSER_TEST_F(CopresenceApiTest, BadId) { | |
257 scoped_ptr<Operation> unsubscribeOperation(new Operation); | |
258 unsubscribeOperation->unsubscribe.reset(new UnsubscribeOperation); | |
259 unsubscribeOperation->unsubscribe->unsubscribe_id = "invalid id"; | |
260 | |
261 ExecuteOperation(unsubscribeOperation.Pass(), true); | |
262 } | |
263 | |
264 IN_PROC_BROWSER_TEST_F(CopresenceApiTest, MultipleOperations) { | |
265 scoped_ptr<Operation> multiOperation(new Operation); | |
not at google - send to devlin
2014/08/15 22:17:03
multiOperation -> multi_operation.
There are othe
Charlie
2014/08/15 23:46:40
Fixed the ones I could find, at least.
| |
266 multiOperation->publish.reset(CreatePublish("pub")); | |
267 multiOperation->subscribe.reset(CreateSubscribe("sub")); | |
268 | |
269 ExecuteOperation(multiOperation.Pass(), true); | |
270 } | |
271 | |
272 } // namespace extensions | |
OLD | NEW |