diff options
author | yusukes@google.com <yusukes@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-10 03:57:38 +0000 |
---|---|---|
committer | yusukes@google.com <yusukes@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-10 03:57:38 +0000 |
commit | 310999f4d5fd41768c77d0f7d508ca57bf5a3d7f (patch) | |
tree | 9a8976801bd07b644635fac4ff9a8330b0d40582 /chrome/browser/chromeos/options | |
parent | 0034f9391f33cdcaaaee7cc8202d799d383910c1 (diff) | |
download | chromium_src-310999f4d5fd41768c77d0f7d508ca57bf5a3d7f.zip chromium_src-310999f4d5fd41768c77d0f7d508ca57bf5a3d7f.tar.gz chromium_src-310999f4d5fd41768c77d0f7d508ca57bf5a3d7f.tar.bz2 |
Sort input methods names in the language selector by language names.
BUG=chromium-os:3867
TEST=manual. ran the unit_test as well.
Review URL: http://codereview.chromium.org/2764001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@49367 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos/options')
3 files changed, 117 insertions, 7 deletions
diff --git a/chrome/browser/chromeos/options/language_config_view.cc b/chrome/browser/chromeos/options/language_config_view.cc index 52bc243..271792b 100644 --- a/chrome/browser/chromeos/options/language_config_view.cc +++ b/chrome/browser/chromeos/options/language_config_view.cc @@ -825,6 +825,11 @@ void LanguageConfigView::SetInputMethodActivated( // Update Chrome's preference. std::vector<std::string> new_input_method_ids(input_method_id_set.begin(), input_method_id_set.end()); + + // Note: Since |new_input_method_ids| is alphabetically sorted and the sort + // function below uses stable sort, the relateve order of input methods that + // belong to the same language (e.g. "mozc" and "xkb:jp::jpn") is maintained. + SortInputMethodIdsByNames(id_to_language_code_map_, &new_input_method_ids); preload_engines_.SetValue(UTF8ToWide(JoinString(new_input_method_ids, ','))); } @@ -940,12 +945,12 @@ std::wstring LanguageConfigView::GetLanguageDisplayNameFromCode( } namespace { + // The comparator is used for sorting language codes by their // corresponding language names, using the ICU collator. -struct CompareByLanguageName : std::binary_function<const std::string&, - const std::string&, - bool> { - CompareByLanguageName(icu::Collator* collator) +struct CompareLanguageCodesByLanguageName + : std::binary_function<const std::string&, const std::string&, bool> { + explicit CompareLanguageCodesByLanguageName(icu::Collator* collator) : collator_(collator) { } @@ -959,8 +964,40 @@ struct CompareByLanguageName : std::binary_function<const std::string&, LanguageConfigView::GetLanguageDisplayNameFromCode(s2); return l10n_util::StringComparator<std::wstring>(collator_)(key1, key2); } + icu::Collator* collator_; }; + +// The comparator is used for sorting input method ids by their +// corresponding language names, using the ICU collator. +struct CompareInputMethodIdsByLanguageName + : std::binary_function<const std::string&, const std::string&, bool> { + CompareInputMethodIdsByLanguageName( + icu::Collator* collator, + const std::map<std::string, std::string>& id_to_language_code_map) + : comparator_(collator), + id_to_language_code_map_(id_to_language_code_map) { + } + + bool operator()(const std::string& s1, const std::string& s2) const { + std::string language_code_1; + std::map<std::string, std::string>::const_iterator iter = + id_to_language_code_map_.find(s1); + if (iter != id_to_language_code_map_.end()) { + language_code_1 = iter->second; + } + std::string language_code_2; + iter = id_to_language_code_map_.find(s2); + if (iter != id_to_language_code_map_.end()) { + language_code_2 = iter->second; + } + return comparator_(language_code_1, language_code_2); + } + + const CompareLanguageCodesByLanguageName comparator_; + const std::map<std::string, std::string>& id_to_language_code_map_; +}; + } // namespace void LanguageConfigView::SortLanguageCodesByNames( @@ -971,10 +1008,26 @@ void LanguageConfigView::SortLanguageCodesByNames( icu::Locale locale(g_browser_process->GetApplicationLocale().c_str()); scoped_ptr<icu::Collator> collator( icu::Collator::createInstance(locale, error)); - if (U_FAILURE(error)) + if (U_FAILURE(error)) { collator.reset(); + } std::sort(language_codes->begin(), language_codes->end(), - CompareByLanguageName(collator.get())); + CompareLanguageCodesByLanguageName(collator.get())); +} + +void LanguageConfigView::SortInputMethodIdsByNames( + const std::map<std::string, std::string>& id_to_language_code_map, + std::vector<std::string>* input_method_ids) { + UErrorCode error = U_ZERO_ERROR; + icu::Locale locale(g_browser_process->GetApplicationLocale().c_str()); + scoped_ptr<icu::Collator> collator( + icu::Collator::createInstance(locale, error)); + if (U_FAILURE(error)) { + collator.reset(); + } + std::stable_sort(input_method_ids->begin(), input_method_ids->end(), + CompareInputMethodIdsByLanguageName( + collator.get(), id_to_language_code_map)); } void LanguageConfigView::ReorderInputMethodIdsForLanguageCode( diff --git a/chrome/browser/chromeos/options/language_config_view.h b/chrome/browser/chromeos/options/language_config_view.h index 0a67fc6..72f636a 100644 --- a/chrome/browser/chromeos/options/language_config_view.h +++ b/chrome/browser/chromeos/options/language_config_view.h @@ -136,10 +136,16 @@ class LanguageConfigView : public TableModel, const std::string& language_code); // Sorts the given language codes by their corresponding language names, - // using the unicode string comparator. + // using the unicode string comparator. Uses unstable sorting. static void SortLanguageCodesByNames( std::vector<std::string>* language_codes); + // Sorts the given input method ids by their corresponding language names, + // using the unicode string comparator. Uses stable sorting. + static void SortInputMethodIdsByNames( + const std::map<std::string, std::string>& id_to_language_code_map, + std::vector<std::string>* input_method_ids); + // Reorders the given input method ids for the language code. For // example, if |language_codes| is "fr" and |input_method_ids| contains // ["xkb:be::fra", and "xkb:fr::fra"], the list is reordered to diff --git a/chrome/browser/chromeos/options/language_config_view_test.cc b/chrome/browser/chromeos/options/language_config_view_test.cc index b44bf43..19522ba 100644 --- a/chrome/browser/chromeos/options/language_config_view_test.cc +++ b/chrome/browser/chromeos/options/language_config_view_test.cc @@ -2,8 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <map> #include <string> +#include <utility> #include <vector> + #include "app/l10n_util.h" #include "base/utf_string_conversions.h" #include "chrome/browser/chromeos/options/language_config_view.h" @@ -29,6 +32,9 @@ TEST(LanguageConfigViewTest, GetLanguageDisplayNameFromCode) { TEST(LanguageConfigViewTest, SortLanguageCodesByNames) { std::vector<std::string> language_codes; + // Check if this function can handle an empty list. + LanguageConfigView::SortLanguageCodesByNames(&language_codes); + language_codes.push_back("ja"); language_codes.push_back("fr"); language_codes.push_back("t"); @@ -48,6 +54,51 @@ TEST(LanguageConfigViewTest, SortLanguageCodesByNames) { ASSERT_EQ("t", language_codes[3]); // Others } +TEST(LanguageConfigViewTest, SortInputMethodIdsByNames) { + std::map<std::string, std::string> id_to_language_code_map; + id_to_language_code_map.insert(std::make_pair("mozc", "ja")); + id_to_language_code_map.insert(std::make_pair("mozc-jp", "ja")); + id_to_language_code_map.insert(std::make_pair("xkb:jp::jpn", "ja")); + id_to_language_code_map.insert(std::make_pair("xkb:fr::fra", "fr")); + id_to_language_code_map.insert(std::make_pair("m17n:latn-pre", "t")); + + std::vector<std::string> input_method_ids; + // Check if this function can handle an empty list. + LanguageConfigView::SortInputMethodIdsByNames(id_to_language_code_map, + &input_method_ids); + + input_method_ids.push_back("mozc"); // Japanese + input_method_ids.push_back("xkb:fr::fra"); // French + input_method_ids.push_back("m17n:latn-pre"); // Others + LanguageConfigView::SortInputMethodIdsByNames(id_to_language_code_map, + &input_method_ids); + ASSERT_EQ(3, static_cast<int>(input_method_ids.size())); + ASSERT_EQ("xkb:fr::fra", input_method_ids[0]); // French + ASSERT_EQ("mozc", input_method_ids[1]); // Japanese + ASSERT_EQ("m17n:latn-pre", input_method_ids[2]); // Others + + // Add a duplicate entry and see if it works. + // Note that SortInputMethodIdsByNames uses std::stable_sort. + input_method_ids.push_back("xkb:jp::jpn"); // also Japanese + LanguageConfigView::SortInputMethodIdsByNames(id_to_language_code_map, + &input_method_ids); + ASSERT_EQ(4, static_cast<int>(input_method_ids.size())); + ASSERT_EQ("xkb:fr::fra", input_method_ids[0]); // French + ASSERT_EQ("mozc", input_method_ids[1]); // Japanese + ASSERT_EQ("xkb:jp::jpn", input_method_ids[2]); // Japanese + ASSERT_EQ("m17n:latn-pre", input_method_ids[3]); // Others + + input_method_ids.push_back("mozc-jp"); // also Japanese + LanguageConfigView::SortInputMethodIdsByNames(id_to_language_code_map, + &input_method_ids); + ASSERT_EQ(5, static_cast<int>(input_method_ids.size())); + ASSERT_EQ("xkb:fr::fra", input_method_ids[0]); // French + ASSERT_EQ("mozc", input_method_ids[1]); // Japanese + ASSERT_EQ("xkb:jp::jpn", input_method_ids[2]); // Japanese + ASSERT_EQ("mozc-jp", input_method_ids[3]); // Japanese + ASSERT_EQ("m17n:latn-pre", input_method_ids[4]); // Others +} + TEST(LanguageConfigViewTest, ReorderInputMethodIdsForLanguageCode_DE) { std::vector<std::string> input_method_ids; input_method_ids.push_back("xkb:ch::ger"); // Switzerland - German |