| 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 <map> | 5 #include <map> |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "content/renderer/pepper/pepper_device_enumeration_host_helper.h" | 10 #include "content/renderer/pepper/pepper_device_enumeration_host_helper.h" |
| 11 #include "ppapi/c/pp_errors.h" | 11 #include "ppapi/c/pp_errors.h" |
| 12 #include "ppapi/host/host_message_context.h" | 12 #include "ppapi/host/host_message_context.h" |
| 13 #include "ppapi/host/ppapi_host.h" | 13 #include "ppapi/host/ppapi_host.h" |
| 14 #include "ppapi/host/resource_host.h" | 14 #include "ppapi/host/resource_host.h" |
| 15 #include "ppapi/proxy/ppapi_message_utils.h" | 15 #include "ppapi/proxy/ppapi_message_utils.h" |
| 16 #include "ppapi/proxy/ppapi_messages.h" | 16 #include "ppapi/proxy/ppapi_messages.h" |
| 17 #include "ppapi/proxy/resource_message_params.h" | 17 #include "ppapi/proxy/resource_message_params.h" |
| 18 #include "ppapi/proxy/resource_message_test_sink.h" | 18 #include "ppapi/proxy/resource_message_test_sink.h" |
| 19 #include "ppapi/shared_impl/ppapi_permissions.h" | 19 #include "ppapi/shared_impl/ppapi_permissions.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 22 | 22 |
| 23 namespace content { | 23 namespace content { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 class TestDelegate : public PepperDeviceEnumerationHostHelper::Delegate { | 27 class TestDelegate : public PepperDeviceEnumerationHostHelper::Delegate { |
| 28 public: | 28 public: |
| 29 TestDelegate() : last_used_id_(0) { | 29 TestDelegate() : last_used_id_(0) {} |
| 30 } | |
| 31 | 30 |
| 32 virtual ~TestDelegate() { | 31 virtual ~TestDelegate() { CHECK(callbacks_.empty()); } |
| 33 CHECK(callbacks_.empty()); | |
| 34 } | |
| 35 | 32 |
| 36 virtual int EnumerateDevices( | 33 virtual int EnumerateDevices(PP_DeviceType_Dev /* type */, |
| 37 PP_DeviceType_Dev /* type */, | 34 const GURL& /* document_url */, |
| 38 const GURL& /* document_url */, | 35 const EnumerateDevicesCallback& callback) |
| 39 const EnumerateDevicesCallback& callback) OVERRIDE { | 36 OVERRIDE { |
| 40 last_used_id_++; | 37 last_used_id_++; |
| 41 callbacks_[last_used_id_] = callback; | 38 callbacks_[last_used_id_] = callback; |
| 42 return last_used_id_; | 39 return last_used_id_; |
| 43 } | 40 } |
| 44 | 41 |
| 45 virtual void StopEnumerateDevices(int request_id) OVERRIDE { | 42 virtual void StopEnumerateDevices(int request_id) OVERRIDE { |
| 46 std::map<int, EnumerateDevicesCallback>::iterator iter = | 43 std::map<int, EnumerateDevicesCallback>::iterator iter = |
| 47 callbacks_.find(request_id); | 44 callbacks_.find(request_id); |
| 48 CHECK(iter != callbacks_.end()); | 45 CHECK(iter != callbacks_.end()); |
| 49 callbacks_.erase(iter); | 46 callbacks_.erase(iter); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 71 int last_used_id_; | 68 int last_used_id_; |
| 72 | 69 |
| 73 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 70 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 74 }; | 71 }; |
| 75 | 72 |
| 76 class PepperDeviceEnumerationHostHelperTest : public testing::Test { | 73 class PepperDeviceEnumerationHostHelperTest : public testing::Test { |
| 77 protected: | 74 protected: |
| 78 PepperDeviceEnumerationHostHelperTest() | 75 PepperDeviceEnumerationHostHelperTest() |
| 79 : ppapi_host_(&sink_, ppapi::PpapiPermissions()), | 76 : ppapi_host_(&sink_, ppapi::PpapiPermissions()), |
| 80 resource_host_(&ppapi_host_, 12345, 67890), | 77 resource_host_(&ppapi_host_, 12345, 67890), |
| 81 device_enumeration_(&resource_host_, &delegate_, | 78 device_enumeration_(&resource_host_, |
| 79 &delegate_, |
| 82 PP_DEVICETYPE_DEV_AUDIOCAPTURE, | 80 PP_DEVICETYPE_DEV_AUDIOCAPTURE, |
| 83 GURL("http://example.com")) { | 81 GURL("http://example.com")) {} |
| 84 } | |
| 85 | 82 |
| 86 virtual ~PepperDeviceEnumerationHostHelperTest() {} | 83 virtual ~PepperDeviceEnumerationHostHelperTest() {} |
| 87 | 84 |
| 88 void SimulateMonitorDeviceChangeReceived(uint32_t callback_id) { | 85 void SimulateMonitorDeviceChangeReceived(uint32_t callback_id) { |
| 89 PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange msg(callback_id); | 86 PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange msg(callback_id); |
| 90 ppapi::proxy::ResourceMessageCallParams call_params( | 87 ppapi::proxy::ResourceMessageCallParams call_params( |
| 91 resource_host_.pp_resource(), 123); | 88 resource_host_.pp_resource(), 123); |
| 92 ppapi::host::HostMessageContext context(call_params); | 89 ppapi::host::HostMessageContext context(call_params); |
| 93 int32_t result = PP_ERROR_FAILED; | 90 int32_t result = PP_ERROR_FAILED; |
| 94 ASSERT_TRUE(device_enumeration_.HandleResourceMessage( | 91 ASSERT_TRUE( |
| 95 msg, &context, &result)); | 92 device_enumeration_.HandleResourceMessage(msg, &context, &result)); |
| 96 EXPECT_EQ(PP_OK, result); | 93 EXPECT_EQ(PP_OK, result); |
| 97 } | 94 } |
| 98 | 95 |
| 99 void CheckNotifyDeviceChangeMessage( | 96 void CheckNotifyDeviceChangeMessage( |
| 100 uint32_t callback_id, | 97 uint32_t callback_id, |
| 101 const std::vector<ppapi::DeviceRefData>& expected) { | 98 const std::vector<ppapi::DeviceRefData>& expected) { |
| 102 ppapi::proxy::ResourceMessageReplyParams reply_params; | 99 ppapi::proxy::ResourceMessageReplyParams reply_params; |
| 103 IPC::Message reply_msg; | 100 IPC::Message reply_msg; |
| 104 ASSERT_TRUE(sink_.GetFirstResourceReplyMatching( | 101 ASSERT_TRUE(sink_.GetFirstResourceReplyMatching( |
| 105 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange::ID, | 102 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange::ID, |
| 106 &reply_params, &reply_msg)); | 103 &reply_params, |
| 104 &reply_msg)); |
| 107 sink_.ClearMessages(); | 105 sink_.ClearMessages(); |
| 108 | 106 |
| 109 EXPECT_EQ(PP_OK, reply_params.result()); | 107 EXPECT_EQ(PP_OK, reply_params.result()); |
| 110 | 108 |
| 111 uint32_t reply_callback_id = 0; | 109 uint32_t reply_callback_id = 0; |
| 112 std::vector<ppapi::DeviceRefData> reply_data; | 110 std::vector<ppapi::DeviceRefData> reply_data; |
| 113 ASSERT_TRUE(ppapi::UnpackMessage< | 111 ASSERT_TRUE(ppapi::UnpackMessage< |
| 114 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange>( | 112 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange>( |
| 115 reply_msg, &reply_callback_id, &reply_data)); | 113 reply_msg, &reply_callback_id, &reply_data)); |
| 116 EXPECT_EQ(callback_id, reply_callback_id); | 114 EXPECT_EQ(callback_id, reply_callback_id); |
| 117 EXPECT_EQ(expected, reply_data); | 115 EXPECT_EQ(expected, reply_data); |
| 118 } | 116 } |
| 119 | 117 |
| 120 TestDelegate delegate_; | 118 TestDelegate delegate_; |
| 121 ppapi::proxy::ResourceMessageTestSink sink_; | 119 ppapi::proxy::ResourceMessageTestSink sink_; |
| 122 ppapi::host::PpapiHost ppapi_host_; | 120 ppapi::host::PpapiHost ppapi_host_; |
| 123 ppapi::host::ResourceHost resource_host_; | 121 ppapi::host::ResourceHost resource_host_; |
| 124 PepperDeviceEnumerationHostHelper device_enumeration_; | 122 PepperDeviceEnumerationHostHelper device_enumeration_; |
| 125 | 123 |
| 126 private: | 124 private: |
| 127 DISALLOW_COPY_AND_ASSIGN(PepperDeviceEnumerationHostHelperTest); | 125 DISALLOW_COPY_AND_ASSIGN(PepperDeviceEnumerationHostHelperTest); |
| 128 }; | 126 }; |
| 129 | 127 |
| 130 } // namespace | 128 } // namespace |
| 131 | 129 |
| 132 TEST_F(PepperDeviceEnumerationHostHelperTest, EnumerateDevices) { | 130 TEST_F(PepperDeviceEnumerationHostHelperTest, EnumerateDevices) { |
| 133 PpapiHostMsg_DeviceEnumeration_EnumerateDevices msg; | 131 PpapiHostMsg_DeviceEnumeration_EnumerateDevices msg; |
| 134 ppapi::proxy::ResourceMessageCallParams call_params( | 132 ppapi::proxy::ResourceMessageCallParams call_params( |
| 135 resource_host_.pp_resource(), 123); | 133 resource_host_.pp_resource(), 123); |
| 136 ppapi::host::HostMessageContext context(call_params); | 134 ppapi::host::HostMessageContext context(call_params); |
| 137 int32_t result = PP_ERROR_FAILED; | 135 int32_t result = PP_ERROR_FAILED; |
| 138 ASSERT_TRUE(device_enumeration_.HandleResourceMessage(msg, &context, | 136 ASSERT_TRUE( |
| 139 &result)); | 137 device_enumeration_.HandleResourceMessage(msg, &context, &result)); |
| 140 EXPECT_EQ(PP_OK_COMPLETIONPENDING, result); | 138 EXPECT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 141 | 139 |
| 142 EXPECT_EQ(1U, delegate_.GetRegisteredCallbackCount()); | 140 EXPECT_EQ(1U, delegate_.GetRegisteredCallbackCount()); |
| 143 int request_id = delegate_.last_used_id(); | 141 int request_id = delegate_.last_used_id(); |
| 144 | 142 |
| 145 std::vector<ppapi::DeviceRefData> data; | 143 std::vector<ppapi::DeviceRefData> data; |
| 146 ppapi::DeviceRefData data_item; | 144 ppapi::DeviceRefData data_item; |
| 147 data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE; | 145 data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE; |
| 148 data_item.name = "name_1"; | 146 data_item.name = "name_1"; |
| 149 data_item.id = "id_1"; | 147 data_item.id = "id_1"; |
| 150 data.push_back(data_item); | 148 data.push_back(data_item); |
| 151 data_item.type = PP_DEVICETYPE_DEV_VIDEOCAPTURE; | 149 data_item.type = PP_DEVICETYPE_DEV_VIDEOCAPTURE; |
| 152 data_item.name = "name_2"; | 150 data_item.name = "name_2"; |
| 153 data_item.id = "id_2"; | 151 data_item.id = "id_2"; |
| 154 data.push_back(data_item); | 152 data.push_back(data_item); |
| 155 ASSERT_TRUE(delegate_.SimulateEnumerateResult(request_id, data)); | 153 ASSERT_TRUE(delegate_.SimulateEnumerateResult(request_id, data)); |
| 156 | 154 |
| 157 // StopEnumerateDevices() should have been called since the EnumerateDevices | 155 // StopEnumerateDevices() should have been called since the EnumerateDevices |
| 158 // message is not a persistent request. | 156 // message is not a persistent request. |
| 159 EXPECT_EQ(0U, delegate_.GetRegisteredCallbackCount()); | 157 EXPECT_EQ(0U, delegate_.GetRegisteredCallbackCount()); |
| 160 | 158 |
| 161 // A reply message should have been sent to the test sink. | 159 // A reply message should have been sent to the test sink. |
| 162 ppapi::proxy::ResourceMessageReplyParams reply_params; | 160 ppapi::proxy::ResourceMessageReplyParams reply_params; |
| 163 IPC::Message reply_msg; | 161 IPC::Message reply_msg; |
| 164 ASSERT_TRUE(sink_.GetFirstResourceReplyMatching( | 162 ASSERT_TRUE(sink_.GetFirstResourceReplyMatching( |
| 165 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply::ID, | 163 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply::ID, |
| 166 &reply_params, &reply_msg)); | 164 &reply_params, |
| 165 &reply_msg)); |
| 167 | 166 |
| 168 EXPECT_EQ(call_params.sequence(), reply_params.sequence()); | 167 EXPECT_EQ(call_params.sequence(), reply_params.sequence()); |
| 169 EXPECT_EQ(PP_OK, reply_params.result()); | 168 EXPECT_EQ(PP_OK, reply_params.result()); |
| 170 | 169 |
| 171 std::vector<ppapi::DeviceRefData> reply_data; | 170 std::vector<ppapi::DeviceRefData> reply_data; |
| 172 ASSERT_TRUE(ppapi::UnpackMessage< | 171 ASSERT_TRUE(ppapi::UnpackMessage< |
| 173 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply>( | 172 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply>(reply_msg, |
| 174 reply_msg, &reply_data)); | 173 &reply_data)); |
| 175 EXPECT_EQ(data, reply_data); | 174 EXPECT_EQ(data, reply_data); |
| 176 } | 175 } |
| 177 | 176 |
| 178 TEST_F(PepperDeviceEnumerationHostHelperTest, MonitorDeviceChange) { | 177 TEST_F(PepperDeviceEnumerationHostHelperTest, MonitorDeviceChange) { |
| 179 uint32_t callback_id = 456; | 178 uint32_t callback_id = 456; |
| 180 SimulateMonitorDeviceChangeReceived(callback_id); | 179 SimulateMonitorDeviceChangeReceived(callback_id); |
| 181 | 180 |
| 182 EXPECT_EQ(1U, delegate_.GetRegisteredCallbackCount()); | 181 EXPECT_EQ(1U, delegate_.GetRegisteredCallbackCount()); |
| 183 int request_id = delegate_.last_used_id(); | 182 int request_id = delegate_.last_used_id(); |
| 184 | 183 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 data.push_back(data_item); | 217 data.push_back(data_item); |
| 219 ASSERT_TRUE(delegate_.SimulateEnumerateResult(request_id2, data)); | 218 ASSERT_TRUE(delegate_.SimulateEnumerateResult(request_id2, data)); |
| 220 | 219 |
| 221 CheckNotifyDeviceChangeMessage(callback_id2, data); | 220 CheckNotifyDeviceChangeMessage(callback_id2, data); |
| 222 | 221 |
| 223 PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange msg; | 222 PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange msg; |
| 224 ppapi::proxy::ResourceMessageCallParams call_params( | 223 ppapi::proxy::ResourceMessageCallParams call_params( |
| 225 resource_host_.pp_resource(), 123); | 224 resource_host_.pp_resource(), 123); |
| 226 ppapi::host::HostMessageContext context(call_params); | 225 ppapi::host::HostMessageContext context(call_params); |
| 227 int32_t result = PP_ERROR_FAILED; | 226 int32_t result = PP_ERROR_FAILED; |
| 228 ASSERT_TRUE(device_enumeration_.HandleResourceMessage( | 227 ASSERT_TRUE( |
| 229 msg, &context, &result)); | 228 device_enumeration_.HandleResourceMessage(msg, &context, &result)); |
| 230 EXPECT_EQ(PP_OK, result); | 229 EXPECT_EQ(PP_OK, result); |
| 231 | 230 |
| 232 EXPECT_EQ(0U, delegate_.GetRegisteredCallbackCount()); | 231 EXPECT_EQ(0U, delegate_.GetRegisteredCallbackCount()); |
| 233 } | 232 } |
| 234 | 233 |
| 235 } // namespace content | 234 } // namespace content |
| OLD | NEW |