Chromium Code Reviews| 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/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/sys_string_conversions.h" | 12 #include "base/sys_string_conversions.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
| 15 #include "unicode/usearch.h" | 15 #include "unicode/usearch.h" |
| 16 | 16 |
| 17 #if defined(TOOLKIT_GTK) | |
| 18 #include <gtk/gtk.h> | |
| 19 #endif | |
| 20 | |
| 21 namespace base { | |
| 22 namespace i18n { | |
| 23 | |
| 17 namespace { | 24 namespace { |
| 18 base::i18n::TextDirection GetTextDirection(const char* locale_name) { | 25 |
| 19 return base::i18n::GetTextDirectionForLocale(locale_name); | 26 // A test utility function to set the application default text direction. |
| 27 void SetRTL(bool rtl) { | |
| 28 // Override the current locale/direction. | |
| 29 SetICUDefaultLocale(rtl ? "he" : "en"); | |
| 30 #if defined(TOOLKIT_GTK) | |
| 31 // Do the same for GTK, which does not rely on the ICU default locale. | |
| 32 gtk_widget_set_default_direction(rtl ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR); | |
| 33 #endif | |
| 34 EXPECT_EQ(rtl, IsRTL()); | |
| 20 } | 35 } |
| 21 } | 36 |
| 37 } // namespace | |
| 22 | 38 |
| 23 class RTLTest : public PlatformTest { | 39 class RTLTest : public PlatformTest { |
| 24 }; | 40 }; |
| 25 | 41 |
| 26 typedef struct { | |
| 27 const wchar_t* text; | |
| 28 base::i18n::TextDirection direction; | |
| 29 } TextAndDirection; | |
| 30 | |
| 31 TEST_F(RTLTest, GetFirstStrongCharacterDirection) { | 42 TEST_F(RTLTest, GetFirstStrongCharacterDirection) { |
| 32 const TextAndDirection test_data[] = { | 43 struct { |
| 44 const wchar_t* text; | |
| 45 TextDirection direction; | |
| 46 } cases[] = { | |
| 33 // Test pure LTR string. | 47 // Test pure LTR string. |
| 34 { L"foo bar", base::i18n::LEFT_TO_RIGHT }, | 48 { L"foo bar", LEFT_TO_RIGHT }, |
| 35 // Test bidi string in which the first character with strong directionality | 49 // Test bidi string in which the first character with strong directionality |
| 36 // is a character with type L. | 50 // is a character with type L. |
| 37 { L"foo \x05d0 bar", base::i18n::LEFT_TO_RIGHT }, | 51 { L"foo \x05d0 bar", LEFT_TO_RIGHT }, |
| 38 // Test bidi string in which the first character with strong directionality | 52 // Test bidi string in which the first character with strong directionality |
| 39 // is a character with type R. | 53 // is a character with type R. |
| 40 { L"\x05d0 foo bar", base::i18n::RIGHT_TO_LEFT }, | 54 { L"\x05d0 foo bar", RIGHT_TO_LEFT }, |
| 41 // Test bidi string which starts with a character with weak directionality | 55 // Test bidi string which starts with a character with weak directionality |
| 42 // and in which the first character with strong directionality is a | 56 // and in which the first character with strong directionality is a |
| 43 // character with type L. | 57 // character with type L. |
| 44 { L"!foo \x05d0 bar", base::i18n::LEFT_TO_RIGHT }, | 58 { L"!foo \x05d0 bar", LEFT_TO_RIGHT }, |
| 45 // Test bidi string which starts with a character with weak directionality | 59 // Test bidi string which starts with a character with weak directionality |
| 46 // and in which the first character with strong directionality is a | 60 // and in which the first character with strong directionality is a |
| 47 // character with type R. | 61 // character with type R. |
| 48 { L",\x05d0 foo bar", base::i18n::RIGHT_TO_LEFT }, | 62 { L",\x05d0 foo bar", RIGHT_TO_LEFT }, |
| 49 // Test bidi string in which the first character with strong directionality | 63 // Test bidi string in which the first character with strong directionality |
| 50 // is a character with type LRE. | 64 // is a character with type LRE. |
| 51 { L"\x202a \x05d0 foo bar", base::i18n::LEFT_TO_RIGHT }, | 65 { L"\x202a \x05d0 foo bar", LEFT_TO_RIGHT }, |
| 52 // Test bidi string in which the first character with strong directionality | 66 // Test bidi string in which the first character with strong directionality |
| 53 // is a character with type LRO. | 67 // is a character with type LRO. |
| 54 { L"\x202d \x05d0 foo bar", base::i18n::LEFT_TO_RIGHT }, | 68 { L"\x202d \x05d0 foo bar", LEFT_TO_RIGHT }, |
| 55 // Test bidi string in which the first character with strong directionality | 69 // Test bidi string in which the first character with strong directionality |
| 56 // is a character with type RLE. | 70 // is a character with type RLE. |
| 57 { L"\x202b foo \x05d0 bar", base::i18n::RIGHT_TO_LEFT }, | 71 { L"\x202b foo \x05d0 bar", RIGHT_TO_LEFT }, |
| 58 // Test bidi string in which the first character with strong directionality | 72 // Test bidi string in which the first character with strong directionality |
| 59 // is a character with type RLO. | 73 // is a character with type RLO. |
| 60 { L"\x202e foo \x05d0 bar", base::i18n::RIGHT_TO_LEFT }, | 74 { L"\x202e foo \x05d0 bar", RIGHT_TO_LEFT }, |
| 61 // Test bidi string in which the first character with strong directionality | 75 // Test bidi string in which the first character with strong directionality |
| 62 // is a character with type AL. | 76 // is a character with type AL. |
| 63 { L"\x0622 foo \x05d0 bar", base::i18n::RIGHT_TO_LEFT }, | 77 { L"\x0622 foo \x05d0 bar", RIGHT_TO_LEFT }, |
| 64 // Test a string without strong directionality characters. | 78 // Test a string without strong directionality characters. |
| 65 { L",!.{}", base::i18n::LEFT_TO_RIGHT }, | 79 { L",!.{}", LEFT_TO_RIGHT }, |
| 66 // Test empty string. | 80 // Test empty string. |
| 67 { L"", base::i18n::LEFT_TO_RIGHT }, | 81 { L"", LEFT_TO_RIGHT }, |
| 68 // Test characters in non-BMP (e.g. Phoenician letters. Please refer to | 82 // Test characters in non-BMP (e.g. Phoenician letters. Please refer to |
| 69 // http://demo.icu-project.org/icu-bin/ubrowse?scr=151&b=10910 for more | 83 // http://demo.icu-project.org/icu-bin/ubrowse?scr=151&b=10910 for more |
| 70 // information). | 84 // information). |
| 71 { | 85 { |
| 72 #if defined(WCHAR_T_IS_UTF32) | 86 #if defined(WCHAR_T_IS_UTF32) |
| 73 L" ! \x10910" L"abc 123", | 87 L" ! \x10910" L"abc 123", |
| 74 #elif defined(WCHAR_T_IS_UTF16) | 88 #elif defined(WCHAR_T_IS_UTF16) |
| 75 L" ! \xd802\xdd10" L"abc 123", | 89 L" ! \xd802\xdd10" L"abc 123", |
| 76 #else | 90 #else |
| 77 #error wchar_t should be either UTF-16 or UTF-32 | 91 #error wchar_t should be either UTF-16 or UTF-32 |
| 78 #endif | 92 #endif |
| 79 base::i18n::RIGHT_TO_LEFT }, | 93 RIGHT_TO_LEFT }, |
| 80 { | 94 { |
| 81 #if defined(WCHAR_T_IS_UTF32) | 95 #if defined(WCHAR_T_IS_UTF32) |
| 82 L" ! \x10401" L"abc 123", | 96 L" ! \x10401" L"abc 123", |
| 83 #elif defined(WCHAR_T_IS_UTF16) | 97 #elif defined(WCHAR_T_IS_UTF16) |
| 84 L" ! \xd801\xdc01" L"abc 123", | 98 L" ! \xd801\xdc01" L"abc 123", |
| 85 #else | 99 #else |
| 86 #error wchar_t should be either UTF-16 or UTF-32 | 100 #error wchar_t should be either UTF-16 or UTF-32 |
| 87 #endif | 101 #endif |
| 88 base::i18n::LEFT_TO_RIGHT }, | 102 LEFT_TO_RIGHT }, |
| 89 }; | 103 }; |
| 90 | 104 |
| 91 for (size_t i = 0; i < arraysize(test_data); ++i) { | 105 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) |
| 92 EXPECT_EQ(test_data[i].direction, | 106 EXPECT_EQ(cases[i].direction, |
| 93 base::i18n::GetFirstStrongCharacterDirection( | 107 GetFirstStrongCharacterDirection(WideToUTF16(cases[i].text))); |
| 94 WideToUTF16(test_data[i].text))); | |
| 95 } | |
| 96 } | 108 } |
| 97 | 109 |
| 98 TEST_F(RTLTest, WrapPathWithLTRFormatting) { | 110 TEST_F(RTLTest, WrapPathWithLTRFormatting) { |
| 99 const wchar_t* test_data[] = { | 111 const wchar_t* cases[] = { |
| 100 // Test common path, such as "c:\foo\bar". | 112 // Test common path, such as "c:\foo\bar". |
| 101 L"c:/foo/bar", | 113 L"c:/foo/bar", |
| 102 // Test path with file name, such as "c:\foo\bar\test.jpg". | 114 // Test path with file name, such as "c:\foo\bar\test.jpg". |
| 103 L"c:/foo/bar/test.jpg", | 115 L"c:/foo/bar/test.jpg", |
| 104 // Test path ending with punctuation, such as "c:\(foo)\bar.". | 116 // Test path ending with punctuation, such as "c:\(foo)\bar.". |
| 105 L"c:/(foo)/bar.", | 117 L"c:/(foo)/bar.", |
| 106 // Test path ending with separator, such as "c:\foo\bar\". | 118 // Test path ending with separator, such as "c:\foo\bar\". |
| 107 L"c:/foo/bar/", | 119 L"c:/foo/bar/", |
| 108 // Test path with RTL character. | 120 // Test path with RTL character. |
| 109 L"c:/\x05d0", | 121 L"c:/\x05d0", |
| 110 // Test path with 2 level RTL directory names. | 122 // Test path with 2 level RTL directory names. |
| 111 L"c:/\x05d0/\x0622", | 123 L"c:/\x05d0/\x0622", |
| 112 // Test path with mixed RTL/LTR directory names and ending with punctuation. | 124 // Test path with mixed RTL/LTR directory names and ending with punctuation. |
| 113 L"c:/\x05d0/\x0622/(foo)/b.a.r.", | 125 L"c:/\x05d0/\x0622/(foo)/b.a.r.", |
| 114 // Test path without driver name, such as "/foo/bar/test/jpg". | 126 // Test path without driver name, such as "/foo/bar/test/jpg". |
| 115 L"/foo/bar/test.jpg", | 127 L"/foo/bar/test.jpg", |
| 116 // Test path start with current directory, such as "./foo". | 128 // Test path start with current directory, such as "./foo". |
| 117 L"./foo", | 129 L"./foo", |
| 118 // Test path start with parent directory, such as "../foo/bar.jpg". | 130 // Test path start with parent directory, such as "../foo/bar.jpg". |
| 119 L"../foo/bar.jpg", | 131 L"../foo/bar.jpg", |
| 120 // Test absolute path, such as "//foo/bar.jpg". | 132 // Test absolute path, such as "//foo/bar.jpg". |
| 121 L"//foo/bar.jpg", | 133 L"//foo/bar.jpg", |
| 122 // Test path with mixed RTL/LTR directory names. | 134 // Test path with mixed RTL/LTR directory names. |
| 123 L"c:/foo/\x05d0/\x0622/\x05d1.jpg", | 135 L"c:/foo/\x05d0/\x0622/\x05d1.jpg", |
| 124 // Test empty path. | 136 // Test empty path. |
| 125 L"" | 137 L"" |
| 126 }; | 138 }; |
| 127 for (size_t i = 0; i < arraysize(test_data); ++i) { | 139 |
| 140 for (size_t i = 0; i < arraysize(cases); ++i) { | |
| 128 FilePath path; | 141 FilePath path; |
| 129 #if defined(OS_WIN) | 142 #if defined(OS_WIN) |
| 130 std::wstring win_path(test_data[i]); | 143 std::wstring win_path(cases[i]); |
| 131 std::replace(win_path.begin(), win_path.end(), '/', '\\'); | 144 std::replace(win_path.begin(), win_path.end(), '/', '\\'); |
| 132 path = FilePath(win_path); | 145 path = FilePath(win_path); |
| 133 std::wstring wrapped_expected = | 146 std::wstring wrapped_expected = |
| 134 std::wstring(L"\x202a") + win_path + L"\x202c"; | 147 std::wstring(L"\x202a") + win_path + L"\x202c"; |
| 135 #else | 148 #else |
| 136 path = FilePath(base::SysWideToNativeMB(test_data[i])); | 149 path = FilePath(base::SysWideToNativeMB(cases[i])); |
| 137 std::wstring wrapped_expected = | 150 std::wstring wrapped_expected = |
| 138 std::wstring(L"\x202a") + test_data[i] + L"\x202c"; | 151 std::wstring(L"\x202a") + cases[i] + L"\x202c"; |
| 139 #endif | 152 #endif |
| 140 string16 localized_file_path_string; | 153 string16 localized_file_path_string; |
| 141 base::i18n::WrapPathWithLTRFormatting(path, &localized_file_path_string); | 154 WrapPathWithLTRFormatting(path, &localized_file_path_string); |
| 142 | 155 |
| 143 std::wstring wrapped_actual = UTF16ToWide(localized_file_path_string); | 156 std::wstring wrapped_actual = UTF16ToWide(localized_file_path_string); |
| 144 EXPECT_EQ(wrapped_expected, wrapped_actual); | 157 EXPECT_EQ(wrapped_expected, wrapped_actual); |
| 145 } | 158 } |
| 146 } | 159 } |
| 147 | 160 |
| 148 typedef struct { | 161 TEST_F(RTLTest, WrapString) { |
| 149 const wchar_t* raw_filename; | 162 const wchar_t* cases[] = { |
| 150 const wchar_t* display_string; | 163 L" . ", |
| 151 } StringAndLTRString; | 164 L"abc", |
| 165 L"a"L"\x5d0\x5d1", | |
| 166 L"a"L"\x5d1"L"b", | |
| 167 L"\x5d0\x5d1\x5d2", | |
| 168 L"\x5d0\x5d1"L"a", | |
| 169 L"\x5d0"L"a"L"\x5d1", | |
| 170 }; | |
| 171 | |
| 172 const bool was_rtl = IsRTL(); | |
| 173 | |
| 174 for (size_t i = 0; i < 2; ++i) { | |
| 175 // Toggle the application default text direction (to try each direction). | |
| 176 SetRTL(!IsRTL()); | |
| 177 | |
| 178 string16 empty; | |
| 179 WrapStringWithLTRFormatting(&empty); | |
| 180 EXPECT_TRUE(empty.empty()); | |
| 181 WrapStringWithRTLFormatting(&empty); | |
| 182 EXPECT_TRUE(empty.empty()); | |
| 183 | |
| 184 for (size_t i = 0; i < arraysize(cases); ++i) { | |
| 185 string16 input = WideToUTF16(cases[i]); | |
| 186 string16 ltr_wrap = input; | |
| 187 WrapStringWithLTRFormatting(<r_wrap); | |
| 188 EXPECT_EQ(ltr_wrap[0], kLeftToRightEmbeddingMark); | |
| 189 EXPECT_EQ(ltr_wrap.substr(1, ltr_wrap.length() - 2), input); | |
| 190 EXPECT_EQ(ltr_wrap[ltr_wrap.length() -1], kPopDirectionalFormatting); | |
| 191 | |
| 192 string16 rtl_wrap = input; | |
| 193 WrapStringWithRTLFormatting(&rtl_wrap); | |
| 194 EXPECT_EQ(rtl_wrap[0], kRightToLeftEmbeddingMark); | |
| 195 EXPECT_EQ(rtl_wrap.substr(1, rtl_wrap.length() - 2), input); | |
| 196 EXPECT_EQ(rtl_wrap[rtl_wrap.length() -1], kPopDirectionalFormatting); | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 EXPECT_EQ(was_rtl, IsRTL()); | |
| 201 } | |
| 152 | 202 |
| 153 TEST_F(RTLTest, GetDisplayStringInLTRDirectionality) { | 203 TEST_F(RTLTest, GetDisplayStringInLTRDirectionality) { |
| 154 const StringAndLTRString test_data[] = { | 204 struct { |
| 155 { L"test", L"\x202atest\x202c" }, | 205 const wchar_t* path; |
| 156 { L"test.html", L"\x202atest.html\x202c" }, | 206 bool wrap_ltr; |
| 157 { L"\x05d0\x05d1\x05d2", L"\x202a\x05d0\x05d1\x05d2\x202c" }, | 207 bool wrap_rtl; |
| 158 { L"\x05d0\x05d1\x05d2.txt", L"\x202a\x05d0\x05d1\x05d2.txt\x202c" }, | 208 } cases[] = { |
| 159 { L"\x05d0"L"abc", L"\x202a\x05d0"L"abc\x202c" }, | 209 { L"test", false, true }, |
| 160 { L"\x05d0"L"abc.txt", L"\x202a\x05d0"L"abc.txt\x202c" }, | 210 { L"test.html", false, true }, |
| 161 { L"abc\x05d0\x05d1", L"\x202a"L"abc\x05d0\x05d1\x202c" }, | 211 { L"\x05d0\x05d1\x05d2", true, true }, |
| 162 { L"abc\x05d0\x05d1.jpg", L"\x202a"L"abc\x05d0\x05d1.jpg\x202c" }, | 212 { L"\x05d0\x05d1\x05d2.txt", true, true }, |
| 213 { L"\x05d0"L"abc", true, true }, | |
| 214 { L"\x05d0"L"abc.txt", true, true }, | |
| 215 { L"abc\x05d0\x05d1", false, true }, | |
| 216 { L"abc\x05d0\x05d1.jpg", false, true }, | |
| 163 }; | 217 }; |
| 164 for (size_t i = 0; i < arraysize(test_data); ++i) { | 218 |
| 165 string16 input = WideToUTF16(test_data[i].raw_filename); | 219 const bool was_rtl = IsRTL(); |
| 166 string16 expected = base::i18n::GetDisplayStringInLTRDirectionality(input); | 220 |
| 167 if (base::i18n::IsRTL()) | 221 for (size_t i = 0; i < 2; ++i) { |
| 168 EXPECT_EQ(expected, WideToUTF16(test_data[i].display_string)); | 222 // Toggle the application default text direction (to try each direction). |
| 169 else | 223 SetRTL(!IsRTL()); |
| 170 EXPECT_EQ(expected, input); | 224 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 225 string16 input = WideToUTF16(cases[i].path); | |
| 226 string16 output = GetDisplayStringInLTRDirectionality(input); | |
| 227 // Test the expected wrapping behavior for the current UI directionality. | |
| 228 if (IsRTL() ? cases[i].wrap_rtl : cases[i].wrap_ltr) | |
| 229 EXPECT_NE(output, input); | |
| 230 else | |
| 231 EXPECT_EQ(output, input); | |
| 232 } | |
| 171 } | 233 } |
| 234 | |
| 235 EXPECT_EQ(was_rtl, IsRTL()); | |
| 172 } | 236 } |
| 173 | 237 |
| 174 TEST_F(RTLTest, GetTextDirection) { | 238 TEST_F(RTLTest, GetTextDirection) { |
| 175 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, GetTextDirection("ar")); | 239 EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocale("ar")); |
| 176 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, GetTextDirection("ar_EG")); | 240 EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocale("ar_EG")); |
| 177 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, GetTextDirection("he")); | 241 EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocale("he")); |
| 178 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, GetTextDirection("he_IL")); | 242 EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocale("he_IL")); |
| 179 // iw is an obsolete code for Hebrew. | 243 // iw is an obsolete code for Hebrew. |
| 180 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, GetTextDirection("iw")); | 244 EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocale("iw")); |
| 181 // Although we're not yet localized to Farsi and Urdu, we | 245 // Although we're not yet localized to Farsi and Urdu, we |
| 182 // do have the text layout direction information for them. | 246 // do have the text layout direction information for them. |
| 183 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, GetTextDirection("fa")); | 247 EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocale("fa")); |
| 184 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, GetTextDirection("ur")); | 248 EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocale("ur")); |
| 185 #if 0 | 249 #if 0 |
| 186 // Enable these when we include the minimal locale data for Azerbaijani | 250 // Enable these when we include the minimal locale data for Azerbaijani |
| 187 // written in Arabic and Dhivehi. At the moment, our copy of | 251 // written in Arabic and Dhivehi. At the moment, our copy of |
| 188 // ICU data does not have entries for them. | 252 // ICU data does not have entries for them. |
| 189 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, GetTextDirection("az_Arab")); | 253 EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocale("az_Arab")); |
| 190 // Dhivehi that uses Thaana script. | 254 // Dhivehi that uses Thaana script. |
| 191 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, GetTextDirection("dv")); | 255 EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocale("dv")); |
| 192 #endif | 256 #endif |
| 193 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, GetTextDirection("en")); | 257 EXPECT_EQ(LEFT_TO_RIGHT, GetTextDirectionForLocale("en")); |
| 194 // Chinese in China with '-'. | 258 // Chinese in China with '-'. |
| 195 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, GetTextDirection("zh-CN")); | 259 EXPECT_EQ(LEFT_TO_RIGHT, GetTextDirectionForLocale("zh-CN")); |
| 196 // Filipino : 3-letter code | 260 // Filipino : 3-letter code |
| 197 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, GetTextDirection("fil")); | 261 EXPECT_EQ(LEFT_TO_RIGHT, GetTextDirectionForLocale("fil")); |
| 198 // Russian | 262 // Russian |
| 199 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, GetTextDirection("ru")); | 263 EXPECT_EQ(LEFT_TO_RIGHT, GetTextDirectionForLocale("ru")); |
| 200 // Japanese that uses multiple scripts | 264 // Japanese that uses multiple scripts |
| 201 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, GetTextDirection("ja")); | 265 EXPECT_EQ(LEFT_TO_RIGHT, GetTextDirectionForLocale("ja")); |
| 202 } | 266 } |
| 203 | 267 |
| 204 TEST_F(RTLTest, UnadjustStringForLocaleDirection) { | 268 TEST_F(RTLTest, UnadjustStringForLocaleDirection) { |
| 205 // These test strings are borrowed from WrapPathWithLTRFormatting | 269 // These test strings are borrowed from WrapPathWithLTRFormatting |
| 206 const wchar_t* test_data[] = { | 270 const wchar_t* cases[] = { |
| 207 L"foo bar", | 271 L"foo bar", |
| 208 L"foo \x05d0 bar", | 272 L"foo \x05d0 bar", |
| 209 L"\x05d0 foo bar", | 273 L"\x05d0 foo bar", |
| 210 L"!foo \x05d0 bar", | 274 L"!foo \x05d0 bar", |
| 211 L",\x05d0 foo bar", | 275 L",\x05d0 foo bar", |
| 212 L"\x202a \x05d0 foo bar", | 276 L"\x202a \x05d0 foo bar", |
| 213 L"\x202d \x05d0 foo bar", | 277 L"\x202d \x05d0 foo bar", |
| 214 L"\x202b foo \x05d0 bar", | 278 L"\x202b foo \x05d0 bar", |
| 215 L"\x202e foo \x05d0 bar", | 279 L"\x202e foo \x05d0 bar", |
| 216 L"\x0622 foo \x05d0 bar", | 280 L"\x0622 foo \x05d0 bar", |
| 217 }; | 281 }; |
| 218 | 282 |
| 219 const char* default_locale = uloc_getDefault(); | 283 const bool was_rtl = IsRTL(); |
| 220 | 284 |
| 221 for (size_t i = 0; i < 2; i++) { | 285 for (size_t i = 0; i < 2; ++i) { |
| 222 // Try in LTR and RTL. | 286 // Toggle the application default text direction (to try each direction). |
| 223 std::string locale(i == 0 ? "en_US" : "he_IL"); | 287 SetRTL(!IsRTL()); |
| 224 base::i18n::SetICUDefaultLocale(locale); | |
| 225 | 288 |
| 226 for (size_t i = 0; i < arraysize(test_data); ++i) { | 289 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 227 string16 test_case = WideToUTF16(test_data[i]); | 290 string16 test_case = WideToUTF16(cases[i]); |
| 228 string16 adjusted_string = test_case; | 291 string16 adjusted_string = test_case; |
| 229 | 292 |
| 230 if (!base::i18n::AdjustStringForLocaleDirection(&adjusted_string)) | 293 if (!AdjustStringForLocaleDirection(&adjusted_string)) |
| 231 continue; | 294 continue; |
| 232 | 295 |
| 233 EXPECT_NE(test_case, adjusted_string); | 296 EXPECT_NE(test_case, adjusted_string); |
| 234 EXPECT_TRUE(base::i18n::UnadjustStringForLocaleDirection( | 297 EXPECT_TRUE(UnadjustStringForLocaleDirection(&adjusted_string)); |
| 235 &adjusted_string)); | 298 EXPECT_EQ(test_case, adjusted_string) << " for test case " << test_case; |
|
jungshik at Google
2012/09/12 20:24:35
nit: shouldn't we indicate whether it's RTL or LTR
msw
2012/09/12 20:46:52
Done.
| |
| 236 EXPECT_EQ(test_case, adjusted_string) << " for test case " << test_case | |
| 237 << " and locale " << locale; | |
| 238 } | 299 } |
| 239 } | 300 } |
| 240 | 301 |
| 241 base::i18n::SetICUDefaultLocale(default_locale); | 302 EXPECT_EQ(was_rtl, IsRTL()); |
| 242 } | 303 } |
| 304 | |
| 305 } // namespace i18n | |
| 306 } // namespace base | |
| OLD | NEW |