| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" |
| 14 #include "content/renderer/pepper/pepper_device_enumeration_host_helper.h" | 16 #include "content/renderer/pepper/pepper_device_enumeration_host_helper.h" |
| 15 #include "ppapi/c/pp_errors.h" | 17 #include "ppapi/c/pp_errors.h" |
| 16 #include "ppapi/host/host_message_context.h" | 18 #include "ppapi/host/host_message_context.h" |
| 17 #include "ppapi/host/ppapi_host.h" | 19 #include "ppapi/host/ppapi_host.h" |
| 18 #include "ppapi/host/resource_host.h" | 20 #include "ppapi/host/resource_host.h" |
| 19 #include "ppapi/proxy/ppapi_message_utils.h" | 21 #include "ppapi/proxy/ppapi_message_utils.h" |
| 20 #include "ppapi/proxy/ppapi_messages.h" | 22 #include "ppapi/proxy/ppapi_messages.h" |
| 21 #include "ppapi/proxy/resource_message_params.h" | 23 #include "ppapi/proxy/resource_message_params.h" |
| 22 #include "ppapi/proxy/resource_message_test_sink.h" | 24 #include "ppapi/proxy/resource_message_test_sink.h" |
| 23 #include "ppapi/shared_impl/ppapi_permissions.h" | 25 #include "ppapi/shared_impl/ppapi_permissions.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 26 | 28 |
| 27 namespace content { | 29 namespace content { |
| 28 | 30 |
| 29 namespace { | 31 namespace { |
| 30 | 32 |
| 33 std::vector<ppapi::DeviceRefData> TestEnumerationData() { |
| 34 std::vector<ppapi::DeviceRefData> data; |
| 35 ppapi::DeviceRefData data_item; |
| 36 data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE; |
| 37 data_item.name = "name_1"; |
| 38 data_item.id = "id_1"; |
| 39 data.push_back(data_item); |
| 40 data_item.type = PP_DEVICETYPE_DEV_VIDEOCAPTURE; |
| 41 data_item.name = "name_2"; |
| 42 data_item.id = "id_2"; |
| 43 data.push_back(data_item); |
| 44 |
| 45 return data; |
| 46 } |
| 47 |
| 31 class TestDelegate : public PepperDeviceEnumerationHostHelper::Delegate, | 48 class TestDelegate : public PepperDeviceEnumerationHostHelper::Delegate, |
| 32 public base::SupportsWeakPtr<TestDelegate> { | 49 public base::SupportsWeakPtr<TestDelegate> { |
| 33 public: | 50 public: |
| 34 TestDelegate() : last_used_id_(0) {} | 51 TestDelegate() : last_used_id_(0) {} |
| 35 | 52 |
| 36 ~TestDelegate() override { CHECK(callbacks_.empty()); } | 53 ~TestDelegate() override { CHECK(monitoring_callbacks_.empty()); } |
| 37 | 54 |
| 38 int EnumerateDevices(PP_DeviceType_Dev /* type */, | 55 void EnumerateDevices(PP_DeviceType_Dev /* type */, |
| 39 const GURL& /* document_url */, | 56 const GURL& /* document_url */, |
| 40 const EnumerateDevicesCallback& callback) override { | 57 const DevicesCallback& callback) override { |
| 58 callback.Run(TestEnumerationData()); |
| 59 } |
| 60 |
| 61 uint32_t StartMonitoringDevices(PP_DeviceType_Dev /* type */, |
| 62 const GURL& /* document_url */, |
| 63 const DevicesCallback& callback) override { |
| 41 last_used_id_++; | 64 last_used_id_++; |
| 42 callbacks_[last_used_id_] = callback; | 65 monitoring_callbacks_[last_used_id_] = callback; |
| 43 return last_used_id_; | 66 return last_used_id_; |
| 44 } | 67 } |
| 45 | 68 |
| 46 void StopEnumerateDevices(int request_id) override { | 69 void StopMonitoringDevices(PP_DeviceType_Dev /* type */, |
| 47 std::map<int, EnumerateDevicesCallback>::iterator iter = | 70 uint32_t subscription_id) override { |
| 48 callbacks_.find(request_id); | 71 auto iter = monitoring_callbacks_.find(subscription_id); |
| 49 CHECK(iter != callbacks_.end()); | 72 CHECK(iter != monitoring_callbacks_.end()); |
| 50 callbacks_.erase(iter); | 73 monitoring_callbacks_.erase(iter); |
| 51 } | 74 } |
| 52 | 75 |
| 53 // Returns false if |request_id| is not found. | 76 // Returns false if |request_id| is not found. |
| 54 bool SimulateEnumerateResult( | 77 bool SimulateDevicesChanged( |
| 55 int request_id, | 78 uint32_t subscription_id, |
| 56 const std::vector<ppapi::DeviceRefData>& devices) { | 79 const std::vector<ppapi::DeviceRefData>& devices) { |
| 57 std::map<int, EnumerateDevicesCallback>::iterator iter = | 80 auto iter = monitoring_callbacks_.find(subscription_id); |
| 58 callbacks_.find(request_id); | 81 if (iter == monitoring_callbacks_.end()) |
| 59 if (iter == callbacks_.end()) | |
| 60 return false; | 82 return false; |
| 61 | 83 |
| 62 iter->second.Run(request_id, devices); | 84 iter->second.Run(devices); |
| 63 return true; | 85 return true; |
| 64 } | 86 } |
| 65 | 87 |
| 66 size_t GetRegisteredCallbackCount() const { return callbacks_.size(); } | 88 size_t GetRegisteredCallbackCount() const { |
| 89 return monitoring_callbacks_.size(); |
| 90 } |
| 67 | 91 |
| 68 int last_used_id() const { return last_used_id_; } | 92 int last_used_id() const { return last_used_id_; } |
| 69 | 93 |
| 70 private: | 94 private: |
| 71 std::map<int, EnumerateDevicesCallback> callbacks_; | 95 std::map<uint32_t, DevicesCallback> monitoring_callbacks_; |
| 72 int last_used_id_; | 96 int last_used_id_; |
| 73 | 97 |
| 74 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 98 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 75 }; | 99 }; |
| 76 | 100 |
| 77 class PepperDeviceEnumerationHostHelperTest : public testing::Test { | 101 class PepperDeviceEnumerationHostHelperTest : public testing::Test { |
| 78 protected: | 102 protected: |
| 79 PepperDeviceEnumerationHostHelperTest() | 103 PepperDeviceEnumerationHostHelperTest() |
| 80 : ppapi_host_(&sink_, ppapi::PpapiPermissions()), | 104 : ppapi_host_(&sink_, ppapi::PpapiPermissions()), |
| 81 resource_host_(&ppapi_host_, 12345, 67890), | 105 resource_host_(&ppapi_host_, 12345, 67890), |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 reply_msg, &reply_callback_id, &reply_data)); | 141 reply_msg, &reply_callback_id, &reply_data)); |
| 118 EXPECT_EQ(callback_id, reply_callback_id); | 142 EXPECT_EQ(callback_id, reply_callback_id); |
| 119 EXPECT_EQ(expected, reply_data); | 143 EXPECT_EQ(expected, reply_data); |
| 120 } | 144 } |
| 121 | 145 |
| 122 TestDelegate delegate_; | 146 TestDelegate delegate_; |
| 123 ppapi::proxy::ResourceMessageTestSink sink_; | 147 ppapi::proxy::ResourceMessageTestSink sink_; |
| 124 ppapi::host::PpapiHost ppapi_host_; | 148 ppapi::host::PpapiHost ppapi_host_; |
| 125 ppapi::host::ResourceHost resource_host_; | 149 ppapi::host::ResourceHost resource_host_; |
| 126 PepperDeviceEnumerationHostHelper device_enumeration_; | 150 PepperDeviceEnumerationHostHelper device_enumeration_; |
| 151 base::MessageLoop message_loop_; // required for async calls to work. |
| 127 | 152 |
| 128 private: | 153 private: |
| 129 DISALLOW_COPY_AND_ASSIGN(PepperDeviceEnumerationHostHelperTest); | 154 DISALLOW_COPY_AND_ASSIGN(PepperDeviceEnumerationHostHelperTest); |
| 130 }; | 155 }; |
| 131 | 156 |
| 132 } // namespace | 157 } // namespace |
| 133 | 158 |
| 134 TEST_F(PepperDeviceEnumerationHostHelperTest, EnumerateDevices) { | 159 TEST_F(PepperDeviceEnumerationHostHelperTest, EnumerateDevices) { |
| 135 PpapiHostMsg_DeviceEnumeration_EnumerateDevices msg; | 160 PpapiHostMsg_DeviceEnumeration_EnumerateDevices msg; |
| 136 ppapi::proxy::ResourceMessageCallParams call_params( | 161 ppapi::proxy::ResourceMessageCallParams call_params( |
| 137 resource_host_.pp_resource(), 123); | 162 resource_host_.pp_resource(), 123); |
| 138 ppapi::host::HostMessageContext context(call_params); | 163 ppapi::host::HostMessageContext context(call_params); |
| 139 int32_t result = PP_ERROR_FAILED; | 164 int32_t result = PP_ERROR_FAILED; |
| 140 ASSERT_TRUE( | 165 ASSERT_TRUE( |
| 141 device_enumeration_.HandleResourceMessage(msg, &context, &result)); | 166 device_enumeration_.HandleResourceMessage(msg, &context, &result)); |
| 142 EXPECT_EQ(PP_OK_COMPLETIONPENDING, result); | 167 EXPECT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 143 | 168 base::RunLoop().RunUntilIdle(); |
| 144 EXPECT_EQ(1U, delegate_.GetRegisteredCallbackCount()); | |
| 145 int request_id = delegate_.last_used_id(); | |
| 146 | |
| 147 std::vector<ppapi::DeviceRefData> data; | |
| 148 ppapi::DeviceRefData data_item; | |
| 149 data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE; | |
| 150 data_item.name = "name_1"; | |
| 151 data_item.id = "id_1"; | |
| 152 data.push_back(data_item); | |
| 153 data_item.type = PP_DEVICETYPE_DEV_VIDEOCAPTURE; | |
| 154 data_item.name = "name_2"; | |
| 155 data_item.id = "id_2"; | |
| 156 data.push_back(data_item); | |
| 157 ASSERT_TRUE(delegate_.SimulateEnumerateResult(request_id, data)); | |
| 158 | |
| 159 // StopEnumerateDevices() should have been called since the EnumerateDevices | |
| 160 // message is not a persistent request. | |
| 161 EXPECT_EQ(0U, delegate_.GetRegisteredCallbackCount()); | |
| 162 | 169 |
| 163 // A reply message should have been sent to the test sink. | 170 // A reply message should have been sent to the test sink. |
| 164 ppapi::proxy::ResourceMessageReplyParams reply_params; | 171 ppapi::proxy::ResourceMessageReplyParams reply_params; |
| 165 IPC::Message reply_msg; | 172 IPC::Message reply_msg; |
| 166 ASSERT_TRUE(sink_.GetFirstResourceReplyMatching( | 173 ASSERT_TRUE(sink_.GetFirstResourceReplyMatching( |
| 167 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply::ID, | 174 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply::ID, |
| 168 &reply_params, | 175 &reply_params, |
| 169 &reply_msg)); | 176 &reply_msg)); |
| 170 | 177 |
| 171 EXPECT_EQ(call_params.sequence(), reply_params.sequence()); | 178 EXPECT_EQ(call_params.sequence(), reply_params.sequence()); |
| 172 EXPECT_EQ(PP_OK, reply_params.result()); | 179 EXPECT_EQ(PP_OK, reply_params.result()); |
| 173 | 180 |
| 174 std::vector<ppapi::DeviceRefData> reply_data; | 181 std::vector<ppapi::DeviceRefData> reply_data; |
| 175 ASSERT_TRUE(ppapi::UnpackMessage< | 182 ASSERT_TRUE(ppapi::UnpackMessage< |
| 176 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply>(reply_msg, | 183 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply>(reply_msg, |
| 177 &reply_data)); | 184 &reply_data)); |
| 178 EXPECT_EQ(data, reply_data); | 185 EXPECT_EQ(TestEnumerationData(), reply_data); |
| 179 } | 186 } |
| 180 | 187 |
| 181 TEST_F(PepperDeviceEnumerationHostHelperTest, MonitorDeviceChange) { | 188 TEST_F(PepperDeviceEnumerationHostHelperTest, MonitorDeviceChange) { |
| 182 uint32_t callback_id = 456; | 189 uint32_t callback_id = 456; |
| 183 SimulateMonitorDeviceChangeReceived(callback_id); | 190 SimulateMonitorDeviceChangeReceived(callback_id); |
| 184 | 191 |
| 185 EXPECT_EQ(1U, delegate_.GetRegisteredCallbackCount()); | 192 EXPECT_EQ(1U, delegate_.GetRegisteredCallbackCount()); |
| 186 int request_id = delegate_.last_used_id(); | 193 int request_id = delegate_.last_used_id(); |
| 187 | 194 |
| 188 std::vector<ppapi::DeviceRefData> data; | 195 std::vector<ppapi::DeviceRefData> data; |
| 189 ASSERT_TRUE(delegate_.SimulateEnumerateResult(request_id, data)); | 196 ASSERT_TRUE(delegate_.SimulateDevicesChanged(request_id, data)); |
| 190 | 197 |
| 191 // StopEnumerateDevices() shouldn't be called because the MonitorDeviceChange | 198 // StopEnumerateDevices() shouldn't be called because the MonitorDeviceChange |
| 192 // message is a persistent request. | 199 // message is a persistent request. |
| 193 EXPECT_EQ(1U, delegate_.GetRegisteredCallbackCount()); | 200 EXPECT_EQ(1U, delegate_.GetRegisteredCallbackCount()); |
| 194 | 201 |
| 195 CheckNotifyDeviceChangeMessage(callback_id, data); | 202 CheckNotifyDeviceChangeMessage(callback_id, data); |
| 196 | 203 |
| 197 ppapi::DeviceRefData data_item; | 204 ppapi::DeviceRefData data_item; |
| 198 data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE; | 205 data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE; |
| 199 data_item.name = "name_1"; | 206 data_item.name = "name_1"; |
| 200 data_item.id = "id_1"; | 207 data_item.id = "id_1"; |
| 201 data.push_back(data_item); | 208 data.push_back(data_item); |
| 202 data_item.type = PP_DEVICETYPE_DEV_VIDEOCAPTURE; | 209 data_item.type = PP_DEVICETYPE_DEV_VIDEOCAPTURE; |
| 203 data_item.name = "name_2"; | 210 data_item.name = "name_2"; |
| 204 data_item.id = "id_2"; | 211 data_item.id = "id_2"; |
| 205 data.push_back(data_item); | 212 data.push_back(data_item); |
| 206 ASSERT_TRUE(delegate_.SimulateEnumerateResult(request_id, data)); | 213 ASSERT_TRUE(delegate_.SimulateDevicesChanged(request_id, data)); |
| 207 EXPECT_EQ(1U, delegate_.GetRegisteredCallbackCount()); | 214 EXPECT_EQ(1U, delegate_.GetRegisteredCallbackCount()); |
| 208 | 215 |
| 209 CheckNotifyDeviceChangeMessage(callback_id, data); | 216 CheckNotifyDeviceChangeMessage(callback_id, data); |
| 210 | 217 |
| 211 uint32_t callback_id2 = 789; | 218 uint32_t callback_id2 = 789; |
| 212 SimulateMonitorDeviceChangeReceived(callback_id2); | 219 SimulateMonitorDeviceChangeReceived(callback_id2); |
| 213 | 220 |
| 214 // StopEnumerateDevice() should have been called for the previous request. | 221 // StopEnumerateDevice() should have been called for the previous request. |
| 215 EXPECT_EQ(1U, delegate_.GetRegisteredCallbackCount()); | 222 EXPECT_EQ(1U, delegate_.GetRegisteredCallbackCount()); |
| 216 int request_id2 = delegate_.last_used_id(); | 223 int request_id2 = delegate_.last_used_id(); |
| 217 | 224 |
| 218 data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE; | 225 data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE; |
| 219 data_item.name = "name_3"; | 226 data_item.name = "name_3"; |
| 220 data_item.id = "id_3"; | 227 data_item.id = "id_3"; |
| 221 data.push_back(data_item); | 228 data.push_back(data_item); |
| 222 ASSERT_TRUE(delegate_.SimulateEnumerateResult(request_id2, data)); | 229 ASSERT_TRUE(delegate_.SimulateDevicesChanged(request_id2, data)); |
| 223 | 230 |
| 224 CheckNotifyDeviceChangeMessage(callback_id2, data); | 231 CheckNotifyDeviceChangeMessage(callback_id2, data); |
| 225 | 232 |
| 226 PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange msg; | 233 PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange msg; |
| 227 ppapi::proxy::ResourceMessageCallParams call_params( | 234 ppapi::proxy::ResourceMessageCallParams call_params( |
| 228 resource_host_.pp_resource(), 123); | 235 resource_host_.pp_resource(), 123); |
| 229 ppapi::host::HostMessageContext context(call_params); | 236 ppapi::host::HostMessageContext context(call_params); |
| 230 int32_t result = PP_ERROR_FAILED; | 237 int32_t result = PP_ERROR_FAILED; |
| 231 ASSERT_TRUE( | 238 ASSERT_TRUE( |
| 232 device_enumeration_.HandleResourceMessage(msg, &context, &result)); | 239 device_enumeration_.HandleResourceMessage(msg, &context, &result)); |
| 233 EXPECT_EQ(PP_OK, result); | 240 EXPECT_EQ(PP_OK, result); |
| 234 | 241 |
| 235 EXPECT_EQ(0U, delegate_.GetRegisteredCallbackCount()); | 242 EXPECT_EQ(0U, delegate_.GetRegisteredCallbackCount()); |
| 236 } | 243 } |
| 237 | 244 |
| 238 } // namespace content | 245 } // namespace content |
| OLD | NEW |