• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <gtest/gtest.h>
17 
18 #include "device/dev_profile.h"
19 #include "device/dm_adapter.h"
20 #include "pasteboard_error.h"
21 
22 namespace OHOS::MiscServices {
23 using namespace testing::ext;
24 class DevProfileTest : public testing::Test {
25 public:
26     static void SetUpTestCase(void);
27     static void TearDownTestCase(void);
28     void SetUp();
29     void TearDown();
30 };
31 
SetUpTestCase(void)32 void DevProfileTest::SetUpTestCase(void) { }
33 
TearDownTestCase(void)34 void DevProfileTest::TearDownTestCase(void)
35 {
36     DevProfile::GetInstance().ClearDeviceProfileService();
37 }
38 
SetUp(void)39 void DevProfileTest::SetUp(void) { }
40 
TearDown(void)41 void DevProfileTest::TearDown(void) { }
42 
43 /**
44  * @tc.name: GetDeviceVersionTest001
45  * @tc.desc: Get Device Version
46  * @tc.type: FUNC
47  * @tc.require:
48  * @tc.author:
49  */
50 HWTEST_F(DevProfileTest, GetDeviceVersionTest001, TestSize.Level0)
51 {
52 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
53     uint32_t versionId;
54     std::string bundleName = "com.dev.profile";
55     DevProfile::GetInstance().GetDeviceVersion(bundleName, versionId);
56     EXPECT_TRUE(true);
57 #else
58     EXPECT_TRUE(true);
59 #endif
60 }
61 
62 /**
63  * @tc.name: GetDeviceVersionTest002
64  * @tc.desc: Get Device Version
65  * @tc.type: FUNC
66  * @tc.require:
67  * @tc.author:
68  */
69 HWTEST_F(DevProfileTest, GetDeviceVersionTest002, TestSize.Level0)
70 {
71 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
72     uint32_t versionId;
73     std::string bundleName = "com.dev.profile";
74     bool res = DMAdapter::GetInstance().Initialize(bundleName);
75     DevProfile::GetInstance().proxy_ = nullptr;
76     DevProfile::GetInstance().subscribeUdidList_.clear();
77     DevProfile::GetInstance().GetDeviceVersion(bundleName, versionId);
78     EXPECT_TRUE(true);
79 #else
80     EXPECT_TRUE(true);
81 #endif
82 }
83 
84 /**
85  * @tc.name: PostDelayReleaseProxy001
86  * @tc.desc: in SetTimer
87  * @tc.type: FUNC
88  * @tc.require:
89  * @tc.author:
90  */
91 HWTEST_F(DevProfileTest, PostDelayReleaseProxy001, TestSize.Level0)
92 {
93 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
94     DevProfile::GetInstance().proxy_ = nullptr;
95     std::string uuid = "PostDelayReleaseProxy001";
96     DevProfile::GetInstance().SendSubscribeInfos();
97     DevProfile::GetInstance().subscribeUdidList_.emplace(uuid);
98     DevProfile::GetInstance().UnSubscribeAllProfileEvents();
99     EXPECT_TRUE(true);
100 #else
101     EXPECT_TRUE(true);
102 #endif
103 }
104 
105 /**
106  * @tc.name: GetDeviceStatusTest001
107  * @tc.desc: GetDeviceStatus should not return E_OK when query invalid networkId
108  * @tc.type: FUNC
109  * @tc.require:
110  * @tc.author:
111  */
112 HWTEST_F(DevProfileTest, GetDeviceStatusTest001, TestSize.Level0)
113 {
114 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
115     bool enabledStatus = false;
116     std::string networkId = "test.dev.profile";
117     int32_t ret = DevProfile::GetInstance().GetDeviceStatus(networkId, enabledStatus);
118     EXPECT_NE(ret, static_cast<int32_t>(PasteboardError::E_OK));
119 #else
120     EXPECT_EQ(ret, static_cast<int32_t>(PasteboardError::NO_TRUST_DEVICE_ERROR));
121 #endif
122 }
123 
124 /**
125  * @tc.name: GetDeviceStatusTest002
126  * @tc.desc: GetDeviceStatus should not return NO_TRUST_DEVICE_ERROR when query valid networkId
127  * @tc.type: FUNC
128  * @tc.require:
129  * @tc.author:
130  */
131 HWTEST_F(DevProfileTest, GetDeviceStatusTest002, TestSize.Level0)
132 {
133 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
134     bool enabledStatus = false;
135     auto networkId = DMAdapter::GetInstance().GetLocalNetworkId();
136     int32_t ret = DevProfile::GetInstance().GetDeviceStatus(networkId, enabledStatus);
137     EXPECT_NE(ret, static_cast<int32_t>(PasteboardError::NO_TRUST_DEVICE_ERROR));
138 #else
139     EXPECT_EQ(ret, static_cast<int32_t>(PasteboardError::NO_TRUST_DEVICE_ERROR));
140 #endif
141 }
142 
143 /**
144  * @tc.name: PutDeviceStatus001
145  * @tc.desc: PutDeviceStatus should not return NO_TRUST_DEVICE_ERROR when query valid networkId
146  * @tc.type: FUNC
147  * @tc.require:
148  * @tc.author:
149  */
150 HWTEST_F(DevProfileTest, PutDeviceStatus001, TestSize.Level0)
151 {
152 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
153     std::string bundleName = "com.example.myApplication";
154     bool res = DMAdapter::GetInstance().Initialize(bundleName);
155     bool enabledStatus = true;
156     DevProfile::GetInstance().proxy_ = nullptr;
157     DevProfile::GetInstance().subscribeUdidList_.clear();
158     DevProfile::GetInstance().PutDeviceStatus(enabledStatus);
159     EXPECT_TRUE(true);
160 #else
161     EXPECT_TRUE(true);
162 #endif
163 }
164 
165 /**
166  * @tc.name: SubscribeProfileEventTest001
167  * @tc.desc: Subscribe Profile Event
168  * @tc.type: FUNC
169  * @tc.require:
170  * @tc.author:
171  */
172 HWTEST_F(DevProfileTest, SubscribeProfileEventTest001, TestSize.Level0)
173 {
174 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
175     std::string bundleName = "com.pro.proEvent";
176     bool res = DMAdapter::GetInstance().Initialize(bundleName);
177     DevProfile::GetInstance().SubscribeProfileEvent(bundleName);
178     EXPECT_TRUE(true);
179 #else
180     EXPECT_TRUE(true);
181 #endif
182 }
183 
184 /**
185  * @tc.name: SubscribeProfileEventTest002
186  * @tc.desc: Subscribe Profile Event
187  * @tc.type: FUNC
188  * @tc.require:
189  * @tc.author:
190  */
191 HWTEST_F(DevProfileTest, SubscribeProfileEventTest002, TestSize.Level0)
192 {
193 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
194     std::string bundleName = "com.pro.proEvent";
195     bool res = DMAdapter::GetInstance().Initialize(bundleName);
196     DevProfile::GetInstance().SubscribeProfileEvent(bundleName);
197     DevProfile::GetInstance().SendSubscribeInfos();
198     DevProfile::GetInstance().SubscribeProfileEvent(bundleName);
199     EXPECT_TRUE(true);
200 #else
201     EXPECT_TRUE(true);
202 #endif
203 }
204 
205 /**
206  * @tc.name: UnSubscribeProfileEventTest001
207  * @tc.desc: UnSubscribe Profile Event
208  * @tc.type: FUNC
209  * @tc.require:
210  * @tc.author:
211  */
212 HWTEST_F(DevProfileTest, UnSubscribeProfileEventTest001, TestSize.Level0)
213 {
214 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
215     std::string bundleName = "com.pro.proEvent";
216     bool res = DMAdapter::GetInstance().Initialize(bundleName);
217     DevProfile::GetInstance().UnSubscribeProfileEvent(bundleName);
218     EXPECT_TRUE(true);
219 #else
220     EXPECT_TRUE(true);
221 #endif
222 }
223 
224 /**
225  * @tc.name: UnSubscribeProfileEventTest002
226  * @tc.desc: UnSubscribe Profile Event
227  * @tc.type: FUNC
228  * @tc.require:
229  * @tc.author:
230  */
231 HWTEST_F(DevProfileTest, UnSubscribeProfileEventTest002, TestSize.Level0)
232 {
233 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
234     std::string bundleName = "com.pro.proEvent";
235     bool res = DMAdapter::GetInstance().Initialize(bundleName);
236     DevProfile::GetInstance().SubscribeProfileEvent(bundleName);
237     DevProfile::GetInstance().UnSubscribeProfileEvent(bundleName);
238     EXPECT_TRUE(true);
239 #else
240     EXPECT_TRUE(true);
241 #endif
242 }
243 
244 /**
245  * @tc.name: UnSubscribeAllProfileEvents001
246  * @tc.desc: subscribeUdidList_ is empty
247  * @tc.type: FUNC
248  * @tc.require:
249  * @tc.author:
250  */
251 HWTEST_F(DevProfileTest, UnSubscribeAllProfileEvents001, TestSize.Level0)
252 {
253 #ifdef PB_DEVICE_INFO_MANAGER_ENABLE
254     DevProfile& dp = DevProfile::GetInstance();
255     dp.subscribeUdidList_.clear();
256     dp.UnSubscribeAllProfileEvents();
257 
258     dp.proxy_= nullptr;
259     dp.subscribeUdidList_.clear();
260     dp.UnSubscribeAllProfileEvents();
261 
262     EXPECT_TRUE(true);
263 #else
264     EXPECT_TRUE(true);
265 #endif
266 }
267 
268 /**
269  * @tc.name: WatchTest
270  * @tc.desc: Watch
271  * @tc.type: FUNC
272  * @tc.require:
273  * @tc.author:
274  */
275 HWTEST_F(DevProfileTest, Watch, TestSize.Level0)
276 {
277     DevProfile::Observer observer;
278     DevProfile::GetInstance().Watch(observer);
279     EXPECT_FALSE(observer);
280 }
281 } // namespace OHOS::MiscServices