1 /*
2 * Copyright (C) 2016 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 "power_hidl_hal_test"
18 #include <android-base/logging.h>
19
20 #include <cutils/properties.h>
21
22 #include <android-base/unique_fd.h>
23 #include <android/hardware/power/1.0/IPower.h>
24
25 #include <VtsHalHidlTargetTestBase.h>
26 #include <VtsHalHidlTargetTestEnvBase.h>
27
28 #include <fcntl.h>
29 #include <algorithm>
30
31 using ::android::hardware::power::V1_0::IPower;
32 using ::android::hardware::power::V1_0::Feature;
33 using ::android::hardware::power::V1_0::PowerHint;
34 using ::android::hardware::power::V1_0::PowerStatePlatformSleepState;
35 using ::android::hardware::power::V1_0::Status;
36 using ::android::hardware::hidl_vec;
37 using ::android::hardware::Return;
38 using ::android::sp;
39 using ::android::base::unique_fd;
40
41 using std::vector;
42
43 #define CPU_GOVERNOR_PATH \
44 "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
45 #define AVAILABLE_GOVERNORS_PATH \
46 "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors"
47
48 // Test environment for Power HIDL HAL.
49 class PowerHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
50 public:
51 // get the test environment singleton
Instance()52 static PowerHidlEnvironment* Instance() {
53 static PowerHidlEnvironment* instance = new PowerHidlEnvironment;
54 return instance;
55 }
56
registerTestServices()57 virtual void registerTestServices() override { registerTestService<IPower>(); }
58 };
59
60 class PowerHidlTest : public ::testing::VtsHalHidlTargetTestBase {
61 public:
SetUp()62 virtual void SetUp() override {
63 power = ::testing::VtsHalHidlTargetTestBase::getService<IPower>(
64 PowerHidlEnvironment::Instance()->getServiceName<IPower>());
65 ASSERT_NE(power, nullptr);
66 }
67
TearDown()68 virtual void TearDown() override {}
69
70 sp<IPower> power;
71 };
72
73 // Sanity check Power::setInteractive.
TEST_F(PowerHidlTest,SetInteractive)74 TEST_F(PowerHidlTest, SetInteractive) {
75 Return<void> ret;
76
77 ret = power->setInteractive(true);
78 ASSERT_TRUE(ret.isOk());
79
80 ret = power->setInteractive(false);
81 ASSERT_TRUE(ret.isOk());
82 }
83
84 // Test Power::setInteractive and Power::powerHint(Launch)
85 // with each available CPU governor, if available
TEST_F(PowerHidlTest,TryDifferentGovernors)86 TEST_F(PowerHidlTest, TryDifferentGovernors) {
87 Return<void> ret;
88
89 unique_fd fd1(open(CPU_GOVERNOR_PATH, O_RDWR));
90 unique_fd fd2(open(AVAILABLE_GOVERNORS_PATH, O_RDONLY));
91 if (fd1 < 0 || fd2 < 0) {
92 // Files don't exist, so skip the rest of the test case
93 SUCCEED();
94 return;
95 }
96
97 char old_governor[80];
98 ASSERT_LE(0, read(fd1, old_governor, 80));
99
100 char governors[1024];
101 unsigned len = read(fd2, governors, 1024);
102 ASSERT_LE(0u, len);
103 governors[len] = '\0';
104
105 char *saveptr;
106 char *name = strtok_r(governors, " \n", &saveptr);
107 while (name) {
108 ASSERT_LE(0, write(fd1, name, strlen(name)));
109 ret = power->setInteractive(true);
110 ASSERT_TRUE(ret.isOk());
111
112 ret = power->setInteractive(false);
113 ASSERT_TRUE(ret.isOk());
114
115 ret = power->setInteractive(false);
116 ASSERT_TRUE(ret.isOk());
117
118 power->powerHint(PowerHint::LAUNCH, 1);
119 power->powerHint(PowerHint::LAUNCH, 0);
120
121 name = strtok_r(NULL, " \n", &saveptr);
122 }
123
124 ASSERT_LE(0, write(fd1, old_governor, strlen(old_governor)));
125 }
126
127 // Sanity check Power::powerHint on good and bad inputs.
TEST_F(PowerHidlTest,PowerHint)128 TEST_F(PowerHidlTest, PowerHint) {
129 PowerHint badHint = static_cast<PowerHint>(0xA);
130 auto hints = {PowerHint::VSYNC, PowerHint::INTERACTION,
131 PowerHint::VIDEO_ENCODE, PowerHint::VIDEO_DECODE,
132 PowerHint::LOW_POWER, PowerHint::SUSTAINED_PERFORMANCE,
133 PowerHint::VR_MODE, PowerHint::LAUNCH,
134 badHint};
135 Return<void> ret;
136 for (auto hint : hints) {
137 ret = power->powerHint(hint, 30000);
138 ASSERT_TRUE(ret.isOk());
139
140 ret = power->powerHint(hint, 0);
141 ASSERT_TRUE(ret.isOk());
142 }
143
144 // Turning these hints on in different orders triggers different code paths,
145 // so iterate over possible orderings.
146 std::vector<PowerHint> hints2 = {PowerHint::LAUNCH, PowerHint::VR_MODE,
147 PowerHint::SUSTAINED_PERFORMANCE,
148 PowerHint::INTERACTION};
149 auto compareHints = [](PowerHint l, PowerHint r) {
150 return static_cast<uint32_t>(l) < static_cast<uint32_t>(r);
151 };
152 std::sort(hints2.begin(), hints2.end(), compareHints);
153 do {
154 for (auto iter = hints2.begin(); iter != hints2.end(); iter++) {
155 ret = power->powerHint(*iter, 0);
156 ASSERT_TRUE(ret.isOk());
157 }
158 for (auto iter = hints2.begin(); iter != hints2.end(); iter++) {
159 ret = power->powerHint(*iter, 30000);
160 ASSERT_TRUE(ret.isOk());
161 }
162 } while (std::next_permutation(hints2.begin(), hints2.end(), compareHints));
163 }
164
165 // Sanity check Power::setFeature() on good and bad inputs.
TEST_F(PowerHidlTest,SetFeature)166 TEST_F(PowerHidlTest, SetFeature) {
167 Return<void> ret;
168 ret = power->setFeature(Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE, true);
169 ASSERT_TRUE(ret.isOk());
170 ret = power->setFeature(Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE, false);
171 ASSERT_TRUE(ret.isOk());
172
173 Feature badFeature = static_cast<Feature>(0x2);
174 ret = power->setFeature(badFeature, true);
175 ASSERT_TRUE(ret.isOk());
176 ret = power->setFeature(badFeature, false);
177 ASSERT_TRUE(ret.isOk());
178 }
179
180 // Sanity check Power::getPlatformLowPowerStats().
TEST_F(PowerHidlTest,GetPlatformLowPowerStats)181 TEST_F(PowerHidlTest, GetPlatformLowPowerStats) {
182 hidl_vec<PowerStatePlatformSleepState> vec;
183 Status s;
184 auto cb = [&vec, &s](hidl_vec<PowerStatePlatformSleepState> states,
185 Status status) {
186 vec = states;
187 s = status;
188 };
189 Return<void> ret = power->getPlatformLowPowerStats(cb);
190 ASSERT_TRUE(ret.isOk());
191 ASSERT_TRUE(s == Status::SUCCESS || s == Status::FILESYSTEM_ERROR);
192 }
193
main(int argc,char ** argv)194 int main(int argc, char **argv) {
195 ::testing::AddGlobalTestEnvironment(PowerHidlEnvironment::Instance());
196 ::testing::InitGoogleTest(&argc, argv);
197 PowerHidlEnvironment::Instance()->init(&argc, argv);
198 int status = RUN_ALL_TESTS();
199 LOG(INFO) << "Test result = " << status;
200 return status;
201 }
202