Merge "Convert VtsHalTvInputV1_0TargetTest to parameterized gtest" into rvc-dev

This commit is contained in:
Amy Zhang
2020-04-01 00:12:30 +00:00
committed by Android (Google) Code Review
2 changed files with 232 additions and 261 deletions

View File

@@ -19,6 +19,9 @@ cc_test {
defaults: ["VtsHalTargetTestDefaults"],
srcs: ["VtsHalTvInputV1_0TargetTest.cpp"],
static_libs: ["android.hardware.tv.input@1.0"],
test_suites: ["general-tests"],
test_suites: [
"general-tests",
"vts-core",
],
require_root: true,
}

View File

@@ -17,11 +17,12 @@
#define LOG_TAG "tv_input_hidl_hal_test"
#include <android-base/logging.h>
#include <android/hardware/tv/input/1.0/types.h>
#include <android/hardware/tv/input/1.0/ITvInput.h>
#include <android/hardware/tv/input/1.0/ITvInputCallback.h>
#include <VtsHalHidlTargetTestBase.h>
#include <android/hardware/tv/input/1.0/types.h>
#include <gtest/gtest.h>
#include <hidl/GtestPrinter.h>
#include <hidl/ServiceManagement.h>
#include <utils/KeyedVector.h>
#include <mutex>
#include <vector>
@@ -42,179 +43,161 @@ using ::android::sp;
#define WAIT_FOR_EVENT_TIMEOUT 5
#define DEFAULT_ID INT32_MIN
// Test environment for TvInput HIDL HAL.
class TvInputHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
public:
// get the test environment singleton
static TvInputHidlEnvironment* Instance() {
static TvInputHidlEnvironment* instance = new TvInputHidlEnvironment;
return instance;
}
virtual void registerTestServices() override { registerTestService<ITvInput>(); }
private:
TvInputHidlEnvironment() {}
};
/* The main test class for TV Input HIDL HAL. */
class TvInputHidlTest : public ::testing::VtsHalHidlTargetTestBase {
public:
virtual void SetUp() override {
tv_input_ = ::testing::VtsHalHidlTargetTestBase::getService<ITvInput>(
TvInputHidlEnvironment::Instance()->getServiceName<ITvInput>());
ASSERT_NE(tv_input_, nullptr);
tv_input_callback_ = new TvInputCallback(*this);
ASSERT_NE(tv_input_callback_, nullptr);
tv_input_->setCallback(tv_input_callback_);
// All events received within the timeout should be handled.
sleep(WAIT_FOR_EVENT_TIMEOUT);
}
virtual void TearDown() override {}
/* Called when a DEVICE_AVAILABLE event is received. */
void onDeviceAvailable(const TvInputDeviceInfo& deviceInfo) {
device_info_.add(deviceInfo.deviceId, deviceInfo);
}
/* Called when a DEVICE_UNAVAILABLE event is received. */
void onDeviceUnavailable(int32_t deviceId) {
device_info_.removeItem(deviceId);
}
/* Called when a DEVICE_CONFIGURATIONS_CHANGED event is received. */
Result onStreamConfigurationsChanged(int32_t deviceId) {
return updateStreamConfigurations(deviceId);
}
/* Gets and updates the stream configurations for a device. */
Result updateStreamConfigurations(int32_t deviceId) {
stream_config_.removeItem(deviceId);
Result result = Result::UNKNOWN;
hidl_vec<TvStreamConfig> list;
tv_input_->getStreamConfigurations(deviceId,
[&result, &list](Result res, hidl_vec<TvStreamConfig> configs) {
result = res;
if (res == Result::OK) {
list = configs;
}
});
if (result == Result::OK) {
stream_config_.add(deviceId, list);
}
return result;
}
/* Gets and updates the stream configurations for all existing devices. */
void updateAllStreamConfigurations() {
for (size_t i = 0; i < device_info_.size(); i++) {
int32_t device_id = device_info_.keyAt(i);
updateStreamConfigurations(device_id);
}
}
/* Returns a list of indices of stream_config_ whose corresponding values are not empty. */
std::vector<size_t> getConfigIndices() {
std::vector<size_t> indices;
for (size_t i = 0; i < stream_config_.size(); i++) {
if (stream_config_.valueAt(i).size() != 0) {
indices.push_back(i);
}
}
return indices;
}
/*
* Returns DEFAULT_ID if there is no missing integer in the range [0, the size of nums).
* Otherwise, returns the smallest missing non-negative integer.
*/
int32_t getNumNotIn(std::vector<int32_t>& nums) {
int32_t result = DEFAULT_ID;
int32_t size = static_cast<int32_t>(nums.size());
for (int32_t i = 0; i < size; i++) {
// Put every element to its target position, if possible.
int32_t target_pos = nums[i];
while (target_pos >= 0 && target_pos < size && i != target_pos && nums[i] != nums[target_pos]) {
std::swap(nums[i], nums[target_pos]);
target_pos = nums[i];
}
class TvInputHidlTest : public testing::TestWithParam<std::string> {
public:
virtual void SetUp() override {
tv_input_ = ITvInput::getService(GetParam());
tv_input_callback_ = new TvInputCallback(*this);
ASSERT_NE(tv_input_callback_, nullptr);
tv_input_->setCallback(tv_input_callback_);
// All events received within the timeout should be handled.
sleep(WAIT_FOR_EVENT_TIMEOUT);
}
for (int32_t i = 0; i < size; i++) {
if (nums[i] != i) {
return i;
}
virtual void TearDown() override {}
/* Called when a DEVICE_AVAILABLE event is received. */
void onDeviceAvailable(const TvInputDeviceInfo& deviceInfo) {
device_info_.add(deviceInfo.deviceId, deviceInfo);
}
return result;
}
/* A simple test implementation of TvInputCallback for TV Input Events. */
class TvInputCallback : public ITvInputCallback {
public:
TvInputCallback(TvInputHidlTest& parent) : parent_(parent){};
/* Called when a DEVICE_UNAVAILABLE event is received. */
void onDeviceUnavailable(int32_t deviceId) { device_info_.removeItem(deviceId); }
virtual ~TvInputCallback() = default;
/* Called when a DEVICE_CONFIGURATIONS_CHANGED event is received. */
Result onStreamConfigurationsChanged(int32_t deviceId) {
return updateStreamConfigurations(deviceId);
}
/*
* Notifies the client that an event has occured. For possible event types,
* check TvInputEventType.
     */
Return<void> notify(const TvInputEvent& event) override {
std::unique_lock<std::mutex> lock(parent_.mutex_);
switch(event.type) {
case TvInputEventType::DEVICE_AVAILABLE:
parent_.onDeviceAvailable(event.deviceInfo);
break;
case TvInputEventType::DEVICE_UNAVAILABLE:
parent_.onDeviceUnavailable(event.deviceInfo.deviceId);
break;
case TvInputEventType::STREAM_CONFIGURATIONS_CHANGED:
parent_.onStreamConfigurationsChanged(event.deviceInfo.deviceId);
break;
}
return Void();
};
private:
/* The test contains this callback instance. */
TvInputHidlTest& parent_;
};
/* Gets and updates the stream configurations for a device. */
Result updateStreamConfigurations(int32_t deviceId) {
stream_config_.removeItem(deviceId);
Result result = Result::UNKNOWN;
hidl_vec<TvStreamConfig> list;
tv_input_->getStreamConfigurations(
deviceId, [&result, &list](Result res, hidl_vec<TvStreamConfig> configs) {
result = res;
if (res == Result::OK) {
list = configs;
}
});
if (result == Result::OK) {
stream_config_.add(deviceId, list);
}
return result;
}
/* The TvInput used for the test. */
sp<ITvInput> tv_input_;
/* Gets and updates the stream configurations for all existing devices. */
void updateAllStreamConfigurations() {
for (size_t i = 0; i < device_info_.size(); i++) {
int32_t device_id = device_info_.keyAt(i);
updateStreamConfigurations(device_id);
}
}
/* The TvInputCallback used for the test. */
sp<ITvInputCallback> tv_input_callback_;
/* Returns a list of indices of stream_config_ whose corresponding values are not empty. */
std::vector<size_t> getConfigIndices() {
std::vector<size_t> indices;
for (size_t i = 0; i < stream_config_.size(); i++) {
if (stream_config_.valueAt(i).size() != 0) {
indices.push_back(i);
}
}
return indices;
}
/*
* A KeyedVector stores device information of every available device.
* A key is a device ID and the corresponding value is the TvInputDeviceInfo.
*/
android::KeyedVector<int32_t, TvInputDeviceInfo> device_info_;
/*
* Returns DEFAULT_ID if there is no missing integer in the range [0, the size of nums).
* Otherwise, returns the smallest missing non-negative integer.
*/
int32_t getNumNotIn(std::vector<int32_t>& nums) {
int32_t result = DEFAULT_ID;
int32_t size = static_cast<int32_t>(nums.size());
for (int32_t i = 0; i < size; i++) {
// Put every element to its target position, if possible.
int32_t target_pos = nums[i];
while (target_pos >= 0 && target_pos < size && i != target_pos &&
nums[i] != nums[target_pos]) {
std::swap(nums[i], nums[target_pos]);
target_pos = nums[i];
}
}
/*
* A KeyedVector stores a list of stream configurations of every available device.
* A key is a device ID and the corresponding value is the stream configuration list.
*/
android::KeyedVector<int32_t, hidl_vec<TvStreamConfig>> stream_config_;
for (int32_t i = 0; i < size; i++) {
if (nums[i] != i) {
return i;
}
}
return result;
}
/* The mutex controls the access of shared data. */
std::mutex mutex_;
/* A simple test implementation of TvInputCallback for TV Input Events. */
class TvInputCallback : public ITvInputCallback {
public:
TvInputCallback(TvInputHidlTest& parent) : parent_(parent){};
virtual ~TvInputCallback() = default;
/*
* Notifies the client that an event has occurred. For possible event types,
* check TvInputEventType.
     */
Return<void> notify(const TvInputEvent& event) override {
std::unique_lock<std::mutex> lock(parent_.mutex_);
switch (event.type) {
case TvInputEventType::DEVICE_AVAILABLE:
parent_.onDeviceAvailable(event.deviceInfo);
break;
case TvInputEventType::DEVICE_UNAVAILABLE:
parent_.onDeviceUnavailable(event.deviceInfo.deviceId);
break;
case TvInputEventType::STREAM_CONFIGURATIONS_CHANGED:
parent_.onStreamConfigurationsChanged(event.deviceInfo.deviceId);
break;
}
return Void();
};
private:
/* The test contains this callback instance. */
TvInputHidlTest& parent_;
};
/* The TvInput used for the test. */
sp<ITvInput> tv_input_;
/* The TvInputCallback used for the test. */
sp<ITvInputCallback> tv_input_callback_;
/*
* A KeyedVector stores device information of every available device.
* A key is a device ID and the corresponding value is the TvInputDeviceInfo.
*/
android::KeyedVector<int32_t, TvInputDeviceInfo> device_info_;
/*
* A KeyedVector stores a list of stream configurations of every available device.
* A key is a device ID and the corresponding value is the stream configuration list.
*/
android::KeyedVector<int32_t, hidl_vec<TvStreamConfig>> stream_config_;
/* The mutex controls the access of shared data. */
std::mutex mutex_;
};
/*
* GetStreamConfigTest:
* Calls updateStreamConfigurations() for each existing device
* Checks returned results
*/
TEST_F(TvInputHidlTest, GetStreamConfigTest) {
std::unique_lock<std::mutex> lock(mutex_);
for (size_t i = 0; i < device_info_.size(); i++) {
int32_t device_id = device_info_.keyAt(i);
Result result = updateStreamConfigurations(device_id);
EXPECT_EQ(Result::OK, result);
}
TEST_P(TvInputHidlTest, GetStreamConfigTest) {
std::unique_lock<std::mutex> lock(mutex_);
for (size_t i = 0; i < device_info_.size(); i++) {
int32_t device_id = device_info_.keyAt(i);
Result result = updateStreamConfigurations(device_id);
EXPECT_EQ(Result::OK, result);
}
}
/*
@@ -222,26 +205,24 @@ TEST_F(TvInputHidlTest, GetStreamConfigTest) {
* Calls openStream() and then closeStream() for each existing stream
* Checks returned results
*/
TEST_F(TvInputHidlTest, OpenAndCloseStreamTest) {
std::unique_lock<std::mutex> lock(mutex_);
updateAllStreamConfigurations();
for (size_t j = 0; j < stream_config_.size(); j++) {
int32_t device_id = stream_config_.keyAt(j);
hidl_vec<TvStreamConfig> config = stream_config_.valueAt(j);
for (size_t i = 0; i < config.size(); i++) {
Result result = Result::UNKNOWN;
int32_t stream_id = config[i].streamId;
tv_input_->openStream(device_id, stream_id,
[&result](Result res, const native_handle_t*) {
result = res;
});
EXPECT_EQ(Result::OK, result);
TEST_P(TvInputHidlTest, OpenAndCloseStreamTest) {
std::unique_lock<std::mutex> lock(mutex_);
updateAllStreamConfigurations();
for (size_t j = 0; j < stream_config_.size(); j++) {
int32_t device_id = stream_config_.keyAt(j);
hidl_vec<TvStreamConfig> config = stream_config_.valueAt(j);
for (size_t i = 0; i < config.size(); i++) {
Result result = Result::UNKNOWN;
int32_t stream_id = config[i].streamId;
tv_input_->openStream(device_id, stream_id,
[&result](Result res, const native_handle_t*) { result = res; });
EXPECT_EQ(Result::OK, result);
result = Result::UNKNOWN;
result = tv_input_->closeStream(device_id, stream_id);
EXPECT_EQ(Result::OK, result);
result = Result::UNKNOWN;
result = tv_input_->closeStream(device_id, stream_id);
EXPECT_EQ(Result::OK, result);
}
}
}
}
/*
@@ -251,28 +232,26 @@ TEST_F(TvInputHidlTest, OpenAndCloseStreamTest) {
* Checks returned results
* The results should be Result::INVALID_ARGUMENTS
*/
TEST_F(TvInputHidlTest, InvalidDeviceIdTest) {
std::unique_lock<std::mutex> lock(mutex_);
TEST_P(TvInputHidlTest, InvalidDeviceIdTest) {
std::unique_lock<std::mutex> lock(mutex_);
std::vector<int32_t> device_ids;
for (size_t i = 0; i < device_info_.size(); i++) {
device_ids.push_back(device_info_.keyAt(i));
}
// Get a non-existing device ID.
int32_t id = getNumNotIn(device_ids);
EXPECT_EQ(Result::INVALID_ARGUMENTS, updateStreamConfigurations(id));
std::vector<int32_t> device_ids;
for (size_t i = 0; i < device_info_.size(); i++) {
device_ids.push_back(device_info_.keyAt(i));
}
// Get a non-existing device ID.
int32_t id = getNumNotIn(device_ids);
EXPECT_EQ(Result::INVALID_ARGUMENTS, updateStreamConfigurations(id));
Result result = Result::UNKNOWN;
int32_t stream_id = 0;
tv_input_->openStream(id, stream_id,
[&result](Result res, const native_handle_t*) {
result = res;
});
EXPECT_EQ(Result::INVALID_ARGUMENTS, result);
Result result = Result::UNKNOWN;
int32_t stream_id = 0;
tv_input_->openStream(id, stream_id,
[&result](Result res, const native_handle_t*) { result = res; });
EXPECT_EQ(Result::INVALID_ARGUMENTS, result);
result = Result::UNKNOWN;
result = tv_input_->closeStream(id, stream_id);
EXPECT_EQ(Result::INVALID_ARGUMENTS, result);
result = Result::UNKNOWN;
result = tv_input_->closeStream(id, stream_id);
EXPECT_EQ(Result::INVALID_ARGUMENTS, result);
}
/*
@@ -281,35 +260,33 @@ TEST_F(TvInputHidlTest, InvalidDeviceIdTest) {
* Checks returned results
* The results should be Result::INVALID_ARGUMENTS
*/
TEST_F(TvInputHidlTest, InvalidStreamIdTest) {
std::unique_lock<std::mutex> lock(mutex_);
if (device_info_.isEmpty()) {
return;
}
updateAllStreamConfigurations();
int32_t device_id = device_info_.keyAt(0);
// Get a non-existing stream ID.
int32_t id = DEFAULT_ID;
if (stream_config_.indexOfKey(device_id) >= 0) {
std::vector<int32_t> stream_ids;
hidl_vec<TvStreamConfig> config = stream_config_.valueFor(device_id);
for (size_t i = 0; i < config.size(); i++) {
stream_ids.push_back(config[i].streamId);
TEST_P(TvInputHidlTest, InvalidStreamIdTest) {
std::unique_lock<std::mutex> lock(mutex_);
if (device_info_.isEmpty()) {
return;
}
id = getNumNotIn(stream_ids);
}
updateAllStreamConfigurations();
Result result = Result::UNKNOWN;
tv_input_->openStream(device_id, id,
[&result](Result res, const native_handle_t*) {
result = res;
});
EXPECT_EQ(Result::INVALID_ARGUMENTS, result);
int32_t device_id = device_info_.keyAt(0);
// Get a non-existing stream ID.
int32_t id = DEFAULT_ID;
if (stream_config_.indexOfKey(device_id) >= 0) {
std::vector<int32_t> stream_ids;
hidl_vec<TvStreamConfig> config = stream_config_.valueFor(device_id);
for (size_t i = 0; i < config.size(); i++) {
stream_ids.push_back(config[i].streamId);
}
id = getNumNotIn(stream_ids);
}
result = Result::UNKNOWN;
result = tv_input_->closeStream(device_id, id);
EXPECT_EQ(Result::INVALID_ARGUMENTS, result);
Result result = Result::UNKNOWN;
tv_input_->openStream(device_id, id,
[&result](Result res, const native_handle_t*) { result = res; });
EXPECT_EQ(Result::INVALID_ARGUMENTS, result);
result = Result::UNKNOWN;
result = tv_input_->closeStream(device_id, id);
EXPECT_EQ(Result::INVALID_ARGUMENTS, result);
}
/*
@@ -318,28 +295,24 @@ TEST_F(TvInputHidlTest, InvalidStreamIdTest) {
* Checks returned results
* The result of the second call should be Result::INVALID_STATE
*/
TEST_F(TvInputHidlTest, OpenAnOpenedStreamsTest) {
std::unique_lock<std::mutex> lock(mutex_);
updateAllStreamConfigurations();
std::vector<size_t> indices = getConfigIndices();
if (indices.empty()) {
return;
}
int32_t device_id = stream_config_.keyAt(indices[0]);
int32_t stream_id = stream_config_.valueAt(indices[0])[0].streamId;
TEST_P(TvInputHidlTest, OpenAnOpenedStreamsTest) {
std::unique_lock<std::mutex> lock(mutex_);
updateAllStreamConfigurations();
std::vector<size_t> indices = getConfigIndices();
if (indices.empty()) {
return;
}
int32_t device_id = stream_config_.keyAt(indices[0]);
int32_t stream_id = stream_config_.valueAt(indices[0])[0].streamId;
Result result = Result::UNKNOWN;
tv_input_->openStream(device_id, stream_id,
[&result](Result res, const native_handle_t*) {
result = res;
});
EXPECT_EQ(Result::OK, result);
Result result = Result::UNKNOWN;
tv_input_->openStream(device_id, stream_id,
[&result](Result res, const native_handle_t*) { result = res; });
EXPECT_EQ(Result::OK, result);
tv_input_->openStream(device_id, stream_id,
[&result](Result res, const native_handle_t*) {
result = res;
});
EXPECT_EQ(Result::INVALID_STATE, result);
tv_input_->openStream(device_id, stream_id,
[&result](Result res, const native_handle_t*) { result = res; });
EXPECT_EQ(Result::INVALID_STATE, result);
}
/*
@@ -348,24 +321,19 @@ TEST_F(TvInputHidlTest, OpenAnOpenedStreamsTest) {
* Checks the returned result
* The result should be Result::INVALID_STATE
*/
TEST_F(TvInputHidlTest, CloseStreamBeforeOpenTest) {
std::unique_lock<std::mutex> lock(mutex_);
updateAllStreamConfigurations();
std::vector<size_t> indices = getConfigIndices();
if (indices.empty()) {
return;
}
int32_t device_id = stream_config_.keyAt(indices[0]);
int32_t stream_id = stream_config_.valueAt(indices[0])[0].streamId;
EXPECT_EQ(Result::INVALID_STATE, tv_input_->closeStream(device_id, stream_id));
}
int main(int argc, char **argv) {
::testing::AddGlobalTestEnvironment(TvInputHidlEnvironment::Instance());
::testing::InitGoogleTest(&argc, argv);
TvInputHidlEnvironment::Instance()->init(&argc, argv);
int status = RUN_ALL_TESTS();
ALOGI("Test result = %d", status);
return status;
TEST_P(TvInputHidlTest, CloseStreamBeforeOpenTest) {
std::unique_lock<std::mutex> lock(mutex_);
updateAllStreamConfigurations();
std::vector<size_t> indices = getConfigIndices();
if (indices.empty()) {
return;
}
int32_t device_id = stream_config_.keyAt(indices[0]);
int32_t stream_id = stream_config_.valueAt(indices[0])[0].streamId;
EXPECT_EQ(Result::INVALID_STATE, tv_input_->closeStream(device_id, stream_id));
}
INSTANTIATE_TEST_SUITE_P(
PerInstance, TvInputHidlTest,
testing::ValuesIn(android::hardware::getAllHalInstanceNames(ITvInput::descriptor)),
android::hardware::PrintInstanceNameToString);