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