OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "content/common/media/media_stream_messages.h" |
| 11 #include "content/renderer/media/media_stream_dispatcher.h" |
| 12 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 const int kRouteId = 0; |
| 18 const int kAudioSessionId = 3; |
| 19 const int kVideoSessionId = 5; |
| 20 const int kRequestId1 = 10; |
| 21 const int kRequestId2 = 20; |
| 22 |
| 23 class MockMediaStreamDispatcherEventHandler |
| 24 : public MediaStreamDispatcherEventHandler { |
| 25 public: |
| 26 MockMediaStreamDispatcherEventHandler() |
| 27 : request_id_(-1), |
| 28 audio_failed(false), |
| 29 video_failed(false) {} |
| 30 |
| 31 virtual void OnStreamGenerated( |
| 32 int request_id, |
| 33 const std::string &label, |
| 34 const media_stream::StreamDeviceInfoArray& audio_device_array, |
| 35 const media_stream::StreamDeviceInfoArray& video_device_array) { |
| 36 request_id_ = request_id; |
| 37 label_ = label; |
| 38 } |
| 39 |
| 40 virtual void OnStreamGenerationFailed(int request_id) { |
| 41 request_id_ = request_id; |
| 42 } |
| 43 |
| 44 virtual void OnAudioDeviceFailed(const std::string& label, |
| 45 int index) { |
| 46 audio_failed = true; |
| 47 } |
| 48 |
| 49 virtual void OnVideoDeviceFailed(const std::string& label, |
| 50 int index) { |
| 51 video_failed = true; |
| 52 } |
| 53 |
| 54 int request_id_; |
| 55 std::string label_; |
| 56 bool audio_failed; |
| 57 bool video_failed; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(MockMediaStreamDispatcherEventHandler); |
| 60 }; |
| 61 |
| 62 } // namespace |
| 63 |
| 64 TEST(MediaStreamDispatcherTest, Basic) { |
| 65 scoped_ptr<MediaStreamDispatcher> dispatcher(new MediaStreamDispatcher(NULL)); |
| 66 scoped_ptr<MockMediaStreamDispatcherEventHandler> |
| 67 handler(new MockMediaStreamDispatcherEventHandler); |
| 68 media_stream::StreamOptions components( |
| 69 true, media_stream::StreamOptions::kFacingUser); |
| 70 std::string security_origin; |
| 71 |
| 72 int ipc_request_id1 = dispatcher->next_ipc_id_; |
| 73 dispatcher->GenerateStream(kRequestId1, handler.get(), components, |
| 74 security_origin); |
| 75 int ipc_request_id2 = dispatcher->next_ipc_id_; |
| 76 EXPECT_NE(ipc_request_id1, ipc_request_id2); |
| 77 dispatcher->GenerateStream(kRequestId2, handler.get(), components, |
| 78 security_origin); |
| 79 EXPECT_EQ(dispatcher->requests_.size(), size_t(2)); |
| 80 |
| 81 media_stream::StreamDeviceInfoArray audio_device_array(1); |
| 82 media_stream::StreamDeviceInfo audio_device_info; |
| 83 audio_device_info.name = "Microphone"; |
| 84 audio_device_info.stream_type = media_stream::kAudioCapture; |
| 85 audio_device_info.session_id = kAudioSessionId; |
| 86 audio_device_array[0] = audio_device_info; |
| 87 |
| 88 media_stream::StreamDeviceInfoArray video_device_array(1); |
| 89 media_stream::StreamDeviceInfo video_device_info; |
| 90 video_device_info.name = "Camera"; |
| 91 video_device_info.stream_type = media_stream::kVideoCapture; |
| 92 video_device_info.session_id = kVideoSessionId; |
| 93 video_device_array[0] = video_device_info; |
| 94 |
| 95 // Complete the creation of stream1. |
| 96 std::string stream_label1 = std::string("stream1"); |
| 97 dispatcher->OnMessageReceived(MediaStreamMsg_StreamGenerated( |
| 98 kRouteId, ipc_request_id1, stream_label1, |
| 99 audio_device_array, video_device_array)); |
| 100 EXPECT_EQ(handler->request_id_, kRequestId1); |
| 101 EXPECT_EQ(handler->label_, stream_label1); |
| 102 |
| 103 // Complete the creation of stream2. |
| 104 std::string stream_label2 = std::string("stream2"); |
| 105 dispatcher->OnMessageReceived(MediaStreamMsg_StreamGenerated( |
| 106 kRouteId, ipc_request_id2, stream_label2, |
| 107 audio_device_array, video_device_array)); |
| 108 EXPECT_EQ(handler->request_id_, kRequestId2); |
| 109 EXPECT_EQ(handler->label_, stream_label2); |
| 110 |
| 111 EXPECT_EQ(dispatcher->requests_.size(), size_t(0)); |
| 112 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(2)); |
| 113 |
| 114 // Check the session_id of stream2. |
| 115 EXPECT_EQ(dispatcher->audio_session_id(stream_label2, 0), kAudioSessionId); |
| 116 EXPECT_EQ(dispatcher->video_session_id(stream_label2, 0), kVideoSessionId); |
| 117 |
| 118 // Stop stream2. |
| 119 dispatcher->StopStream(stream_label2); |
| 120 EXPECT_EQ(dispatcher->audio_session_id(stream_label2, 0), |
| 121 media_stream::StreamDeviceInfo::kNoId); |
| 122 EXPECT_EQ(dispatcher->video_session_id(stream_label2, 0), |
| 123 media_stream::StreamDeviceInfo::kNoId); |
| 124 |
| 125 // Stop stream1. |
| 126 dispatcher->StopStream(stream_label1); |
| 127 EXPECT_EQ(dispatcher->audio_session_id(stream_label1, 0), |
| 128 media_stream::StreamDeviceInfo::kNoId); |
| 129 EXPECT_EQ(dispatcher->video_session_id(stream_label1, 0), |
| 130 media_stream::StreamDeviceInfo::kNoId); |
| 131 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); |
| 132 |
| 133 // Verify that the request have been completed. |
| 134 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); |
| 135 EXPECT_EQ(dispatcher->requests_.size(), size_t(0)); |
| 136 } |
| 137 |
| 138 TEST(MediaStreamDispatcherTest, TestFailure) { |
| 139 scoped_ptr<MediaStreamDispatcher> dispatcher(new MediaStreamDispatcher(NULL)); |
| 140 scoped_ptr<MockMediaStreamDispatcherEventHandler> |
| 141 handler(new MockMediaStreamDispatcherEventHandler); |
| 142 media_stream::StreamOptions components( |
| 143 true, media_stream::StreamOptions::kFacingUser); |
| 144 std::string security_origin; |
| 145 |
| 146 // Test failure when creating a stream. |
| 147 int ipc_request_id1 = dispatcher->next_ipc_id_; |
| 148 dispatcher->GenerateStream(kRequestId1, handler.get(), components, |
| 149 security_origin); |
| 150 dispatcher->OnMessageReceived(MediaStreamMsg_StreamGenerationFailed( |
| 151 kRouteId, ipc_request_id1)); |
| 152 |
| 153 // Verify that the request have been completed. |
| 154 EXPECT_EQ(handler->request_id_, kRequestId1); |
| 155 EXPECT_EQ(dispatcher->requests_.size(), size_t(0)); |
| 156 |
| 157 // Create a new stream. |
| 158 ipc_request_id1 = dispatcher->next_ipc_id_; |
| 159 dispatcher->GenerateStream(kRequestId1, handler.get(), components, |
| 160 security_origin); |
| 161 |
| 162 media_stream::StreamDeviceInfoArray audio_device_array(1); |
| 163 media_stream::StreamDeviceInfo audio_device_info; |
| 164 audio_device_info.name = "Microphone"; |
| 165 audio_device_info.stream_type = media_stream::kAudioCapture; |
| 166 audio_device_info.session_id = kAudioSessionId; |
| 167 audio_device_array[0] = audio_device_info; |
| 168 |
| 169 media_stream::StreamDeviceInfoArray video_device_array(1); |
| 170 media_stream::StreamDeviceInfo video_device_info; |
| 171 video_device_info.name = "Camera"; |
| 172 video_device_info.stream_type = media_stream::kVideoCapture; |
| 173 video_device_info.session_id = kVideoSessionId; |
| 174 video_device_array[0] = video_device_info; |
| 175 |
| 176 // Complete the creation of stream1. |
| 177 std::string stream_label1 = std::string("stream1"); |
| 178 dispatcher->OnMessageReceived(MediaStreamMsg_StreamGenerated( |
| 179 kRouteId, ipc_request_id1, stream_label1, |
| 180 audio_device_array, video_device_array)); |
| 181 EXPECT_EQ(handler->request_id_, kRequestId1); |
| 182 EXPECT_EQ(handler->label_, stream_label1); |
| 183 EXPECT_EQ(dispatcher->video_session_id(stream_label1, 0), kVideoSessionId); |
| 184 |
| 185 // Test failure of the video device. |
| 186 dispatcher->OnMessageReceived( |
| 187 MediaStreamHostMsg_VideoDeviceFailed(kRouteId, stream_label1, 0)); |
| 188 EXPECT_EQ(handler->video_failed, true); |
| 189 EXPECT_EQ(handler->audio_failed, false); |
| 190 // Make sure the audio device still exist but not the video device. |
| 191 EXPECT_EQ(dispatcher->audio_session_id(stream_label1, 0), kAudioSessionId); |
| 192 |
| 193 // Test failure of the audio device. |
| 194 dispatcher->OnMessageReceived( |
| 195 MediaStreamHostMsg_AudioDeviceFailed(kRouteId, stream_label1, 0)); |
| 196 EXPECT_EQ(handler->audio_failed, true); |
| 197 |
| 198 // Stop stream1. |
| 199 dispatcher->StopStream(stream_label1); |
| 200 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); |
| 201 } |
OLD | NEW |