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_PREPARED_MODEL_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_PREPARED_MODEL_H
19 
20 #include <android/hardware/neuralnetworks/1.3/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_3::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     // V1_2 methods below.
44     MOCK_METHOD(Return<V1_0::ErrorStatus>, execute_1_2,
45                 (const V1_0::Request& request, V1_2::MeasureTiming measure,
46                  const sp<V1_2::IExecutionCallback>& callback),
47                 (override));
48     MOCK_METHOD(Return<void>, executeSynchronously,
49                 (const V1_0::Request& request, V1_2::MeasureTiming measure,
50                  executeSynchronously_cb cb),
51                 (override));
52     MOCK_METHOD(Return<void>, configureExecutionBurst,
53                 (const sp<V1_2::IBurstCallback>& callback,
54                  const MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel,
55                  const MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel,
56                  configureExecutionBurst_cb cb),
57                 (override));
58 
59     // V1_3 methods below.
60     MOCK_METHOD(Return<V1_3::ErrorStatus>, execute_1_3,
61                 (const V1_3::Request& request, V1_2::MeasureTiming measure,
62                  const V1_3::OptionalTimePoint& deadline,
63                  const V1_3::OptionalTimeoutDuration& loopTimeoutDuration,
64                  const sp<V1_3::IExecutionCallback>& callback),
65                 (override));
66     MOCK_METHOD(Return<void>, executeSynchronously_1_3,
67                 (const V1_3::Request& request, V1_2::MeasureTiming measure,
68                  const V1_3::OptionalTimePoint& deadline,
69                  const V1_3::OptionalTimeoutDuration& loopTimeoutDuration,
70                  executeSynchronously_1_3_cb cb),
71                 (override));
72     MOCK_METHOD(Return<void>, executeFenced,
73                 (const V1_3::Request& request, const hidl_vec<hidl_handle>& waitFor,
74                  V1_2::MeasureTiming measure, const V1_3::OptionalTimePoint& deadline,
75                  const V1_3::OptionalTimeoutDuration& loopTimeoutDuration,
76                  const V1_3::OptionalTimeoutDuration& duration, executeFenced_cb cb),
77                 (override));
78 
79     // Helper methods.
80     void simulateCrash();
81 
82   private:
83     sp<hidl_death_recipient> mDeathRecipient;
84 };
85 
create()86 inline sp<MockPreparedModel> MockPreparedModel::create() {
87     auto mockPreparedModel = sp<MockPreparedModel>::make();
88 
89     // Setup default actions for each relevant call.
90     const auto ret = []() -> Return<bool> { return true; };
91 
92     // Setup default actions for each relevant call.
93     ON_CALL(*mockPreparedModel, linkToDeathRet()).WillByDefault(testing::Invoke(ret));
94 
95     // These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
96     // uninteresting methods calls.
97     EXPECT_CALL(*mockPreparedModel, linkToDeathRet()).Times(testing::AnyNumber());
98 
99     return mockPreparedModel;
100 }
101 
linkToDeath(const sp<hidl_death_recipient> & recipient,uint64_t)102 inline Return<bool> MockPreparedModel::linkToDeath(const sp<hidl_death_recipient>& recipient,
103                                                    uint64_t /*cookie*/) {
104     mDeathRecipient = recipient;
105     return linkToDeathRet();
106 }
107 
simulateCrash()108 inline void MockPreparedModel::simulateCrash() {
109     ASSERT_NE(nullptr, mDeathRecipient.get());
110 
111     // Currently, the utils::PreparedModel will not use the `cookie` or `who` arguments, so we pass
112     // in 0 and nullptr for these arguments instead. Normally, they are used by the
113     // hidl_death_recipient to determine which object is dead. However, the utils::PreparedModel
114     // code only pairs a single death recipient with a single HIDL interface object, so these
115     // arguments are redundant.
116     mDeathRecipient->serviceDied(0, nullptr);
117 }
118 
119 }  // namespace android::hardware::neuralnetworks::V1_3::utils
120 
121 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_PREPARED_MODEL_H
122