OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/options/language_options_handler.h" | 5 #include "chrome/browser/ui/webui/options/language_options_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/memory/singleton.h" | |
10 #include "base/message_loop/message_loop.h" | |
9 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/chromeos/customization_document.h" | |
10 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" | 13 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" |
11 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h" | 14 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h" |
12 #include "chrome/browser/ui/webui/options/chromeos/cros_language_options_handler .h" | 15 #include "chrome/browser/ui/webui/options/chromeos/cros_language_options_handler .h" |
13 #include "chromeos/ime/input_method_descriptor.h" | 16 #include "chromeos/ime/input_method_descriptor.h" |
17 #include "chromeos/system/statistics_provider.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
15 | 19 |
16 using chromeos::input_method::InputMethodDescriptor; | 20 using chromeos::input_method::InputMethodDescriptor; |
17 using chromeos::input_method::InputMethodDescriptors; | 21 using chromeos::input_method::InputMethodDescriptors; |
18 using chromeos::input_method::MockInputMethodManager; | 22 using chromeos::input_method::MockInputMethodManager; |
19 | 23 |
20 namespace { | 24 namespace { |
21 | 25 |
26 class MachineStatisticsInitializer { | |
27 public: | |
28 MachineStatisticsInitializer() { | |
29 base::MessageLoop tmp_loop(base::MessageLoop::TYPE_DEFAULT); | |
30 chromeos::system::StatisticsProvider::GetInstance() | |
31 ->StartLoadingMachineStatistics(tmp_loop.message_loop_proxy(), false); | |
32 tmp_loop.RunUntilIdle(); | |
33 } | |
34 static MachineStatisticsInitializer* GetInstance(); | |
35 }; | |
36 | |
37 MachineStatisticsInitializer* MachineStatisticsInitializer::GetInstance() { | |
38 return Singleton<MachineStatisticsInitializer>::get(); | |
39 } | |
40 | |
22 class CrosLanguageOptionsHandlerTest : public testing::Test { | 41 class CrosLanguageOptionsHandlerTest : public testing::Test { |
23 public: | 42 public: |
24 CrosLanguageOptionsHandlerTest() { | 43 CrosLanguageOptionsHandlerTest() { |
25 chromeos::input_method::InitializeForTesting(new MockInputMethodManager); | 44 chromeos::input_method::InitializeForTesting(new MockInputMethodManager); |
45 MachineStatisticsInitializer::GetInstance(); // Ignore result | |
26 } | 46 } |
27 virtual ~CrosLanguageOptionsHandlerTest() { | 47 virtual ~CrosLanguageOptionsHandlerTest() { |
28 chromeos::input_method::Shutdown(); | 48 chromeos::input_method::Shutdown(); |
29 } | 49 } |
30 | 50 |
31 protected: | 51 protected: |
32 InputMethodDescriptors CreateInputMethodDescriptors() { | 52 InputMethodDescriptors CreateInputMethodDescriptors1() { |
33 InputMethodDescriptors descriptors; | 53 InputMethodDescriptors descriptors; |
34 descriptors.push_back(GetDesc("xkb:us::eng", "us", "en-US")); | 54 descriptors.push_back(GetDesc("xkb:us::eng", "us", "en-US")); |
35 descriptors.push_back(GetDesc("xkb:fr::fra", "fr", "fr")); | 55 descriptors.push_back(GetDesc("xkb:fr::fra", "fr", "fr")); |
36 descriptors.push_back(GetDesc("xkb:be::fra", "be", "fr")); | 56 descriptors.push_back(GetDesc("xkb:be::fra", "be", "fr")); |
37 descriptors.push_back(GetDesc("xkb:is::ice", "is", "is")); | 57 descriptors.push_back(GetDesc("xkb:is::ice", "is", "is")); |
38 return descriptors; | 58 return descriptors; |
39 } | 59 } |
40 | 60 |
61 InputMethodDescriptors CreateInputMethodDescriptors2() { | |
62 InputMethodDescriptors descriptors; | |
63 descriptors.push_back(GetDesc("xkb:us::eng", "us", "en-US")); | |
64 descriptors.push_back(GetDesc("xkb:ch:fr:fra", "ch(fr)", "fr")); | |
65 descriptors.push_back(GetDesc("xkb:ch::ger", "ch", "de")); | |
66 descriptors.push_back(GetDesc("xkb:it::ita", "it", "it")); | |
67 descriptors.push_back(GetDesc("xkb:is::ice", "is", "is")); | |
68 return descriptors; | |
69 } | |
70 | |
41 private: | 71 private: |
42 InputMethodDescriptor GetDesc(const std::string& id, | 72 InputMethodDescriptor GetDesc(const std::string& id, |
43 const std::string& raw_layout, | 73 const std::string& raw_layout, |
44 const std::string& language_code) { | 74 const std::string& language_code) { |
45 std::vector<std::string> layouts; | 75 std::vector<std::string> layouts; |
46 layouts.push_back(raw_layout); | 76 layouts.push_back(raw_layout); |
47 std::vector<std::string> languages; | 77 std::vector<std::string> languages; |
48 languages.push_back(language_code); | 78 languages.push_back(language_code); |
49 return InputMethodDescriptor( | 79 return InputMethodDescriptor( |
50 id, std::string(), layouts, languages, true, GURL(), GURL()); | 80 id, std::string(), layouts, languages, true, GURL(), GURL()); |
51 } | 81 } |
52 }; | 82 }; |
53 | 83 |
54 } // namespace | 84 } // namespace |
55 | 85 |
56 TEST_F(CrosLanguageOptionsHandlerTest, GetInputMethodList) { | 86 TEST_F(CrosLanguageOptionsHandlerTest, GetInputMethodList) { |
57 InputMethodDescriptors descriptors = CreateInputMethodDescriptors(); | 87 InputMethodDescriptors descriptors = CreateInputMethodDescriptors1(); |
58 scoped_ptr<base::ListValue> list( | 88 scoped_ptr<base::ListValue> list( |
59 chromeos::options::CrosLanguageOptionsHandler::GetInputMethodList( | 89 chromeos::options::CrosLanguageOptionsHandler::GetInputMethodList( |
60 descriptors)); | 90 descriptors)); |
61 ASSERT_EQ(4U, list->GetSize()); | 91 ASSERT_EQ(4U, list->GetSize()); |
62 | 92 |
63 base::DictionaryValue* entry = NULL; | 93 base::DictionaryValue* entry = NULL; |
64 base::DictionaryValue *language_code_set = NULL; | 94 base::DictionaryValue *language_code_set = NULL; |
65 std::string input_method_id; | 95 std::string input_method_id; |
66 std::string display_name; | 96 std::string display_name; |
67 std::string language_code; | 97 std::string language_code; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 ASSERT_TRUE(entry->GetString("id", &input_method_id)); | 130 ASSERT_TRUE(entry->GetString("id", &input_method_id)); |
101 ASSERT_TRUE(entry->GetString("displayName", &display_name)); | 131 ASSERT_TRUE(entry->GetString("displayName", &display_name)); |
102 ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set)); | 132 ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set)); |
103 EXPECT_EQ("xkb:is::ice", input_method_id); | 133 EXPECT_EQ("xkb:is::ice", input_method_id); |
104 // Commented out. See above. | 134 // Commented out. See above. |
105 // EXPECT_EQ("Japanese input method (for US keyboard)", display_name); | 135 // EXPECT_EQ("Japanese input method (for US keyboard)", display_name); |
106 ASSERT_TRUE(language_code_set->HasKey("is")); | 136 ASSERT_TRUE(language_code_set->HasKey("is")); |
107 } | 137 } |
108 | 138 |
109 TEST_F(CrosLanguageOptionsHandlerTest, GetUILanguageList) { | 139 TEST_F(CrosLanguageOptionsHandlerTest, GetUILanguageList) { |
110 InputMethodDescriptors descriptors = CreateInputMethodDescriptors(); | 140 // This requires initialized StatisticsProvider. |
141 // (see CrosLanguageOptionsHandlerTest() ) | |
142 InputMethodDescriptors descriptors = CreateInputMethodDescriptors1(); | |
111 scoped_ptr<base::ListValue> list( | 143 scoped_ptr<base::ListValue> list( |
112 chromeos::options::CrosLanguageOptionsHandler::GetUILanguageList( | 144 chromeos::options::CrosLanguageOptionsHandler::GetUILanguageList( |
113 descriptors)); | 145 descriptors)); |
114 | 146 |
115 for (size_t i = 0; i < list->GetSize(); ++i) { | 147 for (size_t i = 0; i < list->GetSize(); ++i) { |
116 base::DictionaryValue* dict; | 148 base::DictionaryValue* dict; |
117 ASSERT_TRUE(list->GetDictionary(i, &dict)); | 149 ASSERT_TRUE(list->GetDictionary(i, &dict)); |
118 std::string code; | 150 std::string code; |
119 ASSERT_TRUE(dict->GetString("code", &code)); | 151 ASSERT_TRUE(dict->GetString("code", &code)); |
120 EXPECT_NE("is", code) | 152 EXPECT_NE("is", code) |
121 << "Icelandic is an example language which has input method " | 153 << "Icelandic is an example language which has input method " |
122 << "but can't use it as UI language."; | 154 << "but can't use it as UI language."; |
123 } | 155 } |
124 } | 156 } |
157 | |
158 const char kStartupManifest1[] = | |
159 "{\n" | |
160 " \"version\": \"1.0\",\n" | |
161 " \"initial_locale\" : \"fr,en-US,de,is,it\",\n" | |
162 " \"initial_timezone\" : \"Europe/Zurich\",\n" | |
163 " \"keyboard_layout\" : \"xkb:ch:fr:fra\",\n" | |
164 " \"registration_url\" : \"http://www.google.com\",\n" | |
165 " \"setup_content\" : {\n" | |
166 " \"default\" : {\n" | |
167 " \"help_page\" : \"file:///opt/oem/help/en-US/help.html\",\n" | |
168 " \"eula_page\" : \"file:///opt/oem/eula/en-US/eula.html\",\n" | |
169 " },\n" | |
170 " }," | |
171 "}"; | |
172 | |
173 #define EXPECT_LANGUAGE_CODE_AT(i, value) \ | |
174 if (list->GetSize() > i) { \ | |
175 ASSERT_TRUE(list->GetDictionary(i, &dict)); \ | |
176 ASSERT_TRUE(dict->GetString("code", &code)); \ | |
177 EXPECT_EQ(value, code) << "Wrong language code at index " << i << "."; \ | |
178 } | |
179 | |
180 TEST_F(CrosLanguageOptionsHandlerTest, GetUILanguageListMulti) { | |
181 chromeos::StartupCustomizationDocument::GetInstance()->LoadManifestFromString( | |
182 kStartupManifest1); | |
183 chromeos::StartupCustomizationDocument::GetInstance()->Init( | |
184 chromeos::system::StatisticsProvider::GetInstance()); | |
185 | |
186 // This requires initialized StatisticsProvider. | |
187 // (see CrosLanguageOptionsHandlerTest() ) | |
188 InputMethodDescriptors descriptors = CreateInputMethodDescriptors2(); | |
189 scoped_ptr<base::ListValue> list( | |
190 chromeos::options::CrosLanguageOptionsHandler::GetUILanguageList( | |
191 descriptors)); | |
192 | |
193 base::DictionaryValue* dict; | |
194 std::string code; | |
195 | |
196 for (size_t i = 0; i < list->GetSize(); ++i) { | |
197 ASSERT_TRUE(list->GetDictionary(i, &dict)); | |
198 ASSERT_TRUE(dict->GetString("code", &code)); | |
199 EXPECT_NE("is", code) | |
200 << "Icelandic is an example language which has input method " | |
201 << "but can't use it as UI language."; | |
202 } | |
203 | |
204 // (4 languages (except islandic) + divider)=5 + all other languages | |
205 EXPECT_GT(list->GetSize(), (size_t)5); | |
Nikita (slow)
2014/01/31 15:47:20
nit: Insert space after (size_t)
Dmitry Polukhin
2014/01/31 17:04:41
Please don't use C-style cast. Use static_cast<siz
Alexander Alekseev
2014/01/31 17:21:00
Done.
Alexander Alekseev
2014/01/31 17:21:00
I've changed it to suffix-style "5u".
| |
206 | |
207 EXPECT_LANGUAGE_CODE_AT(0, "fr") | |
208 EXPECT_LANGUAGE_CODE_AT(1, "en-US") | |
209 EXPECT_LANGUAGE_CODE_AT(2, "de") | |
210 EXPECT_LANGUAGE_CODE_AT(3, "it") | |
211 EXPECT_LANGUAGE_CODE_AT( | |
212 4, chromeos::options::kVENDOR_OTHER_LANGUAGES_LIST_DIVIDER); | |
213 } | |
OLD | NEW |