| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "content/common/media/media_stream_messages.h" | 10 #include "content/common/media/media_stream_messages.h" |
| 11 #include "content/renderer/media/media_stream_dispatcher.h" | 11 #include "content/renderer/media/media_stream_dispatcher.h" |
| 12 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h" | 12 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 const int kRouteId = 0; | 17 const int kRouteId = 0; |
| 18 const int kAudioSessionId = 3; | 18 const int kAudioSessionId = 3; |
| 19 const int kVideoSessionId = 5; | 19 const int kVideoSessionId = 5; |
| 20 const int kRequestId1 = 10; | 20 const int kRequestId1 = 10; |
| 21 const int kRequestId2 = 20; | 21 const int kRequestId2 = 20; |
| 22 const int kRequestId3 = 30; |
| 23 const int kRequestId4 = 40; |
| 22 | 24 |
| 23 class MockMediaStreamDispatcherEventHandler | 25 class MockMediaStreamDispatcherEventHandler |
| 24 : public MediaStreamDispatcherEventHandler { | 26 : public MediaStreamDispatcherEventHandler { |
| 25 public: | 27 public: |
| 26 MockMediaStreamDispatcherEventHandler() | 28 MockMediaStreamDispatcherEventHandler() |
| 27 : request_id_(-1), | 29 : request_id_(-1), |
| 28 audio_failed(false), | 30 audio_failed(false), |
| 29 video_failed(false) {} | 31 video_failed(false) {} |
| 30 | 32 |
| 31 virtual void OnStreamGenerated( | 33 virtual void OnStreamGenerated( |
| 32 int request_id, | 34 int request_id, |
| 33 const std::string &label, | 35 const std::string &label, |
| 34 const media_stream::StreamDeviceInfoArray& audio_device_array, | 36 const media_stream::StreamDeviceInfoArray& audio_device_array, |
| 35 const media_stream::StreamDeviceInfoArray& video_device_array) { | 37 const media_stream::StreamDeviceInfoArray& video_device_array) OVERRIDE { |
| 36 request_id_ = request_id; | 38 request_id_ = request_id; |
| 37 label_ = label; | 39 label_ = label; |
| 38 } | 40 } |
| 39 | 41 |
| 40 virtual void OnStreamGenerationFailed(int request_id) { | 42 virtual void OnStreamGenerationFailed(int request_id) OVERRIDE { |
| 41 request_id_ = request_id; | 43 request_id_ = request_id; |
| 42 } | 44 } |
| 43 | 45 |
| 44 virtual void OnAudioDeviceFailed(const std::string& label, | 46 virtual void OnAudioDeviceFailed(const std::string& label, |
| 45 int index) { | 47 int index) OVERRIDE { |
| 46 audio_failed = true; | 48 audio_failed = true; |
| 47 } | 49 } |
| 48 | 50 |
| 49 virtual void OnVideoDeviceFailed(const std::string& label, | 51 virtual void OnVideoDeviceFailed(const std::string& label, |
| 50 int index) { | 52 int index) OVERRIDE { |
| 51 video_failed = true; | 53 video_failed = true; |
| 52 } | 54 } |
| 53 | 55 |
| 56 virtual void OnDevicesEnumerated( |
| 57 int request_id, |
| 58 const media_stream::StreamDeviceInfoArray& device_array) OVERRIDE { |
| 59 request_id_ = request_id; |
| 60 } |
| 61 |
| 62 virtual void OnDevicesEnumerationFailed(int request_id) OVERRIDE { |
| 63 request_id_ = request_id; |
| 64 } |
| 65 |
| 66 virtual void OnDeviceOpened( |
| 67 int request_id, |
| 68 const std::string& label, |
| 69 const media_stream::StreamDeviceInfo& video_device) OVERRIDE { |
| 70 request_id_ = request_id; |
| 71 label_ = label; |
| 72 } |
| 73 |
| 74 virtual void OnDeviceOpenFailed(int request_id) OVERRIDE { |
| 75 request_id_ = request_id; |
| 76 } |
| 77 |
| 54 int request_id_; | 78 int request_id_; |
| 55 std::string label_; | 79 std::string label_; |
| 56 bool audio_failed; | 80 bool audio_failed; |
| 57 bool video_failed; | 81 bool video_failed; |
| 58 | 82 |
| 59 DISALLOW_COPY_AND_ASSIGN(MockMediaStreamDispatcherEventHandler); | 83 DISALLOW_COPY_AND_ASSIGN(MockMediaStreamDispatcherEventHandler); |
| 60 }; | 84 }; |
| 61 | 85 |
| 62 } // namespace | 86 } // namespace |
| 63 | 87 |
| 64 TEST(MediaStreamDispatcherTest, Basic) { | 88 TEST(MediaStreamDispatcherTest, BasicStream) { |
| 65 scoped_ptr<MediaStreamDispatcher> dispatcher(new MediaStreamDispatcher(NULL)); | 89 scoped_ptr<MediaStreamDispatcher> dispatcher(new MediaStreamDispatcher(NULL)); |
| 66 scoped_ptr<MockMediaStreamDispatcherEventHandler> | 90 scoped_ptr<MockMediaStreamDispatcherEventHandler> |
| 67 handler(new MockMediaStreamDispatcherEventHandler); | 91 handler(new MockMediaStreamDispatcherEventHandler); |
| 68 media_stream::StreamOptions components( | 92 media_stream::StreamOptions components( |
| 69 true, media_stream::StreamOptions::kFacingUser); | 93 true, media_stream::StreamOptions::kFacingUser); |
| 70 std::string security_origin; | 94 std::string security_origin; |
| 71 | 95 |
| 72 int ipc_request_id1 = dispatcher->next_ipc_id_; | 96 int ipc_request_id1 = dispatcher->next_ipc_id_; |
| 73 dispatcher->GenerateStream(kRequestId1, handler.get(), components, | 97 dispatcher->GenerateStream(kRequestId1, handler.get(), components, |
| 74 security_origin); | 98 security_origin); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 media_stream::StreamDeviceInfo::kNoId); | 152 media_stream::StreamDeviceInfo::kNoId); |
| 129 EXPECT_EQ(dispatcher->video_session_id(stream_label1, 0), | 153 EXPECT_EQ(dispatcher->video_session_id(stream_label1, 0), |
| 130 media_stream::StreamDeviceInfo::kNoId); | 154 media_stream::StreamDeviceInfo::kNoId); |
| 131 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); | 155 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); |
| 132 | 156 |
| 133 // Verify that the request have been completed. | 157 // Verify that the request have been completed. |
| 134 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); | 158 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); |
| 135 EXPECT_EQ(dispatcher->requests_.size(), size_t(0)); | 159 EXPECT_EQ(dispatcher->requests_.size(), size_t(0)); |
| 136 } | 160 } |
| 137 | 161 |
| 162 TEST(MediaStreamDispatcherTest, BasicVideoDevice) { |
| 163 scoped_ptr<MediaStreamDispatcher> dispatcher(new MediaStreamDispatcher(NULL)); |
| 164 scoped_ptr<MockMediaStreamDispatcherEventHandler> |
| 165 handler(new MockMediaStreamDispatcherEventHandler); |
| 166 std::string security_origin; |
| 167 |
| 168 int ipc_request_id1 = dispatcher->next_ipc_id_; |
| 169 dispatcher->EnumerateDevices(kRequestId1, handler.get(), |
| 170 media_stream::kVideoCapture, security_origin); |
| 171 int ipc_request_id2 = dispatcher->next_ipc_id_; |
| 172 EXPECT_NE(ipc_request_id1, ipc_request_id2); |
| 173 dispatcher->EnumerateDevices(kRequestId2, handler.get(), |
| 174 media_stream::kVideoCapture, security_origin); |
| 175 EXPECT_EQ(dispatcher->requests_.size(), size_t(2)); |
| 176 |
| 177 media_stream::StreamDeviceInfoArray video_device_array(1); |
| 178 media_stream::StreamDeviceInfo video_device_info; |
| 179 video_device_info.name = "Camera"; |
| 180 video_device_info.device_id = "device_path"; |
| 181 video_device_info.stream_type = media_stream::kVideoCapture; |
| 182 video_device_info.session_id = kVideoSessionId; |
| 183 video_device_array[0] = video_device_info; |
| 184 |
| 185 // Complete the enumeration of request 1. |
| 186 dispatcher->OnMessageReceived(MediaStreamMsg_DevicesEnumerated( |
| 187 kRouteId, ipc_request_id1, video_device_array)); |
| 188 EXPECT_EQ(handler->request_id_, kRequestId1); |
| 189 |
| 190 // Complete the enumeration of request 2. |
| 191 dispatcher->OnMessageReceived(MediaStreamMsg_DevicesEnumerated( |
| 192 kRouteId, ipc_request_id2, video_device_array)); |
| 193 EXPECT_EQ(handler->request_id_, kRequestId2); |
| 194 |
| 195 EXPECT_EQ(dispatcher->requests_.size(), size_t(0)); |
| 196 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); |
| 197 |
| 198 int ipc_request_id3 = dispatcher->next_ipc_id_; |
| 199 dispatcher->OpenDevice(kRequestId3, handler.get(), |
| 200 video_device_info.device_id, |
| 201 media_stream::kVideoCapture, security_origin); |
| 202 int ipc_request_id4 = dispatcher->next_ipc_id_; |
| 203 EXPECT_NE(ipc_request_id3, ipc_request_id4); |
| 204 dispatcher->OpenDevice(kRequestId4, handler.get(), |
| 205 video_device_info.device_id, |
| 206 media_stream::kVideoCapture, security_origin); |
| 207 EXPECT_EQ(dispatcher->requests_.size(), size_t(2)); |
| 208 |
| 209 // Complete the OpenDevice of request 1. |
| 210 std::string stream_label1 = std::string("stream1"); |
| 211 dispatcher->OnMessageReceived(MediaStreamMsg_DeviceOpened( |
| 212 kRouteId, ipc_request_id3, stream_label1, video_device_info)); |
| 213 EXPECT_EQ(handler->request_id_, kRequestId3); |
| 214 |
| 215 // Complete the OpenDevice of request 2. |
| 216 std::string stream_label2 = std::string("stream2"); |
| 217 dispatcher->OnMessageReceived(MediaStreamMsg_DeviceOpened( |
| 218 kRouteId, ipc_request_id4, stream_label2, video_device_info)); |
| 219 EXPECT_EQ(handler->request_id_, kRequestId4); |
| 220 |
| 221 EXPECT_EQ(dispatcher->requests_.size(), size_t(0)); |
| 222 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(2)); |
| 223 |
| 224 // Check the video_session_id. |
| 225 EXPECT_EQ(dispatcher->video_session_id(stream_label1, 0), kVideoSessionId); |
| 226 EXPECT_EQ(dispatcher->video_session_id(stream_label2, 0), kVideoSessionId); |
| 227 |
| 228 // Stop stream2. |
| 229 dispatcher->StopStream(stream_label2); |
| 230 EXPECT_EQ(dispatcher->video_session_id(stream_label2, 0), |
| 231 media_stream::StreamDeviceInfo::kNoId); |
| 232 |
| 233 // Stop stream1. |
| 234 dispatcher->StopStream(stream_label1); |
| 235 EXPECT_EQ(dispatcher->video_session_id(stream_label1, 0), |
| 236 media_stream::StreamDeviceInfo::kNoId); |
| 237 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); |
| 238 |
| 239 // Verify that the request have been completed. |
| 240 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); |
| 241 EXPECT_EQ(dispatcher->requests_.size(), size_t(0)); |
| 242 } |
| 243 |
| 138 TEST(MediaStreamDispatcherTest, TestFailure) { | 244 TEST(MediaStreamDispatcherTest, TestFailure) { |
| 139 scoped_ptr<MediaStreamDispatcher> dispatcher(new MediaStreamDispatcher(NULL)); | 245 scoped_ptr<MediaStreamDispatcher> dispatcher(new MediaStreamDispatcher(NULL)); |
| 140 scoped_ptr<MockMediaStreamDispatcherEventHandler> | 246 scoped_ptr<MockMediaStreamDispatcherEventHandler> |
| 141 handler(new MockMediaStreamDispatcherEventHandler); | 247 handler(new MockMediaStreamDispatcherEventHandler); |
| 142 media_stream::StreamOptions components( | 248 media_stream::StreamOptions components( |
| 143 true, media_stream::StreamOptions::kFacingUser); | 249 true, media_stream::StreamOptions::kFacingUser); |
| 144 std::string security_origin; | 250 std::string security_origin; |
| 145 | 251 |
| 146 // Test failure when creating a stream. | 252 // Test failure when creating a stream. |
| 147 int ipc_request_id1 = dispatcher->next_ipc_id_; | 253 int ipc_request_id1 = dispatcher->next_ipc_id_; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 298 |
| 193 // Test failure of the audio device. | 299 // Test failure of the audio device. |
| 194 dispatcher->OnMessageReceived( | 300 dispatcher->OnMessageReceived( |
| 195 MediaStreamHostMsg_AudioDeviceFailed(kRouteId, stream_label1, 0)); | 301 MediaStreamHostMsg_AudioDeviceFailed(kRouteId, stream_label1, 0)); |
| 196 EXPECT_EQ(handler->audio_failed, true); | 302 EXPECT_EQ(handler->audio_failed, true); |
| 197 | 303 |
| 198 // Stop stream1. | 304 // Stop stream1. |
| 199 dispatcher->StopStream(stream_label1); | 305 dispatcher->StopStream(stream_label1); |
| 200 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); | 306 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); |
| 201 } | 307 } |
| OLD | NEW |