blob: d2977c668f99d93fccc9fdda623f2e3448ca9414 [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 = {
49 static_cast<Effect>(static_cast<int32_t>(*kEffects.begin()) - 1),
50 static_cast<Effect>(static_cast<int32_t>(*kEffects.end()) + 1),
51};
52
53const std::vector<EffectStrength> kInvalidEffectStrengths = {
54 static_cast<EffectStrength>(static_cast<int8_t>(*kEffectStrengths.begin()) - 1),
55 static_cast<EffectStrength>(static_cast<int8_t>(*kEffectStrengths.end()) + 1),
56};
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) {
122 EXPECT_TRUE(status.isOk());
Steven Morelandd44007e2019-10-24 18:12:46 -0700123 EXPECT_GT(lengthMs, 0);
124 } else {
Steven Moreland2932b222019-11-05 14:30:17 -0800125 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
Steven Morelandd44007e2019-10-24 18:12:46 -0700126 EXPECT_EQ(lengthMs, 0);
127 }
128 }
129 }
130}
131
132TEST_P(VibratorAidl, ValidateEffectWithCallback) {
133 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return;
134
Steven Moreland2932b222019-11-05 14:30:17 -0800135 std::vector<Effect> supported;
136 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
137
Steven Morelandd44007e2019-10-24 18:12:46 -0700138 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800139 bool isEffectSupported =
140 std::find(supported.begin(), supported.end(), effect) != supported.end();
141
Steven Morelandd44007e2019-10-24 18:12:46 -0700142 for (EffectStrength strength : kEffectStrengths) {
143 std::promise<void> completionPromise;
144 std::future<void> completionFuture{completionPromise.get_future()};
145 sp<CompletionCallback> callback =
146 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800147 int lengthMs = 0;
Steven Morelandd44007e2019-10-24 18:12:46 -0700148 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800149
150 if (isEffectSupported) {
151 EXPECT_TRUE(status.isOk());
152 EXPECT_GT(lengthMs, 0);
153 } else {
154 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
155 EXPECT_EQ(lengthMs, 0);
156 }
157
Steven Morelandd44007e2019-10-24 18:12:46 -0700158 if (!status.isOk()) continue;
159
160 std::chrono::milliseconds timeout{lengthMs * 2};
161 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
162 }
163 }
164}
165
166TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
167 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK) return;
168
169 for (Effect effect : kEffects) {
170 for (EffectStrength strength : kEffectStrengths) {
171 sp<CompletionCallback> callback = new CompletionCallback([] {});
172 int lengthMs;
173 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
174 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode());
175 EXPECT_EQ(lengthMs, 0);
176 }
177 }
178}
179
180TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
181 for (Effect effect : kInvalidEffects) {
182 for (EffectStrength strength : kInvalidEffectStrengths) {
183 int32_t lengthMs;
184 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
185 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
186 }
187 }
188}
189
190TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
191 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
192 EXPECT_TRUE(vibrator->setAmplitude(1).isOk());
193 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
194 EXPECT_TRUE(vibrator->setAmplitude(128).isOk());
195 sleep(1);
196 EXPECT_TRUE(vibrator->setAmplitude(255).isOk());
197 sleep(1);
198 }
199}
200
201TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
202 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
203 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode());
204 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode());
205 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(256).exceptionCode());
206 }
207}
208
209TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
210 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
211 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->setAmplitude(1).exceptionCode());
212 }
213}
214
215TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
216 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
217 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
218 sleep(1);
219 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
220 sleep(1);
221 }
222}
223
224TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
225 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
226 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
227 vibrator->setExternalControl(true).exceptionCode());
228 }
229}
230
Haibo Huang83b4c1e2019-11-08 11:59:40 -0800231INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl,
Steven Morelandd44007e2019-10-24 18:12:46 -0700232 testing::ValuesIn(android::getAidlHalInstanceNames(IVibrator::descriptor)),
233 android::PrintInstanceNameToString);
234
235int main(int argc, char** argv) {
236 ::testing::InitGoogleTest(&argc, argv);
237 ProcessState::self()->setThreadPoolMaxThreadCount(1);
238 ProcessState::self()->startThreadPool();
239 return RUN_ALL_TESTS();
240}