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 #include <cmath>
16 #include <cstdio>
17 #include <gtest/gtest.h>
18 #include <securec.h>
19 #include "hdf_base.h"
20 #include "osal_time.h"
21 #include "v2_0/ivibrator_interface.h"
22 #include "vibrator_type.h"
23 #include "vibrator_plug_callback_impl.h"
24
25 using namespace OHOS::HDI::Vibrator;
26 using namespace OHOS::HDI::Vibrator::V2_0;
27 using namespace testing::ext;
28
29 namespace {
30 sptr<OHOS::HDI::Vibrator::V2_0::IVibratorInterface> g_vibratorInterface = nullptr;
31 sptr<VibratorPlugCallbackImpl> g_vibratorPlugCallback = new VibratorPlugCallbackImpl();
32 }
33
34 class HatsHdfVibratorPlugCallbackTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp();
39 void TearDown();
40 };
41
SetUpTestCase()42 void HatsHdfVibratorPlugCallbackTest::SetUpTestCase()
43 {
44 g_vibratorInterface = OHOS::HDI::Vibrator::V2_0::IVibratorInterface::Get();
45 }
46
TearDownTestCase()47 void HatsHdfVibratorPlugCallbackTest::TearDownTestCase()
48 {
49 }
50
SetUp()51 void HatsHdfVibratorPlugCallbackTest::SetUp()
52 {
53 if (g_vibratorInterface == nullptr) {
54 printf("g_vibratorInterface is nullptr");
55 GTEST_SKIP() << "Device not exist" << std::endl;
56 return;
57 }
58 }
59
TearDown()60 void HatsHdfVibratorPlugCallbackTest::TearDown()
61 {
62 }
63
64 /**
65 * @tc.name: SUB_Driver_Vibrator_HdiVibrator_0100
66 * @tc.number : SUB_Driver_Vibrator_HdiVibrator_0100
67 * @tc.desc: Creat a vibrator instance. The instance is not empty.
68 * @tc.type: FUNC
69 */
70 HWTEST_F(HatsHdfVibratorPlugCallbackTest, SUB_Driver_Vibrator_HdiVibrator_0100, Function | MediumTest | Level1)
71 {
72 ASSERT_NE(nullptr, g_vibratorInterface);
73 }
74
75 /**
76 * @tc.name: testHdiVibratorRegVibratorPlugCallBack
77 * @tc.number : testHdiVibratorRegVibratorPlugCallBack
78 * @tc.desc: Read event data for the specified vibrator.
79 */
80 HWTEST_F(HatsHdfVibratorPlugCallbackTest, testHdiVibratorRegVibratorPlugCallBack, TestSize.Level1)
81 {
82 if (g_vibratorInterface == nullptr) {
83 ASSERT_NE(nullptr, g_vibratorInterface);
84 return;
85 }
86 int32_t ret = g_vibratorInterface->RegVibratorPlugCallback(g_vibratorPlugCallback);
87 EXPECT_EQ(0, ret);
88 printf("RegVibratorPlugCallBack\n");
89
90 OsalMSleep(3000);
91
92 ret = g_vibratorInterface->UnRegVibratorPlugCallback(g_vibratorPlugCallback);
93 EXPECT_EQ(0, ret);
94 printf("RegVibratorPlugCallBack\n");
95 }