blob: f420e02a2b2eeaab036276c221f7e29956514c6c [file] [log] [blame]
Haofan Wange02b8092024-10-28 21:47:27 +00001/*
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 */
Haofan Wangb97b3f32024-10-31 15:33:17 -070016#define LOG_TAG "ambient_backlight_aidl_hal_test"
Haofan Wange02b8092024-10-28 21:47:27 +000017
18#include <aidl/Gtest.h>
19#include <aidl/Vintf.h>
Haofan Wangb97b3f32024-10-31 15:33:17 -070020#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>
Haofan Wang58d25d22024-11-18 17:47:57 -080023#include <aidl/android/hardware/tv/mediaquality/BnPictureProfileAdjustmentListener.h>
24#include <aidl/android/hardware/tv/mediaquality/BnSoundProfileAdjustmentListener.h>
Haofan Wangb97b3f32024-10-31 15:33:17 -070025#include <aidl/android/hardware/tv/mediaquality/IMediaQuality.h>
Haofan Wang58d25d22024-11-18 17:47:57 -080026#include <aidl/android/hardware/tv/mediaquality/PictureParameters.h>
27#include <aidl/android/hardware/tv/mediaquality/PictureProfile.h>
28#include <aidl/android/hardware/tv/mediaquality/SoundParameters.h>
29#include <aidl/android/hardware/tv/mediaquality/SoundProfile.h>
30
Haofan Wangb97b3f32024-10-31 15:33:17 -070031#include <android/binder_auto_utils.h>
32#include <android/binder_manager.h>
Haofan Wange02b8092024-10-28 21:47:27 +000033#include <binder/IServiceManager.h>
34#include <binder/ProcessState.h>
Haofan Wangb97b3f32024-10-31 15:33:17 -070035#include <future>
Haofan Wange02b8092024-10-28 21:47:27 +000036
Haofan Wangb97b3f32024-10-31 15:33:17 -070037using aidl::android::hardware::graphics::common::PixelFormat;
38using aidl::android::hardware::tv::mediaquality::AmbientBacklightEvent;
39using aidl::android::hardware::tv::mediaquality::AmbientBacklightSettings;
40using aidl::android::hardware::tv::mediaquality::AmbientBacklightSource;
41using aidl::android::hardware::tv::mediaquality::BnMediaQualityCallback;
Haofan Wang58d25d22024-11-18 17:47:57 -080042using aidl::android::hardware::tv::mediaquality::BnPictureProfileAdjustmentListener;
43using aidl::android::hardware::tv::mediaquality::BnSoundProfileAdjustmentListener;
Haofan Wangb97b3f32024-10-31 15:33:17 -070044using aidl::android::hardware::tv::mediaquality::IMediaQuality;
Haofan Wang58d25d22024-11-18 17:47:57 -080045using aidl::android::hardware::tv::mediaquality::PictureParameters;
46using aidl::android::hardware::tv::mediaquality::PictureProfile;
47using aidl::android::hardware::tv::mediaquality::SoundParameters;
48using aidl::android::hardware::tv::mediaquality::SoundProfile;
Haofan Wange02b8092024-10-28 21:47:27 +000049using android::ProcessState;
Haofan Wange02b8092024-10-28 21:47:27 +000050using android::String16;
Haofan Wangb97b3f32024-10-31 15:33:17 -070051using ndk::ScopedAStatus;
52using ndk::SpAIBinder;
Haofan Wange02b8092024-10-28 21:47:27 +000053
54#define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk())
55#define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk())
56
Haofan Wangb97b3f32024-10-31 15:33:17 -070057class MediaQualityCallback : public BnMediaQualityCallback {
Haofan Wange02b8092024-10-28 21:47:27 +000058 public:
Haofan Wangb97b3f32024-10-31 15:33:17 -070059 explicit MediaQualityCallback(
60 const std::function<void(const AmbientBacklightEvent& event)>& on_hal_event_cb)
61 : on_hal_event_cb_(on_hal_event_cb) {}
62 ScopedAStatus notifyAmbientBacklightEvent(const AmbientBacklightEvent& event) override {
63 on_hal_event_cb_(event);
64 return ScopedAStatus::ok();
Haofan Wange02b8092024-10-28 21:47:27 +000065 }
66
Haofan Wangb97b3f32024-10-31 15:33:17 -070067 private:
68 std::function<void(const AmbientBacklightEvent& event)> on_hal_event_cb_;
Haofan Wange02b8092024-10-28 21:47:27 +000069};
70
Haofan Wang58d25d22024-11-18 17:47:57 -080071class PictureProfileAdjustmentListener : public BnPictureProfileAdjustmentListener {
72 public:
73 explicit PictureProfileAdjustmentListener(
74 const std::function<void(const PictureProfile& pictureProfile)>&
75 on_hal_picture_profile_adjust)
76 : on_hal_picture_profile_adjust_(on_hal_picture_profile_adjust) {}
77 ScopedAStatus onPictureProfileAdjusted(const PictureProfile& pictureProfile) override {
78 on_hal_picture_profile_adjust_(pictureProfile);
79 return ScopedAStatus::ok();
80 }
81
82 private:
83 std::function<void(const PictureProfile& pictureProfile)> on_hal_picture_profile_adjust_;
84};
85
86class SoundProfileAdjustmentListener : public BnSoundProfileAdjustmentListener {
87 public:
88 explicit SoundProfileAdjustmentListener(
89 const std::function<void(const SoundProfile& soundProfile)>&
90 on_hal_sound_profile_adjust)
91 : on_hal_sound_profile_adjust_(on_hal_sound_profile_adjust) {}
92 ScopedAStatus onSoundProfileAdjusted(const SoundProfile& soundProfile) override {
93 on_hal_sound_profile_adjust_(soundProfile);
94 return ScopedAStatus::ok();
95 }
96
97 private:
98 std::function<void(const SoundProfile& soundProfile)> on_hal_sound_profile_adjust_;
99};
100
Haofan Wangb97b3f32024-10-31 15:33:17 -0700101class MediaQualityAidl : public testing::TestWithParam<std::string> {
102 public:
103 virtual void SetUp() override {
104 mediaquality = IMediaQuality::fromBinder(
105 SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
106 ASSERT_NE(mediaquality, nullptr);
107 }
108 std::shared_ptr<IMediaQuality> mediaquality;
109};
110
111TEST_P(MediaQualityAidl, TestSetAmbientBacklightDetectionEnabled) {
112 std::promise<void> open_cb_promise;
113 std::future<void> open_cb_future{open_cb_promise.get_future()};
114 std::shared_ptr<MediaQualityCallback> callback =
115 ndk::SharedRefBase::make<MediaQualityCallback>([&open_cb_promise](auto event) {
116 EXPECT_EQ(event.getTag(), AmbientBacklightEvent::Tag::enabled);
117 EXPECT_EQ(event.template get<AmbientBacklightEvent::Tag::enabled>(), true);
118 open_cb_promise.set_value();
119 return ScopedAStatus::ok();
120 });
121 ASSERT_OK(mediaquality->setCallback(callback));
122 ASSERT_OK(mediaquality->setAmbientBacklightDetectionEnabled(true));
123 std::chrono::milliseconds timeout{10000};
124 EXPECT_EQ(open_cb_future.wait_for(timeout), std::future_status::ready);
Haofan Wange02b8092024-10-28 21:47:27 +0000125}
126
Haofan Wangb97b3f32024-10-31 15:33:17 -0700127TEST_P(MediaQualityAidl, TestGetAmbientBacklightDetectionEnabled) {
Haofan Wange02b8092024-10-28 21:47:27 +0000128 bool enabled;
Haofan Wangb97b3f32024-10-31 15:33:17 -0700129 ASSERT_OK(mediaquality->getAmbientBacklightDetectionEnabled(&enabled));
130}
131
132TEST_P(MediaQualityAidl, TestSetMediaQualityCallback) {
133 std::shared_ptr<MediaQualityCallback> callback = ndk::SharedRefBase::make<MediaQualityCallback>(
134 [](auto /* event */) { return ScopedAStatus::ok(); });
135 ASSERT_OK(mediaquality->setCallback(callback));
136}
137
Haofan Wang58d25d22024-11-18 17:47:57 -0800138TEST_P(MediaQualityAidl, TestSetPictureProfileAdjustmentListener) {
139 std::shared_ptr<PictureProfileAdjustmentListener> listener =
140 ndk::SharedRefBase::make<PictureProfileAdjustmentListener>(
141 [](auto /*picture profile*/) { return ScopedAStatus::ok(); });
142 ASSERT_OK(mediaquality->setPictureProfileAdjustmentListener(listener));
143}
144
145TEST_P(MediaQualityAidl, TestGetPictureParameters) {
146 PictureParameters pictureParams;
147 auto result = mediaquality->getPictureParameters(1, &pictureParams);
148 ASSERT_TRUE(result.isOk());
149 ASSERT_EQ(pictureParams.pictureParameters.size(), 2);
150}
151
152TEST_P(MediaQualityAidl, TestSetSoundProfileAdjustmentListener) {
153 std::shared_ptr<SoundProfileAdjustmentListener> listener =
154 ndk::SharedRefBase::make<SoundProfileAdjustmentListener>(
155 [](auto /*sound profile*/) { return ScopedAStatus::ok(); });
156 ASSERT_OK(mediaquality->setSoundProfileAdjustmentListener(listener));
157}
158
159TEST_P(MediaQualityAidl, TestGetSoundParameters) {
160 SoundParameters soundParams;
161 auto result = mediaquality->getSoundParameters(1, &soundParams);
162 ASSERT_TRUE(result.isOk());
163 ASSERT_EQ(soundParams.soundParameters.size(), 2);
164}
165
Haofan Wangb97b3f32024-10-31 15:33:17 -0700166TEST_P(MediaQualityAidl, TestSetAmbientBacklightDetector) {
167 AmbientBacklightSettings in_settings = {
168 .packageName = "com.android.mediaquality",
169 .source = AmbientBacklightSource::VIDEO,
170 .colorFormat = PixelFormat::RGB_888,
171 .hZonesNumber = 32,
172 .vZonesNumber = 20,
173 .hasLetterbox = true,
174 .threshold = 0,
175 };
176 ASSERT_OK(mediaquality->setAmbientBacklightDetector(in_settings));
Haofan Wange02b8092024-10-28 21:47:27 +0000177}
178
179GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MediaQualityAidl);
180
181INSTANTIATE_TEST_SUITE_P(
182 PerInstance, MediaQualityAidl,
183 testing::ValuesIn(android::getAidlHalInstanceNames(IMediaQuality::descriptor)),
184 android::PrintInstanceNameToString);
185
186int main(int argc, char** argv) {
187 ::testing::InitGoogleTest(&argc, argv);
188 ProcessState::self()->setThreadPoolMaxThreadCount(1);
189 ProcessState::self()->startThreadPool();
190 return RUN_ALL_TESTS();
191}