1 /*
2 * Copyright (c) 2022 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 <gtest/gtest.h>
19 #include <securec.h>
20 #include "hdf_base.h"
21 #include "osal_time.h"
22 #include "v1_1/vibrator_interface_proxy.h"
23
24 using namespace OHOS::HDI::Vibrator::V1_1;
25 using namespace testing::ext;
26
27 namespace {
28 const int32_t VIBRATOR_USEC_TIME = 1000000;
29 const int32_t VIBRATOR_MSEC_TIME = 1000;
30 const int32_t VIBRATOR_DURATION = 2000;
31 const int32_t VIBRATOR_COMMON_TIME = 500;
32 const char *g_timeSequence = "haptic.clock.timer";
33 const char *g_builtIn = "haptic.default.effect";
34 sptr<IVibratorInterface> g_vibratorInterface = nullptr;
35 }
36
37 class HdfVibratorHdiPerformanceTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 void SetUp();
42 void TearDown();
43 };
44
SetUpTestCase()45 void HdfVibratorHdiPerformanceTest::SetUpTestCase()
46 {
47 g_vibratorInterface = IVibratorInterface::Get();
48 if (g_vibratorInterface == nullptr) {
49 printf("test vibratorHdi get Module insttace failed\n\r");
50 }
51 }
52
TearDownTestCase()53 void HdfVibratorHdiPerformanceTest::TearDownTestCase()
54 {
55 }
56
SetUp()57 void HdfVibratorHdiPerformanceTest::SetUp()
58 {
59 }
60
TearDown()61 void HdfVibratorHdiPerformanceTest::TearDown()
62 {
63 }
64
65 /**
66 * @tc.name: VibratorStartOnce001
67 * @tc.desc: Interface performance test.
68 * @tc.type: FUNC
69 * @tc.require: #I4NN4Z
70 */
71 HWTEST_F(HdfVibratorHdiPerformanceTest, VibratorStartOnce001, TestSize.Level1)
72 {
73 int timeUsed = 0;
74 struct timespec tv1 = (struct timespec) {0};
75 struct timespec tv2 = (struct timespec) {0};
76
77 clock_gettime(CLOCK_REALTIME, &tv1);
78 int ret = g_vibratorInterface->StartOnce(VIBRATOR_DURATION);
79 int endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
80 clock_gettime(CLOCK_REALTIME, &tv2);
81
82 timeUsed = ((tv2.tv_sec * VIBRATOR_USEC_TIME + tv2.tv_nsec / VIBRATOR_MSEC_TIME) -
83 (tv1.tv_sec * VIBRATOR_USEC_TIME + tv1.tv_nsec / VIBRATOR_MSEC_TIME));
84 EXPECT_GT(VIBRATOR_COMMON_TIME, timeUsed);
85 EXPECT_EQ(0, ret);
86 EXPECT_EQ(0, endRet);
87 }
88
89 /**
90 * @tc.name: VibratorHapticDefaultTime001
91 * @tc.desc: Interface performance test.
92 * @tc.type: FUNC
93 * @tc.require: #I4NN4Z
94 */
95 HWTEST_F(HdfVibratorHdiPerformanceTest, VibratorHapticDefaultTime001, TestSize.Level1)
96 {
97 int timeUsed = 0;
98 struct timespec tv1 = (struct timespec) {0};
99 struct timespec tv2 = (struct timespec) {0};
100
101 clock_gettime(CLOCK_REALTIME, &tv1);
102 int ret = g_vibratorInterface->Start(g_timeSequence);
103 int endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
104 clock_gettime(CLOCK_REALTIME, &tv2);
105
106 timeUsed = ((tv2.tv_sec * VIBRATOR_USEC_TIME + tv2.tv_nsec / VIBRATOR_MSEC_TIME) -
107 (tv1.tv_sec * VIBRATOR_USEC_TIME + tv1.tv_nsec / VIBRATOR_MSEC_TIME));
108 EXPECT_GT(VIBRATOR_COMMON_TIME, timeUsed);
109 EXPECT_EQ(0, ret);
110 EXPECT_EQ(0, endRet);
111 }
112
113 /**
114 * @tc.name: VibratorHapticDefaultEffect001
115 * @tc.desc: Interface performance test.
116 * @tc.type: FUNC
117 * @tc.require: #I4NN4Z
118 */
119 HWTEST_F(HdfVibratorHdiPerformanceTest, VibratorHapticDefaultEffect001, TestSize.Level1)
120 {
121 int timeUsed = 0;
122 struct timespec tv1 = (struct timespec) {0};
123 struct timespec tv2 = (struct timespec) {0};
124
125 clock_gettime(CLOCK_REALTIME, &tv1);
126 int ret = g_vibratorInterface->Start(g_builtIn);
127 int endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
128 clock_gettime(CLOCK_REALTIME, &tv2);
129
130 timeUsed = ((tv2.tv_sec * VIBRATOR_USEC_TIME + tv2.tv_nsec / VIBRATOR_MSEC_TIME) -
131 (tv1.tv_sec * VIBRATOR_USEC_TIME + tv1.tv_nsec / VIBRATOR_MSEC_TIME));
132 EXPECT_GT(VIBRATOR_COMMON_TIME, timeUsed);
133 EXPECT_EQ(0, ret);
134 EXPECT_EQ(0, endRet);
135 }
136