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