1 /*
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 "modules/desktop_capture/cropped_desktop_frame.h"
12
13 #include <memory>
14 #include <utility>
15
16 #include "modules/desktop_capture/desktop_frame.h"
17 #include "modules/desktop_capture/shared_desktop_frame.h"
18 #include "test/gtest.h"
19
20 namespace webrtc {
21
CreateTestFrame()22 std::unique_ptr<DesktopFrame> CreateTestFrame() {
23 return std::make_unique<BasicDesktopFrame>(DesktopSize(10, 20));
24 }
25
TEST(CroppedDesktopFrameTest,DoNotCreateWrapperIfSizeIsNotChanged)26 TEST(CroppedDesktopFrameTest, DoNotCreateWrapperIfSizeIsNotChanged) {
27 std::unique_ptr<DesktopFrame> original = CreateTestFrame();
28 // owned by `original` and CroppedDesktopFrame.
29 DesktopFrame* raw_original = original.get();
30 std::unique_ptr<DesktopFrame> cropped = CreateCroppedDesktopFrame(
31 std::move(original), DesktopRect::MakeWH(10, 20));
32 ASSERT_EQ(cropped.get(), raw_original);
33 }
34
TEST(CroppedDesktopFrameTest,CropWhenPartiallyOutOfBounds)35 TEST(CroppedDesktopFrameTest, CropWhenPartiallyOutOfBounds) {
36 std::unique_ptr<DesktopFrame> cropped =
37 CreateCroppedDesktopFrame(CreateTestFrame(), DesktopRect::MakeWH(11, 10));
38 ASSERT_NE(nullptr, cropped);
39 ASSERT_EQ(cropped->size().width(), 10);
40 ASSERT_EQ(cropped->size().height(), 10);
41 ASSERT_EQ(cropped->top_left().x(), 0);
42 ASSERT_EQ(cropped->top_left().y(), 0);
43 }
44
TEST(CroppedDesktopFrameTest,ReturnNullIfCropRegionIsOutOfBounds)45 TEST(CroppedDesktopFrameTest, ReturnNullIfCropRegionIsOutOfBounds) {
46 std::unique_ptr<DesktopFrame> frame = CreateTestFrame();
47 frame->set_top_left(DesktopVector(100, 200));
48 ASSERT_EQ(nullptr,
49 CreateCroppedDesktopFrame(
50 std::move(frame), DesktopRect::MakeLTRB(101, 203, 109, 218)));
51 }
52
TEST(CroppedDesktopFrameTest,CropASubArea)53 TEST(CroppedDesktopFrameTest, CropASubArea) {
54 std::unique_ptr<DesktopFrame> cropped = CreateCroppedDesktopFrame(
55 CreateTestFrame(), DesktopRect::MakeLTRB(1, 2, 9, 19));
56 ASSERT_EQ(cropped->size().width(), 8);
57 ASSERT_EQ(cropped->size().height(), 17);
58 ASSERT_EQ(cropped->top_left().x(), 1);
59 ASSERT_EQ(cropped->top_left().y(), 2);
60 }
61
TEST(CroppedDesktopFrameTest,SetTopLeft)62 TEST(CroppedDesktopFrameTest, SetTopLeft) {
63 std::unique_ptr<DesktopFrame> frame = CreateTestFrame();
64 frame->set_top_left(DesktopVector(100, 200));
65 frame = CreateCroppedDesktopFrame(std::move(frame),
66 DesktopRect::MakeLTRB(1, 3, 9, 18));
67 ASSERT_EQ(frame->size().width(), 8);
68 ASSERT_EQ(frame->size().height(), 15);
69 ASSERT_EQ(frame->top_left().x(), 101);
70 ASSERT_EQ(frame->top_left().y(), 203);
71 }
72
TEST(CroppedDesktopFrameTest,InitializedWithZeros)73 TEST(CroppedDesktopFrameTest, InitializedWithZeros) {
74 std::unique_ptr<DesktopFrame> frame = CreateTestFrame();
75 const DesktopVector frame_origin = frame->top_left();
76 const DesktopSize frame_size = frame->size();
77 std::unique_ptr<DesktopFrame> cropped = CreateCroppedDesktopFrame(
78 std::move(frame), DesktopRect::MakeOriginSize(frame_origin, frame_size));
79 for (int j = 0; j < cropped->size().height(); ++j) {
80 for (int i = 0; i < cropped->stride(); ++i) {
81 ASSERT_EQ(cropped->data()[i + j * cropped->stride()], 0);
82 }
83 }
84 }
85
TEST(CroppedDesktopFrameTest,IccProfile)86 TEST(CroppedDesktopFrameTest, IccProfile) {
87 const uint8_t fake_icc_profile_data_array[] = {0x1a, 0x00, 0x2b, 0x00,
88 0x3c, 0x00, 0x4d};
89 const std::vector<uint8_t> icc_profile(
90 fake_icc_profile_data_array,
91 fake_icc_profile_data_array + sizeof(fake_icc_profile_data_array));
92
93 std::unique_ptr<DesktopFrame> frame = CreateTestFrame();
94 EXPECT_EQ(frame->icc_profile().size(), 0UL);
95
96 frame->set_icc_profile(icc_profile);
97 EXPECT_EQ(frame->icc_profile().size(), 7UL);
98 EXPECT_EQ(frame->icc_profile(), icc_profile);
99
100 frame = CreateCroppedDesktopFrame(std::move(frame),
101 DesktopRect::MakeLTRB(2, 2, 8, 18));
102 EXPECT_EQ(frame->icc_profile().size(), 7UL);
103 EXPECT_EQ(frame->icc_profile(), icc_profile);
104
105 std::unique_ptr<SharedDesktopFrame> shared =
106 SharedDesktopFrame::Wrap(std::move(frame));
107 EXPECT_EQ(shared->icc_profile().size(), 7UL);
108 EXPECT_EQ(shared->icc_profile(), icc_profile);
109
110 std::unique_ptr<DesktopFrame> shared_other = shared->Share();
111 EXPECT_EQ(shared_other->icc_profile().size(), 7UL);
112 EXPECT_EQ(shared_other->icc_profile(), icc_profile);
113 }
114
115 } // namespace webrtc
116