1 /*
2  * Copyright (c) 2022, 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 "MockVehicle.h"
20 
21 #include <android-base/stringprintf.h>
22 #include <android-base/strings.h>
23 #include <android/binder_interface_utils.h>
24 #include <gmock/gmock.h>
25 
26 #include <AidlVhalClient.h>
27 
28 namespace android {
29 namespace automotive {
30 namespace watchdog {
31 
toString(const std::vector<int32_t> & values)32 std::string toString(const std::vector<int32_t>& values) {
33     std::vector<std::string> strings;
34     for (int32_t value : values) {
35         strings.push_back(std::to_string(value));
36     }
37     return android::base::StringPrintf("[%s]", android::base::Join(strings, ",").c_str());
38 }
39 
40 class MockSubscriptionClient final :
41       public android::frameworks::automotive::vhal::ISubscriptionClient {
42 public:
MockSubscriptionClient(const std::shared_ptr<MockVehicle> & hal,const std::shared_ptr<android::frameworks::automotive::vhal::ISubscriptionCallback> & callback)43     MockSubscriptionClient(
44             const std::shared_ptr<MockVehicle>& hal,
45             const std::shared_ptr<android::frameworks::automotive::vhal::ISubscriptionCallback>&
46                     callback) {
47         mHal = hal;
48         mCallback = ndk::SharedRefBase::make<
49                 android::frameworks::automotive::vhal::SubscriptionVehicleCallback>(callback);
50     }
~MockSubscriptionClient()51     ~MockSubscriptionClient() {
52         mHal.reset();
53         mCallback.reset();
54     }
55 
56     MOCK_METHOD(
57             android::frameworks::automotive::vhal::VhalClientResult<void>, subscribe,
58             (const std::vector<aidl::android::hardware::automotive::vehicle::SubscribeOptions>&),
59             (override));
60 
unsubscribe(const std::vector<int32_t> & propIds)61     android::frameworks::automotive::vhal::VhalClientResult<void> unsubscribe(
62             const std::vector<int32_t>& propIds) override {
63         if (auto status = mHal->unsubscribe(mCallback, propIds); !status.isOk()) {
64             return android::frameworks::automotive::vhal::ClientStatusError(
65                            static_cast<aidl::android::hardware::automotive::vehicle::StatusCode>(
66                                    status.getServiceSpecificError()))
67                     << "failed to unsubscribe to prop IDs: " << toString(propIds)
68                     << ", error: " << status.getMessage();
69         }
70         return {};
71     }
72 
73 private:
74     std::shared_ptr<MockVehicle> mHal;
75     std::shared_ptr<android::frameworks::automotive::vhal::SubscriptionVehicleCallback> mCallback;
76 };
77 
78 }  // namespace watchdog
79 }  // namespace automotive
80 }  // namespace android
81