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