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