blob: 73b74666706e4ea864fca63310d4c9bb361642de [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 "PowerHalWrapperAidlTest"
18
19#include <android/hardware/power/Boost.h>
20#include <android/hardware/power/Mode.h>
21#include <binder/IServiceManager.h>
22
23#include <gmock/gmock.h>
24#include <gtest/gtest.h>
25
26#include <powermanager/PowerHalWrapper.h>
27
28#include <thread>
29#include <utils/Log.h>
30
31using android::binder::Status;
32using android::hardware::power::Boost;
33using android::hardware::power::IPower;
34using android::hardware::power::Mode;
35
36using namespace android;
37using namespace std::chrono_literals;
38using namespace testing;
39
40// -------------------------------------------------------------------------------------------------
41
42class MockIPower : public IPower {
43public:
44 MOCK_METHOD(Status, isBoostSupported, (Boost boost, bool* ret), (override));
45 MOCK_METHOD(Status, setBoost, (Boost boost, int32_t durationMs), (override));
46 MOCK_METHOD(Status, isModeSupported, (Mode mode, bool* ret), (override));
47 MOCK_METHOD(Status, setMode, (Mode mode, bool enabled), (override));
48 MOCK_METHOD(int32_t, getInterfaceVersion, (), (override));
49 MOCK_METHOD(std::string, getInterfaceHash, (), (override));
50 MOCK_METHOD(IBinder*, onAsBinder, (), (override));
51};
52
53// -------------------------------------------------------------------------------------------------
54
55class PowerHalWrapperAidlTest : public Test {
56public:
57 void SetUp() override;
58
59protected:
60 std::unique_ptr<PowerHalWrapper> mWrapper = nullptr;
61 sp<StrictMock<MockIPower>> mMockHal = nullptr;
62};
63
64// -------------------------------------------------------------------------------------------------
65
66void PowerHalWrapperAidlTest::SetUp() {
67 mMockHal = new StrictMock<MockIPower>();
68 mWrapper = std::make_unique<AidlPowerHalWrapper>(mMockHal);
69 ASSERT_NE(mWrapper, nullptr);
70}
71
72// -------------------------------------------------------------------------------------------------
73
74TEST_F(PowerHalWrapperAidlTest, TestSetBoostSuccessful) {
75 {
76 InSequence seq;
77 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::DISPLAY_UPDATE_IMMINENT), _))
78 .Times(Exactly(1))
79 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
80 EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::DISPLAY_UPDATE_IMMINENT), Eq(100)))
81 .Times(Exactly(1));
82 }
83
84 auto result = mWrapper->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 100);
85 ASSERT_EQ(PowerHalResult::SUCCESSFUL, result);
86}
87
88TEST_F(PowerHalWrapperAidlTest, TestSetBoostFailed) {
89 {
90 InSequence seq;
91 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
92 .Times(Exactly(1))
93 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
94 EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::INTERACTION), Eq(100)))
95 .Times(Exactly(1))
96 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
97 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::DISPLAY_UPDATE_IMMINENT), _))
98 .Times(Exactly(1))
99 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
100 }
101
102 auto result = mWrapper->setBoost(Boost::INTERACTION, 100);
103 ASSERT_EQ(PowerHalResult::FAILED, result);
104 result = mWrapper->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 1000);
105 ASSERT_EQ(PowerHalResult::FAILED, result);
106}
107
108TEST_F(PowerHalWrapperAidlTest, TestSetBoostUnsupported) {
109 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
110 .Times(Exactly(1))
111 .WillRepeatedly(DoAll(SetArgPointee<1>(false), Return(Status())));
112
113 auto result = mWrapper->setBoost(Boost::INTERACTION, 1000);
114 ASSERT_EQ(PowerHalResult::UNSUPPORTED, result);
115 result = mWrapper->setBoost(Boost::CAMERA_SHOT, 10);
116 ASSERT_EQ(PowerHalResult::UNSUPPORTED, result);
117}
118
119TEST_F(PowerHalWrapperAidlTest, TestSetBoostMultiThreadCheckSupportedOnlyOnce) {
120 {
121 InSequence seq;
122 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
123 .Times(Exactly(1))
124 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
125 EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::INTERACTION), Eq(100)))
126 .Times(Exactly(10));
127 }
128
129 std::vector<std::thread> threads;
130 for (int i = 0; i < 10; i++) {
131 threads.push_back(std::thread([&]() {
132 auto result = mWrapper->setBoost(Boost::INTERACTION, 100);
133 ASSERT_EQ(PowerHalResult::SUCCESSFUL, result);
134 }));
135 }
136 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
137}
138
139TEST_F(PowerHalWrapperAidlTest, TestSetModeSuccessful) {
140 {
141 InSequence seq;
142 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::DISPLAY_INACTIVE), _))
143 .Times(Exactly(1))
144 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
145 EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::DISPLAY_INACTIVE), Eq(false)))
146 .Times(Exactly(1));
147 }
148
149 auto result = mWrapper->setMode(Mode::DISPLAY_INACTIVE, false);
150 ASSERT_EQ(PowerHalResult::SUCCESSFUL, result);
151}
152
153TEST_F(PowerHalWrapperAidlTest, TestSetModeFailed) {
154 {
155 InSequence seq;
156 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
157 .Times(Exactly(1))
158 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
159 EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::LAUNCH), Eq(true)))
160 .Times(Exactly(1))
161 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
162 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::DISPLAY_INACTIVE), _))
163 .Times(Exactly(1))
164 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
165 }
166
167 auto result = mWrapper->setMode(Mode::LAUNCH, true);
168 ASSERT_EQ(PowerHalResult::FAILED, result);
169 result = mWrapper->setMode(Mode::DISPLAY_INACTIVE, false);
170 ASSERT_EQ(PowerHalResult::FAILED, result);
171}
172
173TEST_F(PowerHalWrapperAidlTest, TestSetModeUnsupported) {
174 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
175 .Times(Exactly(1))
176 .WillRepeatedly(DoAll(SetArgPointee<1>(false), Return(Status())));
177
178 auto result = mWrapper->setMode(Mode::LAUNCH, true);
179 ASSERT_EQ(PowerHalResult::UNSUPPORTED, result);
180 result = mWrapper->setMode(Mode::CAMERA_STREAMING_HIGH, true);
181 ASSERT_EQ(PowerHalResult::UNSUPPORTED, result);
182}
183
184TEST_F(PowerHalWrapperAidlTest, TestSetModeMultiThreadCheckSupportedOnlyOnce) {
185 {
186 InSequence seq;
187 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
188 .Times(Exactly(1))
189 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
190 EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::LAUNCH), Eq(false)))
191 .Times(Exactly(10));
192 }
193
194 std::vector<std::thread> threads;
195 for (int i = 0; i < 10; i++) {
196 threads.push_back(std::thread([&]() {
197 auto result = mWrapper->setMode(Mode::LAUNCH, false);
198 ASSERT_EQ(PowerHalResult::SUCCESSFUL, result);
199 }));
200 }
201 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
202}