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
3 // found in the LICENSE file.
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/logging.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/renderer/pepper/pepper_device_enumeration_host_helper.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/host/host_message_context.h"
14 #include "ppapi/host/ppapi_host.h"
15 #include "ppapi/host/resource_host.h"
16 #include "ppapi/proxy/ppapi_message_utils.h"
17 #include "ppapi/proxy/ppapi_messages.h"
18 #include "ppapi/proxy/resource_message_params.h"
19 #include "ppapi/proxy/resource_message_test_sink.h"
20 #include "ppapi/shared_impl/ppapi_permissions.h"
21 #include "testing/gtest/include/gtest/gtest.h"
28 class TestDelegate
: public PepperDeviceEnumerationHostHelper::Delegate
,
29 public base::SupportsWeakPtr
<TestDelegate
> {
31 TestDelegate() : last_used_id_(0) {}
33 ~TestDelegate() override
{ CHECK(callbacks_
.empty()); }
35 int EnumerateDevices(PP_DeviceType_Dev
/* type */,
36 const GURL
& /* document_url */,
37 const EnumerateDevicesCallback
& callback
) override
{
39 callbacks_
[last_used_id_
] = callback
;
43 void StopEnumerateDevices(int request_id
) override
{
44 std::map
<int, EnumerateDevicesCallback
>::iterator iter
=
45 callbacks_
.find(request_id
);
46 CHECK(iter
!= callbacks_
.end());
47 callbacks_
.erase(iter
);
50 // Returns false if |request_id| is not found.
51 bool SimulateEnumerateResult(
53 const std::vector
<ppapi::DeviceRefData
>& devices
) {
54 std::map
<int, EnumerateDevicesCallback
>::iterator iter
=
55 callbacks_
.find(request_id
);
56 if (iter
== callbacks_
.end())
59 iter
->second
.Run(request_id
, devices
);
63 size_t GetRegisteredCallbackCount() const { return callbacks_
.size(); }
65 int last_used_id() const { return last_used_id_
; }
68 std::map
<int, EnumerateDevicesCallback
> callbacks_
;
71 DISALLOW_COPY_AND_ASSIGN(TestDelegate
);
74 class PepperDeviceEnumerationHostHelperTest
: public testing::Test
{
76 PepperDeviceEnumerationHostHelperTest()
77 : ppapi_host_(&sink_
, ppapi::PpapiPermissions()),
78 resource_host_(&ppapi_host_
, 12345, 67890),
79 device_enumeration_(&resource_host_
,
80 delegate_
.AsWeakPtr(),
81 PP_DEVICETYPE_DEV_AUDIOCAPTURE
,
82 GURL("http://example.com")) {}
84 ~PepperDeviceEnumerationHostHelperTest() override
{}
86 void SimulateMonitorDeviceChangeReceived(uint32_t callback_id
) {
87 PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange
msg(callback_id
);
88 ppapi::proxy::ResourceMessageCallParams
call_params(
89 resource_host_
.pp_resource(), 123);
90 ppapi::host::HostMessageContext
context(call_params
);
91 int32_t result
= PP_ERROR_FAILED
;
93 device_enumeration_
.HandleResourceMessage(msg
, &context
, &result
));
94 EXPECT_EQ(PP_OK
, result
);
97 void CheckNotifyDeviceChangeMessage(
99 const std::vector
<ppapi::DeviceRefData
>& expected
) {
100 ppapi::proxy::ResourceMessageReplyParams reply_params
;
101 IPC::Message reply_msg
;
102 ASSERT_TRUE(sink_
.GetFirstResourceReplyMatching(
103 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange::ID
,
106 sink_
.ClearMessages();
108 EXPECT_EQ(PP_OK
, reply_params
.result());
110 uint32_t reply_callback_id
= 0;
111 std::vector
<ppapi::DeviceRefData
> reply_data
;
112 ASSERT_TRUE(ppapi::UnpackMessage
<
113 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange
>(
114 reply_msg
, &reply_callback_id
, &reply_data
));
115 EXPECT_EQ(callback_id
, reply_callback_id
);
116 EXPECT_EQ(expected
, reply_data
);
119 TestDelegate delegate_
;
120 ppapi::proxy::ResourceMessageTestSink sink_
;
121 ppapi::host::PpapiHost ppapi_host_
;
122 ppapi::host::ResourceHost resource_host_
;
123 PepperDeviceEnumerationHostHelper device_enumeration_
;
126 DISALLOW_COPY_AND_ASSIGN(PepperDeviceEnumerationHostHelperTest
);
131 TEST_F(PepperDeviceEnumerationHostHelperTest
, EnumerateDevices
) {
132 PpapiHostMsg_DeviceEnumeration_EnumerateDevices msg
;
133 ppapi::proxy::ResourceMessageCallParams
call_params(
134 resource_host_
.pp_resource(), 123);
135 ppapi::host::HostMessageContext
context(call_params
);
136 int32_t result
= PP_ERROR_FAILED
;
138 device_enumeration_
.HandleResourceMessage(msg
, &context
, &result
));
139 EXPECT_EQ(PP_OK_COMPLETIONPENDING
, result
);
141 EXPECT_EQ(1U, delegate_
.GetRegisteredCallbackCount());
142 int request_id
= delegate_
.last_used_id();
144 std::vector
<ppapi::DeviceRefData
> data
;
145 ppapi::DeviceRefData data_item
;
146 data_item
.type
= PP_DEVICETYPE_DEV_AUDIOCAPTURE
;
147 data_item
.name
= "name_1";
148 data_item
.id
= "id_1";
149 data
.push_back(data_item
);
150 data_item
.type
= PP_DEVICETYPE_DEV_VIDEOCAPTURE
;
151 data_item
.name
= "name_2";
152 data_item
.id
= "id_2";
153 data
.push_back(data_item
);
154 ASSERT_TRUE(delegate_
.SimulateEnumerateResult(request_id
, data
));
156 // StopEnumerateDevices() should have been called since the EnumerateDevices
157 // message is not a persistent request.
158 EXPECT_EQ(0U, delegate_
.GetRegisteredCallbackCount());
160 // A reply message should have been sent to the test sink.
161 ppapi::proxy::ResourceMessageReplyParams reply_params
;
162 IPC::Message reply_msg
;
163 ASSERT_TRUE(sink_
.GetFirstResourceReplyMatching(
164 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply::ID
,
168 EXPECT_EQ(call_params
.sequence(), reply_params
.sequence());
169 EXPECT_EQ(PP_OK
, reply_params
.result());
171 std::vector
<ppapi::DeviceRefData
> reply_data
;
172 ASSERT_TRUE(ppapi::UnpackMessage
<
173 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply
>(reply_msg
,
175 EXPECT_EQ(data
, reply_data
);
178 TEST_F(PepperDeviceEnumerationHostHelperTest
, MonitorDeviceChange
) {
179 uint32_t callback_id
= 456;
180 SimulateMonitorDeviceChangeReceived(callback_id
);
182 EXPECT_EQ(1U, delegate_
.GetRegisteredCallbackCount());
183 int request_id
= delegate_
.last_used_id();
185 std::vector
<ppapi::DeviceRefData
> data
;
186 ASSERT_TRUE(delegate_
.SimulateEnumerateResult(request_id
, data
));
188 // StopEnumerateDevices() shouldn't be called because the MonitorDeviceChange
189 // message is a persistent request.
190 EXPECT_EQ(1U, delegate_
.GetRegisteredCallbackCount());
192 CheckNotifyDeviceChangeMessage(callback_id
, data
);
194 ppapi::DeviceRefData data_item
;
195 data_item
.type
= PP_DEVICETYPE_DEV_AUDIOCAPTURE
;
196 data_item
.name
= "name_1";
197 data_item
.id
= "id_1";
198 data
.push_back(data_item
);
199 data_item
.type
= PP_DEVICETYPE_DEV_VIDEOCAPTURE
;
200 data_item
.name
= "name_2";
201 data_item
.id
= "id_2";
202 data
.push_back(data_item
);
203 ASSERT_TRUE(delegate_
.SimulateEnumerateResult(request_id
, data
));
204 EXPECT_EQ(1U, delegate_
.GetRegisteredCallbackCount());
206 CheckNotifyDeviceChangeMessage(callback_id
, data
);
208 uint32_t callback_id2
= 789;
209 SimulateMonitorDeviceChangeReceived(callback_id2
);
211 // StopEnumerateDevice() should have been called for the previous request.
212 EXPECT_EQ(1U, delegate_
.GetRegisteredCallbackCount());
213 int request_id2
= delegate_
.last_used_id();
215 data_item
.type
= PP_DEVICETYPE_DEV_AUDIOCAPTURE
;
216 data_item
.name
= "name_3";
217 data_item
.id
= "id_3";
218 data
.push_back(data_item
);
219 ASSERT_TRUE(delegate_
.SimulateEnumerateResult(request_id2
, data
));
221 CheckNotifyDeviceChangeMessage(callback_id2
, data
);
223 PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange msg
;
224 ppapi::proxy::ResourceMessageCallParams
call_params(
225 resource_host_
.pp_resource(), 123);
226 ppapi::host::HostMessageContext
context(call_params
);
227 int32_t result
= PP_ERROR_FAILED
;
229 device_enumeration_
.HandleResourceMessage(msg
, &context
, &result
));
230 EXPECT_EQ(PP_OK
, result
);
232 EXPECT_EQ(0U, delegate_
.GetRegisteredCallbackCount());
235 } // namespace content