| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/i18n/rtl.h" | 5 #include "base/i18n/rtl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 class RTLTest : public PlatformTest { | 39 class RTLTest : public PlatformTest { |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 TEST_F(RTLTest, GetFirstStrongCharacterDirection) { | 42 TEST_F(RTLTest, GetFirstStrongCharacterDirection) { |
| 43 struct { | 43 struct { |
| 44 const wchar_t* text; | 44 const wchar_t* text; |
| 45 TextDirection direction; | 45 TextDirection direction; |
| 46 } cases[] = { | 46 } cases[] = { |
| 47 // Test pure LTR string. | 47 // Test pure LTR string. |
| 48 { L"foo bar", LEFT_TO_RIGHT }, | 48 { L"foo bar", LEFT_TO_RIGHT }, |
| 49 // Test pure RTL string. | |
| 50 { L"\x05d0\x05d1\x05d2 \x05d3\x0d4\x05d5", RIGHT_TO_LEFT}, | |
| 51 // Test bidi string in which the first character with strong directionality | 49 // Test bidi string in which the first character with strong directionality |
| 52 // is a character with type L. | 50 // is a character with type L. |
| 53 { L"foo \x05d0 bar", LEFT_TO_RIGHT }, | 51 { L"foo \x05d0 bar", LEFT_TO_RIGHT }, |
| 54 // Test bidi string in which the first character with strong directionality | 52 // Test bidi string in which the first character with strong directionality |
| 55 // is a character with type R. | 53 // is a character with type R. |
| 56 { L"\x05d0 foo bar", RIGHT_TO_LEFT }, | 54 { L"\x05d0 foo bar", RIGHT_TO_LEFT }, |
| 57 // Test bidi string which starts with a character with weak directionality | 55 // Test bidi string which starts with a character with weak directionality |
| 58 // and in which the first character with strong directionality is a | 56 // and in which the first character with strong directionality is a |
| 59 // character with type L. | 57 // character with type L. |
| 60 { L"!foo \x05d0 bar", LEFT_TO_RIGHT }, | 58 { L"!foo \x05d0 bar", LEFT_TO_RIGHT }, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 #error wchar_t should be either UTF-16 or UTF-32 | 100 #error wchar_t should be either UTF-16 or UTF-32 |
| 103 #endif | 101 #endif |
| 104 LEFT_TO_RIGHT }, | 102 LEFT_TO_RIGHT }, |
| 105 }; | 103 }; |
| 106 | 104 |
| 107 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) | 105 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) |
| 108 EXPECT_EQ(cases[i].direction, | 106 EXPECT_EQ(cases[i].direction, |
| 109 GetFirstStrongCharacterDirection(WideToUTF16(cases[i].text))); | 107 GetFirstStrongCharacterDirection(WideToUTF16(cases[i].text))); |
| 110 } | 108 } |
| 111 | 109 |
| 112 | |
| 113 // Note that the cases with LRE, LRO, RLE and RLO are invalid for | |
| 114 // GetLastStrongCharacterDirection because they should be followed by PDF | |
| 115 // character. | |
| 116 TEST_F(RTLTest, GetLastStrongCharacterDirection) { | |
| 117 struct { | |
| 118 const wchar_t* text; | |
| 119 TextDirection direction; | |
| 120 } cases[] = { | |
| 121 // Test pure LTR string. | |
| 122 { L"foo bar", LEFT_TO_RIGHT }, | |
| 123 // Test pure RTL string. | |
| 124 { L"\x05d0\x05d1\x05d2 \x05d3\x0d4\x05d5", RIGHT_TO_LEFT}, | |
| 125 // Test bidi string in which the last character with strong directionality | |
| 126 // is a character with type L. | |
| 127 { L"foo \x05d0 bar", LEFT_TO_RIGHT }, | |
| 128 // Test bidi string in which the last character with strong directionality | |
| 129 // is a character with type R. | |
| 130 { L"\x05d0 foo bar \x05d3", RIGHT_TO_LEFT }, | |
| 131 // Test bidi string which ends with a character with weak directionality | |
| 132 // and in which the last character with strong directionality is a | |
| 133 // character with type L. | |
| 134 { L"!foo \x05d0 bar!", LEFT_TO_RIGHT }, | |
| 135 // Test bidi string which ends with a character with weak directionality | |
| 136 // and in which the last character with strong directionality is a | |
| 137 // character with type R. | |
| 138 { L",\x05d0 foo bar \x05d1,", RIGHT_TO_LEFT }, | |
| 139 // Test bidi string in which the last character with strong directionality | |
| 140 // is a character with type AL. | |
| 141 { L"\x0622 foo \x05d0 bar \x0622", RIGHT_TO_LEFT }, | |
| 142 // Test a string without strong directionality characters. | |
| 143 { L",!.{}", LEFT_TO_RIGHT }, | |
| 144 // Test empty string. | |
| 145 { L"", LEFT_TO_RIGHT }, | |
| 146 // Test characters in non-BMP (e.g. Phoenician letters. Please refer to | |
| 147 // http://demo.icu-project.org/icu-bin/ubrowse?scr=151&b=10910 for more | |
| 148 // information). | |
| 149 { | |
| 150 #if defined(WCHAR_T_IS_UTF32) | |
| 151 L"abc 123" L" ! \x10910 !", | |
| 152 #elif defined(WCHAR_T_IS_UTF16) | |
| 153 L"abc 123" L" ! \xd802\xdd10 !", | |
| 154 #else | |
| 155 #error wchar_t should be either UTF-16 or UTF-32 | |
| 156 #endif | |
| 157 RIGHT_TO_LEFT }, | |
| 158 { | |
| 159 #if defined(WCHAR_T_IS_UTF32) | |
| 160 L"abc 123" L" ! \x10401 !", | |
| 161 #elif defined(WCHAR_T_IS_UTF16) | |
| 162 L"abc 123" L" ! \xd801\xdc01 !", | |
| 163 #else | |
| 164 #error wchar_t should be either UTF-16 or UTF-32 | |
| 165 #endif | |
| 166 LEFT_TO_RIGHT }, | |
| 167 }; | |
| 168 | |
| 169 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) | |
| 170 EXPECT_EQ(cases[i].direction, | |
| 171 GetLastStrongCharacterDirection(WideToUTF16(cases[i].text))); | |
| 172 } | |
| 173 | |
| 174 TEST_F(RTLTest, GetStringDirection) { | 110 TEST_F(RTLTest, GetStringDirection) { |
| 175 struct { | 111 struct { |
| 176 const wchar_t* text; | 112 const wchar_t* text; |
| 177 TextDirection direction; | 113 TextDirection direction; |
| 178 } cases[] = { | 114 } cases[] = { |
| 179 // Test pure LTR string. | 115 // Test pure LTR string. |
| 180 { L"foobar", LEFT_TO_RIGHT }, | 116 { L"foobar", LEFT_TO_RIGHT }, |
| 181 { L".foobar", LEFT_TO_RIGHT }, | 117 { L".foobar", LEFT_TO_RIGHT }, |
| 182 { L"foo, bar", LEFT_TO_RIGHT }, | 118 { L"foo, bar", LEFT_TO_RIGHT }, |
| 183 // Test pure LTR with strong directionality characters of type LRE. | 119 // Test pure LTR with strong directionality characters of type LRE. |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 EXPECT_EQ(test_case, adjusted_string) << " for test case [" << test_case | 376 EXPECT_EQ(test_case, adjusted_string) << " for test case [" << test_case |
| 441 << "] with IsRTL() == " << IsRTL(); | 377 << "] with IsRTL() == " << IsRTL(); |
| 442 } | 378 } |
| 443 } | 379 } |
| 444 | 380 |
| 445 EXPECT_EQ(was_rtl, IsRTL()); | 381 EXPECT_EQ(was_rtl, IsRTL()); |
| 446 } | 382 } |
| 447 | 383 |
| 448 } // namespace i18n | 384 } // namespace i18n |
| 449 } // namespace base | 385 } // namespace base |
| OLD | NEW |