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,ReturnNullptrIfSizeIsNotSufficient)35 TEST(CroppedDesktopFrameTest, ReturnNullptrIfSizeIsNotSufficient) {
36 ASSERT_EQ(nullptr, CreateCroppedDesktopFrame(CreateTestFrame(),
37 DesktopRect::MakeWH(11, 10)));
38 }
39
TEST(CroppedDesktopFrameTest,ReturnNullIfCropRegionIsOutOfBounds)40 TEST(CroppedDesktopFrameTest, ReturnNullIfCropRegionIsOutOfBounds) {
41 std::unique_ptr<DesktopFrame> frame = CreateTestFrame();
42 frame->set_top_left(DesktopVector(100, 200));
43 ASSERT_EQ(nullptr,
44 CreateCroppedDesktopFrame(
45 std::move(frame), DesktopRect::MakeLTRB(101, 203, 109, 218)));
46 }
47
TEST(CroppedDesktopFrameTest,CropASubArea)48 TEST(CroppedDesktopFrameTest, CropASubArea) {
49 std::unique_ptr<DesktopFrame> cropped = CreateCroppedDesktopFrame(
50 CreateTestFrame(), DesktopRect::MakeLTRB(1, 2, 9, 19));
51 ASSERT_EQ(cropped->size().width(), 8);
52 ASSERT_EQ(cropped->size().height(), 17);
53 ASSERT_EQ(cropped->top_left().x(), 1);
54 ASSERT_EQ(cropped->top_left().y(), 2);
55 }
56
TEST(CroppedDesktopFrameTest,SetTopLeft)57 TEST(CroppedDesktopFrameTest, SetTopLeft) {
58 std::unique_ptr<DesktopFrame> frame = CreateTestFrame();
59 frame->set_top_left(DesktopVector(100, 200));
60 frame = CreateCroppedDesktopFrame(std::move(frame),
61 DesktopRect::MakeLTRB(1, 3, 9, 18));
62 ASSERT_EQ(frame->size().width(), 8);
63 ASSERT_EQ(frame->size().height(), 15);
64 ASSERT_EQ(frame->top_left().x(), 101);
65 ASSERT_EQ(frame->top_left().y(), 203);
66 }
67
TEST(CroppedDesktopFrameTest,InitializedWithZeros)68 TEST(CroppedDesktopFrameTest, InitializedWithZeros) {
69 std::unique_ptr<DesktopFrame> frame = CreateTestFrame();
70 const DesktopVector frame_origin = frame->top_left();
71 const DesktopSize frame_size = frame->size();
72 std::unique_ptr<DesktopFrame> cropped = CreateCroppedDesktopFrame(
73 std::move(frame), DesktopRect::MakeOriginSize(frame_origin, frame_size));
74 for (int j = 0; j < cropped->size().height(); ++j) {
75 for (int i = 0; i < cropped->stride(); ++i) {
76 ASSERT_EQ(cropped->data()[i + j * cropped->stride()], 0);
77 }
78 }
79 }
80
TEST(CroppedDesktopFrameTest,IccProfile)81 TEST(CroppedDesktopFrameTest, IccProfile) {
82 const uint8_t fake_icc_profile_data_array[] = {0x1a, 0x00, 0x2b, 0x00,
83 0x3c, 0x00, 0x4d};
84 const std::vector<uint8_t> icc_profile(
85 fake_icc_profile_data_array,
86 fake_icc_profile_data_array + sizeof(fake_icc_profile_data_array));
87
88 std::unique_ptr<DesktopFrame> frame = CreateTestFrame();
89 EXPECT_EQ(frame->icc_profile().size(), 0UL);
90
91 frame->set_icc_profile(icc_profile);
92 EXPECT_EQ(frame->icc_profile().size(), 7UL);
93 EXPECT_EQ(frame->icc_profile(), icc_profile);
94
95 frame = CreateCroppedDesktopFrame(std::move(frame),
96 DesktopRect::MakeLTRB(2, 2, 8, 18));
97 EXPECT_EQ(frame->icc_profile().size(), 7UL);
98 EXPECT_EQ(frame->icc_profile(), icc_profile);
99
100 std::unique_ptr<SharedDesktopFrame> shared =
101 SharedDesktopFrame::Wrap(std::move(frame));
102 EXPECT_EQ(shared->icc_profile().size(), 7UL);
103 EXPECT_EQ(shared->icc_profile(), icc_profile);
104
105 std::unique_ptr<DesktopFrame> shared_other = shared->Share();
106 EXPECT_EQ(shared_other->icc_profile().size(), 7UL);
107 EXPECT_EQ(shared_other->icc_profile(), icc_profile);
108 }
109
110 } // namespace webrtc
111