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/hardware/power/IPowerHintSession.h>
18 #include <powermanager/PowerHintSessionWrapper.h>
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 using aidl::android::hardware::power::IPowerHintSession;
24 using android::power::PowerHintSessionWrapper;
25
26 using namespace android;
27 using namespace std::chrono_literals;
28 using namespace testing;
29
30 class MockIPowerHintSession : public IPowerHintSession {
31 public:
32 MockIPowerHintSession() = default;
33 MOCK_METHOD(::ndk::ScopedAStatus, updateTargetWorkDuration, (int64_t in_targetDurationNanos),
34 (override));
35 MOCK_METHOD(::ndk::ScopedAStatus, reportActualWorkDuration,
36 (const std::vector<::aidl::android::hardware::power::WorkDuration>& in_durations),
37 (override));
38 MOCK_METHOD(::ndk::ScopedAStatus, pause, (), (override));
39 MOCK_METHOD(::ndk::ScopedAStatus, resume, (), (override));
40 MOCK_METHOD(::ndk::ScopedAStatus, close, (), (override));
41 MOCK_METHOD(::ndk::ScopedAStatus, sendHint,
42 (::aidl::android::hardware::power::SessionHint in_hint), (override));
43 MOCK_METHOD(::ndk::ScopedAStatus, setThreads, (const std::vector<int32_t>& in_threadIds),
44 (override));
45 MOCK_METHOD(::ndk::ScopedAStatus, setMode,
46 (::aidl::android::hardware::power::SessionMode in_type, bool in_enabled),
47 (override));
48 MOCK_METHOD(::ndk::ScopedAStatus, getSessionConfig,
49 (::aidl::android::hardware::power::SessionConfig * _aidl_return), (override));
50 MOCK_METHOD(::ndk::ScopedAStatus, getInterfaceVersion, (int32_t * _aidl_return), (override));
51 MOCK_METHOD(::ndk::ScopedAStatus, getInterfaceHash, (std::string * _aidl_return), (override));
52 MOCK_METHOD(::ndk::SpAIBinder, asBinder, (), (override));
53 MOCK_METHOD(bool, isRemote, (), (override));
54 };
55
56 class PowerHintSessionWrapperTest : public Test {
57 public:
58 void SetUp() override;
59
60 protected:
61 std::shared_ptr<NiceMock<MockIPowerHintSession>> mMockSession = nullptr;
62 std::unique_ptr<PowerHintSessionWrapper> mSession = nullptr;
63 };
64
SetUp()65 void PowerHintSessionWrapperTest::SetUp() {
66 mMockSession = ndk::SharedRefBase::make<NiceMock<MockIPowerHintSession>>();
67 EXPECT_CALL(*mMockSession, getInterfaceVersion(_)).WillRepeatedly(([](int32_t* ret) {
68 *ret = 5;
69 return ndk::ScopedAStatus::ok();
70 }));
71 mSession = std::make_unique<PowerHintSessionWrapper>(mMockSession);
72 ASSERT_NE(nullptr, mSession);
73 }
74
TEST_F(PowerHintSessionWrapperTest,updateTargetWorkDuration)75 TEST_F(PowerHintSessionWrapperTest, updateTargetWorkDuration) {
76 EXPECT_CALL(*mMockSession.get(), updateTargetWorkDuration(1000000000))
77 .WillOnce(Return(ndk::ScopedAStatus::ok()));
78 auto status = mSession->updateTargetWorkDuration(1000000000);
79 ASSERT_TRUE(status.isOk());
80 }
81
TEST_F(PowerHintSessionWrapperTest,reportActualWorkDuration)82 TEST_F(PowerHintSessionWrapperTest, reportActualWorkDuration) {
83 EXPECT_CALL(*mMockSession.get(),
84 reportActualWorkDuration(
85 std::vector<::aidl::android::hardware::power::WorkDuration>()))
86 .WillOnce(Return(ndk::ScopedAStatus::ok()));
87 auto status = mSession->reportActualWorkDuration(
88 std::vector<::aidl::android::hardware::power::WorkDuration>());
89 ASSERT_TRUE(status.isOk());
90 }
91
TEST_F(PowerHintSessionWrapperTest,pause)92 TEST_F(PowerHintSessionWrapperTest, pause) {
93 EXPECT_CALL(*mMockSession.get(), pause()).WillOnce(Return(ndk::ScopedAStatus::ok()));
94 auto status = mSession->pause();
95 ASSERT_TRUE(status.isOk());
96 }
97
TEST_F(PowerHintSessionWrapperTest,resume)98 TEST_F(PowerHintSessionWrapperTest, resume) {
99 EXPECT_CALL(*mMockSession.get(), resume()).WillOnce(Return(ndk::ScopedAStatus::ok()));
100 auto status = mSession->resume();
101 ASSERT_TRUE(status.isOk());
102 }
103
TEST_F(PowerHintSessionWrapperTest,close)104 TEST_F(PowerHintSessionWrapperTest, close) {
105 EXPECT_CALL(*mMockSession.get(), close()).WillOnce(Return(ndk::ScopedAStatus::ok()));
106 auto status = mSession->close();
107 ASSERT_TRUE(status.isOk());
108 }
109
TEST_F(PowerHintSessionWrapperTest,sendHint)110 TEST_F(PowerHintSessionWrapperTest, sendHint) {
111 EXPECT_CALL(*mMockSession.get(),
112 sendHint(::aidl::android::hardware::power::SessionHint::CPU_LOAD_UP))
113 .WillOnce(Return(ndk::ScopedAStatus::ok()));
114 auto status = mSession->sendHint(::aidl::android::hardware::power::SessionHint::CPU_LOAD_UP);
115 ASSERT_TRUE(status.isOk());
116 }
117
TEST_F(PowerHintSessionWrapperTest,setThreads)118 TEST_F(PowerHintSessionWrapperTest, setThreads) {
119 EXPECT_CALL(*mMockSession.get(), setThreads(_)).WillOnce(Return(ndk::ScopedAStatus::ok()));
120 auto status = mSession->setThreads(std::vector<int32_t>{gettid()});
121 ASSERT_TRUE(status.isOk());
122 }
123
TEST_F(PowerHintSessionWrapperTest,setMode)124 TEST_F(PowerHintSessionWrapperTest, setMode) {
125 EXPECT_CALL(*mMockSession.get(),
126 setMode(::aidl::android::hardware::power::SessionMode::POWER_EFFICIENCY, true))
127 .WillOnce(Return(ndk::ScopedAStatus::ok()));
128 auto status = mSession->setMode(::aidl::android::hardware::power::SessionMode::POWER_EFFICIENCY,
129 true);
130 ASSERT_TRUE(status.isOk());
131 }
132
TEST_F(PowerHintSessionWrapperTest,getSessionConfig)133 TEST_F(PowerHintSessionWrapperTest, getSessionConfig) {
134 EXPECT_CALL(*mMockSession.get(), getSessionConfig(_))
135 .WillOnce(DoAll(SetArgPointee<0>(
136 aidl::android::hardware::power::SessionConfig{.id = 12L}),
137 Return(ndk::ScopedAStatus::ok())));
138 auto status = mSession->getSessionConfig();
139 ASSERT_TRUE(status.isOk());
140 }
141