blob: 9c2606984b7b5d99c046858f48f72992782faa15 [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++) {
166 int32_t stream_id = config[i].streamId;
167 ALOGD("OpenAndCloseStreamTest: open stream, device_id=%d, stream_id=%d", device_id,
168 stream_id);
169 ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle_).isOk());
170 ALOGD("OpenAndCloseStreamTest: close stream, device_id=%d, stream_id=%d", device_id,
171 stream_id);
172 ASSERT_TRUE(tv_input_->closeStream(device_id, stream_id).isOk());
173 }
174 }
175}
176
177/*
178 * InvalidDeviceIdTest:
179 * Calls updateStreamConfigurations(), openStream(), and closeStream()
180 * for a non-existing device
181 * Checks returned results
182 * The results should be ITvInput::STATUS_INVALID_ARGUMENTS
183 */
184TEST_P(TvInputAidlTest, InvalidDeviceIdTest) {
185 unique_lock<mutex> lock(mutex_);
186
187 vector<int32_t> device_ids;
188 for (size_t i = 0; i < device_info_.size(); i++) {
189 device_ids.push_back(device_info_.keyAt(i));
190 }
191 // Get a non-existing device ID.
192 int32_t id = getNumNotIn(device_ids);
193 ALOGD("InvalidDeviceIdTest: update stream config, device_id=%d", id);
194 ASSERT_TRUE(updateStreamConfigurations(id).getServiceSpecificError() ==
195 ITvInput::STATUS_INVALID_ARGUMENTS);
196
197 int32_t stream_id = 0;
198
199 ALOGD("InvalidDeviceIdTest: open stream, device_id=%d, stream_id=%d", id, stream_id);
200 ASSERT_TRUE(tv_input_->openStream(id, stream_id, &handle_).getServiceSpecificError() ==
201 ITvInput::STATUS_INVALID_ARGUMENTS);
202
203 ALOGD("InvalidDeviceIdTest: close stream, device_id=%d, stream_id=%d", id, stream_id);
204 ASSERT_TRUE(tv_input_->closeStream(id, stream_id).getServiceSpecificError() ==
205 ITvInput::STATUS_INVALID_ARGUMENTS);
206}
207
208/*
209 * InvalidStreamIdTest:
210 * Calls openStream(), and closeStream() for a non-existing stream
211 * Checks returned results
212 * The results should be ITvInput::STATUS_INVALID_ARGUMENTS
213 */
214TEST_P(TvInputAidlTest, InvalidStreamIdTest) {
215 unique_lock<mutex> lock(mutex_);
216
217 if (device_info_.isEmpty()) {
218 return;
219 }
220 updateAllStreamConfigurations();
221
222 int32_t device_id = device_info_.keyAt(0);
223 // Get a non-existing stream ID.
224 int32_t id = DEFAULT_ID;
225 if (stream_config_.indexOfKey(device_id) >= 0) {
226 vector<int32_t> stream_ids;
227 vector<TvStreamConfig> config = stream_config_.valueFor(device_id);
228 for (size_t i = 0; i < config.size(); i++) {
229 stream_ids.push_back(config[i].streamId);
230 }
231 id = getNumNotIn(stream_ids);
232 }
233
234 ALOGD("InvalidStreamIdTest: open stream, device_id=%d, stream_id=%d", device_id, id);
235 ASSERT_TRUE(tv_input_->openStream(device_id, id, &handle_).getServiceSpecificError() ==
236 ITvInput::STATUS_INVALID_ARGUMENTS);
237
238 ALOGD("InvalidStreamIdTest: close stream, device_id=%d, stream_id=%d", device_id, id);
239 ASSERT_TRUE(tv_input_->closeStream(device_id, id).getServiceSpecificError() ==
240 ITvInput::STATUS_INVALID_ARGUMENTS);
241}
242
243/*
244 * OpenAnOpenedStreamsTest:
245 * Calls openStream() twice for a stream (if any)
246 * Checks returned results
247 * The result of the second call should be ITvInput::STATUS_INVALID_STATE
248 */
249TEST_P(TvInputAidlTest, OpenAnOpenedStreamsTest) {
250 unique_lock<mutex> lock(mutex_);
251
252 updateAllStreamConfigurations();
253 vector<size_t> indices = getConfigIndices();
254 if (indices.empty()) {
255 return;
256 }
257 int32_t device_id = stream_config_.keyAt(indices[0]);
258 int32_t stream_id = stream_config_.valueAt(indices[0])[0].streamId;
259
260 ALOGD("OpenAnOpenedStreamsTest: open stream, device_id=%d, stream_id=%d", device_id, stream_id);
261 ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle_).isOk());
262
263 ALOGD("OpenAnOpenedStreamsTest: open stream, device_id=%d, stream_id=%d", device_id, stream_id);
264 ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle_).getServiceSpecificError() ==
265 ITvInput::STATUS_INVALID_STATE);
266
267 // close stream as subsequent tests assume no open streams
268 ALOGD("OpenAnOpenedStreamsTest: close stream, device_id=%d, stream_id=%d", device_id,
269 stream_id);
270 ASSERT_TRUE(tv_input_->closeStream(device_id, stream_id).isOk());
271}
272
273/*
274 * CloseStreamBeforeOpenTest:
275 * Calls closeStream() without calling openStream() for a stream (if any)
276 * Checks the returned result
277 * The result should be ITvInput::STATUS_INVALID_STATE
278 */
279TEST_P(TvInputAidlTest, CloseStreamBeforeOpenTest) {
280 unique_lock<mutex> lock(mutex_);
281
282 updateAllStreamConfigurations();
283 vector<size_t> indices = getConfigIndices();
284 if (indices.empty()) {
285 return;
286 }
287 int32_t device_id = stream_config_.keyAt(indices[0]);
288 int32_t stream_id = stream_config_.valueAt(indices[0])[0].streamId;
289
290 ALOGD("CloseStreamBeforeOpenTest: close stream, device_id=%d, stream_id=%d", device_id,
291 stream_id);
292 ASSERT_TRUE(tv_input_->closeStream(device_id, stream_id).getServiceSpecificError() ==
293 ITvInput::STATUS_INVALID_STATE);
294}
295
296INSTANTIATE_TEST_SUITE_P(PerInstance, TvInputAidlTest,
297 testing::ValuesIn(android::getAidlHalInstanceNames(ITvInput::descriptor)),
298 android::PrintInstanceNameToString);
299
300// TODO remove from the allow list once the cf tv target is enabled for testing
301GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(TvInputAidlTest);