| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 7 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 8 #include <cstdlib> | 8 #include <cstdlib> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 public: | 32 public: |
| 33 explicit StringWrapper(const std::wstring& string) : string_(string) {} | 33 explicit StringWrapper(const std::wstring& string) : string_(string) {} |
| 34 const std::wstring& string() const { return string_; } | 34 const std::wstring& string() const { return string_; } |
| 35 | 35 |
| 36 private: | 36 private: |
| 37 std::wstring string_; | 37 std::wstring string_; |
| 38 | 38 |
| 39 DISALLOW_COPY_AND_ASSIGN(StringWrapper); | 39 DISALLOW_COPY_AND_ASSIGN(StringWrapper); |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 l10n_util::TextDirection GetTextDirection(const char* locale_name) { | |
| 43 return l10n_util::GetTextDirectionForLocale(locale_name); | |
| 44 } | |
| 45 | |
| 46 } // namespace | 42 } // namespace |
| 47 | 43 |
| 48 class L10nUtilTest : public PlatformTest { | 44 class L10nUtilTest : public PlatformTest { |
| 49 }; | 45 }; |
| 50 | 46 |
| 51 #if defined(OS_WIN) | 47 #if defined(OS_WIN) |
| 52 // TODO(beng): disabled until app strings move to app. | 48 // TODO(beng): disabled until app strings move to app. |
| 53 TEST_F(L10nUtilTest, DISABLED_GetString) { | 49 TEST_F(L10nUtilTest, DISABLED_GetString) { |
| 54 std::wstring s = l10n_util::GetString(IDS_SIMPLE); | 50 std::wstring s = l10n_util::GetString(IDS_SIMPLE); |
| 55 EXPECT_EQ(std::wstring(L"Hello World!"), s); | 51 EXPECT_EQ(std::wstring(L"Hello World!"), s); |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 strings.push_back(new StringWrapper(L"b")); | 263 strings.push_back(new StringWrapper(L"b")); |
| 268 strings.push_back(new StringWrapper(L"a")); | 264 strings.push_back(new StringWrapper(L"a")); |
| 269 l10n_util::SortStringsUsingMethod(L"en-US", &strings, &StringWrapper::string); | 265 l10n_util::SortStringsUsingMethod(L"en-US", &strings, &StringWrapper::string); |
| 270 ASSERT_TRUE(L"a" == strings[0]->string()); | 266 ASSERT_TRUE(L"a" == strings[0]->string()); |
| 271 ASSERT_TRUE(L"b" == strings[1]->string()); | 267 ASSERT_TRUE(L"b" == strings[1]->string()); |
| 272 ASSERT_TRUE(L"C" == strings[2]->string()); | 268 ASSERT_TRUE(L"C" == strings[2]->string()); |
| 273 ASSERT_TRUE(L"d" == strings[3]->string()); | 269 ASSERT_TRUE(L"d" == strings[3]->string()); |
| 274 STLDeleteElements(&strings); | 270 STLDeleteElements(&strings); |
| 275 } | 271 } |
| 276 | 272 |
| 277 TEST_F(L10nUtilTest, GetFirstStrongCharacterDirection) { | |
| 278 // Test pure LTR string. | |
| 279 std::wstring string(L"foo bar"); | |
| 280 EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, | |
| 281 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 282 | |
| 283 // Test bidi string in which the first character with strong directionality | |
| 284 // is a character with type L. | |
| 285 string.assign(L"foo \x05d0 bar"); | |
| 286 EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, | |
| 287 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 288 | |
| 289 // Test bidi string in which the first character with strong directionality | |
| 290 // is a character with type R. | |
| 291 string.assign(L"\x05d0 foo bar"); | |
| 292 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, | |
| 293 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 294 | |
| 295 // Test bidi string which starts with a character with weak directionality | |
| 296 // and in which the first character with strong directionality is a character | |
| 297 // with type L. | |
| 298 string.assign(L"!foo \x05d0 bar"); | |
| 299 EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, | |
| 300 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 301 | |
| 302 // Test bidi string which starts with a character with weak directionality | |
| 303 // and in which the first character with strong directionality is a character | |
| 304 // with type R. | |
| 305 string.assign(L",\x05d0 foo bar"); | |
| 306 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, | |
| 307 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 308 | |
| 309 // Test bidi string in which the first character with strong directionality | |
| 310 // is a character with type LRE. | |
| 311 string.assign(L"\x202a \x05d0 foo bar"); | |
| 312 EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, | |
| 313 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 314 | |
| 315 // Test bidi string in which the first character with strong directionality | |
| 316 // is a character with type LRO. | |
| 317 string.assign(L"\x202d \x05d0 foo bar"); | |
| 318 EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, | |
| 319 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 320 | |
| 321 // Test bidi string in which the first character with strong directionality | |
| 322 // is a character with type RLE. | |
| 323 string.assign(L"\x202b foo \x05d0 bar"); | |
| 324 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, | |
| 325 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 326 | |
| 327 // Test bidi string in which the first character with strong directionality | |
| 328 // is a character with type RLO. | |
| 329 string.assign(L"\x202e foo \x05d0 bar"); | |
| 330 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, | |
| 331 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 332 | |
| 333 // Test bidi string in which the first character with strong directionality | |
| 334 // is a character with type AL. | |
| 335 string.assign(L"\x0622 foo \x05d0 bar"); | |
| 336 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, | |
| 337 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 338 | |
| 339 // Test a string without strong directionality characters. | |
| 340 string.assign(L",!.{}"); | |
| 341 EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, | |
| 342 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 343 | |
| 344 // Test empty string. | |
| 345 string.assign(L""); | |
| 346 EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, | |
| 347 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 348 | |
| 349 // Test characters in non-BMP (e.g. Phoenician letters. Please refer to | |
| 350 // http://demo.icu-project.org/icu-bin/ubrowse?scr=151&b=10910 for more | |
| 351 // information). | |
| 352 #if defined(WCHAR_T_IS_UTF32) | |
| 353 string.assign(L" ! \x10910" L"abc 123"); | |
| 354 #elif defined(WCHAR_T_IS_UTF16) | |
| 355 string.assign(L" ! \xd802\xdd10" L"abc 123"); | |
| 356 #else | |
| 357 #error wchar_t should be either UTF-16 or UTF-32 | |
| 358 #endif | |
| 359 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, | |
| 360 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 361 | |
| 362 #if defined(WCHAR_T_IS_UTF32) | |
| 363 string.assign(L" ! \x10401" L"abc 123"); | |
| 364 #elif defined(WCHAR_T_IS_UTF16) | |
| 365 string.assign(L" ! \xd801\xdc01" L"abc 123"); | |
| 366 #else | |
| 367 #error wchar_t should be either UTF-16 or UTF-32 | |
| 368 #endif | |
| 369 EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, | |
| 370 l10n_util::GetFirstStrongCharacterDirection(string)); | |
| 371 } | |
| 372 | |
| 373 typedef struct { | |
| 374 std::wstring path; | |
| 375 std::wstring wrapped_path; | |
| 376 } PathAndWrappedPath; | |
| 377 | |
| 378 TEST_F(L10nUtilTest, WrapPathWithLTRFormatting) { | |
| 379 std::wstring kSeparator; | |
| 380 kSeparator.push_back(static_cast<wchar_t>(FilePath::kSeparators[0])); | |
| 381 const PathAndWrappedPath test_data[] = { | |
| 382 // Test common path, such as "c:\foo\bar". | |
| 383 { L"c:" + kSeparator + L"foo" + kSeparator + L"bar", | |
| 384 L"\x202a"L"c:" + kSeparator + L"foo" + kSeparator + | |
| 385 L"bar\x202c" | |
| 386 }, | |
| 387 // Test path with file name, such as "c:\foo\bar\test.jpg". | |
| 388 { L"c:" + kSeparator + L"foo" + kSeparator + L"bar" + kSeparator + | |
| 389 L"test.jpg", | |
| 390 L"\x202a"L"c:" + kSeparator + L"foo" + kSeparator + | |
| 391 L"bar" + kSeparator + L"test.jpg\x202c" | |
| 392 }, | |
| 393 // Test path ending with punctuation, such as "c:\(foo)\bar.". | |
| 394 { L"c:" + kSeparator + L"(foo)" + kSeparator + L"bar.", | |
| 395 L"\x202a"L"c:" + kSeparator + L"(foo)" + kSeparator + | |
| 396 L"bar.\x202c" | |
| 397 }, | |
| 398 // Test path ending with separator, such as "c:\foo\bar\". | |
| 399 { L"c:" + kSeparator + L"foo" + kSeparator + L"bar" + kSeparator, | |
| 400 L"\x202a"L"c:" + kSeparator + L"foo" + kSeparator + | |
| 401 L"bar" + kSeparator + L"\x202c", | |
| 402 }, | |
| 403 // Test path with RTL character. | |
| 404 { L"c:" + kSeparator + L"\x05d0", | |
| 405 L"\x202a"L"c:" + kSeparator + L"\x05d0\x202c", | |
| 406 }, | |
| 407 // Test path with 2 level RTL directory names. | |
| 408 { L"c:" + kSeparator + L"\x05d0" + kSeparator + L"\x0622", | |
| 409 L"\x202a"L"c:" + kSeparator + L"\x05d0" + kSeparator + | |
| 410 L"\x0622\x202c", | |
| 411 }, | |
| 412 // Test path with mixed RTL/LTR directory names and ending with punctuation. | |
| 413 { L"c:" + kSeparator + L"\x05d0" + kSeparator + L"\x0622" + kSeparator + | |
| 414 L"(foo)" + kSeparator + L"b.a.r.", | |
| 415 L"\x202a"L"c:" + kSeparator + L"\x05d0" + kSeparator + | |
| 416 L"\x0622" + kSeparator + L"(foo)" + kSeparator + | |
| 417 L"b.a.r.\x202c", | |
| 418 }, | |
| 419 // Test path without driver name, such as "/foo/bar/test/jpg". | |
| 420 { kSeparator + L"foo" + kSeparator + L"bar" + kSeparator + L"test.jpg", | |
| 421 L"\x202a" + kSeparator + L"foo" + kSeparator + L"bar" + | |
| 422 kSeparator + L"test.jpg" + L"\x202c" | |
| 423 }, | |
| 424 // Test path start with current directory, such as "./foo". | |
| 425 { L"." + kSeparator + L"foo", | |
| 426 L"\x202a"L"." + kSeparator + L"foo" + L"\x202c" | |
| 427 }, | |
| 428 // Test path start with parent directory, such as "../foo/bar.jpg". | |
| 429 { L".." + kSeparator + L"foo" + kSeparator + L"bar.jpg", | |
| 430 L"\x202a"L".." + kSeparator + L"foo" + kSeparator + | |
| 431 L"bar.jpg" + L"\x202c" | |
| 432 }, | |
| 433 // Test absolute path, such as "//foo/bar.jpg". | |
| 434 { kSeparator + kSeparator + L"foo" + kSeparator + L"bar.jpg", | |
| 435 L"\x202a" + kSeparator + kSeparator + L"foo" + kSeparator + | |
| 436 L"bar.jpg" + L"\x202c" | |
| 437 }, | |
| 438 // Test path with mixed RTL/LTR directory names. | |
| 439 { L"c:" + kSeparator + L"foo" + kSeparator + L"\x05d0" + kSeparator + | |
| 440 L"\x0622" + kSeparator + L"\x05d1.jpg", | |
| 441 L"\x202a"L"c:" + kSeparator + L"foo" + kSeparator + L"\x05d0" + | |
| 442 kSeparator + L"\x0622" + kSeparator + L"\x05d1.jpg" + L"\x202c", | |
| 443 }, | |
| 444 // Test empty path. | |
| 445 { L"", | |
| 446 L"\x202a\x202c" | |
| 447 } | |
| 448 }; | |
| 449 for (unsigned int i = 0; i < arraysize(test_data); ++i) { | |
| 450 string16 localized_file_path_string; | |
| 451 FilePath path = FilePath::FromWStringHack(test_data[i].path); | |
| 452 l10n_util::WrapPathWithLTRFormatting(path, &localized_file_path_string); | |
| 453 std::wstring wrapped_path = UTF16ToWide(localized_file_path_string); | |
| 454 EXPECT_EQ(wrapped_path, test_data[i].wrapped_path); | |
| 455 } | |
| 456 } | |
| 457 | |
| 458 typedef struct { | |
| 459 std::wstring raw_filename; | |
| 460 std::wstring display_string; | |
| 461 } StringAndLTRString; | |
| 462 | |
| 463 TEST_F(L10nUtilTest, GetDisplayStringInLTRDirectionality) { | |
| 464 const StringAndLTRString test_data[] = { | |
| 465 { L"test", L"\x202atest\x202c" }, | |
| 466 { L"test.html", L"\x202atest.html\x202c" }, | |
| 467 { L"\x05d0\x05d1\x05d2", L"\x202a\x05d0\x05d1\x05d2\x202c" }, | |
| 468 { L"\x05d0\x05d1\x05d2.txt", L"\x202a\x05d0\x05d1\x05d2.txt\x202c" }, | |
| 469 { L"\x05d0"L"abc", L"\x202a\x05d0"L"abc\x202c" }, | |
| 470 { L"\x05d0"L"abc.txt", L"\x202a\x05d0"L"abc.txt\x202c" }, | |
| 471 { L"abc\x05d0\x05d1", L"\x202a"L"abc\x05d0\x05d1\x202c" }, | |
| 472 { L"abc\x05d0\x05d1.jpg", L"\x202a"L"abc\x05d0\x05d1.jpg\x202c" }, | |
| 473 }; | |
| 474 for (unsigned int i = 0; i < arraysize(test_data); ++i) { | |
| 475 std::wstring input = test_data[i].raw_filename; | |
| 476 std::wstring expected = | |
| 477 l10n_util::GetDisplayStringInLTRDirectionality(&input); | |
| 478 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) | |
| 479 EXPECT_EQ(test_data[i].display_string, expected); | |
| 480 else | |
| 481 EXPECT_EQ(input, expected); | |
| 482 } | |
| 483 } | |
| 484 | |
| 485 TEST_F(L10nUtilTest, GetTextDirection) { | |
| 486 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("ar")); | |
| 487 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("ar_EG")); | |
| 488 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("he")); | |
| 489 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("he_IL")); | |
| 490 // iw is an obsolete code for Hebrew. | |
| 491 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("iw")); | |
| 492 // Although we're not yet localized to Farsi and Urdu, we | |
| 493 // do have the text layout direction information for them. | |
| 494 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("fa")); | |
| 495 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("ur")); | |
| 496 #if 0 | |
| 497 // Enable these when we include the minimal locale data for Azerbaijani | |
| 498 // written in Arabic and Dhivehi. At the moment, our copy of | |
| 499 // ICU data does not have entries for them. | |
| 500 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("az_Arab")); | |
| 501 // Dhivehi that uses Thaana script. | |
| 502 EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("dv")); | |
| 503 #endif | |
| 504 EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("en")); | |
| 505 // Chinese in China with '-'. | |
| 506 EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("zh-CN")); | |
| 507 // Filipino : 3-letter code | |
| 508 EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("fil")); | |
| 509 // Russian | |
| 510 EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("ru")); | |
| 511 // Japanese that uses multiple scripts | |
| 512 EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("ja")); | |
| 513 } | |
| 514 | |
| 515 // Test upper and lower case string conversion. | 273 // Test upper and lower case string conversion. |
| 516 TEST_F(L10nUtilTest, UpperLower) { | 274 TEST_F(L10nUtilTest, UpperLower) { |
| 517 string16 mixed(ASCIIToUTF16("Text with UPPer & lowER casE.")); | 275 string16 mixed(ASCIIToUTF16("Text with UPPer & lowER casE.")); |
| 518 const string16 expected_lower(ASCIIToUTF16("text with upper & lower case.")); | 276 const string16 expected_lower(ASCIIToUTF16("text with upper & lower case.")); |
| 519 const string16 expected_upper(ASCIIToUTF16("TEXT WITH UPPER & LOWER CASE.")); | 277 const string16 expected_upper(ASCIIToUTF16("TEXT WITH UPPER & LOWER CASE.")); |
| 520 | 278 |
| 521 string16 result = l10n_util::ToLower(mixed); | 279 string16 result = l10n_util::ToLower(mixed); |
| 522 EXPECT_EQ(result, expected_lower); | 280 EXPECT_EQ(result, expected_lower); |
| 523 | 281 |
| 524 result = l10n_util::ToUpper(mixed); | 282 result = l10n_util::ToUpper(mixed); |
| 525 EXPECT_EQ(result, expected_upper); | 283 EXPECT_EQ(result, expected_upper); |
| 526 } | 284 } |
| 527 | 285 |
| 528 TEST_F(L10nUtilTest, LocaleDisplayName) { | 286 TEST_F(L10nUtilTest, LocaleDisplayName) { |
| 529 // TODO(jungshik): Make this test more extensive. | 287 // TODO(jungshik): Make this test more extensive. |
| 530 // Test zh-CN and zh-TW are treated as zh-Hans and zh-Hant. | 288 // Test zh-CN and zh-TW are treated as zh-Hans and zh-Hant. |
| 531 string16 result = l10n_util::GetDisplayNameForLocale("zh-CN", "en", false); | 289 string16 result = l10n_util::GetDisplayNameForLocale("zh-CN", "en", false); |
| 532 EXPECT_EQ(result, ASCIIToUTF16("Chinese (Simplified Han)")); | 290 EXPECT_EQ(result, ASCIIToUTF16("Chinese (Simplified Han)")); |
| 533 | 291 |
| 534 result = l10n_util::GetDisplayNameForLocale("zh-TW", "en", false); | 292 result = l10n_util::GetDisplayNameForLocale("zh-TW", "en", false); |
| 535 EXPECT_EQ(result, ASCIIToUTF16("Chinese (Traditional Han)")); | 293 EXPECT_EQ(result, ASCIIToUTF16("Chinese (Traditional Han)")); |
| 536 | 294 |
| 537 result = l10n_util::GetDisplayNameForLocale("pt-BR", "en", false); | 295 result = l10n_util::GetDisplayNameForLocale("pt-BR", "en", false); |
| 538 EXPECT_EQ(result, ASCIIToUTF16("Portuguese (Brazil)")); | 296 EXPECT_EQ(result, ASCIIToUTF16("Portuguese (Brazil)")); |
| 539 | 297 |
| 540 result = l10n_util::GetDisplayNameForLocale("es-419", "en", false); | 298 result = l10n_util::GetDisplayNameForLocale("es-419", "en", false); |
| 541 EXPECT_EQ(result, ASCIIToUTF16("Spanish (Latin America and the Caribbean)")); | 299 EXPECT_EQ(result, ASCIIToUTF16("Spanish (Latin America and the Caribbean)")); |
| 542 } | 300 } |
| OLD | NEW |