OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
7 #include "ppapi/c/pp_errors.h" | 7 #include "ppapi/c/pp_errors.h" |
8 #include "ppapi/proxy/connection.h" | 8 #include "ppapi/proxy/connection.h" |
9 #include "ppapi/proxy/device_enumeration_resource_helper.h" | 9 #include "ppapi/proxy/device_enumeration_resource_helper.h" |
10 #include "ppapi/proxy/plugin_resource.h" | 10 #include "ppapi/proxy/plugin_resource.h" |
11 #include "ppapi/proxy/plugin_resource_tracker.h" | 11 #include "ppapi/proxy/plugin_resource_tracker.h" |
12 #include "ppapi/proxy/plugin_var_tracker.h" | 12 #include "ppapi/proxy/plugin_var_tracker.h" |
13 #include "ppapi/proxy/ppapi_message_utils.h" | 13 #include "ppapi/proxy/ppapi_message_utils.h" |
14 #include "ppapi/proxy/ppapi_messages.h" | 14 #include "ppapi/proxy/ppapi_messages.h" |
15 #include "ppapi/proxy/ppapi_proxy_test.h" | 15 #include "ppapi/proxy/ppapi_proxy_test.h" |
16 #include "ppapi/shared_impl/ppb_device_ref_shared.h" | 16 #include "ppapi/shared_impl/ppb_device_ref_shared.h" |
| 17 #include "ppapi/shared_impl/proxy_lock.h" |
17 #include "ppapi/shared_impl/var.h" | 18 #include "ppapi/shared_impl/var.h" |
18 #include "ppapi/thunk/enter.h" | 19 #include "ppapi/thunk/enter.h" |
19 #include "ppapi/thunk/ppb_device_ref_api.h" | 20 #include "ppapi/thunk/ppb_device_ref_api.h" |
20 #include "ppapi/thunk/thunk.h" | 21 #include "ppapi/thunk/thunk.h" |
21 | 22 |
22 namespace ppapi { | 23 namespace ppapi { |
23 namespace proxy { | 24 namespace proxy { |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 typedef PluginProxyTest DeviceEnumerationResourceHelperTest; | 28 typedef PluginProxyTest DeviceEnumerationResourceHelperTest; |
28 | 29 |
29 Connection GetConnection(PluginProxyTestHarness* harness) { | 30 Connection GetConnection(PluginProxyTestHarness* harness) { |
30 CHECK(harness->GetGlobals()->IsPluginGlobals()); | 31 CHECK(harness->GetGlobals()->IsPluginGlobals()); |
31 | 32 |
32 return Connection( | 33 return Connection( |
33 static_cast<PluginGlobals*>(harness->GetGlobals())->GetBrowserSender(), | 34 static_cast<PluginGlobals*>(harness->GetGlobals())->GetBrowserSender(), |
34 harness->plugin_dispatcher()); | 35 harness->plugin_dispatcher()); |
35 } | 36 } |
36 | 37 |
37 bool CompareDeviceRef(PluginVarTracker* var_tracker, | 38 bool CompareDeviceRef(PluginVarTracker* var_tracker, |
38 PP_Resource resource, | 39 PP_Resource resource, |
39 const DeviceRefData& expected) { | 40 const DeviceRefData& expected) { |
40 thunk::EnterResource<thunk::PPB_DeviceRef_API> enter(resource, true); | 41 thunk::EnterResourceNoLock<thunk::PPB_DeviceRef_API> enter(resource, true); |
41 if (enter.failed()) | 42 if (enter.failed()) |
42 return false; | 43 return false; |
43 | 44 |
44 if (expected.type != enter.object()->GetType()) | 45 if (expected.type != enter.object()->GetType()) |
45 return false; | 46 return false; |
46 | 47 |
47 PP_Var name_pp_var = enter.object()->GetName(); | 48 PP_Var name_pp_var = enter.object()->GetName(); |
48 bool result = false; | 49 bool result = false; |
49 do { | 50 do { |
50 Var* name_var = var_tracker->GetVar(name_pp_var); | 51 Var* name_var = var_tracker->GetVar(name_pp_var); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 expected_ = expected; | 183 expected_ = expected; |
183 } | 184 } |
184 | 185 |
185 bool called() const { return called_; } | 186 bool called() const { return called_; } |
186 | 187 |
187 bool same_as_expected() const { return same_as_expected_; } | 188 bool same_as_expected() const { return same_as_expected_; } |
188 | 189 |
189 static void MonitorDeviceChangeCallback(void* user_data, | 190 static void MonitorDeviceChangeCallback(void* user_data, |
190 uint32_t device_count, | 191 uint32_t device_count, |
191 const PP_Resource devices[]) { | 192 const PP_Resource devices[]) { |
| 193 ProxyAutoLock lock; |
192 TestMonitorDeviceChange* helper = | 194 TestMonitorDeviceChange* helper = |
193 static_cast<TestMonitorDeviceChange*>(user_data); | 195 static_cast<TestMonitorDeviceChange*>(user_data); |
194 CHECK(!helper->called_); | 196 CHECK(!helper->called_); |
195 | 197 |
196 helper->called_ = true; | 198 helper->called_ = true; |
197 helper->same_as_expected_ = false; | 199 helper->same_as_expected_ = false; |
198 if (device_count != helper->expected_.size()) | 200 if (device_count != helper->expected_.size()) |
199 return; | 201 return; |
200 for (size_t i = 0; i < device_count; ++i) { | 202 for (size_t i = 0; i < device_count; ++i) { |
201 if (!CompareDeviceRef(helper->var_tracker_, devices[i], | 203 if (!CompareDeviceRef(helper->var_tracker_, devices[i], |
202 helper->expected_[i])) { | 204 helper->expected_[i])) { |
203 return; | 205 return; |
204 } | 206 } |
205 } | 207 } |
206 helper->same_as_expected_ = true; | 208 helper->same_as_expected_ = true; |
207 } | 209 } |
208 | 210 |
209 private: | 211 private: |
210 bool called_; | 212 bool called_; |
211 bool same_as_expected_; | 213 bool same_as_expected_; |
212 std::vector<DeviceRefData> expected_; | 214 std::vector<DeviceRefData> expected_; |
213 PluginVarTracker* var_tracker_; | 215 PluginVarTracker* var_tracker_; |
214 | 216 |
215 DISALLOW_COPY_AND_ASSIGN(TestMonitorDeviceChange); | 217 DISALLOW_COPY_AND_ASSIGN(TestMonitorDeviceChange); |
216 }; | 218 }; |
217 | 219 |
218 } // namespace | 220 } // namespace |
219 | 221 |
220 TEST_F(DeviceEnumerationResourceHelperTest, EnumerateDevices) { | 222 TEST_F(DeviceEnumerationResourceHelperTest, EnumerateDevices) { |
| 223 ProxyAutoLock lock; |
| 224 |
221 scoped_refptr<TestResource> resource( | 225 scoped_refptr<TestResource> resource( |
222 new TestResource(GetConnection(this), pp_instance())); | 226 new TestResource(GetConnection(this), pp_instance())); |
223 DeviceEnumerationResourceHelper& device_enumeration = | 227 DeviceEnumerationResourceHelper& device_enumeration = |
224 resource->device_enumeration(); | 228 resource->device_enumeration(); |
225 | 229 |
226 TestArrayOutput output(&resource_tracker()); | 230 TestArrayOutput output(&resource_tracker()); |
227 TestCallback callback; | 231 TestCallback callback; |
228 scoped_refptr<TrackedCallback> tracked_callback( | 232 scoped_refptr<TrackedCallback> tracked_callback( |
229 new TrackedCallback(resource.get(), callback.MakeCompletionCallback())); | 233 new TrackedCallback(resource.get(), callback.MakeCompletionCallback())); |
230 int32_t result = device_enumeration.EnumerateDevices(output.MakeArrayOutput(), | 234 int32_t result = device_enumeration.EnumerateDevices(output.MakeArrayOutput(), |
(...skipping 14 matching lines...) Expand all Loading... |
245 DeviceRefData data_item; | 249 DeviceRefData data_item; |
246 data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE; | 250 data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE; |
247 data_item.name = "name_1"; | 251 data_item.name = "name_1"; |
248 data_item.id = "id_1"; | 252 data_item.id = "id_1"; |
249 data.push_back(data_item); | 253 data.push_back(data_item); |
250 data_item.type = PP_DEVICETYPE_DEV_VIDEOCAPTURE; | 254 data_item.type = PP_DEVICETYPE_DEV_VIDEOCAPTURE; |
251 data_item.name = "name_2"; | 255 data_item.name = "name_2"; |
252 data_item.id = "id_2"; | 256 data_item.id = "id_2"; |
253 data.push_back(data_item); | 257 data.push_back(data_item); |
254 | 258 |
255 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( | 259 { |
256 PpapiPluginMsg_ResourceReply( | 260 ProxyAutoUnlock unlock; |
257 reply_params, | 261 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( |
258 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply(data)))); | 262 PpapiPluginMsg_ResourceReply( |
259 | 263 reply_params, |
| 264 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply(data)))); |
| 265 } |
260 EXPECT_TRUE(callback.called()); | 266 EXPECT_TRUE(callback.called()); |
261 EXPECT_EQ(PP_OK, callback.result()); | 267 EXPECT_EQ(PP_OK, callback.result()); |
262 EXPECT_EQ(2U, output.count()); | 268 EXPECT_EQ(2U, output.count()); |
263 for (size_t i = 0; i < output.count(); ++i) | 269 for (size_t i = 0; i < output.count(); ++i) |
264 EXPECT_TRUE(CompareDeviceRef(&var_tracker(), output.data()[i], data[i])); | 270 EXPECT_TRUE(CompareDeviceRef(&var_tracker(), output.data()[i], data[i])); |
265 } | 271 } |
266 | 272 |
267 TEST_F(DeviceEnumerationResourceHelperTest, MonitorDeviceChange) { | 273 TEST_F(DeviceEnumerationResourceHelperTest, MonitorDeviceChange) { |
| 274 ProxyAutoLock lock; |
| 275 |
268 scoped_refptr<TestResource> resource( | 276 scoped_refptr<TestResource> resource( |
269 new TestResource(GetConnection(this), pp_instance())); | 277 new TestResource(GetConnection(this), pp_instance())); |
270 DeviceEnumerationResourceHelper& device_enumeration = | 278 DeviceEnumerationResourceHelper& device_enumeration = |
271 resource->device_enumeration(); | 279 resource->device_enumeration(); |
272 | 280 |
273 TestMonitorDeviceChange helper(&var_tracker()); | 281 TestMonitorDeviceChange helper(&var_tracker()); |
274 | 282 |
275 int32_t result = device_enumeration.MonitorDeviceChange( | 283 int32_t result = device_enumeration.MonitorDeviceChange( |
276 &TestMonitorDeviceChange::MonitorDeviceChangeCallback, &helper); | 284 &TestMonitorDeviceChange::MonitorDeviceChangeCallback, &helper); |
277 ASSERT_EQ(PP_OK, result); | 285 ASSERT_EQ(PP_OK, result); |
278 | 286 |
279 // Should have sent a MonitorDeviceChange message. | 287 // Should have sent a MonitorDeviceChange message. |
280 ResourceMessageCallParams params; | 288 ResourceMessageCallParams params; |
281 IPC::Message msg; | 289 IPC::Message msg; |
282 ASSERT_TRUE(sink().GetFirstResourceCallMatching( | 290 ASSERT_TRUE(sink().GetFirstResourceCallMatching( |
283 PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange::ID, ¶ms, &msg)); | 291 PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange::ID, ¶ms, &msg)); |
284 sink().ClearMessages(); | 292 sink().ClearMessages(); |
285 | 293 |
286 uint32_t callback_id = 0; | 294 uint32_t callback_id = 0; |
287 ASSERT_TRUE(UnpackMessage<PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange>( | 295 ASSERT_TRUE(UnpackMessage<PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange>( |
288 msg, &callback_id)); | 296 msg, &callback_id)); |
289 | 297 |
290 ResourceMessageReplyParams reply_params(params.pp_resource(), 0); | 298 ResourceMessageReplyParams reply_params(params.pp_resource(), 0); |
291 reply_params.set_result(PP_OK); | 299 reply_params.set_result(PP_OK); |
292 std::vector<DeviceRefData> data; | 300 std::vector<DeviceRefData> data; |
293 | 301 |
294 helper.SetExpectedResult(data); | 302 helper.SetExpectedResult(data); |
295 | 303 |
296 // Synthesize a response with no device. | 304 { |
297 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( | 305 ProxyAutoUnlock unlock; |
298 PpapiPluginMsg_ResourceReply( | 306 // Synthesize a response with no device. |
299 reply_params, | 307 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( |
300 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange( | 308 PpapiPluginMsg_ResourceReply( |
301 callback_id, data)))); | 309 reply_params, |
| 310 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange( |
| 311 callback_id, data)))); |
| 312 } |
302 EXPECT_TRUE(helper.called() && helper.same_as_expected()); | 313 EXPECT_TRUE(helper.called() && helper.same_as_expected()); |
303 | 314 |
304 DeviceRefData data_item; | 315 DeviceRefData data_item; |
305 data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE; | 316 data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE; |
306 data_item.name = "name_1"; | 317 data_item.name = "name_1"; |
307 data_item.id = "id_1"; | 318 data_item.id = "id_1"; |
308 data.push_back(data_item); | 319 data.push_back(data_item); |
309 data_item.type = PP_DEVICETYPE_DEV_VIDEOCAPTURE; | 320 data_item.type = PP_DEVICETYPE_DEV_VIDEOCAPTURE; |
310 data_item.name = "name_2"; | 321 data_item.name = "name_2"; |
311 data_item.id = "id_2"; | 322 data_item.id = "id_2"; |
312 data.push_back(data_item); | 323 data.push_back(data_item); |
313 | 324 |
314 helper.SetExpectedResult(data); | 325 helper.SetExpectedResult(data); |
315 | 326 |
316 // Synthesize a response with some devices. | 327 { |
317 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( | 328 ProxyAutoUnlock unlock; |
318 PpapiPluginMsg_ResourceReply( | 329 // Synthesize a response with some devices. |
319 reply_params, | 330 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( |
320 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange( | 331 PpapiPluginMsg_ResourceReply( |
321 callback_id, data)))); | 332 reply_params, |
| 333 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange( |
| 334 callback_id, data)))); |
| 335 } |
322 EXPECT_TRUE(helper.called() && helper.same_as_expected()); | 336 EXPECT_TRUE(helper.called() && helper.same_as_expected()); |
323 | 337 |
324 TestMonitorDeviceChange helper2(&var_tracker()); | 338 TestMonitorDeviceChange helper2(&var_tracker()); |
325 | 339 |
326 result = device_enumeration.MonitorDeviceChange( | 340 result = device_enumeration.MonitorDeviceChange( |
327 &TestMonitorDeviceChange::MonitorDeviceChangeCallback, &helper2); | 341 &TestMonitorDeviceChange::MonitorDeviceChangeCallback, &helper2); |
328 ASSERT_EQ(PP_OK, result); | 342 ASSERT_EQ(PP_OK, result); |
329 | 343 |
330 // Should have sent another MonitorDeviceChange message. | 344 // Should have sent another MonitorDeviceChange message. |
331 ResourceMessageCallParams params2; | 345 ResourceMessageCallParams params2; |
332 IPC::Message msg2; | 346 IPC::Message msg2; |
333 ASSERT_TRUE(sink().GetFirstResourceCallMatching( | 347 ASSERT_TRUE(sink().GetFirstResourceCallMatching( |
334 PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange::ID, ¶ms2, &msg2)); | 348 PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange::ID, ¶ms2, &msg2)); |
335 sink().ClearMessages(); | 349 sink().ClearMessages(); |
336 | 350 |
337 uint32_t callback_id2 = 0; | 351 uint32_t callback_id2 = 0; |
338 ASSERT_TRUE(UnpackMessage<PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange>( | 352 ASSERT_TRUE(UnpackMessage<PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange>( |
339 msg2, &callback_id2)); | 353 msg2, &callback_id2)); |
340 | 354 |
341 helper.SetExpectedResult(data); | 355 helper.SetExpectedResult(data); |
342 helper2.SetExpectedResult(data); | 356 helper2.SetExpectedResult(data); |
343 // |helper2| should receive the result while |helper| shouldn't. | 357 { |
344 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( | 358 ProxyAutoUnlock unlock; |
345 PpapiPluginMsg_ResourceReply( | 359 // |helper2| should receive the result while |helper| shouldn't. |
346 reply_params, | 360 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( |
347 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange( | 361 PpapiPluginMsg_ResourceReply( |
348 callback_id2, data)))); | 362 reply_params, |
| 363 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange( |
| 364 callback_id2, data)))); |
| 365 } |
349 EXPECT_TRUE(helper2.called() && helper2.same_as_expected()); | 366 EXPECT_TRUE(helper2.called() && helper2.same_as_expected()); |
350 EXPECT_FALSE(helper.called()); | 367 EXPECT_FALSE(helper.called()); |
351 | 368 |
352 helper.SetExpectedResult(data); | 369 helper.SetExpectedResult(data); |
353 helper2.SetExpectedResult(data); | 370 helper2.SetExpectedResult(data); |
354 // Even if a message with |callback_id| arrives. |helper| shouldn't receive | 371 { |
355 // the result. | 372 ProxyAutoUnlock unlock; |
356 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( | 373 // Even if a message with |callback_id| arrives. |helper| shouldn't receive |
357 PpapiPluginMsg_ResourceReply( | 374 // the result. |
358 reply_params, | 375 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( |
359 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange( | 376 PpapiPluginMsg_ResourceReply( |
360 callback_id, data)))); | 377 reply_params, |
| 378 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange( |
| 379 callback_id, data)))); |
| 380 } |
361 EXPECT_FALSE(helper2.called()); | 381 EXPECT_FALSE(helper2.called()); |
362 EXPECT_FALSE(helper.called()); | 382 EXPECT_FALSE(helper.called()); |
363 | 383 |
364 result = device_enumeration.MonitorDeviceChange(NULL, NULL); | 384 result = device_enumeration.MonitorDeviceChange(NULL, NULL); |
365 ASSERT_EQ(PP_OK, result); | 385 ASSERT_EQ(PP_OK, result); |
366 | 386 |
367 // Should have sent a StopMonitoringDeviceChange message. | 387 // Should have sent a StopMonitoringDeviceChange message. |
368 ResourceMessageCallParams params3; | 388 ResourceMessageCallParams params3; |
369 IPC::Message msg3; | 389 IPC::Message msg3; |
370 ASSERT_TRUE(sink().GetFirstResourceCallMatching( | 390 ASSERT_TRUE(sink().GetFirstResourceCallMatching( |
371 PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange::ID, | 391 PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange::ID, |
372 ¶ms3, &msg3)); | 392 ¶ms3, &msg3)); |
373 sink().ClearMessages(); | 393 sink().ClearMessages(); |
374 | 394 |
375 helper2.SetExpectedResult(data); | 395 helper2.SetExpectedResult(data); |
376 // |helper2| shouldn't receive any result any more. | 396 { |
377 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( | 397 ProxyAutoUnlock unlock; |
378 PpapiPluginMsg_ResourceReply( | 398 // |helper2| shouldn't receive any result any more. |
379 reply_params, | 399 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( |
380 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange( | 400 PpapiPluginMsg_ResourceReply( |
381 callback_id2, data)))); | 401 reply_params, |
| 402 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange( |
| 403 callback_id2, data)))); |
| 404 } |
382 EXPECT_FALSE(helper2.called()); | 405 EXPECT_FALSE(helper2.called()); |
383 } | 406 } |
384 | 407 |
385 } // namespace proxy | 408 } // namespace proxy |
386 } // namespace ppapi | 409 } // namespace ppapi |
OLD | NEW |