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 | #include <iostream> |
| 22 | |
| 23 | #include <android/binder_auto_utils.h> |
Krzysztof KosiĆski | a3a78a6 | 2023-03-04 00:58:52 +0000 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
Jaideep Sharma | 7449841 | 2023-09-13 15:25:25 +0530 | [diff] [blame] | 25 | #include <system/audio_aidl_utils.h> |
Mikhail Naganov | 00bac4e | 2022-09-15 04:06:57 +0000 | [diff] [blame] | 26 | |
Mikhail Naganov | 00bac4e | 2022-09-15 04:06:57 +0000 | [diff] [blame] | 27 | namespace android::hardware::audio::common::testing { |
| 28 | |
| 29 | namespace detail { |
| 30 | |
Jaideep Sharma | 7449841 | 2023-09-13 15:25:25 +0530 | [diff] [blame] | 31 | class 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 Naganov | 00bac4e | 2022-09-15 04:06:57 +0000 | [diff] [blame] | 40 | inline ::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 | |
| 49 | inline ::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 Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 60 | template <typename T> |
| 61 | inline ::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 Naganov | 00bac4e | 2022-09-15 04:06:57 +0000 | [diff] [blame] | 73 | } // 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 Sharma | cba4286 | 2023-06-23 10:27:39 +0530 | [diff] [blame] | 90 | |
Shunkai Yao | 8771cec | 2023-09-20 22:46:59 +0000 | [diff] [blame] | 91 | #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 Sharma | cba4286 | 2023-06-23 10:27:39 +0530 | [diff] [blame] | 96 | }) |