sergeyu@chromium.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 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 | |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #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.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | class WindowCapturerTest : public testing::Test, |
| 22 | public DesktopCapturer::Callback { |
| 23 | public: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 24 | void SetUp() override { |
zijiehe | 3045589 | 2016-11-09 16:37:48 -0800 | [diff] [blame] | 25 | capturer_ = DesktopCapturer::CreateWindowCapturer( |
| 26 | DesktopCaptureOptions::CreateDefault()); |
Taylor Brandstetter | 569f4e7 | 2017-10-13 12:53:51 -0700 | [diff] [blame] | 27 | RTC_DCHECK(capturer_); |
sergeyu@chromium.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 28 | } |
| 29 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 30 | void TearDown() override {} |
sergeyu@chromium.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 31 | |
| 32 | // DesktopCapturer::Callback interface |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 33 | void OnCaptureResult(DesktopCapturer::Result result, |
| 34 | std::unique_ptr<DesktopFrame> frame) override { |
| 35 | frame_ = std::move(frame); |
| 36 | } |
sergeyu@chromium.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 37 | |
| 38 | protected: |
zijiehe | 3045589 | 2016-11-09 16:37:48 -0800 | [diff] [blame] | 39 | std::unique_ptr<DesktopCapturer> capturer_; |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 40 | std::unique_ptr<DesktopFrame> frame_; |
sergeyu@chromium.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | // Verify that we can enumerate windows. |
| 44 | TEST_F(WindowCapturerTest, Enumerate) { |
zijiehe | fce4905 | 2016-11-07 15:25:18 -0800 | [diff] [blame] | 45 | DesktopCapturer::SourceList sources; |
| 46 | EXPECT_TRUE(capturer_->GetSourceList(&sources)); |
sergeyu@chromium.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 47 | |
sergeyu@chromium.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 48 | // Verify that window titles are set. |
zijiehe | fce4905 | 2016-11-07 15:25:18 -0800 | [diff] [blame] | 49 | for (auto it = sources.begin(); it != sources.end(); ++it) { |
sergeyu@chromium.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 50 | EXPECT_FALSE(it->title.empty()); |
| 51 | } |
| 52 | } |
| 53 | |
Taylor Brandstetter | 569f4e7 | 2017-10-13 12:53:51 -0700 | [diff] [blame] | 54 | // 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.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 60 | // 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 Brandstetter | 569f4e7 | 2017-10-13 12:53:51 -0700 | [diff] [blame] | 67 | TEST_F(WindowCapturerTest, MAYBE_Capture) { |
zijiehe | fce4905 | 2016-11-07 15:25:18 -0800 | [diff] [blame] | 68 | DesktopCapturer::SourceList sources; |
sergeyu@chromium.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 69 | capturer_->Start(this); |
zijiehe | fce4905 | 2016-11-07 15:25:18 -0800 | [diff] [blame] | 70 | EXPECT_TRUE(capturer_->GetSourceList(&sources)); |
sergeyu@chromium.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 71 | |
| 72 | // Verify that we can select and capture each window. |
zijiehe | fce4905 | 2016-11-07 15:25:18 -0800 | [diff] [blame] | 73 | for (auto it = sources.begin(); it != sources.end(); ++it) { |
sergeyu@chromium.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 74 | frame_.reset(); |
zijiehe | fce4905 | 2016-11-07 15:25:18 -0800 | [diff] [blame] | 75 | if (capturer_->SelectSource(it->id)) { |
zijiehe | 249beee | 2016-10-18 23:13:29 -0700 | [diff] [blame] | 76 | capturer_->CaptureFrame(); |
sergeyu@chromium.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // If we failed to capture a window make sure it no longer exists. |
| 80 | if (!frame_.get()) { |
zijiehe | fce4905 | 2016-11-07 15:25:18 -0800 | [diff] [blame] | 81 | 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.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 85 | 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.org | b10ccbe | 2013-05-19 07:02:48 +0000 | [diff] [blame] | 95 | } // namespace webrtc |