blob: 4c8440e18c7e86c7f7ccad0bc2f9de5fdba1ec1b [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
Kevin Rocardf26f67a2017-05-02 19:21:58 -070029template <class T>
30inline ::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();
Kevin Rocard20e7af62017-03-10 17:10:43 -080035}
36
Kevin Rocardf26f67a2017-05-02 19:21:58 -070037// Call continuation if the provided result isOk
38template <class T, class Continuation>
39inline ::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();
Kevin Rocard20e7af62017-03-10 17:10:43 -080044}
45
Kevin Rocardf26f67a2017-05-02 19:21:58 -070046// Expect two equal Results
47inline ::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
58inline ::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
68inline ::testing::AssertionResult assertResult(
69 const char* e_expr, const char* r_expr, const std::vector<Result>& expected,
70 Result result) {
Kevin Rocard20e7af62017-03-10 17:10:43 -080071 if (std::find(expected.begin(), expected.end(), result) != expected.end()) {
Kevin Rocardf26f67a2017-05-02 19:21:58 -070072 return ::testing::AssertionSuccess(); // result is in expected
Kevin Rocard20e7af62017-03-10 17:10:43 -080073 }
Kevin Rocardf26f67a2017-05-02 19:21:58 -070074 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);
Kevin Rocard20e7af62017-03-10 17:10:43 -080079}
80
Kevin Rocardf26f67a2017-05-02 19:21:58 -070081// Expect a Result wrapped in an OK Return to be part of a list of Results
82inline ::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 });
Kevin Rocard20e7af62017-03-10 17:10:43 -080088}
89
Kevin Rocardf26f67a2017-05-02 19:21:58 -070090inline ::testing::AssertionResult assertOk(const char* expr,
91 const Return<void>& ret) {
92 return assertIsOk(expr, ret);
Kevin Rocardf0357882017-02-10 16:19:28 -080093}
94
Kevin Rocardf26f67a2017-05-02 19:21:58 -070095inline ::testing::AssertionResult assertOk(const char* expr, Result result) {
96 return ::testing::AssertionResult(result == Result::OK)
97 << "Expected success: " << expr
98 << "\nActual: " << ::testing::PrintToString(result);
Kevin Rocardf0357882017-02-10 16:19:28 -080099}
100
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700101inline ::testing::AssertionResult assertOk(const char* expr,
102 const Return<Result>& ret) {
103 return continueIfIsOk(expr, ret,
104 [&] { return assertOk(expr, Result{ret}); });
Kevin Rocardf0357882017-02-10 16:19:28 -0800105}
Kevin Rocardf0357882017-02-10 16:19:28 -0800106}
107
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700108#define ASSERT_IS_OK(ret) ASSERT_PRED_FORMAT1(detail::assertIsOk, ret)
109#define EXPECT_IS_OK(ret) EXPECT_PRED_FORMAT1(detail::assertIsOk, ret)
110
Kevin Rocardf0357882017-02-10 16:19:28 -0800111// Test anything provided is and contains only OK
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700112#define ASSERT_OK(ret) ASSERT_PRED_FORMAT1(detail::assertOk, ret)
113#define EXPECT_OK(ret) EXPECT_PRED_FORMAT1(detail::assertOk, ret)
Kevin Rocard3c405a72017-03-08 16:46:51 -0800114
Kevin Rocard72e50e22017-05-05 14:02:55 -0700115#define ASSERT_RESULT(expected, ret) \
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700116 ASSERT_PRED_FORMAT2(detail::assertResult, expected, ret)
Kevin Rocard72e50e22017-05-05 14:02:55 -0700117#define EXPECT_RESULT(expected, ret) \
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700118 EXPECT_PRED_FORMAT2(detail::assertResult, expected, ret)