blob: 6891dc4af803d03c86a5329af42b9b018c204fd0 [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
Kevin Rocard20e7af62017-03-10 17:10:43 -080017#include <algorithm>
Kevin Rocard72e50e22017-05-05 14:02:55 -070018#include <vector>
Kevin Rocard20e7af62017-03-10 17:10:43 -080019
Kevin Rocardf0357882017-02-10 16:19:28 -080020#include <hidl/Status.h>
21
22namespace detail {
23
Kevin Rocard72e50e22017-05-05 14:02:55 -070024// This is a detail namespace, thus it is OK to import a class as nobody else is
25// allowed to use it
Kevin Rocard20e7af62017-03-10 17:10:43 -080026using ::android::hardware::Return;
27using ::android::hardware::audio::V2_0::Result;
28
29inline void assertResult(Result expected, Result result) {
30 ASSERT_EQ(expected, result);
31}
32
Kevin Rocard72e50e22017-05-05 14:02:55 -070033inline void assertResult(Result expected, const Return<Result>& ret) {
Kevin Rocard20e7af62017-03-10 17:10:43 -080034 ASSERT_TRUE(ret.isOk());
35 Result result = ret;
36 assertResult(expected, result);
37}
38
Kevin Rocard72e50e22017-05-05 14:02:55 -070039inline void assertResult(const std::vector<Result>& expected, Result result) {
Kevin Rocard20e7af62017-03-10 17:10:43 -080040 if (std::find(expected.begin(), expected.end(), result) != expected.end()) {
Kevin Rocard72e50e22017-05-05 14:02:55 -070041 return; // result is in expected
Kevin Rocard20e7af62017-03-10 17:10:43 -080042 }
43 FAIL() << "Expected result " << ::testing::PrintToString(result)
44 << " to be one of " << ::testing::PrintToString(expected);
45}
46
Kevin Rocard72e50e22017-05-05 14:02:55 -070047inline void assertResult(const std::vector<Result>& expected,
48 const Return<Result>& ret) {
Kevin Rocard20e7af62017-03-10 17:10:43 -080049 ASSERT_TRUE(ret.isOk());
50 Result result = ret;
51 assertResult(expected, result);
52}
53
Kevin Rocard72e50e22017-05-05 14:02:55 -070054inline void assertOk(const Return<void>& ret) {
Kevin Rocardf0357882017-02-10 16:19:28 -080055 ASSERT_TRUE(ret.isOk());
56}
57
Kevin Rocard20e7af62017-03-10 17:10:43 -080058inline void assertOk(Result result) {
59 assertResult(Result::OK, result);
Kevin Rocardf0357882017-02-10 16:19:28 -080060}
61
Kevin Rocard72e50e22017-05-05 14:02:55 -070062inline void assertOk(const Return<Result>& ret) {
Kevin Rocard24db65c2017-03-10 18:47:37 -080063 assertResult(Result::OK, ret);
Kevin Rocardf0357882017-02-10 16:19:28 -080064}
Kevin Rocardf0357882017-02-10 16:19:28 -080065}
66
67// Test anything provided is and contains only OK
68#define ASSERT_OK(ret) ASSERT_NO_FATAL_FAILURE(detail::assertOk(ret))
69#define EXPECT_OK(ret) EXPECT_NO_FATAL_FAILURE(detail::assertOk(ret))
Kevin Rocard3c405a72017-03-08 16:46:51 -080070
Kevin Rocard72e50e22017-05-05 14:02:55 -070071#define ASSERT_RESULT(expected, ret) \
72 ASSERT_NO_FATAL_FAILURE(detail::assertResult(expected, ret))
73#define EXPECT_RESULT(expected, ret) \
74 EXPECT_NO_FATAL_FAILURE(detail::assertResult(expected, ret))