Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / renderer_host / media / media_stream_ui_proxy_unittest.cc
blob3bc1b06302680309bd2c611330d593cb9a52bbf3
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/frame_host/render_frame_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 MockRenderFrameHostDelegate : public RenderFrameHostDelegate {
23 public:
24 MOCK_METHOD2(RequestMediaAccessPermission,
25 void(const MediaStreamRequest& request,
26 const MediaResponseCallback& callback));
29 class MockResponseCallback {
30 public:
31 MOCK_METHOD2(OnAccessRequestResponse,
32 void(const MediaStreamDevices& devices,
33 content::MediaStreamRequestResult result));
36 class MockMediaStreamUI : public MediaStreamUI {
37 public:
38 MOCK_METHOD1(OnStarted, gfx::NativeViewId(const base::Closure& stop));
41 class MockStopStreamHandler {
42 public:
43 MOCK_METHOD0(OnStop, void());
44 MOCK_METHOD1(OnWindowId, void(gfx::NativeViewId window_id));
48 } // namespace
50 class MediaStreamUIProxyTest : public testing::Test {
51 public:
52 MediaStreamUIProxyTest()
53 : ui_thread_(BrowserThread::UI, &message_loop_),
54 io_thread_(BrowserThread::IO, &message_loop_) {
55 proxy_ = MediaStreamUIProxy::CreateForTests(&delegate_);
58 virtual ~MediaStreamUIProxyTest() {
59 proxy_.reset();
60 message_loop_.RunUntilIdle();
63 protected:
64 base::MessageLoop message_loop_;
65 TestBrowserThread ui_thread_;
66 TestBrowserThread io_thread_;
68 MockRenderFrameHostDelegate delegate_;
69 MockResponseCallback response_callback_;
70 scoped_ptr<MediaStreamUIProxy> proxy_;
73 MATCHER_P(SameRequest, expected, "") {
74 return
75 expected.render_process_id == arg.render_process_id &&
76 expected.render_frame_id == arg.render_frame_id &&
77 expected.tab_capture_device_id == arg.tab_capture_device_id &&
78 expected.security_origin == arg.security_origin &&
79 expected.request_type == arg.request_type &&
80 expected.requested_audio_device_id == arg.requested_audio_device_id &&
81 expected.requested_video_device_id == arg.requested_video_device_id &&
82 expected.audio_type == arg.audio_type &&
83 expected.video_type == arg.video_type;
86 TEST_F(MediaStreamUIProxyTest, Deny) {
87 MediaStreamRequest request(0, 0, 0, GURL("http://origin/"), false,
88 MEDIA_GENERATE_STREAM, std::string(),
89 std::string(),
90 MEDIA_DEVICE_AUDIO_CAPTURE,
91 MEDIA_DEVICE_VIDEO_CAPTURE);
92 proxy_->RequestAccess(
93 request, base::Bind(&MockResponseCallback::OnAccessRequestResponse,
94 base::Unretained(&response_callback_)));
95 MediaResponseCallback callback;
96 EXPECT_CALL(delegate_, RequestMediaAccessPermission(SameRequest(request), _))
97 .WillOnce(SaveArg<1>(&callback));
98 message_loop_.RunUntilIdle();
99 ASSERT_FALSE(callback.is_null());
101 MediaStreamDevices devices;
102 callback.Run(devices, MEDIA_DEVICE_OK, scoped_ptr<MediaStreamUI>());
104 MediaStreamDevices response;
105 EXPECT_CALL(response_callback_, OnAccessRequestResponse(_, _))
106 .WillOnce(SaveArg<0>(&response));
107 message_loop_.RunUntilIdle();
109 EXPECT_TRUE(response.empty());
112 TEST_F(MediaStreamUIProxyTest, AcceptAndStart) {
113 MediaStreamRequest request(0, 0, 0, GURL("http://origin/"), false,
114 MEDIA_GENERATE_STREAM, std::string(),
115 std::string(),
116 MEDIA_DEVICE_AUDIO_CAPTURE,
117 MEDIA_DEVICE_VIDEO_CAPTURE);
118 proxy_->RequestAccess(
119 request, base::Bind(&MockResponseCallback::OnAccessRequestResponse,
120 base::Unretained(&response_callback_)));
121 MediaResponseCallback callback;
122 EXPECT_CALL(delegate_, RequestMediaAccessPermission(SameRequest(request), _))
123 .WillOnce(SaveArg<1>(&callback));
124 message_loop_.RunUntilIdle();
125 ASSERT_FALSE(callback.is_null());
127 MediaStreamDevices devices;
128 devices.push_back(
129 MediaStreamDevice(MEDIA_DEVICE_AUDIO_CAPTURE, "Mic", "Mic"));
130 scoped_ptr<MockMediaStreamUI> ui(new MockMediaStreamUI());
131 EXPECT_CALL(*ui, OnStarted(_)).WillOnce(Return(0));
132 callback.Run(devices, MEDIA_DEVICE_OK, ui.PassAs<MediaStreamUI>());
134 MediaStreamDevices response;
135 EXPECT_CALL(response_callback_, OnAccessRequestResponse(_, _))
136 .WillOnce(SaveArg<0>(&response));
137 message_loop_.RunUntilIdle();
139 EXPECT_FALSE(response.empty());
141 proxy_->OnStarted(base::Closure(), MediaStreamUIProxy::WindowIdCallback());
142 message_loop_.RunUntilIdle();
145 // Verify that the proxy can be deleted before the request is processed.
146 TEST_F(MediaStreamUIProxyTest, DeleteBeforeAccepted) {
147 MediaStreamRequest request(0, 0, 0, GURL("http://origin/"), false,
148 MEDIA_GENERATE_STREAM, std::string(),
149 std::string(),
150 MEDIA_DEVICE_AUDIO_CAPTURE,
151 MEDIA_DEVICE_VIDEO_CAPTURE);
152 proxy_->RequestAccess(
153 request, base::Bind(&MockResponseCallback::OnAccessRequestResponse,
154 base::Unretained(&response_callback_)));
155 MediaResponseCallback callback;
156 EXPECT_CALL(delegate_, RequestMediaAccessPermission(SameRequest(request), _))
157 .WillOnce(SaveArg<1>(&callback));
158 message_loop_.RunUntilIdle();
159 ASSERT_FALSE(callback.is_null());
161 proxy_.reset();
163 MediaStreamDevices devices;
164 scoped_ptr<MediaStreamUI> ui;
165 callback.Run(devices, MEDIA_DEVICE_OK, ui.Pass());
168 TEST_F(MediaStreamUIProxyTest, StopFromUI) {
169 MediaStreamRequest request(0, 0, 0, GURL("http://origin/"), false,
170 MEDIA_GENERATE_STREAM, std::string(),
171 std::string(),
172 MEDIA_DEVICE_AUDIO_CAPTURE,
173 MEDIA_DEVICE_VIDEO_CAPTURE);
174 proxy_->RequestAccess(
175 request, base::Bind(&MockResponseCallback::OnAccessRequestResponse,
176 base::Unretained(&response_callback_)));
177 MediaResponseCallback callback;
178 EXPECT_CALL(delegate_, RequestMediaAccessPermission(SameRequest(request), _))
179 .WillOnce(SaveArg<1>(&callback));
180 message_loop_.RunUntilIdle();
181 ASSERT_FALSE(callback.is_null());
183 base::Closure stop_callback;
185 MediaStreamDevices devices;
186 devices.push_back(
187 MediaStreamDevice(MEDIA_DEVICE_AUDIO_CAPTURE, "Mic", "Mic"));
188 scoped_ptr<MockMediaStreamUI> ui(new MockMediaStreamUI());
189 EXPECT_CALL(*ui, OnStarted(_))
190 .WillOnce(testing::DoAll(SaveArg<0>(&stop_callback), Return(0)));
191 callback.Run(devices, MEDIA_DEVICE_OK, ui.PassAs<MediaStreamUI>());
193 MediaStreamDevices response;
194 EXPECT_CALL(response_callback_, OnAccessRequestResponse(_, _))
195 .WillOnce(SaveArg<0>(&response));
196 message_loop_.RunUntilIdle();
198 EXPECT_FALSE(response.empty());
200 MockStopStreamHandler stop_handler;
201 proxy_->OnStarted(base::Bind(&MockStopStreamHandler::OnStop,
202 base::Unretained(&stop_handler)),
203 MediaStreamUIProxy::WindowIdCallback());
204 message_loop_.RunUntilIdle();
206 ASSERT_FALSE(stop_callback.is_null());
207 EXPECT_CALL(stop_handler, OnStop());
208 stop_callback.Run();
209 message_loop_.RunUntilIdle();
212 TEST_F(MediaStreamUIProxyTest, WindowIdCallbackCalled) {
213 MediaStreamRequest request(0,
216 GURL("http://origin/"),
217 false,
218 MEDIA_GENERATE_STREAM,
219 std::string(),
220 std::string(),
221 MEDIA_NO_SERVICE,
222 MEDIA_DESKTOP_VIDEO_CAPTURE);
223 proxy_->RequestAccess(
224 request,
225 base::Bind(&MockResponseCallback::OnAccessRequestResponse,
226 base::Unretained(&response_callback_)));
227 MediaResponseCallback callback;
228 EXPECT_CALL(delegate_, RequestMediaAccessPermission(SameRequest(request), _))
229 .WillOnce(SaveArg<1>(&callback));
230 message_loop_.RunUntilIdle();
232 const int kWindowId = 1;
233 scoped_ptr<MockMediaStreamUI> ui(new MockMediaStreamUI());
234 EXPECT_CALL(*ui, OnStarted(_)).WillOnce(Return(kWindowId));
236 callback.Run(
237 MediaStreamDevices(), MEDIA_DEVICE_OK, ui.PassAs<MediaStreamUI>());
238 EXPECT_CALL(response_callback_, OnAccessRequestResponse(_, _));
240 MockStopStreamHandler handler;
241 EXPECT_CALL(handler, OnWindowId(kWindowId));
243 proxy_->OnStarted(
244 base::Bind(&MockStopStreamHandler::OnStop, base::Unretained(&handler)),
245 base::Bind(&MockStopStreamHandler::OnWindowId,
246 base::Unretained(&handler)));
247 message_loop_.RunUntilIdle();
250 } // content