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 #define LOG_TAG "PowerHalWrapperHidlV1_1Test"
18
19 #include <android/hardware/power/1.1/IPower.h>
20 #include <android/hardware/power/Boost.h>
21 #include <android/hardware/power/IPower.h>
22 #include <android/hardware/power/Mode.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 android::hardware::power::Boost;
30 using android::hardware::power::Mode;
31 using android::hardware::power::V1_0::Feature;
32 using android::hardware::power::V1_0::PowerHint;
33 using IPowerV1_1 = android::hardware::power::V1_1::IPower;
34 using IPowerV1_0 = android::hardware::power::V1_0::IPower;
35
36 using namespace android;
37 using namespace android::power;
38 using namespace std::chrono_literals;
39 using namespace testing;
40
41 // -------------------------------------------------------------------------------------------------
42
43 class MockIPowerV1_0 : public IPowerV1_0 {
44 public:
45 MOCK_METHOD(hardware::Return<void>, setInteractive, (bool interactive), (override));
46 MOCK_METHOD(hardware::Return<void>, powerHint, (PowerHint hint, int32_t data), (override));
47 MOCK_METHOD(hardware::Return<void>, setFeature, (Feature feature, bool activate), (override));
48 MOCK_METHOD(hardware::Return<void>, getPlatformLowPowerStats,
49 (getPlatformLowPowerStats_cb _hidl_cb), (override));
50 };
51
52 class MockIPowerV1_1 : public IPowerV1_1 {
53 public:
54 MOCK_METHOD(hardware::Return<void>, setInteractive, (bool interactive), (override));
55 MOCK_METHOD(hardware::Return<void>, powerHint, (PowerHint hint, int32_t data), (override));
56 MOCK_METHOD(hardware::Return<void>, setFeature, (Feature feature, bool activate), (override));
57 MOCK_METHOD(hardware::Return<void>, getPlatformLowPowerStats,
58 (getPlatformLowPowerStats_cb _hidl_cb), (override));
59 MOCK_METHOD(hardware::Return<void>, powerHintAsync, (PowerHint hint, int32_t data), (override));
60 MOCK_METHOD(hardware::Return<void>, getSubsystemLowPowerStats,
61 (getSubsystemLowPowerStats_cb _hidl_cb), (override));
62 };
63
64 // -------------------------------------------------------------------------------------------------
65
66 class PowerHalWrapperHidlV1_1Test : public Test {
67 public:
68 void SetUp() override;
69
70 protected:
71 std::unique_ptr<HalWrapper> mWrapper = nullptr;
72 sp<StrictMock<MockIPowerV1_0>> mMockHalV1_0 = nullptr;
73 sp<StrictMock<MockIPowerV1_1>> mMockHalV1_1 = nullptr;
74 };
75
76 // -------------------------------------------------------------------------------------------------
77
SetUp()78 void PowerHalWrapperHidlV1_1Test::SetUp() {
79 mMockHalV1_0 = new StrictMock<MockIPowerV1_0>();
80 mMockHalV1_1 = new StrictMock<MockIPowerV1_1>();
81 mWrapper = std::make_unique<HidlHalWrapperV1_1>(mMockHalV1_0, mMockHalV1_1);
82 ASSERT_NE(mWrapper, nullptr);
83 }
84
85 // -------------------------------------------------------------------------------------------------
86
TEST_F(PowerHalWrapperHidlV1_1Test,TestSetBoostSuccessful)87 TEST_F(PowerHalWrapperHidlV1_1Test, TestSetBoostSuccessful) {
88 EXPECT_CALL(*mMockHalV1_1.get(), powerHintAsync(Eq(PowerHint::INTERACTION), Eq(1000)))
89 .Times(Exactly(1));
90
91 auto result = mWrapper->setBoost(Boost::INTERACTION, 1000);
92 ASSERT_TRUE(result.isOk());
93 }
94
TEST_F(PowerHalWrapperHidlV1_1Test,TestSetBoostFailed)95 TEST_F(PowerHalWrapperHidlV1_1Test, TestSetBoostFailed) {
96 EXPECT_CALL(*mMockHalV1_1.get(), powerHintAsync(Eq(PowerHint::INTERACTION), Eq(1000)))
97 .Times(Exactly(1))
98 .WillRepeatedly([](PowerHint, int32_t) {
99 return hardware::Return<void>(hardware::Status::fromExceptionCode(-1));
100 });
101
102 auto result = mWrapper->setBoost(Boost::INTERACTION, 1000);
103 ASSERT_TRUE(result.isFailed());
104 }
105
TEST_F(PowerHalWrapperHidlV1_1Test,TestSetBoostUnsupported)106 TEST_F(PowerHalWrapperHidlV1_1Test, TestSetBoostUnsupported) {
107 auto result = mWrapper->setBoost(Boost::CAMERA_LAUNCH, 10);
108 ASSERT_TRUE(result.isUnsupported());
109 }
110
TEST_F(PowerHalWrapperHidlV1_1Test,TestSetMode)111 TEST_F(PowerHalWrapperHidlV1_1Test, TestSetMode) {
112 {
113 InSequence seq;
114 EXPECT_CALL(*mMockHalV1_1.get(), powerHintAsync(Eq(PowerHint::LAUNCH), Eq(1)))
115 .Times(Exactly(1));
116 EXPECT_CALL(*mMockHalV1_1.get(), powerHintAsync(Eq(PowerHint::LOW_POWER), Eq(0)))
117 .Times(Exactly(1));
118 EXPECT_CALL(*mMockHalV1_1.get(),
119 powerHintAsync(Eq(PowerHint::SUSTAINED_PERFORMANCE), Eq(1)))
120 .Times(Exactly(1));
121 EXPECT_CALL(*mMockHalV1_1.get(), powerHintAsync(Eq(PowerHint::VR_MODE), Eq(0)))
122 .Times(Exactly(1));
123 EXPECT_CALL(*mMockHalV1_0.get(), setInteractive(Eq(true))).Times(Exactly(1));
124 EXPECT_CALL(*mMockHalV1_0.get(),
125 setFeature(Eq(Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE), Eq(false)))
126 .Times(Exactly(1));
127 }
128
129 auto result = mWrapper->setMode(Mode::LAUNCH, true);
130 ASSERT_TRUE(result.isOk());
131 result = mWrapper->setMode(Mode::LOW_POWER, false);
132 ASSERT_TRUE(result.isOk());
133 result = mWrapper->setMode(Mode::SUSTAINED_PERFORMANCE, true);
134 ASSERT_TRUE(result.isOk());
135 result = mWrapper->setMode(Mode::VR, false);
136 ASSERT_TRUE(result.isOk());
137 result = mWrapper->setMode(Mode::INTERACTIVE, true);
138 ASSERT_TRUE(result.isOk());
139 result = mWrapper->setMode(Mode::DOUBLE_TAP_TO_WAKE, false);
140 ASSERT_TRUE(result.isOk());
141 }
142
TEST_F(PowerHalWrapperHidlV1_1Test,TestSetModeFailed)143 TEST_F(PowerHalWrapperHidlV1_1Test, TestSetModeFailed) {
144 EXPECT_CALL(*mMockHalV1_1.get(), powerHintAsync(Eq(PowerHint::LAUNCH), Eq(1)))
145 .Times(Exactly(1))
146 .WillRepeatedly([](PowerHint, int32_t) {
147 return hardware::Return<void>(hardware::Status::fromExceptionCode(-1));
148 });
149
150 auto result = mWrapper->setMode(Mode::LAUNCH, 1);
151 ASSERT_TRUE(result.isFailed());
152 }
153
TEST_F(PowerHalWrapperHidlV1_1Test,TestSetModeIgnored)154 TEST_F(PowerHalWrapperHidlV1_1Test, TestSetModeIgnored) {
155 auto result = mWrapper->setMode(Mode::CAMERA_STREAMING_HIGH, true);
156 ASSERT_TRUE(result.isUnsupported());
157 }
158