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