Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(390)

Side by Side Diff: content/browser/renderer_host/media/media_stream_dispatcher_host_unittest.cc

Issue 7649016: Removing singleton property of MediaStreamManager and creating thread first when needed. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/renderer_host/media/media_stream_dispatcher_host.h" 8 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h"
9 #include "content/browser/renderer_host/media/media_stream_manager.h" 9 #include "content/browser/renderer_host/media/media_stream_manager.h"
10 #include "content/browser/renderer_host/media/video_capture_manager.h" 10 #include "content/browser/renderer_host/media/video_capture_manager.h"
11 #include "content/browser/resource_context.h"
11 #include "content/common/media/media_stream_messages.h" 12 #include "content/common/media/media_stream_messages.h"
12 #include "content/common/media/media_stream_options.h" 13 #include "content/common/media/media_stream_options.h"
13 #include "ipc/ipc_message_macros.h" 14 #include "ipc/ipc_message_macros.h"
14 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 using ::testing::_; 18 using ::testing::_;
18 using ::testing::DeleteArg; 19 using ::testing::DeleteArg;
19 using ::testing::DoAll; 20 using ::testing::DoAll;
20 using ::testing::Return; 21 using ::testing::Return;
21 22
22 const int kProcessId = 5; 23 const int kProcessId = 5;
23 const int kRenderId = 6; 24 const int kRenderId = 6;
24 const int kPageRequestId = 7; 25 const int kPageRequestId = 7;
25 26
27 namespace content {
28 // Test version of ResourceContext
29 class MockMediaStreamResourceContext : public ResourceContext {
scherkus (not reviewing) 2011/08/16 19:24:09 you might be able to use MockResourceContext cont
mflodman_chromium_OOO 2011/08/19 11:07:09 This part was removed based on review feedback fro
30 public:
31 MockMediaStreamResourceContext() {}
32 virtual ~MockMediaStreamResourceContext() {}
33
34 virtual media_stream::MediaStreamManager* media_stream_manger() const {
35 return media_stream_manager_.get();
36 }
37
38 void set_media_stream_manager(
39 media_stream::MediaStreamManager* media_stream_manager) {
40 media_stream_manager_ = media_stream_manager;
41 }
42
43 private:
44 // Pure virtual in ResourceContext.
45 virtual void EnsureInitialized() const {}
46
47 scoped_refptr<media_stream::MediaStreamManager> media_stream_manager_;
48 };
49
50 } // namespace content
51
26 namespace media_stream { 52 namespace media_stream {
27 53
28 class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost { 54 class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost {
29 public: 55 public:
30 explicit MockMediaStreamDispatcherHost(MessageLoop* message_loop) 56 MockMediaStreamDispatcherHost(content::ResourceContext* resource_context,
31 : MediaStreamDispatcherHost(kProcessId), 57 MessageLoop* message_loop)
58 : MediaStreamDispatcherHost(resource_context, kProcessId),
32 message_loop_(message_loop) {} 59 message_loop_(message_loop) {}
33 virtual ~MockMediaStreamDispatcherHost() {} 60 virtual ~MockMediaStreamDispatcherHost() {}
34 61
35 // A list of mock methods. 62 // A list of mock methods.
36 MOCK_METHOD4(OnStreamGenerated, 63 MOCK_METHOD4(OnStreamGenerated,
37 void(int routing_id, int request_id, int audio_array_size, 64 void(int routing_id, int request_id, int audio_array_size,
38 int video_array_size)); 65 int video_array_size));
39 MOCK_METHOD2(OnStreamGenerationFailed, void(int routing_id, int request_id)); 66 MOCK_METHOD2(OnStreamGenerationFailed, void(int routing_id, int request_id));
40 MOCK_METHOD2(OnAudioDeviceFailed, void(int routing_id, int index)); 67 MOCK_METHOD2(OnAudioDeviceFailed, void(int routing_id, int index));
41 MOCK_METHOD2(OnVideoDeviceFailed, void(int routing_id, int index)); 68 MOCK_METHOD2(OnVideoDeviceFailed, void(int routing_id, int index));
42 69
43 // Accessor to private functions. 70 // Accessor to private functions.
44 void OnGenerateStream( 71 void OnGenerateStream(int page_request_id, const StreamOptions& components) {
45 int page_request_id,
46 const StreamOptions& components) {
47 MediaStreamDispatcherHost::OnGenerateStream(kRenderId, 72 MediaStreamDispatcherHost::OnGenerateStream(kRenderId,
48 page_request_id, 73 page_request_id,
49 components, 74 components,
50 std::string()); 75 std::string());
51 } 76 }
52 void OnStopGeneratedStream(const std::string& label) { 77 void OnStopGeneratedStream(const std::string& label) {
53 MediaStreamDispatcherHost::OnStopGeneratedStream(kRenderId, label); 78 MediaStreamDispatcherHost::OnStopGeneratedStream(kRenderId, label);
54 } 79 }
55 80
56 // Return the number of streams that have been opened or is being open. 81 // 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
97 StreamDeviceInfoArray video_device_list) { 122 StreamDeviceInfoArray video_device_list) {
98 OnStreamGenerated(msg.routing_id(), request_id, audio_device_list.size(), 123 OnStreamGenerated(msg.routing_id(), request_id, audio_device_list.size(),
99 video_device_list.size()); 124 video_device_list.size());
100 // Notify that the event have occured. 125 // Notify that the event have occured.
101 message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); 126 message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
102 label_ = label; 127 label_ = label;
103 audio_devices_ = audio_device_list; 128 audio_devices_ = audio_device_list;
104 video_devices_ = video_device_list; 129 video_devices_ = video_device_list;
105 } 130 }
106 131
107 void OnStreamGenerationFailed(const IPC::Message& msg, 132 void OnStreamGenerationFailed(const IPC::Message& msg, int request_id) {
108 int request_id) {
109 OnStreamGenerationFailed(msg.routing_id(), request_id); 133 OnStreamGenerationFailed(msg.routing_id(), request_id);
110 message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); 134 message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
111 label_= ""; 135 label_= "";
112 } 136 }
113 137
114 void OnAudioDeviceFailed(const IPC::Message& msg, 138 void OnAudioDeviceFailed(const IPC::Message& msg,
115 std::string label, 139 std::string label,
116 int index) { 140 int index) {
117 OnAudioDeviceFailed(msg.routing_id(), index); 141 OnAudioDeviceFailed(msg.routing_id(), index);
118 audio_devices_.erase(audio_devices_.begin() + index); 142 audio_devices_.erase(audio_devices_.begin() + index);
(...skipping 12 matching lines...) Expand all
131 }; 155 };
132 156
133 class MediaStreamDispatcherHostTest : public testing::Test { 157 class MediaStreamDispatcherHostTest : public testing::Test {
134 public: 158 public:
135 void WaitForResult() { 159 void WaitForResult() {
136 message_loop_->Run(); 160 message_loop_->Run();
137 } 161 }
138 162
139 protected: 163 protected:
140 virtual void SetUp() { 164 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)); 165 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO));
166 // ResourceContext must be created on UI thread.
167 ui_thread_.reset(new BrowserThread(BrowserThread::UI, message_loop_.get()));
168 resource_context_.reset(new content::MockMediaStreamResourceContext());
169
170 // MediaStreamManager must be created and called on IO thread.
144 io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get())); 171 io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get()));
145 172
146 // Make sure the MediaStreamManager exist and use 173 // Create a MediaStreamManager instance and hand over ownership to
147 // fake audio / video devices. 174 // ResourceContext.
148 MediaStreamManager* manager = MediaStreamManager::Get(); 175 MediaStreamManager* media_stream_manager = new MediaStreamManager();
149 manager->UseFakeDevice(); 176 resource_context_->set_media_stream_manager(media_stream_manager);
150 177
151 host_ = 178 // Make sure we use fake devices to avoid long delays.
152 new MockMediaStreamDispatcherHost(message_loop_.get()); 179 resource_context_->media_stream_manager()->UseFakeDevice();
180
181 host_ = new MockMediaStreamDispatcherHost(resource_context_.get(),
182 message_loop_.get());
183 }
184
185 virtual void TearDown() {
186 // Delete the threads in correct order.
187 io_thread_.reset();
188 ui_thread_.reset();
153 } 189 }
154 190
155 scoped_refptr<MockMediaStreamDispatcherHost> host_; 191 scoped_refptr<MockMediaStreamDispatcherHost> host_;
156 scoped_ptr<MessageLoop> message_loop_; 192 scoped_ptr<MessageLoop> message_loop_;
193 scoped_ptr<BrowserThread> ui_thread_;
157 scoped_ptr<BrowserThread> io_thread_; 194 scoped_ptr<BrowserThread> io_thread_;
195 scoped_ptr<content::ResourceContext> resource_context_;
158 }; 196 };
159 197
160 TEST_F(MediaStreamDispatcherHostTest, GenerateStream) { 198 TEST_F(MediaStreamDispatcherHostTest, GenerateStream) {
161 StreamOptions options(false, StreamOptions::kFacingUser); 199 StreamOptions options(false, StreamOptions::kFacingUser);
162 200
163 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId, 0, 1)); 201 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId, 0, 1));
164 host_->OnGenerateStream(kPageRequestId, options); 202 host_->OnGenerateStream(kPageRequestId, options);
165 203
166 WaitForResult(); 204 WaitForResult();
167 205
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 host_->OnGenerateStream(kPageRequestId, options); 254 host_->OnGenerateStream(kPageRequestId, options);
217 WaitForResult(); 255 WaitForResult();
218 std::string label = host_->label_; 256 std::string label = host_->label_;
219 257
220 EXPECT_EQ(host_->audio_devices_.size(), 0u); 258 EXPECT_EQ(host_->audio_devices_.size(), 0u);
221 EXPECT_EQ(host_->video_devices_.size(), 1u); 259 EXPECT_EQ(host_->video_devices_.size(), 1u);
222 EXPECT_EQ(host_->NumberOfStreams(), 1u); 260 EXPECT_EQ(host_->NumberOfStreams(), 1u);
223 261
224 EXPECT_CALL(*host_, OnVideoDeviceFailed(kRenderId, 0)); 262 EXPECT_CALL(*host_, OnVideoDeviceFailed(kRenderId, 0));
225 int session_id = host_->video_devices_[0].session_id; 263 int session_id = host_->video_devices_[0].session_id;
226 MediaStreamManager::Get()->video_capture_manager()->Error(session_id); 264 resource_context_->media_stream_manager()->video_capture_manager()->
265 Error(session_id);
227 WaitForResult(); 266 WaitForResult();
228 EXPECT_EQ(host_->video_devices_.size(), 0u); 267 EXPECT_EQ(host_->video_devices_.size(), 0u);
229 EXPECT_EQ(host_->NumberOfStreams(), 1u); 268 EXPECT_EQ(host_->NumberOfStreams(), 1u);
230 269
231 // TODO(perkj): test audio device failure? 270 // TODO(perkj): test audio device failure?
232 271
233 host_->OnStopGeneratedStream(label); 272 host_->OnStopGeneratedStream(label);
234 EXPECT_EQ(host_->NumberOfStreams(), 0u); 273 EXPECT_EQ(host_->NumberOfStreams(), 0u);
235 } 274 }
236 275
237 }; // namespace media_stream 276 }; // namespace media_stream
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698