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 "dev_profile_mock_test.h"
17
18 #include "device/dev_profile.h"
19 #include "device/dm_adapter.h"
20 #include "pasteboard_error.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace MiscServices {
27
SetUpTestCase(void)28 void DevProfileMockTest::SetUpTestCase(void)
29 {
30 DistributedDeviceProfile::PasteDistributedDeviceProfileClient::pasteDistributedDeviceProfileClient =
31 distributedDeviceProfileClientMock_;
32 }
33
TearDownTestCase(void)34 void DevProfileMockTest::TearDownTestCase(void)
35 {
36 DistributedDeviceProfile::PasteDistributedDeviceProfileClient::pasteDistributedDeviceProfileClient = nullptr;
37 distributedDeviceProfileClientMock_ = nullptr;
38 }
39
SetUp(void)40 void DevProfileMockTest::SetUp(void) { }
41
TearDown(void)42 void DevProfileMockTest::TearDown(void) { }
43 /**
44 * @tc.name: GetEnabledStatusTest001
45 * @tc.desc: GetEnabledStatus should return E_OK when query valid networkId
46 * @tc.type: FUNC
47 * @tc.require:
48 * @tc.author:
49 */
50 HWTEST_F(DevProfileMockTest, GetEnabledStatusTest001, TestSize.Level0)
51 {
52 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
53 EXPECT_CALL(
54 *distributedDeviceProfileClientMock_, GetCharacteristicProfile(testing::_, testing::_, testing::_, testing::_))
55 .WillRepeatedly(testing::Return(static_cast<int32_t>(PasteboardError::NO_TRUST_DEVICE_ERROR)));
56 std::string bundleName = "com.example.myApplication";
57 bool res = DMAdapter::GetInstance().Initialize(bundleName);
58 std::string enabledStatus = "";
59 auto networkId = DMAdapter::GetInstance().GetLocalNetworkId();
60 int32_t ret = DevProfile::GetInstance().GetEnabledStatus(networkId, enabledStatus);
61 ASSERT_EQ(static_cast<int32_t>(PasteboardError::DP_LOAD_SERVICE_ERROR), ret);
62 #else
63 EXPECT_EQ(static_cast<int32_t>(PasteboardError::NO_TRUST_DEVICE_ERROR), ret);
64 #endif
65 }
66
67 /**
68 * @tc.name: GetEnabledStatusTest002
69 * @tc.desc: GetEnabledStatus should return E_OK when query valid networkId
70 * @tc.type: FUNC
71 * @tc.require:
72 * @tc.author:
73 */
74 HWTEST_F(DevProfileMockTest, GetEnabledStatusTest002, TestSize.Level0)
75 {
76 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
77 EXPECT_CALL(
78 *distributedDeviceProfileClientMock_, GetCharacteristicProfile(testing::_, testing::_, testing::_, testing::_))
79 .WillRepeatedly(testing::Return(DistributedDeviceProfile::DP_SUCCESS));
80 std::string bundleName = "com.example.myApplication";
81 bool res = DMAdapter::GetInstance().Initialize(bundleName);
82 std::string enabledStatus = "";
83 auto networkId = DMAdapter::GetInstance().GetLocalNetworkId();
84 int32_t ret = DevProfile::GetInstance().GetEnabledStatus(networkId, enabledStatus);
85 ASSERT_EQ(static_cast<int32_t>(PasteboardError::E_OK), ret);
86 ret = DevProfile::GetInstance().GetEnabledStatus(networkId, enabledStatus);
87 ASSERT_EQ(static_cast<int32_t>(PasteboardError::E_OK), ret);
88 #else
89 EXPECT_EQ(ret, static_cast<int32_t>(PasteboardError::NO_TRUST_DEVICE_ERROR));
90 #endif
91 }
92
93
94 /**
95 * @tc.name: PutCharacteristicProfile001
96 * @tc.desc: PutCharacteristicProfile
97 * @tc.type: FUNC
98 * @tc.require:
99 * @tc.author:
100 */
101 HWTEST_F(DevProfileMockTest, PutCharacteristicProfile001, TestSize.Level0)
102 {
103 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
104 EXPECT_CALL(*distributedDeviceProfileClientMock_, PutCharacteristicProfile(testing::_))
105 .WillRepeatedly(testing::Return(DistributedDeviceProfile::DP_SUCCESS));
106 std::string bundleName = "com.example.myApplication";
107 bool res = DMAdapter::GetInstance().Initialize(bundleName);
108 std::string enabledStatus = "1";
109 DevProfile::GetInstance().PutEnabledStatus(enabledStatus);
110 EXPECT_TRUE(true);
111 #else
112 EXPECT_TRUE(true);
113 #endif
114 }
115
116 /**
117 * @tc.name: PutCharacteristicProfile002
118 * @tc.desc: PutCharacteristicProfile
119 * @tc.type: FUNC
120 * @tc.require:
121 * @tc.author:
122 */
123 HWTEST_F(DevProfileMockTest, PutCharacteristicProfile002, TestSize.Level0)
124 {
125 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
126 EXPECT_CALL(*distributedDeviceProfileClientMock_, PutCharacteristicProfile(testing::_))
127 .WillRepeatedly(testing::Return(DistributedDeviceProfile::DP_CACHE_EXIST));
128 std::string bundleName = "com.example.myApplication";
129 bool res = DMAdapter::GetInstance().Initialize(bundleName);
130 std::string enabledStatus = "1";
131 DevProfile::GetInstance().PutEnabledStatus(enabledStatus);
132 EXPECT_TRUE(true);
133 #else
134 EXPECT_TRUE(true);
135 #endif
136 }
137
138 /**
139 * @tc.name: PutCharacteristicProfile003
140 * @tc.desc: PutCharacteristicProfile
141 * @tc.type: FUNC
142 * @tc.require:
143 * @tc.author:
144 */
145 HWTEST_F(DevProfileMockTest, PutCharacteristicProfile003, TestSize.Level0)
146 {
147 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
148 EXPECT_CALL(*distributedDeviceProfileClientMock_, PutCharacteristicProfile(testing::_))
149 .WillRepeatedly(testing::Return(DistributedDeviceProfile::DP_INVALID_PARAMS));
150 std::string bundleName = "com.example.myApplication";
151 bool res = DMAdapter::GetInstance().Initialize(bundleName);
152 std::string enabledStatus = "1";
153 DevProfile::GetInstance().PutEnabledStatus(enabledStatus);
154 EXPECT_TRUE(true);
155 #else
156 EXPECT_TRUE(true);
157 #endif
158 }
159
160 /**
161 * @tc.name: GetRemoteDeviceVersionTest001
162 * @tc.desc: GetRemoteDeviceVersionTest
163 * @tc.type: FUNC
164 * @tc.require:
165 * @tc.author:
166 */
167 HWTEST_F(DevProfileMockTest, GetRemoteDeviceVersionTest001, TestSize.Level0)
168 {
169 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
170 EXPECT_CALL(
171 *distributedDeviceProfileClientMock_, GetCharacteristicProfile(testing::_, testing::_, testing::_, testing::_))
172 .WillRepeatedly(testing::Return(DistributedDeviceProfile::DP_SUCCESS));
173 uint32_t versionId;
174 std::string bundleName = "com.dev.profile";
175 DevProfile::GetInstance().GetRemoteDeviceVersion(bundleName, versionId);
176 EXPECT_TRUE(true);
177 #else
178 EXPECT_TRUE(true);
179 #endif
180 }
181
182 /**
183 * @tc.name: GetRemoteDeviceVersionTest002
184 * @tc.desc: GetRemoteDeviceVersionTest
185 * @tc.type: FUNC
186 * @tc.require:
187 * @tc.author:
188 */
189 HWTEST_F(DevProfileMockTest, GetRemoteDeviceVersionTest002, TestSize.Level0)
190 {
191 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
192 EXPECT_CALL(
193 *distributedDeviceProfileClientMock_, GetCharacteristicProfile(testing::_, testing::_, testing::_, testing::_))
194 .WillRepeatedly(testing::Return(DistributedDeviceProfile::DP_INVALID_PARAMS));
195 uint32_t versionId;
196 std::string bundleName = "com.dev.profile";
197 DevProfile::GetInstance().GetRemoteDeviceVersion(bundleName, versionId);
198 EXPECT_TRUE(true);
199 #else
200 EXPECT_TRUE(true);
201 #endif
202 }
203
204 } // namespace MiscServices
205 } // namespace OHOS