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 <android/frameworks/automotive/powerpolicy/CarPowerPolicy.h>
20 #include <android/frameworks/automotive/powerpolicy/CarPowerPolicyFilter.h>
21 #include <android/frameworks/automotive/powerpolicy/ICarPowerPolicyChangeCallback.h>
22 #include <android/frameworks/automotive/powerpolicy/ICarPowerPolicyServer.h>
23 #include <android/frameworks/automotive/powerpolicy/PowerComponent.h>
24 #include <binder/IBinder.h>
25 #include <binder/IServiceManager.h>
26 #include <binder/ProcessState.h>
27 #include <gmock/gmock.h>
28 #include <utils/StrongPointer.h>
29
30 namespace {
31
32 using ::android::BBinder;
33 using ::android::IBinder;
34 using ::android::OK;
35 using ::android::ProcessState;
36 using ::android::sp;
37 using ::android::status_t;
38 using ::android::String16;
39 using ::android::wp;
40 using ::android::binder::Status;
41 using ::android::frameworks::automotive::powerpolicy::CarPowerPolicy;
42 using ::android::frameworks::automotive::powerpolicy::CarPowerPolicyFilter;
43 using ::android::frameworks::automotive::powerpolicy::ICarPowerPolicyChangeCallbackDefault;
44 using ::android::frameworks::automotive::powerpolicy::ICarPowerPolicyServer;
45 using ::android::frameworks::automotive::powerpolicy::PowerComponent;
46 using ::testing::_;
47 using ::testing::Return;
48
49 class MockBinder : public BBinder {
50 public:
51 MOCK_METHOD(status_t, linkToDeath,
52 (const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags), (override));
53 MOCK_METHOD(status_t, unlinkToDeath,
54 (const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
55 wp<DeathRecipient>* outRecipient),
56 (override));
57 };
58
59 class MockPowerPolicyChangeCallback : public ICarPowerPolicyChangeCallbackDefault {
60 public:
MockPowerPolicyChangeCallback()61 MockPowerPolicyChangeCallback() { mBinder = new MockBinder(); }
62
63 MOCK_METHOD(IBinder*, onAsBinder, (), (override));
64
expectNormalBinder()65 void expectNormalBinder() {
66 EXPECT_CALL(*mBinder, linkToDeath(_, nullptr, 0)).WillRepeatedly(Return(OK));
67 EXPECT_CALL(*mBinder, unlinkToDeath(_, nullptr, 0, nullptr)).WillRepeatedly(Return(OK));
68 EXPECT_CALL(*this, onAsBinder()).WillRepeatedly(Return(mBinder.get()));
69 }
70
71 private:
72 sp<MockBinder> mBinder;
73 };
74
75 } // namespace
76
77 class PowerPolicyAidlTest : public ::testing::TestWithParam<std::string> {
78 public:
SetUp()79 virtual void SetUp() override {
80 powerPolicyServer =
81 android::waitForDeclaredService<ICarPowerPolicyServer>(String16(GetParam().c_str()));
82 ASSERT_NE(powerPolicyServer.get(), nullptr);
83 }
84
85 sp<ICarPowerPolicyServer> powerPolicyServer;
86 };
87
TEST_P(PowerPolicyAidlTest,TestGetCurrentPowerPolicy)88 TEST_P(PowerPolicyAidlTest, TestGetCurrentPowerPolicy) {
89 CarPowerPolicy policy;
90
91 Status status = powerPolicyServer->getCurrentPowerPolicy(&policy);
92
93 ASSERT_TRUE(status.isOk() || status.exceptionCode() == Status::EX_ILLEGAL_STATE);
94 }
95
TEST_P(PowerPolicyAidlTest,TestGetPowerComponentState)96 TEST_P(PowerPolicyAidlTest, TestGetPowerComponentState) {
97 bool state;
98 for (const auto componentId : android::enum_range<PowerComponent>()) {
99 Status status = powerPolicyServer->getPowerComponentState(componentId, &state);
100
101 ASSERT_TRUE(status.isOk());
102 }
103 }
104
TEST_P(PowerPolicyAidlTest,TestGetPowerComponentState_invalidComponent)105 TEST_P(PowerPolicyAidlTest, TestGetPowerComponentState_invalidComponent) {
106 bool state;
107 PowerComponent invalidComponent = (PowerComponent)-1;
108
109 Status status = powerPolicyServer->getPowerComponentState(invalidComponent, &state);
110
111 ASSERT_FALSE(status.isOk());
112 }
113
TEST_P(PowerPolicyAidlTest,TestRegisterCallback)114 TEST_P(PowerPolicyAidlTest, TestRegisterCallback) {
115 sp<MockPowerPolicyChangeCallback> callback = new MockPowerPolicyChangeCallback();
116 callback->expectNormalBinder();
117 CarPowerPolicyFilter filter;
118 filter.components.push_back(PowerComponent::AUDIO);
119
120 Status status = powerPolicyServer->registerPowerPolicyChangeCallback(callback, filter);
121
122 ASSERT_TRUE(status.isOk());
123
124 status = powerPolicyServer->unregisterPowerPolicyChangeCallback(callback.get());
125
126 ASSERT_TRUE(status.isOk());
127 }
128
TEST_P(PowerPolicyAidlTest,TestRegisterCallback_doubleRegistering)129 TEST_P(PowerPolicyAidlTest, TestRegisterCallback_doubleRegistering) {
130 sp<MockPowerPolicyChangeCallback> callback = new MockPowerPolicyChangeCallback();
131 callback->expectNormalBinder();
132 CarPowerPolicyFilter filter;
133 filter.components.push_back(PowerComponent::AUDIO);
134
135 Status status = powerPolicyServer->registerPowerPolicyChangeCallback(callback, filter);
136
137 ASSERT_TRUE(status.isOk());
138
139 status = powerPolicyServer->registerPowerPolicyChangeCallback(callback, filter);
140
141 ASSERT_FALSE(status.isOk());
142 ASSERT_EQ(status.exceptionCode(), Status::EX_ILLEGAL_ARGUMENT);
143 }
144
TEST_P(PowerPolicyAidlTest,TestUnegisterNotRegisteredCallback)145 TEST_P(PowerPolicyAidlTest, TestUnegisterNotRegisteredCallback) {
146 sp<MockPowerPolicyChangeCallback> callback = new MockPowerPolicyChangeCallback();
147
148 Status status = powerPolicyServer->unregisterPowerPolicyChangeCallback(callback);
149
150 ASSERT_FALSE(status.isOk());
151 }
152
153 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerPolicyAidlTest);
154 INSTANTIATE_TEST_SUITE_P(
155 CarPowerPolicyServer, PowerPolicyAidlTest,
156 ::testing::ValuesIn(android::getAidlHalInstanceNames(ICarPowerPolicyServer::descriptor)),
157 android::PrintInstanceNameToString);
158
main(int argc,char ** argv)159 int main(int argc, char** argv) {
160 ::testing::InitGoogleTest(&argc, argv);
161 ProcessState::self()->setThreadPoolMaxThreadCount(1);
162 ProcessState::self()->startThreadPool();
163 return RUN_ALL_TESTS();
164 }
165