OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/text/TextBreakIterator.h" | 5 #include "platform/text/TextBreakIterator.h" |
6 | 6 |
7 #include "platform/wtf/text/WTFString.h" | 7 #include "platform/wtf/text/WTFString.h" |
8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace blink { | 11 namespace blink { |
12 | 12 |
13 class TextBreakIteratorTest : public testing::Test { | 13 class TextBreakIteratorTest : public ::testing::Test { |
14 protected: | 14 protected: |
15 void SetTestString(const char* test_string) { | 15 void SetTestString(const char* test_string) { |
16 test_string_ = String::FromUTF8(test_string); | 16 test_string_ = String::FromUTF8(test_string); |
17 } | 17 } |
18 | 18 |
19 // The expected break positions must be specified UTF-16 character boundaries. | 19 // The expected break positions must be specified UTF-16 character boundaries. |
20 void MatchLineBreaks(LineBreakType line_break_type, | 20 void MatchLineBreaks(LineBreakType line_break_type, |
21 const Vector<int> expected_break_positions) { | 21 const Vector<int> expected_break_positions) { |
22 if (test_string_.Is8Bit()) { | 22 if (test_string_.Is8Bit()) { |
23 test_string_ = String::Make16BitFrom8BitSource(test_string_.Characters8(), | 23 test_string_ = String::Make16BitFrom8BitSource(test_string_.Characters8(), |
24 test_string_.length()); | 24 test_string_.length()); |
25 } | 25 } |
26 LazyLineBreakIterator lazy_break_iterator(test_string_); | 26 LazyLineBreakIterator lazy_break_iterator(test_string_); |
27 lazy_break_iterator.SetBreakType(line_break_type); | 27 lazy_break_iterator.SetBreakType(line_break_type); |
28 TestIsBreakable(expected_break_positions, lazy_break_iterator); | 28 TestIsBreakable(expected_break_positions, lazy_break_iterator); |
29 TestNextBreakOpportunity(expected_break_positions, lazy_break_iterator); | 29 TestNextBreakOpportunity(expected_break_positions, lazy_break_iterator); |
30 } | 30 } |
31 | 31 |
32 // Test IsBreakable() by iterating all positions. BreakingContext uses this | 32 // Test IsBreakable() by iterating all positions. BreakingContext uses this |
33 // interface. | 33 // interface. |
34 void TestIsBreakable(const Vector<int> expected_break_positions, | 34 void TestIsBreakable(const Vector<int> expected_break_positions, |
35 const LazyLineBreakIterator& break_iterator) { | 35 const LazyLineBreakIterator& break_iterator) { |
36 Vector<int> break_positions; | 36 Vector<int> break_positions; |
37 int next_breakable = -1; | 37 int next_breakable = -1; |
38 for (unsigned i = 0; i <= test_string_.length(); i++) { | 38 for (unsigned i = 0; i <= test_string_.length(); i++) { |
39 if (break_iterator.IsBreakable(i, next_breakable)) | 39 if (break_iterator.IsBreakable(i, next_breakable)) |
40 break_positions.push_back(i); | 40 break_positions.push_back(i); |
41 } | 41 } |
42 EXPECT_THAT(break_positions, | 42 EXPECT_THAT(break_positions, |
43 testing::ElementsAreArray(expected_break_positions)) | 43 ::testing::ElementsAreArray(expected_break_positions)) |
44 << break_iterator.BreakType() << " for " << test_string_; | 44 << break_iterator.BreakType() << " for " << test_string_; |
45 } | 45 } |
46 | 46 |
47 // Test NextBreakOpportunity() by iterating break opportunities. | 47 // Test NextBreakOpportunity() by iterating break opportunities. |
48 // ShapingLineBreaker uses this interface. | 48 // ShapingLineBreaker uses this interface. |
49 void TestNextBreakOpportunity(const Vector<int> expected_break_positions, | 49 void TestNextBreakOpportunity(const Vector<int> expected_break_positions, |
50 const LazyLineBreakIterator& break_iterator) { | 50 const LazyLineBreakIterator& break_iterator) { |
51 Vector<int> break_positions; | 51 Vector<int> break_positions; |
52 for (unsigned i = 0; i <= test_string_.length(); i++) { | 52 for (unsigned i = 0; i <= test_string_.length(); i++) { |
53 i = break_iterator.NextBreakOpportunity(i); | 53 i = break_iterator.NextBreakOpportunity(i); |
54 break_positions.push_back(i); | 54 break_positions.push_back(i); |
55 } | 55 } |
56 EXPECT_THAT(break_positions, | 56 EXPECT_THAT(break_positions, |
57 testing::ElementsAreArray(expected_break_positions)) | 57 ::testing::ElementsAreArray(expected_break_positions)) |
58 << break_iterator.BreakType() << " for " << test_string_; | 58 << break_iterator.BreakType() << " for " << test_string_; |
59 } | 59 } |
60 | 60 |
61 private: | 61 private: |
62 String test_string_; | 62 String test_string_; |
63 }; | 63 }; |
64 | 64 |
65 // Initializing Vector from an initializer list still not possible, C++ feature | 65 // Initializing Vector from an initializer list still not possible, C++ feature |
66 // banned in Blink. | 66 // banned in Blink. |
67 #define DECLARE_BREAKSVECTOR(...) \ | 67 #define DECLARE_BREAKSVECTOR(...) \ |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 TEST_F(TextBreakIteratorTest, KeepEmojiModifierSequence) { | 134 TEST_F(TextBreakIteratorTest, KeepEmojiModifierSequence) { |
135 SetTestString(u8"abc \u261D\U0001F3FB def"); | 135 SetTestString(u8"abc \u261D\U0001F3FB def"); |
136 MATCH_LINE_BREAKS(LineBreakType::kNormal, {3, 7, 11}); | 136 MATCH_LINE_BREAKS(LineBreakType::kNormal, {3, 7, 11}); |
137 MATCH_LINE_BREAKS(LineBreakType::kBreakAll, {1, 2, 3, 7, 9, 10, 11}); | 137 MATCH_LINE_BREAKS(LineBreakType::kBreakAll, {1, 2, 3, 7, 9, 10, 11}); |
138 MATCH_LINE_BREAKS(LineBreakType::kBreakCharacter, | 138 MATCH_LINE_BREAKS(LineBreakType::kBreakCharacter, |
139 {1, 2, 3, 4, 7, 8, 9, 10, 11}); | 139 {1, 2, 3, 4, 7, 8, 9, 10, 11}); |
140 MATCH_LINE_BREAKS(LineBreakType::kKeepAll, {3, 7, 11}); | 140 MATCH_LINE_BREAKS(LineBreakType::kKeepAll, {3, 7, 11}); |
141 } | 141 } |
142 | 142 |
143 } // namespace blink | 143 } // namespace blink |
OLD | NEW |