test-vibrator.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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 <aidl/android/hardware/vibrator/BnVibratorCallback.h>
  17. #include <android-base/logging.h>
  18. #include <gmock/gmock.h>
  19. #include <gtest/gtest.h>
  20. #include <linux/input.h>
  21. #include <linux/uinput.h>
  22. #include <future>
  23. #include "Vibrator.h"
  24. #include "mocks.h"
  25. #include "types.h"
  26. #include "utils.h"
  27. namespace aidl {
  28. namespace android {
  29. namespace hardware {
  30. namespace vibrator {
  31. using ::testing::_;
  32. using ::testing::AnyNumber;
  33. using ::testing::Assign;
  34. using ::testing::AtLeast;
  35. using ::testing::AtMost;
  36. using ::testing::Combine;
  37. using ::testing::DoAll;
  38. using ::testing::DoDefault;
  39. using ::testing::Exactly;
  40. using ::testing::Expectation;
  41. using ::testing::ExpectationSet;
  42. using ::testing::Ge;
  43. using ::testing::Mock;
  44. using ::testing::MockFunction;
  45. using ::testing::Range;
  46. using ::testing::Return;
  47. using ::testing::Sequence;
  48. using ::testing::SetArgPointee;
  49. using ::testing::SetArgReferee;
  50. using ::testing::Test;
  51. using ::testing::TestParamInfo;
  52. using ::testing::ValuesIn;
  53. using ::testing::WithParamInterface;
  54. // Forward Declarations
  55. static EffectQueue Queue(const QueueEffect &effect);
  56. static EffectQueue Queue(const QueueDelay &delay);
  57. template <typename T, typename U, typename... Args>
  58. static EffectQueue Queue(const T &first, const U &second, Args... rest);
  59. static EffectLevel Level(float intensity, float levelLow, float levelHigh);
  60. static EffectScale Scale(float intensity, float levelLow, float levelHigh);
  61. // Constants With Arbitrary Values
  62. static constexpr uint32_t CAL_VERSION = 2;
  63. static constexpr std::array<EffectLevel, 2> V_TICK_DEFAULT = {1, 100};
  64. static constexpr std::array<EffectLevel, 2> V_CLICK_DEFAULT{1, 100};
  65. static constexpr std::array<EffectLevel, 2> V_LONG_DEFAULT{1, 100};
  66. static constexpr std::array<EffectDuration, 14> EFFECT_DURATIONS{
  67. 0, 100, 30, 1000, 300, 130, 150, 500, 100, 15, 20, 1000, 1000, 1000};
  68. // Constants With Prescribed Values
  69. static const std::map<Effect, EffectIndex> EFFECT_INDEX{
  70. {Effect::CLICK, 2},
  71. {Effect::TICK, 2},
  72. {Effect::HEAVY_CLICK, 2},
  73. {Effect::TEXTURE_TICK, 9},
  74. };
  75. static constexpr uint32_t MIN_ON_OFF_INTERVAL_US = 8500;
  76. static constexpr uint8_t VOLTAGE_SCALE_MAX = 100;
  77. static constexpr int8_t MAX_COLD_START_LATENCY_MS = 6; // I2C Transaction + DSP Return-From-Standby
  78. static constexpr auto POLLING_TIMEOUT = 20;
  79. enum WaveformIndex : uint16_t {
  80. /* Physical waveform */
  81. WAVEFORM_LONG_VIBRATION_EFFECT_INDEX = 0,
  82. WAVEFORM_RESERVED_INDEX_1 = 1,
  83. WAVEFORM_CLICK_INDEX = 2,
  84. WAVEFORM_SHORT_VIBRATION_EFFECT_INDEX = 3,
  85. WAVEFORM_THUD_INDEX = 4,
  86. WAVEFORM_SPIN_INDEX = 5,
  87. WAVEFORM_QUICK_RISE_INDEX = 6,
  88. WAVEFORM_SLOW_RISE_INDEX = 7,
  89. WAVEFORM_QUICK_FALL_INDEX = 8,
  90. WAVEFORM_LIGHT_TICK_INDEX = 9,
  91. WAVEFORM_LOW_TICK_INDEX = 10,
  92. WAVEFORM_RESERVED_MFG_1,
  93. WAVEFORM_RESERVED_MFG_2,
  94. WAVEFORM_RESERVED_MFG_3,
  95. WAVEFORM_MAX_PHYSICAL_INDEX,
  96. /* OWT waveform */
  97. WAVEFORM_COMPOSE = WAVEFORM_MAX_PHYSICAL_INDEX,
  98. WAVEFORM_PWLE,
  99. /*
  100. * Refer to <linux/input.h>, the WAVEFORM_MAX_INDEX must not exceed 96.
  101. * #define FF_GAIN 0x60 // 96 in decimal
  102. * #define FF_MAX_EFFECTS FF_GAIN
  103. */
  104. WAVEFORM_MAX_INDEX,
  105. };
  106. static const EffectScale ON_GLOBAL_SCALE{levelToScale(V_LONG_DEFAULT[1])};
  107. static const EffectIndex ON_EFFECT_INDEX{0};
  108. static const std::map<EffectTuple, EffectScale> EFFECT_SCALE{
  109. {{Effect::TICK, EffectStrength::LIGHT},
  110. Scale(0.5f * 0.5f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
  111. {{Effect::TICK, EffectStrength::MEDIUM},
  112. Scale(0.5f * 0.7f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
  113. {{Effect::TICK, EffectStrength::STRONG},
  114. Scale(0.5f * 1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
  115. {{Effect::CLICK, EffectStrength::LIGHT},
  116. Scale(0.7f * 0.5f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
  117. {{Effect::CLICK, EffectStrength::MEDIUM},
  118. Scale(0.7f * 0.7f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
  119. {{Effect::CLICK, EffectStrength::STRONG},
  120. Scale(0.7f * 1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
  121. {{Effect::HEAVY_CLICK, EffectStrength::LIGHT},
  122. Scale(1.0f * 0.5f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
  123. {{Effect::HEAVY_CLICK, EffectStrength::MEDIUM},
  124. Scale(1.0f * 0.7f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
  125. {{Effect::HEAVY_CLICK, EffectStrength::STRONG},
  126. Scale(1.0f * 1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
  127. {{Effect::TEXTURE_TICK, EffectStrength::LIGHT},
  128. Scale(0.5f * 0.5f, V_TICK_DEFAULT[0], V_TICK_DEFAULT[1])},
  129. {{Effect::TEXTURE_TICK, EffectStrength::MEDIUM},
  130. Scale(0.5f * 0.7f, V_TICK_DEFAULT[0], V_TICK_DEFAULT[1])},
  131. {{Effect::TEXTURE_TICK, EffectStrength::STRONG},
  132. Scale(0.5f * 1.0f, V_TICK_DEFAULT[0], V_TICK_DEFAULT[1])},
  133. };
  134. static const std::map<EffectTuple, EffectQueue> EFFECT_QUEUE{
  135. {{Effect::DOUBLE_CLICK, EffectStrength::LIGHT},
  136. Queue(QueueEffect{EFFECT_INDEX.at(Effect::CLICK),
  137. Level(0.7f * 0.5f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
  138. 100,
  139. QueueEffect{EFFECT_INDEX.at(Effect::CLICK),
  140. Level(1.0f * 0.5f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])})},
  141. {{Effect::DOUBLE_CLICK, EffectStrength::MEDIUM},
  142. Queue(QueueEffect{EFFECT_INDEX.at(Effect::CLICK),
  143. Level(0.7f * 0.7f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
  144. 100,
  145. QueueEffect{EFFECT_INDEX.at(Effect::CLICK),
  146. Level(1.0f * 0.7f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])})},
  147. {{Effect::DOUBLE_CLICK, EffectStrength::STRONG},
  148. Queue(QueueEffect{EFFECT_INDEX.at(Effect::CLICK),
  149. Level(0.7f * 1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
  150. 100,
  151. QueueEffect{EFFECT_INDEX.at(Effect::CLICK),
  152. Level(1.0f * 1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])})},
  153. };
  154. EffectQueue Queue(const QueueEffect &effect) {
  155. auto index = std::get<0>(effect);
  156. auto level = std::get<1>(effect);
  157. auto string = std::to_string(index) + "." + std::to_string(level);
  158. auto duration = EFFECT_DURATIONS[index];
  159. return {string, duration};
  160. }
  161. EffectQueue Queue(const QueueDelay &delay) {
  162. auto string = std::to_string(delay);
  163. return {string, delay};
  164. }
  165. template <typename T, typename U, typename... Args>
  166. EffectQueue Queue(const T &first, const U &second, Args... rest) {
  167. auto head = Queue(first);
  168. auto tail = Queue(second, rest...);
  169. auto string = std::get<0>(head) + "," + std::get<0>(tail);
  170. auto duration = std::get<1>(head) + std::get<1>(tail);
  171. return {string, duration};
  172. }
  173. static EffectLevel Level(float intensity, float levelLow, float levelHigh) {
  174. return std::lround(intensity * (levelHigh - levelLow)) + levelLow;
  175. }
  176. static EffectScale Scale(float intensity, float levelLow, float levelHigh) {
  177. return levelToScale(Level(intensity, levelLow, levelHigh));
  178. }
  179. class VibratorTest : public Test {
  180. public:
  181. void SetUp() override {
  182. setenv("INPUT_EVENT_NAME", "CS40L26TestSuite", true);
  183. std::unique_ptr<MockApi> mockapi;
  184. std::unique_ptr<MockCal> mockcal;
  185. createMock(&mockapi, &mockcal);
  186. createVibrator(std::move(mockapi), std::move(mockcal));
  187. }
  188. void TearDown() override { deleteVibrator(); }
  189. protected:
  190. void createMock(std::unique_ptr<MockApi> *mockapi, std::unique_ptr<MockCal> *mockcal) {
  191. *mockapi = std::make_unique<MockApi>();
  192. *mockcal = std::make_unique<MockCal>();
  193. mMockApi = mockapi->get();
  194. mMockCal = mockcal->get();
  195. ON_CALL(*mMockApi, destructor()).WillByDefault(Assign(&mMockApi, nullptr));
  196. ON_CALL(*mMockApi, setFFGain(_, _)).WillByDefault(Return(true));
  197. ON_CALL(*mMockApi, setFFEffect(_, _, _)).WillByDefault(Return(true));
  198. ON_CALL(*mMockApi, setFFPlay(_, _, _)).WillByDefault(Return(true));
  199. ON_CALL(*mMockApi, pollVibeState(_, _)).WillByDefault(Return(true));
  200. ON_CALL(*mMockApi, uploadOwtEffect(_, _, _, _, _, _)).WillByDefault(Return(true));
  201. ON_CALL(*mMockApi, eraseOwtEffect(_, _, _)).WillByDefault(Return(true));
  202. ON_CALL(*mMockApi, getOwtFreeSpace(_))
  203. .WillByDefault(DoAll(SetArgPointee<0>(11504), Return(true)));
  204. ON_CALL(*mMockCal, destructor()).WillByDefault(Assign(&mMockCal, nullptr));
  205. ON_CALL(*mMockCal, getVersion(_))
  206. .WillByDefault(DoAll(SetArgPointee<0>(CAL_VERSION), Return(true)));
  207. ON_CALL(*mMockCal, getTickVolLevels(_))
  208. .WillByDefault(DoAll(SetArgPointee<0>(V_TICK_DEFAULT), Return(true)));
  209. ON_CALL(*mMockCal, getClickVolLevels(_))
  210. .WillByDefault(DoAll(SetArgPointee<0>(V_CLICK_DEFAULT), Return(true)));
  211. ON_CALL(*mMockCal, getLongVolLevels(_))
  212. .WillByDefault(DoAll(SetArgPointee<0>(V_LONG_DEFAULT), Return(true)));
  213. relaxMock(false);
  214. }
  215. void createVibrator(std::unique_ptr<MockApi> mockapi, std::unique_ptr<MockCal> mockcal,
  216. bool relaxed = true) {
  217. if (relaxed) {
  218. relaxMock(true);
  219. }
  220. mVibrator = ndk::SharedRefBase::make<Vibrator>(std::move(mockapi), std::move(mockcal));
  221. if (relaxed) {
  222. relaxMock(false);
  223. }
  224. }
  225. void deleteVibrator(bool relaxed = true) {
  226. if (relaxed) {
  227. relaxMock(true);
  228. }
  229. mVibrator.reset();
  230. }
  231. private:
  232. void relaxMock(bool relax) {
  233. auto times = relax ? AnyNumber() : Exactly(0);
  234. Mock::VerifyAndClearExpectations(mMockApi);
  235. Mock::VerifyAndClearExpectations(mMockCal);
  236. EXPECT_CALL(*mMockApi, destructor()).Times(times);
  237. EXPECT_CALL(*mMockApi, setF0(_)).Times(times);
  238. EXPECT_CALL(*mMockApi, setF0Offset(_)).Times(times);
  239. EXPECT_CALL(*mMockApi, setRedc(_)).Times(times);
  240. EXPECT_CALL(*mMockApi, setQ(_)).Times(times);
  241. EXPECT_CALL(*mMockApi, hasOwtFreeSpace()).Times(times);
  242. EXPECT_CALL(*mMockApi, getOwtFreeSpace(_)).Times(times);
  243. EXPECT_CALL(*mMockApi, setF0CompEnable(_)).Times(times);
  244. EXPECT_CALL(*mMockApi, setRedcCompEnable(_)).Times(times);
  245. EXPECT_CALL(*mMockApi, pollVibeState(_, _)).Times(times);
  246. EXPECT_CALL(*mMockApi, setFFGain(_, _)).Times(times);
  247. EXPECT_CALL(*mMockApi, setFFEffect(_, _, _)).Times(times);
  248. EXPECT_CALL(*mMockApi, setFFPlay(_, _, _)).Times(times);
  249. EXPECT_CALL(*mMockApi, setMinOnOffInterval(_)).Times(times);
  250. EXPECT_CALL(*mMockApi, getContextScale()).Times(times);
  251. EXPECT_CALL(*mMockApi, getContextEnable()).Times(times);
  252. EXPECT_CALL(*mMockApi, getContextSettlingTime()).Times(times);
  253. EXPECT_CALL(*mMockApi, getContextCooldownTime()).Times(times);
  254. EXPECT_CALL(*mMockApi, getContextFadeEnable()).Times(times);
  255. EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).Times(times);
  256. EXPECT_CALL(*mMockApi, setHapticPcmAmp(_, _, _, _)).Times(times);
  257. EXPECT_CALL(*mMockApi, debug(_)).Times(times);
  258. EXPECT_CALL(*mMockCal, destructor()).Times(times);
  259. EXPECT_CALL(*mMockCal, getF0(_)).Times(times);
  260. EXPECT_CALL(*mMockCal, getRedc(_)).Times(times);
  261. EXPECT_CALL(*mMockCal, getQ(_)).Times(times);
  262. EXPECT_CALL(*mMockCal, getTickVolLevels(_)).Times(times);
  263. EXPECT_CALL(*mMockCal, getClickVolLevels(_)).Times(times);
  264. EXPECT_CALL(*mMockCal, getLongVolLevels(_)).Times(times);
  265. EXPECT_CALL(*mMockCal, isChirpEnabled()).Times(times);
  266. EXPECT_CALL(*mMockCal, getLongFrequencyShift(_)).Times(times);
  267. EXPECT_CALL(*mMockCal, isF0CompEnabled()).Times(times);
  268. EXPECT_CALL(*mMockCal, isRedcCompEnabled()).Times(times);
  269. EXPECT_CALL(*mMockCal, debug(_)).Times(times);
  270. }
  271. protected:
  272. MockApi *mMockApi;
  273. MockCal *mMockCal;
  274. std::shared_ptr<IVibrator> mVibrator;
  275. uint32_t mEffectIndex;
  276. };
  277. TEST_F(VibratorTest, Constructor) {
  278. std::unique_ptr<MockApi> mockapi;
  279. std::unique_ptr<MockCal> mockcal;
  280. std::string f0Val = std::to_string(std::rand());
  281. std::string redcVal = std::to_string(std::rand());
  282. std::string qVal = std::to_string(std::rand());
  283. uint32_t calVer;
  284. uint32_t supportedPrimitivesBits = 0x0;
  285. Expectation volGet;
  286. Sequence f0Seq, redcSeq, qSeq, supportedPrimitivesSeq;
  287. EXPECT_CALL(*mMockApi, destructor()).WillOnce(DoDefault());
  288. EXPECT_CALL(*mMockCal, destructor()).WillOnce(DoDefault());
  289. deleteVibrator(false);
  290. createMock(&mockapi, &mockcal);
  291. EXPECT_CALL(*mMockCal, getF0(_))
  292. .InSequence(f0Seq)
  293. .WillOnce(DoAll(SetArgReferee<0>(f0Val), Return(true)));
  294. EXPECT_CALL(*mMockApi, setF0(f0Val)).InSequence(f0Seq).WillOnce(Return(true));
  295. EXPECT_CALL(*mMockCal, getRedc(_))
  296. .InSequence(redcSeq)
  297. .WillOnce(DoAll(SetArgReferee<0>(redcVal), Return(true)));
  298. EXPECT_CALL(*mMockApi, setRedc(redcVal)).InSequence(redcSeq).WillOnce(Return(true));
  299. EXPECT_CALL(*mMockCal, getQ(_))
  300. .InSequence(qSeq)
  301. .WillOnce(DoAll(SetArgReferee<0>(qVal), Return(true)));
  302. EXPECT_CALL(*mMockApi, setQ(qVal)).InSequence(qSeq).WillOnce(Return(true));
  303. EXPECT_CALL(*mMockCal, getLongFrequencyShift(_)).WillOnce(Return(true));
  304. mMockCal->getVersion(&calVer);
  305. if (calVer == 2) {
  306. volGet = EXPECT_CALL(*mMockCal, getTickVolLevels(_)).WillOnce(DoDefault());
  307. volGet = EXPECT_CALL(*mMockCal, getClickVolLevels(_)).WillOnce(DoDefault());
  308. volGet = EXPECT_CALL(*mMockCal, getLongVolLevels(_)).WillOnce(DoDefault());
  309. }
  310. EXPECT_CALL(*mMockCal, isF0CompEnabled()).WillOnce(Return(true));
  311. EXPECT_CALL(*mMockApi, setF0CompEnable(true)).WillOnce(Return(true));
  312. EXPECT_CALL(*mMockCal, isRedcCompEnabled()).WillOnce(Return(true));
  313. EXPECT_CALL(*mMockApi, setRedcCompEnable(true)).WillOnce(Return(true));
  314. EXPECT_CALL(*mMockCal, isChirpEnabled()).WillOnce(Return(true));
  315. EXPECT_CALL(*mMockCal, getSupportedPrimitives(_))
  316. .InSequence(supportedPrimitivesSeq)
  317. .WillOnce(DoAll(SetArgPointee<0>(supportedPrimitivesBits), Return(true)));
  318. EXPECT_CALL(*mMockApi, setMinOnOffInterval(MIN_ON_OFF_INTERVAL_US)).WillOnce(Return(true));
  319. EXPECT_CALL(*mMockApi, getContextScale()).WillOnce(Return(0));
  320. EXPECT_CALL(*mMockApi, getContextEnable()).WillOnce(Return(false));
  321. EXPECT_CALL(*mMockApi, getContextSettlingTime()).WillOnce(Return(0));
  322. EXPECT_CALL(*mMockApi, getContextCooldownTime()).WillOnce(Return(0));
  323. EXPECT_CALL(*mMockApi, getContextFadeEnable()).WillOnce(Return(false));
  324. createVibrator(std::move(mockapi), std::move(mockcal), false);
  325. }
  326. TEST_F(VibratorTest, on) {
  327. Sequence s1, s2;
  328. uint16_t duration = std::rand() + 1;
  329. EXPECT_CALL(*mMockApi, setFFGain(_, ON_GLOBAL_SCALE)).InSequence(s1).WillOnce(DoDefault());
  330. EXPECT_CALL(*mMockApi, setFFEffect(_, _, duration + MAX_COLD_START_LATENCY_MS))
  331. .InSequence(s2)
  332. .WillOnce(DoDefault());
  333. EXPECT_CALL(*mMockApi, setFFPlay(_, ON_EFFECT_INDEX, true))
  334. .InSequence(s1, s2)
  335. .WillOnce(DoDefault());
  336. EXPECT_TRUE(mVibrator->on(duration, nullptr).isOk());
  337. }
  338. TEST_F(VibratorTest, off) {
  339. Sequence s1;
  340. EXPECT_CALL(*mMockApi, setFFGain(_, ON_GLOBAL_SCALE)).InSequence(s1).WillOnce(DoDefault());
  341. EXPECT_TRUE(mVibrator->off().isOk());
  342. }
  343. TEST_F(VibratorTest, supportsAmplitudeControl_supported) {
  344. int32_t capabilities;
  345. EXPECT_CALL(*mMockApi, hasOwtFreeSpace()).WillOnce(Return(true));
  346. EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).WillOnce(Return(true));
  347. EXPECT_TRUE(mVibrator->getCapabilities(&capabilities).isOk());
  348. EXPECT_GT(capabilities & IVibrator::CAP_AMPLITUDE_CONTROL, 0);
  349. }
  350. TEST_F(VibratorTest, supportsExternalAmplitudeControl_unsupported) {
  351. int32_t capabilities;
  352. EXPECT_CALL(*mMockApi, hasOwtFreeSpace()).WillOnce(Return(true));
  353. EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).WillOnce(Return(true));
  354. EXPECT_TRUE(mVibrator->getCapabilities(&capabilities).isOk());
  355. EXPECT_EQ(capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL, 0);
  356. }
  357. TEST_F(VibratorTest, setAmplitude_supported) {
  358. EffectAmplitude amplitude = static_cast<float>(std::rand()) / RAND_MAX ?: 1.0f;
  359. EXPECT_CALL(*mMockApi, setFFGain(_, amplitudeToScale(amplitude))).WillOnce(Return(true));
  360. EXPECT_TRUE(mVibrator->setAmplitude(amplitude).isOk());
  361. }
  362. TEST_F(VibratorTest, supportsExternalControl_supported) {
  363. int32_t capabilities;
  364. EXPECT_CALL(*mMockApi, hasOwtFreeSpace()).WillOnce(Return(true));
  365. EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).WillOnce(Return(true));
  366. EXPECT_TRUE(mVibrator->getCapabilities(&capabilities).isOk());
  367. EXPECT_GT(capabilities & IVibrator::CAP_EXTERNAL_CONTROL, 0);
  368. }
  369. TEST_F(VibratorTest, supportsExternalControl_unsupported) {
  370. int32_t capabilities;
  371. EXPECT_CALL(*mMockApi, hasOwtFreeSpace()).WillOnce(Return(true));
  372. EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).WillOnce(Return(false));
  373. EXPECT_TRUE(mVibrator->getCapabilities(&capabilities).isOk());
  374. EXPECT_EQ(capabilities & IVibrator::CAP_EXTERNAL_CONTROL, 0);
  375. }
  376. TEST_F(VibratorTest, setExternalControl_enable) {
  377. Sequence s1, s2;
  378. EXPECT_CALL(*mMockApi, setFFGain(_, ON_GLOBAL_SCALE)).InSequence(s1).WillOnce(DoDefault());
  379. EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).InSequence(s2).WillOnce(Return(true));
  380. EXPECT_CALL(*mMockApi, setHapticPcmAmp(_, true, _, _))
  381. .InSequence(s1, s2)
  382. .WillOnce(Return(true));
  383. EXPECT_TRUE(mVibrator->setExternalControl(true).isOk());
  384. }
  385. TEST_F(VibratorTest, setExternalControl_disable) {
  386. Sequence s1, s2, s3, s4;
  387. // The default mIsUnderExternalControl is false, so it needs to turn on the External Control
  388. // to make mIsUnderExternalControl become true.
  389. EXPECT_CALL(*mMockApi, setFFGain(_, ON_GLOBAL_SCALE))
  390. .InSequence(s1)
  391. .InSequence(s1)
  392. .WillOnce(DoDefault());
  393. EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).InSequence(s2).WillOnce(Return(true));
  394. EXPECT_CALL(*mMockApi, setHapticPcmAmp(_, true, _, _)).InSequence(s3).WillOnce(Return(true));
  395. EXPECT_TRUE(mVibrator->setExternalControl(true).isOk());
  396. EXPECT_CALL(*mMockApi, setFFGain(_, levelToScale(VOLTAGE_SCALE_MAX)))
  397. .InSequence(s4)
  398. .WillOnce(DoDefault());
  399. EXPECT_CALL(*mMockApi, setHapticPcmAmp(_, false, _, _))
  400. .InSequence(s1, s2, s3, s4)
  401. .WillOnce(Return(true));
  402. EXPECT_TRUE(mVibrator->setExternalControl(false).isOk());
  403. }
  404. class EffectsTest : public VibratorTest, public WithParamInterface<EffectTuple> {
  405. public:
  406. static auto PrintParam(const TestParamInfo<ParamType> &info) {
  407. auto param = info.param;
  408. auto effect = std::get<0>(param);
  409. auto strength = std::get<1>(param);
  410. return toString(effect) + "_" + toString(strength);
  411. }
  412. };
  413. TEST_P(EffectsTest, perform) {
  414. auto param = GetParam();
  415. auto effect = std::get<0>(param);
  416. auto strength = std::get<1>(param);
  417. auto scale = EFFECT_SCALE.find(param);
  418. auto queue = EFFECT_QUEUE.find(param);
  419. EffectDuration duration;
  420. auto callback = ndk::SharedRefBase::make<MockVibratorCallback>();
  421. std::promise<void> promise;
  422. std::future<void> future{promise.get_future()};
  423. auto complete = [&promise] {
  424. promise.set_value();
  425. return ndk::ScopedAStatus::ok();
  426. };
  427. bool composeEffect;
  428. ExpectationSet eSetup;
  429. Expectation eActivate, ePollHaptics, ePollStop, eEraseDone;
  430. if (scale != EFFECT_SCALE.end()) {
  431. EffectIndex index = EFFECT_INDEX.at(effect);
  432. duration = EFFECT_DURATIONS[index];
  433. eSetup += EXPECT_CALL(*mMockApi, setFFGain(_, levelToScale(scale->second)))
  434. .WillOnce(DoDefault());
  435. eActivate = EXPECT_CALL(*mMockApi, setFFPlay(_, index, true))
  436. .After(eSetup)
  437. .WillOnce(DoDefault());
  438. } else if (queue != EFFECT_QUEUE.end()) {
  439. duration = std::get<1>(queue->second);
  440. eSetup += EXPECT_CALL(*mMockApi, setFFGain(_, ON_GLOBAL_SCALE))
  441. .After(eSetup)
  442. .WillOnce(DoDefault());
  443. eSetup += EXPECT_CALL(*mMockApi, getOwtFreeSpace(_)).WillOnce(DoDefault());
  444. eSetup += EXPECT_CALL(*mMockApi, uploadOwtEffect(_, _, _, _, _, _))
  445. .After(eSetup)
  446. .WillOnce(DoDefault());
  447. eActivate = EXPECT_CALL(*mMockApi, setFFPlay(_, WAVEFORM_COMPOSE, true))
  448. .After(eSetup)
  449. .WillOnce(DoDefault());
  450. composeEffect = true;
  451. } else {
  452. duration = 0;
  453. }
  454. if (duration) {
  455. ePollHaptics = EXPECT_CALL(*mMockApi, pollVibeState(1, POLLING_TIMEOUT))
  456. .After(eActivate)
  457. .WillOnce(DoDefault());
  458. ePollStop = EXPECT_CALL(*mMockApi, pollVibeState(0, -1))
  459. .After(ePollHaptics)
  460. .WillOnce(DoDefault());
  461. if (composeEffect) {
  462. eEraseDone = EXPECT_CALL(*mMockApi, eraseOwtEffect(_, _, _))
  463. .After(ePollStop)
  464. .WillOnce(DoDefault());
  465. EXPECT_CALL(*callback, onComplete()).After(eEraseDone).WillOnce(complete);
  466. } else {
  467. EXPECT_CALL(*callback, onComplete()).After(ePollStop).WillOnce(complete);
  468. }
  469. }
  470. int32_t lengthMs;
  471. ndk::ScopedAStatus status = mVibrator->perform(effect, strength, callback, &lengthMs);
  472. if (status.isOk()) {
  473. EXPECT_LE(duration, lengthMs);
  474. } else {
  475. EXPECT_EQ(EX_UNSUPPORTED_OPERATION, status.getExceptionCode());
  476. EXPECT_EQ(0, lengthMs);
  477. }
  478. if (duration) {
  479. EXPECT_EQ(future.wait_for(std::chrono::milliseconds(100)), std::future_status::ready);
  480. }
  481. }
  482. const std::vector<Effect> kEffects{ndk::enum_range<Effect>().begin(),
  483. ndk::enum_range<Effect>().end()};
  484. const std::vector<EffectStrength> kEffectStrengths{ndk::enum_range<EffectStrength>().begin(),
  485. ndk::enum_range<EffectStrength>().end()};
  486. INSTANTIATE_TEST_CASE_P(VibratorTests, EffectsTest,
  487. Combine(ValuesIn(kEffects.begin(), kEffects.end()),
  488. ValuesIn(kEffectStrengths.begin(), kEffectStrengths.end())),
  489. EffectsTest::PrintParam);
  490. struct PrimitiveParam {
  491. CompositePrimitive primitive;
  492. EffectIndex index;
  493. };
  494. class PrimitiveTest : public VibratorTest, public WithParamInterface<PrimitiveParam> {
  495. public:
  496. static auto PrintParam(const TestParamInfo<ParamType> &info) {
  497. return toString(info.param.primitive);
  498. }
  499. };
  500. const std::vector<PrimitiveParam> kPrimitiveParams = {
  501. {CompositePrimitive::CLICK, 2}, {CompositePrimitive::THUD, 4},
  502. {CompositePrimitive::SPIN, 5}, {CompositePrimitive::QUICK_RISE, 6},
  503. {CompositePrimitive::SLOW_RISE, 7}, {CompositePrimitive::QUICK_FALL, 8},
  504. {CompositePrimitive::LIGHT_TICK, 9}, {CompositePrimitive::LOW_TICK, 10},
  505. };
  506. TEST_P(PrimitiveTest, getPrimitiveDuration) {
  507. auto param = GetParam();
  508. auto primitive = param.primitive;
  509. auto index = param.index;
  510. int32_t duration;
  511. EXPECT_EQ(EX_NONE, mVibrator->getPrimitiveDuration(primitive, &duration).getExceptionCode());
  512. EXPECT_EQ(EFFECT_DURATIONS[index], duration);
  513. }
  514. INSTANTIATE_TEST_CASE_P(VibratorTests, PrimitiveTest,
  515. ValuesIn(kPrimitiveParams.begin(), kPrimitiveParams.end()),
  516. PrimitiveTest::PrintParam);
  517. struct ComposeParam {
  518. std::string name;
  519. std::vector<CompositeEffect> composite;
  520. EffectQueue queue;
  521. };
  522. class ComposeTest : public VibratorTest, public WithParamInterface<ComposeParam> {
  523. public:
  524. static auto PrintParam(const TestParamInfo<ParamType> &info) { return info.param.name; }
  525. };
  526. TEST_P(ComposeTest, compose) {
  527. auto param = GetParam();
  528. auto composite = param.composite;
  529. auto queue = std::get<0>(param.queue);
  530. ExpectationSet eSetup;
  531. Expectation eActivate, ePollHaptics, ePollStop, eEraseDone;
  532. auto callback = ndk::SharedRefBase::make<MockVibratorCallback>();
  533. std::promise<void> promise;
  534. std::future<void> future{promise.get_future()};
  535. auto complete = [&promise] {
  536. promise.set_value();
  537. return ndk::ScopedAStatus::ok();
  538. };
  539. eSetup += EXPECT_CALL(*mMockApi, setFFGain(_, ON_GLOBAL_SCALE))
  540. .After(eSetup)
  541. .WillOnce(DoDefault());
  542. eSetup += EXPECT_CALL(*mMockApi, getOwtFreeSpace(_)).WillOnce(DoDefault());
  543. eSetup += EXPECT_CALL(*mMockApi, uploadOwtEffect(_, _, _, _, _, _))
  544. .After(eSetup)
  545. .WillOnce(DoDefault());
  546. eActivate = EXPECT_CALL(*mMockApi, setFFPlay(_, WAVEFORM_COMPOSE, true))
  547. .After(eSetup)
  548. .WillOnce(DoDefault());
  549. ePollHaptics = EXPECT_CALL(*mMockApi, pollVibeState(1, POLLING_TIMEOUT))
  550. .After(eActivate)
  551. .WillOnce(DoDefault());
  552. ePollStop =
  553. EXPECT_CALL(*mMockApi, pollVibeState(0, -1)).After(ePollHaptics).WillOnce(DoDefault());
  554. eEraseDone =
  555. EXPECT_CALL(*mMockApi, eraseOwtEffect(_, _, _)).After(ePollStop).WillOnce(DoDefault());
  556. EXPECT_CALL(*callback, onComplete()).After(eEraseDone).WillOnce(complete);
  557. EXPECT_EQ(EX_NONE, mVibrator->compose(composite, callback).getExceptionCode());
  558. EXPECT_EQ(future.wait_for(std::chrono::milliseconds(100)), std::future_status::ready);
  559. }
  560. const std::vector<ComposeParam> kComposeParams = {
  561. {"click",
  562. {{0, CompositePrimitive::CLICK, 1.0f}},
  563. Queue(QueueEffect(2, Level(1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])), 0)},
  564. {"thud",
  565. {{1, CompositePrimitive::THUD, 0.8f}},
  566. Queue(1, QueueEffect(4, Level(0.8f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])), 0)},
  567. {"spin",
  568. {{2, CompositePrimitive::SPIN, 0.6f}},
  569. Queue(2, QueueEffect(5, Level(0.6f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])), 0)},
  570. {"quick_rise",
  571. {{3, CompositePrimitive::QUICK_RISE, 0.4f}},
  572. Queue(3, QueueEffect(6, Level(0.4f, V_LONG_DEFAULT[0], V_LONG_DEFAULT[1])), 0)},
  573. {"slow_rise",
  574. {{4, CompositePrimitive::SLOW_RISE, 0.0f}},
  575. Queue(4, QueueEffect(7, Level(0.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])), 0)},
  576. {"quick_fall",
  577. {{5, CompositePrimitive::QUICK_FALL, 1.0f}},
  578. Queue(5, QueueEffect(8, Level(1.0f, V_LONG_DEFAULT[0], V_LONG_DEFAULT[1])), 0)},
  579. {"pop",
  580. {{6, CompositePrimitive::SLOW_RISE, 1.0f}, {50, CompositePrimitive::THUD, 1.0f}},
  581. Queue(6, QueueEffect(7, Level(1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])), 50,
  582. QueueEffect(4, Level(1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])), 0)},
  583. {"snap",
  584. {{7, CompositePrimitive::QUICK_RISE, 1.0f}, {0, CompositePrimitive::QUICK_FALL, 1.0f}},
  585. Queue(7, QueueEffect(6, Level(1.0f, V_LONG_DEFAULT[0], V_LONG_DEFAULT[1])),
  586. QueueEffect(8, Level(1.0f, V_LONG_DEFAULT[0], V_LONG_DEFAULT[1])), 0)},
  587. };
  588. INSTANTIATE_TEST_CASE_P(VibratorTests, ComposeTest,
  589. ValuesIn(kComposeParams.begin(), kComposeParams.end()),
  590. ComposeTest::PrintParam);
  591. } // namespace vibrator
  592. } // namespace hardware
  593. } // namespace android
  594. } // namespace aidl