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 #define LOG_TAG "PtsVibratorHalFloralTestSuite"
17 
18 #include <android-base/file.h>
19 #include <gtest/gtest.h>
20 
21 #include <fstream>
22 
23 #include "Hardware.h"
24 
25 using namespace ::testing;
26 
27 namespace android {
28 namespace hardware {
29 namespace vibrator {
30 namespace V1_3 {
31 namespace implementation {
32 
33 class HwCalTest : public Test {
34   protected:
35     static constexpr uint32_t Q_DEFAULT = 15.5f * (1 << 16);
36     static constexpr std::array<uint32_t, 6> V_DEFAULT = {60, 70, 80, 90, 100, 76};
37 
38   public:
SetUp()39     void SetUp() override { setenv("CALIBRATION_FILEPATH", mCalFile.path, true); }
40 
41   private:
pack(std::ostream & stream,const uint32_t & value,std::string lpad,std::string rpad)42     static void pack(std::ostream &stream, const uint32_t &value, std::string lpad,
43                      std::string rpad) {
44         stream << lpad << value << rpad;
45     }
46 
47     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)48     static void pack(std::ostream &stream, const std::array<T, N> &value, std::string lpad,
49                      std::string rpad) {
50         for (auto &entry : value) {
51             pack(stream, entry, lpad, rpad);
52         }
53     }
54 
55   protected:
createHwCal()56     void createHwCal() { mHwCal = std::make_unique<HwCal>(); }
57 
58     template <typename T>
write(const std::string key,const T & value,std::string lpad=" ",std::string rpad="")59     void write(const std::string key, const T &value, std::string lpad = " ",
60                std::string rpad = "") {
61         std::ofstream calfile{mCalFile.path, std::ios_base::app};
62         calfile << key << ":";
63         pack(calfile, value, lpad, rpad);
64         calfile << std::endl;
65     }
66 
unlink()67     void unlink() { ::unlink(mCalFile.path); }
68 
69   protected:
70     std::unique_ptr<Vibrator::HwCal> mHwCal;
71     TemporaryFile mCalFile;
72 };
73 
TEST_F(HwCalTest,f0_measured)74 TEST_F(HwCalTest, f0_measured) {
75     uint32_t expect = std::rand();
76     uint32_t actual = ~expect;
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     uint32_t 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 expect = std::rand();
96     uint32_t actual = ~expect;
97 
98     write("redc_measured", expect);
99 
100     createHwCal();
101 
102     EXPECT_TRUE(mHwCal->getRedc(&actual));
103     EXPECT_EQ(expect, actual);
104 }
105 
TEST_F(HwCalTest,redc_missing)106 TEST_F(HwCalTest, redc_missing) {
107     uint32_t actual;
108 
109     createHwCal();
110 
111     EXPECT_FALSE(mHwCal->getRedc(&actual));
112 }
113 
TEST_F(HwCalTest,q_measured)114 TEST_F(HwCalTest, q_measured) {
115     uint32_t expect = std::rand();
116     uint32_t actual = ~expect;
117 
118     write("q_measured", expect);
119 
120     createHwCal();
121 
122     EXPECT_TRUE(mHwCal->getQ(&actual));
123     EXPECT_EQ(expect, actual);
124 }
125 
TEST_F(HwCalTest,q_index)126 TEST_F(HwCalTest, q_index) {
127     uint8_t value = std::rand();
128     uint32_t expect = value * 1.5f * (1 << 16) + 2.0f * (1 << 16);
129     uint32_t actual = ~expect;
130 
131     write("q_index", value);
132 
133     createHwCal();
134 
135     EXPECT_TRUE(mHwCal->getQ(&actual));
136     EXPECT_EQ(expect, actual);
137 }
138 
TEST_F(HwCalTest,q_missing)139 TEST_F(HwCalTest, q_missing) {
140     uint32_t expect = Q_DEFAULT;
141     uint32_t actual = ~expect;
142 
143     createHwCal();
144 
145     EXPECT_TRUE(mHwCal->getQ(&actual));
146     EXPECT_EQ(expect, actual);
147 }
148 
TEST_F(HwCalTest,q_nofile)149 TEST_F(HwCalTest, q_nofile) {
150     uint32_t expect = Q_DEFAULT;
151     uint32_t actual = ~expect;
152 
153     write("q_measured", actual);
154     unlink();
155 
156     createHwCal();
157 
158     EXPECT_TRUE(mHwCal->getQ(&actual));
159     EXPECT_EQ(expect, actual);
160 }
161 
TEST_F(HwCalTest,v_levels)162 TEST_F(HwCalTest, v_levels) {
163     std::array<uint32_t, 6> expect;
164     std::array<uint32_t, 6> actual;
165 
166     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) {
167         e = std::rand();
168         return ~e;
169     });
170 
171     write("v_levels", expect);
172 
173     createHwCal();
174 
175     EXPECT_TRUE(mHwCal->getVolLevels(&actual));
176     EXPECT_EQ(expect, actual);
177 }
178 
TEST_F(HwCalTest,v_missing)179 TEST_F(HwCalTest, v_missing) {
180     std::array<uint32_t, 6> expect = V_DEFAULT;
181     std::array<uint32_t, 6> actual;
182 
183     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
184 
185     createHwCal();
186 
187     EXPECT_TRUE(mHwCal->getVolLevels(&actual));
188     EXPECT_EQ(expect, actual);
189 }
190 
TEST_F(HwCalTest,v_short)191 TEST_F(HwCalTest, v_short) {
192     std::array<uint32_t, 6> expect = V_DEFAULT;
193     std::array<uint32_t, 6> actual;
194 
195     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
196 
197     write("v_levels", std::array<uint32_t, expect.size() - 1>());
198 
199     createHwCal();
200 
201     EXPECT_TRUE(mHwCal->getVolLevels(&actual));
202     EXPECT_EQ(expect, actual);
203 }
204 
TEST_F(HwCalTest,v_long)205 TEST_F(HwCalTest, v_long) {
206     std::array<uint32_t, 6> expect = V_DEFAULT;
207     std::array<uint32_t, 6> actual;
208 
209     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
210 
211     write("v_levels", std::array<uint32_t, expect.size() + 1>());
212 
213     createHwCal();
214 
215     EXPECT_TRUE(mHwCal->getVolLevels(&actual));
216     EXPECT_EQ(expect, actual);
217 }
218 
TEST_F(HwCalTest,v_nofile)219 TEST_F(HwCalTest, v_nofile) {
220     std::array<uint32_t, 6> expect = V_DEFAULT;
221     std::array<uint32_t, 6> actual;
222 
223     std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
224 
225     write("v_levels", actual);
226     unlink();
227 
228     createHwCal();
229 
230     EXPECT_TRUE(mHwCal->getVolLevels(&actual));
231     EXPECT_EQ(expect, actual);
232 }
233 
TEST_F(HwCalTest,multiple)234 TEST_F(HwCalTest, multiple) {
235     uint32_t f0Expect = std::rand();
236     uint32_t f0Actual = ~f0Expect;
237     uint32_t redcExpect = std::rand();
238     uint32_t redcActual = ~redcExpect;
239     uint32_t qExpect = std::rand();
240     uint32_t qActual = ~qExpect;
241     std::array<uint32_t, 6> volExpect;
242     std::array<uint32_t, 6> volActual;
243 
244     std::transform(volExpect.begin(), volExpect.end(), volActual.begin(), [](uint32_t &e) {
245         e = std::rand();
246         return ~e;
247     });
248 
249     write("f0_measured", f0Expect);
250     write("redc_measured", redcExpect);
251     write("q_measured", qExpect);
252     write("v_levels", volExpect);
253 
254     createHwCal();
255 
256     EXPECT_TRUE(mHwCal->getF0(&f0Actual));
257     EXPECT_EQ(f0Expect, f0Actual);
258     EXPECT_TRUE(mHwCal->getRedc(&redcActual));
259     EXPECT_EQ(redcExpect, redcActual);
260     EXPECT_TRUE(mHwCal->getQ(&qActual));
261     EXPECT_EQ(qExpect, qActual);
262     EXPECT_TRUE(mHwCal->getVolLevels(&volActual));
263     EXPECT_EQ(volExpect, volActual);
264 }
265 
TEST_F(HwCalTest,trimming)266 TEST_F(HwCalTest, trimming) {
267     uint32_t f0Expect = std::rand();
268     uint32_t f0Actual = ~f0Expect;
269     uint32_t redcExpect = std::rand();
270     uint32_t redcActual = ~redcExpect;
271     uint32_t qExpect = std::rand();
272     uint32_t qActual = ~qExpect;
273     std::array<uint32_t, 6> volExpect;
274     std::array<uint32_t, 6> volActual;
275 
276     std::transform(volExpect.begin(), volExpect.end(), volActual.begin(), [](uint32_t &e) {
277         e = std::rand();
278         return ~e;
279     });
280 
281     write("f0_measured", f0Expect, " \t", "\t ");
282     write("redc_measured", redcExpect, " \t", "\t ");
283     write("q_measured", qExpect, " \t", "\t ");
284     write("v_levels", volExpect, " \t", "\t ");
285 
286     createHwCal();
287 
288     EXPECT_TRUE(mHwCal->getF0(&f0Actual));
289     EXPECT_EQ(f0Expect, f0Actual);
290     EXPECT_TRUE(mHwCal->getRedc(&redcActual));
291     EXPECT_EQ(redcExpect, redcActual);
292     EXPECT_TRUE(mHwCal->getQ(&qActual));
293     EXPECT_EQ(qExpect, qActual);
294     EXPECT_TRUE(mHwCal->getVolLevels(&volActual));
295     EXPECT_EQ(volExpect, volActual);
296 }
297 
298 }  // namespace implementation
299 }  // namespace V1_3
300 }  // namespace vibrator
301 }  // namespace hardware
302 }  // namespace android
303