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/screen_capturer.h"
12
13#include <ApplicationServices/ApplicationServices.h>
14
15#include <ostream>
16
17#include "testing/gtest/include/gtest/gtest.h"
18#include "webrtc/modules/desktop_capture/desktop_frame.h"
19#include "webrtc/modules/desktop_capture/desktop_geometry.h"
20#include "webrtc/modules/desktop_capture/desktop_region.h"
21#include "webrtc/modules/desktop_capture/mac/desktop_configuration.h"
22#include "webrtc/modules/desktop_capture/screen_capturer_mock_objects.h"
23#include "webrtc/system_wrappers/interface/scoped_ptr.h"
24
25using ::testing::_;
26using ::testing::AnyNumber;
27using ::testing::Return;
28
29namespace webrtc {
30
31class ScreenCapturerMacTest : public testing::Test {
32 public:
33  // Verifies that the whole screen is initially dirty.
34  void CaptureDoneCallback1(DesktopFrame* frame);
35
36  // Verifies that a rectangle explicitly marked as dirty is propagated
37  // correctly.
38  void CaptureDoneCallback2(DesktopFrame* frame);
39
40 protected:
41  virtual void SetUp() OVERRIDE {
42    capturer_.reset(ScreenCapturer::Create());
43  }
44
45  scoped_ptr<ScreenCapturer> capturer_;
46  MockScreenCapturerCallback callback_;
47};
48
49void ScreenCapturerMacTest::CaptureDoneCallback1(
50    DesktopFrame* frame) {
51  scoped_ptr<DesktopFrame> owned_frame(frame);
52
53  MacDesktopConfiguration config = MacDesktopConfiguration::GetCurrent(
54      MacDesktopConfiguration::BottomLeftOrigin);
55
56  // Verify that the region contains full frame.
57  DesktopRegion::Iterator it(frame->updated_region());
58  EXPECT_TRUE(!it.IsAtEnd() && it.rect().equals(config.pixel_bounds));
59}
60
61void ScreenCapturerMacTest::CaptureDoneCallback2(
62    DesktopFrame* frame) {
63  scoped_ptr<DesktopFrame> owned_frame(frame);
64
65  MacDesktopConfiguration config = MacDesktopConfiguration::GetCurrent(
66      MacDesktopConfiguration::BottomLeftOrigin);
67  int width = config.pixel_bounds.width();
68  int height = config.pixel_bounds.height();
69
70  EXPECT_EQ(width, frame->size().width());
71  EXPECT_EQ(height, frame->size().height());
72  EXPECT_TRUE(frame->data() != NULL);
73  // Depending on the capture method, the screen may be flipped or not, so
74  // the stride may be positive or negative.
75  EXPECT_EQ(static_cast<int>(sizeof(uint32_t) * width),
76            abs(frame->stride()));
77}
78
79TEST_F(ScreenCapturerMacTest, Capture) {
80  EXPECT_CALL(callback_, OnCaptureCompleted(_))
81      .Times(2)
82      .WillOnce(Invoke(this, &ScreenCapturerMacTest::CaptureDoneCallback1))
83      .WillOnce(Invoke(this, &ScreenCapturerMacTest::CaptureDoneCallback2));
84
85  EXPECT_CALL(callback_, CreateSharedMemory(_))
86      .Times(AnyNumber())
87      .WillRepeatedly(Return(static_cast<SharedMemory*>(NULL)));
88
89  SCOPED_TRACE("");
90  capturer_->Start(&callback_);
91
92  // Check that we get an initial full-screen updated.
93  capturer_->Capture(DesktopRegion());
94
95  // Check that subsequent dirty rects are propagated correctly.
96  capturer_->Capture(DesktopRegion());
97}
98
99}  // namespace webrtc
100