1 /* 2 * Copyright (c) 2024 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 <gmock/gmock.h> 17 #include <gtest/gtest.h> 18 #include <string_ex.h> 19 #include <unistd.h> 20 21 #include "global.h" 22 #include "ime_event_listener.h" 23 #include "ime_setting_listener_test_impl.h" 24 #include "ime_system_channel.h" 25 #include "input_method_controller.h" 26 #include "input_method_engine_listener.h" 27 #include "input_method_utils.h" 28 29 using namespace testing; 30 using namespace testing::ext; 31 namespace OHOS { 32 namespace MiscServices { 33 /** 34 * @brief Only pure virtual functions are implemented. 35 */ 36 class TextListenerImpl : public OnTextChangedListener { 37 public: InsertText(const std::u16string & text)38 void InsertText(const std::u16string &text) override 39 { 40 } DeleteForward(int32_t length)41 void DeleteForward(int32_t length) override 42 { 43 } DeleteBackward(int32_t length)44 void DeleteBackward(int32_t length) override 45 { 46 } SendKeyEventFromInputMethod(const KeyEvent & event)47 void SendKeyEventFromInputMethod(const KeyEvent &event) override 48 { 49 } SendKeyboardStatus(const KeyboardStatus & keyboardStatus)50 void SendKeyboardStatus(const KeyboardStatus &keyboardStatus) override 51 { 52 } SendFunctionKey(const FunctionKey & functionKey)53 void SendFunctionKey(const FunctionKey &functionKey) override 54 { 55 } SetKeyboardStatus(bool status)56 void SetKeyboardStatus(bool status) override 57 { 58 } MoveCursor(const Direction direction)59 void MoveCursor(const Direction direction) override 60 { 61 } HandleSetSelection(int32_t start,int32_t end)62 void HandleSetSelection(int32_t start, int32_t end) override 63 { 64 } HandleExtendAction(int32_t action)65 void HandleExtendAction(int32_t action) override 66 { 67 } HandleSelect(int32_t keyCode,int32_t cursorMoveSkip)68 void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) override 69 { 70 } GetLeftTextOfCursor(int32_t number)71 std::u16string GetLeftTextOfCursor(int32_t number) override 72 { 73 return Str8ToStr16("test"); 74 } GetRightTextOfCursor(int32_t number)75 std::u16string GetRightTextOfCursor(int32_t number) override 76 { 77 return Str8ToStr16("test"); 78 } GetTextIndexAtCursor()79 int32_t GetTextIndexAtCursor() override 80 { 81 return 0; 82 } 83 }; 84 /** 85 * @brief Only pure virtual functions are implemented. 86 */ 87 class EngineListenerImpl : public InputMethodEngineListener { 88 public: OnKeyboardStatus(bool isShow)89 void OnKeyboardStatus(bool isShow) override 90 { 91 } OnInputStart()92 void OnInputStart() override 93 { 94 } OnInputStop()95 int32_t OnInputStop() override 96 { 97 return ErrorCode::NO_ERROR; 98 } OnSecurityChange(int32_t security)99 void OnSecurityChange(int32_t security) override 100 { 101 } OnSetCallingWindow(uint32_t windowId)102 void OnSetCallingWindow(uint32_t windowId) override 103 { 104 } OnSetSubtype(const SubProperty & property)105 void OnSetSubtype(const SubProperty &property) override 106 { 107 } ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)108 void ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override 109 { 110 } 111 }; 112 /** 113 * @brief Only pure virtual functions are implemented. 114 */ 115 class EventListenerImpl : public ImeEventListener { 116 }; 117 /** 118 * @brief Only pure virtual functions are implemented. 119 */ 120 class SystemCmdChannelImpl : public OnSystemCmdListener { 121 }; 122 123 class SystemCmdChannelListenerImpl : public OnSystemCmdListener { 124 public: ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)125 void ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override 126 { 127 isReceivePrivateCommand_ = true; 128 } NotifyPanelStatus(const SysPanelStatus & sysPanelStatus)129 void NotifyPanelStatus(const SysPanelStatus &sysPanelStatus)override 130 { 131 isNotifyIsShowSysPanel_ = true; 132 } ResetParam()133 static void ResetParam() 134 { 135 isReceivePrivateCommand_ = false; 136 isNotifyIsShowSysPanel_ = false; 137 } 138 static bool isReceivePrivateCommand_; 139 static bool isNotifyIsShowSysPanel_; 140 }; 141 bool SystemCmdChannelListenerImpl::isReceivePrivateCommand_{ false }; 142 bool SystemCmdChannelListenerImpl::isNotifyIsShowSysPanel_{ false }; 143 144 class VirtualListenerTest : public testing::Test { 145 public: SetUpTestCase(void)146 static void SetUpTestCase(void) 147 { 148 IMSA_HILOGI("VirtualListenerTest::SetUpTestCase"); 149 textListener_ = new (std::nothrow) TextListenerImpl(); 150 eventListener_ = std::make_shared<EventListenerImpl>(); 151 engineListener_ = std::make_shared<EngineListenerImpl>(); 152 systemCmdListener_ = new (std::nothrow) SystemCmdChannelImpl(); 153 } TearDownTestCase(void)154 static void TearDownTestCase(void) 155 { 156 IMSA_HILOGI("VirtualListenerTest::TearDownTestCase"); 157 } SetUp()158 void SetUp() 159 { 160 IMSA_HILOGI("VirtualListenerTest::SetUp"); 161 } TearDown()162 void TearDown() 163 { 164 IMSA_HILOGI("VirtualListenerTest::TearDown"); 165 } 166 static sptr<OnTextChangedListener> textListener_; 167 static std::shared_ptr<ImeEventListener> eventListener_; 168 static std::shared_ptr<InputMethodEngineListener> engineListener_; 169 static sptr<OnSystemCmdListener> systemCmdListener_; 170 }; 171 sptr<OnTextChangedListener> VirtualListenerTest::textListener_{ nullptr }; 172 std::shared_ptr<ImeEventListener> VirtualListenerTest::eventListener_{ nullptr }; 173 std::shared_ptr<InputMethodEngineListener> VirtualListenerTest::engineListener_{ nullptr }; 174 sptr<OnSystemCmdListener> VirtualListenerTest::systemCmdListener_{ nullptr }; 175 176 /** 177 * @tc.name: testOnTextChangedListener_001 178 * @tc.desc: Cover non-pure virtual function in class: OnTextChangedListener. 179 * @tc.type: FUNC 180 * @tc.require: 181 */ 182 HWTEST_F(VirtualListenerTest, testOnTextChangedListener_001, TestSize.Level0) 183 { 184 IMSA_HILOGI("VirtualListenerTest testOnTextChangedListener_001 START"); 185 ASSERT_NE(VirtualListenerTest::textListener_, nullptr); 186 PanelStatusInfo statusInfo; 187 Range range; 188 std::unordered_map<std::string, PrivateDataValue> privateCommand; 189 VirtualListenerTest::textListener_->NotifyPanelStatusInfo(statusInfo); 190 VirtualListenerTest::textListener_->NotifyKeyboardHeight(0); 191 int32_t ret = VirtualListenerTest::textListener_->ReceivePrivateCommand(privateCommand); 192 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 193 VirtualListenerTest::textListener_->FinishTextPreview(); 194 ret = VirtualListenerTest::textListener_->SetPreviewText(Str8ToStr16("test"), range); 195 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 196 } 197 198 /** 199 * @tc.name: testInputMethodEngineListener_001 200 * @tc.desc: Cover non-pure virtual function in class: InputMethodEngineListener. 201 * @tc.type: FUNC 202 * @tc.require: 203 */ 204 HWTEST_F(VirtualListenerTest, testInputMethodEngineListener_001, TestSize.Level0) 205 { 206 IMSA_HILOGI("VirtualListenerTest testInputMethodEngineListener_001 START"); 207 ASSERT_NE(VirtualListenerTest::engineListener_, nullptr); 208 VirtualListenerTest::engineListener_->OnInputFinish(); 209 bool isEnable = VirtualListenerTest::engineListener_->IsEnable(); 210 EXPECT_FALSE(isEnable); 211 } 212 213 /** 214 * @tc.name: testInputMethodEngineListener_002 215 * @tc.desc: Cover non-pure virtual function in class: InputMethodEngineListener. 216 * @tc.type: FUNC 217 * @tc.require: 218 */ 219 HWTEST_F(VirtualListenerTest, testInputMethodEngineListener_002, TestSize.Level0) 220 { 221 IMSA_HILOGI("VirtualListenerTest testInputMethodEngineListener_002 START"); 222 ASSERT_NE(VirtualListenerTest::engineListener_, nullptr); 223 int32_t security = 1; 224 VirtualListenerTest::engineListener_->OnSecurityChange(security); 225 bool isEnable = VirtualListenerTest::engineListener_->IsEnable(); 226 EXPECT_FALSE(isEnable); 227 } 228 229 /** 230 * @tc.name: testImeEventListener_001 231 * @tc.desc: Cover non-pure virtual function in class: ImeEventListener. 232 * @tc.type: FUNC 233 * @tc.require: 234 */ 235 HWTEST_F(VirtualListenerTest, testImeEventListener_001, TestSize.Level0) 236 { 237 IMSA_HILOGI("VirtualListenerTest testImeEventListener_001 START"); 238 ASSERT_NE(VirtualListenerTest::eventListener_, nullptr); 239 Property property; 240 SubProperty subProperty; 241 ImeWindowInfo imeWindowInfo; 242 auto listener = std::make_shared<ImeSettingListenerTestImpl>(); 243 ImeSettingListenerTestImpl::ResetParam(); 244 VirtualListenerTest::eventListener_->OnImeChange(property, subProperty); 245 VirtualListenerTest::eventListener_->OnImeShow(imeWindowInfo); 246 VirtualListenerTest::eventListener_->OnImeHide(imeWindowInfo); 247 EXPECT_FALSE(ImeSettingListenerTestImpl::WaitImeChange()); 248 EXPECT_FALSE(ImeSettingListenerTestImpl::WaitPanelHide()); 249 EXPECT_FALSE(ImeSettingListenerTestImpl::WaitPanelHide()); 250 } 251 252 /** 253 * @tc.name: testOnSystemCmdListener_001 254 * @tc.desc: Cover non-pure virtual function in class: OnSystemCmdListener. 255 * @tc.type: FUNC 256 * @tc.require: 257 */ 258 HWTEST_F(VirtualListenerTest, testOnSystemCmdListener_001, TestSize.Level0) 259 { 260 IMSA_HILOGI("VirtualListenerTest testOnSystemCmdListener_001 START"); 261 sptr<OnSystemCmdListener> listener = new (std::nothrow) SystemCmdChannelListenerImpl(); 262 ASSERT_NE(listener, nullptr); 263 ASSERT_NE(VirtualListenerTest::systemCmdListener_, nullptr); 264 SystemCmdChannelListenerImpl::ResetParam(); 265 std::unordered_map<std::string, PrivateDataValue> privateCommand; 266 VirtualListenerTest::systemCmdListener_->ReceivePrivateCommand(privateCommand); 267 VirtualListenerTest::systemCmdListener_->NotifyPanelStatus({ false, 0, 0, 0 }); 268 EXPECT_FALSE(SystemCmdChannelListenerImpl::isNotifyIsShowSysPanel_); 269 EXPECT_FALSE(SystemCmdChannelListenerImpl::isReceivePrivateCommand_); 270 SystemCmdChannelListenerImpl::ResetParam(); 271 listener->ReceivePrivateCommand(privateCommand); 272 listener->NotifyPanelStatus({ false, 0, 0, 0 }); 273 EXPECT_TRUE(SystemCmdChannelListenerImpl::isNotifyIsShowSysPanel_); 274 EXPECT_TRUE(SystemCmdChannelListenerImpl::isReceivePrivateCommand_); 275 } 276 } // namespace MiscServices 277 } // namespace OHOS