1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <android-base/file.h>
18 #include <android-base/properties.h>
19 #include <gtest/gtest.h>
20 
21 #include <fstream>
22 
23 #include "Hardware.h"
24 
25 namespace android {
26 namespace hardware {
27 namespace vibrator {
28 namespace V1_3 {
29 namespace implementation {
30 
31 using ::android::base::SetProperty;
32 using ::android::base::WaitForProperty;
33 
34 using ::testing::Test;
35 
36 class HwCalTest : public Test {
37   protected:
38     static constexpr char PROPERTY_PREFIX[] = "test.vibrator.hal.";
39 
40     static constexpr uint32_t DEFAULT_LRA_PERIOD = 262;
41 
42     static constexpr uint32_t DEFAULT_FREQUENCY_SHIFT = 10;
43     static constexpr uint32_t DEFAULT_VOLTAGE_MAX = 107;
44 
45     static constexpr uint32_t DEFAULT_CLICK_DURATION_MS = 6;
46     static constexpr uint32_t DEFAULT_TICK_DURATION_MS = 2;
47     static constexpr uint32_t DEFAULT_DOUBLE_CLICK_DURATION_MS = 135;
48     static constexpr uint32_t DEFAULT_HEAVY_CLICK_DURATION_MS = 8;
49 
50   public:
SetUp()51     void SetUp() override {
52         setenv("PROPERTY_PREFIX", PROPERTY_PREFIX, true);
53         setenv("CALIBRATION_FILEPATH", mCalFile.path, true);
54     }
55 
56   private:
57     template <typename T>
pack(std::ostream & stream,const T & value,std::string lpad,std::string rpad)58     static void pack(std::ostream &stream, const T &value, std::string lpad, std::string rpad) {
59         stream << lpad << value << rpad;
60     }
61 
62   protected:
createHwCal()63     void createHwCal() { mHwCal = std::make_unique<HwCal>(); }
64 
65     template <typename T>
write(const std::string key,const T & value,std::string lpad=" ",std::string rpad="")66     void write(const std::string key, const T &value, std::string lpad = " ",
67                std::string rpad = "") {
68         std::ofstream calfile{mCalFile.path, std::ios_base::app};
69         calfile << key << ":";
70         pack(calfile, value, lpad, rpad);
71         calfile << std::endl;
72     }
73 
unlink()74     void unlink() { ::unlink(mCalFile.path); }
75 
76   protected:
77     std::unique_ptr<Vibrator::HwCal> mHwCal;
78     TemporaryFile mCalFile;
79 };
80 
TEST_F(HwCalTest,closeloop_present)81 TEST_F(HwCalTest, closeloop_present) {
82     std::string prefix{PROPERTY_PREFIX};
83     uint32_t expect = std::rand();
84     uint32_t actual = ~expect;
85 
86     EXPECT_TRUE(SetProperty(prefix + "closeloop.threshold", std::to_string(expect)));
87 
88     createHwCal();
89 
90     EXPECT_TRUE(mHwCal->getCloseLoopThreshold(&actual));
91     EXPECT_EQ(expect, actual);
92 }
93 
TEST_F(HwCalTest,closeloop_missing)94 TEST_F(HwCalTest, closeloop_missing) {
95     std::string prefix{PROPERTY_PREFIX};
96     uint32_t expect = UINT32_MAX;
97     uint32_t actual = ~expect;
98 
99     EXPECT_TRUE(SetProperty(prefix + "closeloop.threshold", std::string()));
100 
101     createHwCal();
102 
103     EXPECT_TRUE(mHwCal->getCloseLoopThreshold(&actual));
104     EXPECT_EQ(expect, actual);
105 }
106 
TEST_F(HwCalTest,dynamicconfig_presentFalse)107 TEST_F(HwCalTest, dynamicconfig_presentFalse) {
108     std::string prefix{PROPERTY_PREFIX};
109     bool expect = false;
110     bool actual = !expect;
111 
112     EXPECT_TRUE(SetProperty(prefix + "config.dynamic", "0"));
113 
114     createHwCal();
115 
116     EXPECT_TRUE(mHwCal->getDynamicConfig(&actual));
117     EXPECT_EQ(expect, actual);
118 }
119 
TEST_F(HwCalTest,dynamicconfig_presentTrue)120 TEST_F(HwCalTest, dynamicconfig_presentTrue) {
121     std::string prefix{PROPERTY_PREFIX};
122     bool expect = true;
123     bool actual = !expect;
124 
125     EXPECT_TRUE(SetProperty(prefix + "config.dynamic", "1"));
126 
127     createHwCal();
128 
129     EXPECT_TRUE(mHwCal->getDynamicConfig(&actual));
130     EXPECT_EQ(expect, actual);
131 }
132 
TEST_F(HwCalTest,dynamicconfig_missing)133 TEST_F(HwCalTest, dynamicconfig_missing) {
134     std::string prefix{PROPERTY_PREFIX};
135     bool expect = false;
136     bool actual = !expect;
137 
138     EXPECT_TRUE(SetProperty(prefix + "config.dynamic", std::string()));
139 
140     createHwCal();
141 
142     EXPECT_TRUE(mHwCal->getDynamicConfig(&actual));
143     EXPECT_EQ(expect, actual);
144 }
145 
TEST_F(HwCalTest,freqshift_present)146 TEST_F(HwCalTest, freqshift_present) {
147     std::string prefix{PROPERTY_PREFIX};
148     uint32_t expect = std::rand();
149     uint32_t actual = ~expect;
150 
151     EXPECT_TRUE(SetProperty(prefix + "long.frequency.shift", std::to_string(expect)));
152 
153     createHwCal();
154 
155     EXPECT_TRUE(mHwCal->getLongFrequencyShift(&actual));
156     EXPECT_EQ(expect, actual);
157 }
158 
TEST_F(HwCalTest,freqshift_missing)159 TEST_F(HwCalTest, freqshift_missing) {
160     std::string prefix{PROPERTY_PREFIX};
161     uint32_t expect = DEFAULT_FREQUENCY_SHIFT;
162     uint32_t actual = ~expect;
163 
164     EXPECT_TRUE(SetProperty(prefix + "long.frequency.shift", std::string()));
165 
166     createHwCal();
167 
168     EXPECT_TRUE(mHwCal->getLongFrequencyShift(&actual));
169     EXPECT_EQ(expect, actual);
170 }
171 
TEST_F(HwCalTest,shortvolt_present)172 TEST_F(HwCalTest, shortvolt_present) {
173     std::string prefix{PROPERTY_PREFIX};
174     uint32_t expect = std::rand();
175     uint32_t actual = ~expect;
176 
177     EXPECT_TRUE(SetProperty(prefix + "short.voltage", std::to_string(expect)));
178 
179     createHwCal();
180 
181     EXPECT_TRUE(mHwCal->getShortVoltageMax(&actual));
182     EXPECT_EQ(expect, actual);
183 }
184 
TEST_F(HwCalTest,shortvolt_missing)185 TEST_F(HwCalTest, shortvolt_missing) {
186     std::string prefix{PROPERTY_PREFIX};
187     uint32_t expect = DEFAULT_VOLTAGE_MAX;
188     uint32_t actual = ~expect;
189 
190     EXPECT_TRUE(SetProperty(prefix + "short.voltage", std::string()));
191 
192     createHwCal();
193 
194     EXPECT_TRUE(mHwCal->getShortVoltageMax(&actual));
195     EXPECT_EQ(expect, actual);
196 }
197 
TEST_F(HwCalTest,longvolt_present)198 TEST_F(HwCalTest, longvolt_present) {
199     std::string prefix{PROPERTY_PREFIX};
200     uint32_t expect = std::rand();
201     uint32_t actual = ~expect;
202 
203     EXPECT_TRUE(SetProperty(prefix + "long.voltage", std::to_string(expect)));
204 
205     createHwCal();
206 
207     EXPECT_TRUE(mHwCal->getLongVoltageMax(&actual));
208     EXPECT_EQ(expect, actual);
209 }
210 
TEST_F(HwCalTest,longvolt_missing)211 TEST_F(HwCalTest, longvolt_missing) {
212     std::string prefix{PROPERTY_PREFIX};
213     uint32_t expect = DEFAULT_VOLTAGE_MAX;
214     uint32_t actual = ~expect;
215 
216     EXPECT_TRUE(SetProperty(prefix + "long.voltage", std::string()));
217 
218     createHwCal();
219 
220     EXPECT_TRUE(mHwCal->getLongVoltageMax(&actual));
221     EXPECT_EQ(expect, actual);
222 }
223 
TEST_F(HwCalTest,click_present)224 TEST_F(HwCalTest, click_present) {
225     std::string prefix{PROPERTY_PREFIX};
226     uint32_t expect = std::rand();
227     uint32_t actual = ~expect;
228 
229     EXPECT_TRUE(SetProperty(prefix + "click.duration", std::to_string(expect)));
230 
231     createHwCal();
232 
233     EXPECT_TRUE(mHwCal->getClickDuration(&actual));
234     EXPECT_EQ(expect, actual);
235 }
236 
TEST_F(HwCalTest,click_missing)237 TEST_F(HwCalTest, click_missing) {
238     std::string prefix{PROPERTY_PREFIX};
239     uint32_t expect = DEFAULT_CLICK_DURATION_MS;
240     uint32_t actual = ~expect;
241 
242     EXPECT_TRUE(SetProperty(prefix + "click.duration", std::string()));
243 
244     createHwCal();
245 
246     EXPECT_TRUE(mHwCal->getClickDuration(&actual));
247     EXPECT_EQ(expect, actual);
248 }
249 
TEST_F(HwCalTest,tick_present)250 TEST_F(HwCalTest, tick_present) {
251     std::string prefix{PROPERTY_PREFIX};
252     uint32_t expect = std::rand();
253     uint32_t actual = ~expect;
254 
255     EXPECT_TRUE(SetProperty(prefix + "tick.duration", std::to_string(expect)));
256 
257     createHwCal();
258 
259     EXPECT_TRUE(mHwCal->getTickDuration(&actual));
260     EXPECT_EQ(expect, actual);
261 }
262 
TEST_F(HwCalTest,tick_missing)263 TEST_F(HwCalTest, tick_missing) {
264     std::string prefix{PROPERTY_PREFIX};
265     uint32_t expect = DEFAULT_TICK_DURATION_MS;
266     uint32_t actual = ~expect;
267 
268     EXPECT_TRUE(SetProperty(prefix + "tick.duration", std::string()));
269 
270     createHwCal();
271 
272     EXPECT_TRUE(mHwCal->getTickDuration(&actual));
273     EXPECT_EQ(expect, actual);
274 }
275 
TEST_F(HwCalTest,doubleclick)276 TEST_F(HwCalTest, doubleclick) {
277     std::string prefix{PROPERTY_PREFIX};
278     uint32_t expect = DEFAULT_DOUBLE_CLICK_DURATION_MS;
279     uint32_t actual = ~expect;
280 
281     createHwCal();
282 
283     EXPECT_TRUE(mHwCal->getDoubleClickDuration(&actual));
284     EXPECT_EQ(expect, actual);
285 }
286 
TEST_F(HwCalTest,heavyclick_present)287 TEST_F(HwCalTest, heavyclick_present) {
288     std::string prefix{PROPERTY_PREFIX};
289     uint32_t expect = std::rand();
290     uint32_t actual = ~expect;
291 
292     EXPECT_TRUE(SetProperty(prefix + "heavyclick.duration", std::to_string(expect)));
293 
294     createHwCal();
295 
296     EXPECT_TRUE(mHwCal->getHeavyClickDuration(&actual));
297     EXPECT_EQ(expect, actual);
298 }
299 
TEST_F(HwCalTest,heavyclick_missing)300 TEST_F(HwCalTest, heavyclick_missing) {
301     std::string prefix{PROPERTY_PREFIX};
302     uint32_t expect = DEFAULT_HEAVY_CLICK_DURATION_MS;
303     uint32_t actual = ~expect;
304 
305     EXPECT_TRUE(SetProperty(prefix + "heavyclick.duration", std::string()));
306 
307     createHwCal();
308 
309     EXPECT_TRUE(mHwCal->getHeavyClickDuration(&actual));
310     EXPECT_EQ(expect, actual);
311 }
312 
TEST_F(HwCalTest,autocal_present)313 TEST_F(HwCalTest, autocal_present) {
314     std::string expect = std::to_string(std::rand()) + " " + std::to_string(std::rand()) + " " +
315                          std::to_string(std::rand());
316     std::string actual = "";
317 
318     write("autocal", expect);
319 
320     createHwCal();
321 
322     EXPECT_TRUE(mHwCal->getAutocal(&actual));
323     EXPECT_EQ(expect, actual);
324 }
325 
TEST_F(HwCalTest,autocal_missing)326 TEST_F(HwCalTest, autocal_missing) {
327     std::string actual;
328 
329     createHwCal();
330 
331     EXPECT_FALSE(mHwCal->getAutocal(&actual));
332 }
333 
TEST_F(HwCalTest,lra_period_present)334 TEST_F(HwCalTest, lra_period_present) {
335     uint32_t expect = std::rand();
336     uint32_t actual = ~expect;
337 
338     write("lra_period", expect);
339 
340     createHwCal();
341 
342     EXPECT_TRUE(mHwCal->getLraPeriod(&actual));
343     EXPECT_EQ(expect, actual);
344 }
345 
TEST_F(HwCalTest,lra_period_missing)346 TEST_F(HwCalTest, lra_period_missing) {
347     uint32_t expect = DEFAULT_LRA_PERIOD;
348     uint32_t actual = ~expect;
349 
350     createHwCal();
351 
352     EXPECT_TRUE(mHwCal->getLraPeriod(&actual));
353     EXPECT_EQ(expect, actual);
354 }
355 
TEST_F(HwCalTest,multiple)356 TEST_F(HwCalTest, multiple) {
357     std::string autocalExpect = std::to_string(std::rand()) + " " + std::to_string(std::rand()) +
358                                 " " + std::to_string(std::rand());
359     std::string autocalActual = "";
360     uint32_t lraPeriodExpect = std::rand();
361     uint32_t lraPeriodActual = ~lraPeriodExpect;
362 
363     write("autocal", autocalExpect);
364     write("lra_period", lraPeriodExpect);
365 
366     createHwCal();
367 
368     EXPECT_TRUE(mHwCal->getAutocal(&autocalActual));
369     EXPECT_EQ(autocalExpect, autocalActual);
370     EXPECT_TRUE(mHwCal->getLraPeriod(&lraPeriodActual));
371     EXPECT_EQ(lraPeriodExpect, lraPeriodActual);
372 }
373 
TEST_F(HwCalTest,trimming)374 TEST_F(HwCalTest, trimming) {
375     std::string autocalExpect = std::to_string(std::rand()) + " " + std::to_string(std::rand()) +
376                                 " " + std::to_string(std::rand());
377     std::string autocalActual = "";
378     uint32_t lraPeriodExpect = std::rand();
379     uint32_t lraPeriodActual = ~lraPeriodExpect;
380 
381     write("autocal", autocalExpect, " \t", "\t ");
382     write("lra_period", lraPeriodExpect, " \t", "\t ");
383 
384     createHwCal();
385 
386     EXPECT_TRUE(mHwCal->getAutocal(&autocalActual));
387     EXPECT_EQ(autocalExpect, autocalActual);
388     EXPECT_TRUE(mHwCal->getLraPeriod(&lraPeriodActual));
389     EXPECT_EQ(lraPeriodExpect, lraPeriodActual);
390 }
391 
392 }  // namespace implementation
393 }  // namespace V1_3
394 }  // namespace vibrator
395 }  // namespace hardware
396 }  // namespace android
397