• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #define private public
17 #include "input_method_ability.h"
18 #include "task_manager.h"
19 #undef private
20 
21 #include <gtest/gtest.h>
22 
23 #include "ability_manager_client.h"
24 #include "global.h"
25 #include "ime_event_monitor_manager_impl.h"
26 #include "ime_setting_listener_test_impl.h"
27 #include "input_method_ability_interface.h"
28 #include "input_method_controller.h"
29 #include "input_method_engine_listener_impl.h"
30 #include "input_method_types.h"
31 #include "keyboard_listener_test_impl.h"
32 #include "scope_utils.h"
33 #include "sys_cfg_parser.h"
34 #include "tdd_util.h"
35 #include "text_listener.h"
36 using namespace testing::ext;
37 namespace OHOS {
38 namespace MiscServices {
39 constexpr uint64_t AGENT_IME_DISPLAY_ID = 666;
40 constexpr int32_t INVALID_UID = -1;
41 class ImeProxyAgentImeTest : public testing::Test {
42 public:
43     static sptr<InputMethodController> imc_;
44     static bool isAgentFeatureEnabled_;
45     static int32_t agentUid_;
SetUpTestCase(void)46     static void SetUpTestCase(void)
47     {
48         IMSA_HILOGI("ImeProxyAgentImeTest::SetUpTestCase");
49         TddUtil::StorageSelfTokenID();
50         TddUtil::InitWindow(false);
51         imc_ = InputMethodController::GetInstance();
52         RegisterImeSettingListener();
53         // native sa permission
54         TddUtil::GrantNativePermission();
55         SystemConfig systemConfig;
56         SysCfgParser::ParseSystemConfig(systemConfig);
57         isAgentFeatureEnabled_ = systemConfig.enableAppAgentFeature;
58         if (isAgentFeatureEnabled_) {
59             if (systemConfig.proxyImeUidList.empty()) {
60                 isAgentFeatureEnabled_ = false;
61             }
62             for (auto id : systemConfig.proxyImeUidList) {
63                 agentUid_ = id;
64             }
65         }
66     }
TearDownTestCase(void)67     static void TearDownTestCase(void)
68     {
69         IMSA_HILOGI("ImeProxyAgentImeTest::TearDownTestCase");
70         TddUtil::DestroyWindow();
71         TddUtil::RestoreSelfTokenID();
72         TddUtil::KillImsaProcess();
73     }
SetUp()74     void SetUp()
75     {
76         IMSA_HILOGI("ImeProxyAgentImeTest::SetUp");
77         InputMethodAbilityInterface::GetInstance().SetImeListener(std::make_shared<InputMethodEngineListenerImpl>());
78         InputMethodAbilityInterface::GetInstance().SetKdListener(std::make_shared<KeyboardListenerTestImpl>());
79         TaskManager::GetInstance().SetInited(true);
80     }
TearDown()81     void TearDown()
82     {
83         IMSA_HILOGI("InputMethodAbilityTest::TearDown");
84         std::this_thread::sleep_for(std::chrono::seconds(1));
85         TaskManager::GetInstance().Reset();
86     }
87 
Attach()88     static int32_t Attach()
89     {
90         TextConfig config;
91         config.cursorInfo = { .left = 0, .top = 1, .width = 0.5, .height = 1.2 };
92         sptr<OnTextChangedListener> testListener = new TextListener();
93         auto ret = imc_->Attach(testListener, true, config);
94         return ret;
95     }
96 
Close()97     static void Close()
98     {
99         imc_->Close();
100     }
101 
102 private:
RegisterImeSettingListener()103     static void RegisterImeSettingListener()
104     {
105         TddUtil::StorageSelfTokenID();
106         TddUtil::SetTestTokenID(TddUtil::AllocTestTokenID(true, "ImeProxyAgentImeTest"));
107         auto listener = std::make_shared<ImeSettingListenerTestImpl>();
108         ImeEventMonitorManagerImpl::GetInstance().RegisterImeEventListener(
109             EVENT_IME_HIDE_MASK | EVENT_IME_SHOW_MASK | EVENT_IME_CHANGE_MASK, listener);
110         TddUtil::RestoreSelfTokenID();
111     }
112 };
113 sptr<InputMethodController> ImeProxyAgentImeTest::imc_;
114 bool ImeProxyAgentImeTest::isAgentFeatureEnabled_{ false };
115 int32_t ImeProxyAgentImeTest::agentUid_{ INVALID_UID };
116 
117 /**
118  * @tc.name: testRegisterProxyIme_001
119  * @tc.desc: agent feature enalbed, invalid uid
120  * @tc.type: FUNC
121  */
122 HWTEST_F(ImeProxyAgentImeTest, testRegisterProxyIme_001, TestSize.Level1)
123 {
124     IMSA_HILOGI("ImeProxyAgentImeTest::testRegisterProxyIme_001 start");
125     if (!ImeProxyAgentImeTest::isAgentFeatureEnabled_) {
126         EXPECT_EQ(ImeProxyAgentImeTest::agentUid_, INVALID_UID);
127     } else {
128         auto ret = InputMethodAbilityInterface::GetInstance().RegisterProxyIme(AGENT_IME_DISPLAY_ID);
129         EXPECT_EQ(ret, ErrorCode::ERROR_NOT_AI_APP_IME);
130         InputMethodAbilityInterface::GetInstance().UnregisterProxyIme(AGENT_IME_DISPLAY_ID);
131         EXPECT_EQ(ret, ErrorCode::ERROR_NOT_AI_APP_IME);
132     }
133 }
134 
135 /**
136  * @tc.name: testRegisterProxyIme_002
137  * @tc.desc: agent feature enabled, valid uid
138  * @tc.type: FUNC
139  */
140 HWTEST_F(ImeProxyAgentImeTest, testRegisterProxyIme_002, TestSize.Level1)
141 {
142     IMSA_HILOGI("ImeProxyAgentImeTest::testRegisterProxyIme_002 start");
143     if (!ImeProxyAgentImeTest::isAgentFeatureEnabled_) {
144         EXPECT_EQ(ImeProxyAgentImeTest::agentUid_, INVALID_UID);
145     } else {
146         UidScope scope(ImeProxyAgentImeTest::agentUid_);
147         auto ret = InputMethodAbilityInterface::GetInstance().RegisterProxyIme(AGENT_IME_DISPLAY_ID);
148         EXPECT_EQ(ret, ErrorCode::NO_ERROR);
149         ret = InputMethodAbilityInterface::GetInstance().UnregisterProxyIme(AGENT_IME_DISPLAY_ID);
150         EXPECT_EQ(ret, ErrorCode::NO_ERROR);
151     }
152 }
153 } // namespace MiscServices
154 } // namespace OHOS
155