| 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" |
| 11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/test/icu_test_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
| 15 #include "third_party/icu/source/i18n/unicode/usearch.h" | 16 #include "third_party/icu/source/i18n/unicode/usearch.h" |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 namespace i18n { | 19 namespace i18n { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // A test utility function to set the application default text direction. | 23 // A test utility function to set the application default text direction. |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 LEFT_TO_RIGHT }, | 237 LEFT_TO_RIGHT }, |
| 237 }; | 238 }; |
| 238 | 239 |
| 239 for (size_t i = 0; i < arraysize(cases); ++i) | 240 for (size_t i = 0; i < arraysize(cases); ++i) |
| 240 EXPECT_EQ(cases[i].direction, | 241 EXPECT_EQ(cases[i].direction, |
| 241 GetStringDirection(WideToUTF16(cases[i].text))); | 242 GetStringDirection(WideToUTF16(cases[i].text))); |
| 242 } | 243 } |
| 243 | 244 |
| 244 TEST_F(RTLTest, WrapPathWithLTRFormatting) { | 245 TEST_F(RTLTest, WrapPathWithLTRFormatting) { |
| 245 const wchar_t* cases[] = { | 246 const wchar_t* cases[] = { |
| 246 // Test common path, such as "c:\foo\bar". | |
| 247 L"c:/foo/bar", | |
| 248 // Test path with file name, such as "c:\foo\bar\test.jpg". | |
| 249 L"c:/foo/bar/test.jpg", | |
| 250 // Test path ending with punctuation, such as "c:\(foo)\bar.". | |
| 251 L"c:/(foo)/bar.", | |
| 252 // Test path ending with separator, such as "c:\foo\bar\". | |
| 253 L"c:/foo/bar/", | |
| 254 // Test path with RTL character. | |
| 255 L"c:/\x05d0", | |
| 256 // Test path with 2 level RTL directory names. | |
| 257 L"c:/\x05d0/\x0622", | |
| 258 // Test path with mixed RTL/LTR directory names and ending with punctuation. | |
| 259 L"c:/\x05d0/\x0622/(foo)/b.a.r.", | |
| 260 // Test path without driver name, such as "/foo/bar/test/jpg". | |
| 261 L"/foo/bar/test.jpg", | 247 L"/foo/bar/test.jpg", |
| 262 // Test path start with current directory, such as "./foo". | 248 // Test path start with current directory, such as "./foo". |
| 263 L"./foo", | 249 L"./foo", |
| 264 // Test path start with parent directory, such as "../foo/bar.jpg". | 250 // Test path start with parent directory, such as "../foo/bar.jpg". |
| 265 L"../foo/bar.jpg", | 251 L"../foo/bar.jpg", |
| 266 // Test absolute path, such as "//foo/bar.jpg". | 252 // Test absolute path, such as "//foo/bar.jpg". |
| 267 L"//foo/bar.jpg", | 253 L"//foo/bar.jpg", |
| 268 // Test path with mixed RTL/LTR directory names. | |
| 269 L"c:/foo/\x05d0/\x0622/\x05d1.jpg", | |
| 270 // Test empty path. | 254 // Test empty path. |
| 271 L"" | 255 L"" |
| 272 }; | 256 }; |
| 273 | 257 |
| 274 for (size_t i = 0; i < arraysize(cases); ++i) { | 258 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 275 FilePath path; | 259 FilePath path; |
| 276 #if defined(OS_WIN) | |
| 277 std::wstring win_path(cases[i]); | |
| 278 std::replace(win_path.begin(), win_path.end(), '/', '\\'); | |
| 279 path = FilePath(win_path); | |
| 280 std::wstring wrapped_expected = | |
| 281 std::wstring(L"\x202a") + win_path + L"\x202c"; | |
| 282 #else | |
| 283 path = FilePath(base::SysWideToNativeMB(cases[i])); | 260 path = FilePath(base::SysWideToNativeMB(cases[i])); |
| 284 std::wstring wrapped_expected = | 261 std::wstring wrapped_expected = |
| 285 std::wstring(L"\x202a") + cases[i] + L"\x202c"; | 262 std::wstring(L"\x202a") + cases[i] + L"\x202c"; |
| 286 #endif | |
| 287 string16 localized_file_path_string; | 263 string16 localized_file_path_string; |
| 288 WrapPathWithLTRFormatting(path, &localized_file_path_string); | 264 WrapPathWithLTRFormatting(path, &localized_file_path_string); |
| 289 | 265 |
| 290 std::wstring wrapped_actual = UTF16ToWide(localized_file_path_string); | 266 std::wstring wrapped_actual = UTF16ToWide(localized_file_path_string); |
| 291 EXPECT_EQ(wrapped_expected, wrapped_actual); | 267 EXPECT_EQ(wrapped_expected, wrapped_actual); |
| 292 } | 268 } |
| 293 } | 269 } |
| 294 | 270 |
| 295 TEST_F(RTLTest, WrapString) { | 271 TEST_F(RTLTest, WrapString) { |
| 296 const wchar_t* cases[] = { | 272 const wchar_t* cases[] = { |
| 297 L" . ", | 273 L" . ", |
| 298 L"abc", | 274 L"abc", |
| 299 L"a" L"\x5d0\x5d1", | 275 L"a" L"\x5d0\x5d1", |
| 300 L"a" L"\x5d1" L"b", | 276 L"a" L"\x5d1" L"b", |
| 301 L"\x5d0\x5d1\x5d2", | 277 L"\x5d0\x5d1\x5d2", |
| 302 L"\x5d0\x5d1" L"a", | 278 L"\x5d0\x5d1" L"a", |
| 303 L"\x5d0" L"a" L"\x5d1", | 279 L"\x5d0" L"a" L"\x5d1", |
| 304 }; | 280 }; |
| 305 | 281 |
| 306 const bool was_rtl = IsRTL(); | 282 const bool was_rtl = IsRTL(); |
| 307 | 283 |
| 284 test::ScopedRestoreICUDefaultLocale restore_locale; |
| 308 for (size_t i = 0; i < 2; ++i) { | 285 for (size_t i = 0; i < 2; ++i) { |
| 309 // Toggle the application default text direction (to try each direction). | 286 // Toggle the application default text direction (to try each direction). |
| 310 SetRTL(!IsRTL()); | 287 SetRTL(!IsRTL()); |
| 311 | 288 |
| 312 string16 empty; | 289 string16 empty; |
| 313 WrapStringWithLTRFormatting(&empty); | 290 WrapStringWithLTRFormatting(&empty); |
| 314 EXPECT_TRUE(empty.empty()); | 291 EXPECT_TRUE(empty.empty()); |
| 315 WrapStringWithRTLFormatting(&empty); | 292 WrapStringWithRTLFormatting(&empty); |
| 316 EXPECT_TRUE(empty.empty()); | 293 EXPECT_TRUE(empty.empty()); |
| 317 | 294 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 345 { L"\x05d0\x05d1\x05d2", true, true }, | 322 { L"\x05d0\x05d1\x05d2", true, true }, |
| 346 { L"\x05d0\x05d1\x05d2.txt", true, true }, | 323 { L"\x05d0\x05d1\x05d2.txt", true, true }, |
| 347 { L"\x05d0" L"abc", true, true }, | 324 { L"\x05d0" L"abc", true, true }, |
| 348 { L"\x05d0" L"abc.txt", true, true }, | 325 { L"\x05d0" L"abc.txt", true, true }, |
| 349 { L"abc\x05d0\x05d1", false, true }, | 326 { L"abc\x05d0\x05d1", false, true }, |
| 350 { L"abc\x05d0\x05d1.jpg", false, true }, | 327 { L"abc\x05d0\x05d1.jpg", false, true }, |
| 351 }; | 328 }; |
| 352 | 329 |
| 353 const bool was_rtl = IsRTL(); | 330 const bool was_rtl = IsRTL(); |
| 354 | 331 |
| 332 test::ScopedRestoreICUDefaultLocale restore_locale; |
| 355 for (size_t i = 0; i < 2; ++i) { | 333 for (size_t i = 0; i < 2; ++i) { |
| 356 // Toggle the application default text direction (to try each direction). | 334 // Toggle the application default text direction (to try each direction). |
| 357 SetRTL(!IsRTL()); | 335 SetRTL(!IsRTL()); |
| 358 for (size_t i = 0; i < arraysize(cases); ++i) { | 336 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 359 string16 input = WideToUTF16(cases[i].path); | 337 string16 input = WideToUTF16(cases[i].path); |
| 360 string16 output = GetDisplayStringInLTRDirectionality(input); | 338 string16 output = GetDisplayStringInLTRDirectionality(input); |
| 361 // Test the expected wrapping behavior for the current UI directionality. | 339 // Test the expected wrapping behavior for the current UI directionality. |
| 362 if (IsRTL() ? cases[i].wrap_rtl : cases[i].wrap_ltr) | 340 if (IsRTL() ? cases[i].wrap_rtl : cases[i].wrap_ltr) |
| 363 EXPECT_NE(output, input); | 341 EXPECT_NE(output, input); |
| 364 else | 342 else |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 L",\x05d0 foo bar", | 387 L",\x05d0 foo bar", |
| 410 L"\x202a \x05d0 foo bar", | 388 L"\x202a \x05d0 foo bar", |
| 411 L"\x202d \x05d0 foo bar", | 389 L"\x202d \x05d0 foo bar", |
| 412 L"\x202b foo \x05d0 bar", | 390 L"\x202b foo \x05d0 bar", |
| 413 L"\x202e foo \x05d0 bar", | 391 L"\x202e foo \x05d0 bar", |
| 414 L"\x0622 foo \x05d0 bar", | 392 L"\x0622 foo \x05d0 bar", |
| 415 }; | 393 }; |
| 416 | 394 |
| 417 const bool was_rtl = IsRTL(); | 395 const bool was_rtl = IsRTL(); |
| 418 | 396 |
| 397 test::ScopedRestoreICUDefaultLocale restore_locale; |
| 419 for (size_t i = 0; i < 2; ++i) { | 398 for (size_t i = 0; i < 2; ++i) { |
| 420 // Toggle the application default text direction (to try each direction). | 399 // Toggle the application default text direction (to try each direction). |
| 421 SetRTL(!IsRTL()); | 400 SetRTL(!IsRTL()); |
| 422 | 401 |
| 423 for (size_t i = 0; i < arraysize(cases); ++i) { | 402 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 424 string16 test_case = WideToUTF16(cases[i]); | 403 string16 test_case = WideToUTF16(cases[i]); |
| 425 string16 adjusted_string = test_case; | 404 string16 adjusted_string = test_case; |
| 426 | 405 |
| 427 if (!AdjustStringForLocaleDirection(&adjusted_string)) | 406 if (!AdjustStringForLocaleDirection(&adjusted_string)) |
| 428 continue; | 407 continue; |
| 429 | 408 |
| 430 EXPECT_NE(test_case, adjusted_string); | 409 EXPECT_NE(test_case, adjusted_string); |
| 431 EXPECT_TRUE(UnadjustStringForLocaleDirection(&adjusted_string)); | 410 EXPECT_TRUE(UnadjustStringForLocaleDirection(&adjusted_string)); |
| 432 EXPECT_EQ(test_case, adjusted_string) << " for test case [" << test_case | 411 EXPECT_EQ(test_case, adjusted_string) << " for test case [" << test_case |
| 433 << "] with IsRTL() == " << IsRTL(); | 412 << "] with IsRTL() == " << IsRTL(); |
| 434 } | 413 } |
| 435 } | 414 } |
| 436 | 415 |
| 437 EXPECT_EQ(was_rtl, IsRTL()); | 416 EXPECT_EQ(was_rtl, IsRTL()); |
| 438 } | 417 } |
| 439 | 418 |
| 440 } // namespace i18n | 419 } // namespace i18n |
| 441 } // namespace base | 420 } // namespace base |
| OLD | NEW |