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