OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/chromeos/login/l10n_util.h" |
| 6 |
| 7 #include "base/at_exit.h" |
| 8 #include "base/basictypes.h" |
| 9 #include "base/compiler_specific.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/singleton.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/values.h" |
| 14 #include "chrome/browser/chromeos/customization_document.h" |
| 15 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" |
| 16 #include "chrome/browser/ui/webui/chromeos/login/l10n_util_test_util.h" |
| 17 #include "chromeos/ime/component_extension_ime_manager.h" |
| 18 #include "chromeos/system/statistics_provider.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace chromeos { |
| 22 |
| 23 namespace { |
| 24 |
| 25 class MachineStatisticsInitializer { |
| 26 public: |
| 27 MachineStatisticsInitializer(); |
| 28 |
| 29 static MachineStatisticsInitializer* GetInstance(); |
| 30 |
| 31 private: |
| 32 DISALLOW_COPY_AND_ASSIGN(MachineStatisticsInitializer); |
| 33 }; |
| 34 |
| 35 MachineStatisticsInitializer::MachineStatisticsInitializer() { |
| 36 base::MessageLoop loop; |
| 37 chromeos::system::StatisticsProvider::GetInstance()-> |
| 38 StartLoadingMachineStatistics(loop.message_loop_proxy(), false); |
| 39 loop.RunUntilIdle(); |
| 40 } |
| 41 |
| 42 // static |
| 43 MachineStatisticsInitializer* MachineStatisticsInitializer::GetInstance() { |
| 44 return Singleton<MachineStatisticsInitializer>::get(); |
| 45 } |
| 46 |
| 47 void VerifyOnlyUILanguages(const base::ListValue& list) { |
| 48 for (size_t i = 0; i < list.GetSize(); ++i) { |
| 49 const base::DictionaryValue* dict; |
| 50 ASSERT_TRUE(list.GetDictionary(i, &dict)); |
| 51 std::string code; |
| 52 ASSERT_TRUE(dict->GetString("code", &code)); |
| 53 EXPECT_NE("is", code) |
| 54 << "Icelandic is an example language which has input method " |
| 55 << "but can't use it as UI language."; |
| 56 } |
| 57 } |
| 58 |
| 59 void VerifyLanguageCode(const base::ListValue& list, |
| 60 size_t index, |
| 61 const std::string& expected_code) { |
| 62 const base::DictionaryValue* dict; |
| 63 ASSERT_TRUE(list.GetDictionary(index, &dict)); |
| 64 std::string actual_code; |
| 65 ASSERT_TRUE(dict->GetString("code", &actual_code)); |
| 66 EXPECT_EQ(expected_code, actual_code) |
| 67 << "Wrong language code at index " << index << "."; |
| 68 } |
| 69 |
| 70 } // namespace |
| 71 |
| 72 class L10nUtilTest : public testing::Test { |
| 73 public: |
| 74 L10nUtilTest(); |
| 75 virtual ~L10nUtilTest(); |
| 76 |
| 77 // testing::Test: |
| 78 virtual void SetUp() OVERRIDE; |
| 79 virtual void TearDown() OVERRIDE; |
| 80 |
| 81 void SetInputMethods1(); |
| 82 void SetInputMethods2(); |
| 83 |
| 84 private: |
| 85 base::ShadowingAtExitManager at_exit_manager_; |
| 86 |
| 87 MockInputMethodManagerWithInputMethods* input_manager_; |
| 88 }; |
| 89 |
| 90 L10nUtilTest::L10nUtilTest() |
| 91 : input_manager_(new MockInputMethodManagerWithInputMethods) { |
| 92 } |
| 93 |
| 94 L10nUtilTest::~L10nUtilTest() { |
| 95 } |
| 96 |
| 97 void L10nUtilTest::SetUp() { |
| 98 chromeos::input_method::InitializeForTesting(input_manager_); |
| 99 input_manager_->SetComponentExtensionIMEManager( |
| 100 make_scoped_ptr(new ComponentExtensionIMEManager)); |
| 101 MachineStatisticsInitializer::GetInstance(); // Ignore result. |
| 102 } |
| 103 |
| 104 void L10nUtilTest::TearDown() { |
| 105 chromeos::input_method::Shutdown(); |
| 106 } |
| 107 |
| 108 void L10nUtilTest::SetInputMethods1() { |
| 109 input_manager_->AddInputMethod("xkb:us::eng", "us", "en-US"); |
| 110 input_manager_->AddInputMethod("xkb:fr::fra", "fr", "fr"); |
| 111 input_manager_->AddInputMethod("xkb:be::fra", "be", "fr"); |
| 112 input_manager_->AddInputMethod("xkb:is::ice", "is", "is"); |
| 113 } |
| 114 |
| 115 void L10nUtilTest::SetInputMethods2() { |
| 116 input_manager_->AddInputMethod("xkb:us::eng", "us", "en-US"); |
| 117 input_manager_->AddInputMethod("xkb:ch:fr:fra", "ch(fr)", "fr"); |
| 118 input_manager_->AddInputMethod("xkb:ch::ger", "ch", "de"); |
| 119 input_manager_->AddInputMethod("xkb:it::ita", "it", "it"); |
| 120 input_manager_->AddInputMethod("xkb:is::ice", "is", "is"); |
| 121 } |
| 122 |
| 123 TEST_F(L10nUtilTest, GetUILanguageList) { |
| 124 SetInputMethods1(); |
| 125 |
| 126 // This requires initialized StatisticsProvider (see L10nUtilTest()). |
| 127 scoped_ptr<base::ListValue> list(GetUILanguageList(NULL, std::string())); |
| 128 |
| 129 VerifyOnlyUILanguages(*list); |
| 130 } |
| 131 |
| 132 void InitStartupCustomizationDocumentForTesting(const std::string& manifest) { |
| 133 StartupCustomizationDocument::GetInstance()->LoadManifestFromString(manifest); |
| 134 StartupCustomizationDocument::GetInstance()->Init( |
| 135 chromeos::system::StatisticsProvider::GetInstance()); |
| 136 } |
| 137 |
| 138 const char kStartupManifest[] = |
| 139 "{\n" |
| 140 " \"version\": \"1.0\",\n" |
| 141 " \"initial_locale\" : \"fr,en-US,de,is,it\",\n" |
| 142 " \"initial_timezone\" : \"Europe/Zurich\",\n" |
| 143 " \"keyboard_layout\" : \"xkb:ch:fr:fra\",\n" |
| 144 " \"registration_url\" : \"http://www.google.com\",\n" |
| 145 " \"setup_content\" : {\n" |
| 146 " \"default\" : {\n" |
| 147 " \"help_page\" : \"file:///opt/oem/help/en-US/help.html\",\n" |
| 148 " \"eula_page\" : \"file:///opt/oem/eula/en-US/eula.html\",\n" |
| 149 " },\n" |
| 150 " }," |
| 151 "}"; |
| 152 |
| 153 TEST_F(L10nUtilTest, GetUILanguageListMulti) { |
| 154 InitStartupCustomizationDocumentForTesting(kStartupManifest); |
| 155 SetInputMethods2(); |
| 156 |
| 157 // This requires initialized StatisticsProvider (see L10nUtilTest()). |
| 158 scoped_ptr<base::ListValue> list(GetUILanguageList(NULL, std::string())); |
| 159 |
| 160 VerifyOnlyUILanguages(*list); |
| 161 |
| 162 // (4 languages (except Icelandic) + divider) = 5 + all other languages |
| 163 ASSERT_LE(5u, list->GetSize()); |
| 164 |
| 165 VerifyLanguageCode(*list, 0, "fr"); |
| 166 VerifyLanguageCode(*list, 1, "en-US"); |
| 167 VerifyLanguageCode(*list, 2, "de"); |
| 168 VerifyLanguageCode(*list, 3, "it"); |
| 169 VerifyLanguageCode(*list, 4, kMostRelevantLanguagesDivider); |
| 170 } |
| 171 |
| 172 TEST_F(L10nUtilTest, GetUILanguageListWithMostRelevant) { |
| 173 std::vector<std::string> most_relevant_language_codes; |
| 174 most_relevant_language_codes.push_back("it"); |
| 175 most_relevant_language_codes.push_back("de"); |
| 176 most_relevant_language_codes.push_back("nonexistent"); |
| 177 |
| 178 // This requires initialized StatisticsProvider (see L10nUtilTest()). |
| 179 scoped_ptr<base::ListValue> |
| 180 list(GetUILanguageList(&most_relevant_language_codes, std::string())); |
| 181 |
| 182 VerifyOnlyUILanguages(*list); |
| 183 |
| 184 ASSERT_LE(3u, list->GetSize()); |
| 185 |
| 186 VerifyLanguageCode(*list, 0, "it"); |
| 187 VerifyLanguageCode(*list, 1, "de"); |
| 188 VerifyLanguageCode(*list, 2, kMostRelevantLanguagesDivider); |
| 189 } |
| 190 |
| 191 } // namespace chromeos |
OLD | NEW |