blob: 0c31fb7823a273f16bbf4096c90d61ea082780c1 [file] [log] [blame]
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +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
kwiberg2bb3afa2016-03-16 15:58:08 -070011#include <memory>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/desktop_capture/desktop_capturer.h"
14#include "modules/desktop_capture/desktop_capture_options.h"
15#include "modules/desktop_capture/desktop_frame.h"
16#include "modules/desktop_capture/desktop_region.h"
17#include "test/gtest.h"
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000018
19namespace webrtc {
20
21class WindowCapturerTest : public testing::Test,
22 public DesktopCapturer::Callback {
23 public:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000024 void SetUp() override {
zijiehe30455892016-11-09 16:37:48 -080025 capturer_ = DesktopCapturer::CreateWindowCapturer(
26 DesktopCaptureOptions::CreateDefault());
Taylor Brandstetter569f4e72017-10-13 12:53:51 -070027 RTC_DCHECK(capturer_);
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000028 }
29
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000030 void TearDown() override {}
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000031
32 // DesktopCapturer::Callback interface
sergeyu5d910282016-06-07 16:41:58 -070033 void OnCaptureResult(DesktopCapturer::Result result,
34 std::unique_ptr<DesktopFrame> frame) override {
35 frame_ = std::move(frame);
36 }
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000037
38 protected:
zijiehe30455892016-11-09 16:37:48 -080039 std::unique_ptr<DesktopCapturer> capturer_;
kwiberg2bb3afa2016-03-16 15:58:08 -070040 std::unique_ptr<DesktopFrame> frame_;
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000041};
42
43// Verify that we can enumerate windows.
44TEST_F(WindowCapturerTest, Enumerate) {
zijiehefce49052016-11-07 15:25:18 -080045 DesktopCapturer::SourceList sources;
46 EXPECT_TRUE(capturer_->GetSourceList(&sources));
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000047
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000048 // Verify that window titles are set.
zijiehefce49052016-11-07 15:25:18 -080049 for (auto it = sources.begin(); it != sources.end(); ++it) {
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000050 EXPECT_FALSE(it->title.empty());
51 }
52}
53
Taylor Brandstetter569f4e72017-10-13 12:53:51 -070054// Flaky on Linux. See: crbug.com/webrtc/7830
55#if defined(WEBRTC_LINUX)
56#define MAYBE_Capture DISABLED_Capture
57#else
58#define MAYBE_Capture Capture
59#endif
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000060// Verify we can capture a window.
61//
62// TODO(sergeyu): Currently this test just looks at the windows that already
63// exist. Ideally it should create a test window and capture from it, but there
64// is no easy cross-platform way to create new windows (potentially we could
65// have a python script showing Tk dialog, but launching code will differ
66// between platforms).
Taylor Brandstetter569f4e72017-10-13 12:53:51 -070067TEST_F(WindowCapturerTest, MAYBE_Capture) {
zijiehefce49052016-11-07 15:25:18 -080068 DesktopCapturer::SourceList sources;
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000069 capturer_->Start(this);
zijiehefce49052016-11-07 15:25:18 -080070 EXPECT_TRUE(capturer_->GetSourceList(&sources));
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000071
72 // Verify that we can select and capture each window.
zijiehefce49052016-11-07 15:25:18 -080073 for (auto it = sources.begin(); it != sources.end(); ++it) {
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000074 frame_.reset();
zijiehefce49052016-11-07 15:25:18 -080075 if (capturer_->SelectSource(it->id)) {
zijiehe249beee2016-10-18 23:13:29 -070076 capturer_->CaptureFrame();
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000077 }
78
79 // If we failed to capture a window make sure it no longer exists.
80 if (!frame_.get()) {
zijiehefce49052016-11-07 15:25:18 -080081 DesktopCapturer::SourceList new_list;
82 EXPECT_TRUE(capturer_->GetSourceList(&new_list));
83 for (auto new_list_it = new_list.begin(); new_list_it != new_list.end();
84 ++new_list_it) {
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000085 EXPECT_FALSE(it->id == new_list_it->id);
86 }
87 continue;
88 }
89
90 EXPECT_GT(frame_->size().width(), 0);
91 EXPECT_GT(frame_->size().height(), 0);
92 }
93}
94
sergeyu@chromium.orgb10ccbe2013-05-19 07:02:48 +000095} // namespace webrtc