• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <gtest/gtest.h>
16 #include <sys/time.h>
17 #include <unistd.h>
18 
19 #include <string>
20 #include <vector>
21 
22 #include "global.h"
23 #include "ime_event_monitor_manager_impl.h"
24 #include "ime_setting_listener_test_impl.h"
25 #include "input_method_controller.h"
26 #include "input_method_property.h"
27 #include "key_event_util.h"
28 #include "tdd_util.h"
29 
30 using namespace testing::ext;
31 namespace OHOS {
32 namespace MiscServices {
33 class NewImeSwitchTest : public testing::Test {
34 public:
35     static void SetUpTestCase(void);
36     static void TearDownTestCase(void);
37     void SetUp();
38     void TearDown();
39     static void CheckCurrentProp();
40     static void CheckCurrentSubProp(const std::string &subName);
41     static void CheckCurrentSubProps();
42     static sptr<InputMethodController> imc_;
43     static std::string bundleName;
44     static std::string extName;
45     static std::vector<std::string> subName;
46     static std::vector<std::string> locale;
47     static std::vector<std::string> language;
48 };
49 sptr<InputMethodController> NewImeSwitchTest::imc_;
50 std::string NewImeSwitchTest::bundleName = "com.example.newTestIme";
51 std::string NewImeSwitchTest::extName = "InputMethodExtAbility";
52 std::vector<std::string> NewImeSwitchTest::subName { "lowerInput", "upperInput", "chineseInput" };
53 std::vector<std::string> NewImeSwitchTest::locale { "en-US", "en-US", "zh-CN" };
54 std::vector<std::string> NewImeSwitchTest::language { "english", "english", "chinese" };
55 constexpr uint32_t IME_SUBTYPE_NUM = 3;
56 constexpr uint32_t WAIT_IME_READY_TIME = 1;
SetUpTestCase(void)57 void NewImeSwitchTest::SetUpTestCase(void)
58 {
59     IMSA_HILOGI("NewImeSwitchTest::SetUpTestCase");
60     TddUtil::StorageSelfTokenID();
61     TddUtil::SetTestTokenID(
62         TddUtil::AllocTestTokenID(true, "ohos.inputMethod.test", { "ohos.permission.CONNECT_IME_ABILITY" }));
63     TddUtil::EnabledAllIme();
64     imc_ = InputMethodController::GetInstance();
65     auto listener = std::make_shared<ImeSettingListenerTestImpl>();
66     ImeEventMonitorManagerImpl::GetInstance().RegisterImeEventListener(EVENT_IME_CHANGE_MASK, listener);
67     TddUtil::InitWindow(false);
68 }
69 
TearDownTestCase(void)70 void NewImeSwitchTest::TearDownTestCase(void)
71 {
72     IMSA_HILOGI("NewImeSwitchTest::TearDownTestCase");
73     TddUtil::GrantNativePermission();
74     InputMethodController::GetInstance()->Close();
75     TddUtil::DestroyWindow();
76     TddUtil::RestoreSelfTokenID();
77 }
78 
SetUp(void)79 void NewImeSwitchTest::SetUp(void)
80 {
81     IMSA_HILOGI("NewImeSwitchTest::SetUp");
82 }
83 
TearDown(void)84 void NewImeSwitchTest::TearDown(void)
85 {
86     IMSA_HILOGI("NewImeSwitchTest::TearDown");
87 }
88 
CheckCurrentProp()89 void NewImeSwitchTest::CheckCurrentProp()
90 {
91     std::shared_ptr<Property> property = imc_->GetCurrentInputMethod();
92     ASSERT_TRUE(property != nullptr);
93     EXPECT_EQ(property->name, bundleName);
94     EXPECT_EQ(property->id, extName);
95 }
96 
CheckCurrentSubProp(const std::string & subName)97 void NewImeSwitchTest::CheckCurrentSubProp(const std::string &subName)
98 {
99     auto subProperty = imc_->GetCurrentInputMethodSubtype();
100     ASSERT_TRUE(subProperty != nullptr);
101     EXPECT_EQ(subProperty->id, subName);
102     EXPECT_EQ(subProperty->name, bundleName);
103 }
104 
CheckCurrentSubProps()105 void NewImeSwitchTest::CheckCurrentSubProps()
106 {
107     std::vector<SubProperty> subProps;
108     auto ret = imc_->ListCurrentInputMethodSubtype(subProps);
109     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
110     ASSERT_EQ(subProps.size(), IME_SUBTYPE_NUM);
111     for (uint32_t i = 0; i < IME_SUBTYPE_NUM; i++) {
112         EXPECT_EQ(subProps[i].id, subName[i]);
113         EXPECT_EQ(subProps[i].name, bundleName);
114         EXPECT_EQ(subProps[i].language, language[i]);
115         EXPECT_EQ(subProps[i].locale, locale[i]);
116     }
117 }
118 
119 /**
120 * @tc.name: testSwitchType
121 * @tc.desc: switch ime to newTestIme.
122 * @tc.type: FUNC
123 * @tc.require:
124 * @tc.author: chenyu
125  */
126 HWTEST_F(NewImeSwitchTest, testSwitchType, TestSize.Level0)
127 {
128     IMSA_HILOGI("newIme testSwitchType Test START");
129     TddUtil::DisabledAllIme();
130     ImeSettingListenerTestImpl::ResetParam();
131     bool result =
132         KeyEventUtil::SimulateKeyEvents({ MMI::KeyEvent::KEYCODE_CTRL_LEFT, MMI::KeyEvent::KEYCODE_SHIFT_LEFT });
133     EXPECT_TRUE(result);
134     EXPECT_FALSE(ImeSettingListenerTestImpl::WaitImeChange());
135     TddUtil::EnabledAllIme();
136 }
137 
138 /**
139 * @tc.name: testNewImeSwitch
140 * @tc.desc: switch ime to newTestIme.
141 * @tc.type: FUNC
142 * @tc.require:
143 * @tc.author: chenyu
144 */
145 HWTEST_F(NewImeSwitchTest, testNewImeSwitch, TestSize.Level0)
146 {
147     IMSA_HILOGI("newIme testNewImeSwitch Test START");
148     ImeSettingListenerTestImpl::ResetParam();
149     // switch to newTestIme
150     auto ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName);
151     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
152     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitImeChange());
153     CheckCurrentProp();
154     CheckCurrentSubProp(subName[0]);
155     CheckCurrentSubProps();
156     sleep(WAIT_IME_READY_TIME);
157 }
158 
159 /**
160  * @tc.name: testSubTypeSwitch_001
161  * @tc.desc: switch subtype with subName1.
162  * @tc.type: FUNC
163  * @tc.require:
164  * @tc.author: chenyu
165  */
166 HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_001, TestSize.Level0)
167 {
168     IMSA_HILOGI("newIme testSubTypeSwitch_001 Test START");
169     ImeSettingListenerTestImpl::ResetParam();
170     int32_t ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName, subName[0]);
171     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
172     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitImeChange());
173     CheckCurrentProp();
174     CheckCurrentSubProp(subName[0]);
175     CheckCurrentSubProps();
176 }
177 
178 /**
179  * @tc.name: testSubTypeSwitch_002
180  * @tc.desc: switch subtype with subName2.
181  * @tc.type: FUNC
182  * @tc.require:
183  * @tc.author: chenyu
184  */
185 HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_002, TestSize.Level0)
186 {
187     IMSA_HILOGI("newIme testSubTypeSwitch_002 Test START");
188     ImeSettingListenerTestImpl::ResetParam();
189     int32_t ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName, subName[1]);
190     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
191     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitImeChange());
192     CheckCurrentProp();
193     CheckCurrentSubProp(subName[1]);
194     CheckCurrentSubProps();
195 }
196 
197 /**
198  * @tc.name: testSubTypeSwitch_003
199  * @tc.desc: switch subtype with subName3.
200  * @tc.type: FUNC
201  * @tc.require:
202  * @tc.author: chenyu
203  */
204 HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_003, TestSize.Level0)
205 {
206     IMSA_HILOGI("newIme testSubTypeSwitch_003 Test START");
207     ImeSettingListenerTestImpl::ResetParam();
208     int32_t ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName, subName[2]);
209     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
210     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitImeChange());
211     CheckCurrentProp();
212     CheckCurrentSubProp(subName[2]);
213     CheckCurrentSubProps();
214 }
215 
216 /**
217  * @tc.name: testSubTypeSwitch_004
218  * @tc.desc: switch subtype witch subName1.
219  * @tc.type: FUNC
220  * @tc.require:
221  * @tc.author: chenyu
222  */
223 HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_004, TestSize.Level0)
224 {
225     IMSA_HILOGI("newIme testSubTypeSwitch_004 Test START");
226     ImeSettingListenerTestImpl::ResetParam();
227     int32_t ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName, subName[0]);
228     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
229     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitImeChange());
230     CheckCurrentProp();
231     CheckCurrentSubProp(subName[0]);
232     CheckCurrentSubProps();
233 }
234 
235 /**
236  * @tc.name: testSubTypeSwitchWithErrorSubName
237  * @tc.desc: switch subtype with error subName.
238  * @tc.type: FUNC
239  * @tc.require:
240  * @tc.author: chenyu
241  */
242 HWTEST_F(NewImeSwitchTest, testSubTypeSwitchWithErrorSubName, TestSize.Level0)
243 {
244     IMSA_HILOGI("newIme testSubTypeSwitchWithErrorSubName Test START");
245     int32_t ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName, "errorSubName");
246     EXPECT_EQ(ret, ErrorCode::ERROR_IMSA_GET_IME_INFO_FAILED);
247     CheckCurrentProp();
248     CheckCurrentSubProp(subName[0]);
249     CheckCurrentSubProps();
250 }
251 
252 /**
253  * @tc.name: testSwitchToCurrentImeWithEmptySubName
254  * @tc.desc: switch to currentIme with empty subName.
255  * @tc.type: FUNC
256  * @tc.require:
257  * @tc.author: chenyu
258  */
259 HWTEST_F(NewImeSwitchTest, testSwitchToCurrentImeWithEmptySubName, TestSize.Level0)
260 {
261     IMSA_HILOGI("newIme testSwitchToCurrentImeWithEmptySubName Test START");
262     ImeSettingListenerTestImpl::ResetParam();
263     int32_t ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName);
264     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
265     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitImeChange());
266     CheckCurrentProp();
267     CheckCurrentSubProp(subName[0]);
268     CheckCurrentSubProps();
269 }
270 
271 /**
272  * @tc.name: testSwitchInputMethod_001
273  * @tc.desc: switch ime to newTestIme and switch the subtype to subName1.
274  * @tc.type: FUNC
275  * @tc.require:
276  * @tc.author: weishaoxiong
277  */
278 HWTEST_F(NewImeSwitchTest, testSwitchInputMethod_001, TestSize.Level0)
279 {
280     IMSA_HILOGI("newIme testSwitchInputMethod_001 Test START");
281     ImeSettingListenerTestImpl::ResetParam();
282     auto ret = imc_->SwitchInputMethod(SwitchTrigger::SYSTEM_APP, bundleName, subName[1]);
283     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
284     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitImeChange());
285     CheckCurrentProp();
286     CheckCurrentSubProp(subName[1]);
287     CheckCurrentSubProps();
288 }
289 
290 /**
291  * @tc.name: testSwitchInputMethod_002
292  * @tc.desc: switch the subtype to subName0.
293  * @tc.type: FUNC
294  * @tc.require:
295  * @tc.author: weishaoxiong
296  */
297 HWTEST_F(NewImeSwitchTest, testSwitchInputMethod_002, TestSize.Level0)
298 {
299     IMSA_HILOGI("newIme testSwitchInputMethod_002 Test START");
300     ImeSettingListenerTestImpl::ResetParam();
301     auto ret = imc_->SwitchInputMethod(SwitchTrigger::SYSTEM_APP, bundleName, subName[0]);
302     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
303     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitImeChange());
304     CheckCurrentProp();
305     CheckCurrentSubProp(subName[0]);
306     CheckCurrentSubProps();
307 }
308 
309 /**
310  * @tc.name: testSwitchInputMethod_003
311  * @tc.desc: switch ime to newTestIme.
312  * @tc.type: FUNC
313  * @tc.require:
314  * @tc.author: weishaoxiong
315  */
316 HWTEST_F(NewImeSwitchTest, testSwitchInputMethod_003, TestSize.Level0)
317 {
318     IMSA_HILOGI("newIme testSwitchInputMethod_003 Test START");
319     auto ret = imc_->SwitchInputMethod(SwitchTrigger::SYSTEM_APP, bundleName);
320     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
321     CheckCurrentProp();
322     CheckCurrentSubProp(subName[0]);
323     CheckCurrentSubProps();
324 }
325 
326 /**
327  * @tc.name: testSwitchInputMethod_004
328  * @tc.desc: The caller is not a system app.
329  * @tc.type: FUNC
330  * @tc.require:
331  * @tc.author: weishaoxiong
332  */
333 HWTEST_F(NewImeSwitchTest, testSwitchInputMethod_004, TestSize.Level0)
334 {
335     TddUtil::SetTestTokenID(
336         TddUtil::AllocTestTokenID(false, "ohos.inputMethod.test", { "ohos.permission.CONNECT_IME_ABILITY" }));
337     IMSA_HILOGI("newIme testSwitchInputMethod_004 Test START");
338     auto ret = imc_->SwitchInputMethod(SwitchTrigger::SYSTEM_APP, bundleName);
339     EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_SYSTEM_PERMISSION);
340 }
341 
342 /**
343  * @tc.name: testSwitchInputMethod_005
344  * @tc.desc: The caller has no permissions.
345  * @tc.type: FUNC
346  * @tc.require:
347  * @tc.author: weishaoxiong
348  */
349 HWTEST_F(NewImeSwitchTest, testSwitchInputMethod_005, TestSize.Level0)
350 {
351     TddUtil::SetTestTokenID(TddUtil::AllocTestTokenID(true, "ohos.inputMethod.test", {}));
352     IMSA_HILOGI("newIme testSwitchInputMethod_005 Test START");
353     auto ret = imc_->SwitchInputMethod(SwitchTrigger::SYSTEM_APP, bundleName);
354     EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
355 }
356 } // namespace MiscServices
357 } // namespace OHOS
358