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