blob: 32f84e20b6dd29d987f689b511edcfef04523717 [file] [log] [blame]
Lais Andrade4d51f6c2020-03-25 10:58:31 +00001/*
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>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000024#include <gmock/gmock.h>
25#include <gtest/gtest.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000026#include <powermanager/PowerHalWrapper.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000027#include <utils/Log.h>
28
29using android::hardware::power::Boost;
30using android::hardware::power::Mode;
31using android::hardware::power::V1_0::Feature;
32using android::hardware::power::V1_0::PowerHint;
33using IPowerV1_1 = android::hardware::power::V1_1::IPower;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000034
35using namespace android;
Lais Andradeb59a9b52020-05-07 17:23:42 +010036using namespace android::power;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000037using namespace std::chrono_literals;
38using namespace testing;
39
40// -------------------------------------------------------------------------------------------------
41
Lais Andrade4d51f6c2020-03-25 10:58:31 +000042class MockIPowerV1_1 : public IPowerV1_1 {
43public:
44 MOCK_METHOD(hardware::Return<void>, setInteractive, (bool interactive), (override));
45 MOCK_METHOD(hardware::Return<void>, powerHint, (PowerHint hint, int32_t data), (override));
Lais Andradeb59a9b52020-05-07 17:23:42 +010046 MOCK_METHOD(hardware::Return<void>, setFeature, (Feature feature, bool activate), (override));
47 MOCK_METHOD(hardware::Return<void>, getPlatformLowPowerStats,
48 (getPlatformLowPowerStats_cb _hidl_cb), (override));
49 MOCK_METHOD(hardware::Return<void>, powerHintAsync, (PowerHint hint, int32_t data), (override));
50 MOCK_METHOD(hardware::Return<void>, getSubsystemLowPowerStats,
51 (getSubsystemLowPowerStats_cb _hidl_cb), (override));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000052};
53
54// -------------------------------------------------------------------------------------------------
55
56class PowerHalWrapperHidlV1_1Test : public Test {
57public:
58 void SetUp() override;
59
60protected:
Lais Andradeb59a9b52020-05-07 17:23:42 +010061 std::unique_ptr<HalWrapper> mWrapper = nullptr;
Matt Buckleyc3894a42022-09-01 21:17:15 +000062 sp<StrictMock<MockIPowerV1_1>> mMockHal = nullptr;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000063};
64
65// -------------------------------------------------------------------------------------------------
66
67void PowerHalWrapperHidlV1_1Test::SetUp() {
Matt Buckleyc3894a42022-09-01 21:17:15 +000068 mMockHal = new StrictMock<MockIPowerV1_1>();
69 mWrapper = std::make_unique<HidlHalWrapperV1_1>(mMockHal);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000070 ASSERT_NE(mWrapper, nullptr);
Matt Buckleyc3894a42022-09-01 21:17:15 +000071 EXPECT_CALL(*mMockHal.get(), powerHint(_, _)).Times(0);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000072}
73
74// -------------------------------------------------------------------------------------------------
75
76TEST_F(PowerHalWrapperHidlV1_1Test, TestSetBoostSuccessful) {
Matt Buckleyc3894a42022-09-01 21:17:15 +000077 EXPECT_CALL(*mMockHal.get(), powerHintAsync(Eq(PowerHint::INTERACTION), Eq(1000)))
Lais Andradeb59a9b52020-05-07 17:23:42 +010078 .Times(Exactly(1));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000079
80 auto result = mWrapper->setBoost(Boost::INTERACTION, 1000);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080081 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +000082}
83
84TEST_F(PowerHalWrapperHidlV1_1Test, TestSetBoostFailed) {
Matt Buckleyc3894a42022-09-01 21:17:15 +000085 EXPECT_CALL(*mMockHal.get(), powerHintAsync(Eq(PowerHint::INTERACTION), Eq(1000)))
Lais Andradeb59a9b52020-05-07 17:23:42 +010086 .Times(Exactly(1))
87 .WillRepeatedly([](PowerHint, int32_t) {
88 return hardware::Return<void>(hardware::Status::fromExceptionCode(-1));
89 });
Lais Andrade4d51f6c2020-03-25 10:58:31 +000090
91 auto result = mWrapper->setBoost(Boost::INTERACTION, 1000);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080092 ASSERT_TRUE(result.isFailed());
Lais Andrade4d51f6c2020-03-25 10:58:31 +000093}
94
95TEST_F(PowerHalWrapperHidlV1_1Test, TestSetBoostUnsupported) {
Matt Buckleyc3894a42022-09-01 21:17:15 +000096 EXPECT_CALL(*mMockHal.get(), powerHintAsync(_, _)).Times(0);
97 EXPECT_CALL(*mMockHal.get(), setInteractive(_)).Times(0);
98 EXPECT_CALL(*mMockHal.get(), setFeature(_, _)).Times(0);
99
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000100 auto result = mWrapper->setBoost(Boost::CAMERA_LAUNCH, 10);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800101 ASSERT_TRUE(result.isUnsupported());
Matt Buckleyc3894a42022-09-01 21:17:15 +0000102 result = mWrapper->setBoost(Boost::ML_ACC, 10);
103 ASSERT_TRUE(result.isUnsupported());
104 result = mWrapper->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 10);
105 ASSERT_TRUE(result.isUnsupported());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000106}
107
108TEST_F(PowerHalWrapperHidlV1_1Test, TestSetMode) {
109 {
110 InSequence seq;
Matt Buckleyc3894a42022-09-01 21:17:15 +0000111 EXPECT_CALL(*mMockHal.get(), powerHintAsync(Eq(PowerHint::LAUNCH), Eq(true)))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100112 .Times(Exactly(1));
Matt Buckleyc3894a42022-09-01 21:17:15 +0000113 EXPECT_CALL(*mMockHal.get(), powerHintAsync(Eq(PowerHint::LOW_POWER), Eq(false)))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100114 .Times(Exactly(1));
Matt Buckleyc3894a42022-09-01 21:17:15 +0000115 EXPECT_CALL(*mMockHal.get(), powerHintAsync(Eq(PowerHint::SUSTAINED_PERFORMANCE), Eq(true)))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100116 .Times(Exactly(1));
Matt Buckleyc3894a42022-09-01 21:17:15 +0000117 EXPECT_CALL(*mMockHal.get(), powerHintAsync(Eq(PowerHint::VR_MODE), Eq(false)))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100118 .Times(Exactly(1));
Matt Buckleyc3894a42022-09-01 21:17:15 +0000119 EXPECT_CALL(*mMockHal.get(), setInteractive(Eq(true))).Times(Exactly(1));
120 EXPECT_CALL(*mMockHal.get(),
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000121 setFeature(Eq(Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE), Eq(false)))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100122 .Times(Exactly(1));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000123 }
124
125 auto result = mWrapper->setMode(Mode::LAUNCH, true);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800126 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000127 result = mWrapper->setMode(Mode::LOW_POWER, false);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800128 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000129 result = mWrapper->setMode(Mode::SUSTAINED_PERFORMANCE, true);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800130 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000131 result = mWrapper->setMode(Mode::VR, false);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800132 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000133 result = mWrapper->setMode(Mode::INTERACTIVE, true);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800134 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000135 result = mWrapper->setMode(Mode::DOUBLE_TAP_TO_WAKE, false);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800136 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000137}
138
139TEST_F(PowerHalWrapperHidlV1_1Test, TestSetModeFailed) {
Matt Buckleyc3894a42022-09-01 21:17:15 +0000140 EXPECT_CALL(*mMockHal.get(), powerHintAsync(Eq(PowerHint::LAUNCH), Eq(true)))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100141 .Times(Exactly(1))
142 .WillRepeatedly([](PowerHint, int32_t) {
143 return hardware::Return<void>(hardware::Status::fromExceptionCode(-1));
144 });
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000145
146 auto result = mWrapper->setMode(Mode::LAUNCH, 1);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800147 ASSERT_TRUE(result.isFailed());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000148}
149
150TEST_F(PowerHalWrapperHidlV1_1Test, TestSetModeIgnored) {
Matt Buckleyc3894a42022-09-01 21:17:15 +0000151 EXPECT_CALL(*mMockHal.get(), powerHintAsync(_, _)).Times(0);
152 EXPECT_CALL(*mMockHal.get(), setInteractive(_)).Times(0);
153 EXPECT_CALL(*mMockHal.get(), setFeature(_, _)).Times(0);
154
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000155 auto result = mWrapper->setMode(Mode::CAMERA_STREAMING_HIGH, true);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800156 ASSERT_TRUE(result.isUnsupported());
Matt Buckleyc3894a42022-09-01 21:17:15 +0000157 result = mWrapper->setMode(Mode::EXPENSIVE_RENDERING, false);
158 ASSERT_TRUE(result.isUnsupported());
159 result = mWrapper->setMode(Mode::FIXED_PERFORMANCE, true);
160 ASSERT_TRUE(result.isUnsupported());
161 result = mWrapper->setMode(Mode::GAME_LOADING, false);
162 ASSERT_TRUE(result.isUnsupported());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000163}