Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / media / media_stream_ui_proxy_unittest.cc
blob4529b4999d9262f601a4d612e414d6c47f094ff9
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/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"
15 using testing::_;
16 using testing::Return;
17 using testing::SaveArg;
19 namespace content {
20 namespace {
22 class MockRenderViewHostDelegate : public RenderViewHostDelegate {
23 public:
24 MOCK_METHOD2(RequestMediaAccessPermission,
25 void(const MediaStreamRequest& request,
26 const MediaResponseCallback& callback));
28 // Stubs for pure virtual methods we don't care about.
29 virtual gfx::Rect GetRootWindowResizerRect() const OVERRIDE {
30 NOTREACHED();
31 return gfx::Rect();
33 virtual RendererPreferences GetRendererPrefs(
34 BrowserContext* browser_context) const OVERRIDE {
35 NOTREACHED();
36 return RendererPreferences();
40 class MockResponseCallback {
41 public:
42 MOCK_METHOD2(OnAccessRequestResponse,
43 void(const MediaStreamDevices& devices,
44 content::MediaStreamRequestResult result));
47 class MockMediaStreamUI : public MediaStreamUI {
48 public:
49 MOCK_METHOD1(OnStarted, gfx::NativeViewId(const base::Closure& stop));
52 class MockStopStreamHandler {
53 public:
54 MOCK_METHOD0(OnStop, void());
55 MOCK_METHOD1(OnWindowId, void(gfx::NativeViewId window_id));
59 } // namespace
61 class MediaStreamUIProxyTest : public testing::Test {
62 public:
63 MediaStreamUIProxyTest()
64 : ui_thread_(BrowserThread::UI, &message_loop_),
65 io_thread_(BrowserThread::IO, &message_loop_) {
66 proxy_ = MediaStreamUIProxy::CreateForTests(&delegate_);
69 virtual ~MediaStreamUIProxyTest() {
70 proxy_.reset();
71 message_loop_.RunUntilIdle();
74 protected:
75 base::MessageLoop message_loop_;
76 TestBrowserThread ui_thread_;
77 TestBrowserThread io_thread_;
79 MockRenderViewHostDelegate delegate_;
80 MockResponseCallback response_callback_;
81 scoped_ptr<MediaStreamUIProxy> proxy_;
84 MATCHER_P(SameRequest, expected, "") {
85 return
86 expected.render_process_id == arg.render_process_id &&
87 expected.render_view_id == arg.render_view_id &&
88 expected.tab_capture_device_id == arg.tab_capture_device_id &&
89 expected.security_origin == arg.security_origin &&
90 expected.request_type == arg.request_type &&
91 expected.requested_audio_device_id == arg.requested_audio_device_id &&
92 expected.requested_video_device_id == arg.requested_video_device_id &&
93 expected.audio_type == arg.audio_type &&
94 expected.video_type == arg.video_type;
97 TEST_F(MediaStreamUIProxyTest, Deny) {
98 MediaStreamRequest request(0, 0, 0, GURL("http://origin/"), false,
99 MEDIA_GENERATE_STREAM, std::string(),
100 std::string(),
101 MEDIA_DEVICE_AUDIO_CAPTURE,
102 MEDIA_DEVICE_VIDEO_CAPTURE);
103 proxy_->RequestAccess(
104 request, base::Bind(&MockResponseCallback::OnAccessRequestResponse,
105 base::Unretained(&response_callback_)));
106 MediaResponseCallback callback;
107 EXPECT_CALL(delegate_, RequestMediaAccessPermission(SameRequest(request), _))
108 .WillOnce(SaveArg<1>(&callback));
109 message_loop_.RunUntilIdle();
110 ASSERT_FALSE(callback.is_null());
112 MediaStreamDevices devices;
113 callback.Run(devices, MEDIA_DEVICE_OK, scoped_ptr<MediaStreamUI>());
115 MediaStreamDevices response;
116 EXPECT_CALL(response_callback_, OnAccessRequestResponse(_, _))
117 .WillOnce(SaveArg<0>(&response));
118 message_loop_.RunUntilIdle();
120 EXPECT_TRUE(response.empty());
123 TEST_F(MediaStreamUIProxyTest, AcceptAndStart) {
124 MediaStreamRequest request(0, 0, 0, GURL("http://origin/"), false,
125 MEDIA_GENERATE_STREAM, std::string(),
126 std::string(),
127 MEDIA_DEVICE_AUDIO_CAPTURE,
128 MEDIA_DEVICE_VIDEO_CAPTURE);
129 proxy_->RequestAccess(
130 request, base::Bind(&MockResponseCallback::OnAccessRequestResponse,
131 base::Unretained(&response_callback_)));
132 MediaResponseCallback callback;
133 EXPECT_CALL(delegate_, RequestMediaAccessPermission(SameRequest(request), _))
134 .WillOnce(SaveArg<1>(&callback));
135 message_loop_.RunUntilIdle();
136 ASSERT_FALSE(callback.is_null());
138 MediaStreamDevices devices;
139 devices.push_back(
140 MediaStreamDevice(MEDIA_DEVICE_AUDIO_CAPTURE, "Mic", "Mic"));
141 scoped_ptr<MockMediaStreamUI> ui(new MockMediaStreamUI());
142 EXPECT_CALL(*ui, OnStarted(_)).WillOnce(Return(0));
143 callback.Run(devices, MEDIA_DEVICE_OK, ui.PassAs<MediaStreamUI>());
145 MediaStreamDevices response;
146 EXPECT_CALL(response_callback_, OnAccessRequestResponse(_, _))
147 .WillOnce(SaveArg<0>(&response));
148 message_loop_.RunUntilIdle();
150 EXPECT_FALSE(response.empty());
152 proxy_->OnStarted(base::Closure(), MediaStreamUIProxy::WindowIdCallback());
153 message_loop_.RunUntilIdle();
156 // Verify that the proxy can be deleted before the request is processed.
157 TEST_F(MediaStreamUIProxyTest, DeleteBeforeAccepted) {
158 MediaStreamRequest request(0, 0, 0, GURL("http://origin/"), false,
159 MEDIA_GENERATE_STREAM, std::string(),
160 std::string(),
161 MEDIA_DEVICE_AUDIO_CAPTURE,
162 MEDIA_DEVICE_VIDEO_CAPTURE);
163 proxy_->RequestAccess(
164 request, base::Bind(&MockResponseCallback::OnAccessRequestResponse,
165 base::Unretained(&response_callback_)));
166 MediaResponseCallback callback;
167 EXPECT_CALL(delegate_, RequestMediaAccessPermission(SameRequest(request), _))
168 .WillOnce(SaveArg<1>(&callback));
169 message_loop_.RunUntilIdle();
170 ASSERT_FALSE(callback.is_null());
172 proxy_.reset();
174 MediaStreamDevices devices;
175 scoped_ptr<MediaStreamUI> ui;
176 callback.Run(devices, MEDIA_DEVICE_OK, ui.Pass());
179 TEST_F(MediaStreamUIProxyTest, StopFromUI) {
180 MediaStreamRequest request(0, 0, 0, GURL("http://origin/"), false,
181 MEDIA_GENERATE_STREAM, std::string(),
182 std::string(),
183 MEDIA_DEVICE_AUDIO_CAPTURE,
184 MEDIA_DEVICE_VIDEO_CAPTURE);
185 proxy_->RequestAccess(
186 request, base::Bind(&MockResponseCallback::OnAccessRequestResponse,
187 base::Unretained(&response_callback_)));
188 MediaResponseCallback callback;
189 EXPECT_CALL(delegate_, RequestMediaAccessPermission(SameRequest(request), _))
190 .WillOnce(SaveArg<1>(&callback));
191 message_loop_.RunUntilIdle();
192 ASSERT_FALSE(callback.is_null());
194 base::Closure stop_callback;
196 MediaStreamDevices devices;
197 devices.push_back(
198 MediaStreamDevice(MEDIA_DEVICE_AUDIO_CAPTURE, "Mic", "Mic"));
199 scoped_ptr<MockMediaStreamUI> ui(new MockMediaStreamUI());
200 EXPECT_CALL(*ui, OnStarted(_))
201 .WillOnce(testing::DoAll(SaveArg<0>(&stop_callback), Return(0)));
202 callback.Run(devices, MEDIA_DEVICE_OK, ui.PassAs<MediaStreamUI>());
204 MediaStreamDevices response;
205 EXPECT_CALL(response_callback_, OnAccessRequestResponse(_, _))
206 .WillOnce(SaveArg<0>(&response));
207 message_loop_.RunUntilIdle();
209 EXPECT_FALSE(response.empty());
211 MockStopStreamHandler stop_handler;
212 proxy_->OnStarted(base::Bind(&MockStopStreamHandler::OnStop,
213 base::Unretained(&stop_handler)),
214 MediaStreamUIProxy::WindowIdCallback());
215 message_loop_.RunUntilIdle();
217 ASSERT_FALSE(stop_callback.is_null());
218 EXPECT_CALL(stop_handler, OnStop());
219 stop_callback.Run();
220 message_loop_.RunUntilIdle();
223 TEST_F(MediaStreamUIProxyTest, WindowIdCallbackCalled) {
224 MediaStreamRequest request(0,
227 GURL("http://origin/"),
228 false,
229 MEDIA_GENERATE_STREAM,
230 std::string(),
231 std::string(),
232 MEDIA_NO_SERVICE,
233 MEDIA_DESKTOP_VIDEO_CAPTURE);
234 proxy_->RequestAccess(
235 request,
236 base::Bind(&MockResponseCallback::OnAccessRequestResponse,
237 base::Unretained(&response_callback_)));
238 MediaResponseCallback callback;
239 EXPECT_CALL(delegate_, RequestMediaAccessPermission(SameRequest(request), _))
240 .WillOnce(SaveArg<1>(&callback));
241 message_loop_.RunUntilIdle();
243 const int kWindowId = 1;
244 scoped_ptr<MockMediaStreamUI> ui(new MockMediaStreamUI());
245 EXPECT_CALL(*ui, OnStarted(_)).WillOnce(Return(kWindowId));
247 callback.Run(
248 MediaStreamDevices(), MEDIA_DEVICE_OK, ui.PassAs<MediaStreamUI>());
249 EXPECT_CALL(response_callback_, OnAccessRequestResponse(_, _));
251 MockStopStreamHandler handler;
252 EXPECT_CALL(handler, OnWindowId(kWindowId));
254 proxy_->OnStarted(
255 base::Bind(&MockStopStreamHandler::OnStop, base::Unretained(&handler)),
256 base::Bind(&MockStopStreamHandler::OnWindowId,
257 base::Unretained(&handler)));
258 message_loop_.RunUntilIdle();
261 } // content