1 // Copyright 2013 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.
5 #include "base/logging.h"
6 #include "chromeos/ime/component_extension_ime_manager.h"
7 #include "chromeos/ime/extension_ime_util.h"
8 #include "chromeos/ime/mock_component_extension_ime_manager_delegate.h"
9 #include "testing/gtest/include/gtest/gtest.h"
12 namespace input_method
{
16 class TestableComponentExtensionIMEManager
17 : public ComponentExtensionIMEManager
{
19 using ComponentExtensionIMEManager::GetComponentExtensionIMEId
;
22 class ComponentExtensionIMEManagerTest
:
24 public ComponentExtensionIMEManager::Observer
{
26 ComponentExtensionIMEManagerTest()
27 : mock_delegate_(NULL
),
28 on_initialized_callcount_(0) {
31 virtual void SetUp() {
34 ComponentExtensionIME ext1
;
36 ext1
.description
= "ext1_description";
37 ext1
.options_page_url
=
38 GURL("chrome-extension://" + ext1
.id
+ "/options.html");
39 ext1
.path
= base::FilePath("ext1_file_path");
41 ComponentExtensionEngine ext1_engine1
;
42 ext1_engine1
.engine_id
= "ext1_engine1_engine_id";
43 ext1_engine1
.display_name
= "ext1_engine_1_display_name";
44 ext1_engine1
.language_code
= "en";
45 ext1_engine1
.layouts
.push_back("us");
46 ext1
.engines
.push_back(ext1_engine1
);
48 ComponentExtensionEngine ext1_engine2
;
49 ext1_engine2
.engine_id
= "ext1_engine2_engine_id";
50 ext1_engine2
.display_name
= "ext1_engine2_display_name";
51 ext1_engine2
.language_code
= "en";
52 ext1_engine2
.layouts
.push_back("us");
53 ext1
.engines
.push_back(ext1_engine2
);
55 ComponentExtensionEngine ext1_engine3
;
56 ext1_engine3
.engine_id
= "ext1_engine3_engine_id";
57 ext1_engine3
.display_name
= "ext1_engine3_display_name";
58 ext1_engine3
.language_code
= "ja";
59 ext1_engine3
.layouts
.push_back("us");
60 ext1
.engines
.push_back(ext1_engine3
);
62 ime_list_
.push_back(ext1
);
64 ComponentExtensionIME ext2
;
66 ext2
.description
= "ext2_description";
67 ext2
.path
= base::FilePath("ext2_file_path");
69 ComponentExtensionEngine ext2_engine1
;
70 ext2_engine1
.engine_id
= "ext2_engine1_engine_id";
71 ext2_engine1
.display_name
= "ext2_engine_1_display_name";
72 ext2_engine1
.language_code
= "en";
73 ext2_engine1
.layouts
.push_back("us");
74 ext2
.engines
.push_back(ext2_engine1
);
76 ComponentExtensionEngine ext2_engine2
;
77 ext2_engine2
.engine_id
= "ext2_engine2_engine_id";
78 ext2_engine2
.display_name
= "ext2_engine2_display_name";
79 ext2_engine2
.language_code
= "hi";
80 ext2_engine2
.layouts
.push_back("us");
81 ext2
.engines
.push_back(ext2_engine2
);
83 ComponentExtensionEngine ext2_engine3
;
84 ext2_engine3
.engine_id
= "ext2_engine3_engine_id";
85 ext2_engine3
.display_name
= "ext2_engine3_display_name";
86 ext2_engine3
.language_code
= "ja";
87 ext2_engine3
.layouts
.push_back("jp");
88 ext2
.engines
.push_back(ext2_engine3
);
90 ime_list_
.push_back(ext2
);
92 ComponentExtensionIME ext3
;
94 ext3
.description
= "ext3_description";
95 ext1
.options_page_url
=
96 GURL("chrome-extension://" + ext3
.id
+ "/options.html");
97 ext3
.path
= base::FilePath("ext3_file_path");
99 ComponentExtensionEngine ext3_engine1
;
100 ext3_engine1
.engine_id
= "ext3_engine1_engine_id";
101 ext3_engine1
.display_name
= "ext3_engine_1_display_name";
102 ext3_engine1
.language_code
= "hi";
103 ext3_engine1
.layouts
.push_back("us");
104 ext3
.engines
.push_back(ext3_engine1
);
106 ComponentExtensionEngine ext3_engine2
;
107 ext3_engine2
.engine_id
= "ext3_engine2_engine_id";
108 ext3_engine2
.display_name
= "ext3_engine2_display_name";
109 ext3_engine2
.language_code
= "en";
110 ext3_engine2
.layouts
.push_back("us");
111 ext3
.engines
.push_back(ext3_engine2
);
113 ComponentExtensionEngine ext3_engine3
;
114 ext3_engine3
.engine_id
= "ext3_engine3_engine_id";
115 ext3_engine3
.display_name
= "ext3_engine3_display_name";
116 ext3_engine3
.language_code
= "en";
117 ext3_engine3
.layouts
.push_back("us");
118 ext3
.engines
.push_back(ext3_engine3
);
120 ime_list_
.push_back(ext3
);
122 mock_delegate_
= new MockComponentExtIMEManagerDelegate();
123 mock_delegate_
->set_ime_list(ime_list_
);
124 component_ext_mgr_
.reset(new ComponentExtensionIMEManager());
125 component_ext_mgr_
->AddObserver(this);
126 EXPECT_FALSE(component_ext_mgr_
->IsInitialized());
127 component_ext_mgr_
->Initialize(
128 scoped_ptr
<ComponentExtensionIMEManagerDelegate
>(
129 mock_delegate_
).Pass());
130 EXPECT_TRUE(component_ext_mgr_
->IsInitialized());
134 virtual void TearDown() {
135 EXPECT_EQ(1, on_initialized_callcount_
);
136 component_ext_mgr_
->RemoveObserver(this);
140 MockComponentExtIMEManagerDelegate
* mock_delegate_
;
141 scoped_ptr
<ComponentExtensionIMEManager
> component_ext_mgr_
;
142 std::vector
<ComponentExtensionIME
> ime_list_
;
145 virtual void OnInitialized() OVERRIDE
{
146 ++on_initialized_callcount_
;
149 int on_initialized_callcount_
;
151 DISALLOW_COPY_AND_ASSIGN(ComponentExtensionIMEManagerTest
);
154 TEST_F(ComponentExtensionIMEManagerTest
, LoadComponentExtensionIMETest
) {
155 for (size_t i
= 0; i
< ime_list_
.size(); ++i
) {
156 for (size_t j
= 0; j
< ime_list_
[i
].engines
.size(); ++j
) {
157 const std::string input_method_id
=
158 TestableComponentExtensionIMEManager::GetComponentExtensionIMEId(
160 ime_list_
[i
].engines
[j
].engine_id
);
161 component_ext_mgr_
->LoadComponentExtensionIME(input_method_id
);
162 EXPECT_EQ(ime_list_
[i
].id
, mock_delegate_
->last_loaded_extension_id());
165 EXPECT_EQ(9, mock_delegate_
->load_call_count());
168 TEST_F(ComponentExtensionIMEManagerTest
, UnloadComponentExtensionIMETest
) {
169 for (size_t i
= 0; i
< ime_list_
.size(); ++i
) {
170 for (size_t j
= 0; j
< ime_list_
[i
].engines
.size(); ++j
) {
171 const std::string input_method_id
=
172 TestableComponentExtensionIMEManager::GetComponentExtensionIMEId(
174 ime_list_
[i
].engines
[j
].engine_id
);
175 component_ext_mgr_
->UnloadComponentExtensionIME(input_method_id
);
176 EXPECT_EQ(ime_list_
[i
].id
, mock_delegate_
->last_unloaded_extension_id());
179 EXPECT_EQ(9, mock_delegate_
->unload_call_count());
182 TEST_F(ComponentExtensionIMEManagerTest
, IsWhitelistedTest
) {
183 EXPECT_TRUE(component_ext_mgr_
->IsWhitelisted(
184 TestableComponentExtensionIMEManager::GetComponentExtensionIMEId(
186 ime_list_
[0].engines
[0].engine_id
)));
187 EXPECT_FALSE(component_ext_mgr_
->IsWhitelisted(
188 extension_ime_util::GetInputMethodID(
190 ime_list_
[0].engines
[0].engine_id
)));
191 EXPECT_FALSE(component_ext_mgr_
->IsWhitelisted("mozc"));
192 EXPECT_FALSE(component_ext_mgr_
->IsWhitelisted(
193 extension_ime_util::GetInputMethodID("AAAA", "012345")));
194 EXPECT_FALSE(component_ext_mgr_
->IsWhitelisted(
195 TestableComponentExtensionIMEManager::GetComponentExtensionIMEId(
199 TEST_F(ComponentExtensionIMEManagerTest
, IsWhitelistedExtensionTest
) {
200 EXPECT_TRUE(component_ext_mgr_
->IsWhitelistedExtension(ime_list_
[0].id
));
201 EXPECT_TRUE(component_ext_mgr_
->IsWhitelistedExtension(ime_list_
[1].id
));
202 EXPECT_FALSE(component_ext_mgr_
->IsWhitelistedExtension("dummy"));
203 EXPECT_FALSE(component_ext_mgr_
->IsWhitelistedExtension(""));
206 TEST_F(ComponentExtensionIMEManagerTest
, GetNameDescriptionTest
) {
207 for (size_t i
= 0; i
< ime_list_
.size(); ++i
) {
208 for (size_t j
= 0; j
< ime_list_
[i
].engines
.size(); ++j
) {
209 const ComponentExtensionEngine
& engine
210 = ime_list_
[i
].engines
[j
];
212 const std::string input_method_id
=
213 TestableComponentExtensionIMEManager::GetComponentExtensionIMEId(
217 EXPECT_EQ(input_method_id
,
218 component_ext_mgr_
->GetId(ime_list_
[i
].id
, engine
.engine_id
));
219 EXPECT_EQ(engine
.display_name
,
220 component_ext_mgr_
->GetName(input_method_id
));
221 EXPECT_EQ(engine
.description
,
222 component_ext_mgr_
->GetDescription(input_method_id
));
227 TEST_F(ComponentExtensionIMEManagerTest
, ListIMEByLanguageTest
) {
228 const std::string hindi_layout1
=
229 TestableComponentExtensionIMEManager::GetComponentExtensionIMEId(
230 ime_list_
[1].id
, ime_list_
[1].engines
[1].engine_id
);
231 const std::string hindi_layout2
=
232 TestableComponentExtensionIMEManager::GetComponentExtensionIMEId(
233 ime_list_
[2].id
, ime_list_
[2].engines
[0].engine_id
);
235 std::vector
<std::string
> hindi_list
236 = component_ext_mgr_
->ListIMEByLanguage("hi");
237 ASSERT_EQ(2UL, hindi_list
.size());
238 EXPECT_TRUE(hindi_list
[0] == hindi_layout1
|| hindi_list
[0] == hindi_layout2
);
239 EXPECT_TRUE(hindi_list
[1] == hindi_layout1
|| hindi_list
[1] == hindi_layout2
);
241 EXPECT_EQ(0UL, component_ext_mgr_
->ListIMEByLanguage("ru").size());
242 EXPECT_EQ(0UL, component_ext_mgr_
->ListIMEByLanguage("").size());
243 EXPECT_EQ(0UL, component_ext_mgr_
->ListIMEByLanguage("invalid").size());
244 EXPECT_EQ(5UL, component_ext_mgr_
->ListIMEByLanguage("en").size());
245 EXPECT_EQ(2UL, component_ext_mgr_
->ListIMEByLanguage("ja").size());
248 TEST_F(ComponentExtensionIMEManagerTest
, GetAllIMEAsInputMethodDescriptor
) {
249 input_method::InputMethodDescriptors descriptors
=
250 component_ext_mgr_
->GetAllIMEAsInputMethodDescriptor();
251 size_t total_ime_size
= 0;
252 for (size_t i
= 0; i
< ime_list_
.size(); ++i
) {
253 total_ime_size
+= ime_list_
[i
].engines
.size();
255 EXPECT_EQ(total_ime_size
, descriptors
.size());
260 } // namespace input_method
261 } // namespace chromeos