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