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 #include <android-base/logging.h>
18 #include <android/hardware/neuralnetworks/1.0/ADevice.h>
19 #include <android/hardware/neuralnetworks/1.1/ADevice.h>
20 #include <android/hardware/neuralnetworks/1.2/ADevice.h>
21 #include <android/hardware/neuralnetworks/1.3/ADevice.h>
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24 #include <hidl/Status.h>
25 #include <utils/Errors.h>
26 
27 #include <limits>
28 #include <memory>
29 #include <utility>
30 #include <vector>
31 
32 #include "HalInterfaces.h"
33 #include "MemoryUtils.h"
34 #include "MetaModel.h"
35 #include "VersionedInterfaces.h"
36 
37 namespace android::nn {
38 namespace {
39 
40 using namespace hal;
41 using testing::_;
42 using testing::Invoke;
43 using testing::InvokeWithoutArgs;
44 using testing::MockFunction;
45 using MockDeviceFactory = MockFunction<sp<V1_0::IDevice>(bool blocking)>;
46 
47 constexpr uint32_t kNoCacheFilesNeeded = 0;
48 constexpr uint32_t kMaxNumberOfCacheFiles =
49         static_cast<uint32_t>(Constant::MAX_NUMBER_OF_CACHE_FILES);
50 constexpr Timing kNoTiming = {.timeOnDevice = std::numeric_limits<uint64_t>::max(),
51                               .timeInDriver = std::numeric_limits<uint64_t>::max()};
52 
53 template <typename... Args>
makeCallbackReturn(Args &&...args)54 auto makeCallbackReturn(Args&&... args) {
55     return [argPack = std::make_tuple(std::forward<Args>(args)...)](const auto& cb) {
56         std::apply(cb, argPack);
57         return Void();
58     };
59 };
60 
61 class MockDevice : public IDevice {
62    public:
create()63     static sp<MockDevice> create() {
64         const sp<MockDevice> mockDevice = new MockDevice();
65 
66         const auto linkToDeathRet_ret = []() -> Return<bool> { return true; };
67         const auto getCapabilities_ret =
68                 makeCallbackReturn(V1_0::ErrorStatus::NONE, V1_0::Capabilities{});
69         const auto getCapabilities_1_1_ret =
70                 makeCallbackReturn(V1_0::ErrorStatus::NONE, V1_1::Capabilities{});
71         const auto getVersionString_ret =
72                 makeCallbackReturn(V1_0::ErrorStatus::NONE, "Google-MockV1");
73         const auto getType_ret = makeCallbackReturn(V1_0::ErrorStatus::NONE, DeviceType::OTHER);
74         const auto getCapabilities_1_2_ret =
75                 makeCallbackReturn(V1_0::ErrorStatus::NONE, V1_2::Capabilities{});
76         const auto getSupportedExtensions_ret =
77                 makeCallbackReturn(V1_0::ErrorStatus::NONE, hidl_vec<Extension>{});
78         const auto getNumberOfCacheFilesNeeded_ret = makeCallbackReturn(
79                 V1_0::ErrorStatus::NONE, kMaxNumberOfCacheFiles, kMaxNumberOfCacheFiles);
80         const auto getCapabilities_1_3_ret =
81                 makeCallbackReturn(V1_3::ErrorStatus::NONE, V1_3::Capabilities{});
82 
83         ON_CALL(*mockDevice, linkToDeathRet()).WillByDefault(Invoke(linkToDeathRet_ret));
84         ON_CALL(*mockDevice, getCapabilities(_)).WillByDefault(Invoke(getCapabilities_ret));
85         ON_CALL(*mockDevice, getCapabilities_1_1(_)).WillByDefault(Invoke(getCapabilities_1_1_ret));
86         ON_CALL(*mockDevice, getVersionString(_)).WillByDefault(Invoke(getVersionString_ret));
87         ON_CALL(*mockDevice, getType(_)).WillByDefault(Invoke(getType_ret));
88         ON_CALL(*mockDevice, getCapabilities_1_2(_)).WillByDefault(Invoke(getCapabilities_1_2_ret));
89         ON_CALL(*mockDevice, getSupportedExtensions(_))
90                 .WillByDefault(Invoke(getSupportedExtensions_ret));
91         ON_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
92                 .WillByDefault(Invoke(getNumberOfCacheFilesNeeded_ret));
93         ON_CALL(*mockDevice, getCapabilities_1_3(_)).WillByDefault(Invoke(getCapabilities_1_3_ret));
94 
95         // These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to
96         // suppress warnings on the uninteresting methods calls.
97         EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(testing::AnyNumber());
98         EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(testing::AnyNumber());
99         EXPECT_CALL(*mockDevice, getCapabilities_1_1(_)).Times(testing::AnyNumber());
100         EXPECT_CALL(*mockDevice, getVersionString(_)).Times(testing::AnyNumber());
101         EXPECT_CALL(*mockDevice, getType(_)).Times(testing::AnyNumber());
102         EXPECT_CALL(*mockDevice, getCapabilities_1_2(_)).Times(testing::AnyNumber());
103         EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(testing::AnyNumber());
104         EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(testing::AnyNumber());
105         EXPECT_CALL(*mockDevice, getCapabilities_1_3(_)).Times(testing::AnyNumber());
106 
107         return mockDevice;
108     }
109 
110     // IBase methods below.
linkToDeath(const sp<hidl_death_recipient> & recipient,uint64_t)111     Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient,
112                              uint64_t /*cookie*/) override {
113         mDeathRecipient = recipient;
114         return linkToDeathRet();
115     }
116     MOCK_METHOD(Return<void>, ping, (), (override));
117 
118     // V1_0 methods below.
119     MOCK_METHOD(Return<void>, getCapabilities, (getCapabilities_cb cb), (override));
120     MOCK_METHOD(Return<void>, getSupportedOperations,
121                 (const V1_0::Model& model, getSupportedOperations_cb cb), (override));
122     MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel,
123                 (const V1_0::Model& model, const sp<V1_0::IPreparedModelCallback>& callback),
124                 (override));
125     MOCK_METHOD(Return<DeviceStatus>, getStatus, (), (override));
126 
127     // V1_1 methods below.
128     MOCK_METHOD(Return<void>, getCapabilities_1_1, (getCapabilities_1_1_cb cb), (override));
129     MOCK_METHOD(Return<void>, getSupportedOperations_1_1,
130                 (const V1_1::Model& model, getSupportedOperations_1_1_cb cb), (override));
131     MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel_1_1,
132                 (const V1_1::Model& model, ExecutionPreference preference,
133                  const sp<V1_0::IPreparedModelCallback>& callback),
134                 (override));
135 
136     // V1_2 methods below.
137     MOCK_METHOD(Return<void>, getVersionString, (getVersionString_cb cb), (override));
138     MOCK_METHOD(Return<void>, getType, (getType_cb cb), (override));
139     MOCK_METHOD(Return<void>, getCapabilities_1_2, (getCapabilities_1_2_cb cb), (override));
140     MOCK_METHOD(Return<void>, getSupportedExtensions, (getSupportedExtensions_cb cb), (override));
141     MOCK_METHOD(Return<void>, getSupportedOperations_1_2,
142                 (const V1_2::Model& model, getSupportedOperations_1_2_cb cb), (override));
143     MOCK_METHOD(Return<void>, getNumberOfCacheFilesNeeded, (getNumberOfCacheFilesNeeded_cb cb),
144                 (override));
145     MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel_1_2,
146                 (const V1_2::Model& model, ExecutionPreference preference,
147                  const hidl_vec<hidl_handle>& modelCache, const hidl_vec<hidl_handle>& dataCache,
148                  const CacheToken& token, const sp<V1_2::IPreparedModelCallback>& callback),
149                 (override));
150     MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModelFromCache,
151                 (const hidl_vec<hidl_handle>& modelCache, const hidl_vec<hidl_handle>& dataCache,
152                  const CacheToken& token, const sp<V1_2::IPreparedModelCallback>& callback),
153                 (override));
154 
155     // V1_3 methods below.
156     MOCK_METHOD(Return<void>, getCapabilities_1_3, (getCapabilities_1_3_cb cb), (override));
157     MOCK_METHOD(Return<void>, getSupportedOperations_1_3,
158                 (const V1_3::Model& model, getSupportedOperations_1_3_cb cb), (override));
159     MOCK_METHOD(Return<V1_3::ErrorStatus>, prepareModel_1_3,
160                 (const V1_3::Model& model, ExecutionPreference preference, Priority priority,
161                  const OptionalTimePoint& deadline, const hidl_vec<hidl_handle>& modelCache,
162                  const hidl_vec<hidl_handle>& dataCache, const CacheToken& token,
163                  const sp<V1_3::IPreparedModelCallback>& callback),
164                 (override));
165     MOCK_METHOD(Return<V1_3::ErrorStatus>, prepareModelFromCache_1_3,
166                 (const OptionalTimePoint& deadline, const hidl_vec<hidl_handle>& modelCache,
167                  const hidl_vec<hidl_handle>& dataCache, const CacheToken& token,
168                  const sp<V1_3::IPreparedModelCallback>& callback),
169                 (override));
170     MOCK_METHOD(Return<void>, allocate,
171                 (const BufferDesc& desc, const hidl_vec<sp<V1_3::IPreparedModel>>& preparedModels,
172                  const hidl_vec<BufferRole>& inputRoles, const hidl_vec<BufferRole>& outputRoles,
173                  allocate_cb cb),
174                 (override));
175 
176     // Helper methods.
177     MOCK_METHOD(Return<bool>, linkToDeathRet, ());
simulateCrash()178     void simulateCrash() {
179         ASSERT_NE(nullptr, mDeathRecipient.get());
180 
181         // Currently, the VersionedInterfaces code will not use the `cookie` or
182         // `who` arguments, so we pass in 0 and nullptr for these arguments
183         // instead. Normally, they are used by the hidl_death_recipient to
184         // determine which object is dead. However, the VersionedInterfaces
185         // code only pairs a single death recipient with a single HIDL
186         // interface object, so these arguments are redundant.
187         mDeathRecipient->serviceDied(0, nullptr);
188     }
189 
190    private:
191     // Members.
192     sp<hidl_death_recipient> mDeathRecipient;
193 };
194 
195 class MockPreparedModel : public IPreparedModel {
196    public:
create()197     static sp<MockPreparedModel> create() {
198         const sp<MockPreparedModel> mockPreparedModel = new MockPreparedModel();
199 
200         const auto linkToDeathRet_ret = []() -> Return<bool> { return true; };
201         ON_CALL(*mockPreparedModel, linkToDeathRet()).WillByDefault(Invoke(linkToDeathRet_ret));
202 
203         // This EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to
204         // suppress warnings on the uninteresting methods calls.
205         EXPECT_CALL(*mockPreparedModel, linkToDeathRet()).Times(testing::AnyNumber());
206 
207         return mockPreparedModel;
208     }
209 
210     // IBase methods below.
linkToDeath(const sp<hidl_death_recipient> & recipient,uint64_t)211     Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient,
212                              uint64_t /*cookie*/) override {
213         mDeathRecipient = recipient;
214         return linkToDeathRet();
215     }
216     MOCK_METHOD(Return<void>, ping, (), (override));
217 
218     // V1_0 methods below.
219     MOCK_METHOD(Return<V1_0::ErrorStatus>, execute,
220                 (const V1_0::Request& request, const sp<V1_0::IExecutionCallback>& callback),
221                 (override));
222 
223     // V1_2 methods below.
224     MOCK_METHOD(Return<V1_0::ErrorStatus>, execute_1_2,
225                 (const V1_0::Request& request, MeasureTiming measure,
226                  const sp<V1_2::IExecutionCallback>& callback),
227                 (override));
228     MOCK_METHOD(Return<void>, executeSynchronously,
229                 (const V1_0::Request& request, MeasureTiming measure, executeSynchronously_cb cb),
230                 (override));
231     MOCK_METHOD(Return<void>, configureExecutionBurst,
232                 (const sp<V1_2::IBurstCallback>& callback,
233                  const hardware::MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel,
234                  const hardware::MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel,
235                  configureExecutionBurst_cb cb),
236                 (override));
237 
238     // V1_3 methods below.
239     MOCK_METHOD(Return<ErrorStatus>, execute_1_3,
240                 (const V1_3::Request& request, MeasureTiming measure,
241                  const OptionalTimePoint& deadline,
242                  const OptionalTimeoutDuration& loopTimeoutDuration,
243                  const sp<IExecutionCallback>& callback),
244                 (override));
245     MOCK_METHOD(Return<void>, executeSynchronously_1_3,
246                 (const V1_3::Request& request, MeasureTiming measure,
247                  const OptionalTimePoint& deadline,
248                  const OptionalTimeoutDuration& loopTimeoutDuration,
249                  executeSynchronously_1_3_cb cb),
250                 (override));
251     MOCK_METHOD(Return<void>, executeFenced,
252                 (const V1_3::Request& request, const hidl_vec<hidl_handle>& waitFor,
253                  MeasureTiming measure, const OptionalTimePoint& deadline,
254                  const OptionalTimeoutDuration& loopTimeoutDuration,
255                  const OptionalTimeoutDuration& duration, executeFenced_cb cb),
256                 (override));
257 
258     // Helper methods.
259     MOCK_METHOD(Return<bool>, linkToDeathRet, ());
simulateCrash()260     void simulateCrash() {
261         ASSERT_NE(nullptr, mDeathRecipient.get());
262 
263         // Currently, the VersionedInterfaces code will not use the `cookie` or
264         // `who` arguments, so we pass in 0 and nullptr for these arguments
265         // instead. Normally, they are used by the hidl_death_recipient to
266         // determine which object is dead. However, the VersionedInterfaces
267         // code only pairs a single death recipient with a single HIDL
268         // interface object, so these arguments are redundant.
269         mDeathRecipient->serviceDied(0, nullptr);
270     }
271 
272    private:
273     // Members.
274     sp<hidl_death_recipient> mDeathRecipient;
275 };
276 
277 class MockBurstContext : public V1_2::IBurstContext {
278    public:
279     // V1_2 methods below.
280     MOCK_METHOD(Return<void>, freeMemory, (int32_t slot), (override));
281 };
282 
283 class MockFencedExecutionCallback : public IFencedExecutionCallback {
284    public:
285     // V1_3 methods below.
286     MOCK_METHOD(Return<void>, getExecutionInfo, (getExecutionInfo_cb cb), (override));
287 };
288 
289 class MockBuffer : public IBuffer {
290    public:
291     // V1_3 methods below.
292     MOCK_METHOD(Return<ErrorStatus>, copyTo, (const hidl_memory& dst), (override));
293     MOCK_METHOD(Return<ErrorStatus>, copyFrom,
294                 (const hidl_memory& src, const hidl_vec<uint32_t>& dimensions), (override));
295 };
296 
297 enum class Version { V1_0, V1_1, V1_2, V1_3, MOCK };
298 
adaptAs(const sp<MockDevice> & mockDevice,Version version)299 sp<V1_0::IDevice> adaptAs(const sp<MockDevice>& mockDevice, Version version) {
300     switch (version) {
301         case Version::V1_0:
302             return new V1_0::ADevice(mockDevice);
303         case Version::V1_1:
304             return new V1_1::ADevice(mockDevice);
305         case Version::V1_2:
306             return new V1_2::ADevice(mockDevice);
307         case Version::V1_3:
308             return new V1_3::ADevice(mockDevice);
309         case Version::MOCK:
310             return mockDevice;
311     }
312     LOG(FATAL) << "unrecognized version: " << static_cast<int>(version);
313     return nullptr;
314 }
315 
makePreparedModelReturn(V1_0::ErrorStatus launchStatus,V1_0::ErrorStatus returnStatus,const sp<MockPreparedModel> & preparedModel)316 auto makePreparedModelReturn(V1_0::ErrorStatus launchStatus, V1_0::ErrorStatus returnStatus,
317                              const sp<MockPreparedModel>& preparedModel) {
318     return [launchStatus, returnStatus, preparedModel](
319                    const V1_0::Model& /*model*/,
320                    const sp<V1_0::IPreparedModelCallback>& cb) -> Return<V1_0::ErrorStatus> {
321         cb->notify(returnStatus, preparedModel).isOk();
322         return launchStatus;
323     };
324 }
makePreparedModel_1_1Return(V1_0::ErrorStatus launchStatus,V1_0::ErrorStatus returnStatus,const sp<MockPreparedModel> & preparedModel)325 auto makePreparedModel_1_1Return(V1_0::ErrorStatus launchStatus, V1_0::ErrorStatus returnStatus,
326                                  const sp<MockPreparedModel>& preparedModel) {
327     return [launchStatus, returnStatus, preparedModel](
328                    const V1_1::Model& /*model*/, ExecutionPreference /*preference*/,
329                    const sp<V1_0::IPreparedModelCallback>& cb) -> Return<V1_0::ErrorStatus> {
330         cb->notify(returnStatus, preparedModel).isOk();
331         return launchStatus;
332     };
333 }
makePreparedModel_1_2Return(V1_0::ErrorStatus launchStatus,V1_0::ErrorStatus returnStatus,const sp<MockPreparedModel> & preparedModel)334 auto makePreparedModel_1_2Return(V1_0::ErrorStatus launchStatus, V1_0::ErrorStatus returnStatus,
335                                  const sp<MockPreparedModel>& preparedModel) {
336     return [launchStatus, returnStatus, preparedModel](
337                    const V1_2::Model& /*model*/, ExecutionPreference /*preference*/,
338                    const auto& /*modelCache*/, const auto& /*dataCache*/, const auto& /*token*/,
339                    const sp<V1_2::IPreparedModelCallback>& cb) -> Return<V1_0::ErrorStatus> {
340         cb->notify_1_2(returnStatus, preparedModel).isOk();
341         return launchStatus;
342     };
343 }
makePreparedModel_1_3Return(V1_3::ErrorStatus launchStatus,V1_3::ErrorStatus returnStatus,const sp<MockPreparedModel> & preparedModel)344 auto makePreparedModel_1_3Return(V1_3::ErrorStatus launchStatus, V1_3::ErrorStatus returnStatus,
345                                  const sp<MockPreparedModel>& preparedModel) {
346     return [launchStatus, returnStatus, preparedModel](
347                    const V1_3::Model& /*model*/, ExecutionPreference /*preference*/,
348                    Priority /*priority*/, const OptionalTimePoint& /*deadline*/,
349                    const hidl_vec<hidl_handle>& /*modelCache*/,
350                    const hidl_vec<hidl_handle>& /*dataCache*/, const CacheToken& /*token*/,
351                    const sp<V1_3::IPreparedModelCallback>& cb) -> Return<V1_3::ErrorStatus> {
352         cb->notify_1_3(returnStatus, preparedModel).isOk();
353         return launchStatus;
354     };
355 }
356 
makeExecuteReturn(V1_0::ErrorStatus launchStatus,V1_0::ErrorStatus returnStatus)357 auto makeExecuteReturn(V1_0::ErrorStatus launchStatus, V1_0::ErrorStatus returnStatus) {
358     return [launchStatus, returnStatus](
359                    const V1_0::Request& /*request*/,
360                    const sp<V1_0::IExecutionCallback>& cb) -> Return<V1_0::ErrorStatus> {
361         cb->notify(returnStatus);
362         return launchStatus;
363     };
364 }
makeExecute_1_2Return(V1_0::ErrorStatus launchStatus,V1_0::ErrorStatus returnStatus,const std::vector<OutputShape> & outputShapes,const Timing & timing)365 auto makeExecute_1_2Return(V1_0::ErrorStatus launchStatus, V1_0::ErrorStatus returnStatus,
366                            const std::vector<OutputShape>& outputShapes, const Timing& timing) {
367     return [launchStatus, returnStatus, outputShapes, timing](
368                    const V1_0::Request& /*request*/, MeasureTiming /*measureTiming*/,
369                    const sp<V1_2::IExecutionCallback>& cb) -> Return<V1_0::ErrorStatus> {
370         cb->notify_1_2(returnStatus, outputShapes, timing);
371         return launchStatus;
372     };
373 }
makeExecute_1_3Return(V1_3::ErrorStatus launchStatus,V1_3::ErrorStatus returnStatus,const std::vector<OutputShape> & outputShapes,const Timing & timing)374 auto makeExecute_1_3Return(V1_3::ErrorStatus launchStatus, V1_3::ErrorStatus returnStatus,
375                            const std::vector<OutputShape>& outputShapes, const Timing& timing) {
376     return [launchStatus, returnStatus, outputShapes, timing](
377                    const V1_3::Request& /*request*/, MeasureTiming /*measureTiming*/,
378                    const OptionalTimePoint& /*deadline*/,
379                    const OptionalTimeoutDuration& /*loopTimeoutDuration*/,
380                    const sp<V1_3::IExecutionCallback>& cb) -> Return<V1_3::ErrorStatus> {
381         cb->notify_1_3(returnStatus, outputShapes, timing);
382         return launchStatus;
383     };
384 }
makeExecuteSynchronouslyReturn(V1_0::ErrorStatus status,const std::vector<OutputShape> & outputShapes,const Timing & timing)385 auto makeExecuteSynchronouslyReturn(V1_0::ErrorStatus status,
386                                     const std::vector<OutputShape>& outputShapes,
387                                     const Timing& timing) {
388     return [status, outputShapes, timing](const V1_0::Request& /*request*/,
389                                           MeasureTiming /*measureTiming*/,
390                                           const V1_2::IPreparedModel::executeSynchronously_cb& cb) {
391         cb(status, outputShapes, timing);
392         return Void();
393     };
394 }
makeExecuteSynchronously_1_3Return(V1_3::ErrorStatus status,const std::vector<OutputShape> & outputShapes,const Timing & timing)395 auto makeExecuteSynchronously_1_3Return(V1_3::ErrorStatus status,
396                                         const std::vector<OutputShape>& outputShapes,
397                                         const Timing& timing) {
398     return [status, outputShapes, timing](
399                    const V1_3::Request& /*request*/, MeasureTiming /*measureTiming*/,
400                    const OptionalTimePoint& /*deadline*/,
401                    const OptionalTimeoutDuration& /*loopTimeoutDuration*/,
402                    const V1_3::IPreparedModel::executeSynchronously_1_3_cb& cb) {
403         cb(status, outputShapes, timing);
404         return Void();
405     };
406 }
makeConfigureExecutionBurst(V1_0::ErrorStatus status,const sp<MockBurstContext> & burstContext)407 auto makeConfigureExecutionBurst(V1_0::ErrorStatus status,
408                                  const sp<MockBurstContext>& burstContext) {
409     return [status, burstContext](
410                    const sp<V1_2::IBurstCallback>& /*callback*/,
411                    const hardware::MQDescriptorSync<V1_2::FmqRequestDatum>& /*requestChannel*/,
412                    const hardware::MQDescriptorSync<V1_2::FmqResultDatum>& /*resultChannel*/,
413                    V1_2::IPreparedModel::configureExecutionBurst_cb cb) {
414         cb(status, burstContext);
415         return Void();
416     };
417 }
makeExecuteFencedReturn(V1_3::ErrorStatus status,const hidl_handle & syncFence,const sp<IFencedExecutionCallback> & dispatchCallback)418 auto makeExecuteFencedReturn(V1_3::ErrorStatus status, const hidl_handle& syncFence,
419                              const sp<IFencedExecutionCallback>& dispatchCallback) {
420     return [status, syncFence, dispatchCallback](
421                    const V1_3::Request& /*request*/, const hidl_vec<hidl_handle>& /*waitFor*/,
422                    MeasureTiming /*measure*/, const OptionalTimePoint& /*deadline*/,
423                    const OptionalTimeoutDuration& /*loopTimeoutDuration*/,
424                    const OptionalTimeoutDuration& /*duration*/,
425                    V1_3::IPreparedModel::executeFenced_cb cb) {
426         cb(status, syncFence, dispatchCallback);
427         return Void();
428     };
429 }
430 
431 // TODO: The "setupInitializationExpectation*" calls below re-specify the
432 // number of expected times each initialization method is called. Because
433 // this was originally set to `testing::AnyNumber()` when the object was
434 // created, do these calls act as no-ops, do they override the previous
435 // expectations, or are both expectations still active?
436 
setupInitializationExpectationsV1_0(const sp<MockDevice> & mockDevice)437 void setupInitializationExpectationsV1_0(const sp<MockDevice>& mockDevice) {
438     EXPECT_CALL(*mockDevice, getCapabilities_1_1(_)).Times(0);
439     EXPECT_CALL(*mockDevice, getCapabilities_1_2(_)).Times(0);
440     EXPECT_CALL(*mockDevice, getCapabilities_1_3(_)).Times(0);
441     EXPECT_CALL(*mockDevice, getVersionString(_)).Times(0);
442     EXPECT_CALL(*mockDevice, getType(_)).Times(0);
443     EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(0);
444     EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(0);
445 }
446 
setupInitializationExpectationsV1_1(const sp<MockDevice> & mockDevice)447 void setupInitializationExpectationsV1_1(const sp<MockDevice>& mockDevice) {
448     EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(0);
449     EXPECT_CALL(*mockDevice, getCapabilities_1_2(_)).Times(0);
450     EXPECT_CALL(*mockDevice, getCapabilities_1_3(_)).Times(0);
451     EXPECT_CALL(*mockDevice, getVersionString(_)).Times(0);
452     EXPECT_CALL(*mockDevice, getType(_)).Times(0);
453     EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(0);
454     EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(0);
455 }
456 
setupInitializationExpectationsV1_2(const sp<MockDevice> & mockDevice)457 void setupInitializationExpectationsV1_2(const sp<MockDevice>& mockDevice) {
458     EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(0);
459     EXPECT_CALL(*mockDevice, getCapabilities_1_1(_)).Times(0);
460     EXPECT_CALL(*mockDevice, getCapabilities_1_3(_)).Times(0);
461 }
462 
setupInitializationExpectationsV1_3(const sp<MockDevice> & mockDevice)463 void setupInitializationExpectationsV1_3(const sp<MockDevice>& mockDevice) {
464     EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(0);
465     EXPECT_CALL(*mockDevice, getCapabilities_1_1(_)).Times(0);
466     EXPECT_CALL(*mockDevice, getCapabilities_1_2(_)).Times(0);
467 }
468 
setupInitializationExpectations(const sp<MockDevice> & mockDevice,Version version)469 void setupInitializationExpectations(const sp<MockDevice>& mockDevice, Version version) {
470     switch (version) {
471         case Version::V1_0:
472             setupInitializationExpectationsV1_0(mockDevice);
473             return;
474         case Version::V1_1:
475             setupInitializationExpectationsV1_1(mockDevice);
476             return;
477         case Version::V1_2:
478             setupInitializationExpectationsV1_2(mockDevice);
479             return;
480         case Version::V1_3:
481             setupInitializationExpectationsV1_3(mockDevice);
482             return;
483         case Version::MOCK:
484             setupInitializationExpectationsV1_3(mockDevice);
485             return;
486     }
487     LOG(FATAL) << "unrecognized version: " << static_cast<int>(version);
488 }
489 
setupSuccessfulInitializationExpectations(const sp<MockDevice> & mockDevice,Version version)490 void setupSuccessfulInitializationExpectations(const sp<MockDevice>& mockDevice, Version version) {
491     EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(testing::AnyNumber());
492 
493     const int numCallsForV1_0 = (version == Version::V1_0 ? 1 : 0);
494     EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(numCallsForV1_0);
495 
496     const int numCallsForV1_1 = (version == Version::V1_1 ? 1 : 0);
497     EXPECT_CALL(*mockDevice, getCapabilities_1_1(_)).Times(numCallsForV1_1);
498 
499     const int numCallsForV1_2 = (version == Version::V1_2 ? 1 : 0);
500     EXPECT_CALL(*mockDevice, getCapabilities_1_2(_)).Times(numCallsForV1_2);
501 
502     const int numCallsForAtLeastV1_3 = (version >= Version::V1_3 ? 1 : 0);
503     EXPECT_CALL(*mockDevice, getCapabilities_1_3(_)).Times(numCallsForAtLeastV1_3);
504 
505     const int numCallsForAtLeastV1_2 = (version >= Version::V1_2 ? 1 : 0);
506     EXPECT_CALL(*mockDevice, getVersionString(_)).Times(numCallsForAtLeastV1_2);
507     EXPECT_CALL(*mockDevice, getType(_)).Times(numCallsForAtLeastV1_2);
508     EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(numCallsForAtLeastV1_2);
509     EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(numCallsForAtLeastV1_2);
510 }
511 
makeVersionedIDeviceFrom(const sp<MockDevice> & mockDevice,MockDeviceFactory * mockDeviceFactory,Version version)512 std::shared_ptr<VersionedIDevice> makeVersionedIDeviceFrom(const sp<MockDevice>& mockDevice,
513                                                            MockDeviceFactory* mockDeviceFactory,
514                                                            Version version) {
515     setupInitializationExpectations(mockDevice, version);
516     const auto device = adaptAs(mockDevice, version);
517     ON_CALL(*mockDeviceFactory, Call(_)).WillByDefault(testing::Return(device));
518     EXPECT_CALL(*mockDeviceFactory, Call(/*blocking=*/true)).Times(testing::AtLeast(1));
519     const DeviceFactory makeDevice = mockDeviceFactory->AsStdFunction();
520     return VersionedIDevice::create("MockDevice", makeDevice);
521 }
522 
makeVersionedIDeviceSuccessfulInitializationFrom(const sp<MockDevice> & device,MockDeviceFactory * mockDeviceFactory,Version version)523 std::shared_ptr<VersionedIDevice> makeVersionedIDeviceSuccessfulInitializationFrom(
524         const sp<MockDevice>& device, MockDeviceFactory* mockDeviceFactory, Version version) {
525     setupSuccessfulInitializationExpectations(device, version);
526     return makeVersionedIDeviceFrom(device, mockDeviceFactory, version);
527 }
528 
makeTransportFailure(status_t status)529 std::function<hardware::Status()> makeTransportFailure(status_t status) {
530     return [status] { return hardware::Status::fromStatusT(status); };
531 }
532 
533 const auto makeGeneralTransportFailure = makeTransportFailure(NO_MEMORY);
534 const auto makeDeadObjectFailure = makeTransportFailure(DEAD_OBJECT);
535 
536 class VersionedIDeviceTest : public testing::Test {
537    protected:
538     const sp<MockDevice> kMockDevice = MockDevice::create();
539     const std::unique_ptr<MockDeviceFactory> kMockMakeDevice =
540             std::make_unique<MockDeviceFactory>();
541 };
542 
543 class VersionedIDeviceInitializationTest : public VersionedIDeviceTest {};
544 
545 template <Version version>
546 class VersionedIDeviceInitializedTest : public VersionedIDeviceTest {
547    protected:
SetUp()548     void SetUp() override {
549         VersionedIDeviceTest::SetUp();
550         ASSERT_NE(nullptr, kDevice.get());
551     }
552 
553     const std::shared_ptr<VersionedIDevice> kDevice =
554             makeVersionedIDeviceSuccessfulInitializationFrom(kMockDevice, kMockMakeDevice.get(),
555                                                              version);
556 };
557 
558 class VersionedIDeviceV1_0Test : public VersionedIDeviceInitializedTest<Version::V1_0> {};
559 class VersionedIDeviceV1_1Test : public VersionedIDeviceInitializedTest<Version::V1_1> {};
560 class VersionedIDeviceV1_2Test : public VersionedIDeviceInitializedTest<Version::V1_2> {};
561 class VersionedIDeviceV1_3Test : public VersionedIDeviceInitializedTest<Version::V1_3> {};
562 class VersionedIDeviceMockTest : public VersionedIDeviceInitializedTest<Version::MOCK> {};
563 
564 // Simulate initialization/link error
565 
TEST_F(VersionedIDeviceInitializationTest,creationFailure)566 TEST_F(VersionedIDeviceInitializationTest, creationFailure) {
567     // setup failure
568     EXPECT_CALL(*kMockMakeDevice, Call(_)).Times(1).WillOnce(testing::Return(nullptr));
569     const DeviceFactory makeDevice = kMockMakeDevice->AsStdFunction();
570 
571     // run test
572     const auto device = VersionedIDevice::create("MockDevice", makeDevice);
573 
574     // verify failure
575     EXPECT_EQ(nullptr, device.get());
576 }
577 
TEST_F(VersionedIDeviceInitializationTest,linkToDeathTransportFailure)578 TEST_F(VersionedIDeviceInitializationTest, linkToDeathTransportFailure) {
579     // setup failure
580     EXPECT_CALL(*kMockDevice, linkToDeathRet())
581             .Times(1)
582             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
583     EXPECT_CALL(*kMockMakeDevice, Call(_)).Times(1).WillOnce(testing::Return(kMockDevice));
584     const DeviceFactory makeDevice = kMockMakeDevice->AsStdFunction();
585 
586     // run test
587     const auto device = VersionedIDevice::create("MockDevice", makeDevice);
588 
589     // verify failure
590     EXPECT_EQ(nullptr, device.get());
591 }
592 
TEST_F(VersionedIDeviceInitializationTest,linkToDeathReturnError)593 TEST_F(VersionedIDeviceInitializationTest, linkToDeathReturnError) {
594     // setup failure
595     const auto ret = []() -> Return<bool> { return false; };
596     EXPECT_CALL(*kMockMakeDevice, Call(_)).Times(1).WillOnce(testing::Return(kMockDevice));
597     EXPECT_CALL(*kMockDevice, linkToDeathRet()).Times(1).WillOnce(InvokeWithoutArgs(ret));
598     const DeviceFactory makeDevice = kMockMakeDevice->AsStdFunction();
599 
600     // run test
601     const auto device = VersionedIDevice::create("MockDevice", makeDevice);
602 
603     // verify failure
604     EXPECT_EQ(nullptr, device.get());
605 }
606 
TEST_F(VersionedIDeviceInitializationTest,getCapabilitiesFailure)607 TEST_F(VersionedIDeviceInitializationTest, getCapabilitiesFailure) {
608     // setup failure
609     const auto ret = makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE, V1_0::Capabilities{});
610     EXPECT_CALL(*kMockDevice, getCapabilities(_)).Times(1).WillOnce(Invoke(ret));
611 
612     // run test
613     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_0);
614 
615     // verify failure
616     EXPECT_EQ(nullptr, device.get());
617 }
618 
TEST_F(VersionedIDeviceInitializationTest,getCapabilities_1_1Failure)619 TEST_F(VersionedIDeviceInitializationTest, getCapabilities_1_1Failure) {
620     // setup failure
621     const auto ret = makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE, V1_1::Capabilities{});
622     EXPECT_CALL(*kMockDevice, getCapabilities_1_1(_)).Times(1).WillOnce(Invoke(ret));
623 
624     // run test
625     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_1);
626 
627     // verify failure
628     EXPECT_EQ(nullptr, device.get());
629 }
630 
TEST_F(VersionedIDeviceInitializationTest,getCapabilities_1_2Failure)631 TEST_F(VersionedIDeviceInitializationTest, getCapabilities_1_2Failure) {
632     // setup failure
633     const auto ret = makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE, V1_2::Capabilities{});
634     EXPECT_CALL(*kMockDevice, getCapabilities_1_2(_)).Times(1).WillOnce(Invoke(ret));
635 
636     // run test
637     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_2);
638 
639     // verify failure
640     EXPECT_EQ(nullptr, device.get());
641 }
642 
TEST_F(VersionedIDeviceInitializationTest,getCapabilities_1_3Failure)643 TEST_F(VersionedIDeviceInitializationTest, getCapabilities_1_3Failure) {
644     // setup failure
645     const auto ret = makeCallbackReturn(V1_3::ErrorStatus::GENERAL_FAILURE, V1_3::Capabilities{});
646     EXPECT_CALL(*kMockDevice, getCapabilities_1_3(_)).Times(1).WillOnce(Invoke(ret));
647 
648     // run test
649     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_3);
650 
651     // verify failure
652     EXPECT_EQ(nullptr, device.get());
653 }
654 
TEST_F(VersionedIDeviceInitializationTest,getVersionStringFailure)655 TEST_F(VersionedIDeviceInitializationTest, getVersionStringFailure) {
656     // setup failure
657     const auto ret = makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE, "");
658     EXPECT_CALL(*kMockDevice, getVersionString(_)).Times(1).WillOnce(Invoke(ret));
659 
660     // run test
661     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_2);
662 
663     // verify failure
664     EXPECT_EQ(nullptr, device.get());
665 }
666 
TEST_F(VersionedIDeviceInitializationTest,getTypeFailure)667 TEST_F(VersionedIDeviceInitializationTest, getTypeFailure) {
668     // setup failure
669     const auto ret = makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE, DeviceType::OTHER);
670     EXPECT_CALL(*kMockDevice, getType(_)).Times(1).WillOnce(Invoke(ret));
671 
672     // run test
673     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_2);
674 
675     // verify failure
676     EXPECT_EQ(nullptr, device.get());
677 }
678 
TEST_F(VersionedIDeviceInitializationTest,getSupportedExtensionsFailure)679 TEST_F(VersionedIDeviceInitializationTest, getSupportedExtensionsFailure) {
680     // setup failure
681     const auto ret = makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE, hidl_vec<Extension>{});
682     EXPECT_CALL(*kMockDevice, getSupportedExtensions(_)).Times(1).WillOnce(Invoke(ret));
683 
684     // run test
685     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_2);
686 
687     // verify failure
688     EXPECT_EQ(nullptr, device.get());
689 }
690 
TEST_F(VersionedIDeviceInitializationTest,getNumberOfCacheFilesNeededFailure)691 TEST_F(VersionedIDeviceInitializationTest, getNumberOfCacheFilesNeededFailure) {
692     // setup failure
693     const auto ret = makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE, kMaxNumberOfCacheFiles,
694                                         kMaxNumberOfCacheFiles);
695     EXPECT_CALL(*kMockDevice, getNumberOfCacheFilesNeeded(_)).Times(1).WillOnce(Invoke(ret));
696 
697     // run test
698     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_2);
699 
700     // verify failure
701     EXPECT_EQ(nullptr, device.get());
702 }
703 
TEST_F(VersionedIDeviceInitializationTest,dataCacheFilesExceedsSpecifiedMax)704 TEST_F(VersionedIDeviceInitializationTest, dataCacheFilesExceedsSpecifiedMax) {
705     // setup failure
706     const auto ret = makeCallbackReturn(V1_0::ErrorStatus::NONE, kMaxNumberOfCacheFiles + 1,
707                                         kMaxNumberOfCacheFiles);
708     EXPECT_CALL(*kMockDevice, getNumberOfCacheFilesNeeded(_)).Times(1).WillOnce(Invoke(ret));
709 
710     // run test
711     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_2);
712 
713     // verify failure
714     EXPECT_EQ(nullptr, device.get());
715 }
716 
TEST_F(VersionedIDeviceInitializationTest,modelCacheFilesExceedsSpecifiedMax)717 TEST_F(VersionedIDeviceInitializationTest, modelCacheFilesExceedsSpecifiedMax) {
718     // setup failure
719     const auto ret = makeCallbackReturn(V1_0::ErrorStatus::NONE, kMaxNumberOfCacheFiles,
720                                         kMaxNumberOfCacheFiles + 1);
721     EXPECT_CALL(*kMockDevice, getNumberOfCacheFilesNeeded(_)).Times(1).WillOnce(Invoke(ret));
722 
723     // run test
724     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_2);
725 
726     // verify failure
727     EXPECT_EQ(nullptr, device.get());
728 }
729 
TEST_F(VersionedIDeviceInitializationTest,getCapabilitiesTransportFailure)730 TEST_F(VersionedIDeviceInitializationTest, getCapabilitiesTransportFailure) {
731     // setup failure
732     EXPECT_CALL(*kMockDevice, getCapabilities(_))
733             .Times(1)
734             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
735 
736     // run test
737     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_0);
738 
739     // verify failure
740     EXPECT_EQ(nullptr, device.get());
741 }
742 
TEST_F(VersionedIDeviceInitializationTest,getCapabilities_1_1TransportFailure)743 TEST_F(VersionedIDeviceInitializationTest, getCapabilities_1_1TransportFailure) {
744     // setup failure
745     EXPECT_CALL(*kMockDevice, getCapabilities_1_1(_))
746             .Times(1)
747             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
748 
749     // run test
750     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_1);
751 
752     // verify failure
753     EXPECT_EQ(nullptr, device.get());
754 }
755 
TEST_F(VersionedIDeviceInitializationTest,getCapabilities_1_2TransportFailure)756 TEST_F(VersionedIDeviceInitializationTest, getCapabilities_1_2TransportFailure) {
757     // setup failure
758     EXPECT_CALL(*kMockDevice, getCapabilities_1_2(_))
759             .Times(1)
760             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
761 
762     // run test
763     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_2);
764 
765     // verify failure
766     EXPECT_EQ(nullptr, device.get());
767 }
768 
TEST_F(VersionedIDeviceInitializationTest,getCapabilities_1_3TransportFailure)769 TEST_F(VersionedIDeviceInitializationTest, getCapabilities_1_3TransportFailure) {
770     // setup failure
771     EXPECT_CALL(*kMockDevice, getCapabilities_1_3(_))
772             .Times(1)
773             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
774 
775     // run test
776     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_3);
777 
778     // verify failure
779     EXPECT_EQ(nullptr, device.get());
780 }
781 
TEST_F(VersionedIDeviceInitializationTest,getVersionStringTransportFailure)782 TEST_F(VersionedIDeviceInitializationTest, getVersionStringTransportFailure) {
783     // setup failure
784     EXPECT_CALL(*kMockDevice, getVersionString(_))
785             .Times(1)
786             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
787 
788     // run test
789     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_2);
790 
791     // verify failure
792     EXPECT_EQ(nullptr, device.get());
793 }
794 
TEST_F(VersionedIDeviceInitializationTest,getTypeTransportFailure)795 TEST_F(VersionedIDeviceInitializationTest, getTypeTransportFailure) {
796     // setup failure
797     EXPECT_CALL(*kMockDevice, getType(_))
798             .Times(1)
799             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
800 
801     // run test
802     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_2);
803 
804     // verify failure
805     EXPECT_EQ(nullptr, device.get());
806 }
807 
TEST_F(VersionedIDeviceInitializationTest,getSupportedExtensionsTransportFailure)808 TEST_F(VersionedIDeviceInitializationTest, getSupportedExtensionsTransportFailure) {
809     // setup failure
810     EXPECT_CALL(*kMockDevice, getSupportedExtensions(_))
811             .Times(1)
812             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
813 
814     // run test
815     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_2);
816 
817     // verify failure
818     EXPECT_EQ(nullptr, device.get());
819 }
820 
TEST_F(VersionedIDeviceInitializationTest,getNumberOfCacheFilesNeededTransportFailure)821 TEST_F(VersionedIDeviceInitializationTest, getNumberOfCacheFilesNeededTransportFailure) {
822     // setup failure
823     EXPECT_CALL(*kMockDevice, getNumberOfCacheFilesNeeded(_))
824             .Times(1)
825             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
826 
827     // run test
828     const auto device = makeVersionedIDeviceFrom(kMockDevice, kMockMakeDevice.get(), Version::V1_2);
829 
830     // verify failure
831     EXPECT_EQ(nullptr, device.get());
832 }
833 
834 // Ensure device has cached metadata
835 
TEST_F(VersionedIDeviceV1_0Test,getCapabilities)836 TEST_F(VersionedIDeviceV1_0Test, getCapabilities) {
837     // run test
838     const auto capabilities = kDevice->getCapabilities();
839     const auto cached = kDevice->getCapabilities();
840 
841     // verify success
842     EXPECT_EQ(PerformanceInfo{}, capabilities.relaxedFloat32toFloat16PerformanceScalar);
843     EXPECT_EQ(PerformanceInfo{}, capabilities.relaxedFloat32toFloat16PerformanceTensor);
844     EXPECT_LT(0u, capabilities.operandPerformance.size());
845     EXPECT_EQ(cached, capabilities);
846 }
847 
TEST_F(VersionedIDeviceV1_1Test,getCapabilities)848 TEST_F(VersionedIDeviceV1_1Test, getCapabilities) {
849     // run test
850     const auto capabilities = kDevice->getCapabilities();
851     const auto cached = kDevice->getCapabilities();
852 
853     // verify success
854     EXPECT_EQ(PerformanceInfo{}, capabilities.relaxedFloat32toFloat16PerformanceScalar);
855     EXPECT_EQ(PerformanceInfo{}, capabilities.relaxedFloat32toFloat16PerformanceTensor);
856     EXPECT_LT(0u, capabilities.operandPerformance.size());
857     EXPECT_EQ(cached, capabilities);
858 }
859 
TEST_F(VersionedIDeviceV1_2Test,getCapabilities)860 TEST_F(VersionedIDeviceV1_2Test, getCapabilities) {
861     // run test
862     const auto capabilities = kDevice->getCapabilities();
863     const auto cached = kDevice->getCapabilities();
864 
865     // verify success
866     EXPECT_EQ(PerformanceInfo{}, capabilities.relaxedFloat32toFloat16PerformanceScalar);
867     EXPECT_EQ(PerformanceInfo{}, capabilities.relaxedFloat32toFloat16PerformanceTensor);
868     EXPECT_EQ(0u, capabilities.operandPerformance.size());
869     EXPECT_EQ(cached, capabilities);
870 }
871 
TEST_F(VersionedIDeviceV1_3Test,getCapabilities)872 TEST_F(VersionedIDeviceV1_3Test, getCapabilities) {
873     // run test
874     const auto capabilities = kDevice->getCapabilities();
875     const auto cached = kDevice->getCapabilities();
876 
877     // verify success
878     EXPECT_EQ(PerformanceInfo{}, capabilities.relaxedFloat32toFloat16PerformanceScalar);
879     EXPECT_EQ(PerformanceInfo{}, capabilities.relaxedFloat32toFloat16PerformanceTensor);
880     EXPECT_EQ(0u, capabilities.operandPerformance.size());
881     EXPECT_EQ(cached, capabilities);
882 }
883 
TEST_F(VersionedIDeviceV1_0Test,getVersionString)884 TEST_F(VersionedIDeviceV1_0Test, getVersionString) {
885     // run test
886     const auto versionString = kDevice->getVersionString();
887     const auto cached = kDevice->getVersionString();
888 
889     // verify success
890     EXPECT_EQ("UNKNOWN", versionString);
891     EXPECT_EQ(cached, versionString);
892 }
893 
TEST_F(VersionedIDeviceV1_1Test,getVersionString)894 TEST_F(VersionedIDeviceV1_1Test, getVersionString) {
895     // run test
896     const auto versionString = kDevice->getVersionString();
897     const auto cached = kDevice->getVersionString();
898 
899     // verify success
900     EXPECT_EQ("UNKNOWN", versionString);
901     EXPECT_EQ(cached, versionString);
902 }
903 
TEST_F(VersionedIDeviceV1_2Test,getVersionString)904 TEST_F(VersionedIDeviceV1_2Test, getVersionString) {
905     // run test
906     const auto versionString = kDevice->getVersionString();
907     const auto cached = kDevice->getVersionString();
908 
909     // verify success
910     EXPECT_EQ("Google-MockV1", versionString);
911     EXPECT_EQ(cached, versionString);
912 }
913 
TEST_F(VersionedIDeviceV1_3Test,getVersionString)914 TEST_F(VersionedIDeviceV1_3Test, getVersionString) {
915     // run test
916     const auto versionString = kDevice->getVersionString();
917     const auto cached = kDevice->getVersionString();
918 
919     // verify success
920     EXPECT_EQ("Google-MockV1", versionString);
921     EXPECT_EQ(cached, versionString);
922 }
923 
TEST_F(VersionedIDeviceV1_0Test,getType)924 TEST_F(VersionedIDeviceV1_0Test, getType) {
925     // run test
926     const auto type = kDevice->getType();
927     const auto cached = kDevice->getType();
928 
929     // verify success
930     EXPECT_EQ(ANEURALNETWORKS_DEVICE_UNKNOWN, type);
931     EXPECT_EQ(cached, type);
932 }
933 
TEST_F(VersionedIDeviceV1_1Test,getType)934 TEST_F(VersionedIDeviceV1_1Test, getType) {
935     // run test
936     const auto type = kDevice->getType();
937     const auto cached = kDevice->getType();
938 
939     // verify success
940     EXPECT_EQ(ANEURALNETWORKS_DEVICE_UNKNOWN, type);
941     EXPECT_EQ(cached, type);
942 }
943 
TEST_F(VersionedIDeviceV1_2Test,getType)944 TEST_F(VersionedIDeviceV1_2Test, getType) {
945     // run test
946     const auto type = kDevice->getType();
947     const auto cached = kDevice->getType();
948 
949     // verify success
950     EXPECT_EQ(ANEURALNETWORKS_DEVICE_OTHER, type);
951     EXPECT_EQ(cached, type);
952 }
953 
TEST_F(VersionedIDeviceV1_3Test,getType)954 TEST_F(VersionedIDeviceV1_3Test, getType) {
955     // run test
956     const auto type = kDevice->getType();
957     const auto cached = kDevice->getType();
958 
959     // verify success
960     EXPECT_EQ(ANEURALNETWORKS_DEVICE_OTHER, type);
961     EXPECT_EQ(cached, type);
962 }
963 
TEST_F(VersionedIDeviceV1_0Test,getSupportedExtensions)964 TEST_F(VersionedIDeviceV1_0Test, getSupportedExtensions) {
965     // run test
966     const auto supportedExtensions = kDevice->getSupportedExtensions();
967     const auto cached = kDevice->getSupportedExtensions();
968 
969     // verify success
970     EXPECT_EQ(0u, supportedExtensions.size());
971     EXPECT_EQ(cached, supportedExtensions);
972 }
973 
TEST_F(VersionedIDeviceV1_1Test,getSupportedExtensions)974 TEST_F(VersionedIDeviceV1_1Test, getSupportedExtensions) {
975     // run test
976     const auto supportedExtensions = kDevice->getSupportedExtensions();
977     const auto cached = kDevice->getSupportedExtensions();
978 
979     // verify success
980     EXPECT_EQ(0u, supportedExtensions.size());
981     EXPECT_EQ(cached, supportedExtensions);
982 }
983 
TEST_F(VersionedIDeviceV1_2Test,getSupportedExtensions)984 TEST_F(VersionedIDeviceV1_2Test, getSupportedExtensions) {
985     // run test
986     const auto supportedExtensions = kDevice->getSupportedExtensions();
987     const auto cached = kDevice->getSupportedExtensions();
988 
989     // verify success
990     EXPECT_EQ(0u, supportedExtensions.size());
991     EXPECT_EQ(cached, supportedExtensions);
992 }
993 
TEST_F(VersionedIDeviceV1_3Test,getSupportedExtensions)994 TEST_F(VersionedIDeviceV1_3Test, getSupportedExtensions) {
995     // run test
996     const auto supportedExtensions = kDevice->getSupportedExtensions();
997     const auto cached = kDevice->getSupportedExtensions();
998 
999     // verify success
1000     EXPECT_EQ(0u, supportedExtensions.size());
1001     EXPECT_EQ(cached, supportedExtensions);
1002 }
1003 
TEST_F(VersionedIDeviceV1_0Test,getNumberOfCacheFilesNeeded)1004 TEST_F(VersionedIDeviceV1_0Test, getNumberOfCacheFilesNeeded) {
1005     // run test
1006     const auto [dataCacheFilesNeeded, modelCacheFilesNeeded] =
1007             kDevice->getNumberOfCacheFilesNeeded();
1008     const auto [cachedDataCacheFilesNeeded, cachedModelCacheFilesNeeded] =
1009             kDevice->getNumberOfCacheFilesNeeded();
1010 
1011     // verify success
1012     EXPECT_EQ(kNoCacheFilesNeeded, dataCacheFilesNeeded);
1013     EXPECT_EQ(kNoCacheFilesNeeded, modelCacheFilesNeeded);
1014     EXPECT_EQ(cachedDataCacheFilesNeeded, dataCacheFilesNeeded);
1015     EXPECT_EQ(cachedModelCacheFilesNeeded, modelCacheFilesNeeded);
1016 }
1017 
TEST_F(VersionedIDeviceV1_1Test,getNumberOfCacheFilesNeeded)1018 TEST_F(VersionedIDeviceV1_1Test, getNumberOfCacheFilesNeeded) {
1019     // run test
1020     const auto [dataCacheFilesNeeded, modelCacheFilesNeeded] =
1021             kDevice->getNumberOfCacheFilesNeeded();
1022     const auto [cachedDataCacheFilesNeeded, cachedModelCacheFilesNeeded] =
1023             kDevice->getNumberOfCacheFilesNeeded();
1024 
1025     // verify success
1026     EXPECT_EQ(kNoCacheFilesNeeded, dataCacheFilesNeeded);
1027     EXPECT_EQ(kNoCacheFilesNeeded, modelCacheFilesNeeded);
1028     EXPECT_EQ(cachedDataCacheFilesNeeded, dataCacheFilesNeeded);
1029     EXPECT_EQ(cachedModelCacheFilesNeeded, modelCacheFilesNeeded);
1030 }
1031 
TEST_F(VersionedIDeviceV1_2Test,getNumberOfCacheFilesNeeded)1032 TEST_F(VersionedIDeviceV1_2Test, getNumberOfCacheFilesNeeded) {
1033     // run test
1034     const auto [dataCacheFilesNeeded, modelCacheFilesNeeded] =
1035             kDevice->getNumberOfCacheFilesNeeded();
1036     const auto [cachedDataCacheFilesNeeded, cachedModelCacheFilesNeeded] =
1037             kDevice->getNumberOfCacheFilesNeeded();
1038 
1039     // verify success
1040     EXPECT_EQ(kMaxNumberOfCacheFiles, dataCacheFilesNeeded);
1041     EXPECT_EQ(kMaxNumberOfCacheFiles, modelCacheFilesNeeded);
1042     EXPECT_EQ(cachedDataCacheFilesNeeded, dataCacheFilesNeeded);
1043     EXPECT_EQ(cachedModelCacheFilesNeeded, modelCacheFilesNeeded);
1044 }
1045 
TEST_F(VersionedIDeviceV1_3Test,getNumberOfCacheFilesNeeded)1046 TEST_F(VersionedIDeviceV1_3Test, getNumberOfCacheFilesNeeded) {
1047     // run test
1048     const auto [dataCacheFilesNeeded, modelCacheFilesNeeded] =
1049             kDevice->getNumberOfCacheFilesNeeded();
1050     const auto [cachedDataCacheFilesNeeded, cachedModelCacheFilesNeeded] =
1051             kDevice->getNumberOfCacheFilesNeeded();
1052 
1053     // verify success
1054     EXPECT_EQ(kMaxNumberOfCacheFiles, dataCacheFilesNeeded);
1055     EXPECT_EQ(kMaxNumberOfCacheFiles, modelCacheFilesNeeded);
1056     EXPECT_EQ(cachedDataCacheFilesNeeded, dataCacheFilesNeeded);
1057     EXPECT_EQ(cachedModelCacheFilesNeeded, modelCacheFilesNeeded);
1058 }
1059 
TEST_F(VersionedIDeviceV1_0Test,getFeatureLevel)1060 TEST_F(VersionedIDeviceV1_0Test, getFeatureLevel) {
1061     // run test
1062     const auto featureLevel = kDevice->getFeatureLevel();
1063     const auto cached = kDevice->getFeatureLevel();
1064 
1065     // verify success
1066     constexpr int64_t expectedFeatureLevel = __ANDROID_API_O_MR1__;
1067     EXPECT_EQ(expectedFeatureLevel, featureLevel);
1068     EXPECT_EQ(cached, featureLevel);
1069 }
1070 
TEST_F(VersionedIDeviceV1_1Test,getFeatureLevel)1071 TEST_F(VersionedIDeviceV1_1Test, getFeatureLevel) {
1072     // run test
1073     const auto featureLevel = kDevice->getFeatureLevel();
1074     const auto cached = kDevice->getFeatureLevel();
1075 
1076     // verify success
1077     constexpr int64_t expectedFeatureLevel = __ANDROID_API_P__;
1078     EXPECT_EQ(expectedFeatureLevel, featureLevel);
1079     EXPECT_EQ(cached, featureLevel);
1080 }
1081 
TEST_F(VersionedIDeviceV1_2Test,getFeatureLevel)1082 TEST_F(VersionedIDeviceV1_2Test, getFeatureLevel) {
1083     // run test
1084     const auto featureLevel = kDevice->getFeatureLevel();
1085     const auto cached = kDevice->getFeatureLevel();
1086 
1087     // verify success
1088     constexpr int64_t expectedFeatureLevel = __ANDROID_API_Q__;
1089     EXPECT_EQ(expectedFeatureLevel, featureLevel);
1090     EXPECT_EQ(cached, featureLevel);
1091 }
1092 
TEST_F(VersionedIDeviceV1_3Test,getFeatureLevel)1093 TEST_F(VersionedIDeviceV1_3Test, getFeatureLevel) {
1094     // run test
1095     const auto featureLevel = kDevice->getFeatureLevel();
1096     const auto cached = kDevice->getFeatureLevel();
1097 
1098     // verify success
1099     constexpr int64_t expectedFeatureLevel = __ANDROID_API_R__;
1100     EXPECT_EQ(expectedFeatureLevel, featureLevel);
1101     EXPECT_EQ(cached, featureLevel);
1102 }
1103 
1104 // Simulate successful test
1105 
TEST_F(VersionedIDeviceV1_0Test,getSupportedOperations)1106 TEST_F(VersionedIDeviceV1_0Test, getSupportedOperations) {
1107     // setup call
1108     const auto ret = [](const auto& /*model*/, const auto cb) {
1109         cb(V1_0::ErrorStatus::NONE, {});
1110         return Void();
1111     };
1112     EXPECT_CALL(*kMockDevice, getSupportedOperations(_, _)).Times(1).WillOnce(Invoke(ret));
1113 
1114     // run test
1115     const auto metaModel = MetaModel({}, /*strictSlicing=*/true);
1116     const auto [resultCode, supportedOperations] = kDevice->getSupportedOperations(metaModel);
1117 
1118     // verify success
1119     EXPECT_EQ(V1_3::ErrorStatus::NONE, resultCode);
1120     EXPECT_EQ(0u, supportedOperations.size());
1121 }
1122 
TEST_F(VersionedIDeviceV1_1Test,getSupportedOperations)1123 TEST_F(VersionedIDeviceV1_1Test, getSupportedOperations) {
1124     // setup call
1125     const auto ret = [](const auto& /*model*/, const auto cb) {
1126         cb(V1_0::ErrorStatus::NONE, {});
1127         return Void();
1128     };
1129     EXPECT_CALL(*kMockDevice, getSupportedOperations_1_1(_, _)).Times(1).WillOnce(Invoke(ret));
1130 
1131     // run test
1132     const auto metaModel = MetaModel({}, /*strictSlicing=*/true);
1133     const auto [resultCode, supportedOperations] = kDevice->getSupportedOperations(metaModel);
1134 
1135     // verify success
1136     EXPECT_EQ(V1_3::ErrorStatus::NONE, resultCode);
1137     EXPECT_EQ(0u, supportedOperations.size());
1138 }
1139 
TEST_F(VersionedIDeviceV1_2Test,getSupportedOperations)1140 TEST_F(VersionedIDeviceV1_2Test, getSupportedOperations) {
1141     // setup call
1142     const auto ret = [](const auto& /*model*/, const auto cb) {
1143         cb(V1_0::ErrorStatus::NONE, {});
1144         return Void();
1145     };
1146     EXPECT_CALL(*kMockDevice, getSupportedOperations_1_2(_, _)).Times(1).WillOnce(Invoke(ret));
1147 
1148     // run test
1149     const auto metaModel = MetaModel({}, /*strictSlicing=*/true);
1150     const auto [resultCode, supportedOperations] = kDevice->getSupportedOperations(metaModel);
1151 
1152     // verify success
1153     EXPECT_EQ(V1_3::ErrorStatus::NONE, resultCode);
1154     EXPECT_EQ(0u, supportedOperations.size());
1155 }
1156 
TEST_F(VersionedIDeviceV1_3Test,getSupportedOperations)1157 TEST_F(VersionedIDeviceV1_3Test, getSupportedOperations) {
1158     // setup call
1159     const auto ret = [](const auto& /*model*/, const auto cb) {
1160         cb(V1_3::ErrorStatus::NONE, {});
1161         return Void();
1162     };
1163     EXPECT_CALL(*kMockDevice, getSupportedOperations_1_3(_, _)).Times(1).WillOnce(Invoke(ret));
1164 
1165     // run test
1166     const auto metaModel = MetaModel({}, /*strictSlicing=*/true);
1167     const auto [resultCode, supportedOperations] = kDevice->getSupportedOperations(metaModel);
1168 
1169     // verify success
1170     EXPECT_EQ(V1_3::ErrorStatus::NONE, resultCode);
1171     EXPECT_EQ(0u, supportedOperations.size());
1172 }
1173 
TEST_F(VersionedIDeviceV1_0Test,prepareModel)1174 TEST_F(VersionedIDeviceV1_0Test, prepareModel) {
1175     // setup call
1176     const sp<MockPreparedModel> mockPreparedModel = MockPreparedModel::create();
1177     const auto ret = makePreparedModelReturn(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE,
1178                                              mockPreparedModel);
1179     EXPECT_CALL(*kMockDevice, prepareModel(_, _)).Times(1).WillOnce(Invoke(ret));
1180 
1181     // run test
1182     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1183     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1184 
1185     // verify success
1186     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
1187     EXPECT_NE(nullptr, preparedModel.get());
1188 }
1189 
TEST_F(VersionedIDeviceV1_1Test,prepareModel)1190 TEST_F(VersionedIDeviceV1_1Test, prepareModel) {
1191     // setup call
1192     const sp<MockPreparedModel> mockPreparedModel = MockPreparedModel::create();
1193     const auto ret = makePreparedModel_1_1Return(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE,
1194                                                  mockPreparedModel);
1195     EXPECT_CALL(*kMockDevice, prepareModel_1_1(_, _, _)).Times(1).WillOnce(Invoke(ret));
1196 
1197     // run test
1198     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1199     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1200 
1201     // verify success
1202     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
1203     EXPECT_NE(nullptr, preparedModel.get());
1204 }
1205 
TEST_F(VersionedIDeviceV1_2Test,prepareModel)1206 TEST_F(VersionedIDeviceV1_2Test, prepareModel) {
1207     // setup call
1208     const sp<MockPreparedModel> mockPreparedModel = MockPreparedModel::create();
1209     const auto ret = makePreparedModel_1_2Return(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE,
1210                                                  mockPreparedModel);
1211     EXPECT_CALL(*kMockDevice, prepareModel_1_2(_, _, _, _, _, _)).Times(1).WillOnce(Invoke(ret));
1212 
1213     // run test
1214     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1215     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1216 
1217     // verify success
1218     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
1219     EXPECT_NE(nullptr, preparedModel.get());
1220 }
1221 
TEST_F(VersionedIDeviceV1_3Test,prepareModel)1222 TEST_F(VersionedIDeviceV1_3Test, prepareModel) {
1223     // setup call
1224     const sp<MockPreparedModel> mockPreparedModel = MockPreparedModel::create();
1225     const auto ret = makePreparedModel_1_3Return(V1_3::ErrorStatus::NONE, V1_3::ErrorStatus::NONE,
1226                                                  mockPreparedModel);
1227     EXPECT_CALL(*kMockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
1228             .Times(1)
1229             .WillOnce(Invoke(ret));
1230 
1231     // run test
1232     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1233     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1234 
1235     // verify success
1236     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
1237     EXPECT_NE(nullptr, preparedModel.get());
1238 }
1239 
TEST_F(VersionedIDeviceV1_0Test,allocate)1240 TEST_F(VersionedIDeviceV1_0Test, allocate) {
1241     // run test
1242     const auto [status, buffer, token] = kDevice->allocate({}, {}, {}, {});
1243 
1244     // verify success
1245     EXPECT_EQ(V1_3::ErrorStatus::GENERAL_FAILURE, status);
1246     EXPECT_EQ(nullptr, buffer.get());
1247     EXPECT_EQ(0u, token);
1248 }
1249 
TEST_F(VersionedIDeviceV1_1Test,allocate)1250 TEST_F(VersionedIDeviceV1_1Test, allocate) {
1251     // run test
1252     const auto [status, buffer, token] = kDevice->allocate({}, {}, {}, {});
1253 
1254     // verify success
1255     EXPECT_EQ(V1_3::ErrorStatus::GENERAL_FAILURE, status);
1256     EXPECT_EQ(nullptr, buffer.get());
1257     EXPECT_EQ(0u, token);
1258 }
1259 
TEST_F(VersionedIDeviceV1_2Test,allocate)1260 TEST_F(VersionedIDeviceV1_2Test, allocate) {
1261     // run test
1262     const auto [status, buffer, token] = kDevice->allocate({}, {}, {}, {});
1263 
1264     // verify success
1265     EXPECT_EQ(V1_3::ErrorStatus::GENERAL_FAILURE, status);
1266     EXPECT_EQ(nullptr, buffer.get());
1267     EXPECT_EQ(0u, token);
1268 }
1269 
TEST_F(VersionedIDeviceV1_3Test,allocate)1270 TEST_F(VersionedIDeviceV1_3Test, allocate) {
1271     // setup call
1272     const sp<MockBuffer> mockBuffer = new MockBuffer();
1273     constexpr uint32_t mockToken = 1;
1274     const auto ret = [mockBuffer](const BufferDesc& /*desc*/,
1275                                   const hidl_vec<sp<V1_3::IPreparedModel>>& /*preparedModels*/,
1276                                   const hidl_vec<BufferRole>& /*inputRoles*/,
1277                                   const hidl_vec<BufferRole>& /*outputRoles*/,
1278                                   V1_3::IDevice::allocate_cb cb) -> Return<void> {
1279         cb(V1_3::ErrorStatus::NONE, mockBuffer, mockToken);
1280         return Void();
1281     };
1282     EXPECT_CALL(*kMockDevice, allocate(_, _, _, _, _)).Times(1).WillOnce(Invoke(ret));
1283 
1284     // run test
1285     const auto [status, buffer, token] = kDevice->allocate({}, {}, {}, {});
1286 
1287     // verify success
1288     EXPECT_EQ(V1_3::ErrorStatus::NONE, status);
1289     EXPECT_NE(nullptr, buffer.get());
1290     EXPECT_NE(0u, token);
1291 }
1292 
TEST_F(VersionedIDeviceMockTest,wait)1293 TEST_F(VersionedIDeviceMockTest, wait) {
1294     // setup call
1295     const auto ret = []() -> Return<void> { return {}; };
1296     EXPECT_CALL(*kMockDevice, ping()).Times(1).WillOnce(Invoke(ret));
1297 
1298     // run test
1299     const auto resultCode = kDevice->wait();
1300 
1301     // verify success
1302     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
1303 }
1304 
1305 // Simulate general failure
1306 
TEST_F(VersionedIDeviceV1_0Test,getSupportedOperationsFailure)1307 TEST_F(VersionedIDeviceV1_0Test, getSupportedOperationsFailure) {
1308     // setup failure
1309     const auto ret = [](const auto& /*model*/, const auto cb) {
1310         cb(V1_0::ErrorStatus::GENERAL_FAILURE, {});
1311         return Void();
1312     };
1313     EXPECT_CALL(*kMockDevice, getSupportedOperations(_, _)).Times(1).WillOnce(Invoke(ret));
1314 
1315     // run test
1316     const auto metaModel = MetaModel({}, /*strictSlicing=*/true);
1317     const auto [resultCode, supportedOperations] = kDevice->getSupportedOperations(metaModel);
1318 
1319     // verify failure
1320     EXPECT_EQ(V1_3::ErrorStatus::GENERAL_FAILURE, resultCode);
1321     EXPECT_EQ(0u, supportedOperations.size());
1322 }
1323 
TEST_F(VersionedIDeviceV1_1Test,getSupportedOperationsFailure)1324 TEST_F(VersionedIDeviceV1_1Test, getSupportedOperationsFailure) {
1325     // setup failure
1326     const auto ret = [](const auto& /*model*/, const auto cb) {
1327         cb(V1_0::ErrorStatus::GENERAL_FAILURE, {});
1328         return Void();
1329     };
1330     EXPECT_CALL(*kMockDevice, getSupportedOperations_1_1(_, _)).Times(1).WillOnce(Invoke(ret));
1331 
1332     // run test
1333     const auto metaModel = MetaModel({}, /*strictSlicing=*/true);
1334     const auto [resultCode, supportedOperations] = kDevice->getSupportedOperations(metaModel);
1335 
1336     // verify failure
1337     EXPECT_EQ(V1_3::ErrorStatus::GENERAL_FAILURE, resultCode);
1338     EXPECT_EQ(0u, supportedOperations.size());
1339 }
1340 
TEST_F(VersionedIDeviceV1_2Test,getSupportedOperationsFailure)1341 TEST_F(VersionedIDeviceV1_2Test, getSupportedOperationsFailure) {
1342     // setup failure
1343     const auto ret = [](const auto& /*model*/, const auto cb) {
1344         cb(V1_0::ErrorStatus::GENERAL_FAILURE, {});
1345         return Void();
1346     };
1347     EXPECT_CALL(*kMockDevice, getSupportedOperations_1_2(_, _)).Times(1).WillOnce(Invoke(ret));
1348 
1349     // run test
1350     const auto metaModel = MetaModel({}, /*strictSlicing=*/true);
1351     const auto [resultCode, supportedOperations] = kDevice->getSupportedOperations(metaModel);
1352 
1353     // verify failure
1354     EXPECT_EQ(V1_3::ErrorStatus::GENERAL_FAILURE, resultCode);
1355     EXPECT_EQ(0u, supportedOperations.size());
1356 }
1357 
TEST_F(VersionedIDeviceV1_3Test,getSupportedOperationsFailure)1358 TEST_F(VersionedIDeviceV1_3Test, getSupportedOperationsFailure) {
1359     // setup failure
1360     const auto ret = [](const auto& /*model*/, const auto cb) {
1361         cb(V1_3::ErrorStatus::GENERAL_FAILURE, {});
1362         return Void();
1363     };
1364     EXPECT_CALL(*kMockDevice, getSupportedOperations_1_3(_, _)).Times(1).WillOnce(Invoke(ret));
1365 
1366     // run test
1367     const auto metaModel = MetaModel({}, /*strictSlicing=*/true);
1368     const auto [resultCode, supportedOperations] = kDevice->getSupportedOperations(metaModel);
1369 
1370     // verify failure
1371     EXPECT_EQ(V1_3::ErrorStatus::GENERAL_FAILURE, resultCode);
1372     EXPECT_EQ(0u, supportedOperations.size());
1373 }
1374 
TEST_F(VersionedIDeviceV1_0Test,prepareModelLaunchFailure)1375 TEST_F(VersionedIDeviceV1_0Test, prepareModelLaunchFailure) {
1376     // setup failure
1377     const sp<MockPreparedModel> mockPreparedModel = MockPreparedModel::create();
1378     const auto ret = makePreparedModelReturn(V1_0::ErrorStatus::GENERAL_FAILURE,
1379                                              V1_0::ErrorStatus::NONE, mockPreparedModel);
1380     EXPECT_CALL(*kMockDevice, prepareModel(_, _)).Times(1).WillOnce(Invoke(ret));
1381 
1382     // run test
1383     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1384     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1385 
1386     // verify failure
1387     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1388     EXPECT_EQ(nullptr, preparedModel.get());
1389 }
1390 
TEST_F(VersionedIDeviceV1_1Test,prepareModelLaunchFailure)1391 TEST_F(VersionedIDeviceV1_1Test, prepareModelLaunchFailure) {
1392     // setup failure
1393     const sp<MockPreparedModel> mockPreparedModel = MockPreparedModel::create();
1394     const auto ret = makePreparedModel_1_1Return(V1_0::ErrorStatus::GENERAL_FAILURE,
1395                                                  V1_0::ErrorStatus::NONE, mockPreparedModel);
1396     EXPECT_CALL(*kMockDevice, prepareModel_1_1(_, _, _)).Times(1).WillOnce(Invoke(ret));
1397 
1398     // run test
1399     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1400     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1401 
1402     // verify failure
1403     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1404     EXPECT_EQ(nullptr, preparedModel.get());
1405 }
1406 
TEST_F(VersionedIDeviceV1_2Test,prepareModelLaunchFailure)1407 TEST_F(VersionedIDeviceV1_2Test, prepareModelLaunchFailure) {
1408     // setup failure
1409     const sp<MockPreparedModel> mockPreparedModel = MockPreparedModel::create();
1410     const auto ret = makePreparedModel_1_2Return(V1_0::ErrorStatus::GENERAL_FAILURE,
1411                                                  V1_0::ErrorStatus::NONE, mockPreparedModel);
1412     EXPECT_CALL(*kMockDevice, prepareModel_1_2(_, _, _, _, _, _)).Times(1).WillOnce(Invoke(ret));
1413 
1414     // run test
1415     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1416     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1417 
1418     // verify failure
1419     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1420     EXPECT_EQ(nullptr, preparedModel.get());
1421 }
1422 
TEST_F(VersionedIDeviceV1_3Test,prepareModelLaunchFailure)1423 TEST_F(VersionedIDeviceV1_3Test, prepareModelLaunchFailure) {
1424     // setup failure
1425     const sp<MockPreparedModel> mockPreparedModel = MockPreparedModel::create();
1426     const auto ret = makePreparedModel_1_3Return(V1_3::ErrorStatus::GENERAL_FAILURE,
1427                                                  V1_3::ErrorStatus::NONE, mockPreparedModel);
1428     EXPECT_CALL(*kMockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
1429             .Times(1)
1430             .WillOnce(Invoke(ret));
1431 
1432     // run test
1433     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1434     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1435 
1436     // verify failure
1437     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1438     EXPECT_EQ(nullptr, preparedModel.get());
1439 }
1440 
TEST_F(VersionedIDeviceV1_0Test,prepareModelReturnFailure)1441 TEST_F(VersionedIDeviceV1_0Test, prepareModelReturnFailure) {
1442     // setup failure
1443     const sp<MockPreparedModel> mockPreparedModel = MockPreparedModel::create();
1444     const auto ret = makePreparedModelReturn(V1_0::ErrorStatus::NONE,
1445                                              V1_0::ErrorStatus::GENERAL_FAILURE, mockPreparedModel);
1446     EXPECT_CALL(*kMockDevice, prepareModel(_, _)).Times(1).WillOnce(Invoke(ret));
1447 
1448     // run test
1449     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1450     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1451 
1452     // verify failure
1453     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1454     EXPECT_EQ(nullptr, preparedModel.get());
1455 }
1456 
TEST_F(VersionedIDeviceV1_1Test,prepareModelReturnFailure)1457 TEST_F(VersionedIDeviceV1_1Test, prepareModelReturnFailure) {
1458     // setup failure
1459     const sp<MockPreparedModel> mockPreparedModel = MockPreparedModel::create();
1460     const auto ret = makePreparedModel_1_1Return(
1461             V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::GENERAL_FAILURE, mockPreparedModel);
1462     EXPECT_CALL(*kMockDevice, prepareModel_1_1(_, _, _)).Times(1).WillOnce(Invoke(ret));
1463 
1464     // run test
1465     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1466     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1467 
1468     // verify failure
1469     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1470     EXPECT_EQ(nullptr, preparedModel.get());
1471 }
1472 
TEST_F(VersionedIDeviceV1_2Test,prepareModelReturnFailure)1473 TEST_F(VersionedIDeviceV1_2Test, prepareModelReturnFailure) {
1474     // setup failure
1475     const sp<MockPreparedModel> mockPreparedModel = MockPreparedModel::create();
1476     const auto ret = makePreparedModel_1_2Return(
1477             V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::GENERAL_FAILURE, mockPreparedModel);
1478     EXPECT_CALL(*kMockDevice, prepareModel_1_2(_, _, _, _, _, _)).Times(1).WillOnce(Invoke(ret));
1479 
1480     // run test
1481     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1482     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1483 
1484     // verify failure
1485     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1486     EXPECT_EQ(nullptr, preparedModel.get());
1487 }
1488 
TEST_F(VersionedIDeviceV1_3Test,prepareModelReturnFailure)1489 TEST_F(VersionedIDeviceV1_3Test, prepareModelReturnFailure) {
1490     // setup failure
1491     const sp<MockPreparedModel> mockPreparedModel = MockPreparedModel::create();
1492     const auto ret = makePreparedModel_1_3Return(
1493             V1_3::ErrorStatus::NONE, V1_3::ErrorStatus::GENERAL_FAILURE, mockPreparedModel);
1494     EXPECT_CALL(*kMockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
1495             .Times(1)
1496             .WillOnce(Invoke(ret));
1497 
1498     // run test
1499     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1500     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1501 
1502     // verify failure
1503     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1504     EXPECT_EQ(nullptr, preparedModel.get());
1505 }
1506 
TEST_F(VersionedIDeviceV1_0Test,prepareModelNullptrError)1507 TEST_F(VersionedIDeviceV1_0Test, prepareModelNullptrError) {
1508     // setup failure
1509     const sp<MockPreparedModel> mockPreparedModel = nullptr;
1510     const auto ret = makePreparedModelReturn(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE,
1511                                              mockPreparedModel);
1512     EXPECT_CALL(*kMockDevice, prepareModel(_, _)).Times(1).WillOnce(Invoke(ret));
1513 
1514     // run test
1515     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1516     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1517 
1518     // verify failure
1519     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1520     EXPECT_EQ(nullptr, preparedModel.get());
1521 }
1522 
TEST_F(VersionedIDeviceV1_1Test,prepareModelNullptrError)1523 TEST_F(VersionedIDeviceV1_1Test, prepareModelNullptrError) {
1524     // setup failure
1525     const sp<MockPreparedModel> mockPreparedModel = nullptr;
1526     const auto ret = makePreparedModel_1_1Return(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE,
1527                                                  mockPreparedModel);
1528     EXPECT_CALL(*kMockDevice, prepareModel_1_1(_, _, _)).Times(1).WillOnce(Invoke(ret));
1529 
1530     // run test
1531     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1532     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1533 
1534     // verify failure
1535     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1536     EXPECT_EQ(nullptr, preparedModel.get());
1537 }
1538 
TEST_F(VersionedIDeviceV1_2Test,prepareModelNullptrError)1539 TEST_F(VersionedIDeviceV1_2Test, prepareModelNullptrError) {
1540     // setup failure
1541     const sp<MockPreparedModel> mockPreparedModel = nullptr;
1542     const auto ret = makePreparedModel_1_2Return(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE,
1543                                                  mockPreparedModel);
1544     EXPECT_CALL(*kMockDevice, prepareModel_1_2(_, _, _, _, _, _)).Times(1).WillOnce(Invoke(ret));
1545 
1546     // run test
1547     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1548     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1549 
1550     // verify failure
1551     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1552     EXPECT_EQ(nullptr, preparedModel.get());
1553 }
1554 
TEST_F(VersionedIDeviceV1_3Test,prepareModelNullptrError)1555 TEST_F(VersionedIDeviceV1_3Test, prepareModelNullptrError) {
1556     // setup failure
1557     const sp<MockPreparedModel> mockPreparedModel = nullptr;
1558     const auto ret = makePreparedModel_1_3Return(V1_3::ErrorStatus::NONE, V1_3::ErrorStatus::NONE,
1559                                                  mockPreparedModel);
1560     EXPECT_CALL(*kMockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
1561             .Times(1)
1562             .WillOnce(Invoke(ret));
1563 
1564     // run test
1565     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1566     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1567 
1568     // verify failure
1569     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1570     EXPECT_EQ(nullptr, preparedModel.get());
1571 }
1572 
TEST_F(VersionedIDeviceV1_3Test,allocateFailure)1573 TEST_F(VersionedIDeviceV1_3Test, allocateFailure) {
1574     // setup failure
1575     const auto ret = [](const BufferDesc& /*desc*/,
1576                         const hidl_vec<sp<V1_3::IPreparedModel>>& /*preparedModels*/,
1577                         const hidl_vec<BufferRole>& /*inputRoles*/,
1578                         const hidl_vec<BufferRole>& /*outputRoles*/,
1579                         V1_3::IDevice::allocate_cb cb) -> Return<void> {
1580         cb(V1_3::ErrorStatus::GENERAL_FAILURE, nullptr, 0);
1581         return Void();
1582     };
1583     EXPECT_CALL(*kMockDevice, allocate(_, _, _, _, _)).Times(1).WillOnce(Invoke(ret));
1584 
1585     // run test
1586     const auto [status, buffer, token] = kDevice->allocate({}, {}, {}, {});
1587 
1588     // verify failure
1589     EXPECT_EQ(V1_3::ErrorStatus::GENERAL_FAILURE, status);
1590     EXPECT_EQ(nullptr, buffer.get());
1591     EXPECT_EQ(0u, token);
1592 }
1593 
1594 // Simulate transport failure
1595 
TEST_F(VersionedIDeviceV1_0Test,getSupportedOperationsTransportFailure)1596 TEST_F(VersionedIDeviceV1_0Test, getSupportedOperationsTransportFailure) {
1597     // setup failure
1598     EXPECT_CALL(*kMockDevice, getSupportedOperations(_, _))
1599             .Times(1)
1600             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
1601 
1602     // run test
1603     const auto metaModel = MetaModel({}, /*strictSlicing=*/true);
1604     const auto [resultCode, supportedOperations] = kDevice->getSupportedOperations(metaModel);
1605 
1606     // verify failure
1607     EXPECT_EQ(V1_3::ErrorStatus::GENERAL_FAILURE, resultCode);
1608     EXPECT_EQ(0u, supportedOperations.size());
1609 }
1610 
TEST_F(VersionedIDeviceV1_1Test,getSupportedOperationsTransportFailure)1611 TEST_F(VersionedIDeviceV1_1Test, getSupportedOperationsTransportFailure) {
1612     // setup failure
1613     EXPECT_CALL(*kMockDevice, getSupportedOperations_1_1(_, _))
1614             .Times(1)
1615             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
1616 
1617     // run test
1618     const auto metaModel = MetaModel({}, /*strictSlicing=*/true);
1619     const auto [resultCode, supportedOperations] = kDevice->getSupportedOperations(metaModel);
1620 
1621     // verify failure
1622     EXPECT_EQ(V1_3::ErrorStatus::GENERAL_FAILURE, resultCode);
1623     EXPECT_EQ(0u, supportedOperations.size());
1624 }
1625 
TEST_F(VersionedIDeviceV1_2Test,getSupportedOperationsTransportFailure)1626 TEST_F(VersionedIDeviceV1_2Test, getSupportedOperationsTransportFailure) {
1627     // setup failure
1628     EXPECT_CALL(*kMockDevice, getSupportedOperations_1_2(_, _))
1629             .Times(1)
1630             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
1631 
1632     // run test
1633     const auto metaModel = MetaModel({}, /*strictSlicing=*/true);
1634     const auto [resultCode, supportedOperations] = kDevice->getSupportedOperations(metaModel);
1635 
1636     // verify failure
1637     EXPECT_EQ(V1_3::ErrorStatus::GENERAL_FAILURE, resultCode);
1638     EXPECT_EQ(0u, supportedOperations.size());
1639 }
1640 
TEST_F(VersionedIDeviceV1_3Test,getSupportedOperationsTransportFailure)1641 TEST_F(VersionedIDeviceV1_3Test, getSupportedOperationsTransportFailure) {
1642     // setup failure
1643     EXPECT_CALL(*kMockDevice, getSupportedOperations_1_3(_, _))
1644             .Times(1)
1645             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
1646 
1647     // run test
1648     const auto metaModel = MetaModel({}, /*strictSlicing=*/true);
1649     const auto [resultCode, supportedOperations] = kDevice->getSupportedOperations(metaModel);
1650 
1651     // verify failure
1652     EXPECT_EQ(V1_3::ErrorStatus::GENERAL_FAILURE, resultCode);
1653     EXPECT_EQ(0u, supportedOperations.size());
1654 }
1655 
TEST_F(VersionedIDeviceV1_0Test,prepareModelTransportFailure)1656 TEST_F(VersionedIDeviceV1_0Test, prepareModelTransportFailure) {
1657     // setup failure
1658     EXPECT_CALL(*kMockDevice, prepareModel(_, _))
1659             .Times(1)
1660             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
1661 
1662     // run test
1663     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1664     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1665 
1666     // verify failure
1667     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1668     EXPECT_EQ(nullptr, preparedModel.get());
1669 }
1670 
TEST_F(VersionedIDeviceV1_1Test,prepareModelTransportFailure)1671 TEST_F(VersionedIDeviceV1_1Test, prepareModelTransportFailure) {
1672     // setup failure
1673     EXPECT_CALL(*kMockDevice, prepareModel_1_1(_, _, _))
1674             .Times(1)
1675             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
1676 
1677     // run test
1678     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1679     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1680 
1681     // verify failure
1682     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1683     EXPECT_EQ(nullptr, preparedModel.get());
1684 }
1685 
TEST_F(VersionedIDeviceV1_2Test,prepareModelTransportFailure)1686 TEST_F(VersionedIDeviceV1_2Test, prepareModelTransportFailure) {
1687     // setup failure
1688     EXPECT_CALL(*kMockDevice, prepareModel_1_2(_, _, _, _, _, _))
1689             .Times(1)
1690             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
1691 
1692     // run test
1693     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1694     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1695 
1696     // verify failure
1697     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1698     EXPECT_EQ(nullptr, preparedModel.get());
1699 }
1700 
TEST_F(VersionedIDeviceV1_3Test,prepareModelTransportFailure)1701 TEST_F(VersionedIDeviceV1_3Test, prepareModelTransportFailure) {
1702     // setup failure
1703     EXPECT_CALL(*kMockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
1704             .Times(1)
1705             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
1706 
1707     // run test
1708     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1709     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1710 
1711     // verify failure
1712     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1713     EXPECT_EQ(nullptr, preparedModel.get());
1714 }
1715 
TEST_F(VersionedIDeviceV1_3Test,allocateTransportFailure)1716 TEST_F(VersionedIDeviceV1_3Test, allocateTransportFailure) {
1717     // setup failure
1718     EXPECT_CALL(*kMockDevice, allocate(_, _, _, _, _))
1719             .Times(1)
1720             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
1721 
1722     // run test
1723     const auto [status, buffer, token] = kDevice->allocate({}, {}, {}, {});
1724 
1725     // verify failure
1726     EXPECT_EQ(V1_3::ErrorStatus::GENERAL_FAILURE, status);
1727     EXPECT_EQ(nullptr, buffer.get());
1728     EXPECT_EQ(0u, token);
1729 }
1730 
TEST_F(VersionedIDeviceMockTest,waitTransportFailure)1731 TEST_F(VersionedIDeviceMockTest, waitTransportFailure) {
1732     // setup call
1733     EXPECT_CALL(*kMockDevice, ping())
1734             .Times(1)
1735             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
1736 
1737     // run test
1738     const auto resultCode = kDevice->wait();
1739 
1740     // verify success
1741     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1742 }
1743 
1744 // Simulate service crash
1745 
1746 // TODO: enable this test once b/154183300 is fixed.
TEST_F(VersionedIDeviceMockTest,DISABLED_prepareModelRecoverCrash)1747 TEST_F(VersionedIDeviceMockTest, DISABLED_prepareModelRecoverCrash) {
1748     // setup original device calls
1749     EXPECT_CALL(*kMockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
1750             .Times(1)
1751             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
1752     EXPECT_CALL(*kMockDevice, ping()).Times(1).WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
1753 
1754     // setup recovery call
1755     const sp<MockDevice> mockRecoveredDevice = MockDevice::create();
1756     EXPECT_CALL(*kMockMakeDevice, Call(/*blocking=*/false))
1757             .Times(1)
1758             .WillOnce(testing::Return(mockRecoveredDevice));
1759 
1760     // setup recovered device calls
1761     const sp<MockPreparedModel> mockPreparedModel = MockPreparedModel::create();
1762     const auto ret = makePreparedModel_1_3Return(V1_3::ErrorStatus::NONE, V1_3::ErrorStatus::NONE,
1763                                                  mockPreparedModel);
1764     EXPECT_CALL(*mockRecoveredDevice, linkToDeathRet()).Times(1);
1765     EXPECT_CALL(*mockRecoveredDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
1766             .Times(1)
1767             .WillOnce(Invoke(ret));
1768 
1769     // run test
1770     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1771     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1772 
1773     // verify success
1774     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
1775     EXPECT_NE(nullptr, preparedModel.get());
1776 }
1777 
TEST_F(VersionedIDeviceMockTest,prepareModelFullCrash)1778 TEST_F(VersionedIDeviceMockTest, prepareModelFullCrash) {
1779     // setup failure
1780     EXPECT_CALL(*kMockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
1781             .Times(1)
1782             .WillRepeatedly(InvokeWithoutArgs(makeDeadObjectFailure));
1783     EXPECT_CALL(*kMockDevice, ping())
1784             .Times(1)
1785             .WillRepeatedly(InvokeWithoutArgs(makeDeadObjectFailure));
1786     EXPECT_CALL(*kMockMakeDevice, Call(/*blocking=*/false))
1787             .Times(1)
1788             .WillOnce(testing::Return(nullptr));
1789 
1790     // run test
1791     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1792     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1793 
1794     // verify failure
1795     EXPECT_EQ(ANEURALNETWORKS_DEAD_OBJECT, resultCode);
1796     EXPECT_EQ(nullptr, preparedModel.get());
1797 }
1798 
TEST_F(VersionedIDeviceMockTest,prepareModelAsyncCrash)1799 TEST_F(VersionedIDeviceMockTest, prepareModelAsyncCrash) {
1800     // setup failure
1801     const auto ret = [this]() -> Return<V1_3::ErrorStatus> {
1802         kMockDevice->simulateCrash();
1803         return V1_3::ErrorStatus::NONE;
1804     };
1805     EXPECT_CALL(*kMockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
1806             .Times(1)
1807             .WillOnce(InvokeWithoutArgs(ret));
1808 
1809     // run test
1810     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1811     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1812 
1813     // verify failure
1814     EXPECT_EQ(ANEURALNETWORKS_DEAD_OBJECT, resultCode);
1815     EXPECT_EQ(nullptr, preparedModel.get());
1816 }
1817 
TEST_F(VersionedIDeviceMockTest,waitCrash)1818 TEST_F(VersionedIDeviceMockTest, waitCrash) {
1819     // setup failure
1820     EXPECT_CALL(*kMockDevice, ping())
1821             .Times(1)
1822             .WillRepeatedly(InvokeWithoutArgs(makeDeadObjectFailure));
1823     EXPECT_CALL(*kMockMakeDevice, Call(/*blocking=*/true))
1824             .Times(1)
1825             .WillOnce(testing::Return(nullptr));
1826 
1827     // run test
1828     const auto resultCode = kDevice->wait();
1829 
1830     // verify failure
1831     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1832 }
1833 
TEST_F(VersionedIDeviceMockTest,waitRecoverCrash)1834 TEST_F(VersionedIDeviceMockTest, waitRecoverCrash) {
1835     // setup original device calls
1836     EXPECT_CALL(*kMockDevice, ping()).Times(1).WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
1837 
1838     // setup recovery call
1839     const sp<MockDevice> mockRecoveredDevice = MockDevice::create();
1840     EXPECT_CALL(*kMockMakeDevice, Call(/*blocking=*/true))
1841             .Times(1)
1842             .WillOnce(testing::Return(mockRecoveredDevice));
1843 
1844     // setup recovered device calls
1845     const auto ret = []() -> Return<bool> { return true; };
1846     EXPECT_CALL(*mockRecoveredDevice, linkToDeathRet()).Times(1).WillOnce(Invoke(ret));
1847 
1848     // run test
1849     const auto resultCode = kDevice->wait();
1850 
1851     // verify success
1852     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
1853 }
1854 
TEST_F(VersionedIDeviceMockTest,waitFailedRecoverCrash)1855 TEST_F(VersionedIDeviceMockTest, waitFailedRecoverCrash) {
1856     // setup original device calls
1857     EXPECT_CALL(*kMockDevice, ping()).Times(1).WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
1858 
1859     // setup recovery call
1860     const sp<MockDevice> mockRecoveredDevice = MockDevice::create();
1861     EXPECT_CALL(*kMockMakeDevice, Call(/*blocking=*/true))
1862             .Times(1)
1863             .WillOnce(testing::Return(mockRecoveredDevice));
1864 
1865     // setup recovered device calls
1866     EXPECT_CALL(*mockRecoveredDevice, linkToDeathRet())
1867             .Times(1)
1868             .WillOnce(makeGeneralTransportFailure);
1869 
1870     // run test
1871     const auto resultCode = kDevice->wait();
1872 
1873     // verify failure
1874     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1875 }
1876 
1877 // Harness for VersionedIPreparedModel failures.
1878 
1879 class VersionedIPreparedModelInitializationTest : public VersionedIDeviceMockTest {
1880    protected:
1881     const sp<MockPreparedModel> kMockPreparedModel = MockPreparedModel::create();
1882 };
1883 
makeVersionedIPreparedModelSuccessfulInitializationFrom(const sp<MockDevice> & mockDevice,const sp<MockPreparedModel> & mockPreparedModel,const VersionedIDevice & device)1884 std::shared_ptr<VersionedIPreparedModel> makeVersionedIPreparedModelSuccessfulInitializationFrom(
1885         const sp<MockDevice>& mockDevice, const sp<MockPreparedModel>& mockPreparedModel,
1886         const VersionedIDevice& device) {
1887     const auto retV1_0 = makePreparedModelReturn(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE,
1888                                                  mockPreparedModel);
1889     const auto retV1_1 = makePreparedModel_1_1Return(V1_0::ErrorStatus::NONE,
1890                                                      V1_0::ErrorStatus::NONE, mockPreparedModel);
1891     const auto retV1_2 = makePreparedModel_1_2Return(V1_0::ErrorStatus::NONE,
1892                                                      V1_0::ErrorStatus::NONE, mockPreparedModel);
1893     const auto retV1_3 = makePreparedModel_1_3Return(V1_3::ErrorStatus::NONE,
1894                                                      V1_3::ErrorStatus::NONE, mockPreparedModel);
1895 
1896     ON_CALL(*mockDevice, prepareModel(_, _)).WillByDefault(Invoke(retV1_0));
1897     ON_CALL(*mockDevice, prepareModel_1_1(_, _, _)).WillByDefault(Invoke(retV1_1));
1898     ON_CALL(*mockDevice, prepareModel_1_2(_, _, _, _, _, _)).WillByDefault(Invoke(retV1_2));
1899     ON_CALL(*mockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _)).WillByDefault(Invoke(retV1_3));
1900 
1901     EXPECT_CALL(*mockDevice, prepareModel(_, _)).Times(testing::AnyNumber());
1902     EXPECT_CALL(*mockDevice, prepareModel_1_1(_, _, _)).Times(testing::AnyNumber());
1903     EXPECT_CALL(*mockDevice, prepareModel_1_2(_, _, _, _, _, _)).Times(testing::AnyNumber());
1904     EXPECT_CALL(*mockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _)).Times(testing::AnyNumber());
1905 
1906     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1907     const auto [resultCode, preparedModel] = device.prepareModel(makeModel, {}, {}, {}, {}, {});
1908 
1909     CHECK_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
1910     CHECK(preparedModel != nullptr);
1911 
1912     return preparedModel;
1913 }
1914 
1915 template <Version version>
1916 class VersionedIPreparedModelTest : public VersionedIDeviceInitializedTest<version> {
1917     using Base = VersionedIDeviceInitializedTest<version>;
1918 
1919    protected:
SetUp()1920     void SetUp() override {
1921         VersionedIDeviceInitializedTest<version>::SetUp();
1922         ASSERT_NE(nullptr, kPreparedModel.get());
1923     }
1924 
1925     const sp<MockPreparedModel> kMockPreparedModel = MockPreparedModel::create();
1926     const std::shared_ptr<VersionedIPreparedModel> kPreparedModel =
1927             makeVersionedIPreparedModelSuccessfulInitializationFrom(
1928                     Base::kMockDevice, kMockPreparedModel, *Base::kDevice);
1929 };
1930 
1931 class VersionedIPreparedModelV1_0Test : public VersionedIPreparedModelTest<Version::V1_0> {};
1932 class VersionedIPreparedModelV1_1Test : public VersionedIPreparedModelTest<Version::V1_1> {};
1933 class VersionedIPreparedModelV1_2Test : public VersionedIPreparedModelTest<Version::V1_2> {};
1934 class VersionedIPreparedModelV1_3Test : public VersionedIPreparedModelTest<Version::V1_3> {};
1935 class VersionedIPreparedModelMockTest : public VersionedIPreparedModelTest<Version::MOCK> {};
1936 
1937 // Simulate initialization/link error
1938 
TEST_F(VersionedIPreparedModelInitializationTest,linkToDeathTransportFailure)1939 TEST_F(VersionedIPreparedModelInitializationTest, linkToDeathTransportFailure) {
1940     // setup failure
1941     EXPECT_CALL(*kMockPreparedModel, linkToDeathRet())
1942             .Times(1)
1943             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
1944     const auto ret = makePreparedModel_1_3Return(V1_3::ErrorStatus::NONE, V1_3::ErrorStatus::NONE,
1945                                                  kMockPreparedModel);
1946     EXPECT_CALL(*kMockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
1947             .Times(1)
1948             .WillOnce(Invoke(ret));
1949 
1950     // run test
1951     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1952     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1953 
1954     // verify failure
1955     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1956     EXPECT_EQ(nullptr, preparedModel.get());
1957 }
1958 
TEST_F(VersionedIPreparedModelInitializationTest,linkToDeathDeadObject)1959 TEST_F(VersionedIPreparedModelInitializationTest, linkToDeathDeadObject) {
1960     // setup failure
1961     EXPECT_CALL(*kMockPreparedModel, linkToDeathRet())
1962             .Times(1)
1963             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
1964     const auto ret = makePreparedModel_1_3Return(V1_3::ErrorStatus::NONE, V1_3::ErrorStatus::NONE,
1965                                                  kMockPreparedModel);
1966     EXPECT_CALL(*kMockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
1967             .Times(1)
1968             .WillOnce(Invoke(ret));
1969 
1970     // run test
1971     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1972     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1973 
1974     // verify failure
1975     EXPECT_EQ(ANEURALNETWORKS_DEAD_OBJECT, resultCode);
1976     EXPECT_EQ(nullptr, preparedModel.get());
1977 }
1978 
TEST_F(VersionedIPreparedModelInitializationTest,linkToDeathReturnError)1979 TEST_F(VersionedIPreparedModelInitializationTest, linkToDeathReturnError) {
1980     // setup failure
1981     EXPECT_CALL(*kMockPreparedModel, linkToDeathRet())
1982             .Times(1)
1983             .WillOnce(InvokeWithoutArgs([]() -> Return<bool> { return false; }));
1984     const auto ret = makePreparedModel_1_3Return(V1_3::ErrorStatus::NONE, V1_3::ErrorStatus::NONE,
1985                                                  kMockPreparedModel);
1986     EXPECT_CALL(*kMockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
1987             .Times(1)
1988             .WillOnce(Invoke(ret));
1989 
1990     // run test
1991     const ModelFactory makeModel = [] { return V1_3::Model{}; };
1992     const auto [resultCode, preparedModel] = kDevice->prepareModel(makeModel, {}, {}, {}, {}, {});
1993 
1994     // verify failure
1995     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
1996     EXPECT_EQ(nullptr, preparedModel.get());
1997 }
1998 
1999 // Simulate successful test
2000 
TEST_F(VersionedIPreparedModelV1_0Test,executeAsync)2001 TEST_F(VersionedIPreparedModelV1_0Test, executeAsync) {
2002     // setup call
2003     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE);
2004     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2005 
2006     // run test
2007     const auto [resultCode, outputShapes, timing] =
2008             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2009 
2010     // verify success
2011     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
2012     EXPECT_EQ(0u, outputShapes.size());
2013     EXPECT_EQ(kNoTiming, timing);
2014 }
2015 
TEST_F(VersionedIPreparedModelV1_1Test,executeAsync)2016 TEST_F(VersionedIPreparedModelV1_1Test, executeAsync) {
2017     // setup call
2018     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE);
2019     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2020 
2021     // run test
2022     const auto [resultCode, outputShapes, timing] =
2023             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2024 
2025     // verify success
2026     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
2027     EXPECT_EQ(0u, outputShapes.size());
2028     EXPECT_EQ(kNoTiming, timing);
2029 }
2030 
TEST_F(VersionedIPreparedModelV1_2Test,executeAsync)2031 TEST_F(VersionedIPreparedModelV1_2Test, executeAsync) {
2032     // setup call
2033     const auto ret =
2034             makeExecute_1_2Return(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE, {}, kNoTiming);
2035     EXPECT_CALL(*kMockPreparedModel, execute_1_2(_, _, _)).Times(1).WillOnce(Invoke(ret));
2036 
2037     // run test
2038     const auto [resultCode, outputShapes, timing] =
2039             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2040 
2041     // verify success
2042     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
2043     EXPECT_EQ(0u, outputShapes.size());
2044     EXPECT_EQ(kNoTiming, timing);
2045 }
2046 
TEST_F(VersionedIPreparedModelV1_3Test,executeAsync)2047 TEST_F(VersionedIPreparedModelV1_3Test, executeAsync) {
2048     // setup call
2049     const auto ret =
2050             makeExecute_1_3Return(V1_3::ErrorStatus::NONE, V1_3::ErrorStatus::NONE, {}, kNoTiming);
2051     EXPECT_CALL(*kMockPreparedModel, execute_1_3(_, _, _, _, _)).Times(1).WillOnce(Invoke(ret));
2052 
2053     // run test
2054     const auto [resultCode, outputShapes, timing] =
2055             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2056 
2057     // verify success
2058     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
2059     EXPECT_EQ(0u, outputShapes.size());
2060     EXPECT_EQ(kNoTiming, timing);
2061 }
2062 
TEST_F(VersionedIPreparedModelV1_0Test,executePreferSync)2063 TEST_F(VersionedIPreparedModelV1_0Test, executePreferSync) {
2064     // setup call
2065     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE);
2066     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2067 
2068     // run test
2069     const auto [resultCode, outputShapes, timing] =
2070             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2071 
2072     // verify success
2073     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
2074     EXPECT_EQ(0u, outputShapes.size());
2075     EXPECT_EQ(kNoTiming, timing);
2076 }
2077 
TEST_F(VersionedIPreparedModelV1_1Test,executePreferSync)2078 TEST_F(VersionedIPreparedModelV1_1Test, executePreferSync) {
2079     // setup call
2080     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE);
2081     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2082 
2083     // run test
2084     const auto [resultCode, outputShapes, timing] =
2085             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2086 
2087     // verify success
2088     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
2089     EXPECT_EQ(0u, outputShapes.size());
2090     EXPECT_EQ(kNoTiming, timing);
2091 }
2092 
TEST_F(VersionedIPreparedModelV1_2Test,executePreferSync)2093 TEST_F(VersionedIPreparedModelV1_2Test, executePreferSync) {
2094     // setup call
2095     const auto ret = makeExecuteSynchronouslyReturn(V1_0::ErrorStatus::NONE, {}, kNoTiming);
2096     EXPECT_CALL(*kMockPreparedModel, executeSynchronously(_, _, _)).Times(1).WillOnce(Invoke(ret));
2097 
2098     // run test
2099     const auto [resultCode, outputShapes, timing] =
2100             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2101 
2102     // verify success
2103     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
2104     EXPECT_EQ(0u, outputShapes.size());
2105     EXPECT_EQ(kNoTiming, timing);
2106 }
2107 
TEST_F(VersionedIPreparedModelV1_3Test,executePreferSync)2108 TEST_F(VersionedIPreparedModelV1_3Test, executePreferSync) {
2109     // setup call
2110     const auto ret = makeExecuteSynchronously_1_3Return(V1_3::ErrorStatus::NONE, {}, kNoTiming);
2111     EXPECT_CALL(*kMockPreparedModel, executeSynchronously_1_3(_, _, _, _, _))
2112             .Times(1)
2113             .WillOnce(Invoke(ret));
2114 
2115     // run test
2116     const auto [resultCode, outputShapes, timing] =
2117             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2118 
2119     // verify success
2120     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
2121     EXPECT_EQ(0u, outputShapes.size());
2122     EXPECT_EQ(kNoTiming, timing);
2123 }
2124 
TEST_F(VersionedIPreparedModelV1_0Test,executeFenced)2125 TEST_F(VersionedIPreparedModelV1_0Test, executeFenced) {
2126     // setup call
2127     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE);
2128     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2129 
2130     // run test
2131     const auto [resultCode, syncFence, dispatchCallback, timing] =
2132             kPreparedModel->executeFenced({}, {}, {}, {}, {}, {});
2133 
2134     // verify success
2135     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
2136     EXPECT_EQ(nullptr, syncFence.getNativeHandle());
2137     EXPECT_EQ(nullptr, dispatchCallback.get());
2138     EXPECT_EQ(kNoTiming, timing);
2139 }
2140 
TEST_F(VersionedIPreparedModelV1_1Test,executeFenced)2141 TEST_F(VersionedIPreparedModelV1_1Test, executeFenced) {
2142     // setup call
2143     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE);
2144     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2145 
2146     // run test
2147     const auto [resultCode, syncFence, dispatchCallback, timing] =
2148             kPreparedModel->executeFenced({}, {}, {}, {}, {}, {});
2149 
2150     // verify success
2151     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
2152     EXPECT_EQ(nullptr, syncFence.getNativeHandle());
2153     EXPECT_EQ(nullptr, dispatchCallback.get());
2154     EXPECT_EQ(kNoTiming, timing);
2155 }
2156 
TEST_F(VersionedIPreparedModelV1_2Test,executeFenced)2157 TEST_F(VersionedIPreparedModelV1_2Test, executeFenced) {
2158     // setup call
2159     const auto ret = makeExecuteSynchronouslyReturn(V1_0::ErrorStatus::NONE, {}, kNoTiming);
2160     EXPECT_CALL(*kMockPreparedModel, executeSynchronously(_, _, _)).Times(1).WillOnce(Invoke(ret));
2161 
2162     // run test
2163     const auto [resultCode, syncFence, dispatchCallback, timing] =
2164             kPreparedModel->executeFenced({}, {}, {}, {}, {}, {});
2165 
2166     // verify success
2167     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
2168     EXPECT_EQ(nullptr, syncFence.getNativeHandle());
2169     EXPECT_EQ(nullptr, dispatchCallback.get());
2170     EXPECT_EQ(kNoTiming, timing);
2171 }
2172 
TEST_F(VersionedIPreparedModelV1_3Test,executeFenced)2173 TEST_F(VersionedIPreparedModelV1_3Test, executeFenced) {
2174     // setup call
2175     auto memory = allocateSharedMemory(4);
2176     hidl_handle fakeSyncFence(memory.handle());
2177     const sp<IFencedExecutionCallback> callback = new MockFencedExecutionCallback();
2178     const auto ret = makeExecuteFencedReturn(V1_3::ErrorStatus::NONE, fakeSyncFence, callback);
2179     EXPECT_CALL(*kMockPreparedModel, executeFenced(_, _, _, _, _, _, _))
2180             .Times(1)
2181             .WillOnce(Invoke(ret));
2182 
2183     // run test
2184     const auto [resultCode, syncFence, dispatchCallback, timing] =
2185             kPreparedModel->executeFenced({}, {}, {}, {}, {}, {});
2186 
2187     // verify success
2188     EXPECT_EQ(ANEURALNETWORKS_NO_ERROR, resultCode);
2189     EXPECT_NE(nullptr, syncFence.getNativeHandle());
2190     EXPECT_NE(nullptr, dispatchCallback.get());
2191     EXPECT_EQ(kNoTiming, timing);
2192 }
2193 
TEST_F(VersionedIPreparedModelV1_0Test,configureExecutionBurst)2194 TEST_F(VersionedIPreparedModelV1_0Test, configureExecutionBurst) {
2195     // run test
2196     const auto executionBurstController =
2197             kPreparedModel->configureExecutionBurst(/*preferPowerOverLatency=*/false);
2198 
2199     // verify success
2200     EXPECT_EQ(nullptr, executionBurstController);
2201 }
2202 
TEST_F(VersionedIPreparedModelV1_1Test,configureExecutionBurst)2203 TEST_F(VersionedIPreparedModelV1_1Test, configureExecutionBurst) {
2204     // run test
2205     const auto executionBurstController =
2206             kPreparedModel->configureExecutionBurst(/*preferPowerOverLatency=*/false);
2207 
2208     // verify success
2209     EXPECT_EQ(nullptr, executionBurstController);
2210 }
2211 
TEST_F(VersionedIPreparedModelV1_2Test,configureExecutionBurst)2212 TEST_F(VersionedIPreparedModelV1_2Test, configureExecutionBurst) {
2213     // setup call
2214     const sp<MockBurstContext> burstContext = new MockBurstContext();
2215     const auto ret = makeConfigureExecutionBurst(V1_0::ErrorStatus::NONE, burstContext);
2216     EXPECT_CALL(*kMockPreparedModel, configureExecutionBurst(_, _, _, _))
2217             .Times(1)
2218             .WillOnce(Invoke(ret));
2219 
2220     // run test
2221     const auto executionBurstController =
2222             kPreparedModel->configureExecutionBurst(/*preferPowerOverLatency=*/false);
2223 
2224     // verify success
2225     EXPECT_NE(nullptr, executionBurstController);
2226 }
2227 
TEST_F(VersionedIPreparedModelV1_3Test,configureExecutionBurst)2228 TEST_F(VersionedIPreparedModelV1_3Test, configureExecutionBurst) {
2229     // setup call
2230     const sp<MockBurstContext> burstContext = new MockBurstContext();
2231     const auto ret = makeConfigureExecutionBurst(V1_0::ErrorStatus::NONE, burstContext);
2232     EXPECT_CALL(*kMockPreparedModel, configureExecutionBurst(_, _, _, _))
2233             .Times(1)
2234             .WillOnce(Invoke(ret));
2235 
2236     // run test
2237     const auto executionBurstController =
2238             kPreparedModel->configureExecutionBurst(/*preferPowerOverLatency=*/false);
2239 
2240     // verify success
2241     EXPECT_NE(nullptr, executionBurstController);
2242 }
2243 
2244 // Simulate general failure
2245 
TEST_F(VersionedIPreparedModelV1_0Test,executeAsyncLaunchFailure)2246 TEST_F(VersionedIPreparedModelV1_0Test, executeAsyncLaunchFailure) {
2247     // setup failure
2248     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::GENERAL_FAILURE, V1_0::ErrorStatus::NONE);
2249     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2250 
2251     // run test
2252     const auto [resultCode, outputShapes, timing] =
2253             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2254 
2255     // verify failure
2256     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2257     EXPECT_EQ(0u, outputShapes.size());
2258     EXPECT_EQ(kNoTiming, timing);
2259 }
2260 
TEST_F(VersionedIPreparedModelV1_1Test,executeAsyncLaunchFailure)2261 TEST_F(VersionedIPreparedModelV1_1Test, executeAsyncLaunchFailure) {
2262     // setup failure
2263     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::GENERAL_FAILURE, V1_0::ErrorStatus::NONE);
2264     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2265 
2266     // run test
2267     const auto [resultCode, outputShapes, timing] =
2268             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2269 
2270     // verify failure
2271     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2272     EXPECT_EQ(0u, outputShapes.size());
2273     EXPECT_EQ(kNoTiming, timing);
2274 }
2275 
TEST_F(VersionedIPreparedModelV1_2Test,executeAsyncLaunchFailure)2276 TEST_F(VersionedIPreparedModelV1_2Test, executeAsyncLaunchFailure) {
2277     // setup failure
2278     const auto ret = makeExecute_1_2Return(V1_0::ErrorStatus::GENERAL_FAILURE,
2279                                            V1_0::ErrorStatus::NONE, {}, kNoTiming);
2280     EXPECT_CALL(*kMockPreparedModel, execute_1_2(_, _, _)).Times(1).WillOnce(Invoke(ret));
2281 
2282     // run test
2283     const auto [resultCode, outputShapes, timing] =
2284             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2285 
2286     // verify failure
2287     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2288     EXPECT_EQ(0u, outputShapes.size());
2289     EXPECT_EQ(kNoTiming, timing);
2290 }
2291 
TEST_F(VersionedIPreparedModelV1_3Test,executeAsyncLaunchFailure)2292 TEST_F(VersionedIPreparedModelV1_3Test, executeAsyncLaunchFailure) {
2293     // setup failure
2294     const auto ret = makeExecute_1_3Return(V1_3::ErrorStatus::GENERAL_FAILURE,
2295                                            V1_3::ErrorStatus::NONE, {}, kNoTiming);
2296     EXPECT_CALL(*kMockPreparedModel, execute_1_3(_, _, _, _, _)).Times(1).WillOnce(Invoke(ret));
2297 
2298     // run test
2299     const auto [resultCode, outputShapes, timing] =
2300             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2301 
2302     // verify failure
2303     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2304     EXPECT_EQ(0u, outputShapes.size());
2305     EXPECT_EQ(kNoTiming, timing);
2306 }
2307 
TEST_F(VersionedIPreparedModelV1_0Test,executeAsyncReturnFailure)2308 TEST_F(VersionedIPreparedModelV1_0Test, executeAsyncReturnFailure) {
2309     // setup failure
2310     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::GENERAL_FAILURE);
2311     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2312 
2313     // run test
2314     const auto [resultCode, outputShapes, timing] =
2315             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2316 
2317     // verify failure
2318     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2319     EXPECT_EQ(0u, outputShapes.size());
2320     EXPECT_EQ(kNoTiming, timing);
2321 }
2322 
TEST_F(VersionedIPreparedModelV1_1Test,executeAsyncReturnFailure)2323 TEST_F(VersionedIPreparedModelV1_1Test, executeAsyncReturnFailure) {
2324     // setup failure
2325     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::GENERAL_FAILURE);
2326     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2327 
2328     // run test
2329     const auto [resultCode, outputShapes, timing] =
2330             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2331 
2332     // verify failure
2333     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2334     EXPECT_EQ(0u, outputShapes.size());
2335     EXPECT_EQ(kNoTiming, timing);
2336 }
2337 
TEST_F(VersionedIPreparedModelV1_2Test,executeAsyncReturnFailure)2338 TEST_F(VersionedIPreparedModelV1_2Test, executeAsyncReturnFailure) {
2339     // setup failure
2340     const auto ret = makeExecute_1_2Return(V1_0::ErrorStatus::NONE,
2341                                            V1_0::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
2342     EXPECT_CALL(*kMockPreparedModel, execute_1_2(_, _, _)).Times(1).WillOnce(Invoke(ret));
2343 
2344     // run test
2345     const auto [resultCode, outputShapes, timing] =
2346             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2347 
2348     // verify failure
2349     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2350     EXPECT_EQ(0u, outputShapes.size());
2351     EXPECT_EQ(kNoTiming, timing);
2352 }
2353 
TEST_F(VersionedIPreparedModelV1_3Test,executeAsyncReturnFailure)2354 TEST_F(VersionedIPreparedModelV1_3Test, executeAsyncReturnFailure) {
2355     // setup failure
2356     const auto ret = makeExecute_1_3Return(V1_3::ErrorStatus::NONE,
2357                                            V1_3::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
2358     EXPECT_CALL(*kMockPreparedModel, execute_1_3(_, _, _, _, _)).Times(1).WillOnce(Invoke(ret));
2359 
2360     // run test
2361     const auto [resultCode, outputShapes, timing] =
2362             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2363 
2364     // verify failure
2365     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2366     EXPECT_EQ(0u, outputShapes.size());
2367     EXPECT_EQ(kNoTiming, timing);
2368 }
2369 
TEST_F(VersionedIPreparedModelV1_0Test,executePreferSyncFailure)2370 TEST_F(VersionedIPreparedModelV1_0Test, executePreferSyncFailure) {
2371     // setup failure
2372     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::GENERAL_FAILURE,
2373                                        V1_0::ErrorStatus::GENERAL_FAILURE);
2374     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2375 
2376     // run test
2377     const auto [resultCode, outputShapes, timing] =
2378             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2379 
2380     // verify failure
2381     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2382     EXPECT_EQ(0u, outputShapes.size());
2383     EXPECT_EQ(kNoTiming, timing);
2384 }
2385 
TEST_F(VersionedIPreparedModelV1_1Test,executePreferSyncFailure)2386 TEST_F(VersionedIPreparedModelV1_1Test, executePreferSyncFailure) {
2387     // setup failure
2388     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::GENERAL_FAILURE,
2389                                        V1_0::ErrorStatus::GENERAL_FAILURE);
2390     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2391 
2392     // run test
2393     const auto [resultCode, outputShapes, timing] =
2394             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2395 
2396     // verify failure
2397     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2398     EXPECT_EQ(0u, outputShapes.size());
2399     EXPECT_EQ(kNoTiming, timing);
2400 }
2401 
TEST_F(VersionedIPreparedModelV1_2Test,executePreferSyncFailure)2402 TEST_F(VersionedIPreparedModelV1_2Test, executePreferSyncFailure) {
2403     // setup failure
2404     const auto ret =
2405             makeExecuteSynchronouslyReturn(V1_0::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
2406     EXPECT_CALL(*kMockPreparedModel, executeSynchronously(_, _, _)).Times(1).WillOnce(Invoke(ret));
2407 
2408     // run test
2409     const auto [resultCode, outputShapes, timing] =
2410             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2411 
2412     // verify failure
2413     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2414     EXPECT_EQ(0u, outputShapes.size());
2415     EXPECT_EQ(kNoTiming, timing);
2416 }
2417 
TEST_F(VersionedIPreparedModelV1_3Test,executePreferSyncFailure)2418 TEST_F(VersionedIPreparedModelV1_3Test, executePreferSyncFailure) {
2419     // setup failure
2420     const auto ret =
2421             makeExecuteSynchronously_1_3Return(V1_3::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
2422     EXPECT_CALL(*kMockPreparedModel, executeSynchronously_1_3(_, _, _, _, _))
2423             .Times(1)
2424             .WillOnce(Invoke(ret));
2425 
2426     // run test
2427     const auto [resultCode, outputShapes, timing] =
2428             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2429 
2430     // verify failure
2431     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2432     EXPECT_EQ(0u, outputShapes.size());
2433     EXPECT_EQ(kNoTiming, timing);
2434 }
2435 
TEST_F(VersionedIPreparedModelV1_0Test,executeFencedFailure)2436 TEST_F(VersionedIPreparedModelV1_0Test, executeFencedFailure) {
2437     // setup failure
2438     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::GENERAL_FAILURE,
2439                                        V1_0::ErrorStatus::GENERAL_FAILURE);
2440     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2441 
2442     // run test
2443     const auto [resultCode, syncFence, dispatchCallback, timing] =
2444             kPreparedModel->executeFenced({}, {}, {}, {}, {}, {});
2445 
2446     // verify failure
2447     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2448     EXPECT_EQ(nullptr, syncFence.getNativeHandle());
2449     EXPECT_EQ(nullptr, dispatchCallback.get());
2450     EXPECT_EQ(kNoTiming, timing);
2451 }
2452 
TEST_F(VersionedIPreparedModelV1_1Test,executeFencedFailure)2453 TEST_F(VersionedIPreparedModelV1_1Test, executeFencedFailure) {
2454     // setup failure
2455     const auto ret = makeExecuteReturn(V1_0::ErrorStatus::GENERAL_FAILURE,
2456                                        V1_0::ErrorStatus::GENERAL_FAILURE);
2457     EXPECT_CALL(*kMockPreparedModel, execute(_, _)).Times(1).WillOnce(Invoke(ret));
2458 
2459     // run test
2460     const auto [resultCode, syncFence, dispatchCallback, timing] =
2461             kPreparedModel->executeFenced({}, {}, {}, {}, {}, {});
2462 
2463     // verify failure
2464     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2465     EXPECT_EQ(nullptr, syncFence.getNativeHandle());
2466     EXPECT_EQ(nullptr, dispatchCallback.get());
2467     EXPECT_EQ(kNoTiming, timing);
2468 }
2469 
TEST_F(VersionedIPreparedModelV1_2Test,executeFencedFailure)2470 TEST_F(VersionedIPreparedModelV1_2Test, executeFencedFailure) {
2471     // setup failure
2472     const auto ret =
2473             makeExecuteSynchronouslyReturn(V1_0::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
2474     EXPECT_CALL(*kMockPreparedModel, executeSynchronously(_, _, _)).Times(1).WillOnce(Invoke(ret));
2475 
2476     // run test
2477     const auto [resultCode, syncFence, dispatchCallback, timing] =
2478             kPreparedModel->executeFenced({}, {}, {}, {}, {}, {});
2479 
2480     // verify failure
2481     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2482     EXPECT_EQ(nullptr, syncFence.getNativeHandle());
2483     EXPECT_EQ(nullptr, dispatchCallback.get());
2484     EXPECT_EQ(kNoTiming, timing);
2485 }
2486 
TEST_F(VersionedIPreparedModelV1_3Test,executeFencedFailure)2487 TEST_F(VersionedIPreparedModelV1_3Test, executeFencedFailure) {
2488     // setup failure
2489     auto memory = allocateSharedMemory(4);
2490     hidl_handle fakeSyncFence(memory.handle());
2491     const sp<IFencedExecutionCallback> callback = new MockFencedExecutionCallback();
2492     const auto ret =
2493             makeExecuteFencedReturn(V1_3::ErrorStatus::GENERAL_FAILURE, fakeSyncFence, callback);
2494     EXPECT_CALL(*kMockPreparedModel, executeFenced(_, _, _, _, _, _, _))
2495             .Times(1)
2496             .WillOnce(Invoke(ret));
2497 
2498     // run test
2499     const auto [resultCode, syncFence, dispatchCallback, timing] =
2500             kPreparedModel->executeFenced({}, {}, {}, {}, {}, {});
2501 
2502     // verify failure
2503     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2504     EXPECT_EQ(nullptr, syncFence.getNativeHandle());
2505     EXPECT_EQ(nullptr, dispatchCallback.get());
2506     EXPECT_EQ(kNoTiming, timing);
2507 }
2508 
TEST_F(VersionedIPreparedModelV1_2Test,configureExecutionBurstFailure)2509 TEST_F(VersionedIPreparedModelV1_2Test, configureExecutionBurstFailure) {
2510     // setup failure
2511     const sp<MockBurstContext> burstContext = new MockBurstContext();
2512     const auto ret = makeConfigureExecutionBurst(V1_0::ErrorStatus::GENERAL_FAILURE, burstContext);
2513     EXPECT_CALL(*kMockPreparedModel, configureExecutionBurst(_, _, _, _))
2514             .Times(1)
2515             .WillOnce(Invoke(ret));
2516 
2517     // run test
2518     const auto executionBurstController =
2519             kPreparedModel->configureExecutionBurst(/*preferPowerOverLatency=*/false);
2520 
2521     // verify failure
2522     EXPECT_EQ(nullptr, executionBurstController);
2523 }
2524 
TEST_F(VersionedIPreparedModelV1_3Test,configureExecutionBurstFailure)2525 TEST_F(VersionedIPreparedModelV1_3Test, configureExecutionBurstFailure) {
2526     // setup failure
2527     const sp<MockBurstContext> burstContext = new MockBurstContext();
2528     const auto ret = makeConfigureExecutionBurst(V1_0::ErrorStatus::GENERAL_FAILURE, burstContext);
2529     EXPECT_CALL(*kMockPreparedModel, configureExecutionBurst(_, _, _, _))
2530             .Times(1)
2531             .WillOnce(Invoke(ret));
2532 
2533     // run test
2534     const auto executionBurstController =
2535             kPreparedModel->configureExecutionBurst(/*preferPowerOverLatency=*/false);
2536 
2537     // verify failure
2538     EXPECT_EQ(nullptr, executionBurstController);
2539 }
2540 
TEST_F(VersionedIPreparedModelV1_2Test,configureExecutionBurstNullptrError)2541 TEST_F(VersionedIPreparedModelV1_2Test, configureExecutionBurstNullptrError) {
2542     // setup failure
2543     const auto ret = makeConfigureExecutionBurst(V1_0::ErrorStatus::NONE, nullptr);
2544     EXPECT_CALL(*kMockPreparedModel, configureExecutionBurst(_, _, _, _))
2545             .Times(1)
2546             .WillOnce(Invoke(ret));
2547 
2548     // run test
2549     const auto executionBurstController =
2550             kPreparedModel->configureExecutionBurst(/*preferPowerOverLatency=*/false);
2551 
2552     // verify failure
2553     EXPECT_EQ(nullptr, executionBurstController);
2554 }
2555 
TEST_F(VersionedIPreparedModelV1_3Test,configureExecutionBurstNullptrError)2556 TEST_F(VersionedIPreparedModelV1_3Test, configureExecutionBurstNullptrError) {
2557     // setup failure
2558     const auto ret = makeConfigureExecutionBurst(V1_0::ErrorStatus::NONE, nullptr);
2559     EXPECT_CALL(*kMockPreparedModel, configureExecutionBurst(_, _, _, _))
2560             .Times(1)
2561             .WillOnce(Invoke(ret));
2562 
2563     // run test
2564     const auto executionBurstController =
2565             kPreparedModel->configureExecutionBurst(/*preferPowerOverLatency=*/false);
2566 
2567     // verify failure
2568     EXPECT_EQ(nullptr, executionBurstController);
2569 }
2570 
2571 // Simulate transport failure
2572 
TEST_F(VersionedIPreparedModelV1_0Test,executeAsyncTransportFailure)2573 TEST_F(VersionedIPreparedModelV1_0Test, executeAsyncTransportFailure) {
2574     // setup failure
2575     EXPECT_CALL(*kMockPreparedModel, execute(_, _))
2576             .Times(1)
2577             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2578 
2579     // run test
2580     const auto [resultCode, outputShapes, timing] =
2581             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2582 
2583     // verify failure
2584     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2585     EXPECT_EQ(0u, outputShapes.size());
2586     EXPECT_EQ(kNoTiming, timing);
2587 }
2588 
TEST_F(VersionedIPreparedModelV1_1Test,executeAsyncTransportFailure)2589 TEST_F(VersionedIPreparedModelV1_1Test, executeAsyncTransportFailure) {
2590     // setup failure
2591     EXPECT_CALL(*kMockPreparedModel, execute(_, _))
2592             .Times(1)
2593             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2594 
2595     // run test
2596     const auto [resultCode, outputShapes, timing] =
2597             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2598 
2599     // verify failure
2600     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2601     EXPECT_EQ(0u, outputShapes.size());
2602     EXPECT_EQ(kNoTiming, timing);
2603 }
2604 
TEST_F(VersionedIPreparedModelV1_2Test,executeAsyncTransportFailure)2605 TEST_F(VersionedIPreparedModelV1_2Test, executeAsyncTransportFailure) {
2606     // setup failure
2607     EXPECT_CALL(*kMockPreparedModel, execute_1_2(_, _, _))
2608             .Times(1)
2609             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2610 
2611     // run test
2612     const auto [resultCode, outputShapes, timing] =
2613             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2614 
2615     // verify failure
2616     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2617     EXPECT_EQ(0u, outputShapes.size());
2618     EXPECT_EQ(kNoTiming, timing);
2619 }
2620 
TEST_F(VersionedIPreparedModelV1_3Test,executeAsyncTransportFailure)2621 TEST_F(VersionedIPreparedModelV1_3Test, executeAsyncTransportFailure) {
2622     // setup failure
2623     EXPECT_CALL(*kMockPreparedModel, execute_1_3(_, _, _, _, _))
2624             .Times(1)
2625             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2626 
2627     // run test
2628     const auto [resultCode, outputShapes, timing] =
2629             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2630 
2631     // verify failure
2632     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2633     EXPECT_EQ(0u, outputShapes.size());
2634     EXPECT_EQ(kNoTiming, timing);
2635 }
2636 
TEST_F(VersionedIPreparedModelV1_0Test,executePreferSyncTransportFailure)2637 TEST_F(VersionedIPreparedModelV1_0Test, executePreferSyncTransportFailure) {
2638     // setup failure
2639     EXPECT_CALL(*kMockPreparedModel, execute(_, _))
2640             .Times(1)
2641             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2642 
2643     // run test
2644     const auto [resultCode, outputShapes, timing] =
2645             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2646 
2647     // verify failure
2648     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2649     EXPECT_EQ(0u, outputShapes.size());
2650     EXPECT_EQ(kNoTiming, timing);
2651 }
2652 
TEST_F(VersionedIPreparedModelV1_1Test,executePreferSyncTransportFailure)2653 TEST_F(VersionedIPreparedModelV1_1Test, executePreferSyncTransportFailure) {
2654     // setup failure
2655     EXPECT_CALL(*kMockPreparedModel, execute(_, _))
2656             .Times(1)
2657             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2658 
2659     // run test
2660     const auto [resultCode, outputShapes, timing] =
2661             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2662 
2663     // verify failure
2664     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2665     EXPECT_EQ(0u, outputShapes.size());
2666     EXPECT_EQ(kNoTiming, timing);
2667 }
2668 
TEST_F(VersionedIPreparedModelV1_2Test,executePreferSyncTransportFailure)2669 TEST_F(VersionedIPreparedModelV1_2Test, executePreferSyncTransportFailure) {
2670     // setup failure
2671     EXPECT_CALL(*kMockPreparedModel, executeSynchronously(_, _, _))
2672             .Times(1)
2673             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2674 
2675     // run test
2676     const auto [resultCode, outputShapes, timing] =
2677             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2678 
2679     // verify failure
2680     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2681     EXPECT_EQ(0u, outputShapes.size());
2682     EXPECT_EQ(kNoTiming, timing);
2683 }
2684 
TEST_F(VersionedIPreparedModelV1_3Test,executePreferSyncTransportFailure)2685 TEST_F(VersionedIPreparedModelV1_3Test, executePreferSyncTransportFailure) {
2686     // setup failure
2687     EXPECT_CALL(*kMockPreparedModel, executeSynchronously_1_3(_, _, _, _, _))
2688             .Times(1)
2689             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2690 
2691     // run test
2692     const auto [resultCode, outputShapes, timing] =
2693             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2694 
2695     // verify failure
2696     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2697     EXPECT_EQ(0u, outputShapes.size());
2698     EXPECT_EQ(kNoTiming, timing);
2699 }
2700 
TEST_F(VersionedIPreparedModelV1_0Test,executeFencedTransportFailure)2701 TEST_F(VersionedIPreparedModelV1_0Test, executeFencedTransportFailure) {
2702     // setup failure
2703     EXPECT_CALL(*kMockPreparedModel, execute(_, _))
2704             .Times(1)
2705             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2706 
2707     // run test
2708     const auto [resultCode, syncFence, dispatchCallback, timing] =
2709             kPreparedModel->executeFenced({}, {}, {}, {}, {}, {});
2710 
2711     // verify failure
2712     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2713     EXPECT_EQ(nullptr, syncFence.getNativeHandle());
2714     EXPECT_EQ(nullptr, dispatchCallback.get());
2715     EXPECT_EQ(kNoTiming, timing);
2716 }
2717 
TEST_F(VersionedIPreparedModelV1_1Test,executeFencedTransportFailure)2718 TEST_F(VersionedIPreparedModelV1_1Test, executeFencedTransportFailure) {
2719     // setup failure
2720     EXPECT_CALL(*kMockPreparedModel, execute(_, _))
2721             .Times(1)
2722             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2723 
2724     // run test
2725     const auto [resultCode, syncFence, dispatchCallback, timing] =
2726             kPreparedModel->executeFenced({}, {}, {}, {}, {}, {});
2727 
2728     // verify failure
2729     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2730     EXPECT_EQ(nullptr, syncFence.getNativeHandle());
2731     EXPECT_EQ(nullptr, dispatchCallback.get());
2732     EXPECT_EQ(kNoTiming, timing);
2733 }
2734 
TEST_F(VersionedIPreparedModelV1_2Test,executeFencedTransportFailure)2735 TEST_F(VersionedIPreparedModelV1_2Test, executeFencedTransportFailure) {
2736     // setup failure
2737     EXPECT_CALL(*kMockPreparedModel, executeSynchronously(_, _, _))
2738             .Times(1)
2739             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2740 
2741     // run test
2742     const auto [resultCode, syncFence, dispatchCallback, timing] =
2743             kPreparedModel->executeFenced({}, {}, {}, {}, {}, {});
2744 
2745     // verify failure
2746     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2747     EXPECT_EQ(nullptr, syncFence.getNativeHandle());
2748     EXPECT_EQ(nullptr, dispatchCallback.get());
2749     EXPECT_EQ(kNoTiming, timing);
2750 }
2751 
TEST_F(VersionedIPreparedModelV1_3Test,executeFencedTransportFailure)2752 TEST_F(VersionedIPreparedModelV1_3Test, executeFencedTransportFailure) {
2753     // setup failure
2754     EXPECT_CALL(*kMockPreparedModel, executeFenced(_, _, _, _, _, _, _))
2755             .Times(1)
2756             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2757 
2758     // run test
2759     const auto [resultCode, syncFence, dispatchCallback, timing] =
2760             kPreparedModel->executeFenced({}, {}, {}, {}, {}, {});
2761 
2762     // verify failure
2763     EXPECT_EQ(ANEURALNETWORKS_OP_FAILED, resultCode);
2764     EXPECT_EQ(nullptr, syncFence.getNativeHandle());
2765     EXPECT_EQ(nullptr, dispatchCallback.get());
2766     EXPECT_EQ(kNoTiming, timing);
2767 }
2768 
TEST_F(VersionedIPreparedModelV1_2Test,configureExecutionBurstTransportFailure)2769 TEST_F(VersionedIPreparedModelV1_2Test, configureExecutionBurstTransportFailure) {
2770     // setup failure
2771     EXPECT_CALL(*kMockPreparedModel, configureExecutionBurst(_, _, _, _))
2772             .Times(1)
2773             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2774 
2775     // run test
2776     const auto executionBurstController =
2777             kPreparedModel->configureExecutionBurst(/*preferPowerOverLatency=*/false);
2778 
2779     // verify failure
2780     EXPECT_EQ(nullptr, executionBurstController);
2781 }
2782 
TEST_F(VersionedIPreparedModelV1_3Test,configureExecutionBurstTransportFailure)2783 TEST_F(VersionedIPreparedModelV1_3Test, configureExecutionBurstTransportFailure) {
2784     // setup failure
2785     EXPECT_CALL(*kMockPreparedModel, configureExecutionBurst(_, _, _, _))
2786             .Times(1)
2787             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
2788 
2789     // run test
2790     const auto executionBurstController =
2791             kPreparedModel->configureExecutionBurst(/*preferPowerOverLatency=*/false);
2792 
2793     // verify failure
2794     EXPECT_EQ(nullptr, executionBurstController);
2795 }
2796 
2797 // Simulate service crash
2798 
TEST_F(VersionedIPreparedModelV1_0Test,executeAsyncLaunchCrash)2799 TEST_F(VersionedIPreparedModelV1_0Test, executeAsyncLaunchCrash) {
2800     // setup failure
2801     EXPECT_CALL(*kMockPreparedModel, execute(_, _))
2802             .Times(1)
2803             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
2804 
2805     // run test
2806     const auto [resultCode, outputShapes, timing] =
2807             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2808 
2809     // verify failure
2810     EXPECT_EQ(ANEURALNETWORKS_DEAD_OBJECT, resultCode);
2811     EXPECT_EQ(0u, outputShapes.size());
2812     EXPECT_EQ(kNoTiming, timing);
2813 }
2814 
TEST_F(VersionedIPreparedModelV1_1Test,executeAsyncLaunchCrash)2815 TEST_F(VersionedIPreparedModelV1_1Test, executeAsyncLaunchCrash) {
2816     // setup failure
2817     EXPECT_CALL(*kMockPreparedModel, execute(_, _))
2818             .Times(1)
2819             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
2820 
2821     // run test
2822     const auto [resultCode, outputShapes, timing] =
2823             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2824 
2825     // verify failure
2826     EXPECT_EQ(ANEURALNETWORKS_DEAD_OBJECT, resultCode);
2827     EXPECT_EQ(0u, outputShapes.size());
2828     EXPECT_EQ(kNoTiming, timing);
2829 }
2830 
TEST_F(VersionedIPreparedModelV1_2Test,executeAsyncLaunchCrash)2831 TEST_F(VersionedIPreparedModelV1_2Test, executeAsyncLaunchCrash) {
2832     // setup failure
2833     EXPECT_CALL(*kMockPreparedModel, execute_1_2(_, _, _))
2834             .Times(1)
2835             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
2836 
2837     // run test
2838     const auto [resultCode, outputShapes, timing] =
2839             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2840 
2841     // verify failure
2842     EXPECT_EQ(ANEURALNETWORKS_DEAD_OBJECT, resultCode);
2843     EXPECT_EQ(0u, outputShapes.size());
2844     EXPECT_EQ(kNoTiming, timing);
2845 }
2846 
TEST_F(VersionedIPreparedModelV1_3Test,executeAsyncLaunchCrash)2847 TEST_F(VersionedIPreparedModelV1_3Test, executeAsyncLaunchCrash) {
2848     // setup failure
2849     EXPECT_CALL(*kMockPreparedModel, execute_1_3(_, _, _, _, _))
2850             .Times(1)
2851             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
2852 
2853     // run test
2854     const auto [resultCode, outputShapes, timing] =
2855             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2856 
2857     // verify failure
2858     EXPECT_EQ(ANEURALNETWORKS_DEAD_OBJECT, resultCode);
2859     EXPECT_EQ(0u, outputShapes.size());
2860     EXPECT_EQ(kNoTiming, timing);
2861 }
2862 
TEST_F(VersionedIPreparedModelV1_2Test,executePreferSyncCrash)2863 TEST_F(VersionedIPreparedModelV1_2Test, executePreferSyncCrash) {
2864     // setup failure
2865     EXPECT_CALL(*kMockPreparedModel, executeSynchronously(_, _, _))
2866             .Times(1)
2867             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
2868 
2869     // run test
2870     const auto [resultCode, outputShapes, timing] =
2871             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2872 
2873     // verify failure
2874     EXPECT_EQ(ANEURALNETWORKS_DEAD_OBJECT, resultCode);
2875     EXPECT_EQ(0u, outputShapes.size());
2876     EXPECT_EQ(kNoTiming, timing);
2877 }
2878 
TEST_F(VersionedIPreparedModelV1_3Test,executePreferSyncCrash)2879 TEST_F(VersionedIPreparedModelV1_3Test, executePreferSyncCrash) {
2880     // setup failure
2881     EXPECT_CALL(*kMockPreparedModel, executeSynchronously_1_3(_, _, _, _, _))
2882             .Times(1)
2883             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
2884 
2885     // run test
2886     const auto [resultCode, outputShapes, timing] =
2887             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/true);
2888 
2889     // verify failure
2890     EXPECT_EQ(ANEURALNETWORKS_DEAD_OBJECT, resultCode);
2891     EXPECT_EQ(0u, outputShapes.size());
2892     EXPECT_EQ(kNoTiming, timing);
2893 }
2894 
TEST_F(VersionedIPreparedModelMockTest,executeAsyncReturnCrash)2895 TEST_F(VersionedIPreparedModelMockTest, executeAsyncReturnCrash) {
2896     // setup failure
2897     const auto ret = [this]() -> Return<V1_3::ErrorStatus> {
2898         kMockPreparedModel->simulateCrash();
2899         return V1_3::ErrorStatus::NONE;
2900     };
2901     EXPECT_CALL(*kMockPreparedModel, execute_1_3(_, _, _, _, _))
2902             .Times(1)
2903             .WillOnce(InvokeWithoutArgs(ret));
2904 
2905     // run test
2906     const auto [resultCode, outputShapes, timing] =
2907             kPreparedModel->execute({}, {}, {}, {}, /*preferSynchronous=*/false);
2908 
2909     // verify failure
2910     EXPECT_EQ(ANEURALNETWORKS_DEAD_OBJECT, resultCode);
2911     EXPECT_EQ(0u, outputShapes.size());
2912     EXPECT_EQ(kNoTiming, timing);
2913 }
2914 
2915 }  // namespace
2916 }  // namespace android::nn
2917