1 /*
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
11 #include <memory>
12 #include <string>
13 #include <utility>
14
15 #include "modules/desktop_capture/desktop_capture_options.h"
16 #include "modules/desktop_capture/desktop_capturer.h"
17 #include "modules/desktop_capture/desktop_frame.h"
18 #include "modules/desktop_capture/desktop_geometry.h"
19 #include "rtc_base/checks.h"
20 #include "test/gtest.h"
21
22 namespace webrtc {
23
24 class WindowCapturerTest : public ::testing::Test,
25 public DesktopCapturer::Callback {
26 public:
SetUp()27 void SetUp() override {
28 capturer_ = DesktopCapturer::CreateWindowCapturer(
29 DesktopCaptureOptions::CreateDefault());
30 ASSERT_TRUE(capturer_);
31 }
32
TearDown()33 void TearDown() override {}
34
35 // DesktopCapturer::Callback interface
OnCaptureResult(DesktopCapturer::Result result,std::unique_ptr<DesktopFrame> frame)36 void OnCaptureResult(DesktopCapturer::Result result,
37 std::unique_ptr<DesktopFrame> frame) override {
38 frame_ = std::move(frame);
39 }
40
41 protected:
42 std::unique_ptr<DesktopCapturer> capturer_;
43 std::unique_ptr<DesktopFrame> frame_;
44 };
45
46 // Verify that we can enumerate windows.
TEST_F(WindowCapturerTest,Enumerate)47 TEST_F(WindowCapturerTest, Enumerate) {
48 DesktopCapturer::SourceList sources;
49 EXPECT_TRUE(capturer_->GetSourceList(&sources));
50
51 // Verify that window titles are set.
52 for (auto it = sources.begin(); it != sources.end(); ++it) {
53 EXPECT_FALSE(it->title.empty());
54 }
55 }
56
57 // Flaky on Linux. See: crbug.com/webrtc/7830
58 #if defined(WEBRTC_LINUX)
59 #define MAYBE_Capture DISABLED_Capture
60 #else
61 #define MAYBE_Capture Capture
62 #endif
63 // Verify we can capture a window.
64 //
65 // TODO(sergeyu): Currently this test just looks at the windows that already
66 // exist. Ideally it should create a test window and capture from it, but there
67 // is no easy cross-platform way to create new windows (potentially we could
68 // have a python script showing Tk dialog, but launching code will differ
69 // between platforms).
TEST_F(WindowCapturerTest,MAYBE_Capture)70 TEST_F(WindowCapturerTest, MAYBE_Capture) {
71 DesktopCapturer::SourceList sources;
72 capturer_->Start(this);
73 EXPECT_TRUE(capturer_->GetSourceList(&sources));
74
75 // Verify that we can select and capture each window.
76 for (auto it = sources.begin(); it != sources.end(); ++it) {
77 frame_.reset();
78 if (capturer_->SelectSource(it->id)) {
79 capturer_->CaptureFrame();
80 }
81
82 // If we failed to capture a window make sure it no longer exists.
83 if (!frame_.get()) {
84 DesktopCapturer::SourceList new_list;
85 EXPECT_TRUE(capturer_->GetSourceList(&new_list));
86 for (auto new_list_it = new_list.begin(); new_list_it != new_list.end();
87 ++new_list_it) {
88 EXPECT_FALSE(it->id == new_list_it->id);
89 }
90 continue;
91 }
92
93 EXPECT_GT(frame_->size().width(), 0);
94 EXPECT_GT(frame_->size().height(), 0);
95 }
96 }
97
98 } // namespace webrtc
99