1 /*
2 * Copyright (c) 2021 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 "vibrator_if.h"
23 #include "vibrator_type.h"
24
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 const struct VibratorInterface *g_vibratorPerformanceDev = nullptr;
35 }
36
37 class HdfVibratorPerformanceTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 void SetUp();
42 void TearDown();
43 };
44
SetUpTestCase()45 void HdfVibratorPerformanceTest::SetUpTestCase()
46 {
47 g_vibratorPerformanceDev = NewVibratorInterfaceInstance();
48 if (g_vibratorPerformanceDev == nullptr) {
49 printf("test vibratorHdi get Module insttace failed\n\r");
50 }
51 }
52
TearDownTestCase()53 void HdfVibratorPerformanceTest::TearDownTestCase()
54 {
55 if (g_vibratorPerformanceDev != nullptr) {
56 FreeVibratorInterfaceInstance();
57 g_vibratorPerformanceDev = nullptr;
58 }
59 }
60
SetUp()61 void HdfVibratorPerformanceTest::SetUp()
62 {
63 }
64
TearDown()65 void HdfVibratorPerformanceTest::TearDown()
66 {
67 }
68
69 /**
70 * @tc.name: VibratorStartOnce001
71 * @tc.desc: Interface performance test.
72 * @tc.type: FUNC
73 * @tc.require: AR000FK1J0,AR000FK1JP
74 */
75 HWTEST_F(HdfVibratorPerformanceTest, VibratorStartOnce001, TestSize.Level1)
76 {
77 int timeUsed = 0;
78 struct timespec tv1 = (struct timespec) {0};
79 struct timespec tv2 = (struct timespec) {0};
80
81 clock_gettime(CLOCK_REALTIME, &tv1);
82 int ret = g_vibratorPerformanceDev->StartOnce(VIBRATOR_DURATION);
83 int endRet = g_vibratorPerformanceDev->Stop(VIBRATOR_MODE_ONCE);
84 clock_gettime(CLOCK_REALTIME, &tv2);
85
86 timeUsed = ((tv2.tv_sec * VIBRATOR_USEC_TIME + tv2.tv_nsec / VIBRATOR_MSEC_TIME) -
87 (tv1.tv_sec * VIBRATOR_USEC_TIME + tv1.tv_nsec / VIBRATOR_MSEC_TIME));
88 EXPECT_GT(VIBRATOR_COMMON_TIME, timeUsed);
89 EXPECT_EQ(0, ret);
90 EXPECT_EQ(0, endRet);
91 }
92
93 /**
94 * @tc.name: VibratorHapticDefaultTime001
95 * @tc.desc: Interface performance test.
96 * @tc.type: FUNC
97 * @tc.require: AR000FK1JN,AR000FK1JP
98 */
99 HWTEST_F(HdfVibratorPerformanceTest, VibratorHapticDefaultTime001, TestSize.Level1)
100 {
101 int timeUsed = 0;
102 struct timespec tv1 = (struct timespec) {0};
103 struct timespec tv2 = (struct timespec) {0};
104
105 clock_gettime(CLOCK_REALTIME, &tv1);
106 int ret = g_vibratorPerformanceDev->Start(g_timeSequence);
107 int endRet = g_vibratorPerformanceDev->Stop(VIBRATOR_MODE_PRESET);
108 clock_gettime(CLOCK_REALTIME, &tv2);
109
110 timeUsed = ((tv2.tv_sec * VIBRATOR_USEC_TIME + tv2.tv_nsec / VIBRATOR_MSEC_TIME) -
111 (tv1.tv_sec * VIBRATOR_USEC_TIME + tv1.tv_nsec / VIBRATOR_MSEC_TIME));
112 EXPECT_GT(VIBRATOR_COMMON_TIME, timeUsed);
113 EXPECT_EQ(0, ret);
114 EXPECT_EQ(0, endRet);
115 }
116
117 /**
118 * @tc.name: VibratorHapticDefaultEffect001
119 * @tc.desc: Interface performance test.
120 * @tc.type: FUNC
121 * @tc.require: AR000FK1J0,AR000FK1JP
122 */
123 HWTEST_F(HdfVibratorPerformanceTest, VibratorHapticDefaultEffect001, TestSize.Level1)
124 {
125 int timeUsed = 0;
126 struct timespec tv1 = (struct timespec) {0};
127 struct timespec tv2 = (struct timespec) {0};
128
129 clock_gettime(CLOCK_REALTIME, &tv1);
130 int ret = g_vibratorPerformanceDev->Start(g_builtIn);
131 int endRet = g_vibratorPerformanceDev->Stop(VIBRATOR_MODE_PRESET);
132 clock_gettime(CLOCK_REALTIME, &tv2);
133
134 timeUsed = ((tv2.tv_sec * VIBRATOR_USEC_TIME + tv2.tv_nsec / VIBRATOR_MSEC_TIME) -
135 (tv1.tv_sec * VIBRATOR_USEC_TIME + tv1.tv_nsec / VIBRATOR_MSEC_TIME));
136 EXPECT_GT(VIBRATOR_COMMON_TIME, timeUsed);
137 EXPECT_EQ(0, ret);
138 EXPECT_EQ(0, endRet);
139 }
140