1 /*
2 * Copyright (C) 2021-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
16 #include <gtest/gtest.h>
17 #define private public
18 #define protected public
19 #include "input_method_ability.h"
20 #undef private
21
22 using namespace testing::ext;
23 namespace OHOS {
24 namespace MiscServices {
25 constexpr int32_t DEALY_TIME = 20;
26 class InputMethodAbilityExecptionTest : public testing::Test {
27 public:
SetUpTestCase(void)28 static void SetUpTestCase(void)
29 {
30 IMSA_HILOGI("InputMethodAbilityExecptionTest::SetUpTestCase");
31 inputMethodAbility_ = InputMethodAbility::GetInstance();
32 inputMethodAbility_->SetCoreAndAgent();
33 inputMethodAbility_->OnImeReady();
34 imeListener_ = std::make_shared<ImeListenerImpl>();
35 inputMethodAbility_->setImeListener(imeListener_);
36 std::unique_lock<std::mutex> lock(lock_);
37 cv_.wait_for(lock, std::chrono::milliseconds(DEALY_TIME),
38 [] { return InputMethodAbilityExecptionTest::isInputStart_; });
39 inputMethodAbility_->dataChannel_ = nullptr;
40 }
TearDownTestCase(void)41 static void TearDownTestCase(void)
42 {
43 IMSA_HILOGI("InputMethodAbilityExecptionTest::TearDownTestCase");
44 inputMethodAbility_->imeListener_ = nullptr;
45 }
SetUp()46 void SetUp()
47 {
48 }
TearDown()49 void TearDown()
50 {
51 }
52 class ImeListenerImpl : public InputMethodEngineListener {
53 public:
ImeListenerImpl()54 ImeListenerImpl(){};
~ImeListenerImpl()55 ~ImeListenerImpl(){};
56 void OnKeyboardStatus(bool isShow) override;
57 void OnInputStart() override;
58 void OnInputStop(const std::string &imeId) override;
59 void OnSetCallingWindow(uint32_t windowId) override;
60 void OnSetSubtype(const SubProperty &property) override;
61 };
62 static sptr<InputMethodAbility> inputMethodAbility_;
63
64 private:
65 static std::shared_ptr<ImeListenerImpl> imeListener_;
66 static std::mutex lock_;
67 static std::condition_variable cv_;
68 static bool isInputStart_;
69 };
OnKeyboardStatus(bool isShow)70 void InputMethodAbilityExecptionTest::ImeListenerImpl::OnKeyboardStatus(bool isShow)
71 {
72 }
OnInputStart()73 void InputMethodAbilityExecptionTest::ImeListenerImpl::OnInputStart()
74 {
75 std::unique_lock<std::mutex> lock(InputMethodAbilityExecptionTest::lock_);
76 InputMethodAbilityExecptionTest::isInputStart_ = true;
77 InputMethodAbilityExecptionTest::cv_.notify_one();
78 }
OnInputStop(const std::string & imeId)79 void InputMethodAbilityExecptionTest::ImeListenerImpl::OnInputStop(const std::string &imeId)
80 {
81 }
OnSetCallingWindow(uint32_t windowId)82 void InputMethodAbilityExecptionTest::ImeListenerImpl::OnSetCallingWindow(uint32_t windowId)
83 {
84 }
OnSetSubtype(const SubProperty & property)85 void InputMethodAbilityExecptionTest::ImeListenerImpl::OnSetSubtype(const SubProperty &property)
86 {
87 }
88 sptr<InputMethodAbility> InputMethodAbilityExecptionTest::inputMethodAbility_;
89 std::shared_ptr<InputMethodAbilityExecptionTest::ImeListenerImpl> InputMethodAbilityExecptionTest::imeListener_;
90 std::mutex InputMethodAbilityExecptionTest::lock_;
91 std::condition_variable InputMethodAbilityExecptionTest::cv_;
92 bool InputMethodAbilityExecptionTest::isInputStart_ = false;
93
94 /**
95 * @tc.name: testMoveCursorExecption
96 * @tc.desc: InputMethodAbility MoveCursor
97 * @tc.type: FUNC
98 * @tc.require:
99 * @tc.author: Hollokin
100 */
101 HWTEST_F(InputMethodAbilityExecptionTest, testMoveCursorExecption, TestSize.Level0)
102 {
103 IMSA_HILOGI("InputMethodAbilityExecptionTest MoveCursor Test START");
104 auto ret = inputMethodAbility_->MoveCursor(4); // move cursor right
105 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
106 }
107
108 /**
109 * @tc.name: testInsertTextExecption
110 * @tc.desc: InputMethodAbility InsertText
111 * @tc.type: FUNC
112 * @tc.require:
113 * @tc.author: Hollokin
114 */
115 HWTEST_F(InputMethodAbilityExecptionTest, testInsertTextExecption, TestSize.Level0)
116 {
117 IMSA_HILOGI("InputMethodAbilityExecptionTest InsertText Test START");
118 auto ret = inputMethodAbility_->InsertText("text");
119 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
120 }
121
122 /**
123 * @tc.name: testSendFunctionKeyExecption
124 * @tc.desc: InputMethodAbility SendFunctionKey
125 * @tc.type: FUNC
126 * @tc.require:
127 * @tc.author: Hollokin
128 */
129 HWTEST_F(InputMethodAbilityExecptionTest, testSendFunctionKeyExecption, TestSize.Level0)
130 {
131 IMSA_HILOGI("InputMethodAbilityExecptionTest SendFunctionKey Test START");
132 auto ret = inputMethodAbility_->SendFunctionKey(0);
133 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
134 }
135
136 /**
137 * @tc.name: testDeleteExecptionText
138 * @tc.desc: InputMethodAbility DeleteForward & DeleteBackward
139 * @tc.type: FUNC
140 * @tc.require:
141 * @tc.author: Hollokin
142 */
143 HWTEST_F(InputMethodAbilityExecptionTest, testDeleteExecptionText, TestSize.Level0)
144 {
145 IMSA_HILOGI("InputMethodAbilityExecptionTest testDelete Test START");
146 int32_t deleteForwardLenth = 1;
147 auto ret = inputMethodAbility_->DeleteForward(deleteForwardLenth);
148 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
149 int32_t deleteBackwardLenth = 2;
150 ret = inputMethodAbility_->DeleteBackward(deleteBackwardLenth);
151 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
152 }
153
154 /**
155 * @tc.name: testGetTextExecption001
156 * @tc.desc: InputMethodAbility GetTextBeforeCursor & GetTextAfterCursor
157 * @tc.type: FUNC
158 * @tc.require:
159 * @tc.author: Hollokin
160 */
161 HWTEST_F(InputMethodAbilityExecptionTest, testGetTextExecption001, TestSize.Level0)
162 {
163 IMSA_HILOGI("InputMethodAbilityExecptionTest testGetText001 START");
164 std::u16string text;
165 auto ret = inputMethodAbility_->GetTextAfterCursor(8, text);
166 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
167 ret = inputMethodAbility_->GetTextBeforeCursor(3, text);
168 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
169 }
170
171 /**
172 * @tc.name: testGetEnterKeyTypeExecption
173 * @tc.desc: InputMethodAbility GetEnterKeyType & GetInputPattern
174 * @tc.type: FUNC
175 * @tc.require:
176 * @tc.author: Hollokin
177 */
178 HWTEST_F(InputMethodAbilityExecptionTest, testGetEnterKeyTypeExecption, TestSize.Level0)
179 {
180 IMSA_HILOGI("InputMethodAbilityExecptionTest testGetEnterKeyType START");
181 int32_t keyType2;
182 auto ret = inputMethodAbility_->GetEnterKeyType(keyType2);
183 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
184 int32_t inputPattern;
185 ret = inputMethodAbility_->GetInputPattern(inputPattern);
186 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
187 }
188 } // namespace MiscServices
189 } // namespace OHOS
190