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