1 // Copyright 2013 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.
5 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h"
7 #include "base/message_loop.h"
8 #include "content/browser/renderer_host/render_view_host_delegate.h"
9 #include "content/public/common/renderer_preferences.h"
10 #include "content/public/test/test_browser_thread.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/gfx/rect.h"
16 using testing::SaveArg
;
21 class MockRenderViewHostDelegate
: public RenderViewHostDelegate
{
23 MOCK_METHOD2(RequestMediaAccessPermission
,
24 void(const MediaStreamRequest
& request
,
25 const MediaResponseCallback
& callback
));
27 // Stubs for pure virtual methods we don't care about.
28 virtual gfx::Rect
GetRootWindowResizerRect() const OVERRIDE
{
32 virtual RendererPreferences
GetRendererPrefs(
33 BrowserContext
* browser_context
) const OVERRIDE
{
35 return RendererPreferences();
39 class MockResponseCallback
{
41 MOCK_METHOD1(OnAccessRequestResponse
,
42 void(const MediaStreamDevices
& devices
));
45 class MockMediaStreamUI
: public MediaStreamUI
{
47 MOCK_METHOD1(OnStarted
, void(const base::Closure
& stop
));
50 class MockStopStreamHandler
{
52 MOCK_METHOD0(OnStop
, void());
58 class MediaStreamUIProxyTest
: public testing::Test
{
60 MediaStreamUIProxyTest()
61 : ui_thread_(BrowserThread::UI
, &message_loop_
),
62 io_thread_(BrowserThread::IO
, &message_loop_
) {
63 proxy_
= MediaStreamUIProxy::CreateForTests(&delegate_
);
66 virtual ~MediaStreamUIProxyTest() {
68 message_loop_
.RunUntilIdle();
72 base::MessageLoop message_loop_
;
73 TestBrowserThread ui_thread_
;
74 TestBrowserThread io_thread_
;
76 MockRenderViewHostDelegate delegate_
;
77 MockResponseCallback response_callback_
;
78 scoped_ptr
<MediaStreamUIProxy
> proxy_
;
81 MATCHER_P(SameRequest
, expected
, "") {
83 expected
.render_process_id
== arg
.render_process_id
&&
84 expected
.render_view_id
== arg
.render_view_id
&&
85 expected
.security_origin
== arg
.security_origin
&&
86 expected
.request_type
== arg
.request_type
&&
87 expected
.requested_device_id
== arg
.requested_device_id
&&
88 expected
.audio_type
== arg
.audio_type
&&
89 expected
.video_type
== arg
.video_type
;
92 TEST_F(MediaStreamUIProxyTest
, Deny
) {
93 MediaStreamRequest
request(0, 0, GURL("http://origin/"),
94 MEDIA_GENERATE_STREAM
, std::string(),
95 MEDIA_DEVICE_AUDIO_CAPTURE
,
96 MEDIA_DEVICE_VIDEO_CAPTURE
);
97 proxy_
->RequestAccess(
98 request
, base::Bind(&MockResponseCallback::OnAccessRequestResponse
,
99 base::Unretained(&response_callback_
)));
100 MediaResponseCallback callback
;
101 EXPECT_CALL(delegate_
, RequestMediaAccessPermission(SameRequest(request
), _
))
102 .WillOnce(SaveArg
<1>(&callback
));
103 message_loop_
.RunUntilIdle();
104 ASSERT_FALSE(callback
.is_null());
106 MediaStreamDevices devices
;
107 callback
.Run(devices
, scoped_ptr
<MediaStreamUI
>());
109 MediaStreamDevices response
;
110 EXPECT_CALL(response_callback_
, OnAccessRequestResponse(_
))
111 .WillOnce(SaveArg
<0>(&response
));
112 message_loop_
.RunUntilIdle();
114 EXPECT_TRUE(response
.empty());
117 TEST_F(MediaStreamUIProxyTest
, AcceptAndStart
) {
118 MediaStreamRequest
request(0, 0, GURL("http://origin/"),
119 MEDIA_GENERATE_STREAM
, std::string(),
120 MEDIA_DEVICE_AUDIO_CAPTURE
,
121 MEDIA_DEVICE_VIDEO_CAPTURE
);
122 proxy_
->RequestAccess(
123 request
, base::Bind(&MockResponseCallback::OnAccessRequestResponse
,
124 base::Unretained(&response_callback_
)));
125 MediaResponseCallback callback
;
126 EXPECT_CALL(delegate_
, RequestMediaAccessPermission(SameRequest(request
), _
))
127 .WillOnce(SaveArg
<1>(&callback
));
128 message_loop_
.RunUntilIdle();
129 ASSERT_FALSE(callback
.is_null());
131 MediaStreamDevices devices
;
133 MediaStreamDevice(MEDIA_DEVICE_AUDIO_CAPTURE
, "Mic", "Mic"));
134 scoped_ptr
<MockMediaStreamUI
> ui(new MockMediaStreamUI());
135 EXPECT_CALL(*ui
, OnStarted(_
));
136 callback
.Run(devices
, ui
.PassAs
<MediaStreamUI
>());
138 MediaStreamDevices response
;
139 EXPECT_CALL(response_callback_
, OnAccessRequestResponse(_
))
140 .WillOnce(SaveArg
<0>(&response
));
141 message_loop_
.RunUntilIdle();
143 EXPECT_FALSE(response
.empty());
145 proxy_
->OnStarted(base::Closure());
146 message_loop_
.RunUntilIdle();
149 // Verify that the proxy can be deleted before the request is processed.
150 TEST_F(MediaStreamUIProxyTest
, DeleteBeforeAccepted
) {
151 MediaStreamRequest
request(0, 0, GURL("http://origin/"),
152 MEDIA_GENERATE_STREAM
, std::string(),
153 MEDIA_DEVICE_AUDIO_CAPTURE
,
154 MEDIA_DEVICE_VIDEO_CAPTURE
);
155 proxy_
->RequestAccess(
156 request
, base::Bind(&MockResponseCallback::OnAccessRequestResponse
,
157 base::Unretained(&response_callback_
)));
158 MediaResponseCallback callback
;
159 EXPECT_CALL(delegate_
, RequestMediaAccessPermission(SameRequest(request
), _
))
160 .WillOnce(SaveArg
<1>(&callback
));
161 message_loop_
.RunUntilIdle();
162 ASSERT_FALSE(callback
.is_null());
166 MediaStreamDevices devices
;
167 scoped_ptr
<MediaStreamUI
> ui
;
168 callback
.Run(devices
, ui
.Pass());
171 TEST_F(MediaStreamUIProxyTest
, StopFromUI
) {
172 MediaStreamRequest
request(0, 0, GURL("http://origin/"),
173 MEDIA_GENERATE_STREAM
, std::string(),
174 MEDIA_DEVICE_AUDIO_CAPTURE
,
175 MEDIA_DEVICE_VIDEO_CAPTURE
);
176 proxy_
->RequestAccess(
177 request
, base::Bind(&MockResponseCallback::OnAccessRequestResponse
,
178 base::Unretained(&response_callback_
)));
179 MediaResponseCallback callback
;
180 EXPECT_CALL(delegate_
, RequestMediaAccessPermission(SameRequest(request
), _
))
181 .WillOnce(SaveArg
<1>(&callback
));
182 message_loop_
.RunUntilIdle();
183 ASSERT_FALSE(callback
.is_null());
185 base::Closure stop_callback
;
187 MediaStreamDevices devices
;
189 MediaStreamDevice(MEDIA_DEVICE_AUDIO_CAPTURE
, "Mic", "Mic"));
190 scoped_ptr
<MockMediaStreamUI
> ui(new MockMediaStreamUI());
191 EXPECT_CALL(*ui
, OnStarted(_
))
192 .WillOnce(SaveArg
<0>(&stop_callback
));
193 callback
.Run(devices
, ui
.PassAs
<MediaStreamUI
>());
195 MediaStreamDevices response
;
196 EXPECT_CALL(response_callback_
, OnAccessRequestResponse(_
))
197 .WillOnce(SaveArg
<0>(&response
));
198 message_loop_
.RunUntilIdle();
200 EXPECT_FALSE(response
.empty());
202 MockStopStreamHandler stop_handler
;
203 proxy_
->OnStarted(base::Bind(&MockStopStreamHandler::OnStop
,
204 base::Unretained(&stop_handler
)));
205 message_loop_
.RunUntilIdle();
207 ASSERT_FALSE(stop_callback
.is_null());
208 EXPECT_CALL(stop_handler
, OnStop());
210 message_loop_
.RunUntilIdle();