1*4d7e907cSAndroid Build Coastguard Worker /*
2*4d7e907cSAndroid Build Coastguard Worker * Copyright (C) 2023 The Android Open Source Project
3*4d7e907cSAndroid Build Coastguard Worker *
4*4d7e907cSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*4d7e907cSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*4d7e907cSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*4d7e907cSAndroid Build Coastguard Worker *
8*4d7e907cSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*4d7e907cSAndroid Build Coastguard Worker *
10*4d7e907cSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*4d7e907cSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*4d7e907cSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4d7e907cSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*4d7e907cSAndroid Build Coastguard Worker * limitations under the License.
15*4d7e907cSAndroid Build Coastguard Worker */
16*4d7e907cSAndroid Build Coastguard Worker
17*4d7e907cSAndroid Build Coastguard Worker #include <aidl/Gtest.h>
18*4d7e907cSAndroid Build Coastguard Worker #include <aidl/Vintf.h>
19*4d7e907cSAndroid Build Coastguard Worker #include <aidl/android/hardware/bluetooth/finder/IBluetoothFinder.h>
20*4d7e907cSAndroid Build Coastguard Worker #include <android-base/logging.h>
21*4d7e907cSAndroid Build Coastguard Worker #include <android/binder_manager.h>
22*4d7e907cSAndroid Build Coastguard Worker #include <android/binder_process.h>
23*4d7e907cSAndroid Build Coastguard Worker #include <binder/IServiceManager.h>
24*4d7e907cSAndroid Build Coastguard Worker #include <utils/Log.h>
25*4d7e907cSAndroid Build Coastguard Worker
26*4d7e907cSAndroid Build Coastguard Worker #include <array>
27*4d7e907cSAndroid Build Coastguard Worker #include <vector>
28*4d7e907cSAndroid Build Coastguard Worker
29*4d7e907cSAndroid Build Coastguard Worker using ::aidl::android::hardware::bluetooth::finder::Eid;
30*4d7e907cSAndroid Build Coastguard Worker using ::aidl::android::hardware::bluetooth::finder::IBluetoothFinder;
31*4d7e907cSAndroid Build Coastguard Worker using ::ndk::ScopedAStatus;
32*4d7e907cSAndroid Build Coastguard Worker
33*4d7e907cSAndroid Build Coastguard Worker class BluetoothFinderTest : public ::testing::TestWithParam<std::string> {
34*4d7e907cSAndroid Build Coastguard Worker public:
SetUp()35*4d7e907cSAndroid Build Coastguard Worker virtual void SetUp() override {
36*4d7e907cSAndroid Build Coastguard Worker ALOGI("SetUp Finder Test");
37*4d7e907cSAndroid Build Coastguard Worker bluetooth_finder = IBluetoothFinder::fromBinder(
38*4d7e907cSAndroid Build Coastguard Worker ndk::SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
39*4d7e907cSAndroid Build Coastguard Worker ASSERT_NE(bluetooth_finder, nullptr);
40*4d7e907cSAndroid Build Coastguard Worker }
41*4d7e907cSAndroid Build Coastguard Worker
TearDown()42*4d7e907cSAndroid Build Coastguard Worker virtual void TearDown() override {
43*4d7e907cSAndroid Build Coastguard Worker ALOGI("TearDown Finder Test");
44*4d7e907cSAndroid Build Coastguard Worker bluetooth_finder = nullptr;
45*4d7e907cSAndroid Build Coastguard Worker ASSERT_EQ(bluetooth_finder, nullptr);
46*4d7e907cSAndroid Build Coastguard Worker }
47*4d7e907cSAndroid Build Coastguard Worker
48*4d7e907cSAndroid Build Coastguard Worker ScopedAStatus sendEids(uint8_t num);
49*4d7e907cSAndroid Build Coastguard Worker ScopedAStatus setPoweredOffFinderMode(bool enable);
50*4d7e907cSAndroid Build Coastguard Worker ScopedAStatus getPoweredOffFinderMode(bool* status);
51*4d7e907cSAndroid Build Coastguard Worker
52*4d7e907cSAndroid Build Coastguard Worker private:
53*4d7e907cSAndroid Build Coastguard Worker std::shared_ptr<IBluetoothFinder> bluetooth_finder;
54*4d7e907cSAndroid Build Coastguard Worker };
55*4d7e907cSAndroid Build Coastguard Worker
sendEids(uint8_t numKeys)56*4d7e907cSAndroid Build Coastguard Worker ScopedAStatus BluetoothFinderTest::sendEids(uint8_t numKeys) {
57*4d7e907cSAndroid Build Coastguard Worker std::vector<Eid> keys(numKeys);
58*4d7e907cSAndroid Build Coastguard Worker for (uint_t i = 0; i < numKeys; i++) {
59*4d7e907cSAndroid Build Coastguard Worker std::array<uint8_t, 20> key;
60*4d7e907cSAndroid Build Coastguard Worker key.fill(i + 1);
61*4d7e907cSAndroid Build Coastguard Worker keys[i].bytes = key;
62*4d7e907cSAndroid Build Coastguard Worker }
63*4d7e907cSAndroid Build Coastguard Worker return bluetooth_finder->sendEids(keys);
64*4d7e907cSAndroid Build Coastguard Worker }
65*4d7e907cSAndroid Build Coastguard Worker
setPoweredOffFinderMode(bool enable)66*4d7e907cSAndroid Build Coastguard Worker ScopedAStatus BluetoothFinderTest::setPoweredOffFinderMode(bool enable) {
67*4d7e907cSAndroid Build Coastguard Worker return bluetooth_finder->setPoweredOffFinderMode(enable);
68*4d7e907cSAndroid Build Coastguard Worker }
69*4d7e907cSAndroid Build Coastguard Worker
getPoweredOffFinderMode(bool * status)70*4d7e907cSAndroid Build Coastguard Worker ScopedAStatus BluetoothFinderTest::getPoweredOffFinderMode(bool* status) {
71*4d7e907cSAndroid Build Coastguard Worker return bluetooth_finder->getPoweredOffFinderMode(status);
72*4d7e907cSAndroid Build Coastguard Worker }
73*4d7e907cSAndroid Build Coastguard Worker
TEST_P(BluetoothFinderTest,SendEidsSingle)74*4d7e907cSAndroid Build Coastguard Worker TEST_P(BluetoothFinderTest, SendEidsSingle) {
75*4d7e907cSAndroid Build Coastguard Worker ScopedAStatus status = sendEids(1);
76*4d7e907cSAndroid Build Coastguard Worker ASSERT_TRUE(status.isOk());
77*4d7e907cSAndroid Build Coastguard Worker }
78*4d7e907cSAndroid Build Coastguard Worker
TEST_P(BluetoothFinderTest,Send255Eids)79*4d7e907cSAndroid Build Coastguard Worker TEST_P(BluetoothFinderTest, Send255Eids) {
80*4d7e907cSAndroid Build Coastguard Worker ScopedAStatus status = sendEids(255);
81*4d7e907cSAndroid Build Coastguard Worker ASSERT_TRUE(status.isOk());
82*4d7e907cSAndroid Build Coastguard Worker }
83*4d7e907cSAndroid Build Coastguard Worker
TEST_P(BluetoothFinderTest,setAndGetPoweredOffFinderModeEnable)84*4d7e907cSAndroid Build Coastguard Worker TEST_P(BluetoothFinderTest, setAndGetPoweredOffFinderModeEnable) {
85*4d7e907cSAndroid Build Coastguard Worker ScopedAStatus status = setPoweredOffFinderMode(true);
86*4d7e907cSAndroid Build Coastguard Worker ASSERT_TRUE(status.isOk());
87*4d7e907cSAndroid Build Coastguard Worker bool pof_status;
88*4d7e907cSAndroid Build Coastguard Worker status = getPoweredOffFinderMode(&pof_status);
89*4d7e907cSAndroid Build Coastguard Worker ASSERT_TRUE(status.isOk());
90*4d7e907cSAndroid Build Coastguard Worker ASSERT_TRUE(pof_status);
91*4d7e907cSAndroid Build Coastguard Worker }
92*4d7e907cSAndroid Build Coastguard Worker
TEST_P(BluetoothFinderTest,setAndGetPoweredOffFinderModeDisable)93*4d7e907cSAndroid Build Coastguard Worker TEST_P(BluetoothFinderTest, setAndGetPoweredOffFinderModeDisable) {
94*4d7e907cSAndroid Build Coastguard Worker ScopedAStatus status = setPoweredOffFinderMode(false);
95*4d7e907cSAndroid Build Coastguard Worker ASSERT_TRUE(status.isOk());
96*4d7e907cSAndroid Build Coastguard Worker bool pof_status;
97*4d7e907cSAndroid Build Coastguard Worker status = getPoweredOffFinderMode(&pof_status);
98*4d7e907cSAndroid Build Coastguard Worker ASSERT_TRUE(status.isOk());
99*4d7e907cSAndroid Build Coastguard Worker ASSERT_TRUE(!pof_status);
100*4d7e907cSAndroid Build Coastguard Worker }
101*4d7e907cSAndroid Build Coastguard Worker
102*4d7e907cSAndroid Build Coastguard Worker GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothFinderTest);
103*4d7e907cSAndroid Build Coastguard Worker INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothFinderTest,
104*4d7e907cSAndroid Build Coastguard Worker testing::ValuesIn(android::getAidlHalInstanceNames(
105*4d7e907cSAndroid Build Coastguard Worker IBluetoothFinder::descriptor)),
106*4d7e907cSAndroid Build Coastguard Worker android::PrintInstanceNameToString);
107*4d7e907cSAndroid Build Coastguard Worker
main(int argc,char ** argv)108*4d7e907cSAndroid Build Coastguard Worker int main(int argc, char** argv) {
109*4d7e907cSAndroid Build Coastguard Worker ::testing::InitGoogleTest(&argc, argv);
110*4d7e907cSAndroid Build Coastguard Worker ABinderProcess_startThreadPool();
111*4d7e907cSAndroid Build Coastguard Worker int status = RUN_ALL_TESTS();
112*4d7e907cSAndroid Build Coastguard Worker ALOGI("Test result = %d", status);
113*4d7e907cSAndroid Build Coastguard Worker return status;
114*4d7e907cSAndroid Build Coastguard Worker }
115