summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/desktop_capture/win/wgc_capture_source_unittest.cc
blob: dc37ec2e0d3217102535567b6e2ca7dd338fbb86 (plain)
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
/*
 *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/desktop_capture/win/wgc_capture_source.h"

#include <windows.graphics.capture.h>
#include <wrl/client.h>

#include <utility>

#include "modules/desktop_capture/desktop_capture_types.h"
#include "modules/desktop_capture/desktop_geometry.h"
#include "modules/desktop_capture/win/screen_capture_utils.h"
#include "modules/desktop_capture/win/test_support/test_window.h"
#include "modules/desktop_capture/win/wgc_capturer_win.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/win/scoped_com_initializer.h"
#include "test/gtest.h"

namespace webrtc {
namespace {

const WCHAR kWindowTitle[] = L"WGC Capture Source Test Window";

const int kFirstXCoord = 25;
const int kFirstYCoord = 50;
const int kSecondXCoord = 50;
const int kSecondYCoord = 75;

}  // namespace

class WgcCaptureSourceTest : public ::testing::TestWithParam<CaptureType> {
 public:
  void SetUp() override {
    com_initializer_ =
        std::make_unique<ScopedCOMInitializer>(ScopedCOMInitializer::kMTA);
    ASSERT_TRUE(com_initializer_->Succeeded());
  }

  void TearDown() override {
    if (window_open_) {
      DestroyTestWindow(window_info_);
    }
  }

  void SetUpForWindowSource() {
    window_info_ = CreateTestWindow(kWindowTitle);
    window_open_ = true;
    source_id_ = reinterpret_cast<DesktopCapturer::SourceId>(window_info_.hwnd);
    source_factory_ = std::make_unique<WgcWindowSourceFactory>();
  }

  void SetUpForScreenSource() {
    source_id_ = kFullDesktopScreenId;
    source_factory_ = std::make_unique<WgcScreenSourceFactory>();
  }

 protected:
  std::unique_ptr<ScopedCOMInitializer> com_initializer_;
  std::unique_ptr<WgcCaptureSourceFactory> source_factory_;
  std::unique_ptr<WgcCaptureSource> source_;
  DesktopCapturer::SourceId source_id_;
  WindowInfo window_info_;
  bool window_open_ = false;
};

// Window specific test
TEST_F(WgcCaptureSourceTest, WindowPosition) {
  if (!IsWgcSupported(CaptureType::kWindow)) {
    RTC_LOG(LS_INFO)
        << "Skipping WgcCapturerWinTests on unsupported platforms.";
    GTEST_SKIP();
  }

  SetUpForWindowSource();
  source_ = source_factory_->CreateCaptureSource(source_id_);
  ASSERT_TRUE(source_);
  EXPECT_EQ(source_->GetSourceId(), source_id_);

  MoveTestWindow(window_info_.hwnd, kFirstXCoord, kFirstYCoord);
  DesktopVector source_vector = source_->GetTopLeft();
  EXPECT_EQ(source_vector.x(), kFirstXCoord);
  EXPECT_EQ(source_vector.y(), kFirstYCoord);

  MoveTestWindow(window_info_.hwnd, kSecondXCoord, kSecondYCoord);
  source_vector = source_->GetTopLeft();
  EXPECT_EQ(source_vector.x(), kSecondXCoord);
  EXPECT_EQ(source_vector.y(), kSecondYCoord);
}

// Screen specific test
TEST_F(WgcCaptureSourceTest, ScreenPosition) {
  if (!IsWgcSupported(CaptureType::kScreen)) {
    RTC_LOG(LS_INFO)
        << "Skipping WgcCapturerWinTests on unsupported platforms.";
    GTEST_SKIP();
  }

  SetUpForScreenSource();
  source_ = source_factory_->CreateCaptureSource(source_id_);
  ASSERT_TRUE(source_);
  EXPECT_EQ(source_id_, source_->GetSourceId());

  DesktopRect screen_rect = GetFullscreenRect();
  DesktopVector source_vector = source_->GetTopLeft();
  EXPECT_EQ(source_vector.x(), screen_rect.left());
  EXPECT_EQ(source_vector.y(), screen_rect.top());
}

// Source agnostic test
TEST_P(WgcCaptureSourceTest, CreateSource) {
  if (!IsWgcSupported(GetParam())) {
    RTC_LOG(LS_INFO)
        << "Skipping WgcCapturerWinTests on unsupported platforms.";
    GTEST_SKIP();
  }

  if (GetParam() == CaptureType::kWindow) {
    SetUpForWindowSource();
  } else {
    SetUpForScreenSource();
  }

  source_ = source_factory_->CreateCaptureSource(source_id_);
  ASSERT_TRUE(source_);
  EXPECT_EQ(source_id_, source_->GetSourceId());
  EXPECT_TRUE(source_->IsCapturable());

  Microsoft::WRL::ComPtr<ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>
      item;
  EXPECT_TRUE(SUCCEEDED(source_->GetCaptureItem(&item)));
  EXPECT_TRUE(item);
}

INSTANTIATE_TEST_SUITE_P(SourceAgnostic,
                         WgcCaptureSourceTest,
                         ::testing::Values(CaptureType::kWindow,
                                           CaptureType::kScreen));

}  // namespace webrtc