1 /*
2  * Copyright (C) 2022 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 #define LOG_TAG "PowerHalWrapperHidlV1_2Test"
18 
19 #include <aidl/android/hardware/power/Boost.h>
20 #include <aidl/android/hardware/power/IPower.h>
21 #include <aidl/android/hardware/power/Mode.h>
22 #include <android/hardware/power/1.2/IPower.h>
23 #include <binder/IServiceManager.h>
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26 #include <powermanager/PowerHalWrapper.h>
27 #include <utils/Log.h>
28 
29 using aidl::android::hardware::power::Boost;
30 using aidl::android::hardware::power::Mode;
31 using android::hardware::power::V1_0::Feature;
32 using PowerHintV1_0 = android::hardware::power::V1_0::PowerHint;
33 using PowerHintV1_2 = android::hardware::power::V1_2::PowerHint;
34 
35 using IPowerV1_2 = android::hardware::power::V1_2::IPower;
36 
37 using namespace android;
38 using namespace android::power;
39 using namespace std::chrono_literals;
40 using namespace testing;
41 
42 // -------------------------------------------------------------------------------------------------
43 
44 class MockIPowerV1_2 : public IPowerV1_2 {
45 public:
46     MOCK_METHOD(hardware::Return<void>, setInteractive, (bool interactive), (override));
47     MOCK_METHOD(hardware::Return<void>, powerHint, (PowerHintV1_0 hint, int32_t data), (override));
48     MOCK_METHOD(hardware::Return<void>, setFeature, (Feature feature, bool activate), (override));
49     MOCK_METHOD(hardware::Return<void>, getPlatformLowPowerStats,
50                 (getPlatformLowPowerStats_cb _hidl_cb), (override));
51     MOCK_METHOD(hardware::Return<void>, powerHintAsync, (PowerHintV1_0 hint, int32_t data),
52                 (override));
53     MOCK_METHOD(hardware::Return<void>, powerHintAsync_1_2, (PowerHintV1_2 hint, int32_t data),
54                 (override));
55     MOCK_METHOD(hardware::Return<void>, getSubsystemLowPowerStats,
56                 (getSubsystemLowPowerStats_cb _hidl_cb), (override));
57 };
58 
59 // -------------------------------------------------------------------------------------------------
60 
61 class PowerHalWrapperHidlV1_2Test : public Test {
62 public:
63     void SetUp() override;
64 
65 protected:
66     std::unique_ptr<HalWrapper> mWrapper = nullptr;
67     sp<StrictMock<MockIPowerV1_2>> mMockHal = nullptr;
68 };
69 
70 // -------------------------------------------------------------------------------------------------
71 
SetUp()72 void PowerHalWrapperHidlV1_2Test::SetUp() {
73     mMockHal = new StrictMock<MockIPowerV1_2>();
74     mWrapper = std::make_unique<HidlHalWrapperV1_2>(mMockHal);
75     ASSERT_NE(mWrapper, nullptr);
76     EXPECT_CALL(*mMockHal.get(), powerHint(_, _)).Times(0);
77     EXPECT_CALL(*mMockHal.get(), powerHintAsync(_, _)).Times(0);
78 }
79 
80 // -------------------------------------------------------------------------------------------------
81 
TEST_F(PowerHalWrapperHidlV1_2Test,TestSetBoostSuccessful)82 TEST_F(PowerHalWrapperHidlV1_2Test, TestSetBoostSuccessful) {
83     {
84         InSequence seq;
85         EXPECT_CALL(*mMockHal.get(), powerHintAsync_1_2(Eq(PowerHintV1_2::INTERACTION), Eq(1000)))
86                 .Times(Exactly(1));
87         EXPECT_CALL(*mMockHal.get(), powerHintAsync_1_2(Eq(PowerHintV1_2::CAMERA_SHOT), Eq(500)))
88                 .Times(Exactly(1));
89         EXPECT_CALL(*mMockHal.get(), powerHintAsync_1_2(Eq(PowerHintV1_2::CAMERA_LAUNCH), Eq(300)))
90                 .Times(Exactly(1));
91     }
92 
93     auto result = mWrapper->setBoost(Boost::INTERACTION, 1000);
94     ASSERT_TRUE(result.isOk());
95     result = mWrapper->setBoost(Boost::CAMERA_SHOT, 500);
96     ASSERT_TRUE(result.isOk());
97     result = mWrapper->setBoost(Boost::CAMERA_LAUNCH, 300);
98     ASSERT_TRUE(result.isOk());
99 }
100 
TEST_F(PowerHalWrapperHidlV1_2Test,TestSetBoostFailed)101 TEST_F(PowerHalWrapperHidlV1_2Test, TestSetBoostFailed) {
102     EXPECT_CALL(*mMockHal.get(), powerHintAsync_1_2(Eq(PowerHintV1_2::INTERACTION), Eq(1000)))
103             .Times(Exactly(1))
104             .WillRepeatedly([](PowerHintV1_2, int32_t) {
105                 return hardware::Return<void>(hardware::Status::fromExceptionCode(-1));
106             });
107 
108     auto result = mWrapper->setBoost(Boost::INTERACTION, 1000);
109     ASSERT_TRUE(result.isFailed());
110 }
111 
TEST_F(PowerHalWrapperHidlV1_2Test,TestSetBoostUnsupported)112 TEST_F(PowerHalWrapperHidlV1_2Test, TestSetBoostUnsupported) {
113     EXPECT_CALL(*mMockHal.get(), powerHintAsync_1_2(_, _)).Times(0);
114     EXPECT_CALL(*mMockHal.get(), setInteractive(_)).Times(0);
115     EXPECT_CALL(*mMockHal.get(), setFeature(_, _)).Times(0);
116 
117     auto result = mWrapper->setBoost(Boost::ML_ACC, 10);
118     ASSERT_TRUE(result.isUnsupported());
119     result = mWrapper->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 10);
120     ASSERT_TRUE(result.isUnsupported());
121     result = mWrapper->setBoost(Boost::AUDIO_LAUNCH, 10);
122     ASSERT_TRUE(result.isUnsupported());
123 }
124 
TEST_F(PowerHalWrapperHidlV1_2Test,TestSetMode)125 TEST_F(PowerHalWrapperHidlV1_2Test, TestSetMode) {
126     {
127         InSequence seq;
128         EXPECT_CALL(*mMockHal.get(), powerHintAsync_1_2(Eq(PowerHintV1_2::LAUNCH), Eq(true)))
129                 .Times(Exactly(1));
130         EXPECT_CALL(*mMockHal.get(), powerHintAsync_1_2(Eq(PowerHintV1_2::LOW_POWER), Eq(false)))
131                 .Times(Exactly(1));
132         EXPECT_CALL(*mMockHal.get(),
133                     powerHintAsync_1_2(Eq(PowerHintV1_2::SUSTAINED_PERFORMANCE), Eq(true)))
134                 .Times(Exactly(1));
135         EXPECT_CALL(*mMockHal.get(), powerHintAsync_1_2(Eq(PowerHintV1_2::VR_MODE), Eq(false)))
136                 .Times(Exactly(1));
137         EXPECT_CALL(*mMockHal.get(), setInteractive(Eq(true))).Times(Exactly(true));
138         EXPECT_CALL(*mMockHal.get(),
139                     setFeature(Eq(Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE), Eq(false)))
140                 .Times(Exactly(1));
141         EXPECT_CALL(*mMockHal.get(),
142                     powerHintAsync_1_2(Eq(PowerHintV1_2::CAMERA_STREAMING), Eq(true)))
143                 .Times(Exactly(2));
144         EXPECT_CALL(*mMockHal.get(),
145                     powerHintAsync_1_2(Eq(PowerHintV1_2::CAMERA_STREAMING), Eq(false)))
146                 .Times(Exactly(2));
147         EXPECT_CALL(*mMockHal.get(),
148                     powerHintAsync_1_2(Eq(PowerHintV1_2::AUDIO_LOW_LATENCY), Eq(true)))
149                 .Times(Exactly(1));
150     }
151 
152     auto result = mWrapper->setMode(Mode::LAUNCH, true);
153     ASSERT_TRUE(result.isOk());
154     result = mWrapper->setMode(Mode::LOW_POWER, false);
155     ASSERT_TRUE(result.isOk());
156     result = mWrapper->setMode(Mode::SUSTAINED_PERFORMANCE, true);
157     ASSERT_TRUE(result.isOk());
158     result = mWrapper->setMode(Mode::VR, false);
159     ASSERT_TRUE(result.isOk());
160     result = mWrapper->setMode(Mode::INTERACTIVE, true);
161     ASSERT_TRUE(result.isOk());
162     result = mWrapper->setMode(Mode::DOUBLE_TAP_TO_WAKE, false);
163     ASSERT_TRUE(result.isOk());
164     result = mWrapper->setMode(Mode::CAMERA_STREAMING_SECURE, true);
165     ASSERT_TRUE(result.isOk());
166     result = mWrapper->setMode(Mode::CAMERA_STREAMING_LOW, true);
167     ASSERT_TRUE(result.isOk());
168     result = mWrapper->setMode(Mode::CAMERA_STREAMING_MID, false);
169     ASSERT_TRUE(result.isOk());
170     result = mWrapper->setMode(Mode::CAMERA_STREAMING_HIGH, false);
171     ASSERT_TRUE(result.isOk());
172     result = mWrapper->setMode(Mode::AUDIO_STREAMING_LOW_LATENCY, true);
173     ASSERT_TRUE(result.isOk());
174 }
175 
TEST_F(PowerHalWrapperHidlV1_2Test,TestSetModeFailed)176 TEST_F(PowerHalWrapperHidlV1_2Test, TestSetModeFailed) {
177     EXPECT_CALL(*mMockHal.get(), powerHintAsync_1_2(Eq(PowerHintV1_2::LAUNCH), Eq(1)))
178             .Times(Exactly(1))
179             .WillRepeatedly([](PowerHintV1_2, int32_t) {
180                 return hardware::Return<void>(hardware::Status::fromExceptionCode(-1));
181             });
182 
183     auto result = mWrapper->setMode(Mode::LAUNCH, 1);
184     ASSERT_TRUE(result.isFailed());
185 }
186 
TEST_F(PowerHalWrapperHidlV1_2Test,TestSetModeIgnored)187 TEST_F(PowerHalWrapperHidlV1_2Test, TestSetModeIgnored) {
188     EXPECT_CALL(*mMockHal.get(), powerHintAsync_1_2(_, _)).Times(0);
189     EXPECT_CALL(*mMockHal.get(), setInteractive(_)).Times(0);
190     EXPECT_CALL(*mMockHal.get(), setFeature(_, _)).Times(0);
191 
192     auto result = mWrapper->setMode(Mode::EXPENSIVE_RENDERING, true);
193     ASSERT_TRUE(result.isUnsupported());
194     result = mWrapper->setMode(Mode::DISPLAY_INACTIVE, true);
195     ASSERT_TRUE(result.isUnsupported());
196     result = mWrapper->setMode(Mode::FIXED_PERFORMANCE, true);
197     ASSERT_TRUE(result.isUnsupported());
198     result = mWrapper->setMode(Mode::GAME_LOADING, false);
199     ASSERT_TRUE(result.isUnsupported());
200 }
201