1 /*
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
17 #include <aidl/android/hardware/tests/extension/vibrator/ICustomVibrator.h>
18 #include <aidl/android/hardware/vibrator/IVibrator.h>
19 #include <android/binder_manager.h>
20
21 #include <gtest/gtest.h>
22
23 using aidl::android::hardware::tests::extension::vibrator::Directionality;
24 using aidl::android::hardware::tests::extension::vibrator::ICustomVibrator;
25 using aidl::android::hardware::vibrator::IVibrator;
26 using ndk::SpAIBinder;
27
28 static const std::string kInstance = std::string() + IVibrator::descriptor + "/default";
29
TEST(Ndk,CallRootMethod)30 TEST(Ndk, CallRootMethod) {
31 SpAIBinder vibBinder = SpAIBinder(AServiceManager_getService(kInstance.c_str()));
32 ASSERT_NE(nullptr, vibBinder.get());
33 std::shared_ptr<IVibrator> vib = IVibrator::fromBinder(vibBinder);
34 ASSERT_NE(nullptr, vib.get());
35 ASSERT_TRUE(vib->off().isOk());
36 }
37
TEST(Ndk,CallExtMethod)38 TEST(Ndk, CallExtMethod) {
39 // normally you would want to cache this
40 //
41 SpAIBinder vibBinder = SpAIBinder(AServiceManager_getService(kInstance.c_str()));
42 ASSERT_NE(nullptr, vibBinder.get());
43 std::shared_ptr<IVibrator> vib = IVibrator::fromBinder(vibBinder);
44 ASSERT_NE(nullptr, vib.get());
45
46 // getting the extension
47 SpAIBinder cvibBinder;
48 ASSERT_EQ(STATUS_OK, AIBinder_getExtension(vibBinder.get(), cvibBinder.getR()));
49 ASSERT_NE(nullptr, cvibBinder.get());
50 std::shared_ptr<ICustomVibrator> cvib = ICustomVibrator::fromBinder(cvibBinder);
51 ASSERT_NE(nullptr, cvib.get());
52
53 // calling extension method
54 ASSERT_TRUE(cvib->setDirectionality(Directionality::TRANSVERSE).isOk());
55 }
56