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_2_UTILS_TEST_MOCK_DEVICE_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_TEST_MOCK_DEVICE_H
19
20 #include <android/hardware/neuralnetworks/1.2/IDevice.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <hidl/Status.h>
24
25 namespace android::hardware::neuralnetworks::V1_2::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 // Helper methods.
77 void simulateCrash();
78
79 private:
80 sp<hidl_death_recipient> mDeathRecipient;
81 };
82
create()83 inline sp<MockDevice> MockDevice::create() {
84 auto mockDevice = sp<MockDevice>::make();
85
86 // Setup default actions for each relevant call.
87 const auto ret = []() -> Return<bool> { return true; };
88
89 // Setup default actions for each relevant call.
90 ON_CALL(*mockDevice, linkToDeathRet()).WillByDefault(testing::Invoke(ret));
91
92 // These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
93 // uninteresting methods calls.
94 EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(testing::AnyNumber());
95
96 return mockDevice;
97 }
98
linkToDeath(const sp<hidl_death_recipient> & recipient,uint64_t)99 inline Return<bool> MockDevice::linkToDeath(const sp<hidl_death_recipient>& recipient,
100 uint64_t /*cookie*/) {
101 mDeathRecipient = recipient;
102 return linkToDeathRet();
103 }
104
simulateCrash()105 inline void MockDevice::simulateCrash() {
106 ASSERT_NE(nullptr, mDeathRecipient.get());
107
108 // Currently, the utils::Device will not use the `cookie` or `who` arguments, so we pass in 0
109 // and nullptr for these arguments instead. Normally, they are used by the hidl_death_recipient
110 // to determine which object is dead. However, the utils::Device code only pairs a single death
111 // recipient with a single HIDL interface object, so these arguments are redundant.
112 mDeathRecipient->serviceDied(0, nullptr);
113 }
114
115 } // namespace android::hardware::neuralnetworks::V1_2::utils
116
117 #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_TEST_MOCK_DEVICE_H
118