• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     return;
81   }
82 
83   char old_governor[80];
84   ASSERT_LE(0, read(fd1, old_governor, 80));
85 
86   char governors[1024];
87   unsigned len = read(fd2, governors, 1024);
88   ASSERT_LE(0u, len);
89   governors[len] = '\0';
90 
91   char *saveptr;
92   char *name = strtok_r(governors, " \n", &saveptr);
93   while (name) {
94     ASSERT_LE(0, write(fd1, name, strlen(name)));
95     ret = power->setInteractive(true);
96     ASSERT_TRUE(ret.isOk());
97 
98     ret = power->setInteractive(false);
99     ASSERT_TRUE(ret.isOk());
100 
101     ret = power->setInteractive(false);
102     ASSERT_TRUE(ret.isOk());
103 
104     power->powerHint(PowerHint::LAUNCH, 1);
105     power->powerHint(PowerHint::LAUNCH, 0);
106 
107     name = strtok_r(NULL, " \n", &saveptr);
108   }
109 
110   ASSERT_LE(0, write(fd1, old_governor, strlen(old_governor)));
111 }
112 
113 // Sanity check Power::powerHint on good and bad inputs.
TEST_F(PowerHidlTest,PowerHint)114 TEST_F(PowerHidlTest, PowerHint) {
115   PowerHint badHint = static_cast<PowerHint>(0xA);
116   auto hints = {PowerHint::VSYNC,         PowerHint::INTERACTION,
117                 PowerHint::VIDEO_ENCODE,  PowerHint::VIDEO_DECODE,
118                 PowerHint::LOW_POWER,     PowerHint::SUSTAINED_PERFORMANCE,
119                 PowerHint::VR_MODE,       PowerHint::LAUNCH,
120                 badHint};
121   Return<void> ret;
122   for (auto hint : hints) {
123     ret = power->powerHint(hint, 30000);
124     ASSERT_TRUE(ret.isOk());
125 
126     ret = power->powerHint(hint, 0);
127     ASSERT_TRUE(ret.isOk());
128   }
129 
130   // Turning these hints on in different orders triggers different code paths,
131   // so iterate over possible orderings.
132   std::vector<PowerHint> hints2 = {PowerHint::LAUNCH, PowerHint::VR_MODE,
133                                    PowerHint::SUSTAINED_PERFORMANCE,
134                                    PowerHint::INTERACTION};
135   auto compareHints = [](PowerHint l, PowerHint r) {
136     return static_cast<uint32_t>(l) < static_cast<uint32_t>(r);
137   };
138   std::sort(hints2.begin(), hints2.end(), compareHints);
139   do {
140     for (auto iter = hints2.begin(); iter != hints2.end(); iter++) {
141       ret = power->powerHint(*iter, 0);
142       ASSERT_TRUE(ret.isOk());
143     }
144     for (auto iter = hints2.begin(); iter != hints2.end(); iter++) {
145       ret = power->powerHint(*iter, 30000);
146       ASSERT_TRUE(ret.isOk());
147     }
148   } while (std::next_permutation(hints2.begin(), hints2.end(), compareHints));
149 }
150 
151 // Sanity check Power::setFeature() on good and bad inputs.
TEST_F(PowerHidlTest,SetFeature)152 TEST_F(PowerHidlTest, SetFeature) {
153   Return<void> ret;
154   ret = power->setFeature(Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE, true);
155   ASSERT_TRUE(ret.isOk());
156   ret = power->setFeature(Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE, false);
157   ASSERT_TRUE(ret.isOk());
158 
159   Feature badFeature = static_cast<Feature>(0x2);
160   ret = power->setFeature(badFeature, true);
161   ASSERT_TRUE(ret.isOk());
162   ret = power->setFeature(badFeature, false);
163   ASSERT_TRUE(ret.isOk());
164 }
165 
166 // Sanity check Power::getPlatformLowPowerStats().
TEST_F(PowerHidlTest,GetPlatformLowPowerStats)167 TEST_F(PowerHidlTest, GetPlatformLowPowerStats) {
168   hidl_vec<PowerStatePlatformSleepState> vec;
169   Status s;
170   auto cb = [&vec, &s](hidl_vec<PowerStatePlatformSleepState> states,
171                        Status status) {
172     vec = states;
173     s = status;
174   };
175   Return<void> ret = power->getPlatformLowPowerStats(cb);
176   ASSERT_TRUE(ret.isOk());
177   ASSERT_TRUE(s == Status::SUCCESS || s == Status::FILESYSTEM_ERROR);
178 }
179 
main(int argc,char ** argv)180 int main(int argc, char **argv) {
181   ::testing::InitGoogleTest(&argc, argv);
182   int status = RUN_ALL_TESTS();
183   LOG(INFO) << "Test result = " << status;
184   return status;
185 }
186