1 /*
2  * Copyright (C) 2024 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 #pragma once
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <ostream>
23 
24 #include <binder/Status.h>
25 
26 namespace android::error {
27 
28 class BinderStatusMatcher {
29   public:
30     using is_gtest_matcher = void;
31 
BinderStatusMatcher(binder::Status status)32     explicit BinderStatusMatcher(binder::Status status) : status_(std::move(status)) {}
33 
hasException(binder::Status::Exception ex)34     static BinderStatusMatcher hasException(binder::Status::Exception ex) {
35         return BinderStatusMatcher(binder::Status::fromExceptionCode(ex));
36     }
37 
isOk()38     static BinderStatusMatcher isOk() { return BinderStatusMatcher(binder::Status::ok()); }
39 
MatchAndExplain(const binder::Status & value,::testing::MatchResultListener * listener)40     bool MatchAndExplain(const binder::Status& value,
41                          ::testing::MatchResultListener* listener) const {
42         if (status_.exceptionCode() == value.exceptionCode() &&
43             status_.transactionError() == value.transactionError() &&
44             status_.serviceSpecificErrorCode() == value.serviceSpecificErrorCode()) {
45             return true;
46         }
47         *listener << "received binder status: " << value;
48         return false;
49     }
50 
DescribeTo(std::ostream * os)51     void DescribeTo(std::ostream* os) const { *os << "contains binder status " << status_; }
52 
DescribeNegationTo(std::ostream * os)53     void DescribeNegationTo(std::ostream* os) const {
54         *os << "does not contain binder status " << status_;
55     }
56 
57   private:
58     const binder::Status status_;
59 };
60 }  // namespace android::error
61