• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #define private public
16 #define protected public
17 #include "input_method_controller.h"
18 #include "input_method_system_ability.h"
19 #include "peruser_session.h"
20 #undef private
21 
22 #include <gtest/gtest.h>
23 #include <sys/time.h>
24 #include <unistd.h>
25 
26 #include <string>
27 #include <vector>
28 
29 #include "application_info.h"
30 #include "global.h"
31 #include "i_input_method_agent.h"
32 #include "i_input_method_core.h"
33 #include "ime_cfg_manager.h"
34 #include "input_method_agent_proxy.h"
35 #include "input_method_agent_stub.h"
36 #include "input_method_core_stub.h"
37 #include "os_account_manager.h"
38 
39 using namespace testing::ext;
40 namespace OHOS {
41 namespace MiscServices {
42 class InputMethodPrivateMemberTest : public testing::Test {
43 public:
44     static void SetUpTestCase(void);
45     static void TearDownTestCase(void);
46     void SetUp();
47     void TearDown();
48 };
49 constexpr std::int32_t MAIN_USER_ID = 100;
50 constexpr std::int32_t CURRENT_IME = 0;
51 constexpr std::int32_t SECURITY_IME = 1;
SetUpTestCase(void)52 void InputMethodPrivateMemberTest::SetUpTestCase(void)
53 {
54     IMSA_HILOGI("InputMethodPrivateMemberTest::SetUpTestCase");
55 }
56 
TearDownTestCase(void)57 void InputMethodPrivateMemberTest::TearDownTestCase(void)
58 {
59     IMSA_HILOGI("InputMethodPrivateMemberTest::TearDownTestCase");
60 }
61 
SetUp(void)62 void InputMethodPrivateMemberTest::SetUp(void)
63 {
64     IMSA_HILOGI("InputMethodPrivateMemberTest::SetUp");
65 }
66 
TearDown(void)67 void InputMethodPrivateMemberTest::TearDown(void)
68 {
69     IMSA_HILOGI("InputMethodPrivateMemberTest::TearDown");
70 }
71 
72 /**
73 * @tc.name: SA_ServiceStartAbnormal
74 * @tc.desc: SA Service Start Abnormal.
75 * @tc.type: FUNC
76 * @tc.require: issuesI640YZ
77 */
78 HWTEST_F(InputMethodPrivateMemberTest, SA_ServiceStartAbnormal, TestSize.Level0)
79 {
80     auto service = new InputMethodSystemAbility();
81     service->state_ = ServiceRunningState::STATE_RUNNING;
82     service->OnStart();
83 
84     EXPECT_NE(service->userId_, MAIN_USER_ID);
85     EXPECT_TRUE(InputMethodSystemAbility::serviceHandler_ == nullptr);
86 
87     service->OnStop();
88     EXPECT_EQ(service->state_, ServiceRunningState::STATE_NOT_START);
89     service->OnStop();
90     delete service;
91     service = nullptr;
92 }
93 
94 /**
95 * @tc.name: SA_GetExtends
96 * @tc.desc: SA GetExtends.
97 * @tc.type: FUNC
98 * @tc.require: issuesI640YZ
99 */
100 HWTEST_F(InputMethodPrivateMemberTest, SA_GetExtends, TestSize.Level0)
101 {
102     constexpr int32_t metaDataNums = 5;
103     ImeInfoInquirer inquirer;
104     std::vector<Metadata> metaData;
105     Metadata metadata[metaDataNums] = { { "language", "english", "" }, { "mode", "mode", "" },
106         { "locale", "local", "" }, { "icon", "icon", "" }, { "", "", "" } };
107     for (auto const &data : metadata) {
108         metaData.emplace_back(data);
109     }
110     auto subProperty = inquirer.GetExtends(metaData);
111     EXPECT_EQ(subProperty.language, "english");
112     EXPECT_EQ(subProperty.mode, "mode");
113     EXPECT_EQ(subProperty.locale, "local");
114     EXPECT_EQ(subProperty.icon, "icon");
115 }
116 
117 /**
118 * @tc.name: SA_OnPackageRemovedWithNullMessage
119 * @tc.desc: SA OnPackageRemoved With Null Message.
120 * @tc.type: FUNC
121 * @tc.require: issuesI640YZ
122 */
123 HWTEST_F(InputMethodPrivateMemberTest, SA_OnPackageRemovedWithNullMessage, TestSize.Level0)
124 {
125     InputMethodSystemAbility service;
126     constexpr int32_t messageId = 5;
127     auto *msg = new Message(messageId, nullptr);
128     auto ret = service.OnPackageRemoved(msg);
129     EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
130     delete msg;
131     msg = nullptr;
132 }
133 
134 /**
135 * @tc.name: SA_OnUserStartedWithNullMessage
136 * @tc.desc: SA OnUserStarted With Null Message.
137 * @tc.type: FUNC
138 * @tc.require: issuesI669E8
139 */
140 HWTEST_F(InputMethodPrivateMemberTest, SA_OnUserStartedWithNullMessage, TestSize.Level0)
141 {
142     InputMethodSystemAbility service;
143     constexpr int32_t messageId = 5;
144     auto *msg = new Message(messageId, nullptr);
145     auto ret = service.OnUserStarted(msg);
146     EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
147     delete msg;
148     msg = nullptr;
149 }
150 
151 /**
152 * @tc.name: SA_ListInputMethodInfoWithInexistentUserId
153 * @tc.desc: SA ListInputMethodInfo With Inexistent UserId.
154 * @tc.type: FUNC
155 * @tc.require: issuesI669E8
156 */
157 HWTEST_F(InputMethodPrivateMemberTest, SA_ListInputMethodInfoWithInexistentUserId, TestSize.Level0)
158 {
159     ImeInfoInquirer inquirer;
160     constexpr int32_t userId = 1;
161     auto inputMethodInfos = inquirer.ListInputMethodInfo(userId);
162     EXPECT_TRUE(inputMethodInfos.empty());
163 }
164 
165 /**
166 * @tc.name: IMC_ListInputMethodCommonWithErrorStatus
167 * @tc.desc: IMC ListInputMethodCommon With Error Status.
168 * @tc.type: FUNC
169 * @tc.require: issuesI669E8
170 */
171 HWTEST_F(InputMethodPrivateMemberTest, IMC_ListInputMethodCommonWithErrorStatus, TestSize.Level0)
172 {
173     std::vector<Property> props;
174     auto ret = InputMethodController::GetInstance()->ListInputMethodCommon(static_cast<InputMethodStatus>(5), props);
175     EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
176     EXPECT_TRUE(props.empty());
177 }
178 
179 /**
180  * @tc.name: PerUserSessionCoreOrAgentNullptr
181  * @tc.desc: Test PerUserSession with core nullptr.
182  * @tc.type: FUNC
183  * @tc.require: issuesI794QF
184  * @tc.author: Zhaolinglan
185  */
186 HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionCoreOrAgentNullptr, TestSize.Level0)
187 {
188     IMSA_HILOGI("InputMethodPrivateMemberTest PerUserSessionCoreOrAgentNullptr TEST START");
189     auto userSession = std::make_shared<PerUserSession>(MAIN_USER_ID);
190     userSession->SetImsCore(CURRENT_IME, nullptr);
191     auto imc = InputMethodController::GetInstance();
192     int32_t ret = userSession->ShowKeyboard(imc->clientInfo_.channel, imc->clientInfo_.client, false, false);
193     EXPECT_EQ(ret, ErrorCode::ERROR_IME_NOT_STARTED);
194     ret = userSession->HideKeyboard(imc->clientInfo_.client);
195     EXPECT_EQ(ret, ErrorCode::ERROR_IME_NOT_STARTED);
196     ret = userSession->ClearDataChannel(imc->clientInfo_.channel);
197     EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
198     ret = userSession->SendAgentToSingleClient(imc->clientInfo_.client);
199     EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
200     ret = userSession->InitInputControlChannel();
201     EXPECT_EQ(ret, ErrorCode::ERROR_IME_NOT_STARTED);
202     userSession->StopInputService("test");
203     userSession->ClearImeData(CURRENT_IME);
204     ret = userSession->OnSwitchIme({}, {}, true);
205     EXPECT_EQ(ret, ErrorCode::ERROR_IME_NOT_STARTED);
206 
207     auto core = userSession->GetImsCore(CURRENT_IME - 1);
208     EXPECT_EQ(core, nullptr);
209     core = userSession->GetImsCore(SECURITY_IME + 1);
210     EXPECT_EQ(core, nullptr);
211 }
212 
213 /**
214  * @tc.name: PerUserSessionClientError
215  * @tc.desc: Test PerUserSession with client error.
216  * @tc.type: FUNC
217  * @tc.require: issuesI794QF
218  * @tc.author: Zhaolinglan
219  */
220 HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionClientError, TestSize.Level0)
221 {
222     IMSA_HILOGI("InputMethodPrivateMemberTest PerUserSessionClientError TEST START");
223     auto userSession = std::make_shared<PerUserSession>(MAIN_USER_ID);
224     auto imc = InputMethodController::GetInstance();
225     sptr<InputMethodCoreStub> core = new InputMethodCoreStub(0);
226 
227     auto clientInfo = userSession->GetClientInfo(imc->clientInfo_.client->AsObject());
228     EXPECT_EQ(clientInfo, nullptr);
229 
230     userSession->SetCurrentClient(nullptr);
231     userSession->OnUnfocused(0, 0);
232     int32_t ret = userSession->OnHideKeyboardSelf();
233     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOUND);
234     ret = userSession->OnShowKeyboardSelf();
235     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOUND);
236     bool result = userSession->IsFocused(1, 1);
237     EXPECT_FALSE(result);
238 
239     userSession->SetCurrentClient(imc->clientInfo_.client);
240     ret = userSession->OnShowKeyboardSelf();
241     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOUND);
242 }
243 
244 /**
245  * @tc.name: PerUserSessionParameterNullptr001
246  * @tc.desc: Test PerUserSession with parameter client nullptr.
247  * @tc.type: FUNC
248  * @tc.require: issuesI794QF
249  * @tc.author: Zhaolinglan
250  */
251 HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionParameterNullptr001, TestSize.Level0)
252 {
253     IMSA_HILOGI("InputMethodPrivateMemberTest PerUserSessionParameterNullptr001 TEST START");
254     auto userSession = std::make_shared<PerUserSession>(MAIN_USER_ID);
255     int32_t ret = userSession->OnStartInput(nullptr, true, false);
256     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
257     ret = userSession->OnReleaseInput(nullptr);
258     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
259     ret = userSession->ShowKeyboard(nullptr, nullptr, false, false);
260     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
261     ret = userSession->RemoveClient(nullptr, false);
262     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
263     auto client = userSession->GetClientInfo(nullptr);
264     EXPECT_EQ(client, nullptr);
265     ret = userSession->SendAgentToSingleClient(nullptr);
266     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
267 }
268 
269 /**
270  * @tc.name: PerUserSessionParameterNullptr002
271  * @tc.desc: Test PerUserSession SetCoreAndAgent with parameter nullptr.
272  * @tc.type: FUNC
273  * @tc.require: issuesI794QF
274  * @tc.author: Zhaolinglan
275  */
276 HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionParameterNullptr002, TestSize.Level0)
277 {
278     IMSA_HILOGI("InputMethodPrivateMemberTest PerUserSessionParameterNullptr002 TEST START");
279     auto userSession = std::make_shared<PerUserSession>(MAIN_USER_ID);
280     sptr<InputMethodCoreStub> core = new InputMethodCoreStub(0);
281     sptr<InputMethodAgentStub> inputMethodAgentStub(new InputMethodAgentStub());
282     sptr<IInputMethodAgent> agent = sptr(new InputMethodAgentProxy(inputMethodAgentStub));
283     int32_t ret = userSession->OnSetCoreAndAgent(nullptr, nullptr);
284     EXPECT_EQ(ret, ErrorCode::ERROR_EX_NULL_POINTER);
285     ret = userSession->OnSetCoreAndAgent(core, nullptr);
286     EXPECT_EQ(ret, ErrorCode::ERROR_EX_NULL_POINTER);
287     ret = userSession->OnSetCoreAndAgent(nullptr, agent);
288     EXPECT_EQ(ret, ErrorCode::ERROR_EX_NULL_POINTER);
289 }
290 
291 /**
292  * @tc.name: PerUserSessionParameterNullptr003
293  * @tc.desc: Test PerUserSession with parameter nullptr.
294  * @tc.type: FUNC
295  * @tc.require: issuesI794QF
296  * @tc.author: Zhaolinglan
297  */
298 HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionParameterNullptr003, TestSize.Level0)
299 {
300     IMSA_HILOGI("InputMethodPrivateMemberTest PerUserSessionParameterNullptr003 TEST START");
301     auto userSession = std::make_shared<PerUserSession>(MAIN_USER_ID);
302     sptr<InputMethodCoreStub> core = new InputMethodCoreStub(0);
303     userSession->OnClientDied(nullptr);
304     userSession->OnImsDied(nullptr);
305     userSession->UpdateClient(nullptr, true);
306     userSession->SetImsCore(CURRENT_IME, core);
307     int32_t ret = userSession->ClearDataChannel(nullptr);
308     EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
309 }
310 } // namespace MiscServices
311 } // namespace OHOS
312