• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 
16 #include "peruser_session.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include <memory>
21 
22 #include "input_client_info.h"
23 #include "mock_input_client.h"
24 #include "mock_input_method_core.h"
25 
26 using namespace OHOS;
27 using namespace MiscServices;
28 using namespace testing;
29 
30 class PerUserSessionTest : public Test {
31 protected:
SetUpTestCase()32     static void SetUpTestCase()
33     {
34         userId = 100;
35         eventHandler = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create(true));
36         perUserSession = std::make_unique<PerUserSession>(userId, eventHandler);
37     }
38 
TearDownTestCase()39     static void TearDownTestCase()
40     {
41         // clear shared resources after all tests
42     }
43 
SetUp()44     void SetUp() override
45     {
46         // prepare resource before each test
47     }
48 
TearDown()49     void TearDown() override
50     {
51         // clear resource after each test
52     }
53     int userId;
54     std::shared_ptr<AppExecFwk::EventHandler> eventHandler;
55     std::unique_ptr<PerUserSession> perUserSession;
56 };
57 /**
58  * @tc.name: AddClientInfoTest
59  * @tc.desc: Verify that the PerUserSession::AddClientInfo method
60  * returns ErrorCode::NO_ERROR when adding client information
61  * @tc.type: FUNC
62  */
TEST_F(PerUserSessionTest,AddClientInfoTest)63 TEST_F(PerUserSessionTest, AddClientInfoTest)
64 {
65     auto mockClient = std::make_shared<MockInputClient>();
66     InputClientInfo clientInfo;
67     clientInfo.client = mockClient;
68     clientInfo.config = { /* initialize configuration */ };
69     clientInfo.uiExtensionTokenId = 123;
70 
71     EXPECT_EQ(perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING), ErrorCode::NO_ERROR);
72 }
73 
74 /**
75  * @tc.name: RemoveClientInfoTest
76  * @tc.desc: Verify that the PerUserSession::RemoveClientInfo method works normally when removing client information
77  * @tc.type: FUNC
78  */
TEST_F(PerUserSessionTest,RemoveClientInfoTest)79 TEST_F(PerUserSessionTest, RemoveClientInfoTest)
80 {
81     auto mockClient = std::make_shared<MockInputClient>();
82     InputClientInfo clientInfo;
83     clientInfo.client = mockClient;
84     clientInfo.config = { /* initialize configuration */ };
85     clientInfo.uiExtensionTokenId = 123;
86 
87     perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING);
88     perUserSession->RemoveClientInfo(mockClient, false);
89 }
90 
91 /**
92  * @tc.name: UpdateClientInfoTest
93  * @tc.desc: Verify that the PerUserSession::UpdateClientInfo method works normally when updating client information
94  * @tc.type: FUNC
95  */
TEST_F(PerUserSessionTest,UpdateClientInfoTest)96 TEST_F(PerUserSessionTest, UpdateClientInfoTest)
97 {
98     auto mockClient = std::make_shared<MockInputClient>();
99     InputClientInfo clientInfo;
100     clientInfo.client = mockClient;
101     clientInfo.config = { /* initialize configuration */ };
102     clientInfo.uiExtensionTokenId = 123;
103 
104     perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING);
105 
106     std::unordered_map<UpdateFlag, std::variant<bool, uint32_t, ImeType, ClientState, TextTotalConfig>> updateInfos;
107     updateInfos[UpdateFlag::ISSHOWKEYBOARD] = true;
108 
109     perUserSession->UpdateClientInfo(mockClient, updateInfos);
110 }
111 
112 /**
113  * @tc.name: HideKeyboardTest
114  * @tc.desc: Verify that the PerUserSession::HideKeyboard method returns ErrorCode::NO_ERROR when hiding the keyboard
115  * @tc.type: FUNC
116  */
TEST_F(PerUserSessionTest,HideKeyboardTest)117 TEST_F(PerUserSessionTest, HideKeyboardTest)
118 {
119     auto mockClient = std::make_shared<MockInputClient>();
120     InputClientInfo clientInfo;
121     clientInfo.client = mockClient;
122     clientInfo.config = { /* initialize configuration */ };
123     clientInfo.uiExtensionTokenId = 123;
124     clientInfo.bindImeType = ImeType::IME;
125 
126     perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING);
127 
128     EXPECT_CALL(*mockClient, AsObject()).WillOnce(Return(mockClient->AsObject()));
129     EXPECT_EQ(perUserSession->HideKeyboard(mockClient), ErrorCode::NO_ERROR);
130 }
131 
132 /**
133  * @tc.name: ShowKeyboardTest
134  * @tc.desc: Verify that the PerUserSession::ShowKeyboard method returns ErrorCode::NO_ERROR when showing the keyboard
135  * @tc.type: FUNC
136  */
TEST_F(PerUserSessionTest,ShowKeyboardTest)137 TEST_F(PerUserSessionTest, ShowKeyboardTest)
138 {
139     auto mockClient = std::make_shared<MockInputClient>();
140     InputClientInfo clientInfo;
141     clientInfo.client = mockClient;
142     clientInfo.config = { /* initialize configuration */ };
143     clientInfo.uiExtensionTokenId = 123;
144     clientInfo.bindImeType = ImeType::IME;
145 
146     perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING);
147 
148     EXPECT_CALL(*mockClient, AsObject()).WillOnce(Return(mockClient->AsObject()));
149     EXPECT_EQ(perUserSession->ShowKeyboard(mockClient), ErrorCode::NO_ERROR);
150 }
151 
152 /**
153  * @tc.name: OnClientDiedTest
154  * @tc.desc: Verify that the PerUserSession::OnClientDied method works normally when the client dies
155  * @tc.type: FUNC
156  */
TEST_F(PerUserSessionTest,OnClientDiedTest)157 TEST_F(PerUserSessionTest, OnClientDiedTest)
158 {
159     auto mockClient = std::make_shared<MockInputClient>();
160     InputClientInfo clientInfo;
161     clientInfo.client = mockClient;
162     clientInfo.config = { /* initialize configuration */ };
163     clientInfo.uiExtensionTokenId = 123;
164     clientInfo.bindImeType = ImeType::IME;
165 
166     perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING);
167     perUserSession->OnClientDied(mockClient);
168 }
169 
170 /**
171  * @tc.name: OnImeDiedTest
172  * @tc.desc: Verify that the PerUserSession::OnImeDied method works normally when the input method dies
173  * @tc.type: FUNC
174  */
TEST_F(PerUserSessionTest,OnImeDiedTest)175 TEST_F(PerUserSessionTest, OnImeDiedTest)
176 {
177     auto mockImeCore = std::make_shared<MockInputMethodCore>();
178     perUserSession->OnImeDied(mockImeCore, ImeType::IME);
179 }
180 
181 /**
182  * @tc.name: RemoveImeTest
183  * @tc.desc: Verify that the PerUserSession::RemoveIme method
184  * returns ErrorCode::ERROR_IME_NOT_STARTED when removing the input method
185  * @tc.type: FUNC
186  */
TEST_F(PerUserSessionTest,RemoveImeTest)187 TEST_F(PerUserSessionTest, RemoveImeTest)
188 {
189     auto mockImeCore = std::make_shared<MockInputMethodCore>();
190     EXPECT_EQ(perUserSession->RemoveIme(mockImeCore, ImeType::IME), ErrorCode::ERROR_IME_NOT_STARTED);
191 }
192 
193 /**
194  * @tc.name: OnHideCurrentInputTest
195  * @tc.desc: Verify that the PerUserSession::OnHideCurrentInput method
196  * returns ErrorCode::NO_ERROR when hiding the current input
197  * @tc.type: FUNC
198  */
TEST_F(PerUserSessionTest,OnHideCurrentInputTest)199 TEST_F(PerUserSessionTest, OnHideCurrentInputTest)
200 {
201     auto mockClient = std::make_shared<MockInputClient>();
202     InputClientInfo clientInfo;
203     clientInfo.client = mockClient;
204     clientInfo.config = { /* initialize configuration */ };
205     clientInfo.uiExtensionTokenId = 123;
206     clientInfo.bindImeType = ImeType::IME;
207 
208     perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING);
209     perUserSession->SetCurrentClient(mockClient);
210     EXPECT_EQ(perUserSession->OnHideCurrentInput(), ErrorCode::NO_ERROR);
211 }
212 
213 /**
214  * @tc.name: OnShowCurrentInputTest
215  * @tc.desc: Verify that the PerUserSession::OnShowCurrentInput method
216  * returns ErrorCode::NO_ERROR when showing the current input
217  * @tc.type: FUNC
218  */
TEST_F(PerUserSessionTest,OnShowCurrentInputTest)219 TEST_F(PerUserSessionTest, OnShowCurrentInputTest)
220 {
221     auto mockClient = std::make_shared<MockInputClient>();
222     InputClientInfo clientInfo;
223     clientInfo.client = mockClient;
224     clientInfo.config = { /* initialize configuration */ };
225     clientInfo.uiExtensionTokenId = 123;
226     clientInfo.bindImeType = ImeType::IME;
227 
228     perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING);
229     perUserSession->SetCurrentClient(mockClient);
230     EXPECT_EQ(perUserSession->OnShowCurrentInput(), ErrorCode::NO_ERROR);
231 }
232 
233 /**
234  * @tc.name: OnHideInputTest
235  * @tc.desc: Verify that the PerUserSession::OnHideInput method returns ErrorCode::NO_ERROR when hiding the input
236  * @tc.type: FUNC
237  */
TEST_F(PerUserSessionTest,OnHideInputTest)238 TEST_F(PerUserSessionTest, OnHideInputTest)
239 {
240     auto mockClient = std::make_shared<MockInputClient>();
241     InputClientInfo clientInfo;
242     clientInfo.client = mockClient;
243     clientInfo.config = { /* initialize configuration */ };
244     clientInfo.uiExtensionTokenId = 123;
245     clientInfo.bindImeType = ImeType::IME;
246 
247     perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING);
248     perUserSession->SetCurrentClient(mockClient);
249     EXPECT_EQ(perUserSession->OnHideInput(mockClient), ErrorCode::NO_ERROR);
250 }
251 
252 /**
253  * @tc.name: OnShowInputTest
254  * @tc.desc: Verify that the PerUserSession::OnShowInput method returns ErrorCode::NO_ERROR when showing the input
255  * @tc.type: FUNC
256  */
TEST_F(PerUserSessionTest,OnShowInputTest)257 TEST_F(PerUserSessionTest, OnShowInputTest)
258 {
259     auto mockClient = std::make_shared<MockInputClient>();
260     InputClientInfo clientInfo;
261     clientInfo.client = mockClient;
262     clientInfo.config = { /* initialize configuration */ };
263     clientInfo.uiExtensionTokenId = 123;
264     clientInfo.bindImeType = ImeType::IME;
265 
266     perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING);
267     perUserSession->SetCurrentClient(mockClient);
268     EXPECT_EQ(perUserSession->OnShowInput(mockClient), ErrorCode::NO_ERROR);
269 }
270 
271 /**
272  * @tc.name: OnHideSoftKeyBoardSelfTest
273  * @tc.desc: Verify that the PerUserSession::OnHideSoftKeyBoardSelf method works normally
274  * when hiding the soft keyboard
275  * @tc.type: FUNC
276  */
TEST_F(PerUserSessionTest,OnHideSoftKeyBoardSelfTest)277 TEST_F(PerUserSessionTest, OnHideSoftKeyBoardSelfTest)
278 {
279     auto mockClient = std::make_shared<MockInputClient>();
280     InputClientInfo clientInfo;
281     clientInfo.client = mockClient;
282     clientInfo.config = { /* initialize configuration */ };
283     clientInfo.uiExtensionTokenId = 123;
284     clientInfo.bindImeType = ImeType::IME;
285 
286     perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING);
287     perUserSession->SetCurrentClient(mockClient);
288     perUserSession->OnHideSoftKeyBoardSelf();
289 }
290 
291 /**
292  * @tc.name: OnRequestShowInputTest
293  * @tc.desc: Verify that the PerUserSession::OnRequestShowInput method
294  * returns ErrorCode::ERROR_IME_NOT_STARTED when requesting to show the input
295  * @tc.type: FUNC
296  */
TEST_F(PerUserSessionTest,OnRequestShowInputTest)297 TEST_F(PerUserSessionTest, OnRequestShowInputTest)
298 {
299     EXPECT_EQ(perUserSession->OnRequestShowInput(), ErrorCode::ERROR_IME_NOT_STARTED);
300 }
301 
302 /**
303  * @tc.name: OnRequestHideInputTest
304  * @tc.desc: Verify that the PerUserSession::OnRequestHideInput method
305  * returns ErrorCode::NO_ERROR when requesting to hide the input
306  * @tc.type: FUNC
307  */
TEST_F(PerUserSessionTest,OnRequestHideInputTest)308 TEST_F(PerUserSessionTest, OnRequestHideInputTest)
309 {
310     EXPECT_EQ(perUserSession->OnRequestHideInput(getpid()), ErrorCode::NO_ERROR);
311 }
312 
313 /**
314  * @tc.name: OnPrepareInputTest
315  * @tc.desc: Verify that the PerUserSession::OnPrepareInput method
316  * returns ErrorCode::NO_ERROR when preparing the input
317  * @tc.type: FUNC
318  */
TEST_F(PerUserSessionTest,OnPrepareInputTest)319 TEST_F(PerUserSessionTest, OnPrepareInputTest)
320 {
321     auto mockClient = std::make_shared<MockInputClient>();
322     InputClientInfo clientInfo;
323     clientInfo.client = mockClient;
324     clientInfo.config = { /* initialize configuration */ };
325     clientInfo.uiExtensionTokenId = 123;
326 
327     EXPECT_EQ(perUserSession->OnPrepareInput(clientInfo), ErrorCode::NO_ERROR);
328 }
329 
330 /**
331  * @tc.name: OnReleaseInputTest
332  * @tc.desc: Verify that the PerUserSession::OnReleaseInput method returns ErrorCode::NO_ERROR when releasing the input
333  * @tc.type: FUNC
334  */
TEST_F(PerUserSessionTest,OnReleaseInputTest)335 TEST_F(PerUserSessionTest, OnReleaseInputTest)
336 {
337     auto mockClient = std::make_shared<MockInputClient>();
338     InputClientInfo clientInfo;
339     clientInfo.client = mockClient;
340     clientInfo.config = { /* initialize configuration */ };
341     clientInfo.uiExtensionTokenId = 123;
342 
343     perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING);
344     EXPECT_EQ(perUserSession->OnReleaseInput(mockClient), ErrorCode::NO_ERROR);
345 }
346 
347 /**
348  * @tc.name: RemoveClientTest
349  * @tc.desc: Verify that the PerUserSession::RemoveClient method returns ErrorCode::NO_ERROR when removing the client
350  * @tc.type: FUNC
351  */
TEST_F(PerUserSessionTest,RemoveClientTest)352 TEST_F(PerUserSessionTest, RemoveClientTest)
353 {
354     auto mockClient = std::make_shared<MockInputClient>();
355     InputClientInfo clientInfo;
356     clientInfo.client = mockClient;
357     clientInfo.config = { /* initialize configuration */ };
358     clientInfo.uiExtensionTokenId = 123;
359 
360     perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING);
361     EXPECT_EQ(perUserSession->RemoveClient(mockClient, true), ErrorCode::NO_ERROR);
362 }
363 
364 /**
365  * @tc.name: DeactivateClientTest
366  * @tc.desc: Verify that the PerUserSession::DeactivateClient method works normally when deactivating the client
367  * @tc.type: FUNC
368  */
TEST_F(PerUserSessionTest,DeactivateClientTest)369 TEST_F(PerUserSessionTest, DeactivateClientTest)
370 {
371     auto mockClient = std::make_shared<MockInputClient>();
372     InputClientInfo clientInfo;
373     clientInfo.client = mockClient;
374     clientInfo.config = { /* initialize configuration */ };
375     clientInfo.uiExtensionTokenId = 123;
376 
377     perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING);
378     perUserSession->DeactivateClient(mockClient);
379 }
380 
381 /**
382  * @tc.name: IsProxyImeEnableTest
383  * @tc.desc: Verify that the PerUserSession::IsProxyImeEnable method
384  * returns false when checking if the proxy IME is enabled
385  * @tc.type: FUNC
386  */
TEST_F(PerUserSessionTest,IsProxyImeEnableTest)387 TEST_F(PerUserSessionTest, IsProxyImeEnableTest)
388 {
389     EXPECT_FALSE(perUserSession->IsProxyImeEnable());
390 }
391 
392 /**
393  * @tc.name: OnStartInputTest
394  * @tc.desc: Verify that the PerUserSession::OnStartInput method
395  * returns ErrorCode::ERROR_IME_NOT_STARTED when starting the input
396  * @tc.type: FUNC
397  */
TEST_F(PerUserSessionTest,OnStartInputTest)398 TEST_F(PerUserSessionTest, OnStartInputTest)
399 {
400     auto mockClient = std::make_shared<MockInputClient>();
401     InputClientInfo clientInfo;
402     clientInfo.client = mockClient;
403     clientInfo.config = { /* initialize configuration */ };
404     clientInfo.uiExtensionTokenId = 123;
405 
406     sptr<IRemoteObject> agent;
407     EXPECT_EQ(perUserSession->OnStartInput(clientInfo, agent), ErrorCode::ERROR_IME_NOT_STARTED);
408 }
409 
410 /**
411  * @tc.name: BindClientWithImeTest
412  * @tc.desc: Verify that the PerUserSession::BindClientWithIme method
413  * returns ErrorCode::ERROR_IME_NOT_STARTED when binding the client with the input method
414  * @tc.type: FUNC
415  */
TEST_F(PerUserSessionTest,BindClientWithImeTest)416 TEST_F(PerUserSessionTest, BindClientWithImeTest)
417 {
418     auto mockClient = std::make_shared<MockInputClient>();
419     InputClientInfo clientInfo;
420     clientInfo.client = mockClient;
421     clientInfo.config = { /* initialize configuration */ };
422     clientInfo.uiExtensionTokenId = 123;
423 
424     EXPECT_EQ(perUserSession->BindClientWithIme(std::make_shared<InputClientInfo>(clientInfo), ImeType::IME, true),
425         ErrorCode::ERROR_IME_NOT_STARTED);
426 }
427 
428 /**
429  * @tc.name: UnBindClientWithImeTest
430  * @tc.desc: Verify that the PerUserSession::UnBindClientWithIme method works normally
431  * when unbinding the client with the input method
432  * @tc.type: FUNC
433  */
TEST_F(PerUserSessionTest,UnBindClientWithImeTest)434 TEST_F(PerUserSessionTest, UnBindClientWithImeTest)
435 {
436     auto mockClient = std::make_shared<MockInputClient>();
437     InputClientInfo clientInfo;
438     clientInfo.client = mockClient;
439     clientInfo.config = { /* initialize configuration */ };
440     clientInfo.uiExtensionTokenId = 123;
441 
442     perUserSession->UnBindClientWithIme(std::make_shared<InputClientInfo>(clientInfo), true);
443 }
444 
445 /**
446  * @tc.name: StopClientInputTest
447  * @tc.desc: Verify that the PerUserSession::StopClientInput method works normally when stopping the client input
448  * @tc.type: FUNC
449  */
TEST_F(PerUserSessionTest,StopClientInputTest)450 TEST_F(PerUserSessionTest, StopClientInputTest)
451 {
452     auto mockClient = std::make_shared<MockInputClient>();
453     InputClientInfo clientInfo;
454     clientInfo.client = mockClient;
455     clientInfo.config = { /* initialize configuration */ };
456     clientInfo.uiExtensionTokenId = 123;
457 
458     perUserSession->StopClientInput(std::make_shared<InputClientInfo>(clientInfo));
459 }