• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cmath>
17 #include <cstdio>
18 #include <unistd.h>
19 #include <gtest/gtest.h>
20 #include <securec.h>
21 #include "hdf_base.h"
22 #include "osal_time.h"
23 #include "v3_0/isensor_interface.h"
24 #include "sensor_type.h"
25 #include "sensor_callback_impl.h"
26 using namespace OHOS::HDI::Sensor::V3_0;
27 using namespace testing::ext;
28 
29 namespace {
30     sptr<ISensorInterface>  g_sensorInterface = nullptr;
31     sptr<ISensorCallback> g_traditionalCallback = new SensorCallbackImpl();
32     std::vector<HdfSensorInformation> g_info;
33 }
34 
35 class HatsHdfSensorMultiClientTest1 : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp();
40     void TearDown();
41 };
42 
SetUpTestCase()43 void HatsHdfSensorMultiClientTest1::SetUpTestCase()
44 {
45     g_sensorInterface = ISensorInterface::Get();
46     if (g_sensorInterface != nullptr) {
47         g_sensorInterface->GetAllSensorInfo(g_info);
48     }
49 }
50 
TearDownTestCase()51 void HatsHdfSensorMultiClientTest1::TearDownTestCase()
52 {
53 }
54 
SetUp()55 void HatsHdfSensorMultiClientTest1::SetUp()
56 {
57     if (g_sensorInterface == nullptr) {
58         printf("Sensor list is empty");
59         GTEST_SKIP() << "Device not exist" << std::endl;
60         return;
61     }
62 }
63 
TearDown()64 void HatsHdfSensorMultiClientTest1::TearDown()
65 {
66 }
67 
68 /**
69   * @tc.name: SUB_Driver_Sensor_HdiSensor_0100
70   * @tc.number : SUB_Driver_Sensor_HdiSensor_0100
71   * @tc.desc: Get a client and check whether the client is empty.
72   * @tc.type: FUNC
73   */
74 HWTEST_F(HatsHdfSensorMultiClientTest1, SUB_Driver_Sensor_HdiSensor_0100, TestSize.Level1)
75 {
76     ASSERT_NE(nullptr, g_sensorInterface);
77 }
78 
79 /**
80   * @tc.name: SUB_Driver_Sensor_HdiSensor_0200
81   * @tc.number : SUB_Driver_Sensor_HdiSensor_0200
82   * @tc.desc: Obtains information about all sensors in the system.
83   * @tc.type: FUNC
84   */
85 HWTEST_F(HatsHdfSensorMultiClientTest1, SUB_Driver_Sensor_HdiSensor_0200, TestSize.Level1)
86 {
87     if (g_info.size() > 0) {
88         DeviceSensorInfo deviceSensorInfo = g_info[0].deviceSensorInfo;
89         printf("test deviceSensorInfo is %s\n", SENSOR_HANDLE_TO_C_STR(deviceSensorInfo));
90         int32_t ret = -1;
91 
92         ret = g_sensorInterface->Register(0, g_traditionalCallback);
93         EXPECT_EQ(0, ret);
94 
95         ret = g_sensorInterface->SetBatch(deviceSensorInfo, 100000000, 0);
96         EXPECT_EQ(0, ret);
97         printf("SetBatch samplingInterval is 100000000\n");
98 
99         ret = g_sensorInterface->Enable(deviceSensorInfo);
100         EXPECT_EQ(0, ret);
101 
102         OsalMSleep(3000);
103 
104         ret = g_sensorInterface->Disable(deviceSensorInfo);
105         EXPECT_EQ(0, ret);
106 
107         ret = g_sensorInterface->Unregister(0, g_traditionalCallback);
108         EXPECT_EQ(0, ret);
109     }
110 }