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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/platform/exported/WebStringTest.cpp
diff --git a/third_party/WebKit/Source/platform/exported/WebStringTest.cpp b/third_party/WebKit/Source/platform/exported/WebStringTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..c5199c4930fc6605eb0a962ab4653ada9eb1be90
--- /dev/null
+++ b/third_party/WebKit/Source/platform/exported/WebStringTest.cpp
@@ -0,0 +1,50 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "public/platform/WebString.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+#include "wtf/text/WTFString.h"
+
+namespace blink {
+
+TEST(WebStringTest, UTF8ConversionRoundTrip) {
+ // Valid characters.
+ for (UChar uchar = 0; uchar <= 0xD7FF; ++uchar) {
+ WebString utf16String(&uchar, 1);
+ std::string utf8String(utf16String.utf8());
+ WebString utf16NewString =
+ WebString::fromUTF8(utf8String.data(), utf8String.length());
+ EXPECT_FALSE(utf16NewString.isNull());
+ EXPECT_TRUE(utf16String == utf16NewString);
+ }
+
+ // Unpaired surrogates.
+ for (UChar uchar = 0xD800; uchar <= 0xDFFF; ++uchar) {
+ WebString utf16String(&uchar, 1);
+
+ // Conversion with Strict mode results in an empty string.
+ std::string utf8String(
+ utf16String.utf8(WebString::UTF8ConversionMode::kStrict));
+ EXPECT_TRUE(utf8String.empty());
+
+ // Unpaired surrogates can't be converted back in Lenient mode.
+ utf8String = utf16String.utf8(WebString::UTF8ConversionMode::kLenient);
+ EXPECT_FALSE(utf8String.empty());
+ WebString utf16NewString =
+ WebString::fromUTF8(utf8String.data(), utf8String.length());
+ EXPECT_TRUE(utf16NewString.isNull());
+
+ // Round-trip works with StrictReplacingErrorsWithFFFD mode.
+ utf8String = utf16String.utf8(
+ WebString::UTF8ConversionMode::kStrictReplacingErrorsWithFFFD);
+ EXPECT_FALSE(utf8String.empty());
+ utf16NewString =
+ WebString::fromUTF8(utf8String.data(), utf8String.length());
+ EXPECT_FALSE(utf16NewString.isNull());
+ EXPECT_FALSE(utf16String == utf16NewString);
+ }
+}
+
+} // namespace blink
« 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