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_0/light_interface_proxy.h"
23 
24 using namespace OHOS::HDI::Light::V1_0;
25 using namespace testing::ext;
26 
27 namespace {
28     constexpr int32_t LIGHT_COMMON_TIME = 500;
29     constexpr int32_t MIN_LIGHT_ID = HDF_LIGHT_ID_BATTERY;
30     constexpr int32_t MAX_LIGHT_ID = HDF_LIGHT_ID_ATTENTION;
31     constexpr int32_t LIGHT_USEC_TIME = 1000000;
32     constexpr int32_t LIGHT_MSEC_TIME = 1000;
33     constexpr int32_t ON_TIME = 500;
34     constexpr int32_t OFF_TIME = 500;
35     sptr<ILightInterface> g_lightInterface = nullptr;
36 }
37 
38 class HdfLightHdiPerformanceTest : public testing::Test {
39 public:
40     static void SetUpTestCase();
41     static void TearDownTestCase();
42     void SetUp();
43     void TearDown();
44 };
45 
SetUpTestCase()46 void HdfLightHdiPerformanceTest::SetUpTestCase()
47 {
48     g_lightInterface = ILightInterface::Get();
49     if (g_lightInterface == nullptr) {
50         printf("test lightHdi get Module insttace failed\n\r");
51     }
52 }
53 
TearDownTestCase()54 void HdfLightHdiPerformanceTest::TearDownTestCase()
55 {
56 }
57 
SetUp()58 void HdfLightHdiPerformanceTest::SetUp()
59 {
60 }
61 
TearDown()62 void HdfLightHdiPerformanceTest::TearDown()
63 {
64 }
65 
66 /**
67   * @tc.name: GetLightInfo
68   * @tc.desc:
69   * @tc.type: FUNC
70   * @tc.require: #I4NN4Z
71   */
72 HWTEST_F(HdfLightHdiPerformanceTest, GetLightInfo001, TestSize.Level1)
73 {
74     int timeUsed = 0;
75     struct timespec tv1 = (struct timespec) {0};
76     struct timespec tv2 = (struct timespec) {0};
77 
78     std::vector<HdfLightInfo> info;
79     clock_gettime(CLOCK_REALTIME, &tv1);
80     int32_t ret = g_lightInterface->GetLightInfo(info);
81     clock_gettime(CLOCK_REALTIME, &tv2);
82 
83     timeUsed = ((tv2.tv_sec * LIGHT_USEC_TIME + tv2.tv_nsec / LIGHT_MSEC_TIME) -
84         (tv1.tv_sec * LIGHT_USEC_TIME + tv1.tv_nsec / LIGHT_MSEC_TIME));
85     EXPECT_GT(LIGHT_COMMON_TIME, timeUsed);
86     EXPECT_EQ(0, ret);
87 }
88 
89 /**
90   * @tc.name: TurnOnLight001
91   * @tc.desc: Interface performance test.
92   * @tc.type: FUNC
93   * @tc.require: #I4NN4Z
94   */
95 HWTEST_F(HdfLightHdiPerformanceTest, TurnOnLight001, TestSize.Level1)
96 {
97     std::vector<HdfLightInfo> info;
98     int32_t ret = g_lightInterface->GetLightInfo(info);
99     EXPECT_EQ(0, ret);
100     printf("get light list num[%zu]\n\r", info.size());
101 
102     for (auto iter : info)
103     {
104         EXPECT_GE(iter.lightId, MIN_LIGHT_ID);
105         EXPECT_LE(iter.lightId, MAX_LIGHT_ID);
106 
107         int timeUsed = 0;
108         struct timespec tv1 = (struct timespec) {0};
109         struct timespec tv2 = (struct timespec) {0};
110         HdfLightEffect effect;
111         effect.flashEffect.flashMode = HDF_LIGHT_FLASH_NONE;
112         clock_gettime(CLOCK_REALTIME, &tv1);
113         ret = g_lightInterface->TurnOnLight(iter.lightId, effect);
114         clock_gettime(CLOCK_REALTIME, &tv2);
115 
116         timeUsed = ((tv2.tv_sec * LIGHT_USEC_TIME + tv2.tv_nsec / LIGHT_MSEC_TIME) -
117             (tv1.tv_sec * LIGHT_USEC_TIME + tv1.tv_nsec / LIGHT_MSEC_TIME));
118         EXPECT_GT(LIGHT_COMMON_TIME, timeUsed);
119         EXPECT_EQ(ret, HDF_SUCCESS);
120 
121         clock_gettime(CLOCK_REALTIME, &tv1);
122         ret = g_lightInterface->TurnOffLight(iter.lightId);
123         clock_gettime(CLOCK_REALTIME, &tv2);
124         timeUsed = ((tv2.tv_sec * LIGHT_USEC_TIME + tv2.tv_nsec / LIGHT_MSEC_TIME) -
125             (tv1.tv_sec * LIGHT_USEC_TIME + tv1.tv_nsec / LIGHT_MSEC_TIME));
126         EXPECT_GT(LIGHT_COMMON_TIME, timeUsed);
127         EXPECT_EQ(ret, HDF_SUCCESS);
128     }
129 }
130 
131 /**
132   * @tc.name: TurnOnLight002
133   * @tc.desc: Interface performance test.
134   * @tc.type: FUNC
135   * @tc.require: #I4NN4Z
136   */
137 HWTEST_F(HdfLightHdiPerformanceTest, TurnOnLight002, TestSize.Level1)
138 {
139     std::vector<HdfLightInfo> info;
140     int32_t ret = g_lightInterface->GetLightInfo(info);
141     EXPECT_EQ(0, ret);
142     printf("get light list num[%zu]\n\r", info.size());
143 
144     for (auto iter : info)
145     {
146         EXPECT_GE(iter.lightId, MIN_LIGHT_ID);
147         EXPECT_LE(iter.lightId, MAX_LIGHT_ID);
148 
149         int timeUsed = 0;
150         struct timespec tv1 = (struct timespec) {0};
151         struct timespec tv2 = (struct timespec) {0};
152         HdfLightEffect effect;
153         effect.lightColor.colorValue.rgbColor.r = 0xFF;
154         effect.flashEffect.flashMode = HDF_LIGHT_FLASH_BLINK;
155         effect.flashEffect.onTime = ON_TIME;
156         effect.flashEffect.offTime = OFF_TIME;
157         clock_gettime(CLOCK_REALTIME, &tv1);
158         ret = g_lightInterface->TurnOnLight(iter.lightId, effect);
159         clock_gettime(CLOCK_REALTIME, &tv2);
160 
161         timeUsed = ((tv2.tv_sec * LIGHT_USEC_TIME + tv2.tv_nsec / LIGHT_MSEC_TIME) -
162             (tv1.tv_sec * LIGHT_USEC_TIME + tv1.tv_nsec / LIGHT_MSEC_TIME));
163         EXPECT_GT(LIGHT_COMMON_TIME, timeUsed);
164         EXPECT_EQ(ret, HDF_SUCCESS);
165 
166         clock_gettime(CLOCK_REALTIME, &tv1);
167         ret = g_lightInterface->TurnOffLight(iter.lightId);
168         clock_gettime(CLOCK_REALTIME, &tv2);
169         timeUsed = ((tv2.tv_sec * LIGHT_USEC_TIME + tv2.tv_nsec / LIGHT_MSEC_TIME) -
170             (tv1.tv_sec * LIGHT_USEC_TIME + tv1.tv_nsec / LIGHT_MSEC_TIME));
171         EXPECT_GT(LIGHT_COMMON_TIME, timeUsed);
172         EXPECT_EQ(ret, HDF_SUCCESS);
173     }
174 }
175