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.2/IPower.h>
20 #include <gtest/gtest.h>
21 #include <hidl/GtestPrinter.h>
22 #include <hidl/ServiceManagement.h>
23 
24 using ::android::sp;
25 using ::android::hardware::hidl_vec;
26 using ::android::hardware::Return;
27 using ::android::hardware::power::V1_2::IPower;
28 using ::android::hardware::power::V1_2::PowerHint;
29 
30 class PowerHidlTest : public testing::TestWithParam<std::string> {
31    public:
SetUp()32     virtual void SetUp() override {
33         power = IPower::getService(GetParam());
34         ASSERT_NE(power, nullptr);
35     }
36 
37     sp<IPower> power;
38 };
39 
40 // Validate Power::PowerHintAsync_1_2 on good and bad inputs.
TEST_P(PowerHidlTest,PowerHintAsync_1_2)41 TEST_P(PowerHidlTest, PowerHintAsync_1_2) {
42     std::vector<PowerHint> hints;
43     for (uint32_t i = static_cast<uint32_t>(PowerHint::VSYNC);
44          i <= static_cast<uint32_t>(PowerHint::CAMERA_SHOT); ++i) {
45         hints.emplace_back(static_cast<PowerHint>(i));
46     }
47     PowerHint badHint = static_cast<PowerHint>(0xFF);
48     hints.emplace_back(badHint);
49 
50     Return<void> ret;
51     for (auto& hint : hints) {
52         ret = power->powerHintAsync_1_2(hint, 30000);
53         ASSERT_TRUE(ret.isOk());
54 
55         ret = power->powerHintAsync_1_2(hint, 0);
56         ASSERT_TRUE(ret.isOk());
57     }
58 
59     // Turning these hints on in different orders triggers different code paths,
60     // so iterate over possible orderings.
61     std::vector<PowerHint> hints2 = {PowerHint::AUDIO_STREAMING, PowerHint::CAMERA_LAUNCH,
62                                      PowerHint::CAMERA_STREAMING, PowerHint::CAMERA_SHOT};
63     auto compareHints = [](PowerHint l, PowerHint r) {
64         return static_cast<uint32_t>(l) < static_cast<uint32_t>(r);
65     };
66     std::sort(hints2.begin(), hints2.end(), compareHints);
67     do {
68         for (auto iter = hints2.begin(); iter != hints2.end(); iter++) {
69             ret = power->powerHintAsync_1_2(*iter, 0);
70             ASSERT_TRUE(ret.isOk());
71         }
72         for (auto iter = hints2.begin(); iter != hints2.end(); iter++) {
73             ret = power->powerHintAsync_1_2(*iter, 30000);
74             ASSERT_TRUE(ret.isOk());
75         }
76     } while (std::next_permutation(hints2.begin(), hints2.end(), compareHints));
77 }
78 
79 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerHidlTest);
80 INSTANTIATE_TEST_SUITE_P(
81         PerInstance, PowerHidlTest,
82         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IPower::descriptor)),
83         android::hardware::PrintInstanceNameToString);
84 
85