From b34af7c1924216ca4c452147802d2b81ea369002 Mon Sep 17 00:00:00 2001 From: Kevin Rocard Date: Fri, 15 Dec 2017 09:31:30 -0800 Subject: [PATCH] Audio V4: Make test helper version independent Bug: 38184704 Test: compile Change-Id: Ia9ec81ccbad1d7411fdc570ae6dd728dd1520065 --- .../functional/AudioPrimaryHidlHalTest.cpp | 8 ++- audio/common/2.0/default/Android.bp | 2 +- audio/common/2.0/default/HidlUtils.cpp | 2 +- audio/common/2.0/default/HidlUtils.h | 2 +- .../test/utility/include/utility/AssertOk.h | 14 ++-- .../include/utility/PrettyPrintAudioTypes.h | 65 ++++++++----------- 6 files changed, 45 insertions(+), 48 deletions(-) diff --git a/audio/2.0/vts/functional/AudioPrimaryHidlHalTest.cpp b/audio/2.0/vts/functional/AudioPrimaryHidlHalTest.cpp index fd175de8f5..73bf61c3d5 100644 --- a/audio/2.0/vts/functional/AudioPrimaryHidlHalTest.cpp +++ b/audio/2.0/vts/functional/AudioPrimaryHidlHalTest.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -40,9 +41,11 @@ #include "utility/AssertOk.h" #include "utility/Documentation.h" #include "utility/EnvironmentTearDown.h" +#define AUDIO_HAL_VERSION V2_0 #include "utility/PrettyPrintAudioTypes.h" #include "utility/ReturnIn.h" +using std::initializer_list; using std::string; using std::to_string; using std::vector; @@ -856,7 +859,7 @@ TEST_IO_STREAM(GetHwAvSync, "Get hardware sync can not fail", ASSERT_IS_OK(device->getHwAvSync())); static void checkGetNoParameter(IStream* stream, hidl_vec keys, - vector expectedResults) { + initializer_list expectedResults) { hidl_vec parameters; Result res; ASSERT_OK(stream->getParameters(keys, returnIn(res, parameters))); @@ -924,8 +927,7 @@ TEST_IO_STREAM(RemoveNonExistingEffect, TEST_IO_STREAM(standby, "Make sure the stream can be put in stanby", ASSERT_OK(stream->standby())) // can not fail -static vector invalidStateOrNotSupported = {Result::INVALID_STATE, - Result::NOT_SUPPORTED}; +static constexpr auto invalidStateOrNotSupported = {Result::INVALID_STATE, Result::NOT_SUPPORTED}; TEST_IO_STREAM(startNoMmap, "Starting a mmaped stream before mapping it should fail", diff --git a/audio/common/2.0/default/Android.bp b/audio/common/2.0/default/Android.bp index c1114764b2..ac66479c93 100644 --- a/audio/common/2.0/default/Android.bp +++ b/audio/common/2.0/default/Android.bp @@ -1,5 +1,5 @@ // -// Copyright (C) 2017 The Android Open Source Project +// Copyright (C) 2016 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/audio/common/2.0/default/HidlUtils.cpp b/audio/common/2.0/default/HidlUtils.cpp index 9b617bb96f..9771b7bda2 100644 --- a/audio/common/2.0/default/HidlUtils.cpp +++ b/audio/common/2.0/default/HidlUtils.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 The Android Open Source Project + * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/audio/common/2.0/default/HidlUtils.h b/audio/common/2.0/default/HidlUtils.h index 3d39933e3b..24543b1617 100644 --- a/audio/common/2.0/default/HidlUtils.h +++ b/audio/common/2.0/default/HidlUtils.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 The Android Open Source Project + * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/audio/common/test/utility/include/utility/AssertOk.h b/audio/common/test/utility/include/utility/AssertOk.h index 1b8abb53d2..11e1c24783 100644 --- a/audio/common/test/utility/include/utility/AssertOk.h +++ b/audio/common/test/utility/include/utility/AssertOk.h @@ -17,7 +17,7 @@ #define ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_ASSERTOK_H #include -#include +#include #include @@ -33,7 +33,6 @@ namespace detail { // This is a detail namespace, thus it is OK to import a class as nobody else is // allowed to use it using ::android::hardware::Return; -using ::android::hardware::audio::V2_0::Result; template inline ::testing::AssertionResult assertIsOk(const char* expr, const Return& ret) { @@ -50,6 +49,7 @@ inline ::testing::AssertionResult continueIfIsOk(const char* expr, const Return< } // Expect two equal Results +template inline ::testing::AssertionResult assertResult(const char* e_expr, const char* r_expr, Result expected, Result result) { return ::testing::AssertionResult(expected == result) @@ -58,6 +58,7 @@ inline ::testing::AssertionResult assertResult(const char* e_expr, const char* r } // Expect two equal Results one being wrapped in an OK Return +template inline ::testing::AssertionResult assertResult(const char* e_expr, const char* r_expr, Result expected, const Return& ret) { return continueIfIsOk(r_expr, ret, @@ -65,8 +66,10 @@ inline ::testing::AssertionResult assertResult(const char* e_expr, const char* r } // Expect a Result to be part of a list of Results +template inline ::testing::AssertionResult assertResult(const char* e_expr, const char* r_expr, - const std::vector& expected, Result result) { + const std::initializer_list& expected, + Result result) { if (std::find(expected.begin(), expected.end(), result) != expected.end()) { return ::testing::AssertionSuccess(); // result is in expected } @@ -77,8 +80,9 @@ inline ::testing::AssertionResult assertResult(const char* e_expr, const char* r } // Expect a Result wrapped in an OK Return to be part of a list of Results +template inline ::testing::AssertionResult assertResult(const char* e_expr, const char* r_expr, - const std::vector& expected, + const std::initializer_list& expected, const Return& ret) { return continueIfIsOk(r_expr, ret, [&] { return assertResult(e_expr, r_expr, expected, Result{ret}); }); @@ -88,11 +92,13 @@ inline ::testing::AssertionResult assertOk(const char* expr, const Return& return assertIsOk(expr, ret); } +template inline ::testing::AssertionResult assertOk(const char* expr, Result result) { return ::testing::AssertionResult(result == Result::OK) << "Expected success: " << expr << "\nActual: " << ::testing::PrintToString(result); } +template inline ::testing::AssertionResult assertOk(const char* expr, const Return& ret) { return continueIfIsOk(expr, ret, [&] { return assertOk(expr, Result{ret}); }); } diff --git a/audio/common/test/utility/include/utility/PrettyPrintAudioTypes.h b/audio/common/test/utility/include/utility/PrettyPrintAudioTypes.h index 37059e748a..88a67e077f 100644 --- a/audio/common/test/utility/include/utility/PrettyPrintAudioTypes.h +++ b/audio/common/test/utility/include/utility/PrettyPrintAudioTypes.h @@ -14,57 +14,46 @@ * limitations under the License. */ +#ifndef AUDIO_HAL_VERSION +#error "AUDIO_HAL_VERSION must be set before including this file." +#endif + #ifndef ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_PRETTY_PRINT_AUDIO_TYPES_H #define ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_PRETTY_PRINT_AUDIO_TYPES_H #include -#include +#include -#include -#include - -/** @file Use HIDL generated toString methods to pretty print gtest errors */ - -namespace prettyPrintAudioTypesDetail { - -// Print the value of an enum as hex -template -inline void printUnderlyingValue(Enum value, ::std::ostream* os) { - *os << std::hex << " (0x" << static_cast>(value) << ")"; -} - -} // namespace detail +/** @file Use HIDL generated toString methods to pretty print gtest errors + * Unfortunately Gtest does not offer a template to specialize, only + * overloading PrintTo. + * @note that this overload can NOT be template because + * the fallback is already template, resulting in ambiguity. + * @note that the overload MUST be in the exact namespace + * the type is declared in, as per the ADL rules. + */ namespace android { namespace hardware { namespace audio { -namespace V2_0 { -inline void PrintTo(const Result& result, ::std::ostream* os) { - *os << toString(result); - prettyPrintAudioTypesDetail::printUnderlyingValue(result, os); -} +#define DEFINE_GTEST_PRINT_TO(T) \ + inline void PrintTo(const T& val, ::std::ostream* os) { *os << toString(val); } + +namespace AUDIO_HAL_VERSION { +DEFINE_GTEST_PRINT_TO(Result) +} // namespace AUDIO_HAL_VERSION -} // namespace V2_0 namespace common { -namespace V2_0 { - -inline void PrintTo(const AudioConfig& config, ::std::ostream* os) { - *os << toString(config); -} - -inline void PrintTo(const AudioDevice& device, ::std::ostream* os) { - *os << toString(device); - prettyPrintAudioTypesDetail::printUnderlyingValue(device, os); -} - -inline void PrintTo(const AudioChannelMask& channelMask, ::std::ostream* os) { - *os << toString(channelMask); - prettyPrintAudioTypesDetail::printUnderlyingValue(channelMask, os); -} - -} // namespace V2_0 +namespace AUDIO_HAL_VERSION { +DEFINE_GTEST_PRINT_TO(AudioConfig) +DEFINE_GTEST_PRINT_TO(AudioDevice) +DEFINE_GTEST_PRINT_TO(AudioChannelMask) +} // namespace AUDIO_HAL_VERSION } // namespace common + +#undef DEFINE_GTEST_PRINT_TO + } // namespace audio } // namespace hardware } // namespace android