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/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "content/browser/mock_resource_context.h" |
8 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h" | 9 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h" |
9 #include "content/browser/renderer_host/media/media_stream_manager.h" | 10 #include "content/browser/renderer_host/media/media_stream_manager.h" |
10 #include "content/browser/renderer_host/media/video_capture_manager.h" | 11 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 12 #include "content/browser/resource_context.h" |
11 #include "content/common/media/media_stream_messages.h" | 13 #include "content/common/media/media_stream_messages.h" |
12 #include "content/common/media/media_stream_options.h" | 14 #include "content/common/media/media_stream_options.h" |
13 #include "ipc/ipc_message_macros.h" | 15 #include "ipc/ipc_message_macros.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 using ::testing::_; | 19 using ::testing::_; |
18 using ::testing::DeleteArg; | 20 using ::testing::DeleteArg; |
19 using ::testing::DoAll; | 21 using ::testing::DoAll; |
20 using ::testing::Return; | 22 using ::testing::Return; |
21 | 23 |
22 const int kProcessId = 5; | 24 const int kProcessId = 5; |
23 const int kRenderId = 6; | 25 const int kRenderId = 6; |
24 const int kPageRequestId = 7; | 26 const int kPageRequestId = 7; |
25 | 27 |
26 namespace media_stream { | 28 namespace media_stream { |
27 | 29 |
28 class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost { | 30 class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost { |
29 public: | 31 public: |
30 explicit MockMediaStreamDispatcherHost(MessageLoop* message_loop) | 32 MockMediaStreamDispatcherHost(content::ResourceContext* resource_context, |
31 : MediaStreamDispatcherHost(kProcessId), | 33 MessageLoop* message_loop) |
| 34 : MediaStreamDispatcherHost(resource_context, kProcessId), |
32 message_loop_(message_loop) {} | 35 message_loop_(message_loop) {} |
33 virtual ~MockMediaStreamDispatcherHost() {} | 36 virtual ~MockMediaStreamDispatcherHost() {} |
34 | 37 |
35 // A list of mock methods. | 38 // A list of mock methods. |
36 MOCK_METHOD4(OnStreamGenerated, | 39 MOCK_METHOD4(OnStreamGenerated, |
37 void(int routing_id, int request_id, int audio_array_size, | 40 void(int routing_id, int request_id, int audio_array_size, |
38 int video_array_size)); | 41 int video_array_size)); |
39 MOCK_METHOD2(OnStreamGenerationFailed, void(int routing_id, int request_id)); | 42 MOCK_METHOD2(OnStreamGenerationFailed, void(int routing_id, int request_id)); |
40 MOCK_METHOD2(OnAudioDeviceFailed, void(int routing_id, int index)); | 43 MOCK_METHOD2(OnAudioDeviceFailed, void(int routing_id, int index)); |
41 MOCK_METHOD2(OnVideoDeviceFailed, void(int routing_id, int index)); | 44 MOCK_METHOD2(OnVideoDeviceFailed, void(int routing_id, int index)); |
42 | 45 |
43 // Accessor to private functions. | 46 // Accessor to private functions. |
44 void OnGenerateStream( | 47 void OnGenerateStream(int page_request_id, const StreamOptions& components) { |
45 int page_request_id, | |
46 const StreamOptions& components) { | |
47 MediaStreamDispatcherHost::OnGenerateStream(kRenderId, | 48 MediaStreamDispatcherHost::OnGenerateStream(kRenderId, |
48 page_request_id, | 49 page_request_id, |
49 components, | 50 components, |
50 std::string()); | 51 std::string()); |
51 } | 52 } |
52 void OnStopGeneratedStream(const std::string& label) { | 53 void OnStopGeneratedStream(const std::string& label) { |
53 MediaStreamDispatcherHost::OnStopGeneratedStream(kRenderId, label); | 54 MediaStreamDispatcherHost::OnStopGeneratedStream(kRenderId, label); |
54 } | 55 } |
55 | 56 |
56 // Return the number of streams that have been opened or is being open. | 57 // Return the number of streams that have been opened or is being open. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 StreamDeviceInfoArray video_device_list) { | 98 StreamDeviceInfoArray video_device_list) { |
98 OnStreamGenerated(msg.routing_id(), request_id, audio_device_list.size(), | 99 OnStreamGenerated(msg.routing_id(), request_id, audio_device_list.size(), |
99 video_device_list.size()); | 100 video_device_list.size()); |
100 // Notify that the event have occured. | 101 // Notify that the event have occured. |
101 message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 102 message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
102 label_ = label; | 103 label_ = label; |
103 audio_devices_ = audio_device_list; | 104 audio_devices_ = audio_device_list; |
104 video_devices_ = video_device_list; | 105 video_devices_ = video_device_list; |
105 } | 106 } |
106 | 107 |
107 void OnStreamGenerationFailed(const IPC::Message& msg, | 108 void OnStreamGenerationFailed(const IPC::Message& msg, int request_id) { |
108 int request_id) { | |
109 OnStreamGenerationFailed(msg.routing_id(), request_id); | 109 OnStreamGenerationFailed(msg.routing_id(), request_id); |
110 message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 110 message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
111 label_= ""; | 111 label_= ""; |
112 } | 112 } |
113 | 113 |
114 void OnAudioDeviceFailed(const IPC::Message& msg, | 114 void OnAudioDeviceFailed(const IPC::Message& msg, |
115 std::string label, | 115 std::string label, |
116 int index) { | 116 int index) { |
117 OnAudioDeviceFailed(msg.routing_id(), index); | 117 OnAudioDeviceFailed(msg.routing_id(), index); |
118 audio_devices_.erase(audio_devices_.begin() + index); | 118 audio_devices_.erase(audio_devices_.begin() + index); |
(...skipping 12 matching lines...) Expand all Loading... |
131 }; | 131 }; |
132 | 132 |
133 class MediaStreamDispatcherHostTest : public testing::Test { | 133 class MediaStreamDispatcherHostTest : public testing::Test { |
134 public: | 134 public: |
135 void WaitForResult() { | 135 void WaitForResult() { |
136 message_loop_->Run(); | 136 message_loop_->Run(); |
137 } | 137 } |
138 | 138 |
139 protected: | 139 protected: |
140 virtual void SetUp() { | 140 virtual void SetUp() { |
141 // Create message loop so that | |
142 // DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)) passes. | |
143 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | 141 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
| 142 // ResourceContext must be created on UI thread. |
| 143 ui_thread_.reset(new BrowserThread(BrowserThread::UI, message_loop_.get())); |
| 144 // MediaStreamManager must be created and called on IO thread. |
144 io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get())); | 145 io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get())); |
145 | 146 |
146 // Make sure the MediaStreamManager exist and use | 147 // Create a MediaStreamManager instance and hand over pointer to |
147 // fake audio / video devices. | 148 // ResourceContext. |
148 MediaStreamManager* manager = MediaStreamManager::Get(); | 149 media_stream_manager_.reset(new MediaStreamManager()); |
149 manager->UseFakeDevice(); | 150 // Make sure we use fake devices to avoid long delays. |
| 151 media_stream_manager_->UseFakeDevice(); |
| 152 content::MockResourceContext::GetInstance()->set_media_stream_manager( |
| 153 media_stream_manager_.get()); |
150 | 154 |
151 host_ = | 155 host_ = new MockMediaStreamDispatcherHost( |
152 new MockMediaStreamDispatcherHost(message_loop_.get()); | 156 content::MockResourceContext::GetInstance(), message_loop_.get()); |
| 157 } |
| 158 |
| 159 virtual void TearDown() { |
| 160 content::MockResourceContext::GetInstance()->set_media_stream_manager(NULL); |
| 161 |
| 162 // Needed to make sure the manager finishes all tasks on its own thread. |
| 163 SyncWithVideoCaptureManagerThread(); |
| 164 } |
| 165 |
| 166 // Called on the VideoCaptureManager thread. |
| 167 static void PostQuitMessageLoop(MessageLoop* message_loop) { |
| 168 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 169 } |
| 170 |
| 171 // Called on the main thread. |
| 172 static void PostQuitOnVideoCaptureManagerThread( |
| 173 MessageLoop* message_loop, |
| 174 media_stream::MediaStreamManager* media_stream_manager) { |
| 175 media_stream_manager->video_capture_manager()->GetMessageLoop()-> |
| 176 PostTask(FROM_HERE, |
| 177 NewRunnableFunction(&PostQuitMessageLoop, message_loop)); |
| 178 } |
| 179 |
| 180 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the |
| 181 // video_capture_manager thread are executed while also processing pending |
| 182 // task in message_loop_ on the current thread. It is used to synchronize |
| 183 // with the video capture manager thread when we are stopping a video |
| 184 // capture device. |
| 185 void SyncWithVideoCaptureManagerThread() { |
| 186 message_loop_->PostTask( |
| 187 FROM_HERE, |
| 188 NewRunnableFunction(&PostQuitOnVideoCaptureManagerThread, |
| 189 message_loop_.get(), |
| 190 media_stream_manager_.get())); |
| 191 message_loop_->Run(); |
153 } | 192 } |
154 | 193 |
155 scoped_refptr<MockMediaStreamDispatcherHost> host_; | 194 scoped_refptr<MockMediaStreamDispatcherHost> host_; |
156 scoped_ptr<MessageLoop> message_loop_; | 195 scoped_ptr<MessageLoop> message_loop_; |
| 196 scoped_ptr<BrowserThread> ui_thread_; |
157 scoped_ptr<BrowserThread> io_thread_; | 197 scoped_ptr<BrowserThread> io_thread_; |
| 198 scoped_ptr<MediaStreamManager> media_stream_manager_; |
158 }; | 199 }; |
159 | 200 |
160 TEST_F(MediaStreamDispatcherHostTest, GenerateStream) { | 201 TEST_F(MediaStreamDispatcherHostTest, GenerateStream) { |
161 StreamOptions options(false, StreamOptions::kFacingUser); | 202 StreamOptions options(false, StreamOptions::kFacingUser); |
162 | 203 |
163 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId, 0, 1)); | 204 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId, 0, 1)); |
164 host_->OnGenerateStream(kPageRequestId, options); | 205 host_->OnGenerateStream(kPageRequestId, options); |
165 | 206 |
166 WaitForResult(); | 207 WaitForResult(); |
167 | 208 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 host_->OnGenerateStream(kPageRequestId, options); | 257 host_->OnGenerateStream(kPageRequestId, options); |
217 WaitForResult(); | 258 WaitForResult(); |
218 std::string label = host_->label_; | 259 std::string label = host_->label_; |
219 | 260 |
220 EXPECT_EQ(host_->audio_devices_.size(), 0u); | 261 EXPECT_EQ(host_->audio_devices_.size(), 0u); |
221 EXPECT_EQ(host_->video_devices_.size(), 1u); | 262 EXPECT_EQ(host_->video_devices_.size(), 1u); |
222 EXPECT_EQ(host_->NumberOfStreams(), 1u); | 263 EXPECT_EQ(host_->NumberOfStreams(), 1u); |
223 | 264 |
224 EXPECT_CALL(*host_, OnVideoDeviceFailed(kRenderId, 0)); | 265 EXPECT_CALL(*host_, OnVideoDeviceFailed(kRenderId, 0)); |
225 int session_id = host_->video_devices_[0].session_id; | 266 int session_id = host_->video_devices_[0].session_id; |
226 MediaStreamManager::Get()->video_capture_manager()->Error(session_id); | 267 content::MockResourceContext::GetInstance()->media_stream_manager()-> |
| 268 video_capture_manager()->Error(session_id); |
227 WaitForResult(); | 269 WaitForResult(); |
228 EXPECT_EQ(host_->video_devices_.size(), 0u); | 270 EXPECT_EQ(host_->video_devices_.size(), 0u); |
229 EXPECT_EQ(host_->NumberOfStreams(), 1u); | 271 EXPECT_EQ(host_->NumberOfStreams(), 1u); |
230 | 272 |
231 // TODO(perkj): test audio device failure? | 273 // TODO(perkj): test audio device failure? |
232 | 274 |
233 host_->OnStopGeneratedStream(label); | 275 host_->OnStopGeneratedStream(label); |
234 EXPECT_EQ(host_->NumberOfStreams(), 0u); | 276 EXPECT_EQ(host_->NumberOfStreams(), 0u); |
235 } | 277 } |
236 | 278 |
237 }; // namespace media_stream | 279 }; // namespace media_stream |
OLD | NEW |