• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "PowerHalWrapperAidlTest"
18 
19 #include <android/hardware/power/Boost.h>
20 #include <android/hardware/power/IPowerHintSession.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 #include <unistd.h>
29 #include <thread>
30 
31 using android::binder::Status;
32 using android::hardware::power::Boost;
33 using android::hardware::power::IPower;
34 using android::hardware::power::IPowerHintSession;
35 using android::hardware::power::Mode;
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 MockIPower : public IPower {
45 public:
46     MOCK_METHOD(Status, isBoostSupported, (Boost boost, bool* ret), (override));
47     MOCK_METHOD(Status, setBoost, (Boost boost, int32_t durationMs), (override));
48     MOCK_METHOD(Status, isModeSupported, (Mode mode, bool* ret), (override));
49     MOCK_METHOD(Status, setMode, (Mode mode, bool enabled), (override));
50     MOCK_METHOD(Status, createHintSession,
51                 (int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
52                  int64_t durationNanos, sp<IPowerHintSession>* session),
53                 (override));
54     MOCK_METHOD(Status, getHintSessionPreferredRate, (int64_t * rate), (override));
55     MOCK_METHOD(int32_t, getInterfaceVersion, (), (override));
56     MOCK_METHOD(std::string, getInterfaceHash, (), (override));
57     MOCK_METHOD(IBinder*, onAsBinder, (), (override));
58 };
59 
60 // -------------------------------------------------------------------------------------------------
61 
62 class PowerHalWrapperAidlTest : public Test {
63 public:
64     void SetUp() override;
65 
66 protected:
67     std::unique_ptr<HalWrapper> mWrapper = nullptr;
68     sp<StrictMock<MockIPower>> mMockHal = nullptr;
69 };
70 
71 // -------------------------------------------------------------------------------------------------
72 
SetUp()73 void PowerHalWrapperAidlTest::SetUp() {
74     mMockHal = new StrictMock<MockIPower>();
75     mWrapper = std::make_unique<AidlHalWrapper>(mMockHal);
76     ASSERT_NE(nullptr, mWrapper);
77 }
78 
79 // -------------------------------------------------------------------------------------------------
80 
TEST_F(PowerHalWrapperAidlTest,TestSetBoostSuccessful)81 TEST_F(PowerHalWrapperAidlTest, TestSetBoostSuccessful) {
82     {
83         InSequence seq;
84         EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::DISPLAY_UPDATE_IMMINENT), _))
85                 .Times(Exactly(1))
86                 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
87         EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::DISPLAY_UPDATE_IMMINENT), Eq(100)))
88                 .Times(Exactly(1));
89     }
90 
91     auto result = mWrapper->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 100);
92     ASSERT_TRUE(result.isOk());
93 }
94 
TEST_F(PowerHalWrapperAidlTest,TestSetBoostFailed)95 TEST_F(PowerHalWrapperAidlTest, TestSetBoostFailed) {
96     {
97         InSequence seq;
98         EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
99                 .Times(Exactly(1))
100                 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
101         EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::INTERACTION), Eq(100)))
102                 .Times(Exactly(1))
103                 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
104         EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::DISPLAY_UPDATE_IMMINENT), _))
105                 .Times(Exactly(1))
106                 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
107     }
108 
109     auto result = mWrapper->setBoost(Boost::INTERACTION, 100);
110     ASSERT_TRUE(result.isFailed());
111     result = mWrapper->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 1000);
112     ASSERT_TRUE(result.isFailed());
113 }
114 
TEST_F(PowerHalWrapperAidlTest,TestSetBoostUnsupported)115 TEST_F(PowerHalWrapperAidlTest, TestSetBoostUnsupported) {
116     EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
117             .Times(Exactly(1))
118             .WillRepeatedly(DoAll(SetArgPointee<1>(false), Return(Status())));
119 
120     auto result = mWrapper->setBoost(Boost::INTERACTION, 1000);
121     ASSERT_TRUE(result.isUnsupported());
122     result = mWrapper->setBoost(Boost::CAMERA_SHOT, 10);
123     ASSERT_TRUE(result.isUnsupported());
124 }
125 
TEST_F(PowerHalWrapperAidlTest,TestSetBoostMultiThreadCheckSupportedOnlyOnce)126 TEST_F(PowerHalWrapperAidlTest, TestSetBoostMultiThreadCheckSupportedOnlyOnce) {
127     {
128         InSequence seq;
129         EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
130                 .Times(Exactly(1))
131                 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
132         EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::INTERACTION), Eq(100))).Times(Exactly(10));
133     }
134 
135     std::vector<std::thread> threads;
136     for (int i = 0; i < 10; i++) {
137         threads.push_back(std::thread([&]() {
138             auto result = mWrapper->setBoost(Boost::INTERACTION, 100);
139             ASSERT_TRUE(result.isOk());
140         }));
141     }
142     std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
143 }
144 
TEST_F(PowerHalWrapperAidlTest,TestSetModeSuccessful)145 TEST_F(PowerHalWrapperAidlTest, TestSetModeSuccessful) {
146     {
147         InSequence seq;
148         EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::DISPLAY_INACTIVE), _))
149                 .Times(Exactly(1))
150                 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
151         EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::DISPLAY_INACTIVE), Eq(false)))
152                 .Times(Exactly(1));
153     }
154 
155     auto result = mWrapper->setMode(Mode::DISPLAY_INACTIVE, false);
156     ASSERT_TRUE(result.isOk());
157 }
158 
TEST_F(PowerHalWrapperAidlTest,TestSetModeFailed)159 TEST_F(PowerHalWrapperAidlTest, TestSetModeFailed) {
160     {
161         InSequence seq;
162         EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
163                 .Times(Exactly(1))
164                 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
165         EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::LAUNCH), Eq(true)))
166                 .Times(Exactly(1))
167                 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
168         EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::DISPLAY_INACTIVE), _))
169                 .Times(Exactly(1))
170                 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
171     }
172 
173     auto result = mWrapper->setMode(Mode::LAUNCH, true);
174     ASSERT_TRUE(result.isFailed());
175     result = mWrapper->setMode(Mode::DISPLAY_INACTIVE, false);
176     ASSERT_TRUE(result.isFailed());
177 }
178 
TEST_F(PowerHalWrapperAidlTest,TestSetModeUnsupported)179 TEST_F(PowerHalWrapperAidlTest, TestSetModeUnsupported) {
180     EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
181             .Times(Exactly(1))
182             .WillRepeatedly(DoAll(SetArgPointee<1>(false), Return(Status())));
183 
184     auto result = mWrapper->setMode(Mode::LAUNCH, true);
185     ASSERT_TRUE(result.isUnsupported());
186 
187     EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::CAMERA_STREAMING_HIGH), _))
188             .Times(Exactly(1))
189             .WillRepeatedly(DoAll(SetArgPointee<1>(false), Return(Status())));
190     result = mWrapper->setMode(Mode::CAMERA_STREAMING_HIGH, true);
191     ASSERT_TRUE(result.isUnsupported());
192 }
193 
TEST_F(PowerHalWrapperAidlTest,TestSetModeMultiThreadCheckSupportedOnlyOnce)194 TEST_F(PowerHalWrapperAidlTest, TestSetModeMultiThreadCheckSupportedOnlyOnce) {
195     {
196         InSequence seq;
197         EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
198                 .Times(Exactly(1))
199                 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
200         EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::LAUNCH), Eq(false))).Times(Exactly(10));
201     }
202 
203     std::vector<std::thread> threads;
204     for (int i = 0; i < 10; i++) {
205         threads.push_back(std::thread([&]() {
206             auto result = mWrapper->setMode(Mode::LAUNCH, false);
207             ASSERT_TRUE(result.isOk());
208         }));
209     }
210     std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
211 }
212 
TEST_F(PowerHalWrapperAidlTest,TestCreateHintSessionSuccessful)213 TEST_F(PowerHalWrapperAidlTest, TestCreateHintSessionSuccessful) {
214     std::vector<int> threadIds{gettid()};
215     int32_t tgid = 999;
216     int32_t uid = 1001;
217     int64_t durationNanos = 16666666L;
218     EXPECT_CALL(*mMockHal.get(),
219                 createHintSession(Eq(tgid), Eq(uid), Eq(threadIds), Eq(durationNanos), _))
220             .Times(Exactly(1));
221     auto result = mWrapper->createHintSession(tgid, uid, threadIds, durationNanos);
222     ASSERT_TRUE(result.isOk());
223 }
224 
TEST_F(PowerHalWrapperAidlTest,TestCreateHintSessionFailed)225 TEST_F(PowerHalWrapperAidlTest, TestCreateHintSessionFailed) {
226     int32_t tgid = 999;
227     int32_t uid = 1001;
228     std::vector<int> threadIds{};
229     int64_t durationNanos = 16666666L;
230     EXPECT_CALL(*mMockHal.get(),
231                 createHintSession(Eq(tgid), Eq(uid), Eq(threadIds), Eq(durationNanos), _))
232             .Times(Exactly(1))
233             .WillRepeatedly(Return(Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT)));
234     auto result = mWrapper->createHintSession(tgid, uid, threadIds, durationNanos);
235     ASSERT_TRUE(result.isFailed());
236 }
237 
TEST_F(PowerHalWrapperAidlTest,TestGetHintSessionPreferredRate)238 TEST_F(PowerHalWrapperAidlTest, TestGetHintSessionPreferredRate) {
239     EXPECT_CALL(*mMockHal.get(), getHintSessionPreferredRate(_)).Times(Exactly(1));
240     auto result = mWrapper->getHintSessionPreferredRate();
241     ASSERT_TRUE(result.isOk());
242     int64_t rate = result.value();
243     ASSERT_GE(0, rate);
244 }
245