123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- /*
- * Copyright (C) 2022 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include <android-base/file.h>
- #include <gtest/gtest.h>
- #include <fstream>
- #include "Hardware.h"
- namespace aidl {
- namespace android {
- namespace hardware {
- namespace vibrator {
- using ::testing::Test;
- class HwCalTest : public Test {
- protected:
- static constexpr std::array<uint32_t, 2> V_TICK_DEFAULT = {1, 100};
- static constexpr std::array<uint32_t, 2> V_CLICK_DEFAULT = {1, 100};
- static constexpr std::array<uint32_t, 2> V_LONG_DEFAULT = {1, 100};
- public:
- void SetUp() override { setenv("CALIBRATION_FILEPATH", mCalFile.path, true); }
- private:
- template <typename T>
- static void pack(std::ostream &stream, const T &value, std::string lpad, std::string rpad) {
- stream << lpad << value << rpad;
- }
- template <typename T, typename std::array<T, 0>::size_type N>
- static void pack(std::ostream &stream, const std::array<T, N> &value, std::string lpad,
- std::string rpad) {
- for (auto &entry : value) {
- pack(stream, entry, lpad, rpad);
- }
- }
- protected:
- void createHwCal() { mHwCal = std::make_unique<HwCal>(); }
- template <typename T>
- void write(const std::string key, const T &value, std::string lpad = " ",
- std::string rpad = "") {
- std::ofstream calfile{mCalFile.path, std::ios_base::app};
- calfile << key << ":";
- pack(calfile, value, lpad, rpad);
- calfile << std::endl;
- }
- void unlink() { ::unlink(mCalFile.path); }
- protected:
- std::unique_ptr<Vibrator::HwCal> mHwCal;
- TemporaryFile mCalFile;
- };
- TEST_F(HwCalTest, f0_measured) {
- uint32_t randInput = std::rand();
- std::string expect = std::to_string(randInput);
- std::string actual = std::to_string(~randInput);
- write("f0_measured", expect);
- createHwCal();
- EXPECT_TRUE(mHwCal->getF0(&actual));
- EXPECT_EQ(expect, actual);
- }
- TEST_F(HwCalTest, f0_missing) {
- std::string actual;
- createHwCal();
- EXPECT_FALSE(mHwCal->getF0(&actual));
- }
- TEST_F(HwCalTest, redc_measured) {
- uint32_t randInput = std::rand();
- std::string expect = std::to_string(randInput);
- std::string actual = std::to_string(~randInput);
- write("redc_measured", expect);
- createHwCal();
- EXPECT_TRUE(mHwCal->getRedc(&actual));
- EXPECT_EQ(expect, actual);
- }
- TEST_F(HwCalTest, redc_missing) {
- std::string actual;
- createHwCal();
- EXPECT_FALSE(mHwCal->getRedc(&actual));
- }
- TEST_F(HwCalTest, q_measured) {
- uint32_t randInput = std::rand();
- std::string expect = std::to_string(randInput);
- std::string actual = std::to_string(~randInput);
- write("q_measured", expect);
- createHwCal();
- EXPECT_TRUE(mHwCal->getQ(&actual));
- EXPECT_EQ(expect, actual);
- }
- TEST_F(HwCalTest, q_missing) {
- std::string actual;
- createHwCal();
- EXPECT_FALSE(mHwCal->getQ(&actual));
- }
- TEST_F(HwCalTest, v_levels) {
- std::array<uint32_t, 2> expect;
- std::array<uint32_t, 2> actual;
- // voltage for tick effects
- std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) {
- e = std::rand();
- return ~e;
- });
- write("v_tick", expect);
- createHwCal();
- EXPECT_TRUE(mHwCal->getTickVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- // voltage for click effects
- std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) {
- e = std::rand();
- return ~e;
- });
- write("v_click", expect);
- createHwCal();
- EXPECT_TRUE(mHwCal->getClickVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- // voltage for long effects
- std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) {
- e = std::rand();
- return ~e;
- });
- write("v_long", expect);
- createHwCal();
- EXPECT_TRUE(mHwCal->getLongVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- }
- TEST_F(HwCalTest, v_missing) {
- std::array<uint32_t, 2> expect = V_TICK_DEFAULT;
- std::array<uint32_t, 2> actual;
- std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
- createHwCal();
- EXPECT_TRUE(mHwCal->getTickVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- expect = V_CLICK_DEFAULT;
- std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
- createHwCal();
- EXPECT_TRUE(mHwCal->getClickVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- expect = V_LONG_DEFAULT;
- std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
- createHwCal();
- EXPECT_TRUE(mHwCal->getLongVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- }
- TEST_F(HwCalTest, v_short) {
- std::array<uint32_t, 2> expect = V_TICK_DEFAULT;
- std::array<uint32_t, 2> actual;
- std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
- write("v_tick", std::array<uint32_t, expect.size() - 1>());
- write("v_click", std::array<uint32_t, expect.size() - 1>());
- write("v_long", std::array<uint32_t, expect.size() - 1>());
- createHwCal();
- EXPECT_TRUE(mHwCal->getTickVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- expect = V_CLICK_DEFAULT;
- EXPECT_TRUE(mHwCal->getClickVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- expect = V_LONG_DEFAULT;
- EXPECT_TRUE(mHwCal->getLongVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- }
- TEST_F(HwCalTest, v_long) {
- std::array<uint32_t, 2> expect = V_TICK_DEFAULT;
- std::array<uint32_t, 2> actual;
- std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
- write("v_tick", std::array<uint32_t, expect.size() + 1>());
- write("v_click", std::array<uint32_t, expect.size() + 1>());
- write("v_long", std::array<uint32_t, expect.size() + 1>());
- createHwCal();
- EXPECT_TRUE(mHwCal->getTickVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- expect = V_CLICK_DEFAULT;
- EXPECT_TRUE(mHwCal->getClickVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- expect = V_LONG_DEFAULT;
- EXPECT_TRUE(mHwCal->getLongVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- }
- TEST_F(HwCalTest, v_nofile) {
- std::array<uint32_t, 2> expect = V_TICK_DEFAULT;
- std::array<uint32_t, 2> actual;
- std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; });
- write("v_tick", actual);
- write("v_click", actual);
- write("v_long", actual);
- unlink();
- createHwCal();
- EXPECT_TRUE(mHwCal->getTickVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- expect = V_CLICK_DEFAULT;
- EXPECT_TRUE(mHwCal->getClickVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- expect = V_LONG_DEFAULT;
- EXPECT_TRUE(mHwCal->getLongVolLevels(&actual));
- EXPECT_EQ(expect, actual);
- }
- TEST_F(HwCalTest, multiple) {
- uint32_t randInput = std::rand();
- std::string f0Expect = std::to_string(randInput);
- std::string f0Actual = std::to_string(~randInput);
- randInput = std::rand();
- std::string redcExpect = std::to_string(randInput);
- std::string redcActual = std::to_string(~randInput);
- randInput = std::rand();
- std::string qExpect = std::to_string(randInput);
- std::string qActual = std::to_string(~randInput);
- std::array<uint32_t, 2> volTickExpect, volClickExpect, volLongExpect;
- std::array<uint32_t, 2> volActual;
- std::transform(volTickExpect.begin(), volTickExpect.end(), volActual.begin(), [](uint32_t &e) {
- e = std::rand();
- return ~e;
- });
- write("f0_measured", f0Expect);
- write("redc_measured", redcExpect);
- write("q_measured", qExpect);
- write("v_tick", volTickExpect);
- std::transform(volClickExpect.begin(), volClickExpect.end(), volActual.begin(),
- [](uint32_t &e) {
- e = std::rand();
- return ~e;
- });
- write("v_click", volClickExpect);
- std::transform(volLongExpect.begin(), volLongExpect.end(), volActual.begin(), [](uint32_t &e) {
- e = std::rand();
- return ~e;
- });
- write("v_long", volLongExpect);
- createHwCal();
- EXPECT_TRUE(mHwCal->getF0(&f0Actual));
- EXPECT_EQ(f0Expect, f0Actual);
- EXPECT_TRUE(mHwCal->getRedc(&redcActual));
- EXPECT_EQ(redcExpect, redcActual);
- EXPECT_TRUE(mHwCal->getQ(&qActual));
- EXPECT_EQ(qExpect, qActual);
- EXPECT_TRUE(mHwCal->getTickVolLevels(&volActual));
- EXPECT_EQ(volTickExpect, volActual);
- EXPECT_TRUE(mHwCal->getClickVolLevels(&volActual));
- EXPECT_EQ(volClickExpect, volActual);
- EXPECT_TRUE(mHwCal->getLongVolLevels(&volActual));
- EXPECT_EQ(volLongExpect, volActual);
- }
- TEST_F(HwCalTest, trimming) {
- uint32_t randInput = std::rand();
- std::string f0Expect = std::to_string(randInput);
- std::string f0Actual = std::to_string(~randInput);
- randInput = std::rand();
- std::string redcExpect = std::to_string(randInput);
- std::string redcActual = std::to_string(randInput);
- randInput = std::rand();
- std::string qExpect = std::to_string(randInput);
- std::string qActual = std::to_string(randInput);
- std::array<uint32_t, 2> volTickExpect, volClickExpect, volLongExpect;
- std::array<uint32_t, 2> volActual;
- std::transform(volTickExpect.begin(), volTickExpect.end(), volActual.begin(), [](uint32_t &e) {
- e = std::rand();
- return ~e;
- });
- write("f0_measured", f0Expect, " \t", "\t ");
- write("redc_measured", redcExpect, " \t", "\t ");
- write("q_measured", qExpect, " \t", "\t ");
- write("v_tick", volTickExpect, " \t", "\t ");
- std::transform(volClickExpect.begin(), volClickExpect.end(), volActual.begin(),
- [](uint32_t &e) {
- e = std::rand();
- return ~e;
- });
- write("v_click", volClickExpect, " \t", "\t ");
- std::transform(volLongExpect.begin(), volLongExpect.end(), volActual.begin(), [](uint32_t &e) {
- e = std::rand();
- return ~e;
- });
- write("v_long", volLongExpect, " \t", "\t ");
- createHwCal();
- EXPECT_TRUE(mHwCal->getF0(&f0Actual));
- EXPECT_EQ(f0Expect, f0Actual);
- EXPECT_TRUE(mHwCal->getRedc(&redcActual));
- EXPECT_EQ(redcExpect, redcActual);
- EXPECT_TRUE(mHwCal->getQ(&qActual));
- EXPECT_EQ(qExpect, qActual);
- EXPECT_TRUE(mHwCal->getTickVolLevels(&volActual));
- EXPECT_EQ(volTickExpect, volActual);
- EXPECT_TRUE(mHwCal->getClickVolLevels(&volActual));
- EXPECT_EQ(volClickExpect, volActual);
- EXPECT_TRUE(mHwCal->getLongVolLevels(&volActual));
- EXPECT_EQ(volLongExpect, volActual);
- }
- } // namespace vibrator
- } // namespace hardware
- } // namespace android
- } // namespace aidl
|