diff --git a/audio/2.0/vts/functional/Android.bp b/audio/2.0/vts/functional/Android.bp index d72fb2cd16..daae7388ea 100644 --- a/audio/2.0/vts/functional/Android.bp +++ b/audio/2.0/vts/functional/Android.bp @@ -18,8 +18,7 @@ cc_test { name: "VtsHalAudioV2_0TargetTest", defaults: ["hidl_defaults"], srcs: ["AudioPrimaryHidlHalTest.cpp", - "ValidateAudioConfiguration.cpp", - "utility/ValidateXml.cpp"], + "ValidateAudioConfiguration.cpp"], shared_libs: [ "libbase", "liblog", @@ -30,6 +29,7 @@ cc_test { "libxml2", "android.hardware.audio@2.0", "android.hardware.audio.common@2.0", + "android.hardware.audio.common.test.utility", ], static_libs: ["VtsHalHidlTargetTestBase"], cflags: [ diff --git a/audio/2.0/vts/functional/AudioPrimaryHidlHalTest.cpp b/audio/2.0/vts/functional/AudioPrimaryHidlHalTest.cpp index 27479ffd46..eec2b10d84 100644 --- a/audio/2.0/vts/functional/AudioPrimaryHidlHalTest.cpp +++ b/audio/2.0/vts/functional/AudioPrimaryHidlHalTest.cpp @@ -21,9 +21,7 @@ #include #include #include -#include #include -#include #include #include @@ -37,6 +35,8 @@ #include #include "utility/AssertOk.h" +#include "utility/Documentation.h" +#include "utility/EnvironmentTearDown.h" #include "utility/PrettyPrintAudioTypes.h" #include "utility/ReturnIn.h" @@ -59,8 +59,7 @@ using ::android::hardware::audio::V2_0::IDevicesFactory; using ::android::hardware::audio::V2_0::IStream; using ::android::hardware::audio::V2_0::IStreamIn; using ::android::hardware::audio::V2_0::TimeSpec; -using ReadParameters = - ::android::hardware::audio::V2_0::IStreamIn::ReadParameters; +using ReadParameters = ::android::hardware::audio::V2_0::IStreamIn::ReadParameters; using ReadStatus = ::android::hardware::audio::V2_0::IStreamIn::ReadStatus; using ::android::hardware::audio::V2_0::IStreamOut; using ::android::hardware::audio::V2_0::IStreamOutCallback; @@ -81,61 +80,8 @@ using ::android::hardware::audio::common::V2_0::AudioOutputFlag; using ::android::hardware::audio::common::V2_0::AudioSource; using ::android::hardware::audio::common::V2_0::ThreadInfo; -using utility::returnIn; +using namespace ::android::hardware::audio::common::test::utility; -const char* getTestName() { - return ::testing::UnitTest::GetInstance()->current_test_info()->name(); -} - -namespace doc { -/** Document the current test case. - * Eg: calling `doc::test("Dump the state of the hal")` in the "debugDump" test - * will output: - * - * see - https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#logging-additional-information - */ -void test(const std::string& testCaseDocumentation) { - ::testing::Test::RecordProperty("description", testCaseDocumentation); -} - -/** Document why a test was not fully run. Usually due to an optional feature - * not implemented. */ -void partialTest(const std::string& reason) { - LOG(INFO) << "Test " << getTestName() << " partially run: " << reason; - ::testing::Test::RecordProperty("partialyRunTest", reason); -} - -/** Add a note to the test. */ -void note(const std::string& note) { - LOG(INFO) << "Test " << getTestName() << " noted: " << note; - ::testing::Test::RecordProperty("note", note); -} -} - -// Register callback for static object destruction -// Avoid destroying static objects after main return. -// Post main return destruction leads to incorrect gtest timing measurements as -// well as harder -// debuging if anything goes wrong during destruction. -class Environment : public ::testing::Environment { - public: - using TearDownFunc = std::function; - void registerTearDown(TearDownFunc&& tearDown) { - tearDowns.push_back(std::move(tearDown)); - } - - private: - void TearDown() override { - // Call the tear downs in reverse order of insertion - for (auto& tearDown : tearDowns) { - tearDown(); - } - } - std::list tearDowns; -}; // Instance to register global tearDown static Environment* environment; @@ -1402,6 +1348,5 @@ int main(int argc, char** argv) { ::testing::AddGlobalTestEnvironment(environment); ::testing::InitGoogleTest(&argc, argv); int status = RUN_ALL_TESTS(); - LOG(INFO) << "Test result = " << status; return status; } diff --git a/audio/Android.bp b/audio/Android.bp index c3c2be1dbd..8046672abf 100644 --- a/audio/Android.bp +++ b/audio/Android.bp @@ -4,6 +4,7 @@ subdirs = [ "2.0/vts/functional", "common/2.0", "common/2.0/default", + "common/test/utility", "effect/2.0", "effect/2.0/vts/functional", ] diff --git a/audio/common/test/utility/Android.bp b/audio/common/test/utility/Android.bp new file mode 100644 index 0000000000..cca56f6b6f --- /dev/null +++ b/audio/common/test/utility/Android.bp @@ -0,0 +1,32 @@ +// +// Copyright (C) 2017 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. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +cc_library_shared { + name: "android.hardware.audio.common.test.utility", + defaults : ["hidl_defaults"], + srcs: ["src/ValidateXml.cpp"], + cflags: [ + "-O0", + "-g", + "-Wextra", + ], + local_include_dirs: ["include/utility"], + export_include_dirs: ["include"], + shared_libs: ["libxml2", "liblog"], + static_libs: ["libgtest"], + export_static_lib_headers: ["libgtest"], +} + diff --git a/audio/2.0/vts/functional/utility/AssertOk.h b/audio/common/test/utility/include/utility/AssertOk.h similarity index 55% rename from audio/2.0/vts/functional/utility/AssertOk.h rename to audio/common/test/utility/include/utility/AssertOk.h index 4c8440e18c..d8aa451005 100644 --- a/audio/2.0/vts/functional/utility/AssertOk.h +++ b/audio/common/test/utility/include/utility/AssertOk.h @@ -13,12 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#ifndef ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_ASSERTOK_H +#define ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_ASSERTOK_H #include #include #include +namespace android { +namespace hardware { +namespace audio { +namespace common { +namespace test { +namespace utility { + namespace detail { // This is a detail namespace, thus it is OK to import a class as nobody else is @@ -27,81 +36,65 @@ using ::android::hardware::Return; using ::android::hardware::audio::V2_0::Result; template -inline ::testing::AssertionResult assertIsOk(const char* expr, - const Return& ret) { +inline ::testing::AssertionResult assertIsOk(const char* expr, const Return& ret) { return ::testing::AssertionResult(ret.isOk()) - << "Expected: " << expr - << "\n to be an OK Return but it is not: " << ret.description(); + << "Expected: " << expr << "\n to be an OK Return but it is not: " << ret.description(); } // Call continuation if the provided result isOk template -inline ::testing::AssertionResult continueIfIsOk(const char* expr, - const Return& ret, +inline ::testing::AssertionResult continueIfIsOk(const char* expr, const Return& ret, Continuation continuation) { auto isOkStatus = assertIsOk(expr, ret); return !isOkStatus ? isOkStatus : continuation(); } // Expect two equal Results -inline ::testing::AssertionResult assertResult(const char* e_expr, - const char* r_expr, +inline ::testing::AssertionResult assertResult(const char* e_expr, const char* r_expr, Result expected, Result result) { return ::testing::AssertionResult(expected == result) - << "Value of: " << r_expr - << "\n Actual: " << ::testing::PrintToString(result) - << "\nExpected: " << e_expr - << "\nWhich is: " << ::testing::PrintToString(expected); + << "Value of: " << r_expr << "\n Actual: " << ::testing::PrintToString(result) + << "\nExpected: " << e_expr << "\nWhich is: " << ::testing::PrintToString(expected); } // Expect two equal Results one being wrapped in an OK Return -inline ::testing::AssertionResult assertResult(const char* e_expr, - const char* r_expr, - Result expected, - const Return& ret) { - return continueIfIsOk(r_expr, ret, [&] { - return assertResult(e_expr, r_expr, expected, Result{ret}); - }); +inline ::testing::AssertionResult assertResult(const char* e_expr, const char* r_expr, + Result expected, const Return& ret) { + return continueIfIsOk(r_expr, ret, + [&] { return assertResult(e_expr, r_expr, expected, Result{ret}); }); } // Expect a Result to be part of a list of Results -inline ::testing::AssertionResult assertResult( - const char* e_expr, const char* r_expr, const std::vector& expected, - Result result) { +inline ::testing::AssertionResult assertResult(const char* e_expr, const char* r_expr, + const std::vector& expected, Result result) { if (std::find(expected.begin(), expected.end(), result) != expected.end()) { return ::testing::AssertionSuccess(); // result is in expected } return ::testing::AssertionFailure() - << "Value of: " << r_expr - << "\n Actual: " << ::testing::PrintToString(result) + << "Value of: " << r_expr << "\n Actual: " << ::testing::PrintToString(result) << "\nExpected one of: " << e_expr << "\n Which is: " << ::testing::PrintToString(expected); } // Expect a Result wrapped in an OK Return to be part of a list of Results -inline ::testing::AssertionResult assertResult( - const char* e_expr, const char* r_expr, const std::vector& expected, - const Return& ret) { - return continueIfIsOk(r_expr, ret, [&] { - return assertResult(e_expr, r_expr, expected, Result{ret}); - }); +inline ::testing::AssertionResult assertResult(const char* e_expr, const char* r_expr, + const std::vector& expected, + const Return& ret) { + return continueIfIsOk(r_expr, ret, + [&] { return assertResult(e_expr, r_expr, expected, Result{ret}); }); } -inline ::testing::AssertionResult assertOk(const char* expr, - const Return& ret) { +inline ::testing::AssertionResult assertOk(const char* expr, const Return& ret) { return assertIsOk(expr, ret); } inline ::testing::AssertionResult assertOk(const char* expr, Result result) { return ::testing::AssertionResult(result == Result::OK) - << "Expected success: " << expr - << "\nActual: " << ::testing::PrintToString(result); + << "Expected success: " << expr << "\nActual: " << ::testing::PrintToString(result); } -inline ::testing::AssertionResult assertOk(const char* expr, - const Return& ret) { - return continueIfIsOk(expr, ret, - [&] { return assertOk(expr, Result{ret}); }); +inline ::testing::AssertionResult assertOk(const char* expr, const Return& ret) { + return continueIfIsOk(expr, ret, [&] { return assertOk(expr, Result{ret}); }); } } @@ -112,7 +105,14 @@ inline ::testing::AssertionResult assertOk(const char* expr, #define ASSERT_OK(ret) ASSERT_PRED_FORMAT1(detail::assertOk, ret) #define EXPECT_OK(ret) EXPECT_PRED_FORMAT1(detail::assertOk, ret) -#define ASSERT_RESULT(expected, ret) \ - ASSERT_PRED_FORMAT2(detail::assertResult, expected, ret) -#define EXPECT_RESULT(expected, ret) \ - EXPECT_PRED_FORMAT2(detail::assertResult, expected, ret) +#define ASSERT_RESULT(expected, ret) ASSERT_PRED_FORMAT2(detail::assertResult, expected, ret) +#define EXPECT_RESULT(expected, ret) EXPECT_PRED_FORMAT2(detail::assertResult, expected, ret) + +} // utility +} // test +} // common +} // audio +} // test +} // utility + +#endif // ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_ASSERTOK_H diff --git a/audio/common/test/utility/include/utility/Documentation.h b/audio/common/test/utility/include/utility/Documentation.h new file mode 100644 index 0000000000..a45cad6aff --- /dev/null +++ b/audio/common/test/utility/include/utility/Documentation.h @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_ENVIRONMENT_TEARDOWN +#define ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_ENVIRONMENT_TEARDOWN + +#include + +namespace android { +namespace hardware { +namespace audio { +namespace common { +namespace test { +namespace utility { + +namespace doc { +namespace detail { +const char* getTestName() { + return ::testing::UnitTest::GetInstance()->current_test_info()->name(); +} +} // namespace detail + +/** Document the current test case. + * Eg: calling `doc::test("Dump the state of the hal")` in the "debugDump" test + * will output: + * + * see + https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#logging-additional-information + */ +void test(const std::string& testCaseDocumentation) { + ::testing::Test::RecordProperty("description", testCaseDocumentation); +} + +/** Document why a test was not fully run. Usually due to an optional feature + * not implemented. */ +void partialTest(const std::string& reason) { + LOG(INFO) << "Test " << detail::getTestName() << " partially run: " << reason; + ::testing::Test::RecordProperty("partialyRunTest", reason); +} + +/** Add a note to the test. */ +void note(const std::string& note) { + LOG(INFO) << "Test " << detail::getTestName() << " noted: " << note; + ::testing::Test::RecordProperty("note", note); +} +} // namespace doc + +} // utility +} // test +} // common +} // audio +} // test +} // utility + +#endif // ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_ENVIRONMENT_TEARDOWN diff --git a/audio/common/test/utility/include/utility/EnvironmentTearDown.h b/audio/common/test/utility/include/utility/EnvironmentTearDown.h new file mode 100644 index 0000000000..15b0bd81fc --- /dev/null +++ b/audio/common/test/utility/include/utility/EnvironmentTearDown.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_ENVIRONMENT_TEARDOWN_H +#define ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_ENVIRONMENT_TEARDOWN_H + +#include +#include + +#include + +namespace android { +namespace hardware { +namespace audio { +namespace common { +namespace test { +namespace utility { + +/** Register callback for static object destruction + * Avoid destroying static objects after main return. + * Post main return destruction leads to incorrect gtest timing measurements as + * well as harder debuging if anything goes wrong during destruction. */ +class Environment : public ::testing::Environment { + public: + using TearDownFunc = std::function; + void registerTearDown(TearDownFunc&& tearDown) { tearDowns.push_back(std::move(tearDown)); } + + private: + void TearDown() override { + // Call the tear downs in reverse order of insertion + for (auto& tearDown : tearDowns) { + tearDown(); + } + } + std::list tearDowns; +}; + +} // utility +} // test +} // common +} // audio +} // test +} // utility + +#endif // ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_ENVIRONMENT_TEARDOWN_H diff --git a/audio/2.0/vts/functional/utility/PrettyPrintAudioTypes.h b/audio/common/test/utility/include/utility/PrettyPrintAudioTypes.h similarity index 65% rename from audio/2.0/vts/functional/utility/PrettyPrintAudioTypes.h rename to audio/common/test/utility/include/utility/PrettyPrintAudioTypes.h index 025cd1c6a4..37059e748a 100644 --- a/audio/2.0/vts/functional/utility/PrettyPrintAudioTypes.h +++ b/audio/common/test/utility/include/utility/PrettyPrintAudioTypes.h @@ -13,11 +13,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +#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 + /** @file Use HIDL generated toString methods to pretty print gtest errors */ -namespace detail { +namespace prettyPrintAudioTypesDetail { // Print the value of an enum as hex template @@ -25,7 +33,7 @@ inline void printUnderlyingValue(Enum value, ::std::ostream* os) { *os << std::hex << " (0x" << static_cast>(value) << ")"; } -} // namespace detail +} // namespace detail namespace android { namespace hardware { @@ -34,10 +42,10 @@ namespace V2_0 { inline void PrintTo(const Result& result, ::std::ostream* os) { *os << toString(result); - detail::printUnderlyingValue(result, os); + prettyPrintAudioTypesDetail::printUnderlyingValue(result, os); } -} // namespace V2_0 +} // namespace V2_0 namespace common { namespace V2_0 { @@ -47,16 +55,18 @@ inline void PrintTo(const AudioConfig& config, ::std::ostream* os) { inline void PrintTo(const AudioDevice& device, ::std::ostream* os) { *os << toString(device); - detail::printUnderlyingValue(device, os); + prettyPrintAudioTypesDetail::printUnderlyingValue(device, os); } inline void PrintTo(const AudioChannelMask& channelMask, ::std::ostream* os) { *os << toString(channelMask); - detail::printUnderlyingValue(channelMask, os); + prettyPrintAudioTypesDetail::printUnderlyingValue(channelMask, os); } -} // namespace V2_0 -} // namespace common -} // namespace audio -} // namespace hardware -} // namespace android +} // namespace V2_0 +} // namespace common +} // namespace audio +} // namespace hardware +} // namespace android + +#endif // ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_PRETTY_PRINT_AUDIO_TYPES_H diff --git a/audio/2.0/vts/functional/utility/ReturnIn.h b/audio/common/test/utility/include/utility/ReturnIn.h similarity index 75% rename from audio/2.0/vts/functional/utility/ReturnIn.h rename to audio/common/test/utility/include/utility/ReturnIn.h index bb2389a2fc..08d502f2d4 100644 --- a/audio/2.0/vts/functional/utility/ReturnIn.h +++ b/audio/common/test/utility/include/utility/ReturnIn.h @@ -14,29 +14,37 @@ * limitations under the License. */ +#ifndef ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_RETURN_IN_H +#define ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_RETURN_IN_H + #include +namespace android { +namespace hardware { +namespace audio { +namespace common { +namespace test { namespace utility { namespace detail { // Helper class to generate the HIDL synchronous callback template class ReturnIn { - public: + public: // Provide to the constructor the variables where the output parameters must be copied // TODO: take pointers to match google output parameter style ? ReturnIn(ResultStore&... ts) : results(ts...) {} // Synchronous callback template - void operator() (Results&&...results) { + void operator()(Results&&... results) { set(std::forward(results)...); } - private: + + private: // Recursively set all output parameters template void set(Head&& head, Tail&&... tail) { - std::get(results) - = std::forward(head); + std::get(results) = std::forward(head); set(tail...); } // Trivial case @@ -45,7 +53,7 @@ class ReturnIn { // All variables to set are stored here std::tuple results; }; -} // namespace detail +} // namespace detail // Generate the HIDL synchronous callback with a copy policy // Input: the variables (lvalue reference) where to save the return values @@ -53,6 +61,15 @@ class ReturnIn { // The output parameters *will be copied* do not use this function if you have // a zero copy policy template -detail::ReturnIn returnIn(ResultStore&... ts) { return {ts...};} - +detail::ReturnIn returnIn(ResultStore&... ts) { + return {ts...}; } + +} // utility +} // test +} // common +} // audio +} // test +} // utility + +#endif // ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_RETURN_IN_H diff --git a/audio/2.0/vts/functional/utility/ValidateXml.h b/audio/common/test/utility/include/utility/ValidateXml.h similarity index 68% rename from audio/2.0/vts/functional/utility/ValidateXml.h rename to audio/common/test/utility/include/utility/ValidateXml.h index edb09bf313..fdfa50666e 100644 --- a/audio/2.0/vts/functional/utility/ValidateXml.h +++ b/audio/common/test/utility/include/utility/ValidateXml.h @@ -14,15 +14,17 @@ * limitations under the License. */ -#ifndef ANDROID_HARDWARE_AUDIO_TEST_VALIDATEXML -#define ANDROID_HARDWARE_AUDIO_TEST_VALIDATEXML +#ifndef ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_VALIDATE_XML_H +#define ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_VALIDATE_XML_H #include namespace android { namespace hardware { namespace audio { +namespace common { namespace test { +namespace utility { /** Validate the provided XmlFile with the provided xsdFile. * Intended to use with ASSERT_PRED_FORMAT2 as such: @@ -33,12 +35,15 @@ namespace test { const char* xmlFilePath, const char* xsdPathName); /** Helper gtest ASSERT to test xml validity against an xsd. */ -#define ASSERT_VALID_XML(xmlFilePath, xsdFilePath) \ - ASSERT_PRED_FORMAT2(::android::hardware::audio::test::validateXml, xmlFilePath, xsdFilePath) +#define ASSERT_VALID_XML(xmlFilePath, xsdFilePath) \ + ASSERT_PRED_FORMAT2(::android::hardware::audio::common::test::utility::validateXml, \ + xmlFilePath, xsdFilePath) -} // namespace test -} // namespace audio -} // namespace hardware -} // namespace android +} // utility +} // test +} // common +} // audio +} // test +} // utility -#endif // ANDROID_HARDWARE_AUDIO_TEST_VALIDATEXML +#endif // ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_VALIDATE_XML_H diff --git a/audio/2.0/vts/functional/utility/ValidateXml.cpp b/audio/common/test/utility/src/ValidateXml.cpp similarity index 97% rename from audio/2.0/vts/functional/utility/ValidateXml.cpp rename to audio/common/test/utility/src/ValidateXml.cpp index 2fb828e307..784f9401ec 100644 --- a/audio/2.0/vts/functional/utility/ValidateXml.cpp +++ b/audio/common/test/utility/src/ValidateXml.cpp @@ -30,7 +30,9 @@ namespace android { namespace hardware { namespace audio { +namespace common { namespace test { +namespace utility { /** Map libxml2 structures to their corresponding deleters. */ template @@ -125,7 +127,9 @@ struct Libxml2Global { return ::testing::AssertionSuccess(); } -} // namespace test -} // namespace audio -} // namespace hardware -} // namespace android +} // utility +} // test +} // common +} // audio +} // test +} // utility