1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "remoting/base/util.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
11 static const int kWidth
= 32 ;
12 static const int kHeight
= 24 ;
13 static const int kBytesPerPixel
= 4;
14 static const int kYStride
= kWidth
;
15 static const int kUvStride
= kWidth
/ 2;
16 static const int kRgbStride
= kWidth
* kBytesPerPixel
;
17 static const uint32 kFillColor
= 0xffffff;
21 class YuvToRgbTester
{
24 yuv_buffer_size_
= (kYStride
+ kUvStride
) * kHeight
;
25 yuv_buffer_
.reset(new uint8
[yuv_buffer_size_
]);
26 yplane_
= yuv_buffer_
.get();
27 uplane_
= yplane_
+ (kYStride
* kHeight
);
28 vplane_
= uplane_
+ (kUvStride
* kHeight
/ 2);
30 rgb_buffer_size_
= kWidth
* kHeight
* kBytesPerPixel
;
31 rgb_buffer_
.reset(new uint8
[rgb_buffer_size_
]);
39 void ResetYuvBuffer() {
40 memset(yuv_buffer_
.get(), 0, yuv_buffer_size_
);
43 void ResetRgbBuffer() {
44 memset(rgb_buffer_
.get(), 0, rgb_buffer_size_
);
47 void FillRgbBuffer(const webrtc::DesktopRect
& rect
) {
48 uint32
* ptr
= reinterpret_cast<uint32
*>(
49 rgb_buffer_
.get() + (rect
.top() * kRgbStride
) +
50 (rect
.left() * kBytesPerPixel
));
51 int width
= rect
.width();
52 for (int height
= rect
.height(); height
> 0; --height
) {
53 std::fill(ptr
, ptr
+ width
, kFillColor
);
54 ptr
+= kRgbStride
/ kBytesPerPixel
;
58 // Check the the desination buffer is filled within expected bounds.
59 void CheckRgbBuffer(const webrtc::DesktopRect
& rect
) {
60 uint32
* ptr
= reinterpret_cast<uint32
*>(rgb_buffer_
.get());
61 for (int y
= 0; y
< kHeight
; ++y
) {
62 if (y
< rect
.top() || rect
.bottom() <= y
) {
63 // The whole line should be intact.
64 EXPECT_EQ((ptrdiff_t)kWidth
,
65 std::count(ptr
, ptr
+ kWidth
, 0u));
67 // The space before the painted rectangle should be intact.
68 EXPECT_EQ((ptrdiff_t)rect
.left(),
69 std::count(ptr
, ptr
+ rect
.left(), 0u));
71 // All pixels of the target rectangle should be touched.
72 EXPECT_EQ(ptr
+ rect
.right(),
73 std::find(ptr
+ rect
.left(), ptr
+ rect
.right(), 0u));
75 // The space after the painted rectangle should be intact.
76 EXPECT_EQ((ptrdiff_t)kWidth
- rect
.right(),
77 std::count(ptr
+ rect
.right(), ptr
+ kWidth
, 0u));
79 ptr
+= kRgbStride
/ kBytesPerPixel
;
83 void RunTest(const webrtc::DesktopSize dest_size
,
84 const webrtc::DesktopRect
& rect
) {
86 DoesRectContain(webrtc::DesktopRect::MakeSize(dest_size
), rect
));
94 ConvertRGB32ToYUVWithRect(rgb_buffer_
.get(),
106 // Reset RGB buffer and do opposite conversion.
108 ConvertAndScaleYUVToRGB32Rect(yplane_
,
113 webrtc::DesktopSize(kWidth
, kHeight
),
114 webrtc::DesktopRect::MakeWH(kWidth
, kHeight
),
118 webrtc::DesktopRect::MakeSize(dest_size
),
121 // Check if it worked out.
122 CheckRgbBuffer(rect
);
125 void TestBasicConversion() {
127 RunTest(webrtc::DesktopSize(kWidth
, kHeight
),
128 webrtc::DesktopRect::MakeWH(kWidth
, kHeight
));
132 size_t yuv_buffer_size_
;
133 scoped_ptr
<uint8
[]> yuv_buffer_
;
138 size_t rgb_buffer_size_
;
139 scoped_ptr
<uint8
[]> rgb_buffer_
;
141 DISALLOW_COPY_AND_ASSIGN(YuvToRgbTester
);
144 TEST(YuvToRgbTest
, BasicConversion
) {
145 YuvToRgbTester tester
;
146 tester
.TestBasicConversion();
149 TEST(YuvToRgbTest
, Clipping
) {
150 YuvToRgbTester tester
;
152 webrtc::DesktopSize dest_size
= webrtc::DesktopSize(kWidth
, kHeight
);
153 webrtc::DesktopRect rect
=
154 webrtc::DesktopRect::MakeLTRB(0, 0, kWidth
- 1, kHeight
- 1);
155 for (int i
= 0; i
< 16; ++i
) {
156 webrtc::DesktopRect dest_rect
= webrtc::DesktopRect::MakeLTRB(
157 rect
.left() + ((i
& 1) ? 1 : 0),
158 rect
.top() + ((i
& 2) ? 1 : 0),
159 rect
.right() + ((i
& 4) ? 1 : 0),
160 rect
.bottom() + ((i
& 8) ? 1 : 0));
162 tester
.RunTest(dest_size
, dest_rect
);
166 TEST(YuvToRgbTest
, ClippingAndScaling
) {
167 YuvToRgbTester tester
;
169 webrtc::DesktopSize dest_size
=
170 webrtc::DesktopSize(kWidth
- 10, kHeight
- 10);
171 webrtc::DesktopRect rect
=
172 webrtc::DesktopRect::MakeLTRB(5, 5, kWidth
- 11, kHeight
- 11);
173 for (int i
= 0; i
< 16; ++i
) {
174 webrtc::DesktopRect dest_rect
= webrtc::DesktopRect::MakeLTRB(
175 rect
.left() + ((i
& 1) ? 1 : 0),
176 rect
.top() + ((i
& 2) ? 1 : 0),
177 rect
.right() + ((i
& 4) ? 1 : 0),
178 rect
.bottom() + ((i
& 8) ? 1 : 0));
180 tester
.RunTest(dest_size
, dest_rect
);
184 TEST(ReplaceLfByCrLfTest
, Basic
) {
185 EXPECT_EQ("ab", ReplaceLfByCrLf("ab"));
186 EXPECT_EQ("\r\nab", ReplaceLfByCrLf("\nab"));
187 EXPECT_EQ("\r\nab\r\n", ReplaceLfByCrLf("\nab\n"));
188 EXPECT_EQ("\r\nab\r\ncd", ReplaceLfByCrLf("\nab\ncd"));
189 EXPECT_EQ("\r\nab\r\ncd\r\n", ReplaceLfByCrLf("\nab\ncd\n"));
190 EXPECT_EQ("\r\n\r\nab\r\n\r\ncd\r\n\r\n",
191 ReplaceLfByCrLf("\n\nab\n\ncd\n\n"));
194 TEST(ReplaceLfByCrLfTest
, Speed
) {
196 std::string
line(kLineSize
, 'a');
197 line
[kLineSize
- 1] = '\n';
198 // Make a 10M string.
199 int kLineNum
= 10 * 1024 * 1024 / kLineSize
;
201 buffer
.resize(kLineNum
* kLineSize
);
202 for (int i
= 0; i
< kLineNum
; ++i
) {
203 memcpy(&buffer
[i
* kLineSize
], &line
[0], kLineSize
);
205 // Convert the string.
206 buffer
= ReplaceLfByCrLf(buffer
);
207 // Check the converted string.
208 EXPECT_EQ(static_cast<size_t>((kLineSize
+ 1) * kLineNum
), buffer
.size());
209 const char* p
= &buffer
[0];
210 for (int i
= 0; i
< kLineNum
; ++i
) {
211 EXPECT_EQ(0, memcmp(&line
[0], p
, kLineSize
- 1));
213 EXPECT_EQ('\r', *p
++);
214 EXPECT_EQ('\n', *p
++);
218 TEST(ReplaceCrLfByLfTest
, Basic
) {
219 EXPECT_EQ("ab", ReplaceCrLfByLf("ab"));
220 EXPECT_EQ("\nab", ReplaceCrLfByLf("\r\nab"));
221 EXPECT_EQ("\nab\n", ReplaceCrLfByLf("\r\nab\r\n"));
222 EXPECT_EQ("\nab\ncd", ReplaceCrLfByLf("\r\nab\r\ncd"));
223 EXPECT_EQ("\nab\ncd\n", ReplaceCrLfByLf("\r\nab\r\ncd\n"));
224 EXPECT_EQ("\n\nab\n\ncd\n\n",
225 ReplaceCrLfByLf("\r\n\r\nab\r\n\r\ncd\r\n\r\n"));
226 EXPECT_EQ("\rab\rcd\r", ReplaceCrLfByLf("\rab\rcd\r"));
229 TEST(ReplaceCrLfByLfTest
, Speed
) {
231 std::string
line(kLineSize
, 'a');
232 line
[kLineSize
- 2] = '\r';
233 line
[kLineSize
- 1] = '\n';
234 // Make a 10M string.
235 int kLineNum
= 10 * 1024 * 1024 / kLineSize
;
237 buffer
.resize(kLineNum
* kLineSize
);
238 for (int i
= 0; i
< kLineNum
; ++i
) {
239 memcpy(&buffer
[i
* kLineSize
], &line
[0], kLineSize
);
241 // Convert the string.
242 buffer
= ReplaceCrLfByLf(buffer
);
243 // Check the converted string.
244 EXPECT_EQ(static_cast<size_t>((kLineSize
- 1) * kLineNum
), buffer
.size());
245 const char* p
= &buffer
[0];
246 for (int i
= 0; i
< kLineNum
; ++i
) {
247 EXPECT_EQ(0, memcmp(&line
[0], p
, kLineSize
- 2));
249 EXPECT_EQ('\n', *p
++);
253 TEST(StringIsUtf8Test
, Basic
) {
254 EXPECT_TRUE(StringIsUtf8("", 0));
255 EXPECT_TRUE(StringIsUtf8("\0", 1));
256 EXPECT_TRUE(StringIsUtf8("abc", 3));
257 EXPECT_TRUE(StringIsUtf8("\xc0\x80", 2));
258 EXPECT_TRUE(StringIsUtf8("\xe0\x80\x80", 3));
259 EXPECT_TRUE(StringIsUtf8("\xf0\x80\x80\x80", 4));
260 EXPECT_TRUE(StringIsUtf8("\xf8\x80\x80\x80\x80", 5));
261 EXPECT_TRUE(StringIsUtf8("\xfc\x80\x80\x80\x80\x80", 6));
263 // Not enough continuation characters
264 EXPECT_FALSE(StringIsUtf8("\xc0", 1));
265 EXPECT_FALSE(StringIsUtf8("\xe0\x80", 2));
266 EXPECT_FALSE(StringIsUtf8("\xf0\x80\x80", 3));
267 EXPECT_FALSE(StringIsUtf8("\xf8\x80\x80\x80", 4));
268 EXPECT_FALSE(StringIsUtf8("\xfc\x80\x80\x80\x80", 5));
270 // One more continuation character than needed
271 EXPECT_FALSE(StringIsUtf8("\xc0\x80\x80", 3));
272 EXPECT_FALSE(StringIsUtf8("\xe0\x80\x80\x80", 4));
273 EXPECT_FALSE(StringIsUtf8("\xf0\x80\x80\x80\x80", 5));
274 EXPECT_FALSE(StringIsUtf8("\xf8\x80\x80\x80\x80\x80", 6));
275 EXPECT_FALSE(StringIsUtf8("\xfc\x80\x80\x80\x80\x80\x80", 7));
277 // Invalid first byte
278 EXPECT_FALSE(StringIsUtf8("\xfe\x80\x80\x80\x80\x80\x80", 7));
279 EXPECT_FALSE(StringIsUtf8("\xff\x80\x80\x80\x80\x80\x80", 7));
281 // Invalid continuation byte
282 EXPECT_FALSE(StringIsUtf8("\xc0\x00", 2));
283 EXPECT_FALSE(StringIsUtf8("\xc0\x40", 2));
284 EXPECT_FALSE(StringIsUtf8("\xc0\xc0", 2));
287 } // namespace remoting