1 /*
2  * Copyright (C) 2022 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 <gtest/gtest.h>
19 
20 #include <fstream>
21 
22 #include "Hardware.h"
23 
24 namespace aidl {
25 namespace android {
26 namespace hardware {
27 namespace vibrator {
28 
29 using ::testing::Test;
30 
31 class HwCalTest : public Test {
32   protected:
33     static constexpr std::array<uint32_t, 2> V_TICK_DEFAULT = {1, 100};
34     static constexpr std::array<uint32_t, 2> V_CLICK_DEFAULT = {1, 100};
35     static constexpr std::array<uint32_t, 2> V_LONG_DEFAULT = {1, 100};
36 
37   public:
SetUp()38     void SetUp() override { setenv("CALIBRATION_FILEPATH", mCalFile.path, true); }
39 
40   private:
41     template <typename T>
pack(std::ostream & stream,const T & value,std::string lpad,std::string rpad)42     static void pack(std::ostream &stream, const T &value, std::string lpad, std::string rpad) {
43         stream << lpad << value << rpad;
44     }
45 
46     template <typename T, typename std::array<T, 0>::size_type N>
pack(std::ostream & stream,const std::array<T,N> & value,std::string lpad,std::string rpad)47     static void pack(std::ostream &stream, const std::array<T, N> &value, std::string lpad,
48                      std::string rpad) {
49         for (auto &entry : value) {
50             pack(stream, entry, lpad, rpad);
51         }
52     }
53 
54   protected:
createHwCal()55     void createHwCal() { mHwCal = std::make_unique<HwCal>(); }
56 
57     template <typename T>
write(const std::string key,const T & value,std::string lpad=" ",std::string rpad="")58     void write(const std::string key, const T &value, std::string lpad = " ",
59                std::string rpad = "") {
60         std::ofstream calfile{mCalFile.path, std::ios_base::app};
61         calfile << key << ":";
62         pack(calfile, value, lpad, rpad);
63         calfile << std::endl;
64     }
65 
unlink()66     void unlink() { ::unlink(mCalFile.path); }
67 
68   protected:
69     std::unique_ptr<Vibrator::HwCal> mHwCal;
70     TemporaryFile mCalFile;
71 };
72 
TEST_F(HwCalTest,f0_measured)73 TEST_F(HwCalTest, f0_measured) {
74     uint32_t randInput = std::rand();
75     std::string expect = std::to_string(randInput);
76     std::string actual = std::to_string(~randInput);
77 
78     write("f0_measured", expect);
79 
80     createHwCal();
81 
82     EXPECT_TRUE(mHwCal->getF0(&actual));
83     EXPECT_EQ(expect, actual);
84 }
85 
TEST_F(HwCalTest,f0_missing)86 TEST_F(HwCalTest, f0_missing) {
87     std::string actual;
88 
89     createHwCal();
90 
91     EXPECT_FALSE(mHwCal->getF0(&actual));
92 }
93 
TEST_F(HwCalTest,redc_measured)94 TEST_F(HwCalTest, redc_measured) {
95     uint32_t randInput = std::rand();
96     std::string expect = std::to_string(randInput);
97     std::string actual = std::to_string(~randInput);
98 
99     write("redc_measured", expect);
100 
101     createHwCal();
102 
103     EXPECT_TRUE(mHwCal->getRedc(&actual));
104     EXPECT_EQ(expect, actual);
105 }
106 
TEST_F(HwCalTest,redc_missing)107 TEST_F(HwCalTest, redc_missing) {
108     std::string actual;
109 
110     createHwCal();
111 
112     EXPECT_FALSE(mHwCal->getRedc(&actual));
113 }
114 
TEST_F(HwCalTest,q_measured)115 TEST_F(HwCalTest, q_measured) {
116     uint32_t randInput = std::rand();
117     std::string expect = std::to_string(randInput);
118     std::string actual = std::to_string(~randInput);
119 
120     write("q_measured", expect);
121 
122     createHwCal();
123 
124     EXPECT_TRUE(mHwCal->getQ(&actual));
125     EXPECT_EQ(expect, actual);
126 }
127 
TEST_F(HwCalTest,q_missing)128 TEST_F(HwCalTest, q_missing) {
129     std::string actual;
130 
131     createHwCal();
132 
133     EXPECT_FALSE(mHwCal->getQ(&actual));
134 }
135 
TEST_F(HwCalTest,v_levels)136 TEST_F(HwCalTest, v_levels) {
137     std::array<uint32_t, 2> expect;
138     std::array<uint32_t, 2> actual;
139 
140     // voltage for tick effects
141     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) {
142         e = std::rand();
143         return ~e;
144     });
145 
146     write("v_tick", expect);
147 
148     createHwCal();
149 
150     EXPECT_TRUE(mHwCal->getTickVolLevels(&actual));
151     EXPECT_EQ(expect, actual);
152 
153     // voltage for click effects
154     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) {
155         e = std::rand();
156         return ~e;
157     });
158 
159     write("v_click", expect);
160 
161     createHwCal();
162 
163     EXPECT_TRUE(mHwCal->getClickVolLevels(&actual));
164     EXPECT_EQ(expect, actual);
165 
166     // voltage for long effects
167     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) {
168         e = std::rand();
169         return ~e;
170     });
171 
172     write("v_long", expect);
173 
174     createHwCal();
175 
176     EXPECT_TRUE(mHwCal->getLongVolLevels(&actual));
177     EXPECT_EQ(expect, actual);
178 }
179 
TEST_F(HwCalTest,v_missing)180 TEST_F(HwCalTest, v_missing) {
181     std::array<uint32_t, 2> expect = V_TICK_DEFAULT;
182     std::array<uint32_t, 2> actual;
183 
184     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
185 
186     createHwCal();
187 
188     EXPECT_TRUE(mHwCal->getTickVolLevels(&actual));
189     EXPECT_EQ(expect, actual);
190 
191     expect = V_CLICK_DEFAULT;
192 
193     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
194 
195     createHwCal();
196 
197     EXPECT_TRUE(mHwCal->getClickVolLevels(&actual));
198     EXPECT_EQ(expect, actual);
199 
200     expect = V_LONG_DEFAULT;
201 
202     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
203 
204     createHwCal();
205 
206     EXPECT_TRUE(mHwCal->getLongVolLevels(&actual));
207     EXPECT_EQ(expect, actual);
208 }
209 
TEST_F(HwCalTest,v_short)210 TEST_F(HwCalTest, v_short) {
211     std::array<uint32_t, 2> expect = V_TICK_DEFAULT;
212     std::array<uint32_t, 2> actual;
213 
214     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
215 
216     write("v_tick", std::array<uint32_t, expect.size() - 1>());
217     write("v_click", std::array<uint32_t, expect.size() - 1>());
218     write("v_long", std::array<uint32_t, expect.size() - 1>());
219 
220     createHwCal();
221 
222     EXPECT_TRUE(mHwCal->getTickVolLevels(&actual));
223     EXPECT_EQ(expect, actual);
224 
225     expect = V_CLICK_DEFAULT;
226     EXPECT_TRUE(mHwCal->getClickVolLevels(&actual));
227     EXPECT_EQ(expect, actual);
228 
229     expect = V_LONG_DEFAULT;
230     EXPECT_TRUE(mHwCal->getLongVolLevels(&actual));
231     EXPECT_EQ(expect, actual);
232 }
233 
TEST_F(HwCalTest,v_long)234 TEST_F(HwCalTest, v_long) {
235     std::array<uint32_t, 2> expect = V_TICK_DEFAULT;
236     std::array<uint32_t, 2> actual;
237 
238     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
239 
240     write("v_tick", std::array<uint32_t, expect.size() + 1>());
241     write("v_click", std::array<uint32_t, expect.size() + 1>());
242     write("v_long", std::array<uint32_t, expect.size() + 1>());
243 
244     createHwCal();
245 
246     EXPECT_TRUE(mHwCal->getTickVolLevels(&actual));
247     EXPECT_EQ(expect, actual);
248 
249     expect = V_CLICK_DEFAULT;
250     EXPECT_TRUE(mHwCal->getClickVolLevels(&actual));
251     EXPECT_EQ(expect, actual);
252 
253     expect = V_LONG_DEFAULT;
254     EXPECT_TRUE(mHwCal->getLongVolLevels(&actual));
255     EXPECT_EQ(expect, actual);
256 }
257 
TEST_F(HwCalTest,v_nofile)258 TEST_F(HwCalTest, v_nofile) {
259     std::array<uint32_t, 2> expect = V_TICK_DEFAULT;
260     std::array<uint32_t, 2> actual;
261 
262     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
263 
264     write("v_tick", actual);
265     write("v_click", actual);
266     write("v_long", actual);
267     unlink();
268 
269     createHwCal();
270 
271     EXPECT_TRUE(mHwCal->getTickVolLevels(&actual));
272     EXPECT_EQ(expect, actual);
273 
274     expect = V_CLICK_DEFAULT;
275     EXPECT_TRUE(mHwCal->getClickVolLevels(&actual));
276     EXPECT_EQ(expect, actual);
277 
278     expect = V_LONG_DEFAULT;
279     EXPECT_TRUE(mHwCal->getLongVolLevels(&actual));
280     EXPECT_EQ(expect, actual);
281 }
282 
TEST_F(HwCalTest,multiple)283 TEST_F(HwCalTest, multiple) {
284     uint32_t randInput = std::rand();
285     std::string f0Expect = std::to_string(randInput);
286     std::string f0Actual = std::to_string(~randInput);
287     randInput = std::rand();
288     std::string redcExpect = std::to_string(randInput);
289     std::string redcActual = std::to_string(~randInput);
290     randInput = std::rand();
291     std::string qExpect = std::to_string(randInput);
292     std::string qActual = std::to_string(~randInput);
293     std::array<uint32_t, 2> volTickExpect, volClickExpect, volLongExpect;
294     std::array<uint32_t, 2> volActual;
295 
296     std::transform(volTickExpect.begin(), volTickExpect.end(), volActual.begin(), [](uint32_t &e) {
297         e = std::rand();
298         return ~e;
299     });
300 
301     write("f0_measured", f0Expect);
302     write("redc_measured", redcExpect);
303     write("q_measured", qExpect);
304     write("v_tick", volTickExpect);
305     std::transform(volClickExpect.begin(), volClickExpect.end(), volActual.begin(),
306                    [](uint32_t &e) {
307                        e = std::rand();
308                        return ~e;
309                    });
310     write("v_click", volClickExpect);
311     std::transform(volLongExpect.begin(), volLongExpect.end(), volActual.begin(), [](uint32_t &e) {
312         e = std::rand();
313         return ~e;
314     });
315     write("v_long", volLongExpect);
316 
317     createHwCal();
318 
319     EXPECT_TRUE(mHwCal->getF0(&f0Actual));
320     EXPECT_EQ(f0Expect, f0Actual);
321     EXPECT_TRUE(mHwCal->getRedc(&redcActual));
322     EXPECT_EQ(redcExpect, redcActual);
323     EXPECT_TRUE(mHwCal->getQ(&qActual));
324     EXPECT_EQ(qExpect, qActual);
325     EXPECT_TRUE(mHwCal->getTickVolLevels(&volActual));
326     EXPECT_EQ(volTickExpect, volActual);
327     EXPECT_TRUE(mHwCal->getClickVolLevels(&volActual));
328     EXPECT_EQ(volClickExpect, volActual);
329     EXPECT_TRUE(mHwCal->getLongVolLevels(&volActual));
330     EXPECT_EQ(volLongExpect, volActual);
331 }
332 
TEST_F(HwCalTest,trimming)333 TEST_F(HwCalTest, trimming) {
334     uint32_t randInput = std::rand();
335     std::string f0Expect = std::to_string(randInput);
336     std::string f0Actual = std::to_string(~randInput);
337     randInput = std::rand();
338     std::string redcExpect = std::to_string(randInput);
339     std::string redcActual = std::to_string(randInput);
340     randInput = std::rand();
341     std::string qExpect = std::to_string(randInput);
342     std::string qActual = std::to_string(randInput);
343     std::array<uint32_t, 2> volTickExpect, volClickExpect, volLongExpect;
344     std::array<uint32_t, 2> volActual;
345 
346     std::transform(volTickExpect.begin(), volTickExpect.end(), volActual.begin(), [](uint32_t &e) {
347         e = std::rand();
348         return ~e;
349     });
350 
351     write("f0_measured", f0Expect, " \t", "\t ");
352     write("redc_measured", redcExpect, " \t", "\t ");
353     write("q_measured", qExpect, " \t", "\t ");
354     write("v_tick", volTickExpect, " \t", "\t ");
355     std::transform(volClickExpect.begin(), volClickExpect.end(), volActual.begin(),
356                    [](uint32_t &e) {
357                        e = std::rand();
358                        return ~e;
359                    });
360     write("v_click", volClickExpect, " \t", "\t ");
361     std::transform(volLongExpect.begin(), volLongExpect.end(), volActual.begin(), [](uint32_t &e) {
362         e = std::rand();
363         return ~e;
364     });
365     write("v_long", volLongExpect, " \t", "\t ");
366 
367     createHwCal();
368 
369     EXPECT_TRUE(mHwCal->getF0(&f0Actual));
370     EXPECT_EQ(f0Expect, f0Actual);
371     EXPECT_TRUE(mHwCal->getRedc(&redcActual));
372     EXPECT_EQ(redcExpect, redcActual);
373     EXPECT_TRUE(mHwCal->getQ(&qActual));
374     EXPECT_EQ(qExpect, qActual);
375     EXPECT_TRUE(mHwCal->getTickVolLevels(&volActual));
376     EXPECT_EQ(volTickExpect, volActual);
377     EXPECT_TRUE(mHwCal->getClickVolLevels(&volActual));
378     EXPECT_EQ(volClickExpect, volActual);
379     EXPECT_TRUE(mHwCal->getLongVolLevels(&volActual));
380     EXPECT_EQ(volLongExpect, volActual);
381 }
382 
383 }  // namespace vibrator
384 }  // namespace hardware
385 }  // namespace android
386 }  // namespace aidl
387