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 <vector>
17 
18 #include <unistd.h>
19 
20 #include "accesstoken_kit.h"
21 #include "device_manager.h"
22 #include "dm_device_info.h"
23 #include <gtest/gtest.h>
24 #include "nativetoken_kit.h"
25 #include "pointer_event.h"
26 #include "securec.h"
27 #include "token_setproc.h"
28 
29 #include "devicestatus_define.h"
30 #include "devicestatus_errors.h"
31 #include "i_event_listener.h"
32 #include "interaction_manager.h"
33 #include "utility.h"
34 
35 #undef LOG_TAG
36 #define LOG_TAG "MouseLocationListenerTest"
37 
38 namespace OHOS {
39 namespace Msdp {
40 namespace DeviceStatus {
41 using namespace testing::ext;
42 namespace {
43 const std::string COOPERATE_ACCESS_PERMISSION { "ohos.permission.COOPERATE_MANAGER" };
44 const std::string DM_SERVICE_ACCESS_PERMISSION { "ohos.permission.ACCESS_SERVICE_DM" };
45 const std::string DM_SERVICE_ACCESS_NEWPERMISSION { "ohos.permission.DISTRIBUTED_DATASYNC" };
46 const std::string PKG_NAME_PREFIX { "DBinderBus_Dms_" };
47 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
48 #define DSTB_HARDWARE DistributedHardware::DeviceManager::GetInstance()
49 } // namespace
50 
51 class MouseLocationListenerTest : public testing::Test {
52 public:
53     void SetUp();
54     void TearDown();
55     static void SetUpTestCase();
56     static std::string GetLocalNetworkId();
57 };
58 
SetUpTestCase()59 void MouseLocationListenerTest::SetUpTestCase() {}
60 
SetUp()61 void MouseLocationListenerTest::SetUp()
62 {
63     const int32_t permsNum = 3;
64     const char *perms[] = {DM_SERVICE_ACCESS_NEWPERMISSION.c_str(), DM_SERVICE_ACCESS_PERMISSION.c_str(),
65         COOPERATE_ACCESS_PERMISSION.c_str()};
66     NativeTokenInfoParams infoInstance = {
67         .dcapsNum = 0,
68         .permsNum = permsNum,
69         .aclsNum = 0,
70         .dcaps = NULL,
71         .perms = perms,
72         .acls = NULL,
73         .processName = "MouseLocationListenerTest",
74         .aplStr = "system_core",
75     };
76     uint64_t tokenId = GetAccessTokenId(&infoInstance);
77     SetSelfTokenID(tokenId);
78     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
79 }
80 
TearDown()81 void MouseLocationListenerTest::TearDown()
82 {
83     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
84 }
85 
GetLocalNetworkId()86 std::string MouseLocationListenerTest::GetLocalNetworkId()
87 {
88     auto packageName = PKG_NAME_PREFIX + std::to_string(getpid());
89     OHOS::DistributedHardware::DmDeviceInfo dmDeviceInfo;
90     if (int32_t errCode = DSTB_HARDWARE.GetLocalDeviceInfo(packageName, dmDeviceInfo); errCode != RET_OK) {
91         FI_HILOGE("GetLocalBasicInfo failed, errCode:%{public}d", errCode);
92         return {};
93     }
94     FI_HILOGD("LocalNetworkId:%{public}s", Utility::Anonymize(dmDeviceInfo.networkId).c_str());
95     return dmDeviceInfo.networkId;
96 }
97 
98 class EventListener : public IEventListener {
99     void OnMouseLocationEvent(const std::string &networkId, const Event &event) override;
100 };
101 
OnMouseLocationEvent(const std::string & networkId,const Event & event)102 void EventListener::OnMouseLocationEvent(const std::string &networkId, const Event &event)
103 {
104     FI_HILOGI("NetworkId:%{public}s, DisplayX:%{public}d, displayY:%{public}d,"
105         "displayWidth:%{public}d, displayHeight:%{public}d", Utility::Anonymize(networkId).c_str(),
106         event.displayX, event.displayY, event.displayWidth, event.displayHeight);
107 }
108 
109 /**
110  * @tc.name: RegisterEventListener_00
111  * @tc.desc: Default NetworkId, Valid Listener.
112  * @tc.type: FUNC
113  * @tc.require:
114  */
115 HWTEST_F(MouseLocationListenerTest, RegisterEventListener_00, TestSize.Level1)
116 {
117     CALL_TEST_DEBUG;
118     std::string networkId { "Default" };
119     auto listener = std::make_shared<EventListener>();
120     int32_t ret = InteractionManager::GetInstance()->RegisterEventListener(networkId, listener);
121 #ifdef OHOS_BUILD_ENABLE_COORDINATION
122     ASSERT_EQ(ret, RET_OK);
123 #else
124     ASSERT_EQ(ret, ERROR_UNSUPPORT);
125 #endif // OHOS_BUILD_ENABLE_COORDINATION
126 }
127 
128 /**
129  * @tc.name: RegisterEventListener_01
130  * @tc.desc: Default NetworkId, NULL Listener.
131  * @tc.type: FUNC
132  * @tc.require:
133  */
134 HWTEST_F(MouseLocationListenerTest, RegisterEventListener_01, TestSize.Level1)
135 {
136     CALL_TEST_DEBUG;
137     std::string networkId { "Default" };
138     int32_t ret = InteractionManager::GetInstance()->RegisterEventListener(networkId, nullptr);
139 #ifdef OHOS_BUILD_ENABLE_COORDINATION
140     ASSERT_EQ(ret, COMMON_PARAMETER_ERROR);
141 #else
142     ASSERT_EQ(ret, ERROR_UNSUPPORT);
143 #endif // OHOS_BUILD_ENABLE_COORDINATION
144 }
145 
146 /**
147  * @tc.name: UnregisterEventListener_00
148  * @tc.desc: Local NetworkId, Valid Listener.
149  * @tc.type: FUNC
150  * @tc.require:
151  */
152 HWTEST_F(MouseLocationListenerTest, UnregisterEventListener_00, TestSize.Level1)
153 {
154     CALL_TEST_DEBUG;
155     std::string networkId { "Default" };
156     auto listener = std::make_shared<EventListener>();
157     int32_t ret = InteractionManager::GetInstance()->RegisterEventListener(networkId, listener);
158 #ifdef OHOS_BUILD_ENABLE_COORDINATION
159     ASSERT_EQ(ret, RET_OK);
160 #else
161     ASSERT_EQ(ret, ERROR_UNSUPPORT);
162 #endif // OHOS_BUILD_ENABLE_COORDINATION
163     ret = InteractionManager::GetInstance()->UnregisterEventListener(networkId, listener);
164 #ifdef OHOS_BUILD_ENABLE_COORDINATION
165     ASSERT_EQ(ret, RET_OK);
166 #else
167     ASSERT_EQ(ret, ERROR_UNSUPPORT);
168 #endif // OHOS_BUILD_ENABLE_COORDINATION
169 }
170 
171 /**
172  * @tc.name: UnregisterEventListener_01
173  * @tc.desc: Local networkId, NULL Listener.
174  * @tc.type: FUNC
175  * @tc.require:
176  */
177 HWTEST_F(MouseLocationListenerTest, UnregisterEventListener_01, TestSize.Level1)
178 {
179     CALL_TEST_DEBUG;
180     std::string networkId { "Default" };
181     auto listener = std::make_shared<EventListener>();
182     int32_t ret = InteractionManager::GetInstance()->RegisterEventListener(networkId, listener);
183 #ifdef OHOS_BUILD_ENABLE_COORDINATION
184     ASSERT_EQ(ret, RET_OK);
185 #else
186     ASSERT_EQ(ret, ERROR_UNSUPPORT);
187 #endif // OHOS_BUILD_ENABLE_COORDINATION
188     ret = InteractionManager::GetInstance()->UnregisterEventListener(networkId, nullptr);
189 #ifdef OHOS_BUILD_ENABLE_COORDINATION
190     ASSERT_EQ(ret, RET_OK);
191 #else
192     ASSERT_EQ(ret, ERROR_UNSUPPORT);
193 #endif // OHOS_BUILD_ENABLE_COORDINATION
194 }
195 } // namespace DeviceStatus
196 } // namespace Msdp
197 } // namespace OHOS
198