diff options
author | jeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-05 23:20:02 +0000 |
---|---|---|
committer | jeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-05 23:20:02 +0000 |
commit | 6ee68ee792e95e2460ac1f52162f6762944d5239 (patch) | |
tree | 938e0d7c79687d5eba031392b49a6aa04e44e746 /chrome/common | |
parent | 89b9208ab8d4dd8dacbc1ba39a81943fa7ec5796 (diff) | |
download | chromium_src-6ee68ee792e95e2460ac1f52162f6762944d5239.zip chromium_src-6ee68ee792e95e2460ac1f52162f6762944d5239.tar.gz chromium_src-6ee68ee792e95e2460ac1f52162f6762944d5239.tar.bz2 |
* Port l10n_utiL and it's unit test on POSIX.
* Bringup visit_database_unittest.cc on POSIX
Review URL: http://codereview.chromium.org/17025
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7570 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r-- | chrome/common/common.scons | 2 | ||||
-rw-r--r-- | chrome/common/l10n_util.cc | 89 | ||||
-rw-r--r-- | chrome/common/l10n_util_unittest.cc | 15 |
3 files changed, 86 insertions, 20 deletions
diff --git a/chrome/common/common.scons b/chrome/common/common.scons index cfdc390..e72ed47 100644 --- a/chrome/common/common.scons +++ b/chrome/common/common.scons @@ -58,6 +58,7 @@ input_files.extend([ 'ipc_sync_message.cc', 'jpeg_codec.cc', 'json_value_serializer.cc', + 'l10n_util.cc', 'libxml_utils.cc', 'logging_chrome.cc', 'message_router.cc', @@ -103,7 +104,6 @@ if env.Bit('windows'): 'ipc_logging.cc', 'ipc_sync_channel.cc', 'jstemplate_builder.cc', - 'l10n_util.cc', 'net/url_request_intercept_job.cc', 'os_exchange_data.cc', 'plugin_messages.cc', diff --git a/chrome/common/l10n_util.cc b/chrome/common/l10n_util.cc index 199d937..71b2450 100644 --- a/chrome/common/l10n_util.cc +++ b/chrome/common/l10n_util.cc @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "build/build_config.h" + #include <algorithm> #include "chrome/common/l10n_util.h" @@ -15,13 +17,21 @@ #include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/gfx/chrome_canvas.h" +#if defined(OS_WIN) +// TODO(port): re-enable. #include "chrome/common/resource_bundle.h" #include "chrome/views/view.h" +#endif // defined(OS_WIN) #include "unicode/coll.h" #include "unicode/locid.h" #include "unicode/rbbi.h" #include "unicode/uchar.h" +// TODO(playmobil): remove this undef once SkPostConfig.h is fixed. +// skia/include/corecg/SkPostConfig.h #defines strcasecmp() so we can't use +// base::strcasecmp() without #undefing it here. +#undef strcasecmp + namespace { // Added to the end of strings that are too big in TrucateString. @@ -99,10 +109,24 @@ class StringComparator : public std::binary_function<const std::wstring&, // Returns true if lhs preceeds rhs. bool operator() (const std::wstring& lhs, const std::wstring& rhs) { UErrorCode error = U_ZERO_ERROR; +#if defined(WCHAR_T_IS_UTF32) + // Need to convert to UTF-16 to be compatible with UnicodeString's + // constructor. + string16 lhs_utf16 = WideToUTF16(lhs); + string16 rhs_utf16 = WideToUTF16(rhs); + + UCollationResult result = collator_->compare( + static_cast<const UChar*>(lhs_utf16.c_str()), + static_cast<int>(lhs_utf16.length()), + static_cast<const UChar*>(rhs_utf16.c_str()), + static_cast<int>(rhs_utf16.length()), + error); +#else UCollationResult result = collator_->compare( static_cast<const UChar*>(lhs.c_str()), static_cast<int>(lhs.length()), static_cast<const UChar*>(rhs.c_str()), static_cast<int>(rhs.length()), error); +#endif DCHECK(U_SUCCESS(error)); return result == UCOL_LESS; @@ -127,8 +151,8 @@ bool IsDuplicateName(const std::string& locale_name) { // has to be added manually in GetAvailableLocales(). if (LowerCaseEqualsASCII(locale_name.substr(0, 3), "es_")) return true; - for (int i = 0; i < arraysize(kDuplicateNames); ++i) { - if (_stricmp(kDuplicateNames[i], locale_name.c_str()) == 0) + for (size_t i = 0; i < arraysize(kDuplicateNames); ++i) { + if (base::strcasecmp(kDuplicateNames[i], locale_name.c_str()) == 0) return true; } return false; @@ -195,7 +219,7 @@ bool CheckAndResolveLocale(const std::wstring& locale, {"en", L"en-US"}, }; - for (int i = 0; i < arraysize(alias_map); ++i) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(alias_map); ++i) { if (LowerCaseEqualsASCII(locale, alias_map[i].source)) { std::wstring tmp_locale(alias_map[i].dest); if (IsLocaleAvailable(tmp_locale, locale_path)) { @@ -274,9 +298,17 @@ std::wstring GetLocalName(const std::wstring& locale_code_wstr, const char* locale_code = locale_code_str.c_str(); UErrorCode error = U_ZERO_ERROR; const int buffer_size = 1024; + +#if defined(WCHAR_T_IS_UTF32) + string16 name_local_utf16; + int actual_size = uloc_getDisplayName(locale_code, app_locale.c_str(), + WriteInto(&name_local_utf16, buffer_size + 1), buffer_size, &error); + std::wstring name_local = UTF16ToWide(name_local_utf16); +#else std::wstring name_local; int actual_size = uloc_getDisplayName(locale_code, app_locale.c_str(), WriteInto(&name_local, buffer_size + 1), buffer_size, &error); +#endif DCHECK(U_SUCCESS(error)); name_local.resize(actual_size); // Add an RTL mark so parentheses are properly placed. @@ -286,6 +318,8 @@ std::wstring GetLocalName(const std::wstring& locale_code_wstr, return name_local; } +#if defined(OS_WIN) +// TODO(port): re-enable. std::wstring GetString(int message_id) { ResourceBundle &rb = ResourceBundle::GetSharedInstance(); return rb.GetLocalizedString(message_id); @@ -348,6 +382,7 @@ std::wstring GetStringF(int message_id, int a) { std::wstring GetStringF(int message_id, int64 a) { return GetStringF(message_id, Int64ToWString(a)); } +#endif // defined(OS_WIN) std::wstring TruncateString(const std::wstring& string, size_t length) { if (string.size() <= length) @@ -365,20 +400,25 @@ std::wstring TruncateString(const std::wstring& string, size_t length) { return kElideString; } +#if defined(WCHAR_T_IS_UTF32) + const string16 string_utf16 = WideToUTF16(string); +#else + const std::wstring &string_utf16 = string; +#endif // Use a line iterator to find the first boundary. UErrorCode status = U_ZERO_ERROR; scoped_ptr<RuleBasedBreakIterator> bi(static_cast<RuleBasedBreakIterator*>( RuleBasedBreakIterator::createLineInstance(Locale::getDefault(), status))); if (U_FAILURE(status)) return string.substr(0, max) + kElideString; - bi->setText(string.c_str()); + bi->setText(string_utf16.c_str()); int32_t index = bi->preceding(static_cast<int32_t>(max)); if (index == BreakIterator::DONE) { index = static_cast<int32_t>(max); } else { // Found a valid break (may be the beginning of the string). Now use // a character iterator to find the previous non-whitespace character. - StringCharacterIterator char_iterator(string.c_str()); + StringCharacterIterator char_iterator(string_utf16.c_str()); if (index == 0) { // No valid line breaks. Start at the end again. This ensures we break // on a valid character boundary. @@ -407,6 +447,18 @@ std::wstring TruncateString(const std::wstring& string, size_t length) { return string.substr(0, index) + kElideString; } +#if defined(WCHAR_T_IS_UTF32) +std::wstring ToLower(const std::wstring& string) { + string16 string_utf16 = WideToUTF16(string); + UnicodeString lower_u_str( + UnicodeString(string_utf16.c_str()).toLower(Locale::getDefault())); + string16 result_utf16; + lower_u_str.extract(0, lower_u_str.length(), + WriteInto(&result_utf16, lower_u_str.length() + 1)); + std::wstring result = UTF16ToWide(result_utf16); + return result; +} +#else std::wstring ToLower(const std::wstring& string) { UnicodeString lower_u_str( UnicodeString(string.c_str()).toLower(Locale::getDefault())); @@ -415,6 +467,7 @@ std::wstring ToLower(const std::wstring& string) { WriteInto(&result, lower_u_str.length() + 1)); return result; } +#endif // defined(WCHAR_T_IS_UTF32) // Returns the text direction. // This function retrieves the language corresponding to the default ICU locale @@ -497,7 +550,15 @@ void WrapStringWithRTLFormatting(std::wstring* text) { text->append(L"\x202C"); } -// Returns locale-dependent externded window styles. +int DefaultCanvasTextAlignment() { + if (GetTextDirection() == LEFT_TO_RIGHT) { + return ChromeCanvas::TEXT_ALIGN_LEFT; + } else { + return ChromeCanvas::TEXT_ALIGN_RIGHT; + } +} + +#if defined(OS_WIN) int GetExtendedStyles() { return GetTextDirection() == LEFT_TO_RIGHT ? 0 : WS_EX_LAYOUTRTL | WS_EX_RTLREADING; @@ -507,14 +568,6 @@ int GetExtendedTooltipStyles() { return GetTextDirection() == LEFT_TO_RIGHT ? 0 : WS_EX_LAYOUTRTL; } -int DefaultCanvasTextAlignment() { - if (GetTextDirection() == LEFT_TO_RIGHT) { - return ChromeCanvas::TEXT_ALIGN_LEFT; - } else { - return ChromeCanvas::TEXT_ALIGN_RIGHT; - } -} - void HWNDSetRTLLayout(HWND hwnd) { DWORD ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE); @@ -529,6 +582,7 @@ void HWNDSetRTLLayout(HWND hwnd) { ::InvalidateRect(hwnd, NULL, true); } } +#endif // defined(OS_WIN) void SortStrings(const std::wstring& locale, std::vector<std::wstring>* strings) { @@ -589,7 +643,12 @@ UBool BiDiLineIterator::Open(const std::wstring& text, return false; if (right_to_left && url) ubidi_setReorderingMode(bidi_, UBIDI_REORDER_RUNS_ONLY); - ubidi_setPara(bidi_, text.data(), static_cast<int>(text.length()), +#if defined(WCHAR_T_IS_UTF32) + const string16 text_utf16 = WideToUTF16(text); +#else + const std::wstring &text_utf16 = text; +#endif // U_SIZEOF_WCHAR_T != 4 + ubidi_setPara(bidi_, text_utf16.data(), static_cast<int>(text_utf16.length()), right_to_left ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR, NULL, &error); return U_SUCCESS(error); diff --git a/chrome/common/l10n_util_unittest.cc b/chrome/common/l10n_util_unittest.cc index ee14f23..18cb8f0 100644 --- a/chrome/common/l10n_util_unittest.cc +++ b/chrome/common/l10n_util_unittest.cc @@ -2,21 +2,27 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "build/build_config.h" + #include "base/file_util.h" #include "base/path_service.h" #include "base/string_util.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/l10n_util.h" +#if !defined(OS_MACOSX) #include "chrome/test/data/resource.h" +#endif #include "testing/gtest/include/gtest/gtest.h" +#include "testing/platform_test.h" #include "unicode/locid.h" namespace { -class L10nUtilTest: public testing::Test { +class L10nUtilTest: public PlatformTest { }; -TEST(L10nUtilTest, GetString) { +#if defined(OS_WIN) +TEST_F(L10nUtilTest, GetString) { std::wstring s = l10n_util::GetString(IDS_SIMPLE); EXPECT_EQ(std::wstring(L"Hello World!"), s); @@ -26,8 +32,9 @@ TEST(L10nUtilTest, GetString) { s = l10n_util::GetStringF(IDS_PLACEHOLDERS_2, 20); EXPECT_EQ(std::wstring(L"You owe me $20."), s); } +#endif // defined(OS_WIN) -TEST(L10nUtilTest, TruncateString) { +TEST_F(L10nUtilTest, TruncateString) { std::wstring string(L"foooooey bxxxar baz"); // Make sure it doesn't modify the string if length > string length. @@ -61,7 +68,7 @@ void SetICUDefaultLocale(const std::wstring& locale_string) { EXPECT_TRUE(U_SUCCESS(error_code)); } -TEST(L10nUtilTest, GetAppLocale) { +TEST_F(L10nUtilTest, GetAppLocale) { // Use a temporary locale dir so we don't have to actually build the locale // dlls for this test. std::wstring orig_locale_dir; |