1 /*
2 * Copyright (C) 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 #include <gtest/gtest.h>
16 #include <sys/time.h>
17 #include <unistd.h>
18
19 #include <cstdint>
20 #include <string>
21
22 #include "accesstoken_kit.h"
23 #include "global.h"
24 #include "input_method_ability.h"
25 #include "input_method_controller.h"
26 #include "input_method_property.h"
27 #include "nativetoken_kit.h"
28 #include "os_account_manager.h"
29 #include "securec.h"
30 #include "token_setproc.h"
31
32 using namespace testing::ext;
33 using namespace OHOS::AccountSA;
34 using namespace OHOS::Security::AccessToken;
35 namespace OHOS {
36 namespace MiscServices {
37 constexpr int32_t MAIN_USER_ID = 100;
InitTestConfiguration(const std::string & bundleName)38 static void InitTestConfiguration(const std::string &bundleName)
39 {
40 IMSA_HILOGI("bundleName: %{public}s", bundleName.c_str());
41 std::vector<int32_t> userIds;
42 auto ret = OsAccountManager::QueryActiveOsAccountIds(userIds);
43 if (ret != ErrorCode::NO_ERROR || userIds.empty()) {
44 IMSA_HILOGE("query active os account id failed");
45 userIds[0] = MAIN_USER_ID;
46 }
47 HapInfoParams infoParams = {
48 .userID = userIds[0],
49 .bundleName = bundleName,
50 .instIndex = 0,
51 .appIDDesc = "ohos.inputmethod_test.demo"
52 };
53 HapPolicyParams policyParams = {
54 .apl = APL_NORMAL,
55 .domain = "test.domain.inputmethod",
56 .permList = {},
57 .permStateList = {}
58 };
59 AccessTokenKit::AllocHapToken(infoParams, policyParams);
60 auto tokenID = AccessTokenKit::GetHapTokenID(infoParams.userID, infoParams.bundleName, infoParams.instIndex);
61 int res = SetSelfTokenID(tokenID);
62 if (res == ErrorCode::NO_ERROR) {
63 IMSA_HILOGI("SetSelfTokenID success!");
64 } else {
65 IMSA_HILOGE("SetSelfTokenID fail!");
66 }
67 }
68
69 class TextListener : public OnTextChangedListener {
70 public:
71 TextListener() = default;
72 ~TextListener() override = default;
73 void InsertText(const std::u16string &text) override;
74 void DeleteBackward(int32_t length) override;
75 void SetKeyboardStatus(bool status) override;
76 void DeleteForward(int32_t length) override;
77 void SendKeyEventFromInputMethod(const KeyEvent &event) override;
78 void SendKeyboardInfo(const KeyboardInfo &status) override;
79 void MoveCursor(const Direction direction) override;
80 void HandleSetSelection(int32_t start, int32_t end) override;
81 void HandleExtendAction(int32_t action) override;
82 void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) override;
83 };
84
InsertText(const std::u16string & text)85 void TextListener::InsertText(const std::u16string &text)
86 {
87 }
DeleteBackward(int32_t length)88 void TextListener::DeleteBackward(int32_t length)
89 {
90 }
SetKeyboardStatus(bool status)91 void TextListener::SetKeyboardStatus(bool status)
92 {
93 }
DeleteForward(int32_t length)94 void TextListener::DeleteForward(int32_t length)
95 {
96 }
SendKeyEventFromInputMethod(const KeyEvent & event)97 void TextListener::SendKeyEventFromInputMethod(const KeyEvent &event)
98 {
99 }
SendKeyboardInfo(const KeyboardInfo & status)100 void TextListener::SendKeyboardInfo(const KeyboardInfo &status)
101 {
102 }
MoveCursor(const Direction direction)103 void TextListener::MoveCursor(const Direction direction)
104 {
105 }
HandleSetSelection(int32_t start,int32_t end)106 void TextListener::HandleSetSelection(int32_t start, int32_t end)
107 {
108 }
HandleExtendAction(int32_t action)109 void TextListener::HandleExtendAction(int32_t action)
110 {
111 }
112
HandleSelect(int32_t keyCode,int32_t cursorMoveSkip)113 void TextListener::HandleSelect(int32_t keyCode, int32_t cursorMoveSkip)
114 {
115 }
116
117 class PermissionVerificationExceptionTest : public testing::Test {
118 public:
119 static void SetUpTestCase(void);
120 static void TearDownTestCase(void);
121 void SetUp();
122 void TearDown();
123 static sptr<InputMethodController> imc_;
124 static sptr<OnTextChangedListener> textListener_;
125 };
126 sptr<InputMethodController> PermissionVerificationExceptionTest::imc_;
127 sptr<OnTextChangedListener> PermissionVerificationExceptionTest::textListener_;
SetUpTestCase(void)128 void PermissionVerificationExceptionTest::SetUpTestCase(void)
129 {
130 IMSA_HILOGI("PermissionVerificationExceptionTest::SetUpTestCase");
131 textListener_ = new TextListener();
132 imc_ = InputMethodController::GetInstance();
133 imc_->Attach(textListener_);
134 }
135
TearDownTestCase(void)136 void PermissionVerificationExceptionTest::TearDownTestCase(void)
137 {
138 IMSA_HILOGI("PermissionVerificationExceptionTest::TearDownTestCase");
139 }
140
SetUp(void)141 void PermissionVerificationExceptionTest::SetUp(void)
142 {
143 IMSA_HILOGI("PermissionVerificationExceptionTest::SetUp");
144
145 }
146
TearDown(void)147 void PermissionVerificationExceptionTest::TearDown(void)
148 {
149 IMSA_HILOGI("PermissionVerificationExceptionTest::TearDown");
150 }
151
152 /**
153 * @tc.name: ShowSoftKeyboard
154 * @tc.desc: PermissionVerificationExceptionTest ShowSoftKeyboard.
155 * @tc.type: FUNC
156 * @tc.require: issuesI640YZ
157 */
158 HWTEST_F(PermissionVerificationExceptionTest, ShowSoftKeyboard, TestSize.Level0)
159 {
160 IMSA_HILOGI("PermissionTest ShowSoftKeyboard TEST START");
161 int32_t ret = PermissionVerificationExceptionTest::imc_->ShowSoftKeyboard();
162 EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
163 }
164
165 /**
166 * @tc.name: HideSoftKeyboard
167 * @tc.desc: PermissionVerificationExceptionTest HideSoftKeyboard.
168 * @tc.type: FUNC
169 * @tc.require: issuesI640YZ
170 */
171 HWTEST_F(PermissionVerificationExceptionTest, HideSoftKeyboard, TestSize.Level0)
172 {
173 IMSA_HILOGI("PermissionTest HideSoftKeyboard TEST START");
174 int32_t ret = PermissionVerificationExceptionTest::imc_->HideSoftKeyboard();
175 EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
176 }
177
178 /**
179 * @tc.name: SwitchInputMethod
180 * @tc.desc: PermissionVerificationExceptionTest SwitchInputMethod.
181 * @tc.type: FUNC
182 * @tc.require: issuesI640YZ
183 */
184 HWTEST_F(PermissionVerificationExceptionTest, SwitchInputMethod, TestSize.Level0)
185 {
186 IMSA_HILOGI("PermissionTest SwitchInputMethod TEST START");
187 auto property = PermissionVerificationExceptionTest::imc_->GetCurrentInputMethod();
188 ASSERT_TRUE(property != nullptr);
189 auto subProperty = PermissionVerificationExceptionTest::imc_->GetCurrentInputMethodSubtype();
190 ASSERT_TRUE(subProperty != nullptr);
191
192 int32_t ret = PermissionVerificationExceptionTest::imc_->SwitchInputMethod(property->name, subProperty->label);
193 EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
194 }
195
196 /**
197 * @tc.name: ShowOptionalInputMethod
198 * @tc.desc: PermissionVerificationExceptionTest ShowOptionalInputMethod.
199 * @tc.type: FUNC
200 * @tc.require:
201 * @tc.author: Zhaolinglan
202 */
203 HWTEST_F(PermissionVerificationExceptionTest, ShowOptionalInputMethod, TestSize.Level0)
204 {
205 IMSA_HILOGI("PermissionTest ShowOptionalInputMethod TEST START");
206 int32_t ret = PermissionVerificationExceptionTest::imc_->ShowOptionalInputMethod();
207 EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
208 }
209
210 /**
211 * @tc.name: SetCoreAndAgent
212 * @tc.desc: PermissionVerificationExceptionTest SetCoreAndAgent.
213 * @tc.type: FUNC
214 * @tc.require:
215 * @tc.author: Zhaolinglan
216 */
217 HWTEST_F(PermissionVerificationExceptionTest, SetCoreAndAgent, TestSize.Level0)
218 {
219 IMSA_HILOGI("PermissionTest SetCoreAndAgent TEST START");
220 int32_t ret = InputMethodAbility::GetInstance()->SetCoreAndAgent();
221 EXPECT_EQ(ret, ErrorCode::ERROR_NOT_CURRENT_IME);
222 }
223
224 /**
225 * @tc.name: SetCoreAndAgentPassCheck
226 * @tc.desc: PermissionVerificationExceptionTest SetCoreAndAgentPassCheck.
227 * @tc.type: FUNC
228 * @tc.require:
229 * @tc.author: Zhaolinglan
230 */
231 HWTEST_F(PermissionVerificationExceptionTest, SetCoreAndAgentPassCheck, TestSize.Level0)
232 {
233 IMSA_HILOGI("PermissionTest SetCoreAndAgentPassCheck TEST START");
234 auto property = InputMethodController::GetInstance()->GetCurrentInputMethod();
235 EXPECT_NE(property, nullptr);
236 InitTestConfiguration(property->name);
237 int32_t ret = InputMethodAbility::GetInstance()->SetCoreAndAgent();
238 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
239 }
240 } // namespace MiscServices
241 } // namespace OHOS