blob: 7133a9ebbcd21d75c7be3c6716ad22911154f988 [file] [log] [blame]
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000011#include <ApplicationServices/ApplicationServices.h>
12
kwiberg2bb3afa2016-03-16 15:58:08 -070013#include <memory>
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000014#include <ostream>
15
zijiehe98903d22016-11-10 21:57:10 -080016#include "webrtc/modules/desktop_capture/desktop_capturer.h"
sergeyu6acd9f42016-04-21 09:46:22 -070017#include "webrtc/modules/desktop_capture/desktop_capture_options.h"
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000018#include "webrtc/modules/desktop_capture/desktop_frame.h"
19#include "webrtc/modules/desktop_capture/desktop_geometry.h"
20#include "webrtc/modules/desktop_capture/desktop_region.h"
21#include "webrtc/modules/desktop_capture/mac/desktop_configuration.h"
zijiehe372719b2016-11-11 17:18:34 -080022#include "webrtc/modules/desktop_capture/mock_desktop_capturer_callback.h"
kwibergac9f8762016-09-30 22:29:43 -070023#include "webrtc/test/gtest.h"
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000024
25using ::testing::_;
26using ::testing::AnyNumber;
27using ::testing::Return;
28
29namespace webrtc {
30
31class ScreenCapturerMacTest : public testing::Test {
32 public:
33 // Verifies that the whole screen is initially dirty.
sergeyu5d910282016-06-07 16:41:58 -070034 void CaptureDoneCallback1(DesktopCapturer::Result result,
35 std::unique_ptr<DesktopFrame>* frame);
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000036
37 // Verifies that a rectangle explicitly marked as dirty is propagated
38 // correctly.
sergeyu5d910282016-06-07 16:41:58 -070039 void CaptureDoneCallback2(DesktopCapturer::Result result,
40 std::unique_ptr<DesktopFrame>* frame);
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000041
42 protected:
sergeyu6acd9f42016-04-21 09:46:22 -070043 void SetUp() override {
zijiehe30455892016-11-09 16:37:48 -080044 capturer_ = DesktopCapturer::CreateScreenCapturer(
45 DesktopCaptureOptions::CreateDefault());
sergeyu6acd9f42016-04-21 09:46:22 -070046 }
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000047
zijiehe30455892016-11-09 16:37:48 -080048 std::unique_ptr<DesktopCapturer> capturer_;
zijiehe372719b2016-11-11 17:18:34 -080049 MockDesktopCapturerCallback callback_;
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000050};
51
52void ScreenCapturerMacTest::CaptureDoneCallback1(
sergeyu5d910282016-06-07 16:41:58 -070053 DesktopCapturer::Result result,
54 std::unique_ptr<DesktopFrame>* frame) {
55 EXPECT_EQ(result, DesktopCapturer::Result::SUCCESS);
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000056
57 MacDesktopConfiguration config = MacDesktopConfiguration::GetCurrent(
58 MacDesktopConfiguration::BottomLeftOrigin);
59
60 // Verify that the region contains full frame.
sergeyu5d910282016-06-07 16:41:58 -070061 DesktopRegion::Iterator it((*frame)->updated_region());
sergeyu@chromium.org7d055a62014-04-18 23:45:38 +000062 EXPECT_TRUE(!it.IsAtEnd() && it.rect().equals(config.pixel_bounds));
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000063}
64
65void ScreenCapturerMacTest::CaptureDoneCallback2(
sergeyu5d910282016-06-07 16:41:58 -070066 DesktopCapturer::Result result,
67 std::unique_ptr<DesktopFrame>* frame) {
68 EXPECT_EQ(result, DesktopCapturer::Result::SUCCESS);
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000069
70 MacDesktopConfiguration config = MacDesktopConfiguration::GetCurrent(
71 MacDesktopConfiguration::BottomLeftOrigin);
sergeyu@chromium.org7d055a62014-04-18 23:45:38 +000072 int width = config.pixel_bounds.width();
73 int height = config.pixel_bounds.height();
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000074
sergeyu5d910282016-06-07 16:41:58 -070075 EXPECT_EQ(width, (*frame)->size().width());
76 EXPECT_EQ(height, (*frame)->size().height());
77 EXPECT_TRUE((*frame)->data() != NULL);
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000078 // Depending on the capture method, the screen may be flipped or not, so
79 // the stride may be positive or negative.
80 EXPECT_EQ(static_cast<int>(sizeof(uint32_t) * width),
sergeyu5d910282016-06-07 16:41:58 -070081 abs((*frame)->stride()));
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000082}
83
84TEST_F(ScreenCapturerMacTest, Capture) {
sergeyu5d910282016-06-07 16:41:58 -070085 EXPECT_CALL(callback_,
86 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _))
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000087 .Times(2)
88 .WillOnce(Invoke(this, &ScreenCapturerMacTest::CaptureDoneCallback1))
89 .WillOnce(Invoke(this, &ScreenCapturerMacTest::CaptureDoneCallback2));
90
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000091 SCOPED_TRACE("");
92 capturer_->Start(&callback_);
93
94 // Check that we get an initial full-screen updated.
zijiehe249beee2016-10-18 23:13:29 -070095 capturer_->CaptureFrame();
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000096
97 // Check that subsequent dirty rects are propagated correctly.
zijiehe249beee2016-10-18 23:13:29 -070098 capturer_->CaptureFrame();
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000099}
100
101} // namespace webrtc