1 /*
2  * Copyright (C) 2017 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 #include <android/hardware/power/1.1/IPower.h>
20 #include <gtest/gtest.h>
21 #include <hidl/GtestPrinter.h>
22 #include <hidl/ServiceManagement.h>
23 
24 using ::android::hardware::power::V1_1::IPower;
25 using ::android::hardware::power::V1_1::PowerStateSubsystem;
26 using ::android::hardware::power::V1_0::Status;
27 using ::android::hardware::power::V1_0::PowerHint;
28 using ::android::hardware::hidl_vec;
29 using ::android::hardware::Return;
30 using ::android::sp;
31 
32 class PowerHidlTest : public testing::TestWithParam<std::string> {
33  public:
SetUp()34   virtual void SetUp() override {
35       power = IPower::getService(GetParam());
36       ASSERT_NE(power, nullptr);
37   }
38 
TearDown()39   virtual void TearDown() override {}
40 
41   sp<IPower> power;
42 };
43 
44 // Validate Power::getSubsystemLowPowerStats().
TEST_P(PowerHidlTest,GetSubsystemLowPowerStats)45 TEST_P(PowerHidlTest, GetSubsystemLowPowerStats) {
46   hidl_vec<PowerStateSubsystem> vec;
47   Status s;
48   auto cb = [&vec, &s](hidl_vec<PowerStateSubsystem> subsystems,
49                        Status status) {
50     vec = subsystems;
51     s = status;
52   };
53 
54   Return<void> ret = power->getSubsystemLowPowerStats(cb);
55   ASSERT_TRUE(ret.isOk());
56   ASSERT_TRUE(s == Status::SUCCESS || s == Status::FILESYSTEM_ERROR);
57 }
58 
59 // Validate Power::powerHintAsync on good and bad inputs.
TEST_P(PowerHidlTest,PowerHintAsync)60 TEST_P(PowerHidlTest, PowerHintAsync) {
61     PowerHint badHint = static_cast<PowerHint>(0xA);
62     auto hints = {PowerHint::VSYNC,        PowerHint::INTERACTION, PowerHint::VIDEO_ENCODE,
63                   PowerHint::VIDEO_DECODE, PowerHint::LOW_POWER,   PowerHint::SUSTAINED_PERFORMANCE,
64                   PowerHint::VR_MODE,      PowerHint::LAUNCH,      badHint};
65     Return<void> ret;
66     for (auto hint : hints) {
67         ret = power->powerHintAsync(hint, 30000);
68         ASSERT_TRUE(ret.isOk());
69 
70         ret = power->powerHintAsync(hint, 0);
71         ASSERT_TRUE(ret.isOk());
72     }
73 
74     // Turning these hints on in different orders triggers different code paths,
75     // so iterate over possible orderings.
76     std::vector<PowerHint> hints2 = {PowerHint::LAUNCH, PowerHint::VR_MODE,
77                                      PowerHint::SUSTAINED_PERFORMANCE, PowerHint::INTERACTION};
78     auto compareHints = [](PowerHint l, PowerHint r) {
79         return static_cast<uint32_t>(l) < static_cast<uint32_t>(r);
80     };
81     std::sort(hints2.begin(), hints2.end(), compareHints);
82     do {
83         for (auto iter = hints2.begin(); iter != hints2.end(); iter++) {
84             ret = power->powerHintAsync(*iter, 0);
85             ASSERT_TRUE(ret.isOk());
86         }
87         for (auto iter = hints2.begin(); iter != hints2.end(); iter++) {
88             ret = power->powerHintAsync(*iter, 30000);
89             ASSERT_TRUE(ret.isOk());
90         }
91     } while (std::next_permutation(hints2.begin(), hints2.end(), compareHints));
92 }
93 
94 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerHidlTest);
95 INSTANTIATE_TEST_SUITE_P(
96         PerInstance, PowerHidlTest,
97         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IPower::descriptor)),
98         android::hardware::PrintInstanceNameToString);
99