blob: b6aa9e2fd0060e1f5b6297039aa409de62087d8f [file] [log] [blame]
Steven Morelandd44007e2019-10-24 18:12:46 -07001/*
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#include <aidl/Gtest.h>
17#include <aidl/Vintf.h>
18
19#include <android/hardware/vibrator/BnVibratorCallback.h>
20#include <android/hardware/vibrator/IVibrator.h>
21#include <binder/IServiceManager.h>
22#include <binder/ProcessState.h>
23
24#include <future>
25
26using android::ProcessState;
27using android::sp;
28using android::String16;
29using android::binder::Status;
30using android::hardware::vibrator::BnVibratorCallback;
31using android::hardware::vibrator::Effect;
32using android::hardware::vibrator::EffectStrength;
33using android::hardware::vibrator::IVibrator;
34
Steven Moreland2932b222019-11-05 14:30:17 -080035// TODO(b/143992652): autogenerate
Steven Morelandd44007e2019-10-24 18:12:46 -070036const std::vector<Effect> kEffects = {
37 Effect::CLICK, Effect::DOUBLE_CLICK, Effect::TICK, Effect::THUD,
38 Effect::POP, Effect::HEAVY_CLICK, Effect::RINGTONE_1, Effect::RINGTONE_2,
39 Effect::RINGTONE_3, Effect::RINGTONE_4, Effect::RINGTONE_5, Effect::RINGTONE_6,
40 Effect::RINGTONE_7, Effect::RINGTONE_8, Effect::RINGTONE_9, Effect::RINGTONE_10,
41 Effect::RINGTONE_11, Effect::RINGTONE_12, Effect::RINGTONE_13, Effect::RINGTONE_14,
42 Effect::RINGTONE_15, Effect::TEXTURE_TICK};
43
Steven Moreland2932b222019-11-05 14:30:17 -080044// TODO(b/143992652): autogenerate
Steven Morelandd44007e2019-10-24 18:12:46 -070045const std::vector<EffectStrength> kEffectStrengths = {EffectStrength::LIGHT, EffectStrength::MEDIUM,
46 EffectStrength::STRONG};
47
48const std::vector<Effect> kInvalidEffects = {
Steven Morelandf3353882019-11-07 17:02:43 -080049 static_cast<Effect>(static_cast<int32_t>(kEffects.front()) - 1),
50 static_cast<Effect>(static_cast<int32_t>(kEffects.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070051};
52
53const std::vector<EffectStrength> kInvalidEffectStrengths = {
Steven Morelandf3353882019-11-07 17:02:43 -080054 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.front()) - 1),
55 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070056};
57
58class CompletionCallback : public BnVibratorCallback {
59 public:
60 CompletionCallback(const std::function<void()>& callback) : mCallback(callback) {}
61 Status onComplete() override {
62 mCallback();
63 return Status::ok();
64 }
65
66 private:
67 std::function<void()> mCallback;
68};
69
70class VibratorAidl : public testing::TestWithParam<std::string> {
71 public:
72 virtual void SetUp() override {
73 vibrator = android::waitForDeclaredService<IVibrator>(String16(GetParam().c_str()));
74 ASSERT_NE(vibrator, nullptr);
75 ASSERT_TRUE(vibrator->getCapabilities(&capabilities).isOk());
76 }
77
78 sp<IVibrator> vibrator;
79 int32_t capabilities;
80};
81
82TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
83 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
84 sleep(1);
85 EXPECT_TRUE(vibrator->off().isOk());
86}
87
88TEST_P(VibratorAidl, OnWithCallback) {
89 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return;
90
91 std::promise<void> completionPromise;
92 std::future<void> completionFuture{completionPromise.get_future()};
93 sp<CompletionCallback> callback =
94 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
95 uint32_t durationMs = 250;
96 std::chrono::milliseconds timeout{durationMs * 2};
97 EXPECT_TRUE(vibrator->on(durationMs, callback).isOk());
98 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
99 EXPECT_TRUE(vibrator->off().isOk());
100}
101
102TEST_P(VibratorAidl, OnCallbackNotSupported) {
103 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) {
104 sp<CompletionCallback> callback = new CompletionCallback([] {});
105 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->on(250, callback).exceptionCode());
106 }
107}
108
109TEST_P(VibratorAidl, ValidateEffect) {
Steven Moreland2932b222019-11-05 14:30:17 -0800110 std::vector<Effect> supported;
111 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
112
Steven Morelandd44007e2019-10-24 18:12:46 -0700113 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800114 bool isEffectSupported =
115 std::find(supported.begin(), supported.end(), effect) != supported.end();
116
Steven Morelandd44007e2019-10-24 18:12:46 -0700117 for (EffectStrength strength : kEffectStrengths) {
118 int32_t lengthMs = 0;
119 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800120
121 if (isEffectSupported) {
Steven Morelandf3353882019-11-07 17:02:43 -0800122 EXPECT_TRUE(status.isOk())
123 << static_cast<int>(effect) << " " << static_cast<int>(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700124 EXPECT_GT(lengthMs, 0);
Steven Morelandf3353882019-11-07 17:02:43 -0800125 usleep(lengthMs * 1000);
Steven Morelandd44007e2019-10-24 18:12:46 -0700126 } else {
Steven Morelandf3353882019-11-07 17:02:43 -0800127 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
128 << static_cast<int>(effect) << " " << static_cast<int>(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700129 EXPECT_EQ(lengthMs, 0);
130 }
131 }
132 }
133}
134
135TEST_P(VibratorAidl, ValidateEffectWithCallback) {
136 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return;
137
Steven Moreland2932b222019-11-05 14:30:17 -0800138 std::vector<Effect> supported;
139 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
140
Steven Morelandd44007e2019-10-24 18:12:46 -0700141 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800142 bool isEffectSupported =
143 std::find(supported.begin(), supported.end(), effect) != supported.end();
144
Steven Morelandd44007e2019-10-24 18:12:46 -0700145 for (EffectStrength strength : kEffectStrengths) {
146 std::promise<void> completionPromise;
147 std::future<void> completionFuture{completionPromise.get_future()};
148 sp<CompletionCallback> callback =
149 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800150 int lengthMs = 0;
Steven Morelandd44007e2019-10-24 18:12:46 -0700151 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800152
153 if (isEffectSupported) {
154 EXPECT_TRUE(status.isOk());
155 EXPECT_GT(lengthMs, 0);
156 } else {
157 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
158 EXPECT_EQ(lengthMs, 0);
159 }
160
Steven Morelandd44007e2019-10-24 18:12:46 -0700161 if (!status.isOk()) continue;
162
163 std::chrono::milliseconds timeout{lengthMs * 2};
164 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
165 }
166 }
167}
168
169TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
170 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK) return;
171
172 for (Effect effect : kEffects) {
173 for (EffectStrength strength : kEffectStrengths) {
174 sp<CompletionCallback> callback = new CompletionCallback([] {});
175 int lengthMs;
176 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
177 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode());
178 EXPECT_EQ(lengthMs, 0);
179 }
180 }
181}
182
183TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
184 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800185 for (EffectStrength strength : kEffectStrengths) {
186 int32_t lengthMs;
187 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
188 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
189 << static_cast<int>(effect) << " " << static_cast<int>(strength);
190 }
191 }
192 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700193 for (EffectStrength strength : kInvalidEffectStrengths) {
194 int32_t lengthMs;
195 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Morelandf3353882019-11-07 17:02:43 -0800196 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
197 << static_cast<int>(effect) << " " << static_cast<int>(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700198 }
199 }
200}
201
202TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
203 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
204 EXPECT_TRUE(vibrator->setAmplitude(1).isOk());
205 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
206 EXPECT_TRUE(vibrator->setAmplitude(128).isOk());
207 sleep(1);
208 EXPECT_TRUE(vibrator->setAmplitude(255).isOk());
209 sleep(1);
210 }
211}
212
213TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
214 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
215 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode());
216 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode());
217 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(256).exceptionCode());
218 }
219}
220
221TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
222 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
223 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->setAmplitude(1).exceptionCode());
224 }
225}
226
227TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
228 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
229 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
230 sleep(1);
231 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
232 sleep(1);
233 }
234}
235
Steven Morelandc0b92d52019-11-05 13:31:41 -0800236TEST_P(VibratorAidl, ExternalAmplitudeControl) {
237 const bool supportsExternalAmplitudeControl =
238 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
239
240 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
241 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
242
243 Status amplitudeStatus = vibrator->setAmplitude(128);
244 if (supportsExternalAmplitudeControl) {
245 EXPECT_TRUE(amplitudeStatus.isOk());
246 } else {
247 EXPECT_EQ(amplitudeStatus.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
248 }
249 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
250 } else {
251 EXPECT_FALSE(supportsExternalAmplitudeControl);
252 }
253}
254
Steven Morelandd44007e2019-10-24 18:12:46 -0700255TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
256 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
257 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
258 vibrator->setExternalControl(true).exceptionCode());
259 }
260}
261
Haibo Huang83b4c1e2019-11-08 11:59:40 -0800262INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl,
Steven Morelandd44007e2019-10-24 18:12:46 -0700263 testing::ValuesIn(android::getAidlHalInstanceNames(IVibrator::descriptor)),
264 android::PrintInstanceNameToString);
265
266int main(int argc, char** argv) {
267 ::testing::InitGoogleTest(&argc, argv);
268 ProcessState::self()->setThreadPoolMaxThreadCount(1);
269 ProcessState::self()->startThreadPool();
270 return RUN_ALL_TESTS();
271}