Add MockVehicleCallback for testing.
Test: None
Bug: 200737967
Change-Id: I2d34138a865be684720a1e0582cc9ea5a8a8ff04
diff --git a/automotive/vehicle/aidl/impl/vhal/test/MockVehicleCallback.cpp b/automotive/vehicle/aidl/impl/vhal/test/MockVehicleCallback.cpp
new file mode 100644
index 0000000..ca366cd
--- /dev/null
+++ b/automotive/vehicle/aidl/impl/vhal/test/MockVehicleCallback.cpp
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MockVehicleCallback.h"
+
+namespace android {
+namespace hardware {
+namespace automotive {
+namespace vehicle {
+
+namespace {
+
+using ::aidl::android::hardware::automotive::vehicle::GetValueResults;
+using ::aidl::android::hardware::automotive::vehicle::SetValueResults;
+using ::aidl::android::hardware::automotive::vehicle::VehiclePropErrors;
+using ::aidl::android::hardware::automotive::vehicle::VehiclePropValues;
+using ::ndk::ScopedAStatus;
+using ::ndk::ScopedFileDescriptor;
+
+template <class T>
+std::optional<T> pop(std::list<T>& items) {
+ if (items.size() > 0) {
+ auto item = std::move(items.front());
+ items.pop_front();
+ return item;
+ }
+ return std::nullopt;
+}
+
+template <class T>
+static ScopedAStatus storeResults(const T& results, std::list<T>* storedResults) {
+ T resultsCopy{
+ .payloads = results.payloads,
+ };
+ int fd = results.sharedMemoryFd.get();
+ if (fd != -1) {
+ resultsCopy.sharedMemoryFd = ScopedFileDescriptor(dup(fd));
+ }
+ storedResults->push_back(std::move(resultsCopy));
+ return ScopedAStatus::ok();
+}
+
+} // namespace
+
+ScopedAStatus MockVehicleCallback::onGetValues(const GetValueResults& results) {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+ return storeResults(results, &mGetValueResults);
+}
+
+ScopedAStatus MockVehicleCallback::onSetValues(const SetValueResults& results) {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+ return storeResults(results, &mSetValueResults);
+}
+
+ScopedAStatus MockVehicleCallback::onPropertyEvent(const VehiclePropValues&, int32_t) {
+ return ScopedAStatus::ok();
+}
+
+ScopedAStatus MockVehicleCallback::onPropertySetError(const VehiclePropErrors&) {
+ return ScopedAStatus::ok();
+}
+
+std::optional<GetValueResults> MockVehicleCallback::nextGetValueResults() {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+ return pop(mGetValueResults);
+}
+
+std::optional<SetValueResults> MockVehicleCallback::nextSetValueResults() {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+ return pop(mSetValueResults);
+}
+
+} // namespace vehicle
+} // namespace automotive
+} // namespace hardware
+} // namespace android
diff --git a/automotive/vehicle/aidl/impl/vhal/test/MockVehicleCallback.h b/automotive/vehicle/aidl/impl/vhal/test/MockVehicleCallback.h
new file mode 100644
index 0000000..916575a
--- /dev/null
+++ b/automotive/vehicle/aidl/impl/vhal/test/MockVehicleCallback.h
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef android_hardware_automotive_vehicle_aidl_impl_vhal_test_MockVehicleCallback_H_
+#define android_hardware_automotive_vehicle_aidl_impl_vhal_test_MockVehicleCallback_H_
+
+#include <VehicleHalTypes.h>
+
+#include <aidl/android/hardware/automotive/vehicle/BnVehicleCallback.h>
+#include <android-base/thread_annotations.h>
+
+#include <list>
+#include <mutex>
+#include <optional>
+
+namespace android {
+namespace hardware {
+namespace automotive {
+namespace vehicle {
+
+// MockVehicleCallback is a mock VehicleCallback implementation that simply stores the results.
+class MockVehicleCallback final
+ : public ::aidl::android::hardware::automotive::vehicle::BnVehicleCallback {
+ public:
+ ::ndk::ScopedAStatus onGetValues(
+ const ::aidl::android::hardware::automotive::vehicle::GetValueResults& results)
+ override;
+ ::ndk::ScopedAStatus onSetValues(
+ const ::aidl::android::hardware::automotive::vehicle::SetValueResults& results)
+ override;
+ ::ndk::ScopedAStatus onPropertyEvent(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValues&,
+ int32_t) override;
+ ::ndk::ScopedAStatus onPropertySetError(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropErrors&) override;
+
+ // Test functions
+ std::optional<::aidl::android::hardware::automotive::vehicle::GetValueResults>
+ nextGetValueResults();
+ std::optional<::aidl::android::hardware::automotive::vehicle::SetValueResults>
+ nextSetValueResults();
+
+ private:
+ std::mutex mLock;
+ std::list<::aidl::android::hardware::automotive::vehicle::GetValueResults> mGetValueResults
+ GUARDED_BY(mLock);
+ std::list<::aidl::android::hardware::automotive::vehicle::SetValueResults> mSetValueResults
+ GUARDED_BY(mLock);
+};
+
+} // namespace vehicle
+} // namespace automotive
+} // namespace hardware
+} // namespace android
+
+#endif // android_hardware_automotive_vehicle_aidl_impl_vhal_test_MockVehicleCallback_H_