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_0_UTILS_TEST_MOCK_DEVICE_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_TEST_MOCK_DEVICE_H
19
20 #include <android/hardware/neuralnetworks/1.0/IDevice.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <hidl/Status.h>
24
25 namespace android::hardware::neuralnetworks::V1_0::utils {
26
27 class MockDevice final : public IDevice {
28 public:
29 static sp<MockDevice> create();
30
31 // IBase methods below.
32 MOCK_METHOD(Return<void>, ping, (), (override));
33 MOCK_METHOD(Return<bool>, linkToDeathRet, ());
34 Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient, uint64_t /*cookie*/);
35
36 // V1_0 methods below.
37 MOCK_METHOD(Return<void>, getCapabilities, (getCapabilities_cb cb), (override));
38 MOCK_METHOD(Return<void>, getSupportedOperations,
39 (const V1_0::Model& model, getSupportedOperations_cb cb), (override));
40 MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel,
41 (const V1_0::Model& model, const sp<V1_0::IPreparedModelCallback>& callback),
42 (override));
43 MOCK_METHOD(Return<V1_0::DeviceStatus>, getStatus, (), (override));
44
45 // Helper methods.
46 void simulateCrash();
47
48 private:
49 sp<hidl_death_recipient> mDeathRecipient;
50 };
51
create()52 inline sp<MockDevice> MockDevice::create() {
53 auto mockDevice = sp<MockDevice>::make();
54
55 // Setup default actions for each relevant call.
56 const auto ret = []() -> Return<bool> { return true; };
57
58 // Setup default actions for each relevant call.
59 ON_CALL(*mockDevice, linkToDeathRet()).WillByDefault(testing::Invoke(ret));
60
61 // These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
62 // uninteresting methods calls.
63 EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(testing::AnyNumber());
64
65 return mockDevice;
66 }
67
linkToDeath(const sp<hidl_death_recipient> & recipient,uint64_t)68 inline Return<bool> MockDevice::linkToDeath(const sp<hidl_death_recipient>& recipient,
69 uint64_t /*cookie*/) {
70 mDeathRecipient = recipient;
71 return linkToDeathRet();
72 }
73
simulateCrash()74 inline void MockDevice::simulateCrash() {
75 ASSERT_NE(nullptr, mDeathRecipient.get());
76
77 // Currently, the utils::Device will not use the `cookie` or `who` arguments, so we pass in 0
78 // and nullptr for these arguments instead. Normally, they are used by the hidl_death_recipient
79 // to determine which object is dead. However, the utils::Device code only pairs a single death
80 // recipient with a single HIDL interface object, so these arguments are redundant.
81 mDeathRecipient->serviceDied(0, nullptr);
82 }
83
84 } // namespace android::hardware::neuralnetworks::V1_0::utils
85
86 #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_TEST_MOCK_DEVICE_H
87