blob: ec83e29bfd5542c96cb0aff7ba5c4d843db0ed90 [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
46void TvInputAidlTest::SetUp() {
47 if (AServiceManager_isDeclared(GetParam().c_str())) {
48 ::ndk::SpAIBinder binder(AServiceManager_waitForService(GetParam().c_str()));
49 tv_input_ = ITvInput::fromBinder(binder);
50 } else {
51 tv_input_ = nullptr;
52 }
53 ASSERT_NE(tv_input_, nullptr);
54
55 tv_input_callback_ =
56 ::ndk::SharedRefBase::make<TvInputCallback>(shared_ptr<TvInputAidlTest>(this));
57 ASSERT_NE(tv_input_callback_, nullptr);
58
59 tv_input_->setCallback(tv_input_callback_);
60 // All events received within the timeout should be handled.
61 sleep(WAIT_FOR_EVENT_TIMEOUT);
62}
63
64void TvInputAidlTest::TearDown() {
65 tv_input_ = nullptr;
66}
67
68void TvInputAidlTest::onDeviceAvailable(const TvInputDeviceInfo& deviceInfo) {
69 ALOGD("onDeviceAvailable for device id %d", deviceInfo.deviceId);
70 device_info_.add(deviceInfo.deviceId, deviceInfo);
71}
72
73void TvInputAidlTest::onDeviceUnavailable(int32_t deviceId) {
74 ALOGD("onDeviceUnavailable for device id %d", deviceId);
75 device_info_.removeItem(deviceId);
76 stream_config_.removeItem(deviceId);
77}
78
79::ndk::ScopedAStatus TvInputAidlTest::onStreamConfigurationsChanged(int32_t deviceId) {
80 ALOGD("onStreamConfigurationsChanged for device id %d", deviceId);
81 return updateStreamConfigurations(deviceId);
82}
83
84::ndk::ScopedAStatus TvInputAidlTest::updateStreamConfigurations(int32_t deviceId) {
85 stream_config_.removeItem(deviceId);
86 vector<TvStreamConfig> list;
87 ::ndk::ScopedAStatus status = tv_input_->getStreamConfigurations(deviceId, &list);
88 if (status.isOk()) {
89 stream_config_.add(deviceId, list);
90 }
91 return status;
92}
93
94void TvInputAidlTest::updateAllStreamConfigurations() {
95 for (size_t i = 0; i < device_info_.size(); i++) {
96 int32_t device_id = device_info_.keyAt(i);
97 updateStreamConfigurations(device_id);
98 }
99}
100
101vector<size_t> TvInputAidlTest::getConfigIndices() {
102 vector<size_t> indices;
103 for (size_t i = 0; i < stream_config_.size(); i++) {
104 if (stream_config_.valueAt(i).size() != 0) {
105 indices.push_back(i);
106 }
107 }
108 return indices;
109}
110
111int32_t TvInputAidlTest::getNumNotIn(vector<int32_t>& nums) {
112 int32_t result = DEFAULT_ID;
113 int32_t size = static_cast<int32_t>(nums.size());
114 for (int32_t i = 0; i < size; i++) {
115 // Put every element to its target position, if possible.
116 int32_t target_pos = nums[i];
117 while (target_pos >= 0 && target_pos < size && i != target_pos &&
118 nums[i] != nums[target_pos]) {
119 swap(nums[i], nums[target_pos]);
120 target_pos = nums[i];
121 }
122 }
123
124 for (int32_t i = 0; i < size; i++) {
125 if (nums[i] != i) {
126 return i;
127 }
128 }
129 return result;
130}
131
132/*
133 * GetStreamConfigTest:
134 * Calls updateStreamConfigurations() for each existing device
135 * Checks returned results
136 */
137TEST_P(TvInputAidlTest, GetStreamConfigTest) {
138 unique_lock<mutex> lock(mutex_);
139
140 for (size_t i = 0; i < device_info_.size(); i++) {
141 int32_t device_id = device_info_.keyAt(i);
142 ALOGD("GetStreamConfigTest: device_id=%d", device_id);
143 ASSERT_TRUE(updateStreamConfigurations(device_id).isOk());
144 }
145}
146
147/*
148 * OpenAndCloseStreamTest:
149 * Calls openStream() and then closeStream() for each existing stream
150 * Checks returned results
151 */
152TEST_P(TvInputAidlTest, OpenAndCloseStreamTest) {
153 unique_lock<mutex> lock(mutex_);
154
155 updateAllStreamConfigurations();
156
157 for (size_t j = 0; j < stream_config_.size(); j++) {
158 int32_t device_id = stream_config_.keyAt(j);
159 vector<TvStreamConfig> config = stream_config_.valueAt(j);
160 for (size_t i = 0; i < config.size(); i++) {
161 int32_t stream_id = config[i].streamId;
162 ALOGD("OpenAndCloseStreamTest: open stream, device_id=%d, stream_id=%d", device_id,
163 stream_id);
164 ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle_).isOk());
165 ALOGD("OpenAndCloseStreamTest: close stream, device_id=%d, stream_id=%d", device_id,
166 stream_id);
167 ASSERT_TRUE(tv_input_->closeStream(device_id, stream_id).isOk());
168 }
169 }
170}
171
172/*
173 * InvalidDeviceIdTest:
174 * Calls updateStreamConfigurations(), openStream(), and closeStream()
175 * for a non-existing device
176 * Checks returned results
177 * The results should be ITvInput::STATUS_INVALID_ARGUMENTS
178 */
179TEST_P(TvInputAidlTest, InvalidDeviceIdTest) {
180 unique_lock<mutex> lock(mutex_);
181
182 vector<int32_t> device_ids;
183 for (size_t i = 0; i < device_info_.size(); i++) {
184 device_ids.push_back(device_info_.keyAt(i));
185 }
186 // Get a non-existing device ID.
187 int32_t id = getNumNotIn(device_ids);
188 ALOGD("InvalidDeviceIdTest: update stream config, device_id=%d", id);
189 ASSERT_TRUE(updateStreamConfigurations(id).getServiceSpecificError() ==
190 ITvInput::STATUS_INVALID_ARGUMENTS);
191
192 int32_t stream_id = 0;
193
194 ALOGD("InvalidDeviceIdTest: open stream, device_id=%d, stream_id=%d", id, stream_id);
195 ASSERT_TRUE(tv_input_->openStream(id, stream_id, &handle_).getServiceSpecificError() ==
196 ITvInput::STATUS_INVALID_ARGUMENTS);
197
198 ALOGD("InvalidDeviceIdTest: close stream, device_id=%d, stream_id=%d", id, stream_id);
199 ASSERT_TRUE(tv_input_->closeStream(id, stream_id).getServiceSpecificError() ==
200 ITvInput::STATUS_INVALID_ARGUMENTS);
201}
202
203/*
204 * InvalidStreamIdTest:
205 * Calls openStream(), and closeStream() for a non-existing stream
206 * Checks returned results
207 * The results should be ITvInput::STATUS_INVALID_ARGUMENTS
208 */
209TEST_P(TvInputAidlTest, InvalidStreamIdTest) {
210 unique_lock<mutex> lock(mutex_);
211
212 if (device_info_.isEmpty()) {
213 return;
214 }
215 updateAllStreamConfigurations();
216
217 int32_t device_id = device_info_.keyAt(0);
218 // Get a non-existing stream ID.
219 int32_t id = DEFAULT_ID;
220 if (stream_config_.indexOfKey(device_id) >= 0) {
221 vector<int32_t> stream_ids;
222 vector<TvStreamConfig> config = stream_config_.valueFor(device_id);
223 for (size_t i = 0; i < config.size(); i++) {
224 stream_ids.push_back(config[i].streamId);
225 }
226 id = getNumNotIn(stream_ids);
227 }
228
229 ALOGD("InvalidStreamIdTest: open stream, device_id=%d, stream_id=%d", device_id, id);
230 ASSERT_TRUE(tv_input_->openStream(device_id, id, &handle_).getServiceSpecificError() ==
231 ITvInput::STATUS_INVALID_ARGUMENTS);
232
233 ALOGD("InvalidStreamIdTest: close stream, device_id=%d, stream_id=%d", device_id, id);
234 ASSERT_TRUE(tv_input_->closeStream(device_id, id).getServiceSpecificError() ==
235 ITvInput::STATUS_INVALID_ARGUMENTS);
236}
237
238/*
239 * OpenAnOpenedStreamsTest:
240 * Calls openStream() twice for a stream (if any)
241 * Checks returned results
242 * The result of the second call should be ITvInput::STATUS_INVALID_STATE
243 */
244TEST_P(TvInputAidlTest, OpenAnOpenedStreamsTest) {
245 unique_lock<mutex> lock(mutex_);
246
247 updateAllStreamConfigurations();
248 vector<size_t> indices = getConfigIndices();
249 if (indices.empty()) {
250 return;
251 }
252 int32_t device_id = stream_config_.keyAt(indices[0]);
253 int32_t stream_id = stream_config_.valueAt(indices[0])[0].streamId;
254
255 ALOGD("OpenAnOpenedStreamsTest: open stream, device_id=%d, stream_id=%d", device_id, stream_id);
256 ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle_).isOk());
257
258 ALOGD("OpenAnOpenedStreamsTest: open stream, device_id=%d, stream_id=%d", device_id, stream_id);
259 ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle_).getServiceSpecificError() ==
260 ITvInput::STATUS_INVALID_STATE);
261
262 // close stream as subsequent tests assume no open streams
263 ALOGD("OpenAnOpenedStreamsTest: close stream, device_id=%d, stream_id=%d", device_id,
264 stream_id);
265 ASSERT_TRUE(tv_input_->closeStream(device_id, stream_id).isOk());
266}
267
268/*
269 * CloseStreamBeforeOpenTest:
270 * Calls closeStream() without calling openStream() for a stream (if any)
271 * Checks the returned result
272 * The result should be ITvInput::STATUS_INVALID_STATE
273 */
274TEST_P(TvInputAidlTest, CloseStreamBeforeOpenTest) {
275 unique_lock<mutex> lock(mutex_);
276
277 updateAllStreamConfigurations();
278 vector<size_t> indices = getConfigIndices();
279 if (indices.empty()) {
280 return;
281 }
282 int32_t device_id = stream_config_.keyAt(indices[0]);
283 int32_t stream_id = stream_config_.valueAt(indices[0])[0].streamId;
284
285 ALOGD("CloseStreamBeforeOpenTest: close stream, device_id=%d, stream_id=%d", device_id,
286 stream_id);
287 ASSERT_TRUE(tv_input_->closeStream(device_id, stream_id).getServiceSpecificError() ==
288 ITvInput::STATUS_INVALID_STATE);
289}
290
291INSTANTIATE_TEST_SUITE_P(PerInstance, TvInputAidlTest,
292 testing::ValuesIn(android::getAidlHalInstanceNames(ITvInput::descriptor)),
293 android::PrintInstanceNameToString);
294
295// TODO remove from the allow list once the cf tv target is enabled for testing
296GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(TvInputAidlTest);