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_PREPARED_MODEL_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_TEST_MOCK_PREPARED_MODEL_H
19 
20 #include <android/hardware/neuralnetworks/1.0/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_0::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     // Helper methods.
44     void simulateCrash();
45 
46   private:
47     sp<hidl_death_recipient> mDeathRecipient;
48 };
49 
create()50 inline sp<MockPreparedModel> MockPreparedModel::create() {
51     auto mockPreparedModel = sp<MockPreparedModel>::make();
52 
53     // Setup default actions for each relevant call.
54     const auto ret = []() -> Return<bool> { return true; };
55 
56     // Setup default actions for each relevant call.
57     ON_CALL(*mockPreparedModel, linkToDeathRet()).WillByDefault(testing::Invoke(ret));
58 
59     // These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
60     // uninteresting methods calls.
61     EXPECT_CALL(*mockPreparedModel, linkToDeathRet()).Times(testing::AnyNumber());
62 
63     return mockPreparedModel;
64 }
65 
linkToDeath(const sp<hidl_death_recipient> & recipient,uint64_t)66 inline Return<bool> MockPreparedModel::linkToDeath(const sp<hidl_death_recipient>& recipient,
67                                                    uint64_t /*cookie*/) {
68     mDeathRecipient = recipient;
69     return linkToDeathRet();
70 }
71 
simulateCrash()72 inline void MockPreparedModel::simulateCrash() {
73     ASSERT_NE(nullptr, mDeathRecipient.get());
74 
75     // Currently, the utils::PreparedModel will not use the `cookie` or `who` arguments, so we pass
76     // in 0 and nullptr for these arguments instead. Normally, they are used by the
77     // hidl_death_recipient to determine which object is dead. However, the utils::PreparedModel
78     // code only pairs a single death recipient with a single HIDL interface object, so these
79     // arguments are redundant.
80     mDeathRecipient->serviceDied(0, nullptr);
81 }
82 
83 }  // namespace android::hardware::neuralnetworks::V1_0::utils
84 
85 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_TEST_MOCK_PREPARED_MODEL_H
86