blob: e9f3251c6b76b8cfea8856733b63072b8a5ee209 [file] [log] [blame]
Mikhail Naganov00bac4e2022-09-15 04:06:57 +00001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
Mikhail Naganove9f10fc2022-10-14 23:31:52 +000019#include <algorithm>
20#include <initializer_list>
Mikhail Naganov00bac4e2022-09-15 04:06:57 +000021
22#include <android/binder_auto_utils.h>
Krzysztof KosiƄskia3a78a62023-03-04 00:58:52 +000023#include <gtest/gtest.h>
Jaideep Sharma74498412023-09-13 15:25:25 +053024#include <system/audio_aidl_utils.h>
Mikhail Naganov00bac4e2022-09-15 04:06:57 +000025
Mikhail Naganov00bac4e2022-09-15 04:06:57 +000026namespace android::hardware::audio::common::testing {
27
28namespace detail {
29
Jaideep Sharma74498412023-09-13 15:25:25 +053030class TestExecutionTracer : public ::testing::EmptyTestEventListener {
31 public:
32 void OnTestStart(const ::testing::TestInfo& test_info) override;
33 void OnTestEnd(const ::testing::TestInfo& test_info) override;
34 void OnTestPartResult(const ::testing::TestPartResult& result) override;
35 private:
36 static void TraceTestState(const std::string& state, const ::testing::TestInfo& test_info);
37};
38
Mikhail Naganov00bac4e2022-09-15 04:06:57 +000039inline ::testing::AssertionResult assertIsOk(const char* expr, const ::ndk::ScopedAStatus& status) {
40 if (status.isOk()) {
41 return ::testing::AssertionSuccess();
42 }
43 return ::testing::AssertionFailure()
44 << "Expected the transaction \'" << expr << "\' to succeed\n"
45 << " but it has failed with: " << status;
46}
47
48inline ::testing::AssertionResult assertResult(const char* exp_expr, const char* act_expr,
49 int32_t expected,
50 const ::ndk::ScopedAStatus& status) {
51 if (status.getExceptionCode() == expected) {
52 return ::testing::AssertionSuccess();
53 }
54 return ::testing::AssertionFailure()
55 << "Expected the transaction \'" << act_expr << "\' to fail with " << exp_expr
56 << "\n but is has completed with: " << status;
57}
58
Mikhail Naganove9f10fc2022-10-14 23:31:52 +000059template <typename T>
60inline ::testing::AssertionResult assertResult(const char* exp_expr, const char* act_expr,
61 const std::initializer_list<T>& expected,
62 const ::ndk::ScopedAStatus& status) {
63 if (std::find(expected.begin(), expected.end(), status.getExceptionCode()) != expected.end()) {
64 return ::testing::AssertionSuccess();
65 }
66 return ::testing::AssertionFailure() << "Expected the transaction \'" << act_expr
67 << "\' to complete with one of: " << exp_expr
68 << "\n which is: " << ::testing::PrintToString(expected)
69 << "\n but is has completed with: " << status;
70}
71
Mikhail Naganov00bac4e2022-09-15 04:06:57 +000072} // namespace detail
73
74} // namespace android::hardware::audio::common::testing
75
76// Test that the transaction status 'isOk'
77#define ASSERT_IS_OK(ret) \
78 ASSERT_PRED_FORMAT1(::android::hardware::audio::common::testing::detail::assertIsOk, ret)
79#define EXPECT_IS_OK(ret) \
80 EXPECT_PRED_FORMAT1(::android::hardware::audio::common::testing::detail::assertIsOk, ret)
81
82// Test that the transaction status is as expected.
83#define ASSERT_STATUS(expected, ret) \
84 ASSERT_PRED_FORMAT2(::android::hardware::audio::common::testing::detail::assertResult, \
85 expected, ret)
86#define EXPECT_STATUS(expected, ret) \
87 EXPECT_PRED_FORMAT2(::android::hardware::audio::common::testing::detail::assertResult, \
88 expected, ret)
Jaideep Sharmacba42862023-06-23 10:27:39 +053089
Shunkai Yao8771cec2023-09-20 22:46:59 +000090#define SKIP_TEST_IF_DATA_UNSUPPORTED(flags) \
91 ({ \
92 if ((flags).hwAcceleratorMode == Flags::HardwareAccelerator::TUNNEL || (flags).bypass) { \
93 GTEST_SKIP() << "Skip data path for offload"; \
94 } \
Mikhail Naganov95f22772023-10-25 08:44:47 -070095 })