1 /*
2  * Copyright (C) 2023 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/Gtest.h>
18 #include <aidl/Vintf.h>
19 #include <aidl/android/hardware/bluetooth/finder/IBluetoothFinder.h>
20 #include <android-base/logging.h>
21 #include <android/binder_manager.h>
22 #include <android/binder_process.h>
23 #include <binder/IServiceManager.h>
24 #include <utils/Log.h>
25 
26 #include <array>
27 #include <vector>
28 
29 using ::aidl::android::hardware::bluetooth::finder::Eid;
30 using ::aidl::android::hardware::bluetooth::finder::IBluetoothFinder;
31 using ::ndk::ScopedAStatus;
32 
33 class BluetoothFinderTest : public ::testing::TestWithParam<std::string> {
34  public:
SetUp()35   virtual void SetUp() override {
36     ALOGI("SetUp Finder Test");
37     bluetooth_finder = IBluetoothFinder::fromBinder(
38         ndk::SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
39     ASSERT_NE(bluetooth_finder, nullptr);
40   }
41 
TearDown()42   virtual void TearDown() override {
43     ALOGI("TearDown Finder Test");
44     bluetooth_finder = nullptr;
45     ASSERT_EQ(bluetooth_finder, nullptr);
46   }
47 
48   ScopedAStatus sendEids(uint8_t num);
49   ScopedAStatus setPoweredOffFinderMode(bool enable);
50   ScopedAStatus getPoweredOffFinderMode(bool* status);
51 
52  private:
53   std::shared_ptr<IBluetoothFinder> bluetooth_finder;
54 };
55 
sendEids(uint8_t numKeys)56 ScopedAStatus BluetoothFinderTest::sendEids(uint8_t numKeys) {
57   std::vector<Eid> keys(numKeys);
58   for (uint_t i = 0; i < numKeys; i++) {
59     std::array<uint8_t, 20> key;
60     key.fill(i + 1);
61     keys[i].bytes = key;
62   }
63   return bluetooth_finder->sendEids(keys);
64 }
65 
setPoweredOffFinderMode(bool enable)66 ScopedAStatus BluetoothFinderTest::setPoweredOffFinderMode(bool enable) {
67   return bluetooth_finder->setPoweredOffFinderMode(enable);
68 }
69 
getPoweredOffFinderMode(bool * status)70 ScopedAStatus BluetoothFinderTest::getPoweredOffFinderMode(bool* status) {
71   return bluetooth_finder->getPoweredOffFinderMode(status);
72 }
73 
TEST_P(BluetoothFinderTest,SendEidsSingle)74 TEST_P(BluetoothFinderTest, SendEidsSingle) {
75   ScopedAStatus status = sendEids(1);
76   ASSERT_TRUE(status.isOk());
77 }
78 
TEST_P(BluetoothFinderTest,Send255Eids)79 TEST_P(BluetoothFinderTest, Send255Eids) {
80   ScopedAStatus status = sendEids(255);
81   ASSERT_TRUE(status.isOk());
82 }
83 
TEST_P(BluetoothFinderTest,setAndGetPoweredOffFinderModeEnable)84 TEST_P(BluetoothFinderTest, setAndGetPoweredOffFinderModeEnable) {
85   ScopedAStatus status = setPoweredOffFinderMode(true);
86   ASSERT_TRUE(status.isOk());
87   bool pof_status;
88   status = getPoweredOffFinderMode(&pof_status);
89   ASSERT_TRUE(status.isOk());
90   ASSERT_TRUE(pof_status);
91 }
92 
TEST_P(BluetoothFinderTest,setAndGetPoweredOffFinderModeDisable)93 TEST_P(BluetoothFinderTest, setAndGetPoweredOffFinderModeDisable) {
94   ScopedAStatus status = setPoweredOffFinderMode(false);
95   ASSERT_TRUE(status.isOk());
96   bool pof_status;
97   status = getPoweredOffFinderMode(&pof_status);
98   ASSERT_TRUE(status.isOk());
99   ASSERT_TRUE(!pof_status);
100 }
101 
102 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothFinderTest);
103 INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothFinderTest,
104                          testing::ValuesIn(android::getAidlHalInstanceNames(
105                              IBluetoothFinder::descriptor)),
106                          android::PrintInstanceNameToString);
107 
main(int argc,char ** argv)108 int main(int argc, char** argv) {
109   ::testing::InitGoogleTest(&argc, argv);
110   ABinderProcess_startThreadPool();
111   int status = RUN_ALL_TESTS();
112   ALOGI("Test result = %d", status);
113   return status;
114 }
115