1 /*
2 * Copyright (C) 2022-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 #define private public
16 #define protected public
17 #include "input_method_ability.h"
18 #undef private
19
20 #include <cstdint>
21 #include <gtest/gtest.h>
22 #include <regex>
23 #include <sstream>
24 #include <string>
25 #include <sys/time.h>
26 #include <unistd.h>
27
28 #include "global.h"
29 #include "input_method_ability.h"
30 #include "input_method_controller.h"
31 #include "input_method_engine_listener_impl.h"
32 #include "tdd_util.h"
33 #include "text_listener.h"
34
35 using namespace testing::ext;
36 namespace OHOS {
37 namespace MiscServices {
38 using WindowMgr = TddUtil::WindowManager;
39 class PermissionVerificationExceptionTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase(void);
43 void SetUp();
44 void TearDown();
45 static sptr<InputMethodController> imc_;
46 static sptr<OnTextChangedListener> textListener_;
47 static sptr<InputMethodAbility> ima_;
48 static uint64_t tokenId_;
49 };
50 sptr<InputMethodController> PermissionVerificationExceptionTest::imc_;
51 sptr<OnTextChangedListener> PermissionVerificationExceptionTest::textListener_;
52 sptr<InputMethodAbility> PermissionVerificationExceptionTest::ima_;
53 uint64_t PermissionVerificationExceptionTest::tokenId_ = 0;
54
SetUpTestCase(void)55 void PermissionVerificationExceptionTest::SetUpTestCase(void)
56 {
57 IMSA_HILOGI("PermissionVerificationExceptionTest::SetUpTestCase");
58 TddUtil::StorageSelfTokenID();
59 ima_ = InputMethodAbility::GetInstance();
60 ima_->OnImeReady();
61 ima_->SetImeListener(std::make_shared<InputMethodEngineListenerImpl>());
62 PermissionVerificationExceptionTest::textListener_ = new TextListener();
63 imc_ = InputMethodController::GetInstance();
64 auto property = InputMethodController::GetInstance()->GetCurrentInputMethod();
65 EXPECT_NE(property, nullptr);
66 tokenId_ = TddUtil::GetTestTokenID(property->name);
67
68 TddUtil::WindowManager::RegisterFocusChangeListener();
69 WindowMgr::CreateWindow();
70 }
71
TearDownTestCase(void)72 void PermissionVerificationExceptionTest::TearDownTestCase(void)
73 {
74 IMSA_HILOGI("PermissionVerificationExceptionTest::TearDownTestCase");
75 TddUtil::KillImsaProcess();
76 WindowMgr::DestroyWindow();
77 }
78
SetUp(void)79 void PermissionVerificationExceptionTest::SetUp(void)
80 {
81 IMSA_HILOGI("PermissionVerificationExceptionTest::SetUp");
82 }
83
TearDown(void)84 void PermissionVerificationExceptionTest::TearDown(void)
85 {
86 IMSA_HILOGI("PermissionVerificationExceptionTest::TearDown");
87 }
88
89 /**
90 * @tc.name: ShowAndHideSoftKeyboard
91 * @tc.desc: PermissionVerificationExceptionTest ShowAndHideSoftKeyboard.
92 * @tc.type: FUNC
93 * @tc.require: issuesI640YZ
94 */
95 HWTEST_F(PermissionVerificationExceptionTest, ShowAndHideSoftKeyboard, TestSize.Level0)
96 {
97 IMSA_HILOGI("PermissionTest ShowAndHideSoftKeyboard TEST START");
98 TddUtil::SetTestTokenID(tokenId_);
99 PermissionVerificationExceptionTest::ima_->SetCoreAndAgent();
100 TddUtil::RestoreSelfTokenID();
101
102 WindowMgr::ShowWindow();
103 bool isFocused = FocusChangedListenerTestImpl::isFocused_->GetValue();
104 IMSA_HILOGI("ShowAndHideSoftKeyboard getFocus end, isFocused = %{public}d", isFocused);
105 PermissionVerificationExceptionTest::imc_->Attach(PermissionVerificationExceptionTest::textListener_);
106 int32_t ret = PermissionVerificationExceptionTest::imc_->ShowSoftKeyboard();
107 EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
108 ret = PermissionVerificationExceptionTest::imc_->HideSoftKeyboard();
109 EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
110 WindowMgr::HideWindow();
111 bool unFocus = FocusChangedListenerTestImpl::unFocused_->GetValue();
112 IMSA_HILOGI("ShowAndHideSoftKeyboard unFocus end, unFocus = %{public}d", unFocus);
113 }
114
115 /**
116 * @tc.name: SwitchInputMethod
117 * @tc.desc: PermissionVerificationExceptionTest SwitchInputMethod.
118 * @tc.type: FUNC
119 * @tc.require: issuesI640YZ
120 */
121 HWTEST_F(PermissionVerificationExceptionTest, SwitchInputMethod, TestSize.Level0)
122 {
123 IMSA_HILOGI("PermissionTest SwitchInputMethod TEST START");
124 auto property = PermissionVerificationExceptionTest::imc_->GetCurrentInputMethod();
125 ASSERT_TRUE(property != nullptr);
126 auto subProperty = PermissionVerificationExceptionTest::imc_->GetCurrentInputMethodSubtype();
127 ASSERT_TRUE(subProperty != nullptr);
128
129 int32_t ret = PermissionVerificationExceptionTest::imc_->SwitchInputMethod(property->name, subProperty->id);
130 EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
131 }
132
133 /**
134 * @tc.name: SetCoreAndAgent
135 * @tc.desc: PermissionVerificationExceptionTest SetCoreAndAgent.
136 * @tc.type: FUNC
137 * @tc.author: Zhaolinglan
138 */
139 HWTEST_F(PermissionVerificationExceptionTest, SetCoreAndAgent, TestSize.Level0)
140 {
141 IMSA_HILOGI("PermissionTest SetCoreAndAgent TEST START");
142 InputMethodAbility::GetInstance()->isBound_.store(false);
143 int32_t ret = InputMethodAbility::GetInstance()->SetCoreAndAgent();
144 EXPECT_EQ(ret, ErrorCode::ERROR_NOT_CURRENT_IME);
145 }
146
147 /**
148 * @tc.name: SetCoreAndAgentPassCheck
149 * @tc.desc: PermissionVerificationExceptionTest SetCoreAndAgentPassCheck.
150 * @tc.type: FUNC
151 * @tc.author: Zhaolinglan
152 */
153 HWTEST_F(PermissionVerificationExceptionTest, SetCoreAndAgentPassCheck, TestSize.Level0)
154 {
155 IMSA_HILOGI("PermissionTest SetCoreAndAgentPassCheck TEST START");
156 TddUtil::SetTestTokenID(tokenId_);
157 InputMethodAbility::GetInstance()->isBound_.store(false);
158 int32_t ret = InputMethodAbility::GetInstance()->SetCoreAndAgent();
159 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
160 TddUtil::RestoreSelfTokenID();
161 }
162 } // namespace MiscServices
163 } // namespace OHOS