/* * Copyright (C) 2019 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 #include #include #include "Hardware.h" namespace aidl { namespace android { namespace hardware { namespace vibrator { using ::testing::Test; class HwCalTest : public Test { protected: static constexpr uint32_t Q_DEFAULT = 15.5f * (1 << 16); static constexpr std::array V_DEFAULT = {60, 70, 80, 90, 100, 76}; public: void SetUp() override { setenv("CALIBRATION_FILEPATH", mCalFile.path, true); } private: static void pack(std::ostream &stream, const uint32_t &value, std::string lpad, std::string rpad) { stream << lpad << value << rpad; } template ::size_type N> static void pack(std::ostream &stream, const std::array &value, std::string lpad, std::string rpad) { for (auto &entry : value) { pack(stream, entry, lpad, rpad); } } protected: void createHwCal() { mHwCal = std::make_unique(); } template 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 mHwCal; TemporaryFile mCalFile; }; TEST_F(HwCalTest, f0_measured) { uint32_t expect = std::rand(); uint32_t actual = ~expect; write("f0_measured", expect); createHwCal(); EXPECT_TRUE(mHwCal->getF0(&actual)); EXPECT_EQ(expect, actual); } TEST_F(HwCalTest, f0_missing) { uint32_t actual; createHwCal(); EXPECT_FALSE(mHwCal->getF0(&actual)); } TEST_F(HwCalTest, redc_measured) { uint32_t expect = std::rand(); uint32_t actual = ~expect; write("redc_measured", expect); createHwCal(); EXPECT_TRUE(mHwCal->getRedc(&actual)); EXPECT_EQ(expect, actual); } TEST_F(HwCalTest, redc_missing) { uint32_t actual; createHwCal(); EXPECT_FALSE(mHwCal->getRedc(&actual)); } TEST_F(HwCalTest, q_measured) { uint32_t expect = std::rand(); uint32_t actual = ~expect; write("q_measured", expect); createHwCal(); EXPECT_TRUE(mHwCal->getQ(&actual)); EXPECT_EQ(expect, actual); } TEST_F(HwCalTest, q_index) { uint8_t value = std::rand(); uint32_t expect = value * 1.5f * (1 << 16) + 2.0f * (1 << 16); uint32_t actual = ~expect; write("q_index", value); createHwCal(); EXPECT_TRUE(mHwCal->getQ(&actual)); EXPECT_EQ(expect, actual); } TEST_F(HwCalTest, q_missing) { uint32_t expect = Q_DEFAULT; uint32_t actual = ~expect; createHwCal(); EXPECT_TRUE(mHwCal->getQ(&actual)); EXPECT_EQ(expect, actual); } TEST_F(HwCalTest, q_nofile) { uint32_t expect = Q_DEFAULT; uint32_t actual = ~expect; write("q_measured", actual); unlink(); createHwCal(); EXPECT_TRUE(mHwCal->getQ(&actual)); EXPECT_EQ(expect, actual); } TEST_F(HwCalTest, v_levels) { std::array expect; std::array actual; std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { e = std::rand(); return ~e; }); write("v_levels", expect); createHwCal(); EXPECT_TRUE(mHwCal->getVolLevels(&actual)); EXPECT_EQ(expect, actual); } TEST_F(HwCalTest, v_missing) { std::array expect = V_DEFAULT; std::array actual; std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; }); createHwCal(); EXPECT_TRUE(mHwCal->getVolLevels(&actual)); EXPECT_EQ(expect, actual); } TEST_F(HwCalTest, v_short) { std::array expect = V_DEFAULT; std::array actual; std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; }); write("v_levels", std::array()); createHwCal(); EXPECT_TRUE(mHwCal->getVolLevels(&actual)); EXPECT_EQ(expect, actual); } TEST_F(HwCalTest, v_long) { std::array expect = V_DEFAULT; std::array actual; std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; }); write("v_levels", std::array()); createHwCal(); EXPECT_TRUE(mHwCal->getVolLevels(&actual)); EXPECT_EQ(expect, actual); } TEST_F(HwCalTest, v_nofile) { std::array expect = V_DEFAULT; std::array actual; std::transform(expect.begin(), expect.end(), actual.begin(), [](uint32_t &e) { return ~e; }); write("v_levels", actual); unlink(); createHwCal(); EXPECT_TRUE(mHwCal->getVolLevels(&actual)); EXPECT_EQ(expect, actual); } TEST_F(HwCalTest, multiple) { uint32_t f0Expect = std::rand(); uint32_t f0Actual = ~f0Expect; uint32_t redcExpect = std::rand(); uint32_t redcActual = ~redcExpect; uint32_t qExpect = std::rand(); uint32_t qActual = ~qExpect; std::array volExpect; std::array volActual; std::transform(volExpect.begin(), volExpect.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_levels", volExpect); 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->getVolLevels(&volActual)); EXPECT_EQ(volExpect, volActual); } TEST_F(HwCalTest, trimming) { uint32_t f0Expect = std::rand(); uint32_t f0Actual = ~f0Expect; uint32_t redcExpect = std::rand(); uint32_t redcActual = ~redcExpect; uint32_t qExpect = std::rand(); uint32_t qActual = ~qExpect; std::array volExpect; std::array volActual; std::transform(volExpect.begin(), volExpect.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_levels", volExpect, " \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->getVolLevels(&volActual)); EXPECT_EQ(volExpect, volActual); } } // namespace vibrator } // namespace hardware } // namespace android } // namespace aidl