blob: 33bcaf5c9a5fea22e701e113f505a5e02a115262 [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
35const std::vector<Effect> kEffects = {
36 Effect::CLICK, Effect::DOUBLE_CLICK, Effect::TICK, Effect::THUD,
37 Effect::POP, Effect::HEAVY_CLICK, Effect::RINGTONE_1, Effect::RINGTONE_2,
38 Effect::RINGTONE_3, Effect::RINGTONE_4, Effect::RINGTONE_5, Effect::RINGTONE_6,
39 Effect::RINGTONE_7, Effect::RINGTONE_8, Effect::RINGTONE_9, Effect::RINGTONE_10,
40 Effect::RINGTONE_11, Effect::RINGTONE_12, Effect::RINGTONE_13, Effect::RINGTONE_14,
41 Effect::RINGTONE_15, Effect::TEXTURE_TICK};
42
43const std::vector<EffectStrength> kEffectStrengths = {EffectStrength::LIGHT, EffectStrength::MEDIUM,
44 EffectStrength::STRONG};
45
46const std::vector<Effect> kInvalidEffects = {
47 static_cast<Effect>(static_cast<int32_t>(*kEffects.begin()) - 1),
48 static_cast<Effect>(static_cast<int32_t>(*kEffects.end()) + 1),
49};
50
51const std::vector<EffectStrength> kInvalidEffectStrengths = {
52 static_cast<EffectStrength>(static_cast<int8_t>(*kEffectStrengths.begin()) - 1),
53 static_cast<EffectStrength>(static_cast<int8_t>(*kEffectStrengths.end()) + 1),
54};
55
56class CompletionCallback : public BnVibratorCallback {
57 public:
58 CompletionCallback(const std::function<void()>& callback) : mCallback(callback) {}
59 Status onComplete() override {
60 mCallback();
61 return Status::ok();
62 }
63
64 private:
65 std::function<void()> mCallback;
66};
67
68class VibratorAidl : public testing::TestWithParam<std::string> {
69 public:
70 virtual void SetUp() override {
71 vibrator = android::waitForDeclaredService<IVibrator>(String16(GetParam().c_str()));
72 ASSERT_NE(vibrator, nullptr);
73 ASSERT_TRUE(vibrator->getCapabilities(&capabilities).isOk());
74 }
75
76 sp<IVibrator> vibrator;
77 int32_t capabilities;
78};
79
80TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
81 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
82 sleep(1);
83 EXPECT_TRUE(vibrator->off().isOk());
84}
85
86TEST_P(VibratorAidl, OnWithCallback) {
87 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return;
88
89 std::promise<void> completionPromise;
90 std::future<void> completionFuture{completionPromise.get_future()};
91 sp<CompletionCallback> callback =
92 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
93 uint32_t durationMs = 250;
94 std::chrono::milliseconds timeout{durationMs * 2};
95 EXPECT_TRUE(vibrator->on(durationMs, callback).isOk());
96 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
97 EXPECT_TRUE(vibrator->off().isOk());
98}
99
100TEST_P(VibratorAidl, OnCallbackNotSupported) {
101 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) {
102 sp<CompletionCallback> callback = new CompletionCallback([] {});
103 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->on(250, callback).exceptionCode());
104 }
105}
106
107TEST_P(VibratorAidl, ValidateEffect) {
108 for (Effect effect : kEffects) {
109 for (EffectStrength strength : kEffectStrengths) {
110 int32_t lengthMs = 0;
111 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
112 EXPECT_TRUE(status.isOk() ||
113 status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION);
114 if (status.isOk()) {
115 EXPECT_GT(lengthMs, 0);
116 } else {
117 EXPECT_EQ(lengthMs, 0);
118 }
119 }
120 }
121}
122
123TEST_P(VibratorAidl, ValidateEffectWithCallback) {
124 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return;
125
126 for (Effect effect : kEffects) {
127 for (EffectStrength strength : kEffectStrengths) {
128 std::promise<void> completionPromise;
129 std::future<void> completionFuture{completionPromise.get_future()};
130 sp<CompletionCallback> callback =
131 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
132 int lengthMs;
133 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
134 EXPECT_TRUE(status.isOk() ||
135 status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION);
136 if (!status.isOk()) continue;
137
138 std::chrono::milliseconds timeout{lengthMs * 2};
139 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
140 }
141 }
142}
143
144TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
145 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK) return;
146
147 for (Effect effect : kEffects) {
148 for (EffectStrength strength : kEffectStrengths) {
149 sp<CompletionCallback> callback = new CompletionCallback([] {});
150 int lengthMs;
151 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
152 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode());
153 EXPECT_EQ(lengthMs, 0);
154 }
155 }
156}
157
158TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
159 for (Effect effect : kInvalidEffects) {
160 for (EffectStrength strength : kInvalidEffectStrengths) {
161 int32_t lengthMs;
162 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
163 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
164 }
165 }
166}
167
168TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
169 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
170 EXPECT_TRUE(vibrator->setAmplitude(1).isOk());
171 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
172 EXPECT_TRUE(vibrator->setAmplitude(128).isOk());
173 sleep(1);
174 EXPECT_TRUE(vibrator->setAmplitude(255).isOk());
175 sleep(1);
176 }
177}
178
179TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
180 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
181 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode());
182 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode());
183 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(256).exceptionCode());
184 }
185}
186
187TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
188 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
189 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->setAmplitude(1).exceptionCode());
190 }
191}
192
193TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
194 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
195 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
196 sleep(1);
197 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
198 sleep(1);
199 }
200}
201
202TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
203 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
204 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
205 vibrator->setExternalControl(true).exceptionCode());
206 }
207}
208
209INSTANTIATE_TEST_SUITE_P(, VibratorAidl,
210 testing::ValuesIn(android::getAidlHalInstanceNames(IVibrator::descriptor)),
211 android::PrintInstanceNameToString);
212
213int main(int argc, char** argv) {
214 ::testing::InitGoogleTest(&argc, argv);
215 ProcessState::self()->setThreadPoolMaxThreadCount(1);
216 ProcessState::self()->startThreadPool();
217 return RUN_ALL_TESTS();
218}