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
3 // found in the LICENSE file.
8 #include "base/message_loop/message_loop.h"
9 #include "content/browser/browser_thread_impl.h"
10 #include "content/browser/renderer_host/media/media_stream_settings_requester.h"
11 #include "content/browser/renderer_host/media/media_stream_ui_controller.h"
12 #include "content/common/media/media_stream_options.h"
13 #include "content/public/common/media_stream_request.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
21 class MediaStreamDeviceUIControllerTest
22 : public ::testing::Test
,
23 public SettingsRequester
{
25 MediaStreamDeviceUIControllerTest() {}
27 // Mock implementation of SettingsRequester.
28 // TODO(sergeyu): Move mock SettingsRequester to a separate class.
29 MOCK_METHOD2(DevicesAccepted
, void(
30 const std::string
&, const StreamDeviceInfoArray
&));
31 MOCK_METHOD1(SettingsError
, void(const std::string
&));
32 MOCK_METHOD1(StopStreamFromUI
, void(const std::string
&));
33 void GetAvailableDevices(MediaStreamDevices
* devices
) OVERRIDE
{
34 devices
->push_back(MediaStreamDevice(MEDIA_DEVICE_AUDIO_CAPTURE
,
39 devices
->push_back(MediaStreamDevice(MEDIA_DEVICE_VIDEO_CAPTURE
,
45 virtual void SetUp() {
46 message_loop_
.reset(new base::MessageLoop(base::MessageLoop::TYPE_IO
));
47 ui_thread_
.reset(new BrowserThreadImpl(BrowserThread::UI
,
48 message_loop_
.get()));
49 io_thread_
.reset(new BrowserThreadImpl(BrowserThread::IO
,
50 message_loop_
.get()));
51 ui_controller_
.reset(new MediaStreamUIController(this));
54 virtual void TearDown() {
55 message_loop_
->RunUntilIdle();
58 void CreateDummyRequest(const std::string
& label
, bool audio
, bool video
) {
59 int dummy_render_process_id
= 1;
60 int dummy_render_view_id
= 1;
61 StreamOptions
components(
62 audio
? MEDIA_DEVICE_AUDIO_CAPTURE
: MEDIA_NO_SERVICE
,
63 video
? MEDIA_DEVICE_VIDEO_CAPTURE
: MEDIA_NO_SERVICE
);
65 ui_controller_
->MakeUIRequest(label
,
66 dummy_render_process_id
,
70 MEDIA_GENERATE_STREAM
,
74 scoped_ptr
<base::MessageLoop
> message_loop_
;
75 scoped_ptr
<BrowserThreadImpl
> ui_thread_
;
76 scoped_ptr
<BrowserThreadImpl
> io_thread_
;
77 scoped_ptr
<MediaStreamUIController
> ui_controller_
;
80 DISALLOW_COPY_AND_ASSIGN(MediaStreamDeviceUIControllerTest
);
83 TEST_F(MediaStreamDeviceUIControllerTest
, GenerateRequest
) {
84 const std::string label
= "dummy_label";
85 CreateDummyRequest(label
, true, false);
87 // Expecting an error callback triggered by the non-existing
88 // RenderViewHostImpl.
89 EXPECT_CALL(*this, SettingsError(label
));
92 TEST_F(MediaStreamDeviceUIControllerTest
, GenerateAndRemoveRequest
) {
93 const std::string label
= "label";
94 CreateDummyRequest(label
, true, false);
96 // Remove the current request, it should not crash.
97 ui_controller_
->CancelUIRequest(label
);
100 TEST_F(MediaStreamDeviceUIControllerTest
, HandleRequestUsingFakeUI
) {
101 ui_controller_
->UseFakeUI(scoped_ptr
<MediaStreamUI
>());
103 const std::string label
= "label";
104 CreateDummyRequest(label
, true, true);
106 // Remove the current request, it should not crash.
107 EXPECT_CALL(*this, DevicesAccepted(label
, _
));
109 message_loop_
->RunUntilIdle();
111 ui_controller_
->NotifyUIIndicatorDevicesClosed(label
);
114 TEST_F(MediaStreamDeviceUIControllerTest
, CreateRequestsAndCancelTheFirst
) {
115 ui_controller_
->UseFakeUI(scoped_ptr
<MediaStreamUI
>());
117 // Create the first audio request.
118 const std::string label_1
= "label_1";
119 CreateDummyRequest(label_1
, true, false);
121 // Create the second video request.
122 const std::string label_2
= "label_2";
123 CreateDummyRequest(label_2
, false, true);
125 // Create the third audio and video request.
126 const std::string label_3
= "label_3";
127 CreateDummyRequest(label_3
, true, true);
129 // Remove the first request which has been brought to the UI.
130 ui_controller_
->CancelUIRequest(label_1
);
132 // We should get callbacks from the rest of the requests.
133 EXPECT_CALL(*this, DevicesAccepted(label_2
, _
));
134 EXPECT_CALL(*this, DevicesAccepted(label_3
, _
));
136 message_loop_
->RunUntilIdle();
138 ui_controller_
->NotifyUIIndicatorDevicesClosed(label_2
);
139 ui_controller_
->NotifyUIIndicatorDevicesClosed(label_3
);
142 TEST_F(MediaStreamDeviceUIControllerTest
, CreateRequestsAndCancelTheLast
) {
143 ui_controller_
->UseFakeUI(scoped_ptr
<MediaStreamUI
>());
145 // Create the first audio request.
146 const std::string label_1
= "label_1";
147 CreateDummyRequest(label_1
, true, false);
149 // Create the second video request.
150 const std::string label_2
= "label_2";
151 CreateDummyRequest(label_2
, false, true);
153 // Create the third audio and video request.
154 const std::string label_3
= "label_3";
155 CreateDummyRequest(label_3
, true, true);
157 // Remove the last request which is pending in the queue.
158 ui_controller_
->CancelUIRequest(label_3
);
160 // We should get callbacks from the rest of the requests.
161 EXPECT_CALL(*this, DevicesAccepted(label_1
, _
));
162 EXPECT_CALL(*this, DevicesAccepted(label_2
, _
));
164 message_loop_
->RunUntilIdle();
166 ui_controller_
->NotifyUIIndicatorDevicesClosed(label_1
);
167 ui_controller_
->NotifyUIIndicatorDevicesClosed(label_2
);
170 } // namespace content