blob: 191e98002ebb2f10309a0d22f04f1473c3653486 [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#include <iostream>
22
23#include <android/binder_auto_utils.h>
Krzysztof KosiƄskia3a78a62023-03-04 00:58:52 +000024#include <gtest/gtest.h>
Jaideep Sharma74498412023-09-13 15:25:25 +053025#include <system/audio_aidl_utils.h>
Mikhail Naganov00bac4e2022-09-15 04:06:57 +000026
Mikhail Naganov00bac4e2022-09-15 04:06:57 +000027namespace android::hardware::audio::common::testing {
28
29namespace detail {
30
Jaideep Sharma74498412023-09-13 15:25:25 +053031class TestExecutionTracer : public ::testing::EmptyTestEventListener {
32 public:
33 void OnTestStart(const ::testing::TestInfo& test_info) override;
34 void OnTestEnd(const ::testing::TestInfo& test_info) override;
35 void OnTestPartResult(const ::testing::TestPartResult& result) override;
36 private:
37 static void TraceTestState(const std::string& state, const ::testing::TestInfo& test_info);
38};
39
Mikhail Naganov00bac4e2022-09-15 04:06:57 +000040inline ::testing::AssertionResult assertIsOk(const char* expr, const ::ndk::ScopedAStatus& status) {
41 if (status.isOk()) {
42 return ::testing::AssertionSuccess();
43 }
44 return ::testing::AssertionFailure()
45 << "Expected the transaction \'" << expr << "\' to succeed\n"
46 << " but it has failed with: " << status;
47}
48
49inline ::testing::AssertionResult assertResult(const char* exp_expr, const char* act_expr,
50 int32_t expected,
51 const ::ndk::ScopedAStatus& status) {
52 if (status.getExceptionCode() == expected) {
53 return ::testing::AssertionSuccess();
54 }
55 return ::testing::AssertionFailure()
56 << "Expected the transaction \'" << act_expr << "\' to fail with " << exp_expr
57 << "\n but is has completed with: " << status;
58}
59
Mikhail Naganove9f10fc2022-10-14 23:31:52 +000060template <typename T>
61inline ::testing::AssertionResult assertResult(const char* exp_expr, const char* act_expr,
62 const std::initializer_list<T>& expected,
63 const ::ndk::ScopedAStatus& status) {
64 if (std::find(expected.begin(), expected.end(), status.getExceptionCode()) != expected.end()) {
65 return ::testing::AssertionSuccess();
66 }
67 return ::testing::AssertionFailure() << "Expected the transaction \'" << act_expr
68 << "\' to complete with one of: " << exp_expr
69 << "\n which is: " << ::testing::PrintToString(expected)
70 << "\n but is has completed with: " << status;
71}
72
Mikhail Naganov00bac4e2022-09-15 04:06:57 +000073} // namespace detail
74
75} // namespace android::hardware::audio::common::testing
76
77// Test that the transaction status 'isOk'
78#define ASSERT_IS_OK(ret) \
79 ASSERT_PRED_FORMAT1(::android::hardware::audio::common::testing::detail::assertIsOk, ret)
80#define EXPECT_IS_OK(ret) \
81 EXPECT_PRED_FORMAT1(::android::hardware::audio::common::testing::detail::assertIsOk, ret)
82
83// Test that the transaction status is as expected.
84#define ASSERT_STATUS(expected, ret) \
85 ASSERT_PRED_FORMAT2(::android::hardware::audio::common::testing::detail::assertResult, \
86 expected, ret)
87#define EXPECT_STATUS(expected, ret) \
88 EXPECT_PRED_FORMAT2(::android::hardware::audio::common::testing::detail::assertResult, \
89 expected, ret)
Jaideep Sharmacba42862023-06-23 10:27:39 +053090
Shunkai Yao8771cec2023-09-20 22:46:59 +000091#define SKIP_TEST_IF_DATA_UNSUPPORTED(flags) \
92 ({ \
93 if ((flags).hwAcceleratorMode == Flags::HardwareAccelerator::TUNNEL || (flags).bypass) { \
94 GTEST_SKIP() << "Skip data path for offload"; \
95 } \
Jaideep Sharmacba42862023-06-23 10:27:39 +053096 })