1 /*
2 * Copyright (C) 2024 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 #define LOG_TAG "ambient_backlight_aidl_hal_test"
17
18 #include <aidl/Gtest.h>
19 #include <aidl/Vintf.h>
20 #include <aidl/android/hardware/tv/mediaquality/AmbientBacklightEvent.h>
21 #include <aidl/android/hardware/tv/mediaquality/AmbientBacklightSettings.h>
22 #include <aidl/android/hardware/tv/mediaquality/BnMediaQualityCallback.h>
23 #include <aidl/android/hardware/tv/mediaquality/BnPictureProfileAdjustmentListener.h>
24 #include <aidl/android/hardware/tv/mediaquality/BnSoundProfileAdjustmentListener.h>
25 #include <aidl/android/hardware/tv/mediaquality/IMediaQuality.h>
26 #include <aidl/android/hardware/tv/mediaquality/PictureParameter.h>
27 #include <aidl/android/hardware/tv/mediaquality/PictureParameters.h>
28 #include <aidl/android/hardware/tv/mediaquality/PictureProfile.h>
29 #include <aidl/android/hardware/tv/mediaquality/SoundParameter.h>
30 #include <aidl/android/hardware/tv/mediaquality/SoundParameters.h>
31 #include <aidl/android/hardware/tv/mediaquality/SoundProfile.h>
32
33 #include <android/binder_auto_utils.h>
34 #include <android/binder_manager.h>
35 #include <binder/IServiceManager.h>
36 #include <binder/ProcessState.h>
37 #include <future>
38
39 using aidl::android::hardware::graphics::common::PixelFormat;
40 using aidl::android::hardware::tv::mediaquality::AmbientBacklightEvent;
41 using aidl::android::hardware::tv::mediaquality::AmbientBacklightSettings;
42 using aidl::android::hardware::tv::mediaquality::AmbientBacklightSource;
43 using aidl::android::hardware::tv::mediaquality::BnMediaQualityCallback;
44 using aidl::android::hardware::tv::mediaquality::BnPictureProfileAdjustmentListener;
45 using aidl::android::hardware::tv::mediaquality::BnSoundProfileAdjustmentListener;
46 using aidl::android::hardware::tv::mediaquality::IMediaQuality;
47 using aidl::android::hardware::tv::mediaquality::ParamCapability;
48 using aidl::android::hardware::tv::mediaquality::PictureParameter;
49 using aidl::android::hardware::tv::mediaquality::PictureParameters;
50 using aidl::android::hardware::tv::mediaquality::PictureProfile;
51 using aidl::android::hardware::tv::mediaquality::SoundParameter;
52 using aidl::android::hardware::tv::mediaquality::SoundParameters;
53 using aidl::android::hardware::tv::mediaquality::SoundProfile;
54 using aidl::android::hardware::tv::mediaquality::VendorParamCapability;
55 using aidl::android::hardware::tv::mediaquality::VendorParameterIdentifier;
56 using android::ProcessState;
57 using android::String16;
58 using ndk::ScopedAStatus;
59 using ndk::SpAIBinder;
60
61 #define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk())
62 #define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk())
63
64 class MediaQualityCallback : public BnMediaQualityCallback {
65 public:
MediaQualityCallback(const std::function<void (const AmbientBacklightEvent & event)> & on_hal_event_cb)66 explicit MediaQualityCallback(
67 const std::function<void(const AmbientBacklightEvent& event)>& on_hal_event_cb)
68 : on_hal_event_cb_(on_hal_event_cb) {}
notifyAmbientBacklightEvent(const AmbientBacklightEvent & event)69 ScopedAStatus notifyAmbientBacklightEvent(const AmbientBacklightEvent& event) override {
70 on_hal_event_cb_(event);
71 return ScopedAStatus::ok();
72 }
73
74 private:
75 std::function<void(const AmbientBacklightEvent& event)> on_hal_event_cb_;
76 };
77
78 class PictureProfileAdjustmentListener : public BnPictureProfileAdjustmentListener {
79 public:
PictureProfileAdjustmentListener(const std::function<void (const PictureProfile & pictureProfile)> & on_hal_picture_profile_adjust)80 explicit PictureProfileAdjustmentListener(
81 const std::function<void(const PictureProfile& pictureProfile)>&
82 on_hal_picture_profile_adjust)
83 : on_hal_picture_profile_adjust_(on_hal_picture_profile_adjust) {}
onPictureProfileAdjusted(const PictureProfile & pictureProfile)84 ScopedAStatus onPictureProfileAdjusted(const PictureProfile& pictureProfile) override {
85 on_hal_picture_profile_adjust_(pictureProfile);
86 return ScopedAStatus::ok();
87 }
88
onParamCapabilityChanged(int64_t,const std::vector<ParamCapability> &)89 ScopedAStatus onParamCapabilityChanged(int64_t, const std::vector<ParamCapability>&) override {
90 return ScopedAStatus::ok();
91 }
92
onVendorParamCapabilityChanged(int64_t,const std::vector<VendorParamCapability> &)93 ScopedAStatus onVendorParamCapabilityChanged(int64_t,
94 const std::vector<VendorParamCapability>&) {
95 return ScopedAStatus::ok();
96 }
97
onRequestPictureParameters(int64_t)98 ScopedAStatus onRequestPictureParameters(int64_t) { return ScopedAStatus::ok(); }
99
100 private:
101 std::function<void(const PictureProfile& pictureProfile)> on_hal_picture_profile_adjust_;
102 };
103
104 class SoundProfileAdjustmentListener : public BnSoundProfileAdjustmentListener {
105 public:
SoundProfileAdjustmentListener(const std::function<void (const SoundProfile & soundProfile)> & on_hal_sound_profile_adjust)106 explicit SoundProfileAdjustmentListener(
107 const std::function<void(const SoundProfile& soundProfile)>&
108 on_hal_sound_profile_adjust)
109 : on_hal_sound_profile_adjust_(on_hal_sound_profile_adjust) {}
onSoundProfileAdjusted(const SoundProfile & soundProfile)110 ScopedAStatus onSoundProfileAdjusted(const SoundProfile& soundProfile) override {
111 on_hal_sound_profile_adjust_(soundProfile);
112 return ScopedAStatus::ok();
113 }
114
onParamCapabilityChanged(int64_t,const std::vector<ParamCapability> &)115 ScopedAStatus onParamCapabilityChanged(int64_t, const std::vector<ParamCapability>&) override {
116 return ScopedAStatus::ok();
117 }
118
onVendorParamCapabilityChanged(int64_t,const std::vector<VendorParamCapability> &)119 ScopedAStatus onVendorParamCapabilityChanged(int64_t,
120 const std::vector<VendorParamCapability>&) {
121 return ScopedAStatus::ok();
122 }
123
onRequestSoundParameters(int64_t)124 ScopedAStatus onRequestSoundParameters(int64_t) { return ScopedAStatus::ok(); }
125
126 private:
127 std::function<void(const SoundProfile& soundProfile)> on_hal_sound_profile_adjust_;
128 };
129
130 class MediaQualityAidl : public testing::TestWithParam<std::string> {
131 public:
SetUp()132 virtual void SetUp() override {
133 mediaquality = IMediaQuality::fromBinder(
134 SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
135 ASSERT_NE(mediaquality, nullptr);
136 }
137 std::shared_ptr<IMediaQuality> mediaquality;
138 };
139
TEST_P(MediaQualityAidl,TestSetAmbientBacklightDetectionEnabled)140 TEST_P(MediaQualityAidl, TestSetAmbientBacklightDetectionEnabled) {
141 std::promise<void> open_cb_promise;
142 std::future<void> open_cb_future{open_cb_promise.get_future()};
143 std::shared_ptr<MediaQualityCallback> callback =
144 ndk::SharedRefBase::make<MediaQualityCallback>([&open_cb_promise](auto event) {
145 EXPECT_EQ(event.getTag(), AmbientBacklightEvent::Tag::enabled);
146 EXPECT_EQ(event.template get<AmbientBacklightEvent::Tag::enabled>(), true);
147 open_cb_promise.set_value();
148 return ScopedAStatus::ok();
149 });
150 ASSERT_OK(mediaquality->setCallback(callback));
151 ASSERT_OK(mediaquality->setAmbientBacklightDetectionEnabled(true));
152 std::chrono::milliseconds timeout{10000};
153 EXPECT_EQ(open_cb_future.wait_for(timeout), std::future_status::ready);
154 }
155
TEST_P(MediaQualityAidl,TestGetAmbientBacklightDetectionEnabled)156 TEST_P(MediaQualityAidl, TestGetAmbientBacklightDetectionEnabled) {
157 bool enabled;
158 ASSERT_OK(mediaquality->getAmbientBacklightDetectionEnabled(&enabled));
159 }
160
TEST_P(MediaQualityAidl,TestSetMediaQualityCallback)161 TEST_P(MediaQualityAidl, TestSetMediaQualityCallback) {
162 std::shared_ptr<MediaQualityCallback> callback = ndk::SharedRefBase::make<MediaQualityCallback>(
163 [](auto /* event */) { return ScopedAStatus::ok(); });
164 ASSERT_OK(mediaquality->setCallback(callback));
165 }
166
TEST_P(MediaQualityAidl,TestSetPictureProfileAdjustmentListener)167 TEST_P(MediaQualityAidl, TestSetPictureProfileAdjustmentListener) {
168 std::shared_ptr<PictureProfileAdjustmentListener> listener =
169 ndk::SharedRefBase::make<PictureProfileAdjustmentListener>(
170 [](auto /*picture profile*/) { return ScopedAStatus::ok(); });
171 ASSERT_OK(mediaquality->setPictureProfileAdjustmentListener(listener));
172 }
173
TEST_P(MediaQualityAidl,TestSendDefaultPictureParameters)174 TEST_P(MediaQualityAidl, TestSendDefaultPictureParameters) {
175 PictureParameters pictureParameters;
176 std::vector<PictureParameter> picParams;
177
178 PictureParameter brightnessParam;
179 brightnessParam.set<PictureParameter::Tag::brightness>(0.5f);
180 picParams.push_back(brightnessParam);
181
182 PictureParameter contrastParam;
183 contrastParam.set<PictureParameter::Tag::contrast>(50);
184 picParams.push_back(contrastParam);
185
186 pictureParameters.pictureParameters = picParams;
187 ASSERT_OK(mediaquality->sendDefaultPictureParameters(pictureParameters));
188 }
189
TEST_P(MediaQualityAidl,TestSetSoundProfileAdjustmentListener)190 TEST_P(MediaQualityAidl, TestSetSoundProfileAdjustmentListener) {
191 std::shared_ptr<SoundProfileAdjustmentListener> listener =
192 ndk::SharedRefBase::make<SoundProfileAdjustmentListener>(
193 [](auto /*sound profile*/) { return ScopedAStatus::ok(); });
194 ASSERT_OK(mediaquality->setSoundProfileAdjustmentListener(listener));
195 }
196
TEST_P(MediaQualityAidl,TestSendDefaultSoundParameters)197 TEST_P(MediaQualityAidl, TestSendDefaultSoundParameters) {
198 SoundParameters soundParameters;
199 std::vector<SoundParameter> soundParams;
200
201 SoundParameter balanceParam;
202 balanceParam.set<SoundParameter::Tag::balance>(50);
203 soundParams.push_back(balanceParam);
204
205 SoundParameter bassParam;
206 bassParam.set<SoundParameter::Tag::bass>(50);
207 soundParams.push_back(bassParam);
208
209 soundParameters.soundParameters = soundParams;
210 ASSERT_OK(mediaquality->sendDefaultSoundParameters(soundParameters));
211 }
212
TEST_P(MediaQualityAidl,TestSetAmbientBacklightDetector)213 TEST_P(MediaQualityAidl, TestSetAmbientBacklightDetector) {
214 AmbientBacklightSettings in_settings = {
215 .packageName = "com.android.mediaquality",
216 .source = AmbientBacklightSource::VIDEO,
217 .colorFormat = PixelFormat::RGB_888,
218 .hZonesNumber = 32,
219 .vZonesNumber = 20,
220 .hasLetterbox = true,
221 .threshold = 0,
222 };
223 ASSERT_OK(mediaquality->setAmbientBacklightDetector(in_settings));
224 }
225
TEST_P(MediaQualityAidl,TestIsAutoPqSupported)226 TEST_P(MediaQualityAidl, TestIsAutoPqSupported) {
227 bool supported;
228 ASSERT_OK(mediaquality->isAutoPqSupported(&supported));
229 }
230
TEST_P(MediaQualityAidl,TestGetAutoPqEnabled)231 TEST_P(MediaQualityAidl, TestGetAutoPqEnabled) {
232 bool enabled;
233 ASSERT_OK(mediaquality->getAutoPqEnabled(&enabled));
234 }
235
TEST_P(MediaQualityAidl,TestSetAutoPqEnabled)236 TEST_P(MediaQualityAidl, TestSetAutoPqEnabled) {
237 ASSERT_OK(mediaquality->setAutoPqEnabled(true));
238 }
239
TEST_P(MediaQualityAidl,TestIsAutoSrSupported)240 TEST_P(MediaQualityAidl, TestIsAutoSrSupported) {
241 bool supported;
242 ASSERT_OK(mediaquality->isAutoSrSupported(&supported));
243 }
244
TEST_P(MediaQualityAidl,TestGetAutoSrEnabled)245 TEST_P(MediaQualityAidl, TestGetAutoSrEnabled) {
246 bool enabled;
247 ASSERT_OK(mediaquality->getAutoSrEnabled(&enabled));
248 }
249
TEST_P(MediaQualityAidl,TestSetAutoSrEnabled)250 TEST_P(MediaQualityAidl, TestSetAutoSrEnabled) {
251 ASSERT_OK(mediaquality->setAutoSrEnabled(true));
252 }
253
TEST_P(MediaQualityAidl,TestIsAutoAqSupported)254 TEST_P(MediaQualityAidl, TestIsAutoAqSupported) {
255 bool supported;
256 ASSERT_OK(mediaquality->isAutoAqSupported(&supported));
257 }
258
TEST_P(MediaQualityAidl,TestGetAutoAqEnabled)259 TEST_P(MediaQualityAidl, TestGetAutoAqEnabled) {
260 bool enabled;
261 ASSERT_OK(mediaquality->getAutoAqEnabled(&enabled));
262 }
263
TEST_P(MediaQualityAidl,TestSetAutoAqEnabled)264 TEST_P(MediaQualityAidl, TestSetAutoAqEnabled) {
265 ASSERT_OK(mediaquality->setAutoAqEnabled(true));
266 }
267
268 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MediaQualityAidl);
269
270 INSTANTIATE_TEST_SUITE_P(
271 PerInstance, MediaQualityAidl,
272 testing::ValuesIn(android::getAidlHalInstanceNames(IMediaQuality::descriptor)),
273 android::PrintInstanceNameToString);
274
main(int argc,char ** argv)275 int main(int argc, char** argv) {
276 ::testing::InitGoogleTest(&argc, argv);
277 ProcessState::self()->setThreadPoolMaxThreadCount(1);
278 ProcessState::self()->startThreadPool();
279 return RUN_ALL_TESTS();
280 }
281