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 #include <gtest/gtest.h>
17 #include <thread>
18
19 #include "device_profile_client.h"
20 #include "distributed_device_profile_errors.h"
21 #include "distributed_device_profile_proxy.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "system_ability_manager_mock.h"
25
26 namespace OHOS::MiscServices {
27 using namespace OHOS::DistributedDeviceProfile;
28 using namespace testing::ext;
29 using testing::NiceMock;
30
31 class AdapterDeviceProfileClientTest : public testing::Test {
32 public:
SetUpTestCase()33 static void SetUpTestCase()
34 {
35 sptr<ISystemAbilityManager> samgr = sptr<SystemAbilityManager>::MakeSptr();
36 SystemAbilityManagerClient::GetInstance().SetSystemAbilityManager(samgr);
37 }
38
TearDownTestCase()39 static void TearDownTestCase()
40 {
41 }
42
SetUp()43 void SetUp()
44 {
45 }
46
TearDown()47 void TearDown()
48 {
49 }
50 };
51
52 class DistributedDeviceProfileStub : public IRemoteStub<IDistributedDeviceProfile> {
53 public:
PutCharacteristicProfile(const CharacteristicProfile & characteristicProfile)54 int32_t PutCharacteristicProfile(const CharacteristicProfile &characteristicProfile) override
55 {
56 (void)characteristicProfile;
57 return ERR_OK;
58 }
59
GetCharacteristicProfile(const std::string & deviceId,const std::string & serviceName,const std::string & characteristicId,CharacteristicProfile & characteristicProfile)60 int32_t GetCharacteristicProfile(const std::string &deviceId, const std::string &serviceName,
61 const std::string &characteristicId, CharacteristicProfile &characteristicProfile) override
62 {
63 (void)deviceId;
64 (void)serviceName;
65 (void)characteristicId;
66 (void)characteristicProfile;
67 return ERR_OK;
68 }
69
SubscribeDeviceProfile(const SubscribeInfo & subscribeInfo)70 int32_t SubscribeDeviceProfile(const SubscribeInfo &subscribeInfo) override
71 {
72 (void)subscribeInfo;
73 return ERR_OK;
74 }
75
UnSubscribeDeviceProfile(const SubscribeInfo & subscribeInfo)76 int32_t UnSubscribeDeviceProfile(const SubscribeInfo &subscribeInfo) override
77 {
78 (void)subscribeInfo;
79 return ERR_OK;
80 }
81
SendSubscribeInfos(std::map<std::string,SubscribeInfo> listenerMap)82 int32_t SendSubscribeInfos(std::map<std::string, SubscribeInfo> listenerMap) override
83 {
84 (void)listenerMap;
85 return ERR_OK;
86 }
87 };
88
LoadSystemAbilityFailImpl(int32_t systemAbilityId,const sptr<ISystemAbilityLoadCallback> & callback)89 int32_t LoadSystemAbilityFailImpl(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback> &callback)
90 {
91 std::thread thread([=] {
92 if (callback == nullptr) {
93 return;
94 }
95 callback->OnLoadSystemAbilityFail(systemAbilityId);
96 });
97 thread.detach();
98 return ERR_OK;
99 }
100
LoadSystemAbilitySuccImpl(int32_t systemAbilityId,const sptr<ISystemAbilityLoadCallback> & callback)101 int32_t LoadSystemAbilitySuccImpl(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback> &callback)
102 {
103 std::thread thread([=] {
104 if (callback == nullptr) {
105 return;
106 }
107 sptr<IRemoteObject> remoteObject = new DistributedDeviceProfileStub();
108 callback->OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject);
109 });
110 thread.detach();
111 return ERR_OK;
112 }
113
114 /**
115 * @tc.name: TestClearDeviceProfileService001
116 * @tc.desc: test ClearDeviceProfileService
117 * @tc.type: FUNC
118 */
119 HWTEST_F(AdapterDeviceProfileClientTest, TestClearDeviceProfileService001, TestSize.Level1)
120 {
121 DeviceProfileClient::GetInstance().ClearDeviceProfileService();
122 EXPECT_EQ(DeviceProfileClient::GetInstance().dpProxy_, nullptr);
123 }
124
125 /**
126 * @tc.name: TestGetDeviceProfileService001
127 * @tc.desc: test GetDeviceProfileService OnLoadSystemAbilityFail
128 * @tc.type: FUNC
129 */
130 HWTEST_F(AdapterDeviceProfileClientTest, TestGetDeviceProfileService001, TestSize.Level1)
131 {
132 NiceMock<SystemAbilityManagerMock> mock;
133 EXPECT_CALL(mock, CheckSystemAbility).WillRepeatedly(testing::Return(nullptr));
134 EXPECT_CALL(mock, LoadSystemAbility).WillRepeatedly(LoadSystemAbilityFailImpl);
135
136 auto proxy = DeviceProfileClient::GetInstance().GetDeviceProfileService();
137 EXPECT_EQ(proxy, nullptr);
138 }
139
140 /**
141 * @tc.name: TestGetDeviceProfileService002
142 * @tc.desc: test GetDeviceProfileService OnLoadSystemAbilitySuccess
143 * @tc.type: FUNC
144 */
145 HWTEST_F(AdapterDeviceProfileClientTest, TestGetDeviceProfileService002, TestSize.Level1)
146 {
147 NiceMock<SystemAbilityManagerMock> mock;
148 EXPECT_CALL(mock, CheckSystemAbility).WillRepeatedly(testing::Return(nullptr));
149 EXPECT_CALL(mock, LoadSystemAbility).WillRepeatedly(LoadSystemAbilitySuccImpl);
150
151 auto proxy = DeviceProfileClient::GetInstance().GetDeviceProfileService();
152 EXPECT_NE(proxy, nullptr);
153 }
154
155 /**
156 * @tc.name: TestPutCharacteristicProfile001
157 * @tc.desc: test PutCharacteristicProfile
158 * @tc.type: FUNC
159 */
160 HWTEST_F(AdapterDeviceProfileClientTest, TestPutCharacteristicProfile001, TestSize.Level1)
161 {
162 std::string deviceId = "udid1";
163 std::string serviceName = "SwitchStatus_Key_Distributed_Pasteboard";
164 std::string characteristicKey = "SwitchStatus";
165 std::string characteristicValue = "1";
166 CharacteristicProfile profile(deviceId, serviceName, characteristicKey, characteristicValue);
167
168 int32_t ret = DeviceProfileClient::GetInstance().PutCharacteristicProfile(profile);
169 EXPECT_EQ(ret, DP_SUCCESS);
170 }
171
172 /**
173 * @tc.name: TestGetCharacteristicProfile001
174 * @tc.desc: test GetCharacteristicProfile
175 * @tc.type: FUNC
176 */
177 HWTEST_F(AdapterDeviceProfileClientTest, TestGetCharacteristicProfile001, TestSize.Level1)
178 {
179 std::string deviceId = "udid1";
180 std::string serviceName = "SwitchStatus_Key_Distributed_Pasteboard";
181 std::string characteristicKey = "SwitchStatus";
182 CharacteristicProfile profile;
183
184 int32_t ret = DeviceProfileClient::GetInstance().GetCharacteristicProfile(deviceId, serviceName, characteristicKey,
185 profile);
186 EXPECT_EQ(ret, DP_SUCCESS);
187 }
188
189 /**
190 * @tc.name: TestSubscribeDeviceProfile001
191 * @tc.desc: test SubscribeDeviceProfile, SendSubscribeInfos, UnSubscribeDeviceProfile
192 * @tc.type: FUNC
193 */
194 HWTEST_F(AdapterDeviceProfileClientTest, TestSubscribeDeviceProfile001, TestSize.Level1)
195 {
196 DeviceProfileClient::GetInstance().SendSubscribeInfos();
197 EXPECT_EQ(DeviceProfileClient::GetInstance().subscribeInfos_.size(), 0);
198
199 int32_t saId = PASTEBOARD_SERVICE_ID;
200 std::string subscribeKey = "UDID#SWITCH_ID#CHARACTER_ID#CHARACTERISTIC_VALUE";
201 SubscribeInfo subscribeInfo(saId, subscribeKey);
202
203 int32_t ret = DeviceProfileClient::GetInstance().SubscribeDeviceProfile(subscribeInfo);
204 EXPECT_EQ(ret, DP_SUCCESS);
205
206 DeviceProfileClient::GetInstance().SendSubscribeInfos();
207 EXPECT_EQ(DeviceProfileClient::GetInstance().subscribeInfos_.size(), 1);
208
209 ret = DeviceProfileClient::GetInstance().UnSubscribeDeviceProfile(subscribeInfo);
210 EXPECT_EQ(ret, DP_SUCCESS);
211 EXPECT_EQ(DeviceProfileClient::GetInstance().subscribeInfos_.size(), 0);
212 }
213 } // namespace OHOS::MiscServices
214