1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/media/webrtc/media_stream_capture_indicator.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/test/bind.h"
#include "chrome/browser/media/webrtc/desktop_capture_devices_util.h"
#include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h"
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h"
namespace {
class LenientMockObserver : public MediaStreamCaptureIndicator::Observer {
public:
LenientMockObserver() = default;
LenientMockObserver(const LenientMockObserver&) = delete;
LenientMockObserver& operator=(const LenientMockObserver&) = delete;
~LenientMockObserver() override = default;
// Helper functions used to set the expectations of the mock methods. This
// allows passing function pointers to
// MediaStreamCaptureIndicatorTest::TestObserverMethod.
void SetOnIsCapturingVideoChangedExpectation(content::WebContents* contents,
bool is_capturing_video) {
EXPECT_CALL(*this, OnIsCapturingVideoChanged(contents, is_capturing_video));
}
void SetOnIsCapturingAudioChangedExpectation(content::WebContents* contents,
bool is_capturing_audio) {
EXPECT_CALL(*this, OnIsCapturingAudioChanged(contents, is_capturing_audio));
}
void SetOnIsBeingMirroredChangedExpectation(content::WebContents* contents,
bool is_being_mirrored) {
EXPECT_CALL(*this, OnIsBeingMirroredChanged(contents, is_being_mirrored));
}
void SetOnIsCapturingWindowChangedExpectation(content::WebContents* contents,
size_t times) {
EXPECT_CALL(*this, OnIsCapturingWindowChanged(contents, testing::_))
.Times(times);
}
void SetOnIsCapturingTabChangedExpectation(content::WebContents* contents,
bool is_capturing_tab) {
EXPECT_CALL(*this, OnIsCapturingTabChanged(contents, is_capturing_tab));
}
void SetOnIsCapturingWindowChangedExpectation(content::WebContents* contents,
bool is_capturing_window) {
EXPECT_CALL(*this,
OnIsCapturingWindowChanged(contents, is_capturing_window));
}
void SetOnIsCapturingDisplayChangedExpectation(content::WebContents* contents,
size_t times) {
EXPECT_CALL(*this, OnIsCapturingDisplayChanged(contents, testing::_))
.Times(times);
}
void SetOnIsCapturingDisplayChangedExpectation(content::WebContents* contents,
bool is_capturing_display) {
EXPECT_CALL(*this,
OnIsCapturingDisplayChanged(contents, is_capturing_display));
}
private:
MOCK_METHOD2(OnIsCapturingVideoChanged,
void(content::WebContents* contents, bool is_capturing_video));
MOCK_METHOD2(OnIsCapturingAudioChanged,
void(content::WebContents* contents, bool is_capturing_audio));
MOCK_METHOD2(OnIsBeingMirroredChanged,
void(content::WebContents* contents, bool is_being_mirrored));
MOCK_METHOD2(OnIsCapturingTabChanged,
void(content::WebContents* contents, bool is_capturing_tab));
MOCK_METHOD2(OnIsCapturingWindowChanged,
void(content::WebContents* contents, bool is_capturing_window));
MOCK_METHOD2(OnIsCapturingDisplayChanged,
void(content::WebContents* contents, bool is_capturing_display));
};
using MockObserver = testing::StrictMock<LenientMockObserver>;
typedef void (MockObserver::*MockObserverSetExpectationsMethod)(
content::WebContents* web_contents,
bool value);
typedef void (MockObserver::*MockObserverStreamTypeSetExpectationsMethod)(
content::WebContents* web_contents,
size_t value);
typedef bool (MediaStreamCaptureIndicator::*AccessorMethod)(
content::WebContents* web_contents) const;
class MediaStreamCaptureIndicatorTest : public ChromeRenderViewHostTestHarness {
public:
MediaStreamCaptureIndicatorTest() = default;
~MediaStreamCaptureIndicatorTest() override = default;
MediaStreamCaptureIndicatorTest(const MediaStreamCaptureIndicatorTest&) =
delete;
MediaStreamCaptureIndicatorTest& operator=(
const MediaStreamCaptureIndicatorTest&) = delete;
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
content::WebContentsTester::For(web_contents())
->NavigateAndCommit(GURL("https://www.example.com/"));
indicator_ = MediaCaptureDevicesDispatcher::GetInstance()
->GetMediaStreamCaptureIndicator();
observer_ = std::make_unique<MockObserver>();
indicator_->AddObserver(observer());
}
void TearDown() override {
indicator_->RemoveObserver(observer());
observer_.reset();
indicator_.reset();
ChromeRenderViewHostTestHarness::TearDown();
}
MediaStreamCaptureIndicator* indicator() { return indicator_.get(); }
MockObserver* observer() { return observer_.get(); }
private:
std::unique_ptr<MockObserver> observer_;
scoped_refptr<MediaStreamCaptureIndicator> indicator_;
};
struct ObserverMethodTestParam {
ObserverMethodTestParam(
blink::mojom::MediaStreamType stream_type,
media::mojom::DisplayMediaInformationPtr display_media_info,
MockObserverSetExpectationsMethod observer_method,
AccessorMethod accessor_method)
: stream_type(stream_type),
display_media_info(display_media_info.Clone()),
observer_method(observer_method),
accessor_method(accessor_method) {}
ObserverMethodTestParam(const ObserverMethodTestParam& other)
: stream_type(other.stream_type),
display_media_info(other.display_media_info.Clone()),
observer_method(other.observer_method),
accessor_method(other.accessor_method) {}
blink::mojom::MediaStreamType stream_type;
media::mojom::DisplayMediaInformationPtr display_media_info;
MockObserverSetExpectationsMethod observer_method;
AccessorMethod accessor_method;
};
ObserverMethodTestParam kObserverMethodTestParams[] = {
{blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE,
/*display_media_info=*/nullptr,
&MockObserver::SetOnIsCapturingVideoChangedExpectation,
&MediaStreamCaptureIndicator::IsCapturingVideo},
{blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE,
/*display_media_info=*/nullptr,
&MockObserver::SetOnIsCapturingAudioChangedExpectation,
&MediaStreamCaptureIndicator::IsCapturingAudio},
{blink::mojom::MediaStreamType::GUM_TAB_VIDEO_CAPTURE,
/*display_media_info=*/nullptr,
&MockObserver::SetOnIsBeingMirroredChangedExpectation,
&MediaStreamCaptureIndicator::IsBeingMirrored},
{blink::mojom::MediaStreamType::GUM_DESKTOP_VIDEO_CAPTURE,
media::mojom::DisplayMediaInformation::New(
media::mojom::DisplayCaptureSurfaceType::BROWSER,
/*logical_surface=*/true,
media::mojom::CursorCaptureType::NEVER,
/*capture_handle=*/nullptr,
/*initial_zoom_level=*/100),
&MockObserver::SetOnIsCapturingTabChangedExpectation,
&MediaStreamCaptureIndicator::IsCapturingTab},
{blink::mojom::MediaStreamType::GUM_DESKTOP_VIDEO_CAPTURE,
media::mojom::DisplayMediaInformation::New(
media::mojom::DisplayCaptureSurfaceType::WINDOW,
/*logical_surface=*/true,
media::mojom::CursorCaptureType::NEVER,
/*capture_handle=*/nullptr,
/*initial_zoom_level=*/100),
&MockObserver::SetOnIsCapturingWindowChangedExpectation,
&MediaStreamCaptureIndicator::IsCapturingWindow},
{blink::mojom::MediaStreamType::GUM_DESKTOP_VIDEO_CAPTURE,
media::mojom::DisplayMediaInformation::New(
media::mojom::DisplayCaptureSurfaceType::MONITOR,
/*logical_surface=*/true,
media::mojom::CursorCaptureType::NEVER,
/*capture_handle=*/nullptr,
/*initial_zoom_level=*/100),
&MockObserver::SetOnIsCapturingDisplayChangedExpectation,
&MediaStreamCaptureIndicator::IsCapturingDisplay},
};
class MediaStreamCaptureIndicatorObserverMethodTest
: public MediaStreamCaptureIndicatorTest,
public testing::WithParamInterface<ObserverMethodTestParam> {};
blink::mojom::StreamDevices CreateFakeDevice(
const ObserverMethodTestParam& param) {
blink::mojom::StreamDevices fake_devices;
blink::MediaStreamDevice device(param.stream_type, "fake_device",
"fake_device");
if (param.display_media_info)
device.display_media_info = param.display_media_info->Clone();
if (blink::IsAudioInputMediaType(param.stream_type)) {
fake_devices.audio_device = device;
} else if (blink::IsVideoInputMediaType(param.stream_type)) {
fake_devices.video_device = device;
} else {
NOTREACHED();
}
return fake_devices;
}
struct StreamTypeTestParam {
StreamTypeTestParam(
blink::mojom::MediaStreamType video_stream_type,
content::DesktopMediaID::Type media_type,
MockObserverStreamTypeSetExpectationsMethod observer_method)
: video_stream_type(video_stream_type),
media_type(media_type),
observer_method(observer_method) {}
blink::mojom::MediaStreamType video_stream_type;
content::DesktopMediaID::Type media_type;
MockObserverStreamTypeSetExpectationsMethod observer_method;
};
StreamTypeTestParam kStreamTypeTestParams[] = {
{blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE,
content::DesktopMediaID::Type::TYPE_SCREEN,
&MockObserver::SetOnIsCapturingDisplayChangedExpectation},
{blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE_SET,
content::DesktopMediaID::Type::TYPE_SCREEN,
&MockObserver::SetOnIsCapturingDisplayChangedExpectation},
{blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE,
content::DesktopMediaID::Type::TYPE_WINDOW,
&MockObserver::SetOnIsCapturingWindowChangedExpectation},
{blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE_SET,
content::DesktopMediaID::Type::TYPE_WINDOW,
&MockObserver::SetOnIsCapturingWindowChangedExpectation},
};
class MediaStreamCaptureIndicatorStreamTypeTest
: public MediaStreamCaptureIndicatorTest,
public testing::WithParamInterface<StreamTypeTestParam> {};
} // namespace
TEST_P(MediaStreamCaptureIndicatorObserverMethodTest, AddAndRemoveDevice) {
const ObserverMethodTestParam& param = GetParam();
content::WebContents* source = web_contents();
// By default all accessors should return false as there's no stream device.
EXPECT_FALSE((indicator()->*(param.accessor_method))(web_contents()));
std::unique_ptr<content::MediaStreamUI> ui =
indicator()->RegisterMediaStream(source, CreateFakeDevice(param));
// Make sure that the observer gets called and that the corresponding accessor
// gets called when |OnStarted| is called.
(observer()->*(param.observer_method))(source, true);
ui->OnStarted(base::RepeatingClosure(),
content::MediaStreamUI::SourceCallback(),
/*label=*/std::string(), /*screen_capture_ids=*/{},
content::MediaStreamUI::StateChangeCallback());
EXPECT_TRUE((indicator()->*(param.accessor_method))(web_contents()));
::testing::Mock::VerifyAndClear(observer());
// Removing the stream device should cause the observer to be notified that
// the observed property is now set to false.
(observer()->*(param.observer_method))(source, false);
ui.reset();
EXPECT_FALSE((indicator()->*(param.accessor_method))(web_contents()));
::testing::Mock::VerifyAndClear(observer());
}
TEST_P(MediaStreamCaptureIndicatorObserverMethodTest, StopMediaCapturing) {
const ObserverMethodTestParam& param = GetParam();
const auto media_tpy =
MediaStreamCaptureIndicator::GetMediaType(param.stream_type);
content::WebContents* source = web_contents();
// By default all accessors should return false as there's no stream device.
EXPECT_FALSE((indicator()->*(param.accessor_method))(web_contents()));
std::unique_ptr<content::MediaStreamUI> ui =
indicator()->RegisterMediaStream(source, CreateFakeDevice(param));
auto stop_callback = base::BindLambdaForTesting([&]() { ui.reset(); });
// Make sure that the observer gets called and that the corresponding accessor
// gets called when |OnStarted| is called.
(observer()->*(param.observer_method))(source, true);
ui->OnStarted(std::move(stop_callback),
content::MediaStreamUI::SourceCallback(),
/*label=*/std::string(), /*screen_capture_ids=*/{},
content::MediaStreamUI::StateChangeCallback());
EXPECT_TRUE((indicator()->*(param.accessor_method))(web_contents()));
::testing::Mock::VerifyAndClear(observer());
// StopMediaCapturing calls the stop_callback which calls ui.reset; which will
// notify the observer_method that the capturing is stopped.
(observer()->*(param.observer_method))(source, false);
indicator()->StopMediaCapturing(source, media_tpy);
EXPECT_FALSE((indicator()->*(param.accessor_method))(web_contents()));
::testing::Mock::VerifyAndClear(observer());
}
TEST_P(MediaStreamCaptureIndicatorObserverMethodTest, CloseActiveWebContents) {
const ObserverMethodTestParam& param = GetParam();
content::WebContents* source = web_contents();
// Create and start the fake stream device.
std::unique_ptr<content::MediaStreamUI> ui =
indicator()->RegisterMediaStream(source, CreateFakeDevice(param));
(observer()->*(param.observer_method))(source, true);
ui->OnStarted(base::RepeatingClosure(),
content::MediaStreamUI::SourceCallback(),
/*label=*/std::string(), /*screen_capture_ids=*/{},
content::MediaStreamUI::StateChangeCallback());
::testing::Mock::VerifyAndClear(observer());
// Deleting the WebContents should cause the observer to be notified that the
// observed property is now set to false.
(observer()->*(param.observer_method))(source, false);
DeleteContents();
::testing::Mock::VerifyAndClear(observer());
}
INSTANTIATE_TEST_SUITE_P(All,
MediaStreamCaptureIndicatorObserverMethodTest,
testing::ValuesIn(kObserverMethodTestParams));
TEST_P(MediaStreamCaptureIndicatorStreamTypeTest,
CheckIsDeviceCapturingDisplay) {
const blink::mojom::MediaStreamType& video_stream_type =
GetParam().video_stream_type;
const content::DesktopMediaID::Type& media_type = GetParam().media_type;
content::WebContents* source = web_contents();
blink::mojom::StreamDevices devices;
std::unique_ptr<content::MediaStreamUI> ui = GetDevicesForDesktopCapture(
content::MediaStreamRequest(
/*render_process_id=*/0, /*render_frame_id=*/0, /*page_request_id=*/0,
/*url_origin=*/url::Origin(),
/*user_gesture=*/false,
blink::MediaStreamRequestType::MEDIA_GENERATE_STREAM,
/*requested_audio_device_ids=*/{},
/*requested_video_device_ids=*/{"fake_device"},
blink::mojom::MediaStreamType::NO_SERVICE, video_stream_type,
/*disable_local_echo=*/false,
/*request_pan_tilt_zoom_permission=*/false,
/*captured_surface_control_active=*/false),
source, content::DesktopMediaID(media_type, /*id=*/0),
/*capture_audio=*/false, /*disable_local_echo=*/false,
/*suppress_local_audio_playback=*/false, /*restrict_own_audio=*/false,
/*display_notification=*/false, /*application_title=*/u"",
/*captured_surface_control_active=*/false, devices);
ASSERT_EQ(devices.video_device->type, video_stream_type);
(observer()->*(GetParam().observer_method))(source, 2);
ui->OnStarted(base::RepeatingClosure(),
content::MediaStreamUI::SourceCallback(),
/*label=*/std::string(), /*screen_capture_ids=*/{},
content::MediaStreamUI::StateChangeCallback());
}
INSTANTIATE_TEST_SUITE_P(All,
MediaStreamCaptureIndicatorStreamTypeTest,
testing::ValuesIn(kStreamTypeTestParams));
|