Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 1 | /* |
| 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 Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 16 | |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 17 | #include <algorithm> |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 18 | #include <vector> |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 19 | |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 20 | #include <hidl/Status.h> |
| 21 | |
| 22 | namespace detail { |
| 23 | |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 24 | // This is a detail namespace, thus it is OK to import a class as nobody else is |
| 25 | // allowed to use it |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 26 | using ::android::hardware::Return; |
| 27 | using ::android::hardware::audio::V2_0::Result; |
| 28 | |
| 29 | inline void assertResult(Result expected, Result result) { |
| 30 | ASSERT_EQ(expected, result); |
| 31 | } |
| 32 | |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 33 | inline void assertResult(Result expected, const Return<Result>& ret) { |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 34 | ASSERT_TRUE(ret.isOk()); |
| 35 | Result result = ret; |
| 36 | assertResult(expected, result); |
| 37 | } |
| 38 | |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 39 | inline void assertResult(const std::vector<Result>& expected, Result result) { |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 40 | if (std::find(expected.begin(), expected.end(), result) != expected.end()) { |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 41 | return; // result is in expected |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 42 | } |
| 43 | FAIL() << "Expected result " << ::testing::PrintToString(result) |
| 44 | << " to be one of " << ::testing::PrintToString(expected); |
| 45 | } |
| 46 | |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 47 | inline void assertResult(const std::vector<Result>& expected, |
| 48 | const Return<Result>& ret) { |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 49 | ASSERT_TRUE(ret.isOk()); |
| 50 | Result result = ret; |
| 51 | assertResult(expected, result); |
| 52 | } |
| 53 | |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 54 | inline void assertOk(const Return<void>& ret) { |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 55 | ASSERT_TRUE(ret.isOk()); |
| 56 | } |
| 57 | |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 58 | inline void assertOk(Result result) { |
| 59 | assertResult(Result::OK, result); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 62 | inline void assertOk(const Return<Result>& ret) { |
Kevin Rocard | 24db65c | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 63 | assertResult(Result::OK, ret); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 64 | } |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 65 | } |
| 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 Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 70 | |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 71 | #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)) |