1 /*
2  * Copyright (C) 2020 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 <android/hardware/vibrator/IVibratorManager.h>
22 #include <binder/IServiceManager.h>
23 #include <binder/ProcessState.h>
24 
25 #include <cmath>
26 #include <future>
27 
28 using android::ProcessState;
29 using android::sp;
30 using android::String16;
31 using android::binder::Status;
32 using android::hardware::vibrator::BnVibratorCallback;
33 using android::hardware::vibrator::CompositeEffect;
34 using android::hardware::vibrator::CompositePrimitive;
35 using android::hardware::vibrator::Effect;
36 using android::hardware::vibrator::EffectStrength;
37 using android::hardware::vibrator::IVibrator;
38 using android::hardware::vibrator::IVibratorManager;
39 using std::chrono::high_resolution_clock;
40 
41 const std::vector<Effect> kEffects{android::enum_range<Effect>().begin(),
42                                    android::enum_range<Effect>().end()};
43 const std::vector<EffectStrength> kEffectStrengths{android::enum_range<EffectStrength>().begin(),
44                                                    android::enum_range<EffectStrength>().end()};
45 const std::vector<CompositePrimitive> kPrimitives{android::enum_range<CompositePrimitive>().begin(),
46                                                   android::enum_range<CompositePrimitive>().end()};
47 
48 class CompletionCallback : public BnVibratorCallback {
49   public:
CompletionCallback(const std::function<void ()> & callback)50     CompletionCallback(const std::function<void()>& callback) : mCallback(callback) {}
onComplete()51     Status onComplete() override {
52         mCallback();
53         return Status::ok();
54     }
55 
56   private:
57     std::function<void()> mCallback;
58 };
59 
60 class VibratorAidl : public testing::TestWithParam<std::string> {
61   public:
SetUp()62     virtual void SetUp() override {
63         manager = android::waitForDeclaredService<IVibratorManager>(String16(GetParam().c_str()));
64         ASSERT_NE(manager, nullptr);
65         ASSERT_TRUE(manager->getCapabilities(&capabilities).isOk());
66         EXPECT_TRUE(manager->getVibratorIds(&vibratorIds).isOk());
67     }
68 
69     sp<IVibratorManager> manager;
70     int32_t capabilities;
71     std::vector<int32_t> vibratorIds;
72 };
73 
isUnknownOrUnsupported(Status status)74 inline bool isUnknownOrUnsupported(Status status) {
75     return status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
76            status.transactionError() == android::UNKNOWN_TRANSACTION;
77 }
78 
TEST_P(VibratorAidl,ValidateExistingVibrators)79 TEST_P(VibratorAidl, ValidateExistingVibrators) {
80     sp<IVibrator> vibrator;
81     for (auto& id : vibratorIds) {
82         EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk());
83         ASSERT_NE(vibrator, nullptr);
84     }
85 }
86 
TEST_P(VibratorAidl,GetVibratorWithInvalidId)87 TEST_P(VibratorAidl, GetVibratorWithInvalidId) {
88     int32_t invalidId = *max_element(vibratorIds.begin(), vibratorIds.end()) + 1;
89     sp<IVibrator> vibrator;
90     EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
91               manager->getVibrator(invalidId, &vibrator).exceptionCode());
92     ASSERT_EQ(vibrator, nullptr);
93 }
94 
TEST_P(VibratorAidl,ValidatePrepareSyncedExistingVibrators)95 TEST_P(VibratorAidl, ValidatePrepareSyncedExistingVibrators) {
96     if (!(capabilities & IVibratorManager::CAP_SYNC)) return;
97     if (vibratorIds.empty()) return;
98     EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk());
99 }
100 
TEST_P(VibratorAidl,PrepareSyncedEmptySetIsInvalid)101 TEST_P(VibratorAidl, PrepareSyncedEmptySetIsInvalid) {
102     if (!(capabilities & IVibratorManager::CAP_SYNC)) return;
103     std::vector<int32_t> emptyIds;
104     EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, manager->prepareSynced(emptyIds).exceptionCode());
105 }
106 
TEST_P(VibratorAidl,PrepareSyncedNotSupported)107 TEST_P(VibratorAidl, PrepareSyncedNotSupported) {
108     if (!(capabilities & IVibratorManager::CAP_SYNC)) {
109         Status status = manager->prepareSynced(vibratorIds);
110         EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
111     }
112 }
113 
TEST_P(VibratorAidl,PrepareOnNotSupported)114 TEST_P(VibratorAidl, PrepareOnNotSupported) {
115     if (vibratorIds.empty()) return;
116     if (!(capabilities & IVibratorManager::CAP_SYNC)) return;
117     if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) {
118         uint32_t durationMs = 250;
119         EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk());
120         sp<IVibrator> vibrator;
121         for (auto& id : vibratorIds) {
122             EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk());
123             ASSERT_NE(vibrator, nullptr);
124             Status status = vibrator->on(durationMs, nullptr);
125             EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
126         }
127         EXPECT_TRUE(manager->cancelSynced().isOk());
128     }
129 }
130 
TEST_P(VibratorAidl,PreparePerformNotSupported)131 TEST_P(VibratorAidl, PreparePerformNotSupported) {
132     if (vibratorIds.empty()) return;
133     if (!(capabilities & IVibratorManager::CAP_SYNC)) return;
134     if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) {
135         EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk());
136         sp<IVibrator> vibrator;
137         for (auto& id : vibratorIds) {
138             EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk());
139             ASSERT_NE(vibrator, nullptr);
140             int32_t lengthMs = 0;
141             Status status = vibrator->perform(kEffects[0], kEffectStrengths[0], nullptr, &lengthMs);
142             EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
143         }
144         EXPECT_TRUE(manager->cancelSynced().isOk());
145     }
146 }
147 
TEST_P(VibratorAidl,PrepareComposeNotSupported)148 TEST_P(VibratorAidl, PrepareComposeNotSupported) {
149     if (vibratorIds.empty()) return;
150     if (!(capabilities & IVibratorManager::CAP_SYNC)) return;
151     if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) {
152         std::vector<CompositeEffect> composite;
153         CompositeEffect effect;
154         effect.delayMs = 10;
155         effect.primitive = kPrimitives[0];
156         effect.scale = 1.0f;
157         composite.emplace_back(effect);
158 
159         EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk());
160         sp<IVibrator> vibrator;
161         for (auto& id : vibratorIds) {
162             EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk());
163             ASSERT_NE(vibrator, nullptr);
164             Status status = vibrator->compose(composite, nullptr);
165             EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
166         }
167         EXPECT_TRUE(manager->cancelSynced().isOk());
168     }
169 }
170 
TEST_P(VibratorAidl,TriggerWithCallback)171 TEST_P(VibratorAidl, TriggerWithCallback) {
172     if (!(capabilities & IVibratorManager::CAP_SYNC)) return;
173     if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) return;
174     if (!(capabilities & IVibratorManager::CAP_TRIGGER_CALLBACK)) return;
175     if (vibratorIds.empty()) return;
176 
177     std::promise<void> completionPromise;
178     std::future<void> completionFuture{completionPromise.get_future()};
179     sp<CompletionCallback> callback =
180             new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
181     uint32_t durationMs = 250;
182     std::chrono::milliseconds timeout{durationMs * 2};
183 
184     EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk());
185     sp<IVibrator> vibrator;
186     for (auto& id : vibratorIds) {
187         EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk());
188         ASSERT_NE(vibrator, nullptr);
189         EXPECT_TRUE(vibrator->on(durationMs, nullptr).isOk());
190     }
191 
192     EXPECT_TRUE(manager->triggerSynced(callback).isOk());
193     EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
194     EXPECT_TRUE(manager->cancelSynced().isOk());
195 }
196 
TEST_P(VibratorAidl,TriggerSyncNotSupported)197 TEST_P(VibratorAidl, TriggerSyncNotSupported) {
198     if (!(capabilities & IVibratorManager::CAP_SYNC)) {
199         Status status = manager->triggerSynced(nullptr);
200         EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
201     }
202 }
203 
TEST_P(VibratorAidl,TriggerCallbackNotSupported)204 TEST_P(VibratorAidl, TriggerCallbackNotSupported) {
205     if (!(capabilities & IVibratorManager::CAP_SYNC)) return;
206     if (!(capabilities & IVibratorManager::CAP_TRIGGER_CALLBACK)) {
207         sp<CompletionCallback> callback = new CompletionCallback([] {});
208         EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk());
209         Status status = manager->triggerSynced(callback);
210         EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
211     }
212 }
213 
214 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
215 INSTANTIATE_TEST_SUITE_P(
216         Vibrator, VibratorAidl,
217         testing::ValuesIn(android::getAidlHalInstanceNames(IVibratorManager::descriptor)),
218         android::PrintInstanceNameToString);
219 
main(int argc,char ** argv)220 int main(int argc, char** argv) {
221     ::testing::InitGoogleTest(&argc, argv);
222     ProcessState::self()->setThreadPoolMaxThreadCount(1);
223     ProcessState::self()->startThreadPool();
224     return RUN_ALL_TESTS();
225 }
226