1 /*
2 * Copyright (C) 2021 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/frameworks/automotive/powerpolicy/BnCarPowerPolicyChangeCallback.h>
20 #include <aidl/android/frameworks/automotive/powerpolicy/CarPowerPolicy.h>
21 #include <aidl/android/frameworks/automotive/powerpolicy/CarPowerPolicyFilter.h>
22 #include <aidl/android/frameworks/automotive/powerpolicy/ICarPowerPolicyServer.h>
23 #include <aidl/android/frameworks/automotive/powerpolicy/PowerComponent.h>
24 #include <android-base/stringprintf.h>
25 #include <android/binder_auto_utils.h>
26 #include <android/binder_manager.h>
27 #include <android/binder_status.h>
28 #include <binder/IBinder.h>
29 #include <binder/IServiceManager.h>
30 #include <binder/ProcessState.h>
31
32 namespace {
33
34 using ::aidl::android::frameworks::automotive::powerpolicy::BnCarPowerPolicyChangeCallback;
35 using ::aidl::android::frameworks::automotive::powerpolicy::CarPowerPolicy;
36 using ::aidl::android::frameworks::automotive::powerpolicy::CarPowerPolicyFilter;
37 using ::aidl::android::frameworks::automotive::powerpolicy::ICarPowerPolicyServer;
38 using ::aidl::android::frameworks::automotive::powerpolicy::PowerComponent;
39 using ::android::OK;
40 using ::android::ProcessState;
41 using ::android::status_t;
42 using ::android::String16;
43 using ::android::UNKNOWN_ERROR;
44 using ::android::base::StringPrintf;
45 using ::ndk::ScopedAStatus;
46 using ::ndk::SpAIBinder;
47
48 class MockPowerPolicyChangeCallback : public BnCarPowerPolicyChangeCallback {
49 public:
MockPowerPolicyChangeCallback()50 MockPowerPolicyChangeCallback() {}
51
onPolicyChanged(const CarPowerPolicy & policy)52 ScopedAStatus onPolicyChanged([[maybe_unused]] const CarPowerPolicy& policy) override {
53 return ScopedAStatus::ok();
54 }
55 };
56
57 } // namespace
58
59 class PowerPolicyAidlTest : public ::testing::TestWithParam<std::string> {
60 public:
SetUp()61 virtual void SetUp() override {
62 SpAIBinder binder(AServiceManager_getService(GetParam().c_str()));
63 ASSERT_NE(binder.get(), nullptr);
64 powerPolicyServer = ICarPowerPolicyServer::fromBinder(binder);
65 }
66
67 std::shared_ptr<ICarPowerPolicyServer> powerPolicyServer;
68 };
69
TEST_P(PowerPolicyAidlTest,TestGetCurrentPowerPolicy)70 TEST_P(PowerPolicyAidlTest, TestGetCurrentPowerPolicy) {
71 CarPowerPolicy policy;
72
73 ScopedAStatus status = powerPolicyServer->getCurrentPowerPolicy(&policy);
74
75 ASSERT_TRUE(status.isOk() || status.getServiceSpecificError() == EX_ILLEGAL_STATE);
76 }
77
TEST_P(PowerPolicyAidlTest,TestGetPowerComponentState)78 TEST_P(PowerPolicyAidlTest, TestGetPowerComponentState) {
79 bool state;
80 for (const auto componentId : ndk::enum_range<PowerComponent>()) {
81 if (componentId >= PowerComponent::MINIMUM_CUSTOM_COMPONENT_VALUE) {
82 continue;
83 }
84 ScopedAStatus status = powerPolicyServer->getPowerComponentState(componentId, &state);
85 std::string errMsg = StringPrintf("Getting state of component(%d) fails", componentId);
86 ASSERT_TRUE(status.isOk()) << errMsg;
87 }
88 }
89
TEST_P(PowerPolicyAidlTest,TestGetPowerComponentState_invalidComponent)90 TEST_P(PowerPolicyAidlTest, TestGetPowerComponentState_invalidComponent) {
91 bool state;
92 PowerComponent invalidComponent = static_cast<PowerComponent>(-1);
93
94 ScopedAStatus status = powerPolicyServer->getPowerComponentState(invalidComponent, &state);
95
96 ASSERT_FALSE(status.isOk());
97 }
98
TEST_P(PowerPolicyAidlTest,TestRegisterCallback)99 TEST_P(PowerPolicyAidlTest, TestRegisterCallback) {
100 std::shared_ptr<MockPowerPolicyChangeCallback> callback =
101 ndk::SharedRefBase::make<MockPowerPolicyChangeCallback>();
102 CarPowerPolicyFilter filter;
103 filter.components.push_back(PowerComponent::AUDIO);
104
105 ScopedAStatus status = powerPolicyServer->registerPowerPolicyChangeCallback(callback, filter);
106
107 ASSERT_TRUE(status.isOk());
108
109 status = powerPolicyServer->unregisterPowerPolicyChangeCallback(callback);
110
111 ASSERT_TRUE(status.isOk());
112 }
113
TEST_P(PowerPolicyAidlTest,TestRegisterCallback_doubleRegistering)114 TEST_P(PowerPolicyAidlTest, TestRegisterCallback_doubleRegistering) {
115 std::shared_ptr<MockPowerPolicyChangeCallback> callback =
116 ndk::SharedRefBase::make<MockPowerPolicyChangeCallback>();
117 CarPowerPolicyFilter filter;
118 filter.components.push_back(PowerComponent::AUDIO);
119
120 ScopedAStatus status = powerPolicyServer->registerPowerPolicyChangeCallback(callback, filter);
121
122 ASSERT_TRUE(status.isOk());
123
124 status = powerPolicyServer->registerPowerPolicyChangeCallback(callback, filter);
125
126 ASSERT_FALSE(status.isOk());
127 ASSERT_EQ(status.getServiceSpecificError(), EX_ILLEGAL_ARGUMENT);
128 }
129
TEST_P(PowerPolicyAidlTest,TestUnegisterNotRegisteredCallback)130 TEST_P(PowerPolicyAidlTest, TestUnegisterNotRegisteredCallback) {
131 std::shared_ptr<MockPowerPolicyChangeCallback> callback =
132 ndk::SharedRefBase::make<MockPowerPolicyChangeCallback>();
133
134 ScopedAStatus status = powerPolicyServer->unregisterPowerPolicyChangeCallback(callback);
135
136 ASSERT_FALSE(status.isOk());
137 }
138
139 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerPolicyAidlTest);
140 INSTANTIATE_TEST_SUITE_P(
141 CarPowerPolicyServer, PowerPolicyAidlTest,
142 ::testing::ValuesIn(android::getAidlHalInstanceNames(ICarPowerPolicyServer::descriptor)),
143 android::PrintInstanceNameToString);
144
main(int argc,char ** argv)145 int main(int argc, char** argv) {
146 ::testing::InitGoogleTest(&argc, argv);
147 ProcessState::self()->setThreadPoolMaxThreadCount(1);
148 ProcessState::self()->startThreadPool();
149 return RUN_ALL_TESTS();
150 }
151