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 "testing/gtest/include/gtest/gtest.h"
7 #include "ui/base/ime/chromeos/component_extension_ime_manager.h"
8 #include "ui/base/ime/chromeos/extension_ime_util.h"
9 #include "ui/base/ime/chromeos/mock_component_extension_ime_manager_delegate.h"
12 namespace input_method
{
16 class ComponentExtensionIMEManagerTest
: public testing::Test
{
18 ComponentExtensionIMEManagerTest() : mock_delegate_(NULL
) {}
20 virtual void SetUp() {
23 ComponentExtensionIME ext1
;
24 ext1
.id
= "ext1_id_xxxxxxxxxxxxxxxxxxxxxxxx";
25 ext1
.description
= "ext1_description";
26 ext1
.options_page_url
=
27 GURL("chrome-extension://" + ext1
.id
+ "/options.html");
28 ext1
.path
= base::FilePath("ext1_file_path");
30 ComponentExtensionEngine ext1_engine1
;
31 ext1_engine1
.engine_id
= "zh-t-i0-pinyin";
32 ext1_engine1
.display_name
= "ext1_engine_1_display_name";
33 ext1_engine1
.language_codes
.push_back("zh-CN");
34 ext1_engine1
.layouts
.push_back("us");
35 ext1
.engines
.push_back(ext1_engine1
);
37 ComponentExtensionEngine ext1_engine2
;
38 ext1_engine2
.engine_id
= "mozc_us";
39 ext1_engine2
.display_name
= "ext1_engine2_display_name";
40 ext1_engine2
.language_codes
.push_back("jp");
41 ext1_engine2
.layouts
.push_back("us");
42 ext1
.engines
.push_back(ext1_engine2
);
44 ComponentExtensionEngine ext1_engine3
;
45 ext1_engine3
.engine_id
= "xkb:ru::rus";
46 ext1_engine3
.display_name
= "ext1_engine3_display_name";
47 ext1_engine3
.language_codes
.push_back("ru");
48 ext1_engine3
.layouts
.push_back("ru");
49 ext1
.engines
.push_back(ext1_engine3
);
51 ime_list_
.push_back(ext1
);
53 ComponentExtensionIME ext2
;
54 ext2
.id
= "ext2_id_xxxxxxxxxxxxxxxxxxxxxxxx";
55 ext2
.description
= "ext2_description";
56 ext2
.path
= base::FilePath("ext2_file_path");
58 ComponentExtensionEngine ext2_engine1
;
59 ext2_engine1
.engine_id
= "vkd_ru_phone_aatseel";
60 ext2_engine1
.display_name
= "ext2_engine_1_display_name";
61 ext2_engine1
.language_codes
.push_back("ru");
62 ext2_engine1
.layouts
.push_back("us");
63 ext2
.engines
.push_back(ext2_engine1
);
65 ComponentExtensionEngine ext2_engine2
;
66 ext2_engine2
.engine_id
= "vkd_vi_telex";
67 ext2_engine2
.display_name
= "ext2_engine2_display_name";
68 ext2_engine2
.language_codes
.push_back("hi");
69 ext2_engine2
.layouts
.push_back("us");
70 ext2
.engines
.push_back(ext2_engine2
);
72 ComponentExtensionEngine ext2_engine3
;
73 ext2_engine3
.engine_id
= "xkb:us::eng";
74 ext2_engine3
.display_name
= "ext2_engine3_display_name";
75 ext2_engine3
.language_codes
.push_back("us");
76 ext2_engine3
.layouts
.push_back("us");
77 ext2
.engines
.push_back(ext2_engine3
);
79 ime_list_
.push_back(ext2
);
81 ComponentExtensionIME ext3
;
82 ext3
.id
= "ext3_id_xxxxxxxxxxxxxxxxxxxxxxxx";
83 ext3
.description
= "ext3_description";
84 ext3
.options_page_url
=
85 GURL("chrome-extension://" + ext3
.id
+ "/options.html");
86 ext3
.path
= base::FilePath("ext3_file_path");
88 ComponentExtensionEngine ext3_engine1
;
89 ext3_engine1
.engine_id
= "ext3_engine1_engine_id";
90 ext3_engine1
.display_name
= "ext3_engine_1_display_name";
91 ext3_engine1
.language_codes
.push_back("hi");
92 ext3_engine1
.layouts
.push_back("us");
93 ext3
.engines
.push_back(ext3_engine1
);
95 ComponentExtensionEngine ext3_engine2
;
96 ext3_engine2
.engine_id
= "ext3_engine2_engine_id";
97 ext3_engine2
.display_name
= "ext3_engine2_display_name";
98 ext3_engine2
.language_codes
.push_back("en");
99 ext3_engine2
.layouts
.push_back("us");
100 ext3
.engines
.push_back(ext3_engine2
);
102 ComponentExtensionEngine ext3_engine3
;
103 ext3_engine3
.engine_id
= "ext3_engine3_engine_id";
104 ext3_engine3
.display_name
= "ext3_engine3_display_name";
105 ext3_engine3
.language_codes
.push_back("en");
106 ext3_engine3
.layouts
.push_back("us");
107 ext3
.engines
.push_back(ext3_engine3
);
109 ime_list_
.push_back(ext3
);
111 mock_delegate_
= new MockComponentExtIMEManagerDelegate();
112 mock_delegate_
->set_ime_list(ime_list_
);
113 component_ext_mgr_
.reset(new ComponentExtensionIMEManager());
114 component_ext_mgr_
->Initialize(
115 scoped_ptr
<ComponentExtensionIMEManagerDelegate
>(
116 mock_delegate_
).Pass());
119 virtual void TearDown() {
123 MockComponentExtIMEManagerDelegate
* mock_delegate_
;
124 scoped_ptr
<ComponentExtensionIMEManager
> component_ext_mgr_
;
125 std::vector
<ComponentExtensionIME
> ime_list_
;
129 DISALLOW_COPY_AND_ASSIGN(ComponentExtensionIMEManagerTest
);
132 TEST_F(ComponentExtensionIMEManagerTest
, LoadComponentExtensionIMETest
) {
133 for (size_t i
= 0; i
< ime_list_
.size(); ++i
) {
134 for (size_t j
= 0; j
< ime_list_
[i
].engines
.size(); ++j
) {
135 const std::string input_method_id
=
136 extension_ime_util::GetComponentInputMethodID(
138 ime_list_
[i
].engines
[j
].engine_id
);
139 component_ext_mgr_
->LoadComponentExtensionIME(NULL
/* profile */,
141 EXPECT_EQ(ime_list_
[i
].id
, mock_delegate_
->last_loaded_extension_id());
144 EXPECT_EQ(9, mock_delegate_
->load_call_count());
147 TEST_F(ComponentExtensionIMEManagerTest
, UnloadComponentExtensionIMETest
) {
148 for (size_t i
= 0; i
< ime_list_
.size(); ++i
) {
149 for (size_t j
= 0; j
< ime_list_
[i
].engines
.size(); ++j
) {
150 const std::string input_method_id
=
151 extension_ime_util::GetComponentInputMethodID(
153 ime_list_
[i
].engines
[j
].engine_id
);
154 component_ext_mgr_
->UnloadComponentExtensionIME(NULL
/* profile */,
156 EXPECT_EQ(ime_list_
[i
].id
, mock_delegate_
->last_unloaded_extension_id());
159 EXPECT_EQ(9, mock_delegate_
->unload_call_count());
162 TEST_F(ComponentExtensionIMEManagerTest
, IsWhitelistedTest
) {
163 EXPECT_TRUE(component_ext_mgr_
->IsWhitelisted(
164 extension_ime_util::GetComponentInputMethodID(
166 ime_list_
[0].engines
[0].engine_id
)));
167 EXPECT_FALSE(component_ext_mgr_
->IsWhitelisted(
168 extension_ime_util::GetInputMethodID(
170 ime_list_
[0].engines
[0].engine_id
)));
171 EXPECT_FALSE(component_ext_mgr_
->IsWhitelisted("mozc"));
172 EXPECT_FALSE(component_ext_mgr_
->IsWhitelisted(
173 extension_ime_util::GetInputMethodID("AAAA", "012345")));
174 EXPECT_FALSE(component_ext_mgr_
->IsWhitelisted(
175 extension_ime_util::GetComponentInputMethodID(
179 TEST_F(ComponentExtensionIMEManagerTest
, IsWhitelistedExtensionTest
) {
180 EXPECT_TRUE(component_ext_mgr_
->IsWhitelistedExtension(ime_list_
[0].id
));
181 EXPECT_TRUE(component_ext_mgr_
->IsWhitelistedExtension(ime_list_
[1].id
));
182 EXPECT_FALSE(component_ext_mgr_
->IsWhitelistedExtension("dummy"));
183 EXPECT_FALSE(component_ext_mgr_
->IsWhitelistedExtension(""));
186 TEST_F(ComponentExtensionIMEManagerTest
, GetAllIMEAsInputMethodDescriptor
) {
187 input_method::InputMethodDescriptors descriptors
=
188 component_ext_mgr_
->GetAllIMEAsInputMethodDescriptor();
189 size_t total_ime_size
= 0;
190 for (size_t i
= 0; i
< ime_list_
.size(); ++i
) {
191 total_ime_size
+= ime_list_
[i
].engines
.size();
193 EXPECT_EQ(total_ime_size
, descriptors
.size());
196 for (size_t i
= 0; i
< descriptors
.size(); ++i
) {
197 const input_method::InputMethodDescriptor
& d
= descriptors
[i
];
199 EXPECT_TRUE(d
.id().find("xkb:") != std::string::npos
);
200 } else if (i
>= 2 && i
< 4) {
201 EXPECT_TRUE(d
.id().find("vkd_") != std::string::npos
);
208 } // namespace input_method
209 } // namespace chromeos