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
17 #ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_DEVICE_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_DEVICE_H
19
20 #include <android/hardware/neuralnetworks/1.3/IDevice.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <hidl/Status.h>
24
25 namespace android::hardware::neuralnetworks::V1_3::utils {
26
27 using CacheToken =
28 hidl_array<uint8_t, static_cast<uint32_t>(V1_2::Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
29
30 class MockDevice final : public IDevice {
31 public:
32 static sp<MockDevice> create();
33
34 // IBase methods below.
35 MOCK_METHOD(Return<void>, ping, (), (override));
36 MOCK_METHOD(Return<bool>, linkToDeathRet, ());
37 Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient, uint64_t /*cookie*/);
38
39 // V1_0 methods below.
40 MOCK_METHOD(Return<void>, getCapabilities, (getCapabilities_cb cb), (override));
41 MOCK_METHOD(Return<void>, getSupportedOperations,
42 (const V1_0::Model& model, getSupportedOperations_cb cb), (override));
43 MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel,
44 (const V1_0::Model& model, const sp<V1_0::IPreparedModelCallback>& callback),
45 (override));
46 MOCK_METHOD(Return<V1_0::DeviceStatus>, getStatus, (), (override));
47
48 // V1_1 methods below.
49 MOCK_METHOD(Return<void>, getCapabilities_1_1, (getCapabilities_1_1_cb cb), (override));
50 MOCK_METHOD(Return<void>, getSupportedOperations_1_1,
51 (const V1_1::Model& model, getSupportedOperations_1_1_cb cb), (override));
52 MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel_1_1,
53 (const V1_1::Model& model, V1_1::ExecutionPreference preference,
54 const sp<V1_0::IPreparedModelCallback>& callback),
55 (override));
56
57 // V1_2 methods below.
58 MOCK_METHOD(Return<void>, getVersionString, (getVersionString_cb cb), (override));
59 MOCK_METHOD(Return<void>, getType, (getType_cb cb), (override));
60 MOCK_METHOD(Return<void>, getCapabilities_1_2, (getCapabilities_1_2_cb cb), (override));
61 MOCK_METHOD(Return<void>, getSupportedExtensions, (getSupportedExtensions_cb cb), (override));
62 MOCK_METHOD(Return<void>, getSupportedOperations_1_2,
63 (const V1_2::Model& model, getSupportedOperations_1_2_cb cb), (override));
64 MOCK_METHOD(Return<void>, getNumberOfCacheFilesNeeded, (getNumberOfCacheFilesNeeded_cb cb),
65 (override));
66 MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel_1_2,
67 (const V1_2::Model& model, V1_1::ExecutionPreference preference,
68 const hidl_vec<hidl_handle>& modelCache, const hidl_vec<hidl_handle>& dataCache,
69 const CacheToken& token, const sp<V1_2::IPreparedModelCallback>& callback),
70 (override));
71 MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModelFromCache,
72 (const hidl_vec<hidl_handle>& modelCache, const hidl_vec<hidl_handle>& dataCache,
73 const CacheToken& token, const sp<V1_2::IPreparedModelCallback>& callback),
74 (override));
75
76 // V1_3 methods below.
77 MOCK_METHOD(Return<void>, getCapabilities_1_3, (getCapabilities_1_3_cb cb), (override));
78 MOCK_METHOD(Return<void>, getSupportedOperations_1_3,
79 (const V1_3::Model& model, getSupportedOperations_1_3_cb cb), (override));
80 MOCK_METHOD(Return<V1_3::ErrorStatus>, prepareModel_1_3,
81 (const V1_3::Model& model, V1_1::ExecutionPreference preference,
82 V1_3::Priority priority, const V1_3::OptionalTimePoint& deadline,
83 const hidl_vec<hidl_handle>& modelCache, const hidl_vec<hidl_handle>& dataCache,
84 const CacheToken& token, const sp<V1_3::IPreparedModelCallback>& callback),
85 (override));
86 MOCK_METHOD(Return<V1_3::ErrorStatus>, prepareModelFromCache_1_3,
87 (const V1_3::OptionalTimePoint& deadline, const hidl_vec<hidl_handle>& modelCache,
88 const hidl_vec<hidl_handle>& dataCache, const CacheToken& token,
89 const sp<V1_3::IPreparedModelCallback>& callback),
90 (override));
91 MOCK_METHOD(Return<void>, allocate,
92 (const V1_3::BufferDesc& desc,
93 const hidl_vec<sp<V1_3::IPreparedModel>>& preparedModels,
94 const hidl_vec<V1_3::BufferRole>& inputRoles,
95 const hidl_vec<V1_3::BufferRole>& outputRoles, allocate_cb cb),
96 (override));
97
98 // Helper methods.
99 void simulateCrash();
100
101 private:
102 sp<hidl_death_recipient> mDeathRecipient;
103 };
104
create()105 inline sp<MockDevice> MockDevice::create() {
106 auto mockDevice = sp<MockDevice>::make();
107
108 // Setup default actions for each relevant call.
109 const auto ret = []() -> Return<bool> { return true; };
110
111 // Setup default actions for each relevant call.
112 ON_CALL(*mockDevice, linkToDeathRet()).WillByDefault(testing::Invoke(ret));
113
114 // These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
115 // uninteresting methods calls.
116 EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(testing::AnyNumber());
117
118 return mockDevice;
119 }
120
linkToDeath(const sp<hidl_death_recipient> & recipient,uint64_t)121 inline Return<bool> MockDevice::linkToDeath(const sp<hidl_death_recipient>& recipient,
122 uint64_t /*cookie*/) {
123 mDeathRecipient = recipient;
124 return linkToDeathRet();
125 }
126
simulateCrash()127 inline void MockDevice::simulateCrash() {
128 ASSERT_NE(nullptr, mDeathRecipient.get());
129
130 // Currently, the utils::Device will not use the `cookie` or `who` arguments, so we pass in 0
131 // and nullptr for these arguments instead. Normally, they are used by the hidl_death_recipient
132 // to determine which object is dead. However, the utils::Device code only pairs a single death
133 // recipient with a single HIDL interface object, so these arguments are redundant.
134 mDeathRecipient->serviceDied(0, nullptr);
135 }
136
137 } // namespace android::hardware::neuralnetworks::V1_3::utils
138
139 #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_DEVICE_H
140