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
21 #include <VtsHalHidlTargetTestBase.h>
22 #include <VtsHalHidlTargetTestEnvBase.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 // Test environment for Power HIDL HAL.
33 class PowerHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
34 public:
35 // get the test environment singleton
Instance()36 static PowerHidlEnvironment* Instance() {
37 static PowerHidlEnvironment* instance = new PowerHidlEnvironment;
38 return instance;
39 }
40
registerTestServices()41 virtual void registerTestServices() override { registerTestService<IPower>(); }
42 };
43
44 class PowerHidlTest : public ::testing::VtsHalHidlTargetTestBase {
45 public:
SetUp()46 virtual void SetUp() override {
47 power = ::testing::VtsHalHidlTargetTestBase::getService<IPower>(
48 PowerHidlEnvironment::Instance()->getServiceName<IPower>());
49 ASSERT_NE(power, nullptr);
50 }
51
TearDown()52 virtual void TearDown() override {}
53
54 sp<IPower> power;
55 };
56
57 // Sanity check Power::getSubsystemLowPowerStats().
TEST_F(PowerHidlTest,GetSubsystemLowPowerStats)58 TEST_F(PowerHidlTest, GetSubsystemLowPowerStats) {
59 hidl_vec<PowerStateSubsystem> vec;
60 Status s;
61 auto cb = [&vec, &s](hidl_vec<PowerStateSubsystem> subsystems,
62 Status status) {
63 vec = subsystems;
64 s = status;
65 };
66
67 Return<void> ret = power->getSubsystemLowPowerStats(cb);
68 ASSERT_TRUE(ret.isOk());
69 ASSERT_TRUE(s == Status::SUCCESS || s == Status::FILESYSTEM_ERROR);
70 }
71
72 // Sanity check Power::powerHintAsync on good and bad inputs.
TEST_F(PowerHidlTest,PowerHintAsync)73 TEST_F(PowerHidlTest, PowerHintAsync) {
74 PowerHint badHint = static_cast<PowerHint>(0xA);
75 auto hints = {PowerHint::VSYNC, PowerHint::INTERACTION, PowerHint::VIDEO_ENCODE,
76 PowerHint::VIDEO_DECODE, PowerHint::LOW_POWER, PowerHint::SUSTAINED_PERFORMANCE,
77 PowerHint::VR_MODE, PowerHint::LAUNCH, badHint};
78 Return<void> ret;
79 for (auto hint : hints) {
80 ret = power->powerHintAsync(hint, 30000);
81 ASSERT_TRUE(ret.isOk());
82
83 ret = power->powerHintAsync(hint, 0);
84 ASSERT_TRUE(ret.isOk());
85 }
86
87 // Turning these hints on in different orders triggers different code paths,
88 // so iterate over possible orderings.
89 std::vector<PowerHint> hints2 = {PowerHint::LAUNCH, PowerHint::VR_MODE,
90 PowerHint::SUSTAINED_PERFORMANCE, PowerHint::INTERACTION};
91 auto compareHints = [](PowerHint l, PowerHint r) {
92 return static_cast<uint32_t>(l) < static_cast<uint32_t>(r);
93 };
94 std::sort(hints2.begin(), hints2.end(), compareHints);
95 do {
96 for (auto iter = hints2.begin(); iter != hints2.end(); iter++) {
97 ret = power->powerHintAsync(*iter, 0);
98 ASSERT_TRUE(ret.isOk());
99 }
100 for (auto iter = hints2.begin(); iter != hints2.end(); iter++) {
101 ret = power->powerHintAsync(*iter, 30000);
102 ASSERT_TRUE(ret.isOk());
103 }
104 } while (std::next_permutation(hints2.begin(), hints2.end(), compareHints));
105 }
106
main(int argc,char ** argv)107 int main(int argc, char **argv) {
108 ::testing::AddGlobalTestEnvironment(PowerHidlEnvironment::Instance());
109 ::testing::InitGoogleTest(&argc, argv);
110 PowerHidlEnvironment::Instance()->init(&argc, argv);
111 int status = RUN_ALL_TESTS();
112 LOG(INFO) << "Test result = " << status;
113 return status;
114 }
115