• 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 "input_method_controller.h"
24 #include "input_method_property.h"
25 #include "tdd_util.h"
26 
27 using namespace testing::ext;
28 namespace OHOS {
29 namespace MiscServices {
30 class NewImeSwitchTest : public testing::Test {
31 public:
32     static void SetUpTestCase(void);
33     static void TearDownTestCase(void);
34     void SetUp();
35     void TearDown();
36     static void CheckCurrentProp();
37     static void CheckCurrentSubProp(const std::string &subName);
38     static void CheckCurrentSubProps();
39     static std::mutex imeChangeFlagLock;
40     static std::condition_variable conditionVar;
41     static bool imeChangeFlag;
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 std::mutex NewImeSwitchTest::imeChangeFlagLock;
50 std::condition_variable NewImeSwitchTest::conditionVar;
51 bool NewImeSwitchTest::imeChangeFlag = false;
52 sptr<InputMethodController> NewImeSwitchTest::imc_;
53 std::string NewImeSwitchTest::bundleName = "com.example.newTestIme";
54 std::string NewImeSwitchTest::extName = "InputMethodExtAbility";
55 std::vector<std::string> NewImeSwitchTest::subName{ "lowerInput", "upperInput", "chineseInput" };
56 std::vector<std::string> NewImeSwitchTest::locale{ "en-US", "en-US", "zh-CN" };
57 std::vector<std::string> NewImeSwitchTest::language{ "english", "english", "chinese" };
58 constexpr uint32_t IME_SUBTYPE_NUM = 3;
59 constexpr uint32_t SUBTYPE_SWITCH_DELAY_TIME = 10;
60 constexpr uint32_t IME_SWITCH_DELAY_TIME = 200;
61 constexpr uint32_t WAIT_IME_READY_TIME = 1;
62 class InputMethodSettingListenerImpl : public InputMethodSettingListener {
63 public:
64     InputMethodSettingListenerImpl() = default;
65     ~InputMethodSettingListenerImpl() = default;
OnImeChange(const Property & property,const SubProperty & subProperty)66     void OnImeChange(const Property &property, const SubProperty &subProperty)
67     {
68         {
69             std::unique_lock<std::mutex> lock(NewImeSwitchTest::imeChangeFlagLock);
70             NewImeSwitchTest::imeChangeFlag = true;
71         }
72         NewImeSwitchTest::conditionVar.notify_one();
73         IMSA_HILOGI("InputMethodSettingListenerImpl OnImeChange");
74     }
OnPanelStatusChange(const InputWindowStatus & status,const std::vector<InputWindowInfo> & windowInfo)75     void OnPanelStatusChange(const InputWindowStatus &status, const std::vector<InputWindowInfo> &windowInfo)
76     {
77     }
78 };
SetUpTestCase(void)79 void NewImeSwitchTest::SetUpTestCase(void)
80 {
81     IMSA_HILOGI("NewImeSwitchTest::SetUpTestCase");
82     TddUtil::StorageSelfTokenID();
83     TddUtil::SetTestTokenID(TddUtil::AllocTestTokenID(true, true, "ohos.inputMethod.test"));
84     imc_ = InputMethodController::GetInstance();
85     imc_->SetSettingListener(std::make_shared<InputMethodSettingListenerImpl>());
86     imc_->UpdateListenEventFlag("imeChange", true);
87 }
88 
TearDownTestCase(void)89 void NewImeSwitchTest::TearDownTestCase(void)
90 {
91     IMSA_HILOGI("NewImeSwitchTest::TearDownTestCase");
92     InputMethodController::GetInstance()->Close();
93     TddUtil::RestoreSelfTokenID();
94 }
95 
SetUp(void)96 void NewImeSwitchTest::SetUp(void)
97 {
98     IMSA_HILOGI("NewImeSwitchTest::SetUp");
99 }
100 
TearDown(void)101 void NewImeSwitchTest::TearDown(void)
102 {
103     IMSA_HILOGI("NewImeSwitchTest::TearDown");
104 }
105 
CheckCurrentProp()106 void NewImeSwitchTest::CheckCurrentProp()
107 {
108     std::shared_ptr<Property> property = imc_->GetCurrentInputMethod();
109     ASSERT_TRUE(property != nullptr);
110     EXPECT_EQ(property->name, bundleName);
111     EXPECT_EQ(property->id, extName);
112 }
113 
CheckCurrentSubProp(const std::string & subName)114 void NewImeSwitchTest::CheckCurrentSubProp(const std::string &subName)
115 {
116     auto subProperty = imc_->GetCurrentInputMethodSubtype();
117     ASSERT_TRUE(subProperty != nullptr);
118     EXPECT_EQ(subProperty->id, subName);
119     EXPECT_EQ(subProperty->name, bundleName);
120 }
121 
CheckCurrentSubProps()122 void NewImeSwitchTest::CheckCurrentSubProps()
123 {
124     std::vector<SubProperty> subProps;
125     auto ret = imc_->ListCurrentInputMethodSubtype(subProps);
126     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
127     ASSERT_EQ(subProps.size(), IME_SUBTYPE_NUM);
128     for (uint32_t i = 0; i < IME_SUBTYPE_NUM; i++) {
129         EXPECT_EQ(subProps[i].id, subName[i]);
130         EXPECT_EQ(subProps[i].name, bundleName);
131         EXPECT_EQ(subProps[i].language, language[i]);
132         EXPECT_EQ(subProps[i].locale, locale[i]);
133     }
134 }
135 
136 /**
137 * @tc.name: testNewImeSwitch
138 * @tc.desc: switch ime to newTestIme.
139 * @tc.type: FUNC
140 * @tc.require:
141 * @tc.author: chenyu
142 */
143 HWTEST_F(NewImeSwitchTest, testNewImeSwitch, TestSize.Level0)
144 {
145     IMSA_HILOGI("newIme testNewImeSwitch Test START");
146     std::unique_lock<std::mutex> lock(imeChangeFlagLock);
147     imeChangeFlag = false;
148     // switch to newTestIme
149     auto ret = imc_->SwitchInputMethod(bundleName);
150     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
__anon9dc3cb6a0102null151     conditionVar.wait_for(lock, std::chrono::milliseconds(IME_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
152     EXPECT_TRUE(imeChangeFlag);
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     std::unique_lock<std::mutex> lock(imeChangeFlagLock);
170     imeChangeFlag = false;
171     int32_t ret = imc_->SwitchInputMethod(bundleName, subName[0]);
172     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
173     conditionVar.wait_for(
__anon9dc3cb6a0202null174         lock, std::chrono::milliseconds(SUBTYPE_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
175     EXPECT_FALSE(imeChangeFlag);
176     CheckCurrentProp();
177     CheckCurrentSubProp(subName[0]);
178     CheckCurrentSubProps();
179 }
180 
181 /**
182 * @tc.name: testSubTypeSwitch_002
183 * @tc.desc: switch subtype with subName2.
184 * @tc.type: FUNC
185 * @tc.require:
186 * @tc.author: chenyu
187 */
188 HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_002, TestSize.Level0)
189 {
190     IMSA_HILOGI("newIme testSubTypeSwitch_002 Test START");
191     std::unique_lock<std::mutex> lock(imeChangeFlagLock);
192     imeChangeFlag = false;
193     int32_t ret = imc_->SwitchInputMethod(bundleName, subName[1]);
194     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
195     conditionVar.wait_for(
__anon9dc3cb6a0302null196         lock, std::chrono::milliseconds(SUBTYPE_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
197     EXPECT_TRUE(imeChangeFlag);
198     CheckCurrentProp();
199     CheckCurrentSubProp(subName[1]);
200     CheckCurrentSubProps();
201 }
202 
203 /**
204 * @tc.name: testSubTypeSwitch_003
205 * @tc.desc: switch subtype with subName3.
206 * @tc.type: FUNC
207 * @tc.require:
208 * @tc.author: chenyu
209 */
210 HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_003, TestSize.Level0)
211 {
212     IMSA_HILOGI("newIme testSubTypeSwitch_003 Test START");
213     std::unique_lock<std::mutex> lock(imeChangeFlagLock);
214     imeChangeFlag = false;
215     int32_t ret = imc_->SwitchInputMethod(bundleName, subName[2]);
216     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
217     conditionVar.wait_for(
__anon9dc3cb6a0402null218         lock, std::chrono::milliseconds(SUBTYPE_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
219     EXPECT_TRUE(imeChangeFlag);
220     EXPECT_TRUE(imeChangeFlag);
221     CheckCurrentProp();
222     CheckCurrentSubProp(subName[2]);
223     CheckCurrentSubProps();
224 }
225 
226 /**
227 * @tc.name: testSubTypeSwitch_004
228 * @tc.desc: switch subtype witch subName1.
229 * @tc.type: FUNC
230 * @tc.require:
231 * @tc.author: chenyu
232 */
233 HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_004, TestSize.Level0)
234 {
235     IMSA_HILOGI("newIme testSubTypeSwitch_004 Test START");
236     std::unique_lock<std::mutex> lock(imeChangeFlagLock);
237     imeChangeFlag = false;
238     int32_t ret = imc_->SwitchInputMethod(bundleName, subName[0]);
239     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
240     conditionVar.wait_for(
__anon9dc3cb6a0502null241         lock, std::chrono::milliseconds(SUBTYPE_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
242     EXPECT_TRUE(imeChangeFlag);
243     CheckCurrentProp();
244     CheckCurrentSubProp(subName[0]);
245     CheckCurrentSubProps();
246 }
247 
248 /**
249 * @tc.name: testSubTypeSwitchWithErrorSubName
250 * @tc.desc: switch subtype with error subName.
251 * @tc.type: FUNC
252 * @tc.require:
253 * @tc.author: chenyu
254 */
255 HWTEST_F(NewImeSwitchTest, testSubTypeSwitchWithErrorSubName, TestSize.Level0)
256 {
257     IMSA_HILOGI("newIme testSubTypeSwitchWithErrorSubName Test START");
258     int32_t ret = imc_->SwitchInputMethod(bundleName, "errorSubName");
259     EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
260     CheckCurrentProp();
261     CheckCurrentSubProp(subName[0]);
262     CheckCurrentSubProps();
263 }
264 
265 /**
266 * @tc.name: testSwitchToCurrentImeWithEmptySubName
267 * @tc.desc: switch to currentIme witch empty subName.
268 * @tc.type: FUNC
269 * @tc.require:
270 * @tc.author: chenyu
271 */
272 HWTEST_F(NewImeSwitchTest, testSwitchToCurrentImeWithEmptySubName, TestSize.Level0)
273 {
274     IMSA_HILOGI("newIme testSwitchToCurrentImeWithEmptySubName Test START");
275     std::unique_lock<std::mutex> lock(imeChangeFlagLock);
276     imeChangeFlag = false;
277     int32_t ret = imc_->SwitchInputMethod(bundleName);
278     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
279     conditionVar.wait_for(
__anon9dc3cb6a0602null280         lock, std::chrono::milliseconds(SUBTYPE_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
281     EXPECT_FALSE(imeChangeFlag);
282     CheckCurrentProp();
283     CheckCurrentSubProp(subName[0]);
284     CheckCurrentSubProps();
285 }
286 } // namespace MiscServices
287 } // namespace OHOS
288