1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include <input/DisplayViewport.h>
20 #include <input/TouchVideoFrame.h>
21
22 namespace android {
23 namespace test {
24
25 static const struct timeval TIMESTAMP = {1, 2};
26
TEST(TouchVideoFrame,Constructor)27 TEST(TouchVideoFrame, Constructor) {
28 const std::vector<int16_t> data = {1, 2, 3, 4, 5, 6};
29 constexpr uint32_t height = 3;
30 constexpr uint32_t width = 2;
31
32 TouchVideoFrame frame(height, width, data, TIMESTAMP);
33
34 ASSERT_EQ(data, frame.getData());
35 ASSERT_EQ(height, frame.getHeight());
36 ASSERT_EQ(width, frame.getWidth());
37 ASSERT_EQ(TIMESTAMP.tv_sec, frame.getTimestamp().tv_sec);
38 ASSERT_EQ(TIMESTAMP.tv_usec, frame.getTimestamp().tv_usec);
39 }
40
TEST(TouchVideoFrame,Equality)41 TEST(TouchVideoFrame, Equality) {
42 const std::vector<int16_t> data = {1, 2, 3, 4, 5, 6};
43 constexpr uint32_t height = 3;
44 constexpr uint32_t width = 2;
45 TouchVideoFrame frame(height, width, data, TIMESTAMP);
46
47 TouchVideoFrame identicalFrame(height, width, data, TIMESTAMP);
48 ASSERT_EQ(frame, identicalFrame);
49
50 // The two cases below create an invalid frame, but it is OK for comparison purposes.
51 // There aren't any checks currently enforced on the frame dimensions and data
52 // Change height
53 TouchVideoFrame changedHeightFrame(height + 1, width, data, TIMESTAMP);
54 ASSERT_FALSE(frame == changedHeightFrame);
55
56 // Change width
57 TouchVideoFrame changedWidthFrame(height, width + 1, data, TIMESTAMP);
58 ASSERT_FALSE(frame == changedWidthFrame);
59
60 // Change data
61 const std::vector<int16_t> differentData = {1, 2, 3, 3, 5, 6};
62 TouchVideoFrame changedDataFrame(height, width, differentData, TIMESTAMP);
63 ASSERT_FALSE(frame == changedDataFrame);
64
65 // Change timestamp
66 const struct timeval differentTimestamp = {TIMESTAMP.tv_sec + 1, TIMESTAMP.tv_usec + 1};
67 TouchVideoFrame changedTimestampFrame(height, width, data, differentTimestamp);
68 ASSERT_FALSE(frame == changedTimestampFrame);
69 }
70
71 // --- Rotate 90 degrees ---
72
TEST(TouchVideoFrame,Rotate90_0x0)73 TEST(TouchVideoFrame, Rotate90_0x0) {
74 TouchVideoFrame frame(0, 0, {}, TIMESTAMP);
75 TouchVideoFrame frameRotated(0, 0, {}, TIMESTAMP);
76 frame.rotate(ui::ROTATION_90);
77 ASSERT_EQ(frame, frameRotated);
78 }
79
TEST(TouchVideoFrame,Rotate90_1x1)80 TEST(TouchVideoFrame, Rotate90_1x1) {
81 TouchVideoFrame frame(1, 1, {1}, TIMESTAMP);
82 TouchVideoFrame frameRotated(1, 1, {1}, TIMESTAMP);
83 frame.rotate(ui::ROTATION_90);
84 ASSERT_EQ(frame, frameRotated);
85 }
86
TEST(TouchVideoFrame,Rotate90_2x2)87 TEST(TouchVideoFrame, Rotate90_2x2) {
88 TouchVideoFrame frame(2, 2, {1, 2, 3, 4}, TIMESTAMP);
89 TouchVideoFrame frameRotated(2, 2, {2, 4, 1, 3}, TIMESTAMP);
90 frame.rotate(ui::ROTATION_90);
91 ASSERT_EQ(frame, frameRotated);
92 }
93
TEST(TouchVideoFrame,Rotate90_3x2)94 TEST(TouchVideoFrame, Rotate90_3x2) {
95 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, TIMESTAMP);
96 TouchVideoFrame frameRotated(2, 3, {2, 4, 6, 1, 3, 5}, TIMESTAMP);
97 frame.rotate(ui::ROTATION_90);
98 ASSERT_EQ(frame, frameRotated);
99 }
100
TEST(TouchVideoFrame,Rotate90_3x2_4times)101 TEST(TouchVideoFrame, Rotate90_3x2_4times) {
102 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, TIMESTAMP);
103 TouchVideoFrame frameOriginal(3, 2, {1, 2, 3, 4, 5, 6}, TIMESTAMP);
104 frame.rotate(ui::ROTATION_90);
105 frame.rotate(ui::ROTATION_90);
106 frame.rotate(ui::ROTATION_90);
107 frame.rotate(ui::ROTATION_90);
108 ASSERT_EQ(frame, frameOriginal);
109 }
110
111 // --- Rotate 180 degrees ---
112
TEST(TouchVideoFrame,Rotate180_0x0)113 TEST(TouchVideoFrame, Rotate180_0x0) {
114 TouchVideoFrame frame(0, 0, {}, TIMESTAMP);
115 TouchVideoFrame frameRotated(0, 0, {}, TIMESTAMP);
116 frame.rotate(ui::ROTATION_180);
117 ASSERT_EQ(frame, frameRotated);
118 }
119
TEST(TouchVideoFrame,Rotate180_1x1)120 TEST(TouchVideoFrame, Rotate180_1x1) {
121 TouchVideoFrame frame(1, 1, {1}, TIMESTAMP);
122 TouchVideoFrame frameRotated(1, 1, {1}, TIMESTAMP);
123 frame.rotate(ui::ROTATION_180);
124 ASSERT_EQ(frame, frameRotated);
125 }
126
TEST(TouchVideoFrame,Rotate180_2x2)127 TEST(TouchVideoFrame, Rotate180_2x2) {
128 TouchVideoFrame frame(2, 2, {1, 2, 3, 4}, TIMESTAMP);
129 TouchVideoFrame frameRotated(2, 2, {4, 3, 2, 1}, TIMESTAMP);
130 frame.rotate(ui::ROTATION_180);
131 ASSERT_EQ(frame, frameRotated);
132 }
133
TEST(TouchVideoFrame,Rotate180_3x2)134 TEST(TouchVideoFrame, Rotate180_3x2) {
135 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, TIMESTAMP);
136 TouchVideoFrame frameRotated(3, 2, {6, 5, 4, 3, 2, 1}, TIMESTAMP);
137 frame.rotate(ui::ROTATION_180);
138 ASSERT_EQ(frame, frameRotated);
139 }
140
TEST(TouchVideoFrame,Rotate180_3x2_2times)141 TEST(TouchVideoFrame, Rotate180_3x2_2times) {
142 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, TIMESTAMP);
143 TouchVideoFrame frameOriginal(3, 2, {1, 2, 3, 4, 5, 6}, TIMESTAMP);
144 frame.rotate(ui::ROTATION_180);
145 frame.rotate(ui::ROTATION_180);
146 ASSERT_EQ(frame, frameOriginal);
147 }
148
TEST(TouchVideoFrame,Rotate180_3x3)149 TEST(TouchVideoFrame, Rotate180_3x3) {
150 TouchVideoFrame frame(3, 3, {1, 2, 3, 4, 5, 6, 7, 8, 9}, TIMESTAMP);
151 TouchVideoFrame frameRotated(3, 3, {9, 8, 7, 6, 5, 4, 3, 2, 1}, TIMESTAMP);
152 frame.rotate(ui::ROTATION_180);
153 ASSERT_EQ(frame, frameRotated);
154 }
155
156 // --- Rotate 270 degrees ---
157
TEST(TouchVideoFrame,Rotate270_0x0)158 TEST(TouchVideoFrame, Rotate270_0x0) {
159 TouchVideoFrame frame(0, 0, {}, TIMESTAMP);
160 TouchVideoFrame frameRotated(0, 0, {}, TIMESTAMP);
161 frame.rotate(ui::ROTATION_270);
162 ASSERT_EQ(frame, frameRotated);
163 }
164
TEST(TouchVideoFrame,Rotate270_1x1)165 TEST(TouchVideoFrame, Rotate270_1x1) {
166 TouchVideoFrame frame(1, 1, {1}, TIMESTAMP);
167 TouchVideoFrame frameRotated(1, 1, {1}, TIMESTAMP);
168 frame.rotate(ui::ROTATION_270);
169 ASSERT_EQ(frame, frameRotated);
170 }
171
TEST(TouchVideoFrame,Rotate270_2x2)172 TEST(TouchVideoFrame, Rotate270_2x2) {
173 TouchVideoFrame frame(2, 2, {1, 2, 3, 4}, TIMESTAMP);
174 TouchVideoFrame frameRotated(2, 2, {3, 1, 4, 2}, TIMESTAMP);
175 frame.rotate(ui::ROTATION_270);
176 ASSERT_EQ(frame, frameRotated);
177 }
178
TEST(TouchVideoFrame,Rotate270_3x2)179 TEST(TouchVideoFrame, Rotate270_3x2) {
180 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, TIMESTAMP);
181 TouchVideoFrame frameRotated(2, 3, {5, 3, 1, 6, 4, 2}, TIMESTAMP);
182 frame.rotate(ui::ROTATION_270);
183 ASSERT_EQ(frame, frameRotated);
184 }
185
TEST(TouchVideoFrame,Rotate270_3x2_4times)186 TEST(TouchVideoFrame, Rotate270_3x2_4times) {
187 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, TIMESTAMP);
188 TouchVideoFrame frameOriginal(3, 2, {1, 2, 3, 4, 5, 6}, TIMESTAMP);
189 frame.rotate(ui::ROTATION_270);
190 frame.rotate(ui::ROTATION_270);
191 frame.rotate(ui::ROTATION_270);
192 frame.rotate(ui::ROTATION_270);
193 ASSERT_EQ(frame, frameOriginal);
194 }
195
196 } // namespace test
197 } // namespace android
198