1 /*
2 * Copyright (c) 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 <atomic>
17 #include <thread>
18
19 #include <gtest/gtest.h>
20
21 #include "sensor_agent.h"
22 #include "sensor_agent_type.h"
23 #include "sensor_errors.h"
24
25 #undef LOG_TAG
26 #define LOG_TAG "DropDetectionTest"
27
28 namespace OHOS {
29 namespace Sensors {
30 using namespace testing::ext;
31 using namespace OHOS::HiviewDFX;
32
33 namespace {
34 std::atomic_bool g_hasDropDetection = false;
35 } // namespace
36
37 class DropDetectionTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 void SetUp();
42 void TearDown();
43 };
44
SetUpTestCase()45 void DropDetectionTest::SetUpTestCase()
46 {
47 SensorInfo *sensorInfo = nullptr;
48 int32_t count = 0;
49 int32_t ret = GetAllSensors(&sensorInfo, &count);
50 ASSERT_EQ(ret, OHOS::Sensors::SUCCESS);
51 if (sensorInfo == nullptr || count == 0) {
52 SEN_HILOGE("sensorInfo is nullptr or count is 0");
53 return;
54 }
55 for (int32_t i = 0; i < count; ++i) {
56 if (sensorInfo[i].sensorId == SENSOR_TYPE_ID_DROP_DETECTION) {
57 g_hasDropDetection = true;
58 SEN_HILOGD("Exist drop detection sensor");
59 break;
60 }
61 }
62 SEN_HILOGD("Not exist drop detection sensor");
63 }
64
TearDownTestCase()65 void DropDetectionTest::TearDownTestCase() {}
66
SetUp()67 void DropDetectionTest::SetUp() {}
68
TearDown()69 void DropDetectionTest::TearDown() {}
70
DropDetectionDataCallbackImpl(SensorEvent * event)71 void DropDetectionDataCallbackImpl(SensorEvent *event)
72 {
73 if (event == nullptr) {
74 SEN_HILOGE("event is nullptr");
75 return;
76 }
77 if (event[0].data == nullptr) {
78 SEN_HILOGE("event[0].data is nullptr");
79 return;
80 }
81 if (event[0].dataLen < sizeof(DropDetectionData)) {
82 SEN_HILOGE("Event dataLen less than drop detection data size, event.dataLen:%{public}u", event[0].dataLen);
83 return;
84 }
85 DropDetectionData *dropDetectionData = reinterpret_cast<DropDetectionData *>(event[0].data);
86 SEN_HILOGD("sensorId:%{public}d, version:%{public}d, dataLen:%{public}u, status:%{public}f", event[0].sensorTypeId,
87 event[0].version, event[0].dataLen, dropDetectionData->status);
88 }
89
DropDetectionDataCallbackImpl2(SensorEvent * event)90 void DropDetectionDataCallbackImpl2(SensorEvent *event)
91 {
92 if (event == nullptr) {
93 SEN_HILOGE("event is nullptr");
94 return;
95 }
96 if (event[0].data == nullptr) {
97 SEN_HILOGE("event[0].data is nullptr");
98 return;
99 }
100 if (event[0].dataLen < sizeof(DropDetectionData)) {
101 SEN_HILOGE("Event dataLen less than drop detrction data size, event.dataLen:%{public}u", event[0].dataLen);
102 return;
103 }
104 DropDetectionData *dropDetectionData = reinterpret_cast<DropDetectionData *>(event[0].data);
105 SEN_HILOGD("sensorId:%{public}d, version:%{public}d, dataLen:%{public}u, status:%{public}f", event[0].sensorTypeId,
106 event[0].version, event[0].dataLen, dropDetectionData->status);
107 }
108
109 HWTEST_F(DropDetectionTest, DropDetectionTest_001, TestSize.Level1)
110 {
111 SEN_HILOGI("DropDetectionTest_001 enter");
112 if (g_hasDropDetection) {
113 ASSERT_NE(ActivateSensor(SENSOR_TYPE_ID_DROP_DETECTION, nullptr), OHOS::Sensors::SUCCESS);
114 }
115 }
116
117 HWTEST_F(DropDetectionTest, DropDetectionTest_002, TestSize.Level1)
118 {
119 SEN_HILOGI("DropDetectionTest_002 enter");
120 if (g_hasDropDetection) {
121 SensorUser user;
122 user.callback = DropDetectionDataCallbackImpl;
123 ASSERT_NE(ActivateSensor(-1, &user), OHOS::Sensors::SUCCESS);
124 }
125 }
126
127 HWTEST_F(DropDetectionTest, DropDetectionTest_003, TestSize.Level1)
128 {
129 SEN_HILOGI("DropDetectionTest_003 enter");
130 if (g_hasDropDetection) {
131 SensorUser user;
132 user.callback = nullptr;
133 ASSERT_NE(ActivateSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user), OHOS::Sensors::SUCCESS);
134 }
135 }
136
137 HWTEST_F(DropDetectionTest, DropDetectionTest_004, TestSize.Level1)
138 {
139 SEN_HILOGI("DropDetectionTest_004 enter");
140 if (g_hasDropDetection) {
141 ASSERT_NE(DeactivateSensor(SENSOR_TYPE_ID_DROP_DETECTION, nullptr), OHOS::Sensors::SUCCESS);
142 }
143 }
144
145 HWTEST_F(DropDetectionTest, DropDetectionTest_005, TestSize.Level1)
146 {
147 SEN_HILOGI("DropDetectionTest_005 enter");
148 if (g_hasDropDetection) {
149 SensorUser user;
150 user.callback = DropDetectionDataCallbackImpl;
151 ASSERT_NE(DeactivateSensor(-1, &user), OHOS::Sensors::SUCCESS);
152 }
153 }
154
155 HWTEST_F(DropDetectionTest, DropDetectionTest_006, TestSize.Level1)
156 {
157 SEN_HILOGI("DropDetectionTest_006 enter");
158 if (g_hasDropDetection) {
159 SensorUser user;
160 user.callback = nullptr;
161 ASSERT_NE(DeactivateSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user), OHOS::Sensors::SUCCESS);
162 }
163 }
164
165 HWTEST_F(DropDetectionTest, DropDetectionTest_007, TestSize.Level1)
166 {
167 SEN_HILOGI("DropDetectionTest_007 enter");
168 if (g_hasDropDetection) {
169 ASSERT_NE(SetBatch(SENSOR_TYPE_ID_DROP_DETECTION, nullptr, 10000000, 10000000), OHOS::Sensors::SUCCESS);
170 }
171 }
172
173 HWTEST_F(DropDetectionTest, DropDetectionTest_008, TestSize.Level1)
174 {
175 SEN_HILOGI("DropDetectionTest_008 enter");
176 if (g_hasDropDetection) {
177 SensorUser user;
178 user.callback = DropDetectionDataCallbackImpl;
179 ASSERT_NE(SetBatch(-1, &user, 10000000, 10000000), OHOS::Sensors::SUCCESS);
180 }
181 }
182
183 HWTEST_F(DropDetectionTest, DropDetectionTest_009, TestSize.Level1)
184 {
185 SEN_HILOGI("DropDetectionTest_009 enter");
186 if (g_hasDropDetection) {
187 SensorUser user;
188 user.callback = nullptr;
189 ASSERT_NE(SetBatch(SENSOR_TYPE_ID_DROP_DETECTION, &user, 10000000, 10000000), OHOS::Sensors::SUCCESS);
190 }
191 }
192
193 HWTEST_F(DropDetectionTest, DropDetectionTest_010, TestSize.Level1)
194 {
195 SEN_HILOGI("DropDetectionTest_010 enter");
196 if (g_hasDropDetection) {
197 SensorUser user;
198 user.callback = DropDetectionDataCallbackImpl;
199 ASSERT_NE(SetBatch(SENSOR_TYPE_ID_DROP_DETECTION, &user, -1, -1), OHOS::Sensors::SUCCESS);
200 }
201 }
202
203 HWTEST_F(DropDetectionTest, DropDetectionTest_011, TestSize.Level1)
204 {
205 SEN_HILOGI("DropDetectionTest_011 enter");
206 if (g_hasDropDetection) {
207 ASSERT_NE(SubscribeSensor(SENSOR_TYPE_ID_DROP_DETECTION, nullptr), OHOS::Sensors::SUCCESS);
208 }
209 }
210
211 HWTEST_F(DropDetectionTest, DropDetectionTest_012, TestSize.Level1)
212 {
213 SEN_HILOGI("DropDetectionTest_012 enter");
214 if (g_hasDropDetection) {
215 SensorUser user;
216 user.callback = DropDetectionDataCallbackImpl;
217 ASSERT_NE(SubscribeSensor(-1, &user), OHOS::Sensors::SUCCESS);
218 }
219 }
220
221 HWTEST_F(DropDetectionTest, DropDetectionTest_013, TestSize.Level1)
222 {
223 SEN_HILOGI("DropDetectionTest_013 enter");
224 if (g_hasDropDetection) {
225 SensorUser user;
226 user.callback = nullptr;
227 ASSERT_NE(SubscribeSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user), OHOS::Sensors::SUCCESS);
228 }
229 }
230
231 HWTEST_F(DropDetectionTest, DropDetectionTest_014, TestSize.Level1)
232 {
233 SEN_HILOGI("DropDetectionTest_014 enter");
234 if (g_hasDropDetection) {
235 ASSERT_NE(UnsubscribeSensor(SENSOR_TYPE_ID_DROP_DETECTION, nullptr), OHOS::Sensors::SUCCESS);
236 }
237 }
238
239 HWTEST_F(DropDetectionTest, DropDetectionTest_015, TestSize.Level1)
240 {
241 SEN_HILOGI("DropDetectionTest_015 enter");
242 if (g_hasDropDetection) {
243 SensorUser user;
244 user.callback = DropDetectionDataCallbackImpl;
245 ASSERT_NE(UnsubscribeSensor(-1, &user), OHOS::Sensors::SUCCESS);
246 }
247 }
248
249 HWTEST_F(DropDetectionTest, DropDetectionTest_016, TestSize.Level1)
250 {
251 SEN_HILOGI("DropDetectionTest_016 enter");
252 if (g_hasDropDetection) {
253 SensorUser user;
254 user.callback = nullptr;
255 ASSERT_NE(UnsubscribeSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user), OHOS::Sensors::SUCCESS);
256 }
257 }
258
259 HWTEST_F(DropDetectionTest, DropDetectionTest_017, TestSize.Level1)
260 {
261 SEN_HILOGI("DropDetectionTest_017 enter");
262 if (g_hasDropDetection) {
263 SensorUser user;
264 user.callback = DropDetectionDataCallbackImpl;
265 ASSERT_EQ(SubscribeSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user), OHOS::Sensors::SUCCESS);
266 ASSERT_EQ(SetBatch(SENSOR_TYPE_ID_DROP_DETECTION, &user, 10000000, 10000000), OHOS::Sensors::SUCCESS);
267 ASSERT_EQ(ActivateSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user), OHOS::Sensors::SUCCESS);
268 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
269 ASSERT_EQ(DeactivateSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user), OHOS::Sensors::SUCCESS);
270 ASSERT_EQ(UnsubscribeSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user), OHOS::Sensors::SUCCESS);
271 }
272 }
273
274 HWTEST_F(DropDetectionTest, DropDetectionTest_018, TestSize.Level1)
275 {
276 SEN_HILOGI("DropDetectionTest_018 enter");
277 if (g_hasDropDetection) {
278 SensorUser user;
279 user.callback = DropDetectionDataCallbackImpl;
280 ASSERT_EQ(SubscribeSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user), OHOS::Sensors::SUCCESS);
281 ASSERT_EQ(SetBatch(SENSOR_TYPE_ID_DROP_DETECTION, &user, 10000000, 10000000), OHOS::Sensors::SUCCESS);
282 ASSERT_EQ(ActivateSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user), OHOS::Sensors::SUCCESS);
283 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
284 ASSERT_EQ(DeactivateSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user), OHOS::Sensors::SUCCESS);
285 ASSERT_EQ(UnsubscribeSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user), OHOS::Sensors::SUCCESS);
286
287 SensorUser user2;
288 user2.callback = DropDetectionDataCallbackImpl2;
289 ASSERT_EQ(SubscribeSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user2), OHOS::Sensors::SUCCESS);
290 ASSERT_EQ(SetBatch(SENSOR_TYPE_ID_DROP_DETECTION, &user2, 20000000, 20000000), OHOS::Sensors::SUCCESS);
291 ASSERT_EQ(ActivateSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user2), OHOS::Sensors::SUCCESS);
292 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
293 ASSERT_EQ(DeactivateSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user2), OHOS::Sensors::SUCCESS);
294 ASSERT_EQ(UnsubscribeSensor(SENSOR_TYPE_ID_DROP_DETECTION, &user2), OHOS::Sensors::SUCCESS);
295 }
296 }
297 } // namespace Sensors
298 } // namespace OHOS
299