blob: 10b088c374bdcae4bc4671852f4f4bc1d695bb29 [file] [log] [blame]
Kevin Rocardf0357882017-02-10 16:19:28 -08001/*
2 * Copyright (C) 2017 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 */
Kevin Rocard20e7af62017-03-10 17:10:43 -080016
17#include <vector>
18#include <algorithm>
19
Kevin Rocardf0357882017-02-10 16:19:28 -080020#include <hidl/Status.h>
21
22namespace detail {
23
Kevin Rocard20e7af62017-03-10 17:10:43 -080024// This is a detail namespace, thus it is OK to import a class as nobody else is allowed to use it
25using ::android::hardware::Return;
26using ::android::hardware::audio::V2_0::Result;
27
28inline void assertResult(Result expected, Result result) {
29 ASSERT_EQ(expected, result);
30}
31
Kevin Rocard24db65c2017-03-10 18:47:37 -080032inline void assertResult(Result expected, const Return<Result> &ret) {
Kevin Rocard20e7af62017-03-10 17:10:43 -080033 ASSERT_TRUE(ret.isOk());
34 Result result = ret;
35 assertResult(expected, result);
36}
37
Kevin Rocard24db65c2017-03-10 18:47:37 -080038inline void assertResult(const std::vector<Result> &expected, Result result) {
Kevin Rocard20e7af62017-03-10 17:10:43 -080039 if (std::find(expected.begin(), expected.end(), result) != expected.end()) {
40 return; // result is in expected
41 }
42 FAIL() << "Expected result " << ::testing::PrintToString(result)
43 << " to be one of " << ::testing::PrintToString(expected);
44}
45
Kevin Rocard24db65c2017-03-10 18:47:37 -080046inline void assertResult(const std::vector<Result> &expected, const Return<Result> &ret) {
Kevin Rocard20e7af62017-03-10 17:10:43 -080047 ASSERT_TRUE(ret.isOk());
48 Result result = ret;
49 assertResult(expected, result);
50}
51
Kevin Rocard24db65c2017-03-10 18:47:37 -080052inline void assertOk(const Return<void> &ret) {
Kevin Rocardf0357882017-02-10 16:19:28 -080053 ASSERT_TRUE(ret.isOk());
54}
55
Kevin Rocard20e7af62017-03-10 17:10:43 -080056inline void assertOk(Result result) {
57 assertResult(Result::OK, result);
Kevin Rocardf0357882017-02-10 16:19:28 -080058}
59
Kevin Rocard24db65c2017-03-10 18:47:37 -080060inline void assertOk(const Return<Result> &ret) {
61 assertResult(Result::OK, ret);
Kevin Rocardf0357882017-02-10 16:19:28 -080062}
63
64}
65
66// Test anything provided is and contains only OK
67#define ASSERT_OK(ret) ASSERT_NO_FATAL_FAILURE(detail::assertOk(ret))
68#define EXPECT_OK(ret) EXPECT_NO_FATAL_FAILURE(detail::assertOk(ret))
Kevin Rocard3c405a72017-03-08 16:46:51 -080069
Kevin Rocard20e7af62017-03-10 17:10:43 -080070#define ASSERT_RESULT(expected, ret) ASSERT_NO_FATAL_FAILURE(detail::assertResult(expected, ret))
Kevin Rocard24db65c2017-03-10 18:47:37 -080071#define EXPECT_RESULT(expected, ret) EXPECT_NO_FATAL_FAILURE(detail::assertResult(expected, ret))