Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b6d79ff1 authored by Kevin Rocard's avatar Kevin Rocard
Browse files

Audio VTS: Create utility lib



Move code that can be reuse between audio tests of the HIDL interface in
an utility library.

Test: run vts tests
Test: vts-tradefed run vts --module VtsHalAudioV2_0Target
Bug: 35700978

Change-Id: Ia930ad2b0a3ca31628b2cb767b265d54b210756b
Signed-off-by: default avatarKevin Rocard <krocard@google.com>
parent 06a2a403
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -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: [
+4 −59
Original line number Diff line number Diff line
@@ -21,9 +21,7 @@
#include <cstddef>
#include <cstdio>
#include <limits>
#include <list>
#include <string>
#include <type_traits>
#include <vector>

#include <VtsHalHidlTargetTestBase.h>
@@ -37,6 +35,8 @@
#include <android/hardware/audio/common/2.0/types.h>

#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:
 *   <testcase name="debugDump" status="run" time="6"
 *             classname="AudioPrimaryHidlTest"
               description="Dump the state of the hal." />
 * 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()>;
    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<TearDownFunc> 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;
}
+1 −0
Original line number Diff line number Diff line
@@ -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",
]
+32 −0
Original line number Diff line number Diff line
//
// 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"],
}
+42 −42
Original line number Diff line number Diff line
@@ -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 <algorithm>
#include <vector>

#include <hidl/Status.h>

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 <class T>
inline ::testing::AssertionResult assertIsOk(const char* expr,
                                             const Return<T>& ret) {
inline ::testing::AssertionResult assertIsOk(const char* expr, const Return<T>& 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 <class T, class Continuation>
inline ::testing::AssertionResult continueIfIsOk(const char* expr,
                                                 const Return<T>& ret,
inline ::testing::AssertionResult continueIfIsOk(const char* expr, const Return<T>& 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<Result>& 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<Result>& 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<Result>& expected,
    Result result) {
inline ::testing::AssertionResult assertResult(const char* e_expr, const char* r_expr,
                                               const std::vector<Result>& 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<Result>& expected,
inline ::testing::AssertionResult assertResult(const char* e_expr, const char* r_expr,
                                               const std::vector<Result>& expected,
                                               const Return<Result>& ret) {
    return continueIfIsOk(r_expr, ret, [&] {
        return assertResult(e_expr, r_expr, expected, Result{ret});
    });
    return continueIfIsOk(r_expr, ret,
                          [&] { return assertResult(e_expr, r_expr, expected, Result{ret}); });
}

inline ::testing::AssertionResult assertOk(const char* expr,
                                           const Return<void>& ret) {
inline ::testing::AssertionResult assertOk(const char* expr, const Return<void>& 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<Result>& ret) {
    return continueIfIsOk(expr, ret,
                          [&] { return assertOk(expr, Result{ret}); });
inline ::testing::AssertionResult assertOk(const char* expr, const Return<Result>& 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
Loading