Mikhail Naganov | 00bac4e | 2022-09-15 04:06:57 +0000 | [diff] [blame] | 1 | /* |
| 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 Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <initializer_list> |
Mikhail Naganov | 00bac4e | 2022-09-15 04:06:57 +0000 | [diff] [blame] | 21 | |
| 22 | #include <android/binder_auto_utils.h> |
Krzysztof KosiĆski | a3a78a6 | 2023-03-04 00:58:52 +0000 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
Jaideep Sharma | 7449841 | 2023-09-13 15:25:25 +0530 | [diff] [blame] | 24 | #include <system/audio_aidl_utils.h> |
Mikhail Naganov | 00bac4e | 2022-09-15 04:06:57 +0000 | [diff] [blame] | 25 | |
Mikhail Naganov | 00bac4e | 2022-09-15 04:06:57 +0000 | [diff] [blame] | 26 | namespace android::hardware::audio::common::testing { |
| 27 | |
| 28 | namespace detail { |
| 29 | |
Jaideep Sharma | 7449841 | 2023-09-13 15:25:25 +0530 | [diff] [blame] | 30 | class 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 Naganov | 00bac4e | 2022-09-15 04:06:57 +0000 | [diff] [blame] | 39 | inline ::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 | |
| 48 | inline ::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 Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 59 | template <typename T> |
| 60 | inline ::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 | |
jiabin | bc79ff1 | 2023-12-11 19:10:05 +0000 | [diff] [blame] | 72 | inline ::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 | |
| 80 | inline ::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 Naganov | 00bac4e | 2022-09-15 04:06:57 +0000 | [diff] [blame] | 89 | } // 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 Sharma | cba4286 | 2023-06-23 10:27:39 +0530 | [diff] [blame] | 106 | |
Shunkai Yao | f0cb5ec | 2024-03-14 01:54:58 +0000 | [diff] [blame] | 107 | #define SKIP_TEST_IF_DATA_UNSUPPORTED(flags) \ |
| 108 | ({ \ |
| 109 | if ((flags).hwAcceleratorMode == \ |
| 110 | aidl::android::hardware::audio::effect::Flags::HardwareAccelerator::TUNNEL || \ |
| 111 | (flags).bypass) { \ |
| 112 | GTEST_SKIP() << "Skip data path for offload"; \ |
| 113 | } \ |
Mikhail Naganov | 95f2277 | 2023-10-25 08:44:47 -0700 | [diff] [blame] | 114 | }) |
jiabin | bc79ff1 | 2023-12-11 19:10:05 +0000 | [diff] [blame] | 115 | |
| 116 | // Test that the transaction status 'isOk' if it is a known transaction |
| 117 | #define EXPECT_IS_OK_OR_UNKNOWN_TRANSACTION(ret) \ |
| 118 | EXPECT_PRED_FORMAT1( \ |
| 119 | ::android::hardware::audio::common::testing::detail::assertIsOkOrUnknownTransaction, \ |
| 120 | ret) |
| 121 | |
| 122 | // Test that the transaction status is as expected if it is a known transaction |
| 123 | #define EXPECT_STATUS_OR_UNKNOWN_TRANSACTION(expected, ret) \ |
| 124 | EXPECT_PRED_FORMAT2( \ |
| 125 | ::android::hardware::audio::common::testing::detail::assertResultOrUnknownTransaction, \ |
| 126 | expected, ret) |