blob: e5000e31b1bcb1621726d78cdc025c132bce2628 [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
jiabindd23b0e2023-12-11 19:10:05 +000072inline ::testing::AssertionResult assertIsOkOrUnknownTransaction(
73 const char* expr, const ::ndk::ScopedAStatus& status) {
74 if (status.getStatus() == STATUS_UNKNOWN_TRANSACTION) {
75 return ::testing::AssertionSuccess();
76 }
77 return assertIsOk(expr, status);
78}
79
80inline ::testing::AssertionResult assertResultOrUnknownTransaction(
81 const char* exp_expr, const char* act_expr, int32_t expected,
82 const ::ndk::ScopedAStatus& status) {
83 if (status.getStatus() == STATUS_UNKNOWN_TRANSACTION) {
84 return ::testing::AssertionSuccess();
85 }
86 return assertResult(exp_expr, act_expr, expected, status);
87}
88
Mikhail Naganov00bac4e2022-09-15 04:06:57 +000089} // namespace detail
90
91} // namespace android::hardware::audio::common::testing
92
93// Test that the transaction status 'isOk'
94#define ASSERT_IS_OK(ret) \
95 ASSERT_PRED_FORMAT1(::android::hardware::audio::common::testing::detail::assertIsOk, ret)
96#define EXPECT_IS_OK(ret) \
97 EXPECT_PRED_FORMAT1(::android::hardware::audio::common::testing::detail::assertIsOk, ret)
98
99// Test that the transaction status is as expected.
100#define ASSERT_STATUS(expected, ret) \
101 ASSERT_PRED_FORMAT2(::android::hardware::audio::common::testing::detail::assertResult, \
102 expected, ret)
103#define EXPECT_STATUS(expected, ret) \
104 EXPECT_PRED_FORMAT2(::android::hardware::audio::common::testing::detail::assertResult, \
105 expected, ret)
Jaideep Sharmacba42862023-06-23 10:27:39 +0530106
Shunkai Yao50e478b2024-03-14 01:54:58 +0000107#define SKIP_TEST_IF_DATA_UNSUPPORTED(flags) \
108 ({ \
109 if ((flags).hwAcceleratorMode == \
110 aidl::android::hardware::audio::effect::Flags::HardwareAccelerator::TUNNEL || \
Shunkai Yaoef683a92024-04-18 04:38:17 +0000111 (flags).bypass || (flags).offloadIndication) { \
Shunkai Yao50e478b2024-03-14 01:54:58 +0000112 GTEST_SKIP() << "Skip data path for offload"; \
113 } \
Mikhail Naganov95f22772023-10-25 08:44:47 -0700114 })
jiabindd23b0e2023-12-11 19:10:05 +0000115
Shunkai Yao8016a0d2025-03-26 16:39:55 +0000116#define SKIP_TEST_IF_VERSION_UNSUPPORTED(effect, minVersion) \
117 ({ \
118 if (int version = getHalVersion(effect); version < (minVersion)) { \
119 GTEST_SKIP() << "Skipping for HAL version: " << version \
120 << ", minimal version: " << (minVersion) << "\n"; \
121 } \
122 })
123
jiabindd23b0e2023-12-11 19:10:05 +0000124// Test that the transaction status 'isOk' if it is a known transaction
125#define EXPECT_IS_OK_OR_UNKNOWN_TRANSACTION(ret) \
126 EXPECT_PRED_FORMAT1( \
127 ::android::hardware::audio::common::testing::detail::assertIsOkOrUnknownTransaction, \
128 ret)
129
130// Test that the transaction status is as expected if it is a known transaction
131#define EXPECT_STATUS_OR_UNKNOWN_TRANSACTION(expected, ret) \
132 EXPECT_PRED_FORMAT2( \
133 ::android::hardware::audio::common::testing::detail::assertResultOrUnknownTransaction, \
134 expected, ret)
Mikhail Naganov531d04c2025-02-04 16:42:03 -0800135
136namespace android::hardware::audio::common::testing::detail {
137
138template <typename>
139struct mf_traits {};
140template <class T, class U>
141struct mf_traits<U T::*> {
142 using member_type = U;
143};
144
145} // namespace android::hardware::audio::common::testing::detail
146
147namespace aidl::android::media::audio::common {
148
149template <typename P>
150std::enable_if_t<std::is_function_v<typename ::android::hardware::audio::common::testing::detail::
151 mf_traits<decltype(&P::toString)>::member_type>,
152 std::ostream&>
153operator<<(std::ostream& os, const P& p) {
154 return os << p.toString();
155}
156template <typename E>
157std::enable_if_t<std::is_enum_v<E>, std::ostream&> operator<<(std::ostream& os, const E& e) {
158 return os << toString(e);
159}
160
161} // namespace aidl::android::media::audio::common