test-hwcal.cpp 11 KB

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