• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     EXPECT_TRUE(manager->cancelSynced().isOk());
100 }
101 
TEST_P(VibratorAidl,PrepareSyncedEmptySetIsInvalid)102 TEST_P(VibratorAidl, PrepareSyncedEmptySetIsInvalid) {
103     if (!(capabilities & IVibratorManager::CAP_SYNC)) return;
104     std::vector<int32_t> emptyIds;
105     EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, manager->prepareSynced(emptyIds).exceptionCode());
106 }
107 
TEST_P(VibratorAidl,PrepareSyncedNotSupported)108 TEST_P(VibratorAidl, PrepareSyncedNotSupported) {
109     if (!(capabilities & IVibratorManager::CAP_SYNC)) {
110         Status status = manager->prepareSynced(vibratorIds);
111         EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
112     }
113 }
114 
TEST_P(VibratorAidl,PrepareOnNotSupported)115 TEST_P(VibratorAidl, PrepareOnNotSupported) {
116     if (vibratorIds.empty()) return;
117     if (!(capabilities & IVibratorManager::CAP_SYNC)) return;
118     if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) {
119         uint32_t durationMs = 250;
120         EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk());
121         sp<IVibrator> vibrator;
122         for (auto& id : vibratorIds) {
123             EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk());
124             ASSERT_NE(vibrator, nullptr);
125             Status status = vibrator->on(durationMs, nullptr);
126             EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
127         }
128         EXPECT_TRUE(manager->cancelSynced().isOk());
129     }
130 }
131 
TEST_P(VibratorAidl,PreparePerformNotSupported)132 TEST_P(VibratorAidl, PreparePerformNotSupported) {
133     if (vibratorIds.empty()) return;
134     if (!(capabilities & IVibratorManager::CAP_SYNC)) return;
135     if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) {
136         EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk());
137         sp<IVibrator> vibrator;
138         for (auto& id : vibratorIds) {
139             EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk());
140             ASSERT_NE(vibrator, nullptr);
141             int32_t lengthMs = 0;
142             Status status = vibrator->perform(kEffects[0], kEffectStrengths[0], nullptr, &lengthMs);
143             EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
144         }
145         EXPECT_TRUE(manager->cancelSynced().isOk());
146     }
147 }
148 
TEST_P(VibratorAidl,PrepareComposeNotSupported)149 TEST_P(VibratorAidl, PrepareComposeNotSupported) {
150     if (vibratorIds.empty()) return;
151     if (!(capabilities & IVibratorManager::CAP_SYNC)) return;
152     if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) {
153         std::vector<CompositeEffect> composite;
154         CompositeEffect effect;
155         effect.delayMs = 10;
156         effect.primitive = kPrimitives[0];
157         effect.scale = 1.0f;
158         composite.emplace_back(effect);
159 
160         EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk());
161         sp<IVibrator> vibrator;
162         for (auto& id : vibratorIds) {
163             EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk());
164             ASSERT_NE(vibrator, nullptr);
165             Status status = vibrator->compose(composite, nullptr);
166             EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
167         }
168         EXPECT_TRUE(manager->cancelSynced().isOk());
169     }
170 }
171 
TEST_P(VibratorAidl,TriggerWithCallback)172 TEST_P(VibratorAidl, TriggerWithCallback) {
173     if (!(capabilities & IVibratorManager::CAP_SYNC)) return;
174     if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) return;
175     if (!(capabilities & IVibratorManager::CAP_TRIGGER_CALLBACK)) return;
176     if (vibratorIds.empty()) return;
177 
178     std::promise<void> completionPromise;
179     std::future<void> completionFuture{completionPromise.get_future()};
180     sp<CompletionCallback> callback =
181             new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
182     uint32_t durationMs = 250;
183     std::chrono::milliseconds timeout{durationMs * 2};
184 
185     EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk());
186     sp<IVibrator> vibrator;
187     for (auto& id : vibratorIds) {
188         EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk());
189         ASSERT_NE(vibrator, nullptr);
190         EXPECT_TRUE(vibrator->on(durationMs, nullptr).isOk());
191     }
192 
193     EXPECT_TRUE(manager->triggerSynced(callback).isOk());
194     EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
195     EXPECT_TRUE(manager->cancelSynced().isOk());
196 }
197 
TEST_P(VibratorAidl,TriggerSyncNotSupported)198 TEST_P(VibratorAidl, TriggerSyncNotSupported) {
199     if (!(capabilities & IVibratorManager::CAP_SYNC)) {
200         Status status = manager->triggerSynced(nullptr);
201         EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
202     }
203 }
204 
TEST_P(VibratorAidl,TriggerCallbackNotSupported)205 TEST_P(VibratorAidl, TriggerCallbackNotSupported) {
206     if (!(capabilities & IVibratorManager::CAP_SYNC)) return;
207     if (!(capabilities & IVibratorManager::CAP_TRIGGER_CALLBACK)) {
208         sp<CompletionCallback> callback = new CompletionCallback([] {});
209         EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk());
210         Status status = manager->triggerSynced(callback);
211         EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
212         EXPECT_TRUE(manager->cancelSynced().isOk());
213     }
214 }
215 
216 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
217 INSTANTIATE_TEST_SUITE_P(
218         Vibrator, VibratorAidl,
219         testing::ValuesIn(android::getAidlHalInstanceNames(IVibratorManager::descriptor)),
220         android::PrintInstanceNameToString);
221 
main(int argc,char ** argv)222 int main(int argc, char** argv) {
223     ::testing::InitGoogleTest(&argc, argv);
224     ProcessState::self()->setThreadPoolMaxThreadCount(1);
225     ProcessState::self()->startThreadPool();
226     return RUN_ALL_TESTS();
227 }
228