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