blob: 36c51d75c9cd584c499d1b06f384c4b7608d1f16 [file] [log] [blame]
Zijie He05ec1782017-09-06 14:09:20 -07001/*
2 * Copyright (c) 2017 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 <memory>
12#include <utility>
13
14#include "webrtc/modules/desktop_capture/cropped_desktop_frame.h"
15#include "webrtc/modules/desktop_capture/desktop_frame.h"
16#include "webrtc/rtc_base/ptr_util.h"
17#include "webrtc/test/gtest.h"
18
19namespace webrtc {
20
21std::unique_ptr<DesktopFrame> CreateTestFrame() {
22 return rtc::MakeUnique<BasicDesktopFrame>(DesktopSize(10, 20));
23}
24
25TEST(CroppedDesktopFrameTest, DoNotCreateWrapperIfSizeIsNotChanged) {
26 std::unique_ptr<DesktopFrame> original = CreateTestFrame();
27 // owned by |original| and CroppedDesktopFrame.
28 DesktopFrame* raw_original = original.get();
29 std::unique_ptr<DesktopFrame> cropped = CreateCroppedDesktopFrame(
30 std::move(original), DesktopRect::MakeWH(10, 20));
31 ASSERT_EQ(cropped.get(), raw_original);
32}
33
34TEST(CroppedDesktopFrameTest, ReturnNullptrIfSizeIsNotSufficient) {
35 ASSERT_EQ(nullptr, CreateCroppedDesktopFrame(
36 CreateTestFrame(), DesktopRect::MakeWH(11, 10)));
37}
38
39TEST(CroppedDesktopFrameTest, ReturnNullIfCropRegionIsOutOfBounds) {
40 std::unique_ptr<DesktopFrame> frame = CreateTestFrame();
41 frame->set_top_left(DesktopVector(100, 200));
42 ASSERT_EQ(nullptr, CreateCroppedDesktopFrame(std::move(frame),
43 DesktopRect::MakeLTRB(101, 203, 109, 218)));
44}
45
46TEST(CroppedDesktopFrameTest, CropASubArea) {
47 std::unique_ptr<DesktopFrame> cropped = CreateCroppedDesktopFrame(
48 CreateTestFrame(), DesktopRect::MakeLTRB(1, 2, 9, 19));
49 ASSERT_EQ(cropped->size().width(), 8);
50 ASSERT_EQ(cropped->size().height(), 17);
51 ASSERT_EQ(cropped->top_left().x(), 1);
52 ASSERT_EQ(cropped->top_left().y(), 2);
53}
54
55TEST(CroppedDesktopFrameTest, SetTopLeft) {
56 std::unique_ptr<DesktopFrame> frame = CreateTestFrame();
57 frame->set_top_left(DesktopVector(100, 200));
58 frame = CreateCroppedDesktopFrame(std::move(frame),
59 DesktopRect::MakeLTRB(1, 3, 9, 18));
60 ASSERT_EQ(frame->size().width(), 8);
61 ASSERT_EQ(frame->size().height(), 15);
62 ASSERT_EQ(frame->top_left().x(), 101);
63 ASSERT_EQ(frame->top_left().y(), 203);
64}
65
66} // namespace webrtc