blob: 72ca56f228645e5a4b90ac17e3bd896e8a9c6c58 [file] [log] [blame]
Mikhail Naganov00bac4e2022-09-15 04:06:57 +00001/*
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 Naganove9f10fc2022-10-14 23:31:52 +000019#include <algorithm>
20#include <initializer_list>
Mikhail Naganov00bac4e2022-09-15 04:06:57 +000021#include <iostream>
22
23#include <android/binder_auto_utils.h>
Krzysztof KosiƄskia3a78a62023-03-04 00:58:52 +000024#include <gtest/gtest.h>
Mikhail Naganov00bac4e2022-09-15 04:06:57 +000025
Mikhail Naganov00bac4e2022-09-15 04:06:57 +000026namespace android::hardware::audio::common::testing {
27
28namespace detail {
29
30inline ::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
39inline ::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 Naganove9f10fc2022-10-14 23:31:52 +000050template <typename T>
51inline ::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 Naganov00bac4e2022-09-15 04:06:57 +000063} // 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)