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 android {
27 namespace hardware {
28 namespace vibrator {
29 namespace V1_3 {
30 namespace implementation {
31
32 using ::testing::Test;
33 using ::testing::TestParamInfo;
34 using ::testing::ValuesIn;
35 using ::testing::WithParamInterface;
36
37 class HwApiTest : public Test {
38 protected:
39 static constexpr const char *FILE_NAMES[]{
40 "device/autocal",
41 "device/ol_lra_period",
42 "activate",
43 "duration",
44 "state",
45 "device/rtp_input",
46 "device/mode",
47 "device/set_sequencer",
48 "device/scale",
49 "device/ctrl_loop",
50 "device/lp_trigger_effect",
51 "device/lra_wave_shape",
52 "device/od_clamp",
53 };
54
55 static constexpr const char *REQUIRED[]{
56 "activate",
57 "duration",
58 "state",
59 };
60
61 public:
SetUp()62 void SetUp() override {
63 std::string prefix;
64 for (auto n : FILE_NAMES) {
65 auto name = std::filesystem::path(n);
66 auto path = std::filesystem::path(mFilesDir.path) / name;
67 fs_mkdirs(path.c_str(), S_IRWXU);
68 std::ofstream touch{path};
69 mFileMap[name] = path;
70 }
71 prefix = std::filesystem::path(mFilesDir.path) / "";
72 setenv("HWAPI_PATH_PREFIX", prefix.c_str(), true);
73 mHwApi = HwApi::Create();
74
75 for (auto n : REQUIRED) {
76 auto name = std::filesystem::path(n);
77 auto path = std::filesystem::path(mEmptyDir.path) / name;
78 fs_mkdirs(path.c_str(), S_IRWXU);
79 std::ofstream touch{path};
80 }
81 prefix = std::filesystem::path(mEmptyDir.path) / "";
82 setenv("HWAPI_PATH_PREFIX", prefix.c_str(), true);
83 mNoApi = HwApi::Create();
84 }
85
TearDown()86 void TearDown() override { verifyContents(); }
87
88 protected:
89 // Set expected file content for a test.
90 template <typename T>
expectContent(const std::string & name,const T & value)91 void expectContent(const std::string &name, const T &value) {
92 mExpectedContent[name] << value << std::endl;
93 }
94
95 // Set actual file content for an input test.
96 template <typename T>
updateContent(const std::string & name,const T & value)97 void updateContent(const std::string &name, const T &value) {
98 std::ofstream(mFileMap[name]) << value << std::endl;
99 }
100
101 template <typename T>
expectAndUpdateContent(const std::string & name,const T & value)102 void expectAndUpdateContent(const std::string &name, const T &value) {
103 expectContent(name, value);
104 updateContent(name, value);
105 }
106
107 // Compare all file contents against expected contents.
verifyContents()108 void verifyContents() {
109 for (auto &a : mFileMap) {
110 std::ifstream file{a.second};
111 std::string expect = mExpectedContent[a.first].str();
112 std::string actual = std::string(std::istreambuf_iterator<char>(file),
113 std::istreambuf_iterator<char>());
114 EXPECT_EQ(expect, actual) << a.first;
115 }
116 }
117
118 // TODO(eliptus): Determine how to induce errors in required files
isRequired(const std::string & name)119 static bool isRequired(const std::string &name) {
120 for (auto n : REQUIRED) {
121 if (std::string(n) == name) {
122 return true;
123 }
124 }
125 return false;
126 }
127
ParamNameFixup(std::string str)128 static auto ParamNameFixup(std::string str) {
129 std::replace(str.begin(), str.end(), '/', '_');
130 return str;
131 }
132
133 protected:
134 std::unique_ptr<Vibrator::HwApi> mHwApi;
135 std::unique_ptr<Vibrator::HwApi> mNoApi;
136 std::map<std::string, std::string> mFileMap;
137 TemporaryDir mFilesDir;
138 TemporaryDir mEmptyDir;
139 std::map<std::string, std::stringstream> mExpectedContent;
140 };
141
142 class CreateTest : public HwApiTest, public WithParamInterface<const char *> {
143 public:
SetUp()144 void SetUp() override{};
TearDown()145 void TearDown() override{};
146
PrintParam(const TestParamInfo<CreateTest::ParamType> & info)147 static auto PrintParam(const TestParamInfo<CreateTest::ParamType> &info) {
148 return ParamNameFixup(info.param);
149 }
AllParams()150 static auto &AllParams() { return FILE_NAMES; }
151 };
152
TEST_P(CreateTest,file_missing)153 TEST_P(CreateTest, file_missing) {
154 auto skip = std::string(GetParam());
155 TemporaryDir dir;
156 std::unique_ptr<HwApi> hwapi;
157 std::string prefix;
158
159 for (auto n : FILE_NAMES) {
160 auto name = std::string(n);
161 auto path = std::string(dir.path) + "/" + name;
162 if (name == skip) {
163 continue;
164 }
165 fs_mkdirs(path.c_str(), S_IRWXU);
166 std::ofstream touch{path};
167 }
168
169 prefix = std::filesystem::path(dir.path) / "";
170 setenv("HWAPI_PATH_PREFIX", prefix.c_str(), true);
171 hwapi = HwApi::Create();
172 if (isRequired(skip)) {
173 EXPECT_EQ(nullptr, hwapi);
174 } else {
175 EXPECT_NE(nullptr, hwapi);
176 }
177 }
178
179 INSTANTIATE_TEST_CASE_P(HwApiTests, CreateTest, ValuesIn(CreateTest::AllParams()),
180 CreateTest::PrintParam);
181
182 template <typename T>
183 class HwApiTypedTest : public HwApiTest,
184 public WithParamInterface<std::tuple<std::string, std::function<T>>> {
185 public:
PrintParam(const TestParamInfo<typename HwApiTypedTest::ParamType> & info)186 static auto PrintParam(const TestParamInfo<typename HwApiTypedTest::ParamType> &info) {
187 return ParamNameFixup(std::get<0>(info.param));
188 }
MakeParam(std::string name,std::function<T> func)189 static auto MakeParam(std::string name, std::function<T> func) {
190 return std::make_tuple(name, func);
191 }
192 };
193
194 using HasTest = HwApiTypedTest<bool(Vibrator::HwApi &)>;
195
TEST_P(HasTest,success_returnsTrue)196 TEST_P(HasTest, success_returnsTrue) {
197 auto param = GetParam();
198 auto func = std::get<1>(param);
199
200 EXPECT_TRUE(func(*mHwApi));
201 }
202
TEST_P(HasTest,success_returnsFalse)203 TEST_P(HasTest, success_returnsFalse) {
204 auto param = GetParam();
205 auto func = std::get<1>(param);
206
207 EXPECT_FALSE(func(*mNoApi));
208 }
209
210 INSTANTIATE_TEST_CASE_P(HwApiTests, HasTest,
211 ValuesIn({
212 HasTest::MakeParam("device/rtp_input", &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", &Vibrator::HwApi::setLpTriggerEffect),
357 SetUint32Test::MakeParam("device/lra_wave_shape", &Vibrator::HwApi::setLraWaveShape),
358 SetUint32Test::MakeParam("device/od_clamp", &Vibrator::HwApi::setOdClamp),
359 }),
360 SetUint32Test::PrintParam);
361
362 using SetStringTest = HwApiTypedTest<bool(Vibrator::HwApi &, std::string)>;
363
TEST_P(SetStringTest,success)364 TEST_P(SetStringTest, success) {
365 auto param = GetParam();
366 auto name = std::get<0>(param);
367 auto func = std::get<1>(param);
368 std::string value = TemporaryFile().path;
369
370 expectContent(name, value);
371
372 EXPECT_TRUE(func(*mHwApi, value));
373 }
374
TEST_P(SetStringTest,failure)375 TEST_P(SetStringTest, failure) {
376 auto param = GetParam();
377 auto name = std::get<0>(param);
378 auto func = std::get<1>(param);
379 std::string value = TemporaryFile().path;
380
381 if (isRequired(name)) {
382 GTEST_SKIP();
383 }
384
385 EXPECT_FALSE(func(*mNoApi, value));
386 }
387
388 INSTANTIATE_TEST_CASE_P(
389 HwApiTests, SetStringTest,
390 ValuesIn({
391 SetStringTest::MakeParam("device/autocal", &Vibrator::HwApi::setAutocal),
392 SetStringTest::MakeParam("device/mode", &Vibrator::HwApi::setMode),
393 SetStringTest::MakeParam("device/set_sequencer", &Vibrator::HwApi::setSequencer),
394 }),
395 SetStringTest::PrintParam);
396
397 } // namespace implementation
398 } // namespace V1_3
399 } // namespace vibrator
400 } // namespace hardware
401 } // namespace android
402