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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/media/capture/screen_capture_kit_fullscreen_module.h"
#include "base/task/bind_post_task.h"
#import "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "base/timer/timer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "ui/gfx/geometry/rect.h"
namespace content {
namespace {
MATCHER_P(MatchesScWindow, ID, "") {
*result_listener << "with ID " << arg.windowID;
return arg.windowID == ID;
}
static NSString* const kEmptyString = @"";
static NSString* const kApplicationNameFooBar = @"FooBar";
static NSString* const kApplicationNameKeynote = @"Keynote";
static NSString* const kApplicationNameLibreOffice = @"LibreOffice";
static NSString* const kApplicationNameOpenOffice = @"OpenOffice";
static NSString* const kApplicationNamePowerPoint = @"Microsoft PowerPoint";
constexpr gfx::Rect kDisplayPrimary(0, 0, 1920, 1080);
constexpr gfx::Rect kDisplaySecondary(-1920, 10, 1920, 1080);
constexpr gfx::Rect kFrameOther(140, 170, 600, 400);
constexpr gfx::Rect kFrameEditor(10, 20, 1280, 720);
constexpr gfx::Rect kFrameSlideshow = kDisplayPrimary;
constexpr gfx::Rect kFramePresentersView = kDisplaySecondary;
enum class Application {
kFoobar = 0,
kPowerPoint = 1,
kKeynote = 2,
kOpenOffice = 3,
kLibreOffice = 4,
kSize
};
enum class Mode {
kOther = 0,
kEditor = 1,
kPresentersView = 2,
kSlideshow = 3,
};
class WindowConfig {
public:
WindowConfig(Application application,
Mode mode,
int doc_index,
bool on_screen)
: application_(application),
mode_(mode),
doc_index_(doc_index),
on_screen_(on_screen) {}
pid_t process_id() const {
return static_cast<int>(application_) * 11 + 1234;
}
NSString* application_name() const {
switch (application_) {
case Application::kFoobar:
return kApplicationNameFooBar;
case Application::kPowerPoint:
return kApplicationNamePowerPoint;
case Application::kKeynote:
return kApplicationNameKeynote;
case Application::kOpenOffice:
return kApplicationNameOpenOffice;
case Application::kLibreOffice:
return kApplicationNameLibreOffice;
default:
return kEmptyString;
}
}
NSString* window_title() const {
switch (application_) {
case Application::kFoobar:
return kEmptyString;
case Application::kPowerPoint: {
NSString* doc_name =
[NSString stringWithFormat:@"Presentation%d", doc_index_];
switch (mode_) {
case Mode::kEditor:
return doc_name;
case Mode::kPresentersView:
return [NSString
stringWithFormat:@"PowerPoint Presenter View - [%@]", doc_name];
case Mode::kSlideshow:
return [NSString
stringWithFormat:@"PowerPoint Slide Show - [%@]", doc_name];
default:
return kEmptyString;
}
}
case Application::kKeynote: {
NSString* doc_name =
[NSString stringWithFormat:@"Untitled%d", doc_index_];
return doc_name;
}
case Application::kOpenOffice: {
NSString* doc_name =
[NSString stringWithFormat:@"Untitled %d", doc_index_];
switch (mode_) {
case Mode::kEditor:
case Mode::kPresentersView:
return [NSString
stringWithFormat:@"%@ - OpenOffice Impress", doc_name];
case Mode::kSlideshow:
default:
return kEmptyString;
}
}
default:
return kEmptyString;
}
}
int window_layer() const { return static_cast<int>(mode_); }
bool on_screen() const { return on_screen_; }
void set_on_screen(bool on_screen) { on_screen_ = on_screen; }
CGRect frame() const {
switch (mode_) {
case Mode::kOther:
return kFrameOther.ToCGRect();
case Mode::kEditor:
return kFrameEditor.ToCGRect();
case Mode::kPresentersView:
return kFramePresentersView.ToCGRect();
case Mode::kSlideshow:
return kFrameSlideshow.ToCGRect();
default:
NOTREACHED();
}
}
bool active() const { return active_; }
void set_active(bool active) { active_ = active; }
private:
Application application_;
Mode mode_;
int doc_index_;
bool on_screen_;
bool active_ = true;
};
SCDisplay* API_AVAILABLE(macos(12.3)) CreateSCDisplay(CGRect frame) {
id display = OCMClassMock([SCDisplay class]);
OCMStub([display frame]).andReturn(frame);
return display;
}
} // namespace
class API_AVAILABLE(macos(12.3)) MockResetStreamInterface
: public ScreenCaptureKitResetStreamInterface {
public:
MOCK_METHOD1(ResetStreamTo, void(SCWindow* window));
};
class SCKFullscreenModuleTest : public testing::Test {
public:
SCKFullscreenModuleTest() = default;
void SetUp() override {}
SCWindow* API_AVAILABLE(macos(12.3)) AddWindow(WindowConfig window_config) {
windows_.push_back(window_config);
return CreateSCWindow(window_config, windows_.size());
}
void API_AVAILABLE(macos(12.3)) getShareableContentMock(
ScreenCaptureKitFullscreenModule::ContentHandler handler) {
NSArray* windows = [NSArray array];
for (size_t i = 0; i < windows_.size(); ++i) {
if (windows_[i].active()) {
windows =
[windows arrayByAddingObject:CreateSCWindow(windows_[i], i + 1)];
}
}
NSArray* displays = @[
CreateSCDisplay(kDisplayPrimary.ToCGRect()),
CreateSCDisplay(kDisplaySecondary.ToCGRect())
];
id content = OCMClassMock([SCShareableContent class]);
OCMStub([content windows]).andReturn(windows);
OCMStub([content displays]).andReturn(displays);
std::move(handler).Run(content);
}
protected:
SCWindow* API_AVAILABLE(macos(12.3))
CreateSCWindow(WindowConfig config, CGWindowID window_id) const {
id window = OCMClassMock([SCWindow class]);
id owning_application = OCMClassMock([SCRunningApplication class]);
OCMStub([owning_application applicationName])
.andReturn(config.application_name());
OCMStub([owning_application processID]).andReturn(config.process_id());
OCMStub([window owningApplication]).andReturn(owning_application);
OCMStub([window title]).andReturn(config.window_title());
OCMStub([window windowID]).andReturn(window_id);
OCMStub([window windowLayer]).andReturn(config.window_layer());
OCMStub([window frame]).andReturn(config.frame());
OCMStub([window isOnScreen]).andReturn(config.on_screen());
return window;
}
void SetWindowOnScreen(CGWindowID id, bool on_screen) {
windows_[id - 1].set_on_screen(on_screen);
}
void SetWindowActive(CGWindowID id, bool active) {
windows_[id - 1].set_active(active);
}
void StepForward(base::TimeDelta delta, int steps) {
for (int i = 0; i < steps; ++i) {
task_environment_.FastForwardBy(delta);
}
}
base::test::SingleThreadTaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
std::vector<WindowConfig> windows_;
};
TEST_F(SCKFullscreenModuleTest, PowerPointInitialization) {
if (@available(macOS 13.1, *)) {
MockResetStreamInterface reset_stream_interface;
SCWindow* editor_window = AddWindow(
{Application::kPowerPoint, Mode::kEditor, 0, /*on_screen=*/true});
std::unique_ptr<ScreenCaptureKitFullscreenModule> fullscreen_module =
MaybeCreateScreenCaptureKitFullscreenModule(
base::SingleThreadTaskRunner::GetCurrentDefault(),
reset_stream_interface, editor_window);
EXPECT_TRUE(fullscreen_module);
EXPECT_EQ(fullscreen_module->get_mode(),
ScreenCaptureKitFullscreenModule::Mode::kPowerPoint);
}
}
TEST_F(SCKFullscreenModuleTest, OpenOfficeInitialization) {
if (@available(macOS 13.1, *)) {
MockResetStreamInterface reset_stream_interface;
SCWindow* editor_window = AddWindow(
{Application::kOpenOffice, Mode::kEditor, 0, /*on_screen=*/true});
std::unique_ptr<ScreenCaptureKitFullscreenModule> fullscreen_module =
MaybeCreateScreenCaptureKitFullscreenModule(
base::SingleThreadTaskRunner::GetCurrentDefault(),
reset_stream_interface, editor_window);
EXPECT_TRUE(fullscreen_module);
EXPECT_EQ(fullscreen_module->get_mode(),
ScreenCaptureKitFullscreenModule::Mode::kOpenOffice);
}
}
TEST_F(SCKFullscreenModuleTest, KeynoteInitialization) {
if (@available(macOS 13.1, *)) {
MockResetStreamInterface reset_stream_interface;
SCWindow* editor_window = AddWindow(
{Application::kKeynote, Mode::kEditor, 0, /*on_screen=*/true});
std::unique_ptr<ScreenCaptureKitFullscreenModule> fullscreen_module =
MaybeCreateScreenCaptureKitFullscreenModule(
base::SingleThreadTaskRunner::GetCurrentDefault(),
reset_stream_interface, editor_window);
EXPECT_TRUE(fullscreen_module);
EXPECT_EQ(fullscreen_module->get_mode(),
ScreenCaptureKitFullscreenModule::Mode::kKeynote);
}
}
TEST_F(SCKFullscreenModuleTest, LibreOfficeInitializationFails) {
if (@available(macOS 13.1, *)) {
MockResetStreamInterface reset_stream_interface;
SCWindow* editor_window = AddWindow(
{Application::kLibreOffice, Mode::kEditor, 0, /*on_screen=*/true});
std::unique_ptr<ScreenCaptureKitFullscreenModule> fullscreen_module =
MaybeCreateScreenCaptureKitFullscreenModule(
base::SingleThreadTaskRunner::GetCurrentDefault(),
reset_stream_interface, editor_window);
EXPECT_FALSE(fullscreen_module);
}
}
TEST_F(SCKFullscreenModuleTest, FooBarInitializationFails) {
if (@available(macOS 13.1, *)) {
MockResetStreamInterface reset_stream_interface;
SCWindow* editor_window =
AddWindow({Application::kFoobar, Mode::kEditor, 0, /*on_screen=*/true});
std::unique_ptr<ScreenCaptureKitFullscreenModule> fullscreen_module =
MaybeCreateScreenCaptureKitFullscreenModule(
base::SingleThreadTaskRunner::GetCurrentDefault(),
reset_stream_interface, editor_window);
EXPECT_FALSE(fullscreen_module);
}
}
// Test that the callback is run.
// Add content when calling the callback handler.
TEST_F(SCKFullscreenModuleTest, DetectFullscreenWindowPowerPoint) {
if (@available(macOS 13.1, *)) {
MockResetStreamInterface reset_stream_interface;
// Add other application window as first window.
AddWindow({Application::kFoobar, Mode::kEditor, 0, /*on_screen=*/true});
SCWindow* editor_window = AddWindow(
{Application::kPowerPoint, Mode::kEditor, 0, /*on_screen=*/true});
// Add some more applications.
AddWindow({Application::kKeynote, Mode::kEditor, 0, /*on_screen=*/true});
AddWindow({Application::kOpenOffice, Mode::kEditor, 0, /*on_screen=*/true});
std::unique_ptr<ScreenCaptureKitFullscreenModule> fullscreen_module =
MaybeCreateScreenCaptureKitFullscreenModule(
base::SingleThreadTaskRunner::GetCurrentDefault(),
reset_stream_interface, editor_window);
EXPECT_TRUE(fullscreen_module);
EXPECT_EQ(fullscreen_module->get_mode(),
ScreenCaptureKitFullscreenModule::Mode::kPowerPoint);
// Unretained since the test fixture must outlive the test.
fullscreen_module->set_get_sharable_content_for_test(
base::BindRepeating(&SCKFullscreenModuleTest::getShareableContentMock,
base::Unretained(this)));
// Start process of checking for new fullscreen windows and run for a few
// seconds.
fullscreen_module->Start();
StepForward(base::Seconds(1), /*steps=*/4);
EXPECT_FALSE(fullscreen_module->is_fullscreen_window_active());
// Change to fullscreen mode.
SCWindow* slideshow_window = AddWindow(
{Application::kPowerPoint, Mode::kSlideshow, 0, /*on_screen=*/true});
AddWindow({Application::kPowerPoint, Mode::kPresentersView, 0,
/*on_screen=*/true});
EXPECT_CALL(reset_stream_interface,
ResetStreamTo(MatchesScWindow(slideshow_window.windowID)))
.Times(1);
StepForward(base::Seconds(1), /*steps=*/1);
EXPECT_TRUE(fullscreen_module->is_fullscreen_window_active());
StepForward(base::Seconds(1), /*steps=*/4);
EXPECT_TRUE(fullscreen_module->is_fullscreen_window_active());
// For PowerPoint, the fullscreen window is closed once the slideshow stops.
// The stream will then be reset to the editor window by
// ScreenCaptureKitDeviceMac.
}
}
TEST_F(SCKFullscreenModuleTest, DetectFullscreenWindowKeynote) {
if (@available(macOS 13.1, *)) {
MockResetStreamInterface reset_stream_interface;
// Add other application window as first window.
AddWindow({Application::kFoobar, Mode::kEditor, 0, /*on_screen=*/true});
SCWindow* editor_window = AddWindow(
{Application::kKeynote, Mode::kEditor, 0, /*on_screen=*/true});
// Add some more applications.
AddWindow({Application::kPowerPoint, Mode::kEditor, 0, /*on_screen=*/true});
AddWindow({Application::kOpenOffice, Mode::kEditor, 0, /*on_screen=*/true});
std::unique_ptr<ScreenCaptureKitFullscreenModule> fullscreen_module =
MaybeCreateScreenCaptureKitFullscreenModule(
base::SingleThreadTaskRunner::GetCurrentDefault(),
reset_stream_interface, editor_window);
EXPECT_TRUE(fullscreen_module);
EXPECT_EQ(fullscreen_module->get_mode(),
ScreenCaptureKitFullscreenModule::Mode::kKeynote);
// Unretained since the test fixture must outlive the test.
fullscreen_module->set_get_sharable_content_for_test(
base::BindRepeating(&SCKFullscreenModuleTest::getShareableContentMock,
base::Unretained(this)));
// Start process of checking for new fullscreen windows and run for a few
// seconds.
fullscreen_module->Start();
StepForward(base::Seconds(1), /*steps=*/4);
EXPECT_FALSE(fullscreen_module->is_fullscreen_window_active());
// Hide editor and change to fullscreen mode.
SetWindowOnScreen(editor_window.windowID, false);
SCWindow* slideshow_window = AddWindow(
{Application::kKeynote, Mode::kSlideshow, 0, /*on_screen=*/true});
SCWindow* presenters_view_window =
AddWindow({Application::kKeynote, Mode::kPresentersView, 0,
/*on_screen=*/true});
EXPECT_CALL(reset_stream_interface,
ResetStreamTo(MatchesScWindow(slideshow_window.windowID)))
.Times(1);
StepForward(base::Seconds(1), /*steps=*/1);
EXPECT_TRUE(fullscreen_module->is_fullscreen_window_active());
StepForward(base::Seconds(1), /*steps=*/4);
EXPECT_TRUE(fullscreen_module->is_fullscreen_window_active());
// Hide fullscreen windows and restore editor window.
SetWindowOnScreen(editor_window.windowID, true);
SetWindowOnScreen(slideshow_window.windowID, false);
SetWindowOnScreen(presenters_view_window.windowID, false);
EXPECT_CALL(reset_stream_interface,
ResetStreamTo(MatchesScWindow(editor_window.windowID)))
.Times(1);
task_environment_.FastForwardBy(base::Seconds(1));
EXPECT_FALSE(fullscreen_module->is_fullscreen_window_active());
}
}
TEST_F(SCKFullscreenModuleTest, DetectFullscreenWindowOpenOfficeImpress) {
if (@available(macOS 13.1, *)) {
MockResetStreamInterface reset_stream_interface;
// Add other application window as first window.
AddWindow({Application::kFoobar, Mode::kEditor, 0, /*on_screen=*/true});
SCWindow* editor_window = AddWindow(
{Application::kOpenOffice, Mode::kEditor, 0, /*on_screen=*/true});
SCWindow* second_editor_window = AddWindow(
{Application::kOpenOffice, Mode::kEditor, 1, /*on_screen=*/true});
// Add some more applications.
AddWindow({Application::kPowerPoint, Mode::kEditor, 0, /*on_screen=*/true});
AddWindow({Application::kKeynote, Mode::kEditor, 0, /*on_screen=*/true});
std::unique_ptr<ScreenCaptureKitFullscreenModule> fullscreen_module =
MaybeCreateScreenCaptureKitFullscreenModule(
base::SingleThreadTaskRunner::GetCurrentDefault(),
reset_stream_interface, editor_window);
EXPECT_TRUE(fullscreen_module);
EXPECT_EQ(fullscreen_module->get_mode(),
ScreenCaptureKitFullscreenModule::Mode::kOpenOffice);
// Unretained since the test fixture must outlive the test.
fullscreen_module->set_get_sharable_content_for_test(
base::BindRepeating(&SCKFullscreenModuleTest::getShareableContentMock,
base::Unretained(this)));
// Start process of checking for new fullscreen windows and run for a few
// seconds.
fullscreen_module->Start();
StepForward(base::Seconds(1), /*steps=*/4);
EXPECT_FALSE(fullscreen_module->is_fullscreen_window_active());
// Change to fullscreen mode.
SCWindow* slideshow_window = AddWindow(
{Application::kOpenOffice, Mode::kSlideshow, 0, /*on_screen=*/true});
SCWindow* presenters_view_window =
AddWindow({Application::kOpenOffice, Mode::kPresentersView, 0,
/*on_screen=*/true});
EXPECT_CALL(reset_stream_interface,
ResetStreamTo(MatchesScWindow(slideshow_window.windowID)))
.Times(1);
// Ignore fullscreen window since there are two presentations open and we
// cannot determine which presentation is in full screen.
StepForward(base::Seconds(1), /*steps=*/4);
EXPECT_FALSE(fullscreen_module->is_fullscreen_window_active());
// Close the second document window and verify that the fullscreen window is
// detected.
SetWindowOnScreen(second_editor_window.windowID, false);
StepForward(base::Seconds(1), /*steps=*/1);
EXPECT_TRUE(fullscreen_module->is_fullscreen_window_active());
StepForward(base::Seconds(1), /*steps=*/4);
EXPECT_TRUE(fullscreen_module->is_fullscreen_window_active());
// Hide fullscreen windows and restore editor window.
SetWindowOnScreen(editor_window.windowID, true);
SetWindowOnScreen(slideshow_window.windowID, false);
SetWindowOnScreen(presenters_view_window.windowID, false);
EXPECT_CALL(reset_stream_interface,
ResetStreamTo(MatchesScWindow(editor_window.windowID)))
.Times(1);
task_environment_.FastForwardBy(base::Seconds(1));
EXPECT_FALSE(fullscreen_module->is_fullscreen_window_active());
}
}
} // namespace content
|