blob: 515b8a2a2f895da77d77dde6e206a9b2b711d96d [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 Yao8771cec2023-09-20 22:46:59 +0000107#define SKIP_TEST_IF_DATA_UNSUPPORTED(flags) \
108 ({ \
109 if ((flags).hwAcceleratorMode == Flags::HardwareAccelerator::TUNNEL || (flags).bypass) { \
110 GTEST_SKIP() << "Skip data path for offload"; \
111 } \
Mikhail Naganov95f22772023-10-25 08:44:47 -0700112 })
jiabindd23b0e2023-12-11 19:10:05 +0000113
114// Test that the transaction status 'isOk' if it is a known transaction
115#define EXPECT_IS_OK_OR_UNKNOWN_TRANSACTION(ret) \
116 EXPECT_PRED_FORMAT1( \
117 ::android::hardware::audio::common::testing::detail::assertIsOkOrUnknownTransaction, \
118 ret)
119
120// Test that the transaction status is as expected if it is a known transaction
121#define EXPECT_STATUS_OR_UNKNOWN_TRANSACTION(expected, ret) \
122 EXPECT_PRED_FORMAT2( \
123 ::android::hardware::audio::common::testing::detail::assertResultOrUnknownTransaction, \
124 expected, ret)