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> |
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 | |
| 30 | inline ::testing::AssertionResult assertIsOk(const char* expr, const ::ndk::ScopedAStatus& status) { |
| 31 | if (status.isOk()) { |
| 32 | return ::testing::AssertionSuccess(); |
| 33 | } |
| 34 | return ::testing::AssertionFailure() |
| 35 | << "Expected the transaction \'" << expr << "\' to succeed\n" |
| 36 | << " but it has failed with: " << status; |
| 37 | } |
| 38 | |
| 39 | inline ::testing::AssertionResult assertResult(const char* exp_expr, const char* act_expr, |
| 40 | int32_t expected, |
| 41 | const ::ndk::ScopedAStatus& status) { |
| 42 | if (status.getExceptionCode() == expected) { |
| 43 | return ::testing::AssertionSuccess(); |
| 44 | } |
| 45 | return ::testing::AssertionFailure() |
| 46 | << "Expected the transaction \'" << act_expr << "\' to fail with " << exp_expr |
| 47 | << "\n but is has completed with: " << status; |
| 48 | } |
| 49 | |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 50 | template <typename T> |
| 51 | inline ::testing::AssertionResult assertResult(const char* exp_expr, const char* act_expr, |
| 52 | const std::initializer_list<T>& expected, |
| 53 | const ::ndk::ScopedAStatus& status) { |
| 54 | if (std::find(expected.begin(), expected.end(), status.getExceptionCode()) != expected.end()) { |
| 55 | return ::testing::AssertionSuccess(); |
| 56 | } |
| 57 | return ::testing::AssertionFailure() << "Expected the transaction \'" << act_expr |
| 58 | << "\' to complete with one of: " << exp_expr |
| 59 | << "\n which is: " << ::testing::PrintToString(expected) |
| 60 | << "\n but is has completed with: " << status; |
| 61 | } |
| 62 | |
Mikhail Naganov | 00bac4e | 2022-09-15 04:06:57 +0000 | [diff] [blame] | 63 | } // namespace detail |
| 64 | |
| 65 | } // namespace android::hardware::audio::common::testing |
| 66 | |
| 67 | // Test that the transaction status 'isOk' |
| 68 | #define ASSERT_IS_OK(ret) \ |
| 69 | ASSERT_PRED_FORMAT1(::android::hardware::audio::common::testing::detail::assertIsOk, ret) |
| 70 | #define EXPECT_IS_OK(ret) \ |
| 71 | EXPECT_PRED_FORMAT1(::android::hardware::audio::common::testing::detail::assertIsOk, ret) |
| 72 | |
| 73 | // Test that the transaction status is as expected. |
| 74 | #define ASSERT_STATUS(expected, ret) \ |
| 75 | ASSERT_PRED_FORMAT2(::android::hardware::audio::common::testing::detail::assertResult, \ |
| 76 | expected, ret) |
| 77 | #define EXPECT_STATUS(expected, ret) \ |
| 78 | EXPECT_PRED_FORMAT2(::android::hardware::audio::common::testing::detail::assertResult, \ |
| 79 | expected, ret) |