1 /*
2 * Copyright (c) 2021 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 "input_method_panel.h"
16
17 #include <gtest/gtest.h>
18 #include <sys/time.h>
19 #include <unistd.h>
20
21 #include <condition_variable>
22 #include <cstdint>
23 #include <string>
24
25 #include "display_manager.h"
26 #include "global.h"
27 #include "input_method_controller.h"
28 #include "panel_status_listener.h"
29 #include "tdd_util.h"
30
31 using namespace testing::ext;
32 namespace OHOS {
33 namespace MiscServices {
34 constexpr uint32_t IMC_WAIT_PANEL_STATUS_LISTEN_TIME = 200;
35 enum ListeningStatus : uint32_t { ON, OFF, NONE };
36 class InputMethodPanelTest : public testing::Test {
37 public:
38 static void SetUpTestCase(void);
39 static void TearDownTestCase(void);
40 void SetUp();
41 void TearDown();
42 static std::shared_ptr<InputMethodPanel> CreatePanel();
43 static void TriggerShowCallback(std::shared_ptr<InputMethodPanel> &inputMethodPanel);
44 static void TriggerHideCallback(std::shared_ptr<InputMethodPanel> &inputMethodPanel);
45 static void ImcPanelListeningTestCheck(
46 InputWindowStatus realStatus, InputWindowStatus waitStatus, const InputWindowInfo &windowInfo);
47 static void ImcPanelListeningTestCheck(InputWindowStatus realStatus, InputWindowStatus waitStatus);
48 static void ImcPanelListeningTestPrepare(
49 const std::shared_ptr<InputMethodPanel> &inputMethodPanel, const PanelInfo &info, ListeningStatus status);
50 static void ImcPanelListeningTestRestore(InputWindowStatus status);
51 static void ImcPanelListeningTestClear(const std::shared_ptr<InputMethodPanel> &inputMethodPanel);
52 class PanelStatusListenerImpl : public PanelStatusListener {
53 public:
PanelStatusListenerImpl()54 PanelStatusListenerImpl()
55 {
56 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("InputMethodPanelTest");
57 panelHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
58 }
59 ~PanelStatusListenerImpl() = default;
OnPanelStatus(uint32_t windowId,bool isShow)60 void OnPanelStatus(uint32_t windowId, bool isShow)
61 {
62 showPanel_ = isShow;
63 hidePanel_ = !isShow;
64 InputMethodPanelTest::panelListenerCv_.notify_one();
65 IMSA_HILOGI("PanelStatusListenerImpl OnPanelStatus in, isShow is %{public}s", isShow ? "true" : "false");
66 }
67 };
68 static std::mutex imcPanelStatusListenerLock_;
69 static std::condition_variable imcPanelStatusListenerCv_;
70 static InputWindowStatus status_;
71 static std::vector<InputWindowInfo> windowInfo_;
72 static sptr<InputMethodController> imc_;
73 static uint32_t windowWidth_;
74 static uint32_t windowHeight_;
75 static bool showPanel_;
76 static bool hidePanel_;
77 static std::condition_variable panelListenerCv_;
78 static std::mutex panelListenerLock_;
79 static constexpr uint32_t DELAY_TIME = 100;
80 static constexpr int32_t INTERVAL = 10;
81 static std::shared_ptr<AppExecFwk::EventHandler> panelHandler_;
82 static uint64_t tokenId_;
83 };
84 class InputMethodSettingListenerImpl : public InputMethodSettingListener {
85 public:
86 InputMethodSettingListenerImpl() = default;
87 ~InputMethodSettingListenerImpl() = default;
OnImeChange(const Property & property,const SubProperty & subProperty)88 void OnImeChange(const Property &property, const SubProperty &subProperty)
89 {
90 }
OnPanelStatusChange(const InputWindowStatus & status,const std::vector<InputWindowInfo> & windowInfo)91 void OnPanelStatusChange(const InputWindowStatus &status, const std::vector<InputWindowInfo> &windowInfo)
92 {
93 IMSA_HILOGI("InputMethodPanelTest::OnPanelStatusChange");
94 {
95 std::unique_lock<std::mutex> lock(InputMethodPanelTest::imcPanelStatusListenerLock_);
96 InputMethodPanelTest::status_ = status;
97 InputMethodPanelTest::windowInfo_ = windowInfo;
98 }
99 InputMethodPanelTest::imcPanelStatusListenerCv_.notify_one();
100 }
101 };
102 bool InputMethodPanelTest::showPanel_ = false;
103 bool InputMethodPanelTest::hidePanel_ = false;
104 std::condition_variable InputMethodPanelTest::panelListenerCv_;
105 std::mutex InputMethodPanelTest::panelListenerLock_;
106 std::shared_ptr<AppExecFwk::EventHandler> InputMethodPanelTest::panelHandler_{ nullptr };
107 std::condition_variable InputMethodPanelTest::imcPanelStatusListenerCv_;
108 std::mutex InputMethodPanelTest::imcPanelStatusListenerLock_;
109 InputWindowStatus InputMethodPanelTest::status_{ InputWindowStatus::HIDE };
110 std::vector<InputWindowInfo> InputMethodPanelTest::windowInfo_;
111 sptr<InputMethodController> InputMethodPanelTest::imc_;
112 uint32_t InputMethodPanelTest::windowWidth_ = 0;
113 uint32_t InputMethodPanelTest::windowHeight_ = 0;
114 uint64_t InputMethodPanelTest::tokenId_ = 0;
SetUpTestCase(void)115 void InputMethodPanelTest::SetUpTestCase(void)
116 {
117 IMSA_HILOGI("InputMethodPanelTest::SetUpTestCase");
118 TddUtil::StorageSelfTokenID();
119
120 auto listener = std::make_shared<InputMethodSettingListenerImpl>();
121 imc_ = InputMethodController::GetInstance();
122 imc_->SetSettingListener(listener);
123 TddUtil::SetTestTokenID(TddUtil::AllocTestTokenID(true, true, "undefined"));
124 auto ret = imc_->SwitchInputMethod("com.example.testIme");
125 if (ret != ErrorCode::NO_ERROR) {
126 IMSA_HILOGI("SwitchInputMethod failed, ret = %{public}d", ret);
127 return;
128 }
129 TddUtil::RestoreSelfTokenID();
130 std::shared_ptr<Property> property = InputMethodController::GetInstance()->GetCurrentInputMethod();
131 std::string bundleName = property != nullptr ? property->name : "default.inputmethod.unittest";
132 tokenId_ = TddUtil::AllocTestTokenID(true, true, bundleName);
133 }
134
TearDownTestCase(void)135 void InputMethodPanelTest::TearDownTestCase(void)
136 {
137 IMSA_HILOGI("InputMethodPanelTest::TearDownTestCase");
138 }
139
SetUp(void)140 void InputMethodPanelTest::SetUp(void)
141 {
142 IMSA_HILOGI("InputMethodPanelTest::SetUp");
143 }
144
TearDown(void)145 void InputMethodPanelTest::TearDown(void)
146 {
147 IMSA_HILOGI("InputMethodPanelTest::TearDown");
148 }
149
CreatePanel()150 std::shared_ptr<InputMethodPanel> InputMethodPanelTest::CreatePanel()
151 {
152 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
153 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
154 auto ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
155 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
156 return inputMethodPanel;
157 }
158
TriggerShowCallback(std::shared_ptr<InputMethodPanel> & inputMethodPanel)159 void InputMethodPanelTest::TriggerShowCallback(std::shared_ptr<InputMethodPanel> &inputMethodPanel)
160 {
161 panelHandler_->PostTask([&inputMethodPanel]() { inputMethodPanel->ShowPanel(); }, InputMethodPanelTest::INTERVAL);
162 {
163 std::unique_lock<std::mutex> lock(InputMethodPanelTest::panelListenerLock_);
164 InputMethodPanelTest::panelListenerCv_.wait_for(lock,
165 std::chrono::milliseconds(InputMethodPanelTest::DELAY_TIME),
166 [] { return InputMethodPanelTest::showPanel_; });
167 }
168 }
169
TriggerHideCallback(std::shared_ptr<InputMethodPanel> & inputMethodPanel)170 void InputMethodPanelTest::TriggerHideCallback(std::shared_ptr<InputMethodPanel> &inputMethodPanel)
171 {
172 panelHandler_->PostTask([&inputMethodPanel]() { inputMethodPanel->HidePanel(); }, InputMethodPanelTest::INTERVAL);
173 {
174 std::unique_lock<std::mutex> lock(InputMethodPanelTest::panelListenerLock_);
175 InputMethodPanelTest::panelListenerCv_.wait_for(lock,
176 std::chrono::milliseconds(InputMethodPanelTest::DELAY_TIME),
177 [] { return InputMethodPanelTest::hidePanel_; });
178 }
179 }
180
ImcPanelListeningTestCheck(InputWindowStatus realStatus,InputWindowStatus waitStatus,const InputWindowInfo & windowInfo)181 void InputMethodPanelTest::ImcPanelListeningTestCheck(
182 InputWindowStatus realStatus, InputWindowStatus waitStatus, const InputWindowInfo &windowInfo)
183 {
184 std::unique_lock<std::mutex> lock(imcPanelStatusListenerLock_);
185 imcPanelStatusListenerCv_.wait_for(lock, std::chrono::milliseconds(IMC_WAIT_PANEL_STATUS_LISTEN_TIME),
186 [&waitStatus] { return waitStatus == status_; });
187 EXPECT_EQ(status_, realStatus);
188 ASSERT_EQ(windowInfo_.size(), 1);
189 IMSA_HILOGI("InputMethodPanelTest::name: %{public}s, top: %{public}d, left: %{public}d",
190 windowInfo_[0].name.c_str(), windowInfo_[0].top, windowInfo_[0].left);
191 EXPECT_FALSE(windowInfo_[0].name.empty());
192 EXPECT_EQ(windowInfo_[0].width, windowInfo.width);
193 EXPECT_EQ(windowInfo_[0].height, windowInfo.height);
194 }
195
ImcPanelListeningTestCheck(InputWindowStatus realStatus,InputWindowStatus waitStatus)196 void InputMethodPanelTest::ImcPanelListeningTestCheck(InputWindowStatus realStatus, InputWindowStatus waitStatus)
197 {
198 std::unique_lock<std::mutex> lock(imcPanelStatusListenerLock_);
199 imcPanelStatusListenerCv_.wait_for(lock, std::chrono::milliseconds(IMC_WAIT_PANEL_STATUS_LISTEN_TIME),
200 [&waitStatus] { return waitStatus == status_; });
201 EXPECT_EQ(status_, realStatus);
202 EXPECT_TRUE(windowInfo_.empty());
203 }
204
ImcPanelListeningTestPrepare(const std::shared_ptr<InputMethodPanel> & inputMethodPanel,const PanelInfo & info,ListeningStatus status)205 void InputMethodPanelTest::ImcPanelListeningTestPrepare(
206 const std::shared_ptr<InputMethodPanel> &inputMethodPanel, const PanelInfo &info, ListeningStatus status)
207 {
208 auto ret = inputMethodPanel->CreatePanel(nullptr, info);
209 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
210 auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
211 ASSERT_TRUE(defaultDisplay != nullptr);
212 windowWidth_ = defaultDisplay->GetWidth() - 1;
213 windowHeight_ = 1;
214 ret = inputMethodPanel->Resize(windowWidth_, windowHeight_);
215 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
216 switch (status) {
217 case ListeningStatus::NONE: {
218 break;
219 }
220 case ListeningStatus::ON: {
221 imc_->UpdateListenEventFlag("imeShow", true);
222 imc_->UpdateListenEventFlag("imeHide", true);
223 break;
224 }
225 case ListeningStatus::OFF: {
226 imc_->UpdateListenEventFlag("imeShow", false);
227 imc_->UpdateListenEventFlag("imeHide", false);
228 break;
229 }
230 default:
231 break;
232 }
233 }
234
ImcPanelListeningTestRestore(InputWindowStatus status)235 void InputMethodPanelTest::ImcPanelListeningTestRestore(InputWindowStatus status)
236 {
237 status_ = status;
238 windowInfo_.clear();
239 }
240
ImcPanelListeningTestClear(const std::shared_ptr<InputMethodPanel> & inputMethodPanel)241 void InputMethodPanelTest::ImcPanelListeningTestClear(const std::shared_ptr<InputMethodPanel> &inputMethodPanel)
242 {
243 inputMethodPanel->DestroyPanel();
244 }
245
246 /**
247 * @tc.name: testCreatePanel
248 * @tc.desc: Test CreatePanel.
249 * @tc.type: FUNC
250 */
251 HWTEST_F(InputMethodPanelTest, testCreatePanel, TestSize.Level0)
252 {
253 IMSA_HILOGI("InputMethodPanelTest::testCreatePanel start.");
254 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
255 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FLOATING };
256 auto ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
257 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
258 ret = inputMethodPanel->DestroyPanel();
259 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
260 }
261
262 /**
263 * @tc.name: testDestroyPanel
264 * @tc.desc: Test DestroyPanel.
265 * @tc.type: FUNC
266 */
267 HWTEST_F(InputMethodPanelTest, testDestroyPanel, TestSize.Level0)
268 {
269 IMSA_HILOGI("InputMethodPanelTest::testDestroyPanel start.");
270 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
271 // not CreatePanel, DestroyPanel failed
272 auto ret = inputMethodPanel->DestroyPanel();
273 EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
274 }
275
276 /**
277 * @tc.name: testResizePanel
278 * @tc.desc: Test Resize panel. All panels can be resized.
279 * @tc.type: FUNC
280 */
281 HWTEST_F(InputMethodPanelTest, testResizePanel, TestSize.Level0)
282 {
283 IMSA_HILOGI("InputMethodPanelTest::testResizePanel start.");
284 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
285 // not CreatePanel, Resize failed
286 auto ret = inputMethodPanel->Resize(1, 1);
287 EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
288
289 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FLOATING };
290 ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
291 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
292 auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
293 EXPECT_TRUE(defaultDisplay != nullptr);
294 int32_t width = defaultDisplay->GetWidth();
295 int32_t height = defaultDisplay->GetHeight();
296
297 ret = inputMethodPanel->Resize(width - 1, height * 0.6 - 1);
298 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
299
300 ret = inputMethodPanel->Resize(width, height * 0.6);
301 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
302
303 ret = inputMethodPanel->Resize(width + 1, height * 0.6);
304 EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
305
306 ret = inputMethodPanel->Resize(width, height * 0.6 + 1);
307 EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
308 ret = inputMethodPanel->DestroyPanel();
309 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
310 }
311
312 /**
313 * @tc.name: testMovePanel
314 * @tc.desc: Test Move panel. SOFT_KEYBOARD panel with FLG_FIXED can not be moved.
315 * @tc.type: FUNC
316 */
317 HWTEST_F(InputMethodPanelTest, testMovePanel, TestSize.Level0)
318 {
319 IMSA_HILOGI("InputMethodPanelTest::testMovePanel start.");
320 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
321 // not CreatePanel, MoveTo failed
322 auto ret = inputMethodPanel->MoveTo(10, 100);
323 EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
324
325 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
326 ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
327 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
328
329 ret = inputMethodPanel->MoveTo(10, 100);
330 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
331
332 ret = inputMethodPanel->ChangePanelFlag(PanelFlag::FLG_FLOATING);
333 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
334 ret = inputMethodPanel->MoveTo(10, 100);
335 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
336
337 ret = inputMethodPanel->DestroyPanel();
338 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
339
340 panelInfo.panelType = STATUS_BAR;
341 ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
342 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
343 ret = inputMethodPanel->MoveTo(10, 100);
344 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
345 ret = inputMethodPanel->DestroyPanel();
346 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
347 }
348
349 /**
350 * @tc.name: testShowPanel
351 * @tc.desc: Test Show panel.
352 * @tc.type: FUNC
353 */
354 HWTEST_F(InputMethodPanelTest, testShowPanel, TestSize.Level0)
355 {
356 IMSA_HILOGI("InputMethodPanelTest::testShowPanel start.");
357 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
358 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
359 // 0、not create panel, show panel failed.
360 auto ret = inputMethodPanel->ShowPanel();
361 EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
362 ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
363 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
364 ret = inputMethodPanel->ShowPanel();
365 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
366
367 auto statusListener = std::make_shared<InputMethodPanelTest::PanelStatusListenerImpl>();
368 EXPECT_TRUE(statusListener != nullptr);
369 std::string type = "show";
370 inputMethodPanel->SetPanelStatusListener(statusListener, type);
371 ret = inputMethodPanel->ShowPanel();
372 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
373
374 // 2、show floating type panel.
375 ret = inputMethodPanel->ChangePanelFlag(PanelFlag::FLG_FLOATING);
376 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
377 ret = inputMethodPanel->ShowPanel();
378 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
379
380 ret = inputMethodPanel->DestroyPanel();
381 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
382
383 // 4、show status bar.
384 panelInfo.panelType = STATUS_BAR;
385 ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
386 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
387 ret = inputMethodPanel->ShowPanel();
388 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
389 ret = inputMethodPanel->HidePanel();
390 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
391 ret = inputMethodPanel->DestroyPanel();
392 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
393 }
394
395 /**
396 * @tc.name: testSetPanelStatusListener
397 * @tc.desc: Test SetPanelStatusListener.
398 * @tc.type: FUNC
399 */
400 HWTEST_F(InputMethodPanelTest, testSetPanelStatusListener, TestSize.Level0)
401 {
402 IMSA_HILOGI("InputMethodPanelTest::testSetPanelStatusListener start.");
403 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
404 auto statusListener = std::make_shared<InputMethodPanelTest::PanelStatusListenerImpl>();
405 // on('show')->on('hide')->show->hide
406 std::string type = "show";
407 inputMethodPanel->SetPanelStatusListener(statusListener, type);
408 type = "hide";
409 inputMethodPanel->SetPanelStatusListener(statusListener, type);
410
411 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
412 auto ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
413 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
414
415 InputMethodPanelTest::TriggerShowCallback(inputMethodPanel);
416 EXPECT_TRUE(InputMethodPanelTest::showPanel_);
417
418 InputMethodPanelTest::TriggerHideCallback(inputMethodPanel);
419 EXPECT_TRUE(InputMethodPanelTest::hidePanel_);
420 ret = inputMethodPanel->DestroyPanel();
421 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
422 }
423
424 /**
425 * @tc.name: testGetPanelType
426 * @tc.desc: Test GetPanelType.
427 * @tc.type: FUNC
428 */
429 HWTEST_F(InputMethodPanelTest, testGetPanelType, TestSize.Level0)
430 {
431 IMSA_HILOGI("InputMethodPanelTest::testGetPanelType start.");
432 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
433 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FLOATING };
434 auto ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
435 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
436 auto type = inputMethodPanel->GetPanelType();
437 EXPECT_EQ(type, panelInfo.panelType);
438 ret = inputMethodPanel->DestroyPanel();
439 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
440 }
441
442 /**
443 * @tc.name: testGetPanelFlag
444 * @tc.desc: Test GetPanelFlag.
445 * @tc.type: FUNC
446 */
447 HWTEST_F(InputMethodPanelTest, testGetPanelFlag, TestSize.Level0)
448 {
449 IMSA_HILOGI("InputMethodPanelTest::testGetPanelFlag start.");
450 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
451 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FLOATING };
452 auto ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
453 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
454 auto flag = inputMethodPanel->GetPanelFlag();
455 EXPECT_EQ(flag, panelInfo.panelFlag);
456
457 ret = inputMethodPanel->ChangePanelFlag(PanelFlag::FLG_CANDIDATE_COLUMN);
458 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
459 flag = inputMethodPanel->GetPanelFlag();
460 EXPECT_EQ(flag, PanelFlag::FLG_CANDIDATE_COLUMN);
461
462 ret = inputMethodPanel->DestroyPanel();
463 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
464 }
465
466 /**
467 * @tc.name: testChangePanelFlag
468 * @tc.desc: Test ChangePanelFlag.
469 * @tc.type: FUNC
470 */
471 HWTEST_F(InputMethodPanelTest, testChangePanelFlag, TestSize.Level0)
472 {
473 IMSA_HILOGI("InputMethodPanelTest::testChangePanelFlag start.");
474 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
475 PanelFlag flag = FLG_FLOATING;
476
477 // not CreatePanel, ChangePanelFlag failed
478 auto ret = inputMethodPanel->ChangePanelFlag(flag);
479 EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
480
481 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FLOATING };
482 ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
483 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
484
485 // panelFlag is same with the original
486 ret = inputMethodPanel->ChangePanelFlag(flag);
487 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
488
489 // panelFlag modify to FLG_FIXED
490 flag = FLG_FIXED;
491 ret = inputMethodPanel->ChangePanelFlag(flag);
492 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
493
494 inputMethodPanel->DestroyPanel();
495
496 panelInfo = { .panelType = STATUS_BAR, .panelFlag = FLG_FLOATING };
497 ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
498 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
499 // panelType is STATUS_BAR, not allow ChangePanelFlag
500 ret = inputMethodPanel->ChangePanelFlag(flag);
501 EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
502
503 inputMethodPanel->DestroyPanel();
504 }
505
506 /**
507 * @tc.name: testClearPanelListener
508 * @tc.desc: Test ClearPanelListener.
509 * @tc.type: FUNC
510 */
511 HWTEST_F(InputMethodPanelTest, testClearPanelListener, TestSize.Level0)
512 {
513 IMSA_HILOGI("InputMethodPanelTest::testClearPanelListener start.");
514 auto inputMethodPanel = InputMethodPanelTest::CreatePanel();
515
516 std::string subscribeType = "show";
517 inputMethodPanel->ClearPanelListener(subscribeType);
518 InputMethodPanelTest::TriggerShowCallback(inputMethodPanel);
519 EXPECT_EQ(InputMethodPanelTest::showPanel_, false);
520 InputMethodPanelTest::TriggerHideCallback(inputMethodPanel);
521 EXPECT_EQ(InputMethodPanelTest::hidePanel_, true);
522 InputMethodPanelTest::hidePanel_ = false;
523
524 subscribeType = "hide";
525 inputMethodPanel->ClearPanelListener(subscribeType);
526
527 InputMethodPanelTest::TriggerShowCallback(inputMethodPanel);
528 EXPECT_EQ(InputMethodPanelTest::showPanel_, false);
529 InputMethodPanelTest::TriggerHideCallback(inputMethodPanel);
530 EXPECT_EQ(InputMethodPanelTest::hidePanel_, false);
531
532 auto ret = inputMethodPanel->DestroyPanel();
533 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
534 }
535
536 /**
537 * @tc.name: testRegisterListener
538 * @tc.desc: Test ClearPanelListener.
539 * @tc.type: FUNC
540 */
541 HWTEST_F(InputMethodPanelTest, testRegisterListener, TestSize.Level0)
542 {
543 // on('show')->on('hide')->show->hide->off('show')->show->hide->on('show')->show
544 IMSA_HILOGI("InputMethodPanelTest::testRegisterListener start.");
545 auto inputMethodPanel = InputMethodPanelTest::CreatePanel();
546
547 auto statusListener = std::make_shared<InputMethodPanelTest::PanelStatusListenerImpl>();
548 std::string type = "show";
549 inputMethodPanel->SetPanelStatusListener(statusListener, type);
550 type = "hide";
551 inputMethodPanel->SetPanelStatusListener(statusListener, type);
552
553 InputMethodPanelTest::TriggerShowCallback(inputMethodPanel);
554 EXPECT_TRUE(InputMethodPanelTest::showPanel_);
555
556 InputMethodPanelTest::TriggerHideCallback(inputMethodPanel);
557 EXPECT_TRUE(InputMethodPanelTest::hidePanel_);
558
559 type = "show";
560 inputMethodPanel->ClearPanelListener(type);
561
562 InputMethodPanelTest::TriggerShowCallback(inputMethodPanel);
563 EXPECT_TRUE(!InputMethodPanelTest::showPanel_);
564
565 InputMethodPanelTest::TriggerHideCallback(inputMethodPanel);
566 EXPECT_TRUE(InputMethodPanelTest::hidePanel_);
567
568 inputMethodPanel->SetPanelStatusListener(statusListener, type);
569
570 InputMethodPanelTest::TriggerShowCallback(inputMethodPanel);
571 EXPECT_TRUE(InputMethodPanelTest::showPanel_);
572
573 auto ret = inputMethodPanel->DestroyPanel();
574 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
575 }
576
577 /*
578 * @tc.name: testImcPanelListening_001
579 * @tc.desc: SOFT_KEYBOARD FLG_FIXED no listening set up systemApp currentIme
580 * @tc.type: FUNC
581 */
582 HWTEST_F(InputMethodPanelTest, testImcPanelListening_001, TestSize.Level0)
583 {
584 IMSA_HILOGI("InputMethodPanelTest::testImcPanelListening_001 start.");
585 TddUtil::SetTestTokenID(tokenId_);
586 InputMethodPanelTest::ImcPanelListeningTestRestore(InputWindowStatus::HIDE);
587 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
588 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
589 InputMethodPanelTest::ImcPanelListeningTestPrepare(inputMethodPanel, panelInfo, NONE);
590
591 auto ret = inputMethodPanel->ShowPanel();
592 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
593 InputMethodPanelTest::ImcPanelListeningTestCheck(InputWindowStatus::HIDE, InputWindowStatus::SHOW);
594
595 InputMethodPanelTest::ImcPanelListeningTestRestore(InputWindowStatus::SHOW);
596 ret = inputMethodPanel->HidePanel();
597 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
598 InputMethodPanelTest::ImcPanelListeningTestCheck(InputWindowStatus::SHOW, InputWindowStatus::HIDE);
599 InputMethodPanelTest::ImcPanelListeningTestClear(inputMethodPanel);
600 TddUtil::RestoreSelfTokenID();
601 }
602
603 /**
604 * @tc.name: testImcPanelListening_002
605 * @tc.desc: SOFT_KEYBOARD FLG_FIXED Set up listening systemApp currentIme
606 * @tc.type: FUNC
607 */
608 HWTEST_F(InputMethodPanelTest, testImcPanelListening_002, TestSize.Level0)
609 {
610 IMSA_HILOGI("InputMethodPanelTest::testImcPanelListening_002 start.");
611 TddUtil::SetTestTokenID(tokenId_);
612 InputMethodPanelTest::ImcPanelListeningTestRestore(InputWindowStatus::HIDE);
613 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
614 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
615 InputMethodPanelTest::ImcPanelListeningTestPrepare(inputMethodPanel, panelInfo, ON);
616
617 auto ret = inputMethodPanel->ShowPanel();
618 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
619 InputMethodPanelTest::ImcPanelListeningTestCheck(InputWindowStatus::SHOW, InputWindowStatus::SHOW,
620 { "", 0, 0, InputMethodPanelTest::windowWidth_, InputMethodPanelTest::windowHeight_ });
621
622 InputMethodPanelTest::ImcPanelListeningTestRestore(InputWindowStatus::SHOW);
623 ret = inputMethodPanel->HidePanel();
624 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
625 InputMethodPanelTest::ImcPanelListeningTestCheck(InputWindowStatus::HIDE, InputWindowStatus::HIDE,
626 { "", 0, 0, InputMethodPanelTest::windowWidth_, InputMethodPanelTest::windowHeight_ });
627 InputMethodPanelTest::ImcPanelListeningTestClear(inputMethodPanel);
628 TddUtil::RestoreSelfTokenID();
629 }
630
631 /**
632 * @tc.name: testImcPanelListening_003
633 * @tc.desc: SOFT_KEYBOARD FLG_FIXED Cancel listening systemApp currentIme
634 * @tc.type: FUNC
635 */
636 HWTEST_F(InputMethodPanelTest, testImcPanelListening_003, TestSize.Level0)
637 {
638 IMSA_HILOGI("InputMethodPanelTest::testImcPanelListening_003 start.");
639 TddUtil::SetTestTokenID(tokenId_);
640 InputMethodPanelTest::ImcPanelListeningTestRestore(InputWindowStatus::HIDE);
641 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
642 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
643 InputMethodPanelTest::ImcPanelListeningTestPrepare(inputMethodPanel, panelInfo, OFF);
644
645 auto ret = inputMethodPanel->ShowPanel();
646 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
647 InputMethodPanelTest::ImcPanelListeningTestCheck(InputWindowStatus::HIDE, InputWindowStatus::SHOW);
648
649 InputMethodPanelTest::ImcPanelListeningTestRestore(InputWindowStatus::SHOW);
650 ret = inputMethodPanel->HidePanel();
651 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
652 InputMethodPanelTest::ImcPanelListeningTestCheck(InputWindowStatus::SHOW, InputWindowStatus::HIDE);
653 InputMethodPanelTest::ImcPanelListeningTestClear(inputMethodPanel);
654 TddUtil::RestoreSelfTokenID();
655 }
656
657 /**
658 * @tc.name: testImcPanelListening_004
659 * @tc.desc: SOFT_KEYBOARD FLG_FIXED Set up listening not systemApp currentIme
660 * @tc.type: FUNC
661 */
662 HWTEST_F(InputMethodPanelTest, testImcPanelListening_004, TestSize.Level0)
663 {
664 IMSA_HILOGI("InputMethodPanelTest::testImcPanelListening_004 start.");
665 InputMethodPanelTest::ImcPanelListeningTestRestore(InputWindowStatus::HIDE);
666 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
667 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
668 InputMethodPanelTest::ImcPanelListeningTestPrepare(inputMethodPanel, panelInfo, ON);
669
670 TddUtil::SetTestTokenID(tokenId_);
671 auto ret = inputMethodPanel->ShowPanel();
672 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
673 InputMethodPanelTest::ImcPanelListeningTestCheck(InputWindowStatus::HIDE, InputWindowStatus::SHOW);
674
675 InputMethodPanelTest::ImcPanelListeningTestRestore(InputWindowStatus::SHOW);
676 ret = inputMethodPanel->HidePanel();
677 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
678 InputMethodPanelTest::ImcPanelListeningTestCheck(InputWindowStatus::SHOW, InputWindowStatus::HIDE);
679 InputMethodPanelTest::ImcPanelListeningTestClear(inputMethodPanel);
680 TddUtil::RestoreSelfTokenID();
681 }
682
683 /**
684 * @tc.name: testImcPanelListening_005
685 * @tc.desc: SOFT_KEYBOARD FLG_FIXED Set up listening systemApp not currentIme
686 * @tc.type: FUNC
687 */
688 HWTEST_F(InputMethodPanelTest, testImcPanelListening_005, TestSize.Level0)
689 {
690 IMSA_HILOGI("InputMethodPanelTest::testImcPanelListening_005 start.");
691 TddUtil::SetTestTokenID(tokenId_);
692 InputMethodPanelTest::ImcPanelListeningTestRestore(InputWindowStatus::HIDE);
693 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
694 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
695 InputMethodPanelTest::ImcPanelListeningTestPrepare(inputMethodPanel, panelInfo, ON);
696
697 TddUtil::RestoreSelfTokenID();
698 auto ret = inputMethodPanel->ShowPanel();
699 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
700 InputMethodPanelTest::ImcPanelListeningTestCheck(InputWindowStatus::HIDE, InputWindowStatus::SHOW);
701
702 InputMethodPanelTest::ImcPanelListeningTestRestore(InputWindowStatus::SHOW);
703 ret = inputMethodPanel->HidePanel();
704 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
705 InputMethodPanelTest::ImcPanelListeningTestCheck(InputWindowStatus::SHOW, InputWindowStatus::HIDE);
706 InputMethodPanelTest::ImcPanelListeningTestClear(inputMethodPanel);
707 }
708
709 /**
710 * @tc.name: testSetCallingWindow
711 * @tc.desc: test SetCallingWindow
712 * @tc.type: FUNC
713 */
714 HWTEST_F(InputMethodPanelTest, testSetCallingWindow, TestSize.Level0)
715 {
716 IMSA_HILOGI("InputMethodPanelTest::testSetCallingWindow start.");
717 auto inputMethodPanel = std::make_shared<InputMethodPanel>();
718 // not CreatePanel, SetCallingWindow failed
719 uint32_t windowId = 8;
720 auto ret = inputMethodPanel->SetCallingWindow(windowId);
721 EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
722
723 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
724 ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
725 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
726
727 ret = inputMethodPanel->SetCallingWindow(windowId);
728 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
729 }
730 } // namespace MiscServices
731 } // namespace OHOS