blob: db7392d2cbbf124ddbb94577d7747caacfa702a0 [file] [log] [blame]
Michael Butlerafc4d7c2020-12-10 15:38:45 -08001/*
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 Butlerbbbdb0d2021-03-10 18:41:05 -080017#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_1_UTILS_TEST_MOCK_DEVICE_H
18#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_1_UTILS_TEST_MOCK_DEVICE_H
Michael Butlerafc4d7c2020-12-10 15:38:45 -080019
20#include <android/hardware/neuralnetworks/1.1/IDevice.h>
21#include <gmock/gmock.h>
22#include <gtest/gtest.h>
23#include <hidl/Status.h>
24
25namespace android::hardware::neuralnetworks::V1_1::utils {
26
27class 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 // V1_1 methods below.
46 MOCK_METHOD(Return<void>, getCapabilities_1_1, (getCapabilities_1_1_cb cb), (override));
47 MOCK_METHOD(Return<void>, getSupportedOperations_1_1,
48 (const V1_1::Model& model, getSupportedOperations_1_1_cb cb), (override));
49 MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel_1_1,
50 (const V1_1::Model& model, V1_1::ExecutionPreference preference,
51 const sp<V1_0::IPreparedModelCallback>& callback),
52 (override));
53
54 // Helper methods.
55 void simulateCrash();
56
57 private:
58 sp<hidl_death_recipient> mDeathRecipient;
59};
60
61inline sp<MockDevice> MockDevice::create() {
62 auto mockDevice = sp<MockDevice>::make();
63
64 // Setup default actions for each relevant call.
65 const auto ret = []() -> Return<bool> { return true; };
66
67 // Setup default actions for each relevant call.
68 ON_CALL(*mockDevice, linkToDeathRet()).WillByDefault(testing::Invoke(ret));
69
70 // These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
71 // uninteresting methods calls.
72 EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(testing::AnyNumber());
73
74 return mockDevice;
75}
76
77inline Return<bool> MockDevice::linkToDeath(const sp<hidl_death_recipient>& recipient,
78 uint64_t /*cookie*/) {
79 mDeathRecipient = recipient;
80 return linkToDeathRet();
81}
82
83inline void MockDevice::simulateCrash() {
84 ASSERT_NE(nullptr, mDeathRecipient.get());
85
86 // Currently, the utils::Device will not use the `cookie` or `who` arguments, so we pass in 0
87 // and nullptr for these arguments instead. Normally, they are used by the hidl_death_recipient
88 // to determine which object is dead. However, the utils::Device code only pairs a single death
89 // recipient with a single HIDL interface object, so these arguments are redundant.
90 mDeathRecipient->serviceDied(0, nullptr);
91}
92
93} // namespace android::hardware::neuralnetworks::V1_1::utils
94
Michael Butlerbbbdb0d2021-03-10 18:41:05 -080095#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_1_UTILS_TEST_MOCK_DEVICE_H