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 "webrtc/modules/desktop_capture/window_capturer.h"
12 
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webrtc/base/scoped_ptr.h"
15 #include "webrtc/modules/desktop_capture/desktop_capture_options.h"
16 #include "webrtc/modules/desktop_capture/desktop_frame.h"
17 #include "webrtc/modules/desktop_capture/desktop_region.h"
18 #include "webrtc/system_wrappers/include/logging.h"
19 
20 namespace webrtc {
21 
22 class WindowCapturerTest : public testing::Test,
23                            public DesktopCapturer::Callback {
24  public:
SetUp()25   void SetUp() override {
26     capturer_.reset(
27         WindowCapturer::Create(DesktopCaptureOptions::CreateDefault()));
28   }
29 
TearDown()30   void TearDown() override {}
31 
32   // DesktopCapturer::Callback interface
CreateSharedMemory(size_t size)33   SharedMemory* CreateSharedMemory(size_t size) override { return NULL; }
34 
OnCaptureCompleted(DesktopFrame * frame)35   void OnCaptureCompleted(DesktopFrame* frame) override { frame_.reset(frame); }
36 
37  protected:
38   rtc::scoped_ptr<WindowCapturer> capturer_;
39   rtc::scoped_ptr<DesktopFrame> frame_;
40 };
41 
42 // Verify that we can enumerate windows.
TEST_F(WindowCapturerTest,Enumerate)43 TEST_F(WindowCapturerTest, Enumerate) {
44   WindowCapturer::WindowList windows;
45   EXPECT_TRUE(capturer_->GetWindowList(&windows));
46 
47   // Verify that window titles are set.
48   for (WindowCapturer::WindowList::iterator it = windows.begin();
49        it != windows.end(); ++it) {
50     EXPECT_FALSE(it->title.empty());
51   }
52 }
53 
54 // Verify we can capture a window.
55 //
56 // TODO(sergeyu): Currently this test just looks at the windows that already
57 // exist. Ideally it should create a test window and capture from it, but there
58 // is no easy cross-platform way to create new windows (potentially we could
59 // have a python script showing Tk dialog, but launching code will differ
60 // between platforms).
TEST_F(WindowCapturerTest,Capture)61 TEST_F(WindowCapturerTest, Capture) {
62   WindowCapturer::WindowList windows;
63   capturer_->Start(this);
64   EXPECT_TRUE(capturer_->GetWindowList(&windows));
65 
66   // Verify that we can select and capture each window.
67   for (WindowCapturer::WindowList::iterator it = windows.begin();
68        it != windows.end(); ++it) {
69     frame_.reset();
70     if (capturer_->SelectWindow(it->id)) {
71       capturer_->Capture(DesktopRegion());
72     }
73 
74     // If we failed to capture a window make sure it no longer exists.
75     if (!frame_.get()) {
76       WindowCapturer::WindowList new_list;
77       EXPECT_TRUE(capturer_->GetWindowList(&new_list));
78       for (WindowCapturer::WindowList::iterator new_list_it = new_list.begin();
79            new_list_it != new_list.end(); ++new_list_it) {
80         EXPECT_FALSE(it->id == new_list_it->id);
81       }
82       continue;
83     }
84 
85     EXPECT_GT(frame_->size().width(), 0);
86     EXPECT_GT(frame_->size().height(), 0);
87   }
88 }
89 
90 }  // namespace webrtc
91