1 /*
2  * Copyright (C) 2024 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/frameworks/devicestate/DeviceStateConfiguration.h"
18 #define LOG_TAG "VtsHalDeviceStateServiceTest"
19 
20 #include <unordered_set>
21 
22 #include "aidl/android/frameworks/devicestate/ErrorCode.h"
23 
24 #include <aidl/Gtest.h>
25 #include <aidl/Vintf.h>
26 #include <aidl/android/frameworks/devicestate/BnDeviceStateListener.h>
27 #include <aidl/android/frameworks/devicestate/BnDeviceStateService.h>
28 #include <android-base/logging.h>
29 #include <android/binder_auto_utils.h>
30 #include <android/binder_manager.h>
31 #include <android/binder_process.h>
32 #include <utils/Condition.h>
33 #include <utils/Log.h>
34 #include <utils/Mutex.h>
35 
36 #include <memory>
37 
38 #include "aidl/android/frameworks/devicestate/IDeviceStateListener.h"
39 #include "aidl/android/frameworks/devicestate/IDeviceStateService.h"
40 
41 #define NOTIFY_TIMEOUT_NS 2000000000
42 
43 namespace android {
44 
45 using ::aidl::android::frameworks::devicestate::BnDeviceStateListener;
46 using ::aidl::android::frameworks::devicestate::DeviceStateConfiguration;
47 using ::aidl::android::frameworks::devicestate::ErrorCode;
48 using ::aidl::android::frameworks::devicestate::IDeviceStateService;
49 using ::android::getAidlHalInstanceNames;
50 using ::android::PrintInstanceNameToString;
51 using ::ndk::enum_range;
52 using ::ndk::SpAIBinder;
53 using ::testing::InitGoogleTest;
54 using ::testing::TestWithParam;
55 
56 class DeviceStateServiceTest : public ::testing::TestWithParam<std::string> {
57    public:
SetUp()58     void SetUp() override {
59         bool ret = ABinderProcess_setThreadPoolMaxThreadCount(/* numThreads= */ 5);
60         ASSERT_TRUE(ret);
61         ABinderProcess_startThreadPool();
62         SpAIBinder binder(AServiceManager_waitForService(GetParam().c_str()));
63         service = IDeviceStateService::fromBinder(binder);
64         ASSERT_NE(service, nullptr);
65     }
66 
67     std::shared_ptr<IDeviceStateService> service;
68 };
69 
70 class DeviceStateListener : public BnDeviceStateListener {
71    public:
DeviceStateListener()72     DeviceStateListener() : mInitialNotification(false) {
73         mPublicPropertyMask = 0;
74         for (const auto& it : enum_range<DeviceStateConfiguration::DeviceStatePropertyValue>()) {
75             mPublicPropertyMask |= static_cast<unsigned long>(it);
76         }
77         mPublicPropertyMask = ~mPublicPropertyMask;
78     }
79 
onDeviceStateChanged(const::aidl::android::frameworks::devicestate::DeviceStateConfiguration & in_deviceState)80     ::ndk::ScopedAStatus onDeviceStateChanged(
81         const ::aidl::android::frameworks::devicestate::DeviceStateConfiguration& in_deviceState)
82         override {
83         Mutex::Autolock l(mLock);
84         EXPECT_TRUE((in_deviceState.deviceProperties & mPublicPropertyMask) == 0);
85         mInitialNotification = true;
86         mNotifyCondition.broadcast();
87         return ::ndk::ScopedAStatus::ok();
88     }
89 
waitForDeviceStateChange()90     bool waitForDeviceStateChange() {
91         Mutex::Autolock l(mLock);
92         if (!mInitialNotification &&
93             (mNotifyCondition.waitRelative(mLock, NOTIFY_TIMEOUT_NS) != android::OK)) {
94             return false;
95         }
96 
97         return mInitialNotification;
98     }
99 
100    private:
101     unsigned long mPublicPropertyMask;
102 
103     mutable Mutex mLock;
104     mutable Condition mNotifyCondition;
105     bool mInitialNotification;
106 };
107 
TEST_P(DeviceStateServiceTest,RegisterAndUnregisterDeviceStateTest)108 TEST_P(DeviceStateServiceTest, RegisterAndUnregisterDeviceStateTest) {
109     auto listener = ::ndk::SharedRefBase::make<DeviceStateListener>();
110     EXPECT_TRUE(service->registerListener(listener).isOk());
111     EXPECT_TRUE(listener->waitForDeviceStateChange());
112 
113     auto secondListener = ::ndk::SharedRefBase::make<DeviceStateListener>();
114     auto ret = service->registerListener(secondListener);
115     EXPECT_TRUE(!ret.isOk());
116     EXPECT_TRUE(static_cast<ErrorCode>(ret.getServiceSpecificError()) == ErrorCode::ALREADY_EXISTS);
117 
118     ret = service->unregisterListener(listener);
119     EXPECT_TRUE(ret.isOk());
120 
121     ret = service->unregisterListener(listener);
122     EXPECT_TRUE(!ret.isOk());
123     EXPECT_TRUE(static_cast<ErrorCode>(ret.getServiceSpecificError()) == ErrorCode::BAD_INPUT);
124 }
125 
126 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(DeviceStateServiceTest);
127 
128 INSTANTIATE_TEST_SUITE_P(
129     PerInstance, DeviceStateServiceTest,
130     testing::ValuesIn(getAidlHalInstanceNames(IDeviceStateService::descriptor)),
131     PrintInstanceNameToString);
132 
main(int argc,char ** argv)133 int main(int argc, char** argv) {
134     InitGoogleTest(&argc, argv);
135     return RUN_ALL_TESTS();
136 }
137 }  // namespace android
138