Yixiao Luo | ce50133 | 2022-08-12 11:18:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 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 | #include "VtsHalTvInputTargetTest.h" |
| 18 | |
| 19 | #include <android-base/properties.h> |
| 20 | #include <android/binder_ibinder.h> |
| 21 | #include <android/binder_process.h> |
| 22 | #include <android/binder_status.h> |
| 23 | |
| 24 | using namespace VtsHalTvInputTargetTest; |
| 25 | |
| 26 | TvInputAidlTest::TvInputCallback::TvInputCallback(shared_ptr<TvInputAidlTest> parent) |
| 27 | : parent_(parent) {} |
| 28 | |
| 29 | ::ndk::ScopedAStatus TvInputAidlTest::TvInputCallback::notify(const TvInputEvent& in_event) { |
| 30 | unique_lock<mutex> lock(parent_->mutex_); |
| 31 | |
| 32 | switch (in_event.type) { |
| 33 | case TvInputEventType::DEVICE_AVAILABLE: |
| 34 | parent_->onDeviceAvailable(in_event.deviceInfo); |
| 35 | break; |
| 36 | case TvInputEventType::DEVICE_UNAVAILABLE: |
| 37 | parent_->onDeviceUnavailable(in_event.deviceInfo.deviceId); |
| 38 | break; |
| 39 | case TvInputEventType::STREAM_CONFIGURATIONS_CHANGED: |
| 40 | parent_->onStreamConfigurationsChanged(in_event.deviceInfo.deviceId); |
| 41 | break; |
| 42 | } |
| 43 | return ::ndk::ScopedAStatus::ok(); |
| 44 | } |
| 45 | |
David Zhao | 6bdbc5e | 2023-01-12 16:51:03 -0800 | [diff] [blame] | 46 | ::ndk::ScopedAStatus TvInputAidlTest::TvInputCallback::notifyTvMessageEvent( |
| 47 | const TvMessageEvent& in_event) { |
| 48 | return ::ndk::ScopedAStatus::ok(); |
| 49 | } |
| 50 | |
Yixiao Luo | ce50133 | 2022-08-12 11:18:18 -0700 | [diff] [blame] | 51 | void TvInputAidlTest::SetUp() { |
| 52 | if (AServiceManager_isDeclared(GetParam().c_str())) { |
| 53 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(GetParam().c_str())); |
| 54 | tv_input_ = ITvInput::fromBinder(binder); |
| 55 | } else { |
| 56 | tv_input_ = nullptr; |
| 57 | } |
| 58 | ASSERT_NE(tv_input_, nullptr); |
| 59 | |
| 60 | tv_input_callback_ = |
| 61 | ::ndk::SharedRefBase::make<TvInputCallback>(shared_ptr<TvInputAidlTest>(this)); |
| 62 | ASSERT_NE(tv_input_callback_, nullptr); |
| 63 | |
| 64 | tv_input_->setCallback(tv_input_callback_); |
| 65 | // All events received within the timeout should be handled. |
| 66 | sleep(WAIT_FOR_EVENT_TIMEOUT); |
| 67 | } |
| 68 | |
| 69 | void TvInputAidlTest::TearDown() { |
| 70 | tv_input_ = nullptr; |
| 71 | } |
| 72 | |
| 73 | void TvInputAidlTest::onDeviceAvailable(const TvInputDeviceInfo& deviceInfo) { |
| 74 | ALOGD("onDeviceAvailable for device id %d", deviceInfo.deviceId); |
| 75 | device_info_.add(deviceInfo.deviceId, deviceInfo); |
| 76 | } |
| 77 | |
| 78 | void TvInputAidlTest::onDeviceUnavailable(int32_t deviceId) { |
| 79 | ALOGD("onDeviceUnavailable for device id %d", deviceId); |
| 80 | device_info_.removeItem(deviceId); |
| 81 | stream_config_.removeItem(deviceId); |
| 82 | } |
| 83 | |
| 84 | ::ndk::ScopedAStatus TvInputAidlTest::onStreamConfigurationsChanged(int32_t deviceId) { |
| 85 | ALOGD("onStreamConfigurationsChanged for device id %d", deviceId); |
| 86 | return updateStreamConfigurations(deviceId); |
| 87 | } |
| 88 | |
| 89 | ::ndk::ScopedAStatus TvInputAidlTest::updateStreamConfigurations(int32_t deviceId) { |
| 90 | stream_config_.removeItem(deviceId); |
| 91 | vector<TvStreamConfig> list; |
| 92 | ::ndk::ScopedAStatus status = tv_input_->getStreamConfigurations(deviceId, &list); |
| 93 | if (status.isOk()) { |
| 94 | stream_config_.add(deviceId, list); |
| 95 | } |
| 96 | return status; |
| 97 | } |
| 98 | |
| 99 | void TvInputAidlTest::updateAllStreamConfigurations() { |
| 100 | for (size_t i = 0; i < device_info_.size(); i++) { |
| 101 | int32_t device_id = device_info_.keyAt(i); |
| 102 | updateStreamConfigurations(device_id); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | vector<size_t> TvInputAidlTest::getConfigIndices() { |
| 107 | vector<size_t> indices; |
| 108 | for (size_t i = 0; i < stream_config_.size(); i++) { |
| 109 | if (stream_config_.valueAt(i).size() != 0) { |
| 110 | indices.push_back(i); |
| 111 | } |
| 112 | } |
| 113 | return indices; |
| 114 | } |
| 115 | |
| 116 | int32_t TvInputAidlTest::getNumNotIn(vector<int32_t>& nums) { |
| 117 | int32_t result = DEFAULT_ID; |
| 118 | int32_t size = static_cast<int32_t>(nums.size()); |
| 119 | for (int32_t i = 0; i < size; i++) { |
| 120 | // Put every element to its target position, if possible. |
| 121 | int32_t target_pos = nums[i]; |
| 122 | while (target_pos >= 0 && target_pos < size && i != target_pos && |
| 123 | nums[i] != nums[target_pos]) { |
| 124 | swap(nums[i], nums[target_pos]); |
| 125 | target_pos = nums[i]; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | for (int32_t i = 0; i < size; i++) { |
| 130 | if (nums[i] != i) { |
| 131 | return i; |
| 132 | } |
| 133 | } |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * GetStreamConfigTest: |
| 139 | * Calls updateStreamConfigurations() for each existing device |
| 140 | * Checks returned results |
| 141 | */ |
| 142 | TEST_P(TvInputAidlTest, GetStreamConfigTest) { |
| 143 | unique_lock<mutex> lock(mutex_); |
| 144 | |
| 145 | for (size_t i = 0; i < device_info_.size(); i++) { |
| 146 | int32_t device_id = device_info_.keyAt(i); |
| 147 | ALOGD("GetStreamConfigTest: device_id=%d", device_id); |
| 148 | ASSERT_TRUE(updateStreamConfigurations(device_id).isOk()); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * OpenAndCloseStreamTest: |
| 154 | * Calls openStream() and then closeStream() for each existing stream |
| 155 | * Checks returned results |
| 156 | */ |
| 157 | TEST_P(TvInputAidlTest, OpenAndCloseStreamTest) { |
| 158 | unique_lock<mutex> lock(mutex_); |
| 159 | |
| 160 | updateAllStreamConfigurations(); |
| 161 | |
| 162 | for (size_t j = 0; j < stream_config_.size(); j++) { |
| 163 | int32_t device_id = stream_config_.keyAt(j); |
| 164 | vector<TvStreamConfig> config = stream_config_.valueAt(j); |
| 165 | for (size_t i = 0; i < config.size(); i++) { |
Yixiao Luo | 4cd52a9 | 2022-10-28 15:51:48 -0700 | [diff] [blame] | 166 | NativeHandle handle; |
Yixiao Luo | ce50133 | 2022-08-12 11:18:18 -0700 | [diff] [blame] | 167 | int32_t stream_id = config[i].streamId; |
| 168 | ALOGD("OpenAndCloseStreamTest: open stream, device_id=%d, stream_id=%d", device_id, |
| 169 | stream_id); |
Yixiao Luo | 4cd52a9 | 2022-10-28 15:51:48 -0700 | [diff] [blame] | 170 | ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle).isOk()); |
Yixiao Luo | ce50133 | 2022-08-12 11:18:18 -0700 | [diff] [blame] | 171 | ALOGD("OpenAndCloseStreamTest: close stream, device_id=%d, stream_id=%d", device_id, |
| 172 | stream_id); |
| 173 | ASSERT_TRUE(tv_input_->closeStream(device_id, stream_id).isOk()); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * InvalidDeviceIdTest: |
| 180 | * Calls updateStreamConfigurations(), openStream(), and closeStream() |
| 181 | * for a non-existing device |
| 182 | * Checks returned results |
| 183 | * The results should be ITvInput::STATUS_INVALID_ARGUMENTS |
| 184 | */ |
| 185 | TEST_P(TvInputAidlTest, InvalidDeviceIdTest) { |
| 186 | unique_lock<mutex> lock(mutex_); |
| 187 | |
| 188 | vector<int32_t> device_ids; |
| 189 | for (size_t i = 0; i < device_info_.size(); i++) { |
| 190 | device_ids.push_back(device_info_.keyAt(i)); |
| 191 | } |
| 192 | // Get a non-existing device ID. |
| 193 | int32_t id = getNumNotIn(device_ids); |
| 194 | ALOGD("InvalidDeviceIdTest: update stream config, device_id=%d", id); |
| 195 | ASSERT_TRUE(updateStreamConfigurations(id).getServiceSpecificError() == |
| 196 | ITvInput::STATUS_INVALID_ARGUMENTS); |
| 197 | |
| 198 | int32_t stream_id = 0; |
Yixiao Luo | 4cd52a9 | 2022-10-28 15:51:48 -0700 | [diff] [blame] | 199 | NativeHandle handle; |
Yixiao Luo | ce50133 | 2022-08-12 11:18:18 -0700 | [diff] [blame] | 200 | |
| 201 | ALOGD("InvalidDeviceIdTest: open stream, device_id=%d, stream_id=%d", id, stream_id); |
Yixiao Luo | 4cd52a9 | 2022-10-28 15:51:48 -0700 | [diff] [blame] | 202 | ASSERT_TRUE(tv_input_->openStream(id, stream_id, &handle).getServiceSpecificError() == |
Yixiao Luo | ce50133 | 2022-08-12 11:18:18 -0700 | [diff] [blame] | 203 | ITvInput::STATUS_INVALID_ARGUMENTS); |
| 204 | |
| 205 | ALOGD("InvalidDeviceIdTest: close stream, device_id=%d, stream_id=%d", id, stream_id); |
| 206 | ASSERT_TRUE(tv_input_->closeStream(id, stream_id).getServiceSpecificError() == |
| 207 | ITvInput::STATUS_INVALID_ARGUMENTS); |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * InvalidStreamIdTest: |
| 212 | * Calls openStream(), and closeStream() for a non-existing stream |
| 213 | * Checks returned results |
| 214 | * The results should be ITvInput::STATUS_INVALID_ARGUMENTS |
| 215 | */ |
| 216 | TEST_P(TvInputAidlTest, InvalidStreamIdTest) { |
| 217 | unique_lock<mutex> lock(mutex_); |
| 218 | |
| 219 | if (device_info_.isEmpty()) { |
| 220 | return; |
| 221 | } |
| 222 | updateAllStreamConfigurations(); |
| 223 | |
| 224 | int32_t device_id = device_info_.keyAt(0); |
| 225 | // Get a non-existing stream ID. |
| 226 | int32_t id = DEFAULT_ID; |
| 227 | if (stream_config_.indexOfKey(device_id) >= 0) { |
| 228 | vector<int32_t> stream_ids; |
| 229 | vector<TvStreamConfig> config = stream_config_.valueFor(device_id); |
| 230 | for (size_t i = 0; i < config.size(); i++) { |
| 231 | stream_ids.push_back(config[i].streamId); |
| 232 | } |
| 233 | id = getNumNotIn(stream_ids); |
| 234 | } |
| 235 | |
Yixiao Luo | 4cd52a9 | 2022-10-28 15:51:48 -0700 | [diff] [blame] | 236 | NativeHandle handle; |
| 237 | |
Yixiao Luo | ce50133 | 2022-08-12 11:18:18 -0700 | [diff] [blame] | 238 | ALOGD("InvalidStreamIdTest: open stream, device_id=%d, stream_id=%d", device_id, id); |
Yixiao Luo | 4cd52a9 | 2022-10-28 15:51:48 -0700 | [diff] [blame] | 239 | ASSERT_TRUE(tv_input_->openStream(device_id, id, &handle).getServiceSpecificError() == |
Yixiao Luo | ce50133 | 2022-08-12 11:18:18 -0700 | [diff] [blame] | 240 | ITvInput::STATUS_INVALID_ARGUMENTS); |
| 241 | |
| 242 | ALOGD("InvalidStreamIdTest: close stream, device_id=%d, stream_id=%d", device_id, id); |
| 243 | ASSERT_TRUE(tv_input_->closeStream(device_id, id).getServiceSpecificError() == |
| 244 | ITvInput::STATUS_INVALID_ARGUMENTS); |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * OpenAnOpenedStreamsTest: |
| 249 | * Calls openStream() twice for a stream (if any) |
| 250 | * Checks returned results |
| 251 | * The result of the second call should be ITvInput::STATUS_INVALID_STATE |
| 252 | */ |
| 253 | TEST_P(TvInputAidlTest, OpenAnOpenedStreamsTest) { |
| 254 | unique_lock<mutex> lock(mutex_); |
| 255 | |
| 256 | updateAllStreamConfigurations(); |
| 257 | vector<size_t> indices = getConfigIndices(); |
| 258 | if (indices.empty()) { |
| 259 | return; |
| 260 | } |
| 261 | int32_t device_id = stream_config_.keyAt(indices[0]); |
David Zhao | 17a9190 | 2023-05-17 16:36:38 -0700 | [diff] [blame] | 262 | vector<TvStreamConfig> streamConfigs = stream_config_.valueAt(indices[0]); |
| 263 | if (streamConfigs.empty()) { |
| 264 | return; |
| 265 | } |
| 266 | int32_t stream_id = streamConfigs[0].streamId; |
Yixiao Luo | 4cd52a9 | 2022-10-28 15:51:48 -0700 | [diff] [blame] | 267 | NativeHandle handle; |
Yixiao Luo | ce50133 | 2022-08-12 11:18:18 -0700 | [diff] [blame] | 268 | |
| 269 | ALOGD("OpenAnOpenedStreamsTest: open stream, device_id=%d, stream_id=%d", device_id, stream_id); |
Yixiao Luo | 4cd52a9 | 2022-10-28 15:51:48 -0700 | [diff] [blame] | 270 | ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle).isOk()); |
| 271 | |
| 272 | ALOGD("OpenAnOpenedStreamsTest: open stream, device_id=%d, stream_id=%d", device_id, stream_id); |
| 273 | ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle).getServiceSpecificError() == |
Yixiao Luo | ce50133 | 2022-08-12 11:18:18 -0700 | [diff] [blame] | 274 | ITvInput::STATUS_INVALID_STATE); |
| 275 | |
| 276 | // close stream as subsequent tests assume no open streams |
| 277 | ALOGD("OpenAnOpenedStreamsTest: close stream, device_id=%d, stream_id=%d", device_id, |
| 278 | stream_id); |
| 279 | ASSERT_TRUE(tv_input_->closeStream(device_id, stream_id).isOk()); |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * CloseStreamBeforeOpenTest: |
| 284 | * Calls closeStream() without calling openStream() for a stream (if any) |
| 285 | * Checks the returned result |
| 286 | * The result should be ITvInput::STATUS_INVALID_STATE |
| 287 | */ |
| 288 | TEST_P(TvInputAidlTest, CloseStreamBeforeOpenTest) { |
| 289 | unique_lock<mutex> lock(mutex_); |
| 290 | |
| 291 | updateAllStreamConfigurations(); |
| 292 | vector<size_t> indices = getConfigIndices(); |
| 293 | if (indices.empty()) { |
| 294 | return; |
| 295 | } |
| 296 | int32_t device_id = stream_config_.keyAt(indices[0]); |
David Zhao | 17a9190 | 2023-05-17 16:36:38 -0700 | [diff] [blame] | 297 | vector<TvStreamConfig> streamConfigs = stream_config_.valueAt(indices[0]); |
| 298 | if (streamConfigs.empty()) { |
| 299 | return; |
| 300 | } |
| 301 | int32_t stream_id = streamConfigs[0].streamId; |
Yixiao Luo | ce50133 | 2022-08-12 11:18:18 -0700 | [diff] [blame] | 302 | |
| 303 | ALOGD("CloseStreamBeforeOpenTest: close stream, device_id=%d, stream_id=%d", device_id, |
| 304 | stream_id); |
| 305 | ASSERT_TRUE(tv_input_->closeStream(device_id, stream_id).getServiceSpecificError() == |
| 306 | ITvInput::STATUS_INVALID_STATE); |
| 307 | } |
| 308 | |
David Zhao | 17a9190 | 2023-05-17 16:36:38 -0700 | [diff] [blame] | 309 | TEST_P(TvInputAidlTest, SetTvMessageEnabledTest) { |
| 310 | unique_lock<mutex> lock(mutex_); |
| 311 | |
| 312 | updateAllStreamConfigurations(); |
| 313 | vector<size_t> indices = getConfigIndices(); |
| 314 | if (indices.empty()) { |
| 315 | return; |
| 316 | } |
| 317 | int32_t device_id = stream_config_.keyAt(indices[0]); |
| 318 | vector<TvStreamConfig> streamConfigs = stream_config_.valueAt(indices[0]); |
| 319 | if (streamConfigs.empty()) { |
| 320 | return; |
| 321 | } |
| 322 | int32_t stream_id = streamConfigs[0].streamId; |
| 323 | ALOGD("SetTvMessageEnabledTest: device_id=%d, stream_id=%d", device_id, stream_id); |
| 324 | tv_input_->setTvMessageEnabled(device_id, stream_id, TvMessageEventType::WATERMARK, true); |
| 325 | } |
| 326 | |
| 327 | TEST_P(TvInputAidlTest, GetTvMessageQueueTest) { |
| 328 | unique_lock<mutex> lock(mutex_); |
| 329 | |
| 330 | updateAllStreamConfigurations(); |
| 331 | vector<size_t> indices = getConfigIndices(); |
| 332 | if (indices.empty()) { |
| 333 | return; |
| 334 | } |
| 335 | int32_t device_id = stream_config_.keyAt(indices[0]); |
| 336 | vector<TvStreamConfig> streamConfigs = stream_config_.valueAt(indices[0]); |
| 337 | if (streamConfigs.empty()) { |
| 338 | return; |
| 339 | } |
| 340 | int32_t stream_id = streamConfigs[0].streamId; |
| 341 | ALOGD("GetTvMessageQueueTest: device_id=%d, stream_id=%d", device_id, stream_id); |
| 342 | MQDescriptor<int8_t, SynchronizedReadWrite> queue; |
| 343 | tv_input_->getTvMessageQueueDesc(&queue, device_id, stream_id); |
| 344 | } |
| 345 | |
Yixiao Luo | ce50133 | 2022-08-12 11:18:18 -0700 | [diff] [blame] | 346 | INSTANTIATE_TEST_SUITE_P(PerInstance, TvInputAidlTest, |
| 347 | testing::ValuesIn(android::getAidlHalInstanceNames(ITvInput::descriptor)), |
| 348 | android::PrintInstanceNameToString); |
| 349 | |
| 350 | // TODO remove from the allow list once the cf tv target is enabled for testing |
| 351 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(TvInputAidlTest); |