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 <cutils/fs.h>
19 #include <gtest/gtest.h>
20
21 #include <cstdlib>
22 #include <fstream>
23
24 #include "Hardware.h"
25
26 namespace aidl {
27 namespace android {
28 namespace hardware {
29 namespace vibrator {
30
31 using ::testing::Test;
32 using ::testing::TestParamInfo;
33 using ::testing::ValuesIn;
34 using ::testing::WithParamInterface;
35
36 class HwApiTest : public Test {
37 protected:
38 static constexpr const char *FILE_NAMES[]{
39 "device/autocal",
40 "device/ol_lra_period",
41 "activate",
42 "duration",
43 "state",
44 "device/rtp_input",
45 "device/mode",
46 "device/set_sequencer",
47 "device/scale",
48 "device/ctrl_loop",
49 "device/lp_trigger_effect",
50 "device/lra_wave_shape",
51 "device/od_clamp",
52 };
53
54 static constexpr const char *REQUIRED[]{
55 "activate",
56 "duration",
57 "state",
58 };
59
60 public:
SetUp()61 void SetUp() override {
62 std::string prefix;
63 for (auto n : FILE_NAMES) {
64 auto name = std::filesystem::path(n);
65 auto path = std::filesystem::path(mFilesDir.path) / name;
66 fs_mkdirs(path.c_str(), S_IRWXU);
67 std::ofstream touch{path};
68 mFileMap[name] = path;
69 }
70 prefix = std::filesystem::path(mFilesDir.path) / "";
71 setenv("HWAPI_PATH_PREFIX", prefix.c_str(), true);
72 mHwApi = HwApi::Create();
73
74 for (auto n : REQUIRED) {
75 auto name = std::filesystem::path(n);
76 auto path = std::filesystem::path(mEmptyDir.path) / name;
77 fs_mkdirs(path.c_str(), S_IRWXU);
78 std::ofstream touch{path};
79 }
80 prefix = std::filesystem::path(mEmptyDir.path) / "";
81 setenv("HWAPI_PATH_PREFIX", prefix.c_str(), true);
82 mNoApi = HwApi::Create();
83 }
84
TearDown()85 void TearDown() override { verifyContents(); }
86
87 protected:
88 // Set expected file content for a test.
89 template <typename T>
expectContent(const std::string & name,const T & value)90 void expectContent(const std::string &name, const T &value) {
91 mExpectedContent[name] << value << std::endl;
92 }
93
94 // Set actual file content for an input test.
95 template <typename T>
updateContent(const std::string & name,const T & value)96 void updateContent(const std::string &name, const T &value) {
97 std::ofstream(mFileMap[name]) << value << std::endl;
98 }
99
100 template <typename T>
expectAndUpdateContent(const std::string & name,const T & value)101 void expectAndUpdateContent(const std::string &name, const T &value) {
102 expectContent(name, value);
103 updateContent(name, value);
104 }
105
106 // Compare all file contents against expected contents.
verifyContents()107 void verifyContents() {
108 for (auto &a : mFileMap) {
109 std::ifstream file{a.second};
110 std::string expect = mExpectedContent[a.first].str();
111 std::string actual = std::string(std::istreambuf_iterator<char>(file),
112 std::istreambuf_iterator<char>());
113 EXPECT_EQ(expect, actual) << a.first;
114 }
115 }
116
117 // TODO(eliptus): Determine how to induce errors in required files
isRequired(const std::string & name)118 static bool isRequired(const std::string &name) {
119 for (auto n : REQUIRED) {
120 if (std::string(n) == name) {
121 return true;
122 }
123 }
124 return false;
125 }
126
ParamNameFixup(std::string str)127 static auto ParamNameFixup(std::string str) {
128 std::replace(str.begin(), str.end(), '/', '_');
129 return str;
130 }
131
132 protected:
133 std::unique_ptr<Vibrator::HwApi> mHwApi;
134 std::unique_ptr<Vibrator::HwApi> mNoApi;
135 std::map<std::string, std::string> mFileMap;
136 TemporaryDir mFilesDir;
137 TemporaryDir mEmptyDir;
138 std::map<std::string, std::stringstream> mExpectedContent;
139 };
140
141 class CreateTest : public HwApiTest, public WithParamInterface<const char *> {
142 public:
SetUp()143 void SetUp() override{};
TearDown()144 void TearDown() override{};
145
PrintParam(const TestParamInfo<CreateTest::ParamType> & info)146 static auto PrintParam(const TestParamInfo<CreateTest::ParamType> &info) {
147 return ParamNameFixup(info.param);
148 }
AllParams()149 static auto &AllParams() { return FILE_NAMES; }
150 };
151
TEST_P(CreateTest,file_missing)152 TEST_P(CreateTest, file_missing) {
153 auto skip = std::string(GetParam());
154 TemporaryDir dir;
155 std::unique_ptr<HwApi> hwapi;
156 std::string prefix;
157
158 for (auto n : FILE_NAMES) {
159 auto name = std::string(n);
160 auto path = std::string(dir.path) + "/" + name;
161 if (name == skip) {
162 continue;
163 }
164 fs_mkdirs(path.c_str(), S_IRWXU);
165 std::ofstream touch{path};
166 }
167
168 prefix = std::filesystem::path(dir.path) / "";
169 setenv("HWAPI_PATH_PREFIX", prefix.c_str(), true);
170 hwapi = HwApi::Create();
171 if (isRequired(skip)) {
172 EXPECT_EQ(nullptr, hwapi);
173 } else {
174 EXPECT_NE(nullptr, hwapi);
175 }
176 }
177
178 INSTANTIATE_TEST_CASE_P(HwApiTests, CreateTest, ValuesIn(CreateTest::AllParams()),
179 CreateTest::PrintParam);
180
181 template <typename T>
182 class HwApiTypedTest : public HwApiTest,
183 public WithParamInterface<std::tuple<std::string, std::function<T>>> {
184 public:
PrintParam(const TestParamInfo<typename HwApiTypedTest::ParamType> & info)185 static auto PrintParam(const TestParamInfo<typename HwApiTypedTest::ParamType> &info) {
186 return ParamNameFixup(std::get<0>(info.param));
187 }
MakeParam(std::string name,std::function<T> func)188 static auto MakeParam(std::string name, std::function<T> func) {
189 return std::make_tuple(name, func);
190 }
191 };
192
193 using HasTest = HwApiTypedTest<bool(Vibrator::HwApi &)>;
194
TEST_P(HasTest,success_returnsTrue)195 TEST_P(HasTest, success_returnsTrue) {
196 auto param = GetParam();
197 auto func = std::get<1>(param);
198
199 EXPECT_TRUE(func(*mHwApi));
200 }
201
TEST_P(HasTest,success_returnsFalse)202 TEST_P(HasTest, success_returnsFalse) {
203 auto param = GetParam();
204 auto func = std::get<1>(param);
205
206 EXPECT_FALSE(func(*mNoApi));
207 }
208
209 INSTANTIATE_TEST_CASE_P(HwApiTests, HasTest,
210 ValuesIn({
211 HasTest::MakeParam("device/rtp_input",
212 &Vibrator::HwApi::hasRtpInput),
213 }),
214 HasTest::PrintParam);
215
216 using SetBoolTest = HwApiTypedTest<bool(Vibrator::HwApi &, bool)>;
217
TEST_P(SetBoolTest,success_returnsTrue)218 TEST_P(SetBoolTest, success_returnsTrue) {
219 auto param = GetParam();
220 auto name = std::get<0>(param);
221 auto func = std::get<1>(param);
222
223 expectContent(name, "1");
224
225 EXPECT_TRUE(func(*mHwApi, true));
226 }
227
TEST_P(SetBoolTest,success_returnsFalse)228 TEST_P(SetBoolTest, success_returnsFalse) {
229 auto param = GetParam();
230 auto name = std::get<0>(param);
231 auto func = std::get<1>(param);
232
233 expectContent(name, "0");
234
235 EXPECT_TRUE(func(*mHwApi, false));
236 }
237
TEST_P(SetBoolTest,failure)238 TEST_P(SetBoolTest, failure) {
239 auto param = GetParam();
240 auto name = std::get<0>(param);
241 auto func = std::get<1>(param);
242
243 if (isRequired(name)) {
244 GTEST_SKIP();
245 }
246
247 EXPECT_FALSE(func(*mNoApi, true));
248 EXPECT_FALSE(func(*mNoApi, false));
249 }
250
251 INSTANTIATE_TEST_CASE_P(HwApiTests, SetBoolTest,
252 ValuesIn({
253 SetBoolTest::MakeParam("activate", &Vibrator::HwApi::setActivate),
254 SetBoolTest::MakeParam("state", &Vibrator::HwApi::setState),
255 SetBoolTest::MakeParam("device/ctrl_loop",
256 &Vibrator::HwApi::setCtrlLoop),
257 }),
258 SetBoolTest::PrintParam);
259
260 using SetInt8Test = HwApiTypedTest<bool(Vibrator::HwApi &, int8_t)>;
261
TEST_P(SetInt8Test,success)262 TEST_P(SetInt8Test, success) {
263 auto param = GetParam();
264 auto name = std::get<0>(param);
265 auto func = std::get<1>(param);
266 int8_t value = std::rand();
267
268 expectContent(name, +value);
269
270 EXPECT_TRUE(func(*mHwApi, value));
271 }
272
TEST_P(SetInt8Test,failure)273 TEST_P(SetInt8Test, failure) {
274 auto param = GetParam();
275 auto name = std::get<0>(param);
276 auto func = std::get<1>(param);
277 int8_t value = std::rand();
278
279 if (isRequired(name)) {
280 GTEST_SKIP();
281 }
282
283 EXPECT_FALSE(func(*mNoApi, value));
284 }
285
286 INSTANTIATE_TEST_CASE_P(HwApiTests, SetInt8Test,
287 ValuesIn({
288 SetInt8Test::MakeParam("device/rtp_input",
289 &Vibrator::HwApi::setRtpInput),
290 }),
291 SetInt8Test::PrintParam);
292
293 using SetUint8Test = HwApiTypedTest<bool(Vibrator::HwApi &, uint8_t)>;
294
TEST_P(SetUint8Test,success)295 TEST_P(SetUint8Test, success) {
296 auto param = GetParam();
297 auto name = std::get<0>(param);
298 auto func = std::get<1>(param);
299 uint8_t value = std::rand();
300
301 expectContent(name, +value);
302
303 EXPECT_TRUE(func(*mHwApi, value));
304 }
305
TEST_P(SetUint8Test,failure)306 TEST_P(SetUint8Test, failure) {
307 auto param = GetParam();
308 auto name = std::get<0>(param);
309 auto func = std::get<1>(param);
310 uint8_t value = std::rand();
311
312 if (isRequired(name)) {
313 GTEST_SKIP();
314 }
315
316 EXPECT_FALSE(func(*mNoApi, value));
317 }
318
319 INSTANTIATE_TEST_CASE_P(HwApiTests, SetUint8Test,
320 ValuesIn({
321 SetUint8Test::MakeParam("device/scale", &Vibrator::HwApi::setScale),
322 }),
323 SetUint8Test::PrintParam);
324
325 using SetUint32Test = HwApiTypedTest<bool(Vibrator::HwApi &, uint32_t)>;
326
TEST_P(SetUint32Test,success)327 TEST_P(SetUint32Test, success) {
328 auto param = GetParam();
329 auto name = std::get<0>(param);
330 auto func = std::get<1>(param);
331 uint32_t value = std::rand();
332
333 expectContent(name, value);
334
335 EXPECT_TRUE(func(*mHwApi, value));
336 }
337
TEST_P(SetUint32Test,failure)338 TEST_P(SetUint32Test, failure) {
339 auto param = GetParam();
340 auto name = std::get<0>(param);
341 auto func = std::get<1>(param);
342 uint32_t value = std::rand();
343
344 if (isRequired(name)) {
345 GTEST_SKIP();
346 }
347
348 EXPECT_FALSE(func(*mNoApi, value));
349 }
350
351 INSTANTIATE_TEST_CASE_P(
352 HwApiTests, SetUint32Test,
353 ValuesIn({
354 SetUint32Test::MakeParam("device/ol_lra_period", &Vibrator::HwApi::setOlLraPeriod),
355 SetUint32Test::MakeParam("duration", &Vibrator::HwApi::setDuration),
356 SetUint32Test::MakeParam("device/lp_trigger_effect",
357 &Vibrator::HwApi::setLpTriggerEffect),
358 SetUint32Test::MakeParam("device/lra_wave_shape",
359 &Vibrator::HwApi::setLraWaveShape),
360 SetUint32Test::MakeParam("device/od_clamp", &Vibrator::HwApi::setOdClamp),
361 }),
362 SetUint32Test::PrintParam);
363
364 using SetStringTest = HwApiTypedTest<bool(Vibrator::HwApi &, std::string)>;
365
TEST_P(SetStringTest,success)366 TEST_P(SetStringTest, success) {
367 auto param = GetParam();
368 auto name = std::get<0>(param);
369 auto func = std::get<1>(param);
370 std::string value = TemporaryFile().path;
371
372 expectContent(name, value);
373
374 EXPECT_TRUE(func(*mHwApi, value));
375 }
376
TEST_P(SetStringTest,failure)377 TEST_P(SetStringTest, failure) {
378 auto param = GetParam();
379 auto name = std::get<0>(param);
380 auto func = std::get<1>(param);
381 std::string value = TemporaryFile().path;
382
383 if (isRequired(name)) {
384 GTEST_SKIP();
385 }
386
387 EXPECT_FALSE(func(*mNoApi, value));
388 }
389
390 INSTANTIATE_TEST_CASE_P(
391 HwApiTests, SetStringTest,
392 ValuesIn({
393 SetStringTest::MakeParam("device/autocal", &Vibrator::HwApi::setAutocal),
394 SetStringTest::MakeParam("device/mode", &Vibrator::HwApi::setMode),
395 SetStringTest::MakeParam("device/set_sequencer", &Vibrator::HwApi::setSequencer),
396 }),
397 SetStringTest::PrintParam);
398
399 } // namespace vibrator
400 } // namespace hardware
401 } // namespace android
402 } // namespace aidl
403