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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/webui/options/language_options_handler.h" 6 7 #include <string> 8 9 #include "base/memory/scoped_ptr.h" 10 #include "base/values.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 #if defined(OS_CHROMEOS) 14 #include "chrome/browser/chromeos/cros/cros_library.h" 15 #include "chrome/browser/chromeos/cros/input_method_library.h" 16 #include "chrome/browser/ui/webui/options/chromeos/cros_language_options_handler.h" 17 #endif // defined(OS_CHROMEOS) 18 19 #if defined(OS_CHROMEOS) 20 static chromeos::InputMethodDescriptors CreateInputMethodDescriptors() { 21 chromeos::InputMethodDescriptors descriptors; 22 descriptors.push_back( 23 chromeos::InputMethodDescriptor("xkb:us::eng", "USA", "us", "eng")); 24 descriptors.push_back( 25 chromeos::InputMethodDescriptor("xkb:fr::fra", "France", "fr", "fra")); 26 descriptors.push_back( 27 chromeos::InputMethodDescriptor("xkb:be::fra", "Belgium", "be", "fr")); 28 descriptors.push_back( 29 chromeos::InputMethodDescriptor("mozc", "Mozc (US keyboard layout)", "us", 30 "ja")); 31 return descriptors; 32 } 33 34 TEST(LanguageOptionsHandlerTest, GetInputMethodList) { 35 // Use the stub libcros. The object will take care of the cleanup. 36 chromeos::ScopedStubCrosEnabler stub_cros_enabler; 37 38 // Reset the library implementation so it will be initialized 39 // again. Otherwise, non-stub implementation can be reused, if it's 40 // already initialized elsewhere, which results in a crash. 41 chromeos::CrosLibrary::Get()->GetTestApi()->SetInputMethodLibrary(NULL, 42 false); 43 44 chromeos::InputMethodDescriptors descriptors = CreateInputMethodDescriptors(); 45 scoped_ptr<ListValue> list( 46 chromeos::CrosLanguageOptionsHandler::GetInputMethodList(descriptors)); 47 ASSERT_EQ(4U, list->GetSize()); 48 49 DictionaryValue* entry = NULL; 50 DictionaryValue *language_code_set = NULL; 51 std::string input_method_id; 52 std::string display_name; 53 std::string language_code; 54 55 // As shown below, the list should be input method ids should appear in 56 // the same order of the descriptors. 57 ASSERT_TRUE(list->GetDictionary(0, &entry)); 58 ASSERT_TRUE(entry->GetString("id", &input_method_id)); 59 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 60 ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set)); 61 EXPECT_EQ("xkb:us::eng", input_method_id); 62 // Commented out as it depends on translation in generated_resources.grd 63 // (i.e. makes the test fragile). 64 // EXPECT_EQ("English (USA) keyboard layout", display_name); 65 ASSERT_TRUE(language_code_set->HasKey("en-US")); 66 ASSERT_TRUE(language_code_set->HasKey("id")); // From kExtraLanguages. 67 ASSERT_TRUE(language_code_set->HasKey("fil")); // From kExtraLanguages. 68 69 ASSERT_TRUE(list->GetDictionary(1, &entry)); 70 ASSERT_TRUE(entry->GetString("id", &input_method_id)); 71 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 72 ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set)); 73 EXPECT_EQ("xkb:fr::fra", input_method_id); 74 // Commented out. See above. 75 // EXPECT_EQ("French keyboard layout", display_name); 76 ASSERT_TRUE(language_code_set->HasKey("fr")); 77 78 ASSERT_TRUE(list->GetDictionary(2, &entry)); 79 ASSERT_TRUE(entry->GetString("id", &input_method_id)); 80 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 81 ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set)); 82 EXPECT_EQ("xkb:be::fra", input_method_id); 83 // Commented out. See above. 84 // EXPECT_EQ("Belgian keyboard layout", display_name); 85 ASSERT_TRUE(language_code_set->HasKey("fr")); 86 87 ASSERT_TRUE(list->GetDictionary(3, &entry)); 88 ASSERT_TRUE(entry->GetString("id", &input_method_id)); 89 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 90 ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set)); 91 EXPECT_EQ("mozc", input_method_id); 92 // Commented out. See above. 93 // EXPECT_EQ("Japanese input method (for US keyboard)", display_name); 94 ASSERT_TRUE(language_code_set->HasKey("ja")); 95 } 96 97 TEST(LanguageOptionsHandlerTest, GetLanguageList) { 98 chromeos::InputMethodDescriptors descriptors = CreateInputMethodDescriptors(); 99 scoped_ptr<ListValue> list( 100 chromeos::CrosLanguageOptionsHandler::GetLanguageList(descriptors)); 101 ASSERT_EQ(7U, list->GetSize()); 102 103 DictionaryValue* entry = NULL; 104 std::string language_code; 105 std::string display_name; 106 std::string native_display_name; 107 108 // As shown below, the list should be sorted by the display names, 109 // and these names should not have duplicates. 110 111 // This comes from kExtraLanguages. 112 ASSERT_TRUE(list->GetDictionary(0, &entry)); 113 ASSERT_TRUE(entry->GetString("code", &language_code)); 114 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 115 ASSERT_TRUE(entry->GetString("nativeDisplayName", &native_display_name)); 116 EXPECT_EQ("en-AU", language_code); 117 EXPECT_EQ("English (Australia)", display_name); 118 EXPECT_EQ("English (Australia)", native_display_name); 119 120 ASSERT_TRUE(list->GetDictionary(1, &entry)); 121 ASSERT_TRUE(entry->GetString("code", &language_code)); 122 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 123 ASSERT_TRUE(entry->GetString("nativeDisplayName", &native_display_name)); 124 EXPECT_EQ("en-US", language_code); 125 EXPECT_EQ("English (United States)", display_name); 126 EXPECT_EQ("English (United States)", native_display_name); 127 128 // This comes from kExtraLanguages. 129 ASSERT_TRUE(list->GetDictionary(2, &entry)); 130 ASSERT_TRUE(entry->GetString("code", &language_code)); 131 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 132 ASSERT_TRUE(entry->GetString("nativeDisplayName", &native_display_name)); 133 EXPECT_EQ("fil", language_code); 134 EXPECT_EQ("Filipino", display_name); 135 EXPECT_EQ("Filipino", native_display_name); 136 137 ASSERT_TRUE(list->GetDictionary(3, &entry)); 138 ASSERT_TRUE(entry->GetString("code", &language_code)); 139 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 140 ASSERT_TRUE(entry->GetString("nativeDisplayName", &native_display_name)); 141 EXPECT_EQ("fr", language_code); 142 EXPECT_EQ("French", display_name); 143 EXPECT_EQ("fran\u00E7ais", native_display_name); 144 145 // This comes from kExtraLanguages. 146 ASSERT_TRUE(list->GetDictionary(4, &entry)); 147 ASSERT_TRUE(entry->GetString("code", &language_code)); 148 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 149 ASSERT_TRUE(entry->GetString("nativeDisplayName", &native_display_name)); 150 EXPECT_EQ("id", language_code); 151 EXPECT_EQ("Indonesian", display_name); 152 EXPECT_EQ("Bahasa Indonesia", native_display_name); 153 154 ASSERT_TRUE(list->GetDictionary(5, &entry)); 155 ASSERT_TRUE(entry->GetString("code", &language_code)); 156 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 157 ASSERT_TRUE(entry->GetString("nativeDisplayName", &native_display_name)); 158 EXPECT_EQ("ja", language_code); 159 EXPECT_EQ("Japanese", display_name); 160 EXPECT_EQ("\u65E5\u672C\u8A9E", native_display_name); 161 162 // This comes from kExtraLanguages. 163 ASSERT_TRUE(list->GetDictionary(6, &entry)); 164 ASSERT_TRUE(entry->GetString("code", &language_code)); 165 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 166 ASSERT_TRUE(entry->GetString("nativeDisplayName", &native_display_name)); 167 EXPECT_EQ("es-419", language_code); 168 EXPECT_EQ("Spanish (Latin America)", display_name); 169 EXPECT_EQ("espa\u00F1ol (Latinoam\u00E9rica y el Caribe)", 170 native_display_name); 171 } 172 #endif // defined(OS_CHROMEOS) 173 174 #if !defined(OS_MACOSX) 175 TEST(LanguageOptionsHandlerTest, GetUILanguageCodeSet) { 176 scoped_ptr<DictionaryValue> dictionary( 177 LanguageOptionsHandler::GetUILanguageCodeSet()); 178 EXPECT_TRUE(dictionary->HasKey("en-US")); 179 // Note that we don't test a false case, as such an expectation will 180 // fail when we add support for the language. 181 // EXPECT_FALSE(dictionary->HasKey("no")); 182 } 183 #endif // !defined(OS_MACOSX) 184 185 TEST(LanguageOptionsHandlerTest, GetSpellCheckLanguageCodeSet) { 186 scoped_ptr<DictionaryValue> dictionary( 187 LanguageOptionsHandler::GetSpellCheckLanguageCodeSet()); 188 EXPECT_TRUE(dictionary->HasKey("en-US")); 189 } 190