blob: a2415b4f8e9e0858fb6a901169159e60502a7c44 [file] [log] [blame]
Yixiao Luoce501332022-08-12 11:18:18 -07001/*
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
24using namespace VtsHalTvInputTargetTest;
25
26TvInputAidlTest::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 Zhao6bdbc5e2023-01-12 16:51:03 -080046::ndk::ScopedAStatus TvInputAidlTest::TvInputCallback::notifyTvMessageEvent(
47 const TvMessageEvent& in_event) {
48 return ::ndk::ScopedAStatus::ok();
49}
50
Yixiao Luoce501332022-08-12 11:18:18 -070051void 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
69void TvInputAidlTest::TearDown() {
70 tv_input_ = nullptr;
71}
72
73void TvInputAidlTest::onDeviceAvailable(const TvInputDeviceInfo& deviceInfo) {
74 ALOGD("onDeviceAvailable for device id %d", deviceInfo.deviceId);
75 device_info_.add(deviceInfo.deviceId, deviceInfo);
76}
77
78void 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
99void 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
106vector<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
116int32_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 */
142TEST_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 */
157TEST_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 Luo4cd52a92022-10-28 15:51:48 -0700166 NativeHandle handle;
Yixiao Luoce501332022-08-12 11:18:18 -0700167 int32_t stream_id = config[i].streamId;
168 ALOGD("OpenAndCloseStreamTest: open stream, device_id=%d, stream_id=%d", device_id,
169 stream_id);
Yixiao Luo4cd52a92022-10-28 15:51:48 -0700170 ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle).isOk());
Yixiao Luoce501332022-08-12 11:18:18 -0700171 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 */
185TEST_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 Luo4cd52a92022-10-28 15:51:48 -0700199 NativeHandle handle;
Yixiao Luoce501332022-08-12 11:18:18 -0700200
201 ALOGD("InvalidDeviceIdTest: open stream, device_id=%d, stream_id=%d", id, stream_id);
Yixiao Luo4cd52a92022-10-28 15:51:48 -0700202 ASSERT_TRUE(tv_input_->openStream(id, stream_id, &handle).getServiceSpecificError() ==
Yixiao Luoce501332022-08-12 11:18:18 -0700203 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 */
216TEST_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 Luo4cd52a92022-10-28 15:51:48 -0700236 NativeHandle handle;
237
Yixiao Luoce501332022-08-12 11:18:18 -0700238 ALOGD("InvalidStreamIdTest: open stream, device_id=%d, stream_id=%d", device_id, id);
Yixiao Luo4cd52a92022-10-28 15:51:48 -0700239 ASSERT_TRUE(tv_input_->openStream(device_id, id, &handle).getServiceSpecificError() ==
Yixiao Luoce501332022-08-12 11:18:18 -0700240 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 */
253TEST_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]);
262 int32_t stream_id = stream_config_.valueAt(indices[0])[0].streamId;
263
Yixiao Luo4cd52a92022-10-28 15:51:48 -0700264 NativeHandle handle;
Yixiao Luoce501332022-08-12 11:18:18 -0700265
266 ALOGD("OpenAnOpenedStreamsTest: open stream, device_id=%d, stream_id=%d", device_id, stream_id);
Yixiao Luo4cd52a92022-10-28 15:51:48 -0700267 ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle).isOk());
268
269 ALOGD("OpenAnOpenedStreamsTest: open stream, device_id=%d, stream_id=%d", device_id, stream_id);
270 ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle).getServiceSpecificError() ==
Yixiao Luoce501332022-08-12 11:18:18 -0700271 ITvInput::STATUS_INVALID_STATE);
272
273 // close stream as subsequent tests assume no open streams
274 ALOGD("OpenAnOpenedStreamsTest: close stream, device_id=%d, stream_id=%d", device_id,
275 stream_id);
276 ASSERT_TRUE(tv_input_->closeStream(device_id, stream_id).isOk());
277}
278
279/*
280 * CloseStreamBeforeOpenTest:
281 * Calls closeStream() without calling openStream() for a stream (if any)
282 * Checks the returned result
283 * The result should be ITvInput::STATUS_INVALID_STATE
284 */
285TEST_P(TvInputAidlTest, CloseStreamBeforeOpenTest) {
286 unique_lock<mutex> lock(mutex_);
287
288 updateAllStreamConfigurations();
289 vector<size_t> indices = getConfigIndices();
290 if (indices.empty()) {
291 return;
292 }
293 int32_t device_id = stream_config_.keyAt(indices[0]);
294 int32_t stream_id = stream_config_.valueAt(indices[0])[0].streamId;
295
296 ALOGD("CloseStreamBeforeOpenTest: close stream, device_id=%d, stream_id=%d", device_id,
297 stream_id);
298 ASSERT_TRUE(tv_input_->closeStream(device_id, stream_id).getServiceSpecificError() ==
299 ITvInput::STATUS_INVALID_STATE);
300}
301
302INSTANTIATE_TEST_SUITE_P(PerInstance, TvInputAidlTest,
303 testing::ValuesIn(android::getAidlHalInstanceNames(ITvInput::descriptor)),
304 android::PrintInstanceNameToString);
305
306// TODO remove from the allow list once the cf tv target is enabled for testing
307GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(TvInputAidlTest);