• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Vintf.h>
18 #include <aidl/android/frameworks/automotive/power/BnCarPowerStateChangeListener.h>
19 #include <aidl/android/frameworks/automotive/power/ICarPowerServer.h>
20 #include <android/binder_ibinder.h>
21 #include <android/binder_manager.h>
22 #include <binder/ProcessState.h>
23 
24 #include "PowerPolicyInterfaceTest.h"
25 
26 namespace {
27 
28 using ::aidl::android::frameworks::automotive::power::BnCarPowerStateChangeListener;
29 using ::aidl::android::frameworks::automotive::power::CarPowerState;
30 using ::aidl::android::frameworks::automotive::power::ICarPowerServer;
31 using ::android::ProcessState;
32 
33 class MockPowerStateChangeListener : public BnCarPowerStateChangeListener {
34    public:
MockPowerStateChangeListener()35     MockPowerStateChangeListener() {}
36 
onStateChanged(CarPowerState state)37     ndk::ScopedAStatus onStateChanged([[maybe_unused]] CarPowerState state) override {
38         return ndk::ScopedAStatus::ok();
39     }
40 };
41 
42 }  // namespace
43 
44 class CarPowerServerAidlTest : public ::testing::TestWithParam<std::string> {
45    public:
SetUp()46     virtual void SetUp() override {
47         powerPolicyTest.SetUp(GetParam());
48     }
49 
50     PowerPolicyInterfaceTest<ICarPowerServer> powerPolicyTest;
51 };
52 
TEST_P(CarPowerServerAidlTest,TestGetCurrentPowerPolicy)53 TEST_P(CarPowerServerAidlTest, TestGetCurrentPowerPolicy) {
54     powerPolicyTest.TestGetCurrentPowerPolicy();
55 }
56 
TEST_P(CarPowerServerAidlTest,TestGetPowerComponentState)57 TEST_P(CarPowerServerAidlTest, TestGetPowerComponentState) {
58     powerPolicyTest.TestGetPowerComponentState();
59 }
60 
TEST_P(CarPowerServerAidlTest,TestGetPowerComponentState_invalidComponent)61 TEST_P(CarPowerServerAidlTest, TestGetPowerComponentState_invalidComponent) {
62     powerPolicyTest.TestGetPowerComponentState_invalidComponent();
63 }
64 
TEST_P(CarPowerServerAidlTest,TestRegisterPowerPolicyCallback)65 TEST_P(CarPowerServerAidlTest, TestRegisterPowerPolicyCallback) {
66     powerPolicyTest.TestRegisterPowerPolicyCallback();
67 }
68 
TEST_P(CarPowerServerAidlTest,TestRegisterPowerPolicyCallback_doubleRegistering)69 TEST_P(CarPowerServerAidlTest, TestRegisterPowerPolicyCallback_doubleRegistering) {
70     powerPolicyTest.TestRegisterPowerPolicyCallback_doubleRegistering();
71 }
72 
TEST_P(CarPowerServerAidlTest,TestUnegisterNotRegisteredPowerPolicyCallback)73 TEST_P(CarPowerServerAidlTest, TestUnegisterNotRegisteredPowerPolicyCallback) {
74     powerPolicyTest.TestUnegisterNotRegisteredPowerPolicyCallback();
75 }
76 
TEST_P(CarPowerServerAidlTest,TestRegisterPowerStateListener)77 TEST_P(CarPowerServerAidlTest, TestRegisterPowerStateListener) {
78     std::shared_ptr<MockPowerStateChangeListener> listener =
79         ndk::SharedRefBase::make<MockPowerStateChangeListener>();
80     std::shared_ptr<ICarPowerServer> powerServer = powerPolicyTest.powerPolicyServer;
81 
82     ndk::ScopedAStatus status = powerServer->registerPowerStateListener(listener);
83 
84     ASSERT_TRUE(status.isOk());
85 
86     status = powerServer->unregisterPowerStateListener(listener);
87 
88     ASSERT_TRUE(status.isOk());
89 }
90 
TEST_P(CarPowerServerAidlTest,TestRegisterPowerStateListener_doubleRegistering)91 TEST_P(CarPowerServerAidlTest, TestRegisterPowerStateListener_doubleRegistering) {
92     std::shared_ptr<MockPowerStateChangeListener> listener =
93         ndk::SharedRefBase::make<MockPowerStateChangeListener>();
94     std::shared_ptr<ICarPowerServer> powerServer = powerPolicyTest.powerPolicyServer;
95 
96     ndk::ScopedAStatus status = powerServer->registerPowerStateListener(listener);
97 
98     ASSERT_TRUE(status.isOk());
99 
100     status = powerServer->registerPowerStateListener(listener);
101 
102     ASSERT_FALSE(status.isOk());
103     ASSERT_EQ(status.getServiceSpecificError(), EX_ILLEGAL_ARGUMENT);
104 }
105 
TEST_P(CarPowerServerAidlTest,TestUnegisterNotRegisteredPowerStateListener)106 TEST_P(CarPowerServerAidlTest, TestUnegisterNotRegisteredPowerStateListener) {
107     std::shared_ptr<MockPowerStateChangeListener> listener =
108         ndk::SharedRefBase::make<MockPowerStateChangeListener>();
109     std::shared_ptr<ICarPowerServer> powerServer = powerPolicyTest.powerPolicyServer;
110 
111     ndk::ScopedAStatus status = powerServer->unregisterPowerStateListener(listener);
112 
113     ASSERT_FALSE(status.isOk());
114 }
115 
116 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(CarPowerServerAidlTest);
117 INSTANTIATE_TEST_SUITE_P(
118     CarPowerServer, CarPowerServerAidlTest,
119     ::testing::ValuesIn(android::getAidlHalInstanceNames(ICarPowerServer::descriptor)),
120     android::PrintInstanceNameToString);
121 
main(int argc,char ** argv)122 int main(int argc, char** argv) {
123     ::testing::InitGoogleTest(&argc, argv);
124     ProcessState::self()->setThreadPoolMaxThreadCount(1);
125     ProcessState::self()->startThreadPool();
126     return RUN_ALL_TESTS();
127 }
128