1 /*
2  *  Copyright (c) 2016 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/test_utils.h"
12 
13 #include <stdint.h>
14 
15 #include "modules/desktop_capture/desktop_geometry.h"
16 #include "modules/desktop_capture/rgba_color.h"
17 #include "rtc_base/checks.h"
18 #include "test/gtest.h"
19 
20 namespace webrtc {
21 
22 namespace {
23 
PaintDesktopFrame(DesktopFrame * frame,DesktopVector pos,RgbaColor color)24 void PaintDesktopFrame(DesktopFrame* frame,
25                        DesktopVector pos,
26                        RgbaColor color) {
27   ASSERT_TRUE(frame);
28   ASSERT_TRUE(DesktopRect::MakeSize(frame->size()).Contains(pos));
29   *reinterpret_cast<uint32_t*>(frame->GetFrameDataAtPos(pos)) =
30       color.ToUInt32();
31 }
32 
33 // A DesktopFrame implementation to store data in heap, but the stide is
34 // doubled.
35 class DoubleSizeDesktopFrame : public DesktopFrame {
36  public:
37   explicit DoubleSizeDesktopFrame(DesktopSize size);
38   ~DoubleSizeDesktopFrame() override;
39 };
40 
DoubleSizeDesktopFrame(DesktopSize size)41 DoubleSizeDesktopFrame::DoubleSizeDesktopFrame(DesktopSize size)
42     : DesktopFrame(
43           size,
44           kBytesPerPixel * size.width() * 2,
45           new uint8_t[kBytesPerPixel * size.width() * size.height() * 2],
46           nullptr) {}
47 
~DoubleSizeDesktopFrame()48 DoubleSizeDesktopFrame::~DoubleSizeDesktopFrame() {
49   delete[] data_;
50 }
51 
52 }  // namespace
53 
TEST(TestUtilsTest,BasicDataEqualsCases)54 TEST(TestUtilsTest, BasicDataEqualsCases) {
55   BasicDesktopFrame frame(DesktopSize(4, 4));
56   for (int i = 0; i < 4; i++) {
57     for (int j = 0; j < 4; j++) {
58       PaintDesktopFrame(&frame, DesktopVector(i, j), RgbaColor(4U * j + i));
59     }
60   }
61 
62   ASSERT_TRUE(DesktopFrameDataEquals(frame, frame));
63   BasicDesktopFrame other(DesktopSize(4, 4));
64   for (int i = 0; i < 4; i++) {
65     for (int j = 0; j < 4; j++) {
66       PaintDesktopFrame(&other, DesktopVector(i, j), RgbaColor(4U * j + i));
67     }
68   }
69   ASSERT_TRUE(DesktopFrameDataEquals(frame, other));
70   PaintDesktopFrame(&other, DesktopVector(2, 2), RgbaColor(0U));
71   ASSERT_FALSE(DesktopFrameDataEquals(frame, other));
72 }
73 
TEST(TestUtilsTest,DifferentSizeShouldNotEqual)74 TEST(TestUtilsTest, DifferentSizeShouldNotEqual) {
75   BasicDesktopFrame frame(DesktopSize(4, 4));
76   for (int i = 0; i < 4; i++) {
77     for (int j = 0; j < 4; j++) {
78       PaintDesktopFrame(&frame, DesktopVector(i, j), RgbaColor(4U * j + i));
79     }
80   }
81 
82   BasicDesktopFrame other(DesktopSize(2, 8));
83   for (int i = 0; i < 2; i++) {
84     for (int j = 0; j < 8; j++) {
85       PaintDesktopFrame(&other, DesktopVector(i, j), RgbaColor(2U * j + i));
86     }
87   }
88 
89   ASSERT_FALSE(DesktopFrameDataEquals(frame, other));
90 }
91 
TEST(TestUtilsTest,DifferentStrideShouldBeComparable)92 TEST(TestUtilsTest, DifferentStrideShouldBeComparable) {
93   BasicDesktopFrame frame(DesktopSize(4, 4));
94   for (int i = 0; i < 4; i++) {
95     for (int j = 0; j < 4; j++) {
96       PaintDesktopFrame(&frame, DesktopVector(i, j), RgbaColor(4U * j + i));
97     }
98   }
99 
100   ASSERT_TRUE(DesktopFrameDataEquals(frame, frame));
101   DoubleSizeDesktopFrame other(DesktopSize(4, 4));
102   for (int i = 0; i < 4; i++) {
103     for (int j = 0; j < 4; j++) {
104       PaintDesktopFrame(&other, DesktopVector(i, j), RgbaColor(4U * j + i));
105     }
106   }
107   ASSERT_TRUE(DesktopFrameDataEquals(frame, other));
108 }
109 
110 }  // namespace webrtc
111