test-hwapi.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 <cutils/fs.h>
  18. #include <gtest/gtest.h>
  19. #include <cstdlib>
  20. #include <fstream>
  21. #include "Hardware.h"
  22. namespace aidl {
  23. namespace android {
  24. namespace hardware {
  25. namespace vibrator {
  26. using ::testing::Test;
  27. using ::testing::TestParamInfo;
  28. using ::testing::ValuesIn;
  29. using ::testing::WithParamInterface;
  30. class HwApiTest : public Test {
  31. private:
  32. static constexpr const char *FILE_NAMES[]{
  33. "calibration/f0_stored",
  34. "default/f0_offset",
  35. "calibration/redc_stored",
  36. "calibration/q_stored",
  37. "default/f0_comp_enable",
  38. "default/redc_comp_enable",
  39. "default/owt_free_space",
  40. "default/num_waves",
  41. "default/delay_before_stop_playback_us",
  42. };
  43. public:
  44. void SetUp() override {
  45. std::string prefix;
  46. for (auto n : FILE_NAMES) {
  47. auto name = std::filesystem::path(n);
  48. auto path = std::filesystem::path(mFilesDir.path) / name;
  49. fs_mkdirs(path.c_str(), S_IRWXU);
  50. std::ofstream touch{path};
  51. mFileMap[name] = path;
  52. }
  53. prefix = std::filesystem::path(mFilesDir.path) / "";
  54. setenv("HWAPI_PATH_PREFIX", prefix.c_str(), true);
  55. mHwApi = std::make_unique<HwApi>();
  56. for (auto n : FILE_NAMES) {
  57. auto name = std::filesystem::path(n);
  58. auto path = std::filesystem::path(mEmptyDir.path) / name;
  59. }
  60. prefix = std::filesystem::path(mEmptyDir.path) / "";
  61. setenv("HWAPI_PATH_PREFIX", prefix.c_str(), true);
  62. mNoApi = std::make_unique<HwApi>();
  63. }
  64. void TearDown() override { verifyContents(); }
  65. static auto ParamNameFixup(std::string str) {
  66. std::replace(str.begin(), str.end(), '/', '_');
  67. return str;
  68. }
  69. protected:
  70. // Set expected file content for a test.
  71. template <typename T>
  72. void expectContent(const std::string &name, const T &value) {
  73. mExpectedContent[name] << value << std::endl;
  74. }
  75. // Set actual file content for an input test.
  76. template <typename T>
  77. void updateContent(const std::string &name, const T &value) {
  78. std::ofstream(mFileMap[name]) << value << std::endl;
  79. }
  80. template <typename T>
  81. void expectAndUpdateContent(const std::string &name, const T &value) {
  82. expectContent(name, value);
  83. updateContent(name, value);
  84. }
  85. // Compare all file contents against expected contents.
  86. void verifyContents() {
  87. for (auto &a : mFileMap) {
  88. std::ifstream file{a.second};
  89. std::string expect = mExpectedContent[a.first].str();
  90. std::string actual = std::string(std::istreambuf_iterator<char>(file),
  91. std::istreambuf_iterator<char>());
  92. EXPECT_EQ(expect, actual) << a.first;
  93. }
  94. }
  95. protected:
  96. std::unique_ptr<Vibrator::HwApi> mHwApi;
  97. std::unique_ptr<Vibrator::HwApi> mNoApi;
  98. std::map<std::string, std::string> mFileMap;
  99. TemporaryDir mFilesDir;
  100. TemporaryDir mEmptyDir;
  101. std::map<std::string, std::stringstream> mExpectedContent;
  102. };
  103. template <typename T>
  104. class HwApiTypedTest : public HwApiTest,
  105. public WithParamInterface<std::tuple<std::string, std::function<T>>> {
  106. public:
  107. static auto PrintParam(const TestParamInfo<typename HwApiTypedTest::ParamType> &info) {
  108. return ParamNameFixup(std::get<0>(info.param));
  109. }
  110. static auto MakeParam(std::string name, std::function<T> func) {
  111. return std::make_tuple(name, func);
  112. }
  113. };
  114. using HasTest = HwApiTypedTest<bool(Vibrator::HwApi &)>;
  115. TEST_P(HasTest, success_returnsTrue) {
  116. auto param = GetParam();
  117. auto func = std::get<1>(param);
  118. EXPECT_TRUE(func(*mHwApi));
  119. }
  120. TEST_P(HasTest, success_returnsFalse) {
  121. auto param = GetParam();
  122. auto func = std::get<1>(param);
  123. EXPECT_FALSE(func(*mNoApi));
  124. }
  125. INSTANTIATE_TEST_CASE_P(HwApiTests, HasTest,
  126. ValuesIn({
  127. HasTest::MakeParam("default/owt_free_space",
  128. &Vibrator::HwApi::hasOwtFreeSpace),
  129. }),
  130. HasTest::PrintParam);
  131. using GetUint32Test = HwApiTypedTest<bool(Vibrator::HwApi &, uint32_t *)>;
  132. TEST_P(GetUint32Test, success) {
  133. auto param = GetParam();
  134. auto name = std::get<0>(param);
  135. auto func = std::get<1>(param);
  136. uint32_t expect = std::rand();
  137. uint32_t actual = ~expect;
  138. expectAndUpdateContent(name, expect);
  139. EXPECT_TRUE(func(*mHwApi, &actual));
  140. EXPECT_EQ(expect, actual);
  141. }
  142. TEST_P(GetUint32Test, failure) {
  143. auto param = GetParam();
  144. auto func = std::get<1>(param);
  145. uint32_t value;
  146. EXPECT_FALSE(func(*mNoApi, &value));
  147. }
  148. INSTANTIATE_TEST_CASE_P(HwApiTests, GetUint32Test,
  149. ValuesIn({
  150. GetUint32Test::MakeParam("default/num_waves",
  151. &Vibrator::HwApi::getEffectCount),
  152. GetUint32Test::MakeParam("default/owt_free_space",
  153. &Vibrator::HwApi::getOwtFreeSpace),
  154. }),
  155. GetUint32Test::PrintParam);
  156. using SetBoolTest = HwApiTypedTest<bool(Vibrator::HwApi &, bool)>;
  157. TEST_P(SetBoolTest, success_returnsTrue) {
  158. auto param = GetParam();
  159. auto name = std::get<0>(param);
  160. auto func = std::get<1>(param);
  161. expectContent(name, "1");
  162. EXPECT_TRUE(func(*mHwApi, true));
  163. }
  164. TEST_P(SetBoolTest, success_returnsFalse) {
  165. auto param = GetParam();
  166. auto name = std::get<0>(param);
  167. auto func = std::get<1>(param);
  168. expectContent(name, "0");
  169. EXPECT_TRUE(func(*mHwApi, false));
  170. }
  171. TEST_P(SetBoolTest, failure) {
  172. auto param = GetParam();
  173. auto func = std::get<1>(param);
  174. EXPECT_FALSE(func(*mNoApi, true));
  175. EXPECT_FALSE(func(*mNoApi, false));
  176. }
  177. INSTANTIATE_TEST_CASE_P(HwApiTests, SetBoolTest,
  178. ValuesIn({
  179. SetBoolTest::MakeParam("default/f0_comp_enable",
  180. &Vibrator::HwApi::setF0CompEnable),
  181. SetBoolTest::MakeParam("default/redc_comp_enable",
  182. &Vibrator::HwApi::setRedcCompEnable),
  183. }),
  184. SetBoolTest::PrintParam);
  185. using SetUint32Test = HwApiTypedTest<bool(Vibrator::HwApi &, uint32_t)>;
  186. TEST_P(SetUint32Test, success) {
  187. auto param = GetParam();
  188. auto name = std::get<0>(param);
  189. auto func = std::get<1>(param);
  190. uint32_t value = std::rand();
  191. expectContent(name, value);
  192. EXPECT_TRUE(func(*mHwApi, value));
  193. }
  194. TEST_P(SetUint32Test, failure) {
  195. auto param = GetParam();
  196. auto func = std::get<1>(param);
  197. uint32_t value = std::rand();
  198. EXPECT_FALSE(func(*mNoApi, value));
  199. }
  200. INSTANTIATE_TEST_CASE_P(HwApiTests, SetUint32Test,
  201. ValuesIn({
  202. SetUint32Test::MakeParam("default/f0_offset",
  203. &Vibrator::HwApi::setF0Offset),
  204. SetUint32Test::MakeParam("default/delay_before_stop_playback_us",
  205. &Vibrator::HwApi::setMinOnOffInterval),
  206. }),
  207. SetUint32Test::PrintParam);
  208. using SetStringTest = HwApiTypedTest<bool(Vibrator::HwApi &, std::string)>;
  209. TEST_P(SetStringTest, success) {
  210. auto param = GetParam();
  211. auto name = std::get<0>(param);
  212. auto func = std::get<1>(param);
  213. std::string value = TemporaryFile().path;
  214. expectContent(name, value);
  215. EXPECT_TRUE(func(*mHwApi, value));
  216. }
  217. TEST_P(SetStringTest, failure) {
  218. auto param = GetParam();
  219. auto func = std::get<1>(param);
  220. std::string value = TemporaryFile().path;
  221. EXPECT_FALSE(func(*mNoApi, value));
  222. }
  223. INSTANTIATE_TEST_CASE_P(
  224. HwApiTests, SetStringTest,
  225. ValuesIn({
  226. SetStringTest::MakeParam("calibration/f0_stored", &Vibrator::HwApi::setF0),
  227. SetStringTest::MakeParam("calibration/redc_stored", &Vibrator::HwApi::setRedc),
  228. SetStringTest::MakeParam("calibration/q_stored", &Vibrator::HwApi::setQ),
  229. }),
  230. SetStringTest::PrintParam);
  231. } // namespace vibrator
  232. } // namespace hardware
  233. } // namespace android
  234. } // namespace aidl