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 "devicestatus_service_test.h"
17
18 #include <chrono>
19 #include <iostream>
20 #include <thread>
21
22 #include <gtest/gtest.h>
23 #include <if_system_ability_manager.h>
24 #include <ipc_skeleton.h>
25 #include <string_ex.h>
26
27 #include "fi_log.h"
28 #include "stationary_manager.h"
29
30 namespace OHOS {
31 namespace Msdp {
32 namespace DeviceStatus {
33 using namespace testing::ext;
34 namespace {
35 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DeviceStatusServiceTest" };
36 constexpr int32_t SLEEP_TIME { 2000 };
37 Type g_type { Type::TYPE_INVALID };
38 auto g_client = StationaryManager::GetInstance();
39 } // namespace
40
41 sptr<IRemoteDevStaCallback> DeviceStatusServiceTest::devCallback_ = nullptr;
42
SetUpTestCase()43 void DeviceStatusServiceTest::SetUpTestCase()
44 {
45 devCallback_ = new (std::nothrow) DeviceStatusServiceTestCallback();
46 }
47
TearDownTestCase()48 void DeviceStatusServiceTest::TearDownTestCase() {}
49
SetUp()50 void DeviceStatusServiceTest::SetUp() {}
51
TearDown()52 void DeviceStatusServiceTest::TearDown() {}
53
OnDeviceStatusChanged(const Data & devicestatusData)54 void DeviceStatusServiceTest::DeviceStatusServiceTestCallback::OnDeviceStatusChanged(const Data& devicestatusData)
55 {
56 GTEST_LOG_(INFO) << "DeviceStatusServiceTestCallback type: " << devicestatusData.type;
57 GTEST_LOG_(INFO) << "DeviceStatusServiceTestCallback value: " << devicestatusData.value;
58 EXPECT_TRUE(devicestatusData.type == g_type && (devicestatusData.value >= OnChangedValue::VALUE_INVALID &&
59 devicestatusData.value <= OnChangedValue::VALUE_EXIT)) << "DeviceStatusServiceTestCallback failed";
60 }
61
62 namespace {
63 /**
64 * @tc.name: DeviceStatusCallbackTest
65 * @tc.desc: test devicestatus callback in proxy
66 * @tc.type: FUNC
67 */
68 HWTEST_F(DeviceStatusServiceTest, DeviceStatusCallbackTest001, TestSize.Level0)
69 {
70 CALL_TEST_DEBUG;
71 g_type = Type::TYPE_ABSOLUTE_STILL;
72 EXPECT_FALSE(devCallback_ == nullptr);
73 GTEST_LOG_(INFO) << "Start register";
74 g_client->SubscribeCallback(g_type, ActivityEvent::ENTER_EXIT, ReportLatencyNs::LONG, devCallback_);
75 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
76 }
77
78 /**
79 * @tc.name: GetDeviceStatusDataTest
80 * @tc.desc: test get devicestatus data in proxy
81 * @tc.type: FUNC
82 */
83 HWTEST_F(DeviceStatusServiceTest, GetDeviceStatusDataTest002, TestSize.Level0)
84 {
85 CALL_TEST_DEBUG;
86 g_type = Type::TYPE_ABSOLUTE_STILL;
87 Data data = g_client->GetDeviceStatusData(g_type);
88 GTEST_LOG_(INFO) << "type: " << data.type;
89 GTEST_LOG_(INFO) << "value: " << data.value;
90 EXPECT_TRUE(data.type == Type::TYPE_ABSOLUTE_STILL &&
91 (data.value >= OnChangedValue::VALUE_ENTER && data.value <= OnChangedValue::VALUE_EXIT))
92 << "GetDeviceStatusData failed";
93 }
94
95 /**
96 * @tc.name: GetDeviceStatusDataTest
97 * @tc.desc: test get devicestatus data in proxy
98 * @tc.type: FUNC
99 */
100 HWTEST_F(DeviceStatusServiceTest, GetDeviceStatusDataTest003, TestSize.Level0)
101 {
102 CALL_TEST_DEBUG;
103 g_type = Type::TYPE_ABSOLUTE_STILL;
104 EXPECT_FALSE(devCallback_ == nullptr);
105 GTEST_LOG_(INFO) << "Cancel register";
106 g_client->UnsubscribeCallback(g_type, ActivityEvent::ENTER_EXIT, devCallback_);
107 }
108
109 /**
110 * @tc.name: DeviceStatusCallbackTest
111 * @tc.desc: test devicestatus callback in proxy
112 * @tc.type: FUNC
113 */
114 HWTEST_F(DeviceStatusServiceTest, DeviceStatusCallbackTest004, TestSize.Level0)
115 {
116 CALL_TEST_DEBUG;
117 g_type = Type::TYPE_CAR_BLUETOOTH;
118 EXPECT_FALSE(devCallback_ == nullptr);
119 GTEST_LOG_(INFO) << "Start register";
120 g_client->SubscribeCallback(g_type, ActivityEvent::ENTER_EXIT, ReportLatencyNs::LONG, devCallback_);
121 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
122 }
123
124 /**
125 * @tc.name: GetDeviceStatusDataTest
126 * @tc.desc: test get devicestatus data in proxy
127 * @tc.type: FUNC
128 */
129 HWTEST_F(DeviceStatusServiceTest, GetDeviceStatusDataTest005, TestSize.Level0)
130 {
131 CALL_TEST_DEBUG;
132 g_type = Type::TYPE_CAR_BLUETOOTH;
133 Data data = g_client->GetDeviceStatusData(g_type);
134 GTEST_LOG_(INFO) << "type: " << data.type;
135 GTEST_LOG_(INFO) << "value: " << data.value;
136 EXPECT_TRUE(data.type == Type::TYPE_CAR_BLUETOOTH &&
137 (data.value <= OnChangedValue::VALUE_EXIT && data.value >= OnChangedValue::VALUE_INVALID))
138 << "GetDeviceStatusData failed";
139 }
140
141 /**
142 * @tc.name: GetDeviceStatusDataTest
143 * @tc.desc: test get devicestatus data in proxy
144 * @tc.type: FUNC
145 */
146 HWTEST_F(DeviceStatusServiceTest, GetDeviceStatusDataTest006, TestSize.Level0)
147 {
148 CALL_TEST_DEBUG;
149 g_type = Type::TYPE_CAR_BLUETOOTH;
150 EXPECT_FALSE(devCallback_ == nullptr);
151 GTEST_LOG_(INFO) << "Cancel register";
152 g_client->UnsubscribeCallback(g_type, ActivityEvent::ENTER_EXIT, devCallback_);
153 }
154
155 /**
156 * @tc.name: DeviceStatusCallbackTest
157 * @tc.desc: test devicestatus callback in proxy
158 * @tc.type: FUNC
159 */
160 HWTEST_F(DeviceStatusServiceTest, DeviceStatusCallbackTest007, TestSize.Level0)
161 {
162 CALL_TEST_DEBUG;
163 g_type = Type::TYPE_HORIZONTAL_POSITION;
164 EXPECT_FALSE(devCallback_ == nullptr);
165 GTEST_LOG_(INFO) << "Start register";
166 g_client->SubscribeCallback(g_type, ActivityEvent::ENTER_EXIT, ReportLatencyNs::LONG, devCallback_);
167 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
168 }
169
170 /**
171 * @tc.name: GetDeviceStatusDataTest
172 * @tc.desc: test get devicestatus data in proxy
173 * @tc.type: FUNC
174 */
175 HWTEST_F(DeviceStatusServiceTest, GetDeviceStatusDataTest008, TestSize.Level0)
176 {
177 CALL_TEST_DEBUG;
178 g_type = Type::TYPE_HORIZONTAL_POSITION;
179 Data data = g_client->GetDeviceStatusData(g_type);
180 GTEST_LOG_(INFO) << "type: " << data.type;
181 GTEST_LOG_(INFO) << "value: " << data.value;
182 EXPECT_TRUE(data.type == Type::TYPE_HORIZONTAL_POSITION &&
183 (data.value >= OnChangedValue::VALUE_ENTER && data.value <= OnChangedValue::VALUE_EXIT))
184 << "GetDeviceStatusData failed";
185 }
186
187 /**
188 * @tc.name: GetDeviceStatusDataTest
189 * @tc.desc: test get devicestatus data in proxy
190 * @tc.type: FUNC
191 */
192 HWTEST_F(DeviceStatusServiceTest, GetDeviceStatusDataTest009, TestSize.Level0)
193 {
194 CALL_TEST_DEBUG;
195 g_type = Type::TYPE_HORIZONTAL_POSITION;
196 EXPECT_FALSE(devCallback_ == nullptr);
197 GTEST_LOG_(INFO) << "Cancel register";
198 g_client->UnsubscribeCallback(g_type, ActivityEvent::ENTER_EXIT, devCallback_);
199 }
200
201 /**
202 * @tc.name: DeviceStatusCallbackTest
203 * @tc.desc: test devicestatus callback in proxy
204 * @tc.type: FUNC
205 */
206 HWTEST_F(DeviceStatusServiceTest, DeviceStatusCallbackTest010, TestSize.Level0)
207 {
208 CALL_TEST_DEBUG;
209 g_type = Type::TYPE_RELATIVE_STILL;
210 EXPECT_FALSE(devCallback_ == nullptr);
211 GTEST_LOG_(INFO) << "Start register";
212 g_client->SubscribeCallback(g_type, ActivityEvent::ENTER_EXIT, ReportLatencyNs::LONG, devCallback_);
213 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
214 }
215
216 /**
217 * @tc.name: GetDeviceStatusDataTest
218 * @tc.desc: test get devicestatus data in proxy
219 * @tc.type: FUNC
220 */
221 HWTEST_F(DeviceStatusServiceTest, GetDeviceStatusDataTest011, TestSize.Level0)
222 {
223 CALL_TEST_DEBUG;
224 g_type = Type::TYPE_RELATIVE_STILL;
225 Data data = g_client->GetDeviceStatusData(g_type);
226 GTEST_LOG_(INFO) << "type: " << data.type;
227 GTEST_LOG_(INFO) << "value: " << data.value;
228 EXPECT_TRUE(data.type == Type::TYPE_RELATIVE_STILL &&
229 (data.value <= OnChangedValue::VALUE_EXIT && data.value >= OnChangedValue::VALUE_INVALID))
230 << "GetDeviceStatusData failed";
231 }
232
233 /**
234 * @tc.name: GetDeviceStatusDataTest
235 * @tc.desc: test get devicestatus data in proxy
236 * @tc.type: FUNC
237 */
238 HWTEST_F(DeviceStatusServiceTest, GetDeviceStatusDataTest012, TestSize.Level0)
239 {
240 CALL_TEST_DEBUG;
241 g_type = Type::TYPE_RELATIVE_STILL;
242 EXPECT_FALSE(devCallback_ == nullptr);
243 GTEST_LOG_(INFO) << "Cancel register";
244 g_client->UnsubscribeCallback(g_type, ActivityEvent::ENTER_EXIT, devCallback_);
245 }
246
247 /**
248 * @tc.name: DeviceStatusCallbackTest
249 * @tc.desc: test devicestatus callback in proxy
250 * @tc.type: FUNC
251 */
252 HWTEST_F(DeviceStatusServiceTest, DeviceStatusCallbackTest013, TestSize.Level0)
253 {
254 CALL_TEST_DEBUG;
255 g_type = Type::TYPE_STILL;
256 EXPECT_FALSE(devCallback_ == nullptr);
257 GTEST_LOG_(INFO) << "Start register";
258 g_client->SubscribeCallback(g_type, ActivityEvent::ENTER_EXIT, ReportLatencyNs::LONG, devCallback_);
259 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
260 }
261
262 /**
263 * @tc.name: GetDeviceStatusDataTest
264 * @tc.desc: test get devicestatus data in proxy
265 * @tc.type: FUNC
266 */
267 HWTEST_F(DeviceStatusServiceTest, GetDeviceStatusDataTest014, TestSize.Level0)
268 {
269 CALL_TEST_DEBUG;
270 g_type = Type::TYPE_STILL;
271 Data data = g_client->GetDeviceStatusData(g_type);
272 GTEST_LOG_(INFO) << "type: " << data.type;
273 GTEST_LOG_(INFO) << "value: " << data.value;
274 EXPECT_TRUE(data.type == Type::TYPE_STILL &&
275 (data.value <= OnChangedValue::VALUE_EXIT && data.value >= OnChangedValue::VALUE_INVALID))
276 << "GetDeviceStatusData failed";
277 }
278
279 /**
280 * @tc.name: GetDeviceStatusDataTest
281 * @tc.desc: test get devicestatus data in proxy
282 * @tc.type: FUNC
283 */
284 HWTEST_F(DeviceStatusServiceTest, GetDeviceStatusDataTest015, TestSize.Level0)
285 {
286 CALL_TEST_DEBUG;
287 g_type = Type::TYPE_STILL;
288 EXPECT_FALSE(devCallback_ == nullptr);
289 GTEST_LOG_(INFO) << "Cancel register";
290 g_client->UnsubscribeCallback(g_type, ActivityEvent::ENTER_EXIT, devCallback_);
291 }
292
293 /**
294 * @tc.name: DeviceStatusCallbackTest
295 * @tc.desc: test devicestatus callback in proxy
296 * @tc.type: FUNC
297 */
298 HWTEST_F(DeviceStatusServiceTest, DeviceStatusCallbackTest016, TestSize.Level0)
299 {
300 CALL_TEST_DEBUG;
301 g_type = Type::TYPE_VERTICAL_POSITION;
302 EXPECT_FALSE(devCallback_ == nullptr);
303 GTEST_LOG_(INFO) << "Start register";
304 g_client->SubscribeCallback(g_type, ActivityEvent::ENTER_EXIT, ReportLatencyNs::LONG, devCallback_);
305 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
306 }
307
308 /**
309 * @tc.name: GetDeviceStatusDataTest
310 * @tc.desc: test get devicestatus data in proxy
311 * @tc.type: FUNC
312 */
313 HWTEST_F(DeviceStatusServiceTest, GetDeviceStatusDataTest017, TestSize.Level0)
314 {
315 CALL_TEST_DEBUG;
316 g_type = Type::TYPE_VERTICAL_POSITION;
317 Data data = g_client->GetDeviceStatusData(g_type);
318 GTEST_LOG_(INFO) << "type: " << data.type;
319 GTEST_LOG_(INFO) << "value: " << data.value;
320 EXPECT_TRUE(data.type == Type::TYPE_VERTICAL_POSITION &&
321 (data.value >= OnChangedValue::VALUE_INVALID && data.value <= OnChangedValue::VALUE_EXIT))
322 << "GetDeviceStatusData failed";
323 }
324
325 /**
326 * @tc.name: GetDeviceStatusDataTest
327 * @tc.desc: test get devicestatus data in proxy
328 * @tc.type: FUNC
329 */
330 HWTEST_F(DeviceStatusServiceTest, GetDeviceStatusDataTest018, TestSize.Level0)
331 {
332 CALL_TEST_DEBUG;
333 g_type = Type::TYPE_VERTICAL_POSITION;
334 EXPECT_FALSE(devCallback_ == nullptr);
335 GTEST_LOG_(INFO) << "Cancel register";
336 g_client->UnsubscribeCallback(g_type, ActivityEvent::ENTER_EXIT, devCallback_);
337 }
338
339 /**
340 * @tc.name: GetDeviceStatusDataTest
341 * @tc.desc: test get devicestatus data in proxy
342 * @tc.type: FUNC
343 */
344 HWTEST_F(DeviceStatusServiceTest, GetDeviceStatusDataTest019, TestSize.Level0)
345 {
346 CALL_TEST_DEBUG;
347 g_type = Type::TYPE_VERTICAL_POSITION;
348 Data data = g_client->GetDeviceStatusData(g_type);
349 GTEST_LOG_(INFO) << "type: " << data.type;
350 GTEST_LOG_(INFO) << "value: " << data.value;
351 EXPECT_TRUE(data.type == Type::TYPE_VERTICAL_POSITION &&
352 (data.value >= OnChangedValue::VALUE_INVALID && data.value <= OnChangedValue::VALUE_EXIT))
353 << "GetDeviceStatusData failed";
354 Data invalidData;
355 invalidData.type = Type::TYPE_INVALID;
356 invalidData.value = OnChangedValue::VALUE_INVALID;
357 invalidData.status = Status::STATUS_INVALID;
358 invalidData.movement = 0.0f;
359 EXPECT_TRUE(data != invalidData);
360 }
361 } // namespace
362 } // namespace DeviceStatus
363 } // namespace Msdp
364 } // namespace OHOS
365