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 */ 16 17 #include <algorithm> 18 #include <vector> 19 20 #include <hidl/Status.h> 21 22 namespace detail { 23 24 // This is a detail namespace, thus it is OK to import a class as nobody else is 25 // allowed to use it 26 using ::android::hardware::Return; 27 using ::android::hardware::audio::V2_0::Result; 28 29 template <class T> 30 inline ::testing::AssertionResult assertIsOk(const char* expr, 31 const Return<T>& ret) { 32 return ::testing::AssertionResult(ret.isOk()) 33 << "Expected: " << expr 34 << "\n to be an OK Return but it is not: " << ret.description(); 35 } 36 37 // Call continuation if the provided result isOk 38 template <class T, class Continuation> 39 inline ::testing::AssertionResult continueIfIsOk(const char* expr, 40 const Return<T>& ret, 41 Continuation continuation) { 42 auto isOkStatus = assertIsOk(expr, ret); 43 return !isOkStatus ? isOkStatus : continuation(); 44 } 45 46 // Expect two equal Results 47 inline ::testing::AssertionResult assertResult(const char* e_expr, 48 const char* r_expr, 49 Result expected, Result result) { 50 return ::testing::AssertionResult(expected == result) 51 << "Value of: " << r_expr 52 << "\n Actual: " << ::testing::PrintToString(result) 53 << "\nExpected: " << e_expr 54 << "\nWhich is: " << ::testing::PrintToString(expected); 55 } 56 57 // Expect two equal Results one being wrapped in an OK Return 58 inline ::testing::AssertionResult assertResult(const char* e_expr, 59 const char* r_expr, 60 Result expected, 61 const Return<Result>& ret) { 62 return continueIfIsOk(r_expr, ret, [&] { 63 return assertResult(e_expr, r_expr, expected, Result{ret}); 64 }); 65 } 66 67 // Expect a Result to be part of a list of Results 68 inline ::testing::AssertionResult assertResult( 69 const char* e_expr, const char* r_expr, const std::vector<Result>& expected, 70 Result result) { 71 if (std::find(expected.begin(), expected.end(), result) != expected.end()) { 72 return ::testing::AssertionSuccess(); // result is in expected 73 } 74 return ::testing::AssertionFailure() 75 << "Value of: " << r_expr 76 << "\n Actual: " << ::testing::PrintToString(result) 77 << "\nExpected one of: " << e_expr 78 << "\n Which is: " << ::testing::PrintToString(expected); 79 } 80 81 // Expect a Result wrapped in an OK Return to be part of a list of Results 82 inline ::testing::AssertionResult assertResult( 83 const char* e_expr, const char* r_expr, const std::vector<Result>& expected, 84 const Return<Result>& ret) { 85 return continueIfIsOk(r_expr, ret, [&] { 86 return assertResult(e_expr, r_expr, expected, Result{ret}); 87 }); 88 } 89 90 inline ::testing::AssertionResult assertOk(const char* expr, 91 const Return<void>& ret) { 92 return assertIsOk(expr, ret); 93 } 94 95 inline ::testing::AssertionResult assertOk(const char* expr, Result result) { 96 return ::testing::AssertionResult(result == Result::OK) 97 << "Expected success: " << expr 98 << "\nActual: " << ::testing::PrintToString(result); 99 } 100 101 inline ::testing::AssertionResult assertOk(const char* expr, 102 const Return<Result>& ret) { 103 return continueIfIsOk(expr, ret, 104 [&] { return assertOk(expr, Result{ret}); }); 105 } 106 } 107 108 #define ASSERT_IS_OK(ret) ASSERT_PRED_FORMAT1(detail::assertIsOk, ret) 109 #define EXPECT_IS_OK(ret) EXPECT_PRED_FORMAT1(detail::assertIsOk, ret) 110 111 // Test anything provided is and contains only OK 112 #define ASSERT_OK(ret) ASSERT_PRED_FORMAT1(detail::assertOk, ret) 113 #define EXPECT_OK(ret) EXPECT_PRED_FORMAT1(detail::assertOk, ret) 114 115 #define ASSERT_RESULT(expected, ret) \ 116 ASSERT_PRED_FORMAT2(detail::assertResult, expected, ret) 117 #define EXPECT_RESULT(expected, ret) \ 118 EXPECT_PRED_FORMAT2(detail::assertResult, expected, ret) 119