Michael Butler | 6e492a6 | 2020-12-10 15:38:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
Michael Butler | 8414a6e | 2021-03-10 18:41:05 -0800 | [diff] [blame] | 17 | #ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_TEST_MOCK_PREPARED_MODEL_H |
| 18 | #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_TEST_MOCK_PREPARED_MODEL_H |
Michael Butler | 6e492a6 | 2020-12-10 15:38:45 -0800 | [diff] [blame] | 19 | |
| 20 | #include <android/hardware/neuralnetworks/1.2/IPreparedModel.h> |
| 21 | #include <gmock/gmock.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | #include <hidl/HidlSupport.h> |
| 24 | #include <hidl/Status.h> |
| 25 | |
| 26 | namespace android::hardware::neuralnetworks::V1_2::utils { |
| 27 | |
| 28 | class MockPreparedModel final : public IPreparedModel { |
| 29 | public: |
| 30 | static sp<MockPreparedModel> create(); |
| 31 | |
| 32 | // IBase methods below. |
| 33 | MOCK_METHOD(Return<void>, ping, (), (override)); |
| 34 | MOCK_METHOD(Return<bool>, linkToDeathRet, ()); |
| 35 | Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient, |
| 36 | uint64_t /*cookie*/) override; |
| 37 | |
| 38 | // V1_0 methods below. |
| 39 | MOCK_METHOD(Return<V1_0::ErrorStatus>, execute, |
| 40 | (const V1_0::Request& request, const sp<V1_0::IExecutionCallback>& callback), |
| 41 | (override)); |
| 42 | |
| 43 | // V1_2 methods below. |
| 44 | MOCK_METHOD(Return<V1_0::ErrorStatus>, execute_1_2, |
| 45 | (const V1_0::Request& request, V1_2::MeasureTiming measure, |
| 46 | const sp<V1_2::IExecutionCallback>& callback), |
| 47 | (override)); |
| 48 | MOCK_METHOD(Return<void>, executeSynchronously, |
| 49 | (const V1_0::Request& request, V1_2::MeasureTiming measure, |
| 50 | executeSynchronously_cb cb), |
| 51 | (override)); |
| 52 | MOCK_METHOD(Return<void>, configureExecutionBurst, |
| 53 | (const sp<V1_2::IBurstCallback>& callback, |
| 54 | const MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel, |
| 55 | const MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel, |
| 56 | configureExecutionBurst_cb cb), |
| 57 | (override)); |
| 58 | |
| 59 | // Helper methods. |
| 60 | void simulateCrash(); |
| 61 | |
| 62 | private: |
| 63 | sp<hidl_death_recipient> mDeathRecipient; |
| 64 | }; |
| 65 | |
| 66 | inline sp<MockPreparedModel> MockPreparedModel::create() { |
| 67 | auto mockPreparedModel = sp<MockPreparedModel>::make(); |
| 68 | |
| 69 | // Setup default actions for each relevant call. |
| 70 | const auto ret = []() -> Return<bool> { return true; }; |
| 71 | |
| 72 | // Setup default actions for each relevant call. |
| 73 | ON_CALL(*mockPreparedModel, linkToDeathRet()).WillByDefault(testing::Invoke(ret)); |
| 74 | |
| 75 | // These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the |
| 76 | // uninteresting methods calls. |
| 77 | EXPECT_CALL(*mockPreparedModel, linkToDeathRet()).Times(testing::AnyNumber()); |
| 78 | |
| 79 | return mockPreparedModel; |
| 80 | } |
| 81 | |
| 82 | inline Return<bool> MockPreparedModel::linkToDeath(const sp<hidl_death_recipient>& recipient, |
| 83 | uint64_t /*cookie*/) { |
| 84 | mDeathRecipient = recipient; |
| 85 | return linkToDeathRet(); |
| 86 | } |
| 87 | |
| 88 | inline void MockPreparedModel::simulateCrash() { |
| 89 | ASSERT_NE(nullptr, mDeathRecipient.get()); |
| 90 | |
| 91 | // Currently, the utils::PreparedModel will not use the `cookie` or `who` arguments, so we pass |
| 92 | // in 0 and nullptr for these arguments instead. Normally, they are used by the |
| 93 | // hidl_death_recipient to determine which object is dead. However, the utils::PreparedModel |
| 94 | // code only pairs a single death recipient with a single HIDL interface object, so these |
| 95 | // arguments are redundant. |
| 96 | mDeathRecipient->serviceDied(0, nullptr); |
| 97 | } |
| 98 | |
| 99 | } // namespace android::hardware::neuralnetworks::V1_2::utils |
| 100 | |
Michael Butler | 8414a6e | 2021-03-10 18:41:05 -0800 | [diff] [blame] | 101 | #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_TEST_MOCK_PREPARED_MODEL_H |