summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/options
diff options
context:
space:
mode:
authorsatorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-07 05:58:37 +0000
committersatorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-07 05:58:37 +0000
commit39244d9354fdd180f0b5f017686a7f99f599bb3d (patch)
tree2d07556f659dee7fb47376e22d3a645e8a9d49f1 /chrome/browser/chromeos/options
parentdf48596b2bf82edb4d628ea83a0bb578cef07469 (diff)
downloadchromium_src-39244d9354fdd180f0b5f017686a7f99f599bb3d.zip
chromium_src-39244d9354fdd180f0b5f017686a7f99f599bb3d.tar.gz
chromium_src-39244d9354fdd180f0b5f017686a7f99f599bb3d.tar.bz2
Reorder input methods based on language code.
For instance, if the language code is "fr" (French), we should list French keyboard first rather than Belgian keyboard. BUG=chromium-os:3822 TEST=wrote unit tests. also tested on the netbook. Review URL: http://codereview.chromium.org/2692002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@49037 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos/options')
-rw-r--r--chrome/browser/chromeos/options/language_config_view.cc71
-rw-r--r--chrome/browser/chromeos/options/language_config_view.h15
-rw-r--r--chrome/browser/chromeos/options/language_config_view_test.cc58
3 files changed, 132 insertions, 12 deletions
diff --git a/chrome/browser/chromeos/options/language_config_view.cc b/chrome/browser/chromeos/options/language_config_view.cc
index f63f469..52bc243 100644
--- a/chrome/browser/chromeos/options/language_config_view.cc
+++ b/chrome/browser/chromeos/options/language_config_view.cc
@@ -61,6 +61,20 @@ const struct ExtraLanguage {
{ "es-419", "xkb:es::spa" },
};
+// The list defines pairs of language code and the default input method
+// id. The list is used for reordering input method ids.
+//
+// TODO(satorux): We may need to handle secondary, and ternary input
+// methods, rather than handling the default input method only.
+const struct LanguageDefaultInputMethodId {
+ const char* language_code;
+ const char* input_method_id;
+} kLanguageDefaultInputMethodIds[] = {
+ { "en-US", "xkb:us::eng", }, // US - English
+ { "fr", "xkb:fr::fra", }, // France - French
+ { "de", "xkb:de::ger", }, // Germany - German
+};
+
// The width of the preferred language table shown on the left side.
const int kPreferredLanguageTableWidth = 300;
@@ -360,12 +374,12 @@ void LanguageConfigView::AddInputMethodSection(
// Add input method names and configuration buttons.
input_method_checkboxes_.clear();
- std::pair<LanguageCodeToIdsMap::const_iterator,
- LanguageCodeToIdsMap::const_iterator> range =
- language_code_to_ids_map_.equal_range(language_code);
- for (LanguageCodeToIdsMap::const_iterator iter = range.first;
- iter != range.second; ++iter) {
- const std::string& input_method_id = iter->second;
+ // Get the list of input method ids associated with the language code.
+ std::vector<std::string> input_method_ids;
+ GetInputMethodIdsFromLanguageCode(language_code, &input_method_ids);
+
+ for (size_t i = 0; i < input_method_ids.size(); ++i) {
+ const std::string& input_method_id = input_method_ids[i];
const std::string display_name = GetInputMethodDisplayNameFromId(
input_method_id);
layout->StartRow(0, kPerLanguageDoubleColumnSetId);
@@ -682,12 +696,10 @@ void LanguageConfigView::OnAddLanguage(const std::string& language_code) {
// Activate the first input language associated with the language. We have
// to call this before the OnItemsAdded() call below so the checkbox
// for the first input language gets checked.
- for (size_t i = 0; i < supported_input_method_ids_.size(); ++i) {
- if (GetLanguageCodeFromInputMethodId(supported_input_method_ids_[i]) ==
- language_code) {
- SetInputMethodActivated(supported_input_method_ids_[i], true);
- break;
- }
+ std::vector<std::string> input_method_ids;
+ GetInputMethodIdsFromLanguageCode(language_code, &input_method_ids);
+ if (!input_method_ids.empty()) {
+ SetInputMethodActivated(input_method_ids[0], true);
}
// Append the language to the list of language codes.
@@ -874,6 +886,23 @@ std::string LanguageConfigView::GetInputMethodDisplayNameFromId(
kDefaultDisplayName : iter->second;
}
+void LanguageConfigView::GetInputMethodIdsFromLanguageCode(
+ const std::string& language_code,
+ std::vector<std::string>* input_method_ids) const {
+ DCHECK(input_method_ids);
+ input_method_ids->clear();
+
+ std::pair<LanguageCodeToIdsMap::const_iterator,
+ LanguageCodeToIdsMap::const_iterator> range =
+ language_code_to_ids_map_.equal_range(language_code);
+ for (LanguageCodeToIdsMap::const_iterator iter = range.first;
+ iter != range.second; ++iter) {
+ input_method_ids->push_back(iter->second);
+ }
+ // Reorder the input methods.
+ ReorderInputMethodIdsForLanguageCode(language_code, input_method_ids);
+}
+
void LanguageConfigView::NotifyPrefChanged() {
std::vector<std::string> input_method_ids;
GetActiveInputMethodIds(&input_method_ids);
@@ -948,4 +977,22 @@ void LanguageConfigView::SortLanguageCodesByNames(
CompareByLanguageName(collator.get()));
}
+void LanguageConfigView::ReorderInputMethodIdsForLanguageCode(
+ const std::string& language_code,
+ std::vector<std::string>* input_method_ids) {
+ for (size_t i = 0; i < arraysize(kLanguageDefaultInputMethodIds); ++i) {
+ if (language_code == kLanguageDefaultInputMethodIds[i].language_code) {
+ std::vector<std::string>::iterator iter =
+ std::find(input_method_ids->begin(), input_method_ids->end(),
+ kLanguageDefaultInputMethodIds[i].input_method_id);
+ // If it's not on the top of |input_method_id|, swap it with the top one.
+ if (iter != input_method_ids->end() &&
+ iter != input_method_ids->begin()) {
+ std::swap(*input_method_ids->begin(), *iter);
+ }
+ break; // Don't have to check other language codes.
+ }
+ }
+}
+
} // namespace chromeos
diff --git a/chrome/browser/chromeos/options/language_config_view.h b/chrome/browser/chromeos/options/language_config_view.h
index b3c37d0..0a67fc6 100644
--- a/chrome/browser/chromeos/options/language_config_view.h
+++ b/chrome/browser/chromeos/options/language_config_view.h
@@ -140,6 +140,15 @@ class LanguageConfigView : public TableModel,
static void SortLanguageCodesByNames(
std::vector<std::string>* language_codes);
+ // 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
+ // ["xkb:fr::fra", and "xkb:be::fra"], so that French keyboard layout
+ // comes before Belgian keyboard layout.
+ static void ReorderInputMethodIdsForLanguageCode(
+ const std::string& language_code,
+ std::vector<std::string>* input_method_ids);
+
// Shows the language config dialog in a new window.
static void Show(Profile* profile, gfx::NativeWindow parent);
@@ -210,6 +219,12 @@ class LanguageConfigView : public TableModel,
std::string GetInputMethodDisplayNameFromId(
const std::string& input_method_id) const;
+ // Gets the list of input method ids associated with the given language
+ // code. The original contents of |input_method_ids| will be lost.
+ void GetInputMethodIdsFromLanguageCode(
+ const std::string& language_code,
+ std::vector<std::string>* input_method_ids) const;
+
// Callback for |preload_engines_| pref updates. Initializes the preferred
// language codes based on the updated pref value.
void NotifyPrefChanged();
diff --git a/chrome/browser/chromeos/options/language_config_view_test.cc b/chrome/browser/chromeos/options/language_config_view_test.cc
index 27e87e5..b44bf43 100644
--- a/chrome/browser/chromeos/options/language_config_view_test.cc
+++ b/chrome/browser/chromeos/options/language_config_view_test.cc
@@ -48,6 +48,64 @@ TEST(LanguageConfigViewTest, SortLanguageCodesByNames) {
ASSERT_EQ("t", language_codes[3]); // Others
}
+TEST(LanguageConfigViewTest, ReorderInputMethodIdsForLanguageCode_DE) {
+ std::vector<std::string> input_method_ids;
+ input_method_ids.push_back("xkb:ch::ger"); // Switzerland - German
+ input_method_ids.push_back("xkb:de::ger"); // Germany - German
+ LanguageConfigView::ReorderInputMethodIdsForLanguageCode(
+ "de", &input_method_ids);
+ // The list should be reordered.
+ ASSERT_EQ(2, static_cast<int>(input_method_ids.size()));
+ EXPECT_EQ("xkb:de::ger", input_method_ids[0]);
+ EXPECT_EQ("xkb:ch::ger", input_method_ids[1]);
+}
+
+TEST(LanguageConfigViewTest, ReorderInputMethodIdsForLanguageCode_FR) {
+ std::vector<std::string> input_method_ids;
+ input_method_ids.push_back("xkb:be::fra"); // Belgium - French
+ input_method_ids.push_back("xkb:fr::fra"); // France - French
+ LanguageConfigView::ReorderInputMethodIdsForLanguageCode(
+ "fr", &input_method_ids);
+ // The list should be reordered.
+ ASSERT_EQ(2, static_cast<int>(input_method_ids.size()));
+ EXPECT_EQ("xkb:fr::fra", input_method_ids[0]);
+ EXPECT_EQ("xkb:be::fra", input_method_ids[1]);
+}
+
+TEST(LanguageConfigViewTest, ReorderInputMethodIdsForLanguageCode_EN_US) {
+ std::vector<std::string> input_method_ids;
+ input_method_ids.push_back("xkb:us:dvorak:eng"); // US - Dvorak - English
+ input_method_ids.push_back("xkb:us::eng"); // US - English
+ LanguageConfigView::ReorderInputMethodIdsForLanguageCode(
+ "en-US", &input_method_ids);
+ // The list should be reordered.
+ ASSERT_EQ(2, static_cast<int>(input_method_ids.size()));
+ EXPECT_EQ("xkb:us::eng", input_method_ids[0]);
+ EXPECT_EQ("xkb:us:dvorak:eng", input_method_ids[1]);
+}
+
+TEST(LanguageConfigViewTest, ReorderInputMethodIdsForLanguageCode_FI) {
+ std::vector<std::string> input_method_ids;
+ input_method_ids.push_back("xkb:fi::fin"); // Finland - Finnish
+ LanguageConfigView::ReorderInputMethodIdsForLanguageCode(
+ "fi", &input_method_ids);
+ // There is no rule for reordering for Finnish.
+ ASSERT_EQ(1, static_cast<int>(input_method_ids.size()));
+ EXPECT_EQ("xkb:fi::fin", input_method_ids[0]);
+}
+
+TEST(LanguageConfigViewTest, ReorderInputMethodIdsForLanguageCode_Noop) {
+ std::vector<std::string> input_method_ids;
+ input_method_ids.push_back("xkb:fr::fra"); // France - French
+ input_method_ids.push_back("xkb:be::fra"); // Belgium - French
+ // If the list is already sorted, nothing should happen.
+ LanguageConfigView::ReorderInputMethodIdsForLanguageCode(
+ "fr", &input_method_ids);
+ ASSERT_EQ(2, static_cast<int>(input_method_ids.size()));
+ EXPECT_EQ("xkb:fr::fra", input_method_ids[0]);
+ EXPECT_EQ("xkb:be::fra", input_method_ids[1]);
+}
+
TEST(LanguageConfigViewTest, AddLanguageComboboxModel) {
std::vector<std::string> language_codes;
language_codes.push_back("de");