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
19 #include <algorithm>
20 #include <initializer_list>
21 #include <iostream>
22
23 #include <android/binder_auto_utils.h>
24 #include <gtest/gtest.h>
25
26 namespace android::hardware::audio::common::testing {
27
28 namespace detail {
29
assertIsOk(const char * expr,const::ndk::ScopedAStatus & status)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
assertResult(const char * exp_expr,const char * act_expr,int32_t expected,const::ndk::ScopedAStatus & status)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
50 template <typename T>
assertResult(const char * exp_expr,const char * act_expr,const std::initializer_list<T> & expected,const::ndk::ScopedAStatus & status)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
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)
80
81 #define SKIP_TEST_IF_DATA_UNSUPPORTED(flags) \
82 ({ \
83 if ((flags).hwAcceleratorMode == Flags::HardwareAccelerator::TUNNEL || (flags).bypass) { \
84 GTEST_SKIP() << "Skip data path for offload"; \
85 } \
86 })