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