blob: 24d92b6e87a7d6e96db41fe47c1f84ae34356ff2 [file] [log] [blame]
zijiehe90ea7362016-11-22 17:17:09 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/desktop_capture/test_utils.h"
zijiehe90ea7362016-11-22 17:17:09 -080012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stdint.h>
14
15#include "modules/desktop_capture/desktop_geometry.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/desktop_capture/rgba_color.h"
17#include "rtc_base/checks.h"
18#include "test/gtest.h"
zijiehe90ea7362016-11-22 17:17:09 -080019
20namespace webrtc {
21
22namespace {
23
24void PaintDesktopFrame(DesktopFrame* frame,
25 DesktopVector pos,
26 RgbaColor color) {
27 RTC_DCHECK(frame);
28 RTC_DCHECK(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.
35class DoubleSizeDesktopFrame : public DesktopFrame {
36 public:
37 explicit DoubleSizeDesktopFrame(DesktopSize size);
38 ~DoubleSizeDesktopFrame() override;
39};
40
41DoubleSizeDesktopFrame::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
48DoubleSizeDesktopFrame::~DoubleSizeDesktopFrame() {
49 delete[] data_;
50}
51
52} // namespace
53
54TEST(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
74TEST(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
92TEST(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