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 "ConfigstoreHidlHalTest"
18 
19 #include <android-base/logging.h>
20 #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
21 #include <android/hardware/configstore/1.0/types.h>
22 #include <gtest/gtest.h>
23 #include <hidl/GtestPrinter.h>
24 #include <hidl/ServiceManagement.h>
25 #include <unistd.h>
26 
27 using ::android::hardware::configstore::V1_0::ISurfaceFlingerConfigs;
28 using ::android::hardware::configstore::V1_0::OptionalBool;
29 using ::android::hardware::configstore::V1_0::OptionalInt64;
30 using ::android::hardware::configstore::V1_0::OptionalUInt64;
31 using ::android::hardware::hidl_vec;
32 using ::android::hardware::Return;
33 using ::android::hardware::Void;
34 using ::android::sp;
35 
36 #define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk())
37 #define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk())
38 
39 class ConfigstoreHidlTest : public ::testing::TestWithParam<std::string> {
40    public:
41     sp<ISurfaceFlingerConfigs> sfConfigs;
42 
SetUp()43     virtual void SetUp() override {
44         sfConfigs = ISurfaceFlingerConfigs::getService(GetParam());
45         ASSERT_NE(sfConfigs, nullptr);
46     }
47 
TearDown()48     virtual void TearDown() override {}
49 };
50 
51 /**
52  * Ensure all ISurfaceFlingerConfigs.hal function calls are successful.
53  */
TEST_P(ConfigstoreHidlTest,TestFunctionCalls)54 TEST_P(ConfigstoreHidlTest, TestFunctionCalls) {
55     bool tmp;
56 
57     Return<void> status = sfConfigs->vsyncEventPhaseOffsetNs(
58         [&tmp](OptionalInt64 arg) { tmp = arg.specified; });
59     EXPECT_OK(status);
60 
61     status = sfConfigs->vsyncSfEventPhaseOffsetNs(
62         [&tmp](OptionalInt64 arg) { tmp = arg.specified; });
63     EXPECT_OK(status);
64 
65     status = sfConfigs->useContextPriority(
66         [&tmp](OptionalBool arg) { tmp = arg.specified; });
67     EXPECT_OK(status);
68 
69     status = sfConfigs->hasWideColorDisplay(
70         [&tmp](OptionalBool arg) { tmp = arg.specified; });
71     EXPECT_OK(status);
72 
73     status = sfConfigs->hasHDRDisplay(
74         [&tmp](OptionalBool arg) { tmp = arg.specified; });
75     EXPECT_OK(status);
76 
77     status = sfConfigs->presentTimeOffsetFromVSyncNs(
78         [&tmp](OptionalInt64 arg) { tmp = arg.specified; });
79     EXPECT_OK(status);
80 
81     status = sfConfigs->useHwcForRGBtoYUV(
82         [&tmp](OptionalBool arg) { tmp = arg.specified; });
83     EXPECT_OK(status);
84 
85     status = sfConfigs->maxVirtualDisplaySize(
86         [&tmp](OptionalUInt64 arg) { tmp = arg.specified; });
87     EXPECT_OK(status);
88 
89     status = sfConfigs->hasSyncFramework(
90         [&tmp](OptionalBool arg) { tmp = arg.specified; });
91     EXPECT_OK(status);
92 
93     status = sfConfigs->useVrFlinger(
94         [&tmp](OptionalBool arg) { tmp = arg.specified; });
95     EXPECT_OK(status);
96 
97     status = sfConfigs->maxFrameBufferAcquiredBuffers(
98         [&tmp](OptionalInt64 arg) { tmp = arg.specified; });
99     EXPECT_OK(status);
100 
101     status = sfConfigs->startGraphicsAllocatorService(
102         [&tmp](OptionalBool arg) { tmp = arg.specified; });
103     EXPECT_OK(status);
104 }
105 
106 /**
107  * Ensure repeated call to the same function returns the same result.
108  */
TEST_P(ConfigstoreHidlTest,TestSameReturnValue)109 TEST_P(ConfigstoreHidlTest, TestSameReturnValue) {
110     int64_t original_ret;
111     Return<void> status = sfConfigs->vsyncEventPhaseOffsetNs(
112         [&original_ret](OptionalInt64 arg) { original_ret = arg.value; });
113 
114     int64_t next_ret;
115     for (int cnt = 0; cnt < 10; cnt++) {
116         status = sfConfigs->vsyncEventPhaseOffsetNs(
117             [&next_ret](OptionalInt64 arg) { next_ret = arg.value; });
118         EXPECT_EQ(original_ret, next_ret);
119     }
120 }
121 
122 /**
123  * Make sure the constrains of hasWideColorDisplay, hasHDRDisplay
124  * are enforced.
125  */
TEST_P(ConfigstoreHidlTest,TestColorConstrainsBasic)126 TEST_P(ConfigstoreHidlTest, TestColorConstrainsBasic) {
127     bool hasWideColorDisplay;
128     bool hasHDRDisplay;
129 
130     Return<void> status = sfConfigs->hasWideColorDisplay(
131         [&](OptionalBool arg) { hasWideColorDisplay = arg.specified; });
132     EXPECT_OK(status);
133 
134     status = sfConfigs->hasHDRDisplay([&](OptionalBool arg) { hasHDRDisplay = arg.specified; });
135     EXPECT_OK(status);
136 
137     // When hasHDRDisplay returns true, hasWideColorDisplay must also return true.
138     if (hasHDRDisplay) {
139         ASSERT_TRUE(hasWideColorDisplay);
140     }
141 }
142 
143 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ConfigstoreHidlTest);
144 INSTANTIATE_TEST_SUITE_P(
145         PerInstance, ConfigstoreHidlTest,
146         testing::ValuesIn(android::hardware::getAllHalInstanceNames(ISurfaceFlingerConfigs::descriptor)),
147         android::hardware::PrintInstanceNameToString);
148