• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #include <android/binder_auto_utils.h>
23 #include <gtest/gtest.h>
24 #include <system/audio_aidl_utils.h>
25 
26 namespace android::hardware::audio::common::testing {
27 
28 namespace detail {
29 
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 
assertIsOk(const char * expr,const::ndk::ScopedAStatus & status)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 
assertResult(const char * exp_expr,const char * act_expr,int32_t expected,const::ndk::ScopedAStatus & status)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 
59 template <typename T>
assertResult(const char * exp_expr,const char * act_expr,const std::initializer_list<T> & expected,const::ndk::ScopedAStatus & status)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 
assertIsOkOrUnknownTransaction(const char * expr,const::ndk::ScopedAStatus & status)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 
assertResultOrUnknownTransaction(const char * exp_expr,const char * act_expr,int32_t expected,const::ndk::ScopedAStatus & status)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 
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)
106 
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 || (flags).offloadIndication) {                                        \
112             GTEST_SKIP() << "Skip data path for offload";                                         \
113         }                                                                                         \
114     })
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)
127