diff --git a/audio/common/all-versions/test/utility/include/utility/AssertOk.h b/audio/common/all-versions/test/utility/include/utility/AssertOk.h index 5ac2fdc936..03a6305b10 100644 --- a/audio/common/all-versions/test/utility/include/utility/AssertOk.h +++ b/audio/common/all-versions/test/utility/include/utility/AssertOk.h @@ -114,6 +114,27 @@ inline ::testing::AssertionResult assertOk(const char* expr, const Return +static R extract(const Return& ret) { + if (!ret.isOk()) { + EXPECT_IS_OK(ret); + return R{}; + } + return ret; +} + +template +static void expectValueOrFailure(Result res, Value expectedValue, Value actualValue, + Result expectedFailure) { + if (res == Result::OK) { + ASSERT_EQ(expectedValue, actualValue); + } else { + ASSERT_EQ(expectedFailure, res) << "Unexpected result " << toString(res); + } +} + } // namespace utility } // namespace test } // namespace common diff --git a/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h b/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h index 0778720bb0..6c51c1ba7f 100644 --- a/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h +++ b/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h @@ -408,11 +408,7 @@ TEST_F(FloatAccessorPrimaryHidlTest, MasterVolumeTest) { class AudioPatchPrimaryHidlTest : public AudioPrimaryHidlTest { protected: - bool areAudioPatchesSupported() { - auto result = device->supportsAudioPatches(); - EXPECT_IS_OK(result); - return result; - } + bool areAudioPatchesSupported() { return extract(device->supportsAudioPatches()); } }; TEST_F(AudioPatchPrimaryHidlTest, AudioPatches) { @@ -829,17 +825,6 @@ INSTANTIATE_TEST_CASE_P( ////////////////////////////// IStream getters /////////////////////////////// ////////////////////////////////////////////////////////////////////////////// -/** Unpack the provided result. - * If the result is not OK, register a failure and return an undefined value. */ -template -static R extract(Return ret) { - if (!ret.isOk()) { - EXPECT_IS_OK(ret); - return R{}; - } - return ret; -} - /* Could not find a way to write a test for two parametrized class fixure * thus use this macro do duplicate tests for Input and Output stream */ #define TEST_IO_STREAM(test_name, documentation, code) \ @@ -1189,11 +1174,7 @@ TEST_P(OutputStreamTest, PrepareForWritingCheckOverflow) { struct Capability { Capability(IStreamOut* stream) { EXPECT_OK(stream->supportsPauseAndResume(returnIn(pause, resume))); - auto ret = stream->supportsDrain(); - EXPECT_IS_OK(ret); - if (ret.isOk()) { - drain = ret; - } + drain = extract(stream->supportsDrain()); } bool pause = false; bool resume = false; @@ -1205,19 +1186,6 @@ TEST_P(OutputStreamTest, SupportsPauseAndResumeAndDrain) { Capability(stream.get()); } -template -static void checkInvalidStateOr0(Result res, Value value) { - switch (res) { - case Result::INVALID_STATE: - break; - case Result::OK: - ASSERT_EQ(0U, value); - break; - default: - FAIL() << "Unexpected result " << toString(res); - } -} - TEST_P(OutputStreamTest, GetRenderPosition) { doc::test("A new stream render position should be 0 or INVALID_STATE"); uint32_t dspFrames; @@ -1226,7 +1194,7 @@ TEST_P(OutputStreamTest, GetRenderPosition) { doc::partialTest("getRenderPosition is not supported"); return; } - checkInvalidStateOr0(res, dspFrames); + expectValueOrFailure(res, 0U, dspFrames, Result::INVALID_STATE); } TEST_P(OutputStreamTest, GetNextWriteTimestamp) { @@ -1237,7 +1205,7 @@ TEST_P(OutputStreamTest, GetNextWriteTimestamp) { doc::partialTest("getNextWriteTimestamp is not supported"); return; } - checkInvalidStateOr0(res, timestampUs); + expectValueOrFailure(res, uint64_t{0}, timestampUs, Result::INVALID_STATE); } /** Stub implementation of out stream callback. */