| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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 "platform/text/TextBreakIterator.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "wtf/text/WTFString.h" |
| 9 |
| 10 namespace blink { |
| 11 |
| 12 class TextBreakIteratorTest : public testing::Test { |
| 13 protected: |
| 14 void SetTestString(const char* testString) |
| 15 { |
| 16 m_testString = String::fromUTF8(testString); |
| 17 } |
| 18 |
| 19 // The expected break positions must be specified UTF-16 character boundarie
s. |
| 20 void MatchLineBreaks(LineBreakType lineBreakType, const Vector<int> expected
BreakPositions) |
| 21 { |
| 22 if (m_testString.is8Bit()) { |
| 23 m_testString = String::make16BitFrom8BitSource(m_testString.characte
rs8(), m_testString.length()); |
| 24 } |
| 25 LazyLineBreakIterator lazyBreakIterator(m_testString); |
| 26 int nextBreakable = 0; |
| 27 for (auto breakPosition : expectedBreakPositions) { |
| 28 int triggerPos = std::min(static_cast<unsigned>(nextBreakable + 1),
m_testString.length()); |
| 29 bool isBreakable = lazyBreakIterator.isBreakable(triggerPos, nextBre
akable, lineBreakType); |
| 30 if (isBreakable) { |
| 31 ASSERT_EQ(triggerPos, breakPosition); |
| 32 } |
| 33 ASSERT_EQ(breakPosition, nextBreakable); |
| 34 } |
| 35 } |
| 36 |
| 37 private: |
| 38 String m_testString; |
| 39 }; |
| 40 |
| 41 // Initializing Vector from an initializer list still not possible, C++ feature
banned in Blink. |
| 42 #define DECLARE_BREAKSVECTOR(...) \ |
| 43 static const int32_t breaksArray[] = __VA_ARGS__; \ |
| 44 Vector<int> breaks; \ |
| 45 breaks.append(breaksArray, sizeof(breaksArray) / sizeof(*breaksArray)); |
| 46 |
| 47 #define MATCH_LINE_BREAKS(LINEBREAKTYPE, ...) \ |
| 48 { \ |
| 49 DECLARE_BREAKSVECTOR(__VA_ARGS__); \ |
| 50 MatchLineBreaks(LINEBREAKTYPE, breaks); \ |
| 51 } |
| 52 |
| 53 TEST_F(TextBreakIteratorTest, Basic) |
| 54 { |
| 55 SetTestString("a b c"); |
| 56 MATCH_LINE_BREAKS(LineBreakType::Normal, { 1, 3, 5 }); |
| 57 } |
| 58 |
| 59 TEST_F(TextBreakIteratorTest, Chinese) |
| 60 { |
| 61 SetTestString("標準萬國碼"); |
| 62 MATCH_LINE_BREAKS(LineBreakType::Normal, { 1, 2, 3, 4, 5 }); |
| 63 MATCH_LINE_BREAKS(LineBreakType::BreakAll, { 1, 2, 3, 4, 5 }); |
| 64 MATCH_LINE_BREAKS(LineBreakType::KeepAll, { 5 }); |
| 65 } |
| 66 |
| 67 TEST_F(TextBreakIteratorTest, KeepEmojiZWJFamilyIsolate) |
| 68 { |
| 69 SetTestString("\xF0\x9F\x91\xA8\xE2\x80\x8D\xF0\x9F\x91\xA9\xE2\x80\x8D\xF0\
x9F\x91\xA7\xE2\x80\x8D\xF0\x9F\x91\xA6"); |
| 70 MATCH_LINE_BREAKS(LineBreakType::Normal, { 11 }); |
| 71 MATCH_LINE_BREAKS(LineBreakType::BreakAll, { 11 }); |
| 72 MATCH_LINE_BREAKS(LineBreakType::KeepAll, { 11 }); |
| 73 } |
| 74 |
| 75 TEST_F(TextBreakIteratorTest, KeepEmojiModifierSequenceIsolate) |
| 76 { |
| 77 SetTestString("\xE2\x98\x9D\xF0\x9F\x8F\xBB"); |
| 78 MATCH_LINE_BREAKS(LineBreakType::Normal, { 3 }); |
| 79 MATCH_LINE_BREAKS(LineBreakType::BreakAll, { 3 }); |
| 80 MATCH_LINE_BREAKS(LineBreakType::KeepAll, { 3 }); |
| 81 } |
| 82 |
| 83 TEST_F(TextBreakIteratorTest, KeepEmojiZWJSequence) |
| 84 { |
| 85 SetTestString("abc \xF0\x9F\x91\xA9\xE2\x80\x8D\xF0\x9F\x91\xA9\xE2\x80\x8D\
xF0\x9F\x91\xA7\xE2\x80\x8D\xF0\x9F\x91\xA7 def"); |
| 86 MATCH_LINE_BREAKS(LineBreakType::Normal, { 3, 15, 19 }); |
| 87 MATCH_LINE_BREAKS(LineBreakType::BreakAll, { 1, 2, 3, 15, 17, 18, 19 }); |
| 88 MATCH_LINE_BREAKS(LineBreakType::KeepAll, { 3, 15, 19 }); |
| 89 } |
| 90 |
| 91 TEST_F(TextBreakIteratorTest, KeepEmojiModifierSequence) |
| 92 { |
| 93 SetTestString("abc \xE2\x98\x9D\xF0\x9F\x8F\xBB def"); |
| 94 MATCH_LINE_BREAKS(LineBreakType::Normal, { 3, 7, 11 }); |
| 95 MATCH_LINE_BREAKS(LineBreakType::BreakAll, { 1, 2, 3, 7, 9, 10, 11 }); |
| 96 MATCH_LINE_BREAKS(LineBreakType::KeepAll, { 3, 7, 11 }); |
| 97 } |
| 98 |
| 99 } // namespace blink |
| OLD | NEW |