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