1 /**
2 * Copyright (c) 2020, 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 "CarPowerPolicyServer.h"
18
19 #include <gmock/gmock.h>
20 #include <utils/Looper.h>
21 #include <utils/StrongPointer.h>
22
23 namespace android {
24 namespace frameworks {
25 namespace automotive {
26 namespace powerpolicy {
27
28 using binder::Status;
29 using ::testing::_;
30 using ::testing::Return;
31
32 namespace {
33
34 class MockBinder : public BBinder {
35 public:
36 MOCK_METHOD(status_t, linkToDeath,
37 (const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags), (override));
38 MOCK_METHOD(status_t, unlinkToDeath,
39 (const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
40 wp<DeathRecipient>* outRecipient),
41 (override));
42 };
43
44 class MockPowerPolicyChangeCallback : public ICarPowerPolicyChangeCallbackDefault {
45 public:
MockPowerPolicyChangeCallback()46 MockPowerPolicyChangeCallback() { mBinder = new MockBinder(); }
47
48 MOCK_METHOD(IBinder*, onAsBinder, (), (override));
49
expectLinkToDeathStatus(status_t linkToDeathResult)50 void expectLinkToDeathStatus(status_t linkToDeathResult) {
51 EXPECT_CALL(*mBinder, linkToDeath(_, nullptr, 0)).WillRepeatedly(Return(linkToDeathResult));
52 EXPECT_CALL(*mBinder, unlinkToDeath(_, nullptr, 0, nullptr)).WillRepeatedly(Return(OK));
53 EXPECT_CALL(*this, onAsBinder()).WillRepeatedly(Return(mBinder.get()));
54 }
55
56 private:
57 sp<MockBinder> mBinder;
58 };
59
60 } // namespace
61
62 namespace internal {
63
64 class CarPowerPolicyServerPeer : public RefBase {
65 public:
CarPowerPolicyServerPeer()66 CarPowerPolicyServerPeer() { server = new CarPowerPolicyServer(); }
67
getCurrentPowerPolicy(CarPowerPolicy * aidlReturn)68 Status getCurrentPowerPolicy(CarPowerPolicy* aidlReturn) {
69 return server->getCurrentPowerPolicy(aidlReturn);
70 }
registerPowerPolicyChangeCallback(const sp<ICarPowerPolicyChangeCallback> & callback,const CarPowerPolicyFilter & filter)71 Status registerPowerPolicyChangeCallback(const sp<ICarPowerPolicyChangeCallback>& callback,
72 const CarPowerPolicyFilter& filter) {
73 return server->registerPowerPolicyChangeCallback(callback, filter);
74 }
unregisterPowerPolicyChangeCallback(const sp<ICarPowerPolicyChangeCallback> & callback)75 Status unregisterPowerPolicyChangeCallback(const sp<ICarPowerPolicyChangeCallback>& callback) {
76 return server->unregisterPowerPolicyChangeCallback(callback);
77 }
78
79 sp<CarPowerPolicyServer> server;
80 };
81
82 } // namespace internal
83
84 class CarPowerPolicyServerTest : public ::testing::Test {};
85
TEST_F(CarPowerPolicyServerTest,TestRegisterCallback)86 TEST_F(CarPowerPolicyServerTest, TestRegisterCallback) {
87 sp<internal::CarPowerPolicyServerPeer> server = new internal::CarPowerPolicyServerPeer();
88 sp<MockPowerPolicyChangeCallback> callbackOne = new MockPowerPolicyChangeCallback();
89 callbackOne->expectLinkToDeathStatus(OK);
90
91 CarPowerPolicyFilter filter;
92 Status status = server->registerPowerPolicyChangeCallback(callbackOne, filter);
93 ASSERT_TRUE(status.isOk()) << status;
94 status = server->registerPowerPolicyChangeCallback(callbackOne, filter);
95 ASSERT_FALSE(status.isOk()) << "Duplicated registration is not allowed";
96 filter.components = {PowerComponent::BLUETOOTH, PowerComponent::AUDIO};
97 status = server->registerPowerPolicyChangeCallback(callbackOne, filter);
98 ASSERT_FALSE(status.isOk()) << "Duplicated registration is not allowed";
99
100 sp<MockPowerPolicyChangeCallback> callbackTwo = new MockPowerPolicyChangeCallback();
101 callbackTwo->expectLinkToDeathStatus(OK);
102
103 status = server->registerPowerPolicyChangeCallback(callbackTwo, filter);
104 ASSERT_TRUE(status.isOk()) << status;
105 }
106
TEST_F(CarPowerPolicyServerTest,TestRegisterCallback_BinderDied)107 TEST_F(CarPowerPolicyServerTest, TestRegisterCallback_BinderDied) {
108 sp<internal::CarPowerPolicyServerPeer> server = new internal::CarPowerPolicyServerPeer();
109 sp<MockPowerPolicyChangeCallback> callback = new MockPowerPolicyChangeCallback();
110 callback->expectLinkToDeathStatus(DEAD_OBJECT);
111 CarPowerPolicyFilter filter;
112
113 ASSERT_FALSE(server->registerPowerPolicyChangeCallback(callback, filter).isOk())
114 << "When linkToDeath fails, registerPowerPolicyChangeCallback should return an error";
115 }
116
TEST_F(CarPowerPolicyServerTest,TestUnregisterCallback)117 TEST_F(CarPowerPolicyServerTest, TestUnregisterCallback) {
118 sp<internal::CarPowerPolicyServerPeer> server = new internal::CarPowerPolicyServerPeer();
119 sp<MockPowerPolicyChangeCallback> callback = new MockPowerPolicyChangeCallback();
120 callback->expectLinkToDeathStatus(OK);
121 CarPowerPolicyFilter filter;
122
123 server->registerPowerPolicyChangeCallback(callback, filter);
124 Status status = server->unregisterPowerPolicyChangeCallback(callback);
125 ASSERT_TRUE(status.isOk()) << status;
126 ASSERT_FALSE(server->unregisterPowerPolicyChangeCallback(callback).isOk())
127 << "Unregistering an unregistered powerpolicy change callback should return an error";
128 }
129
TEST_F(CarPowerPolicyServerTest,TestGetCurrentPowerPolicy)130 TEST_F(CarPowerPolicyServerTest, TestGetCurrentPowerPolicy) {
131 sp<internal::CarPowerPolicyServerPeer> server = new internal::CarPowerPolicyServerPeer();
132 CarPowerPolicy currentPolicy;
133
134 Status status = server->getCurrentPowerPolicy(¤tPolicy);
135 ASSERT_FALSE(status.isOk()) << "The current policy at creation should be null";
136 // TODO(b/168545262): Add more test cases after VHAL integration is complete.
137 }
138
139 } // namespace powerpolicy
140 } // namespace automotive
141 } // namespace frameworks
142 } // namespace android
143