blob: 2fc109a14b576832fc451778ce2423970d6c0ec7 [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
19#include <iostream>
20
21#include <android/binder_auto_utils.h>
22#include <gtest/gtest_pred_impl.h>
23
24namespace ndk {
25
26std::ostream& operator<<(std::ostream& str, const ScopedAStatus& status) {
27 str << status.getDescription();
28 return str;
29}
30
31} // namespace ndk
32
33namespace android::hardware::audio::common::testing {
34
35namespace detail {
36
37inline ::testing::AssertionResult assertIsOk(const char* expr, const ::ndk::ScopedAStatus& status) {
38 if (status.isOk()) {
39 return ::testing::AssertionSuccess();
40 }
41 return ::testing::AssertionFailure()
42 << "Expected the transaction \'" << expr << "\' to succeed\n"
43 << " but it has failed with: " << status;
44}
45
46inline ::testing::AssertionResult assertResult(const char* exp_expr, const char* act_expr,
47 int32_t expected,
48 const ::ndk::ScopedAStatus& status) {
49 if (status.getExceptionCode() == expected) {
50 return ::testing::AssertionSuccess();
51 }
52 return ::testing::AssertionFailure()
53 << "Expected the transaction \'" << act_expr << "\' to fail with " << exp_expr
54 << "\n but is has completed with: " << status;
55}
56
57} // namespace detail
58
59} // namespace android::hardware::audio::common::testing
60
61// Test that the transaction status 'isOk'
62#define ASSERT_IS_OK(ret) \
63 ASSERT_PRED_FORMAT1(::android::hardware::audio::common::testing::detail::assertIsOk, ret)
64#define EXPECT_IS_OK(ret) \
65 EXPECT_PRED_FORMAT1(::android::hardware::audio::common::testing::detail::assertIsOk, ret)
66
67// Test that the transaction status is as expected.
68#define ASSERT_STATUS(expected, ret) \
69 ASSERT_PRED_FORMAT2(::android::hardware::audio::common::testing::detail::assertResult, \
70 expected, ret)
71#define EXPECT_STATUS(expected, ret) \
72 EXPECT_PRED_FORMAT2(::android::hardware::audio::common::testing::detail::assertResult, \
73 expected, ret)