Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(577)

Side by Side Diff: third_party/WebKit/Source/platform/exported/WebStringTest.cpp

Issue 2629573002: Allow WebString.utf8() to take UTF8ConversionMode (Closed)
Patch Set: comment fix Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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.
4
5 #include "public/platform/WebString.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "wtf/text/WTFString.h"
9
10 namespace blink {
11
12 TEST(WebStringTest, UTF8ConversionRoundTrip) {
13 // Valid characters.
14 for (UChar uchar = 0; uchar <= 0xD7FF; ++uchar) {
15 WebString utf16String(&uchar, 1);
16 std::string utf8String(utf16String.utf8());
17 WebString utf16NewString =
18 WebString::fromUTF8(utf8String.data(), utf8String.length());
19 EXPECT_FALSE(utf16NewString.isNull());
20 EXPECT_TRUE(utf16String == utf16NewString);
21 }
22
23 // Unpaired surrogates.
24 for (UChar uchar = 0xD800; uchar <= 0xDFFF; ++uchar) {
25 WebString utf16String(&uchar, 1);
26
27 // Conversion with Strict mode results in an empty string.
28 std::string utf8String(
29 utf16String.utf8(WebString::UTF8ConversionMode::kStrict));
30 EXPECT_TRUE(utf8String.empty());
31
32 // Unpaired surrogates can't be converted back in Lenient mode.
33 utf8String = utf16String.utf8(WebString::UTF8ConversionMode::kLenient);
34 EXPECT_FALSE(utf8String.empty());
35 WebString utf16NewString =
36 WebString::fromUTF8(utf8String.data(), utf8String.length());
37 EXPECT_TRUE(utf16NewString.isNull());
38
39 // Round-trip works with StrictReplacingErrorsWithFFFD mode.
40 utf8String = utf16String.utf8(
41 WebString::UTF8ConversionMode::kStrictReplacingErrorsWithFFFD);
42 EXPECT_FALSE(utf8String.empty());
43 utf16NewString =
44 WebString::fromUTF8(utf8String.data(), utf8String.length());
45 EXPECT_FALSE(utf16NewString.isNull());
46 EXPECT_FALSE(utf16String == utf16NewString);
47 }
48 }
49
50 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/exported/WebString.cpp ('k') | third_party/WebKit/Source/wtf/text/StringUTF8Adaptor.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698