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