• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "MockBuffer.h"
18 #include "MockDevice.h"
19 #include "MockPreparedModel.h"
20 #include "TestUtils.h"
21 
22 #include <aidl/android/hardware/neuralnetworks/BnDevice.h>
23 #include <android/binder_auto_utils.h>
24 #include <android/binder_status.h>
25 #include <gmock/gmock.h>
26 #include <gtest/gtest.h>
27 #include <nnapi/IDevice.h>
28 #include <nnapi/TypeUtils.h>
29 #include <nnapi/Types.h>
30 #include <nnapi/hal/aidl/Device.h>
31 
32 #include <functional>
33 #include <memory>
34 #include <string>
35 
36 namespace aidl::android::hardware::neuralnetworks::utils {
37 namespace {
38 
39 namespace nn = ::android::nn;
40 using ::testing::_;
41 using ::testing::DoAll;
42 using ::testing::Invoke;
43 using ::testing::InvokeWithoutArgs;
44 using ::testing::SetArgPointee;
45 
46 const nn::Model kSimpleModel = {
47         .main = {.operands = {{.type = nn::OperandType::TENSOR_FLOAT32,
48                                .dimensions = {1},
49                                .lifetime = nn::Operand::LifeTime::SUBGRAPH_INPUT},
50                               {.type = nn::OperandType::TENSOR_FLOAT32,
51                                .dimensions = {1},
52                                .lifetime = nn::Operand::LifeTime::SUBGRAPH_OUTPUT}},
53                  .operations = {{.type = nn::OperationType::RELU, .inputs = {0}, .outputs = {1}}},
54                  .inputIndexes = {0},
55                  .outputIndexes = {1}}};
56 
57 const std::string kName = "Google-MockV1";
58 const std::string kInvalidName = "";
59 const std::shared_ptr<BnDevice> kInvalidDevice;
60 constexpr PerformanceInfo kNoPerformanceInfo = {.execTime = std::numeric_limits<float>::max(),
61                                                 .powerUsage = std::numeric_limits<float>::max()};
62 constexpr NumberOfCacheFiles kNumberOfCacheFiles = {.numModelCache = nn::kMaxNumberOfCacheFiles - 1,
63                                                     .numDataCache = nn::kMaxNumberOfCacheFiles};
__anon40c1850f0202null64 constexpr auto makeStatusOk = [] { return ndk::ScopedAStatus::ok(); };
65 
createMockDevice()66 std::shared_ptr<MockDevice> createMockDevice() {
67     const auto mockDevice = MockDevice::create();
68 
69     // Setup default actions for each relevant call.
70     ON_CALL(*mockDevice, getVersionString(_))
71             .WillByDefault(DoAll(SetArgPointee<0>(kName), InvokeWithoutArgs(makeStatusOk)));
72     ON_CALL(*mockDevice, getType(_))
73             .WillByDefault(
74                     DoAll(SetArgPointee<0>(DeviceType::OTHER), InvokeWithoutArgs(makeStatusOk)));
75     ON_CALL(*mockDevice, getSupportedExtensions(_))
76             .WillByDefault(DoAll(SetArgPointee<0>(std::vector<Extension>{}),
77                                  InvokeWithoutArgs(makeStatusOk)));
78     ON_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
79             .WillByDefault(
80                     DoAll(SetArgPointee<0>(kNumberOfCacheFiles), InvokeWithoutArgs(makeStatusOk)));
81     ON_CALL(*mockDevice, getCapabilities(_))
82             .WillByDefault(
83                     DoAll(SetArgPointee<0>(Capabilities{
84                                   .relaxedFloat32toFloat16PerformanceScalar = kNoPerformanceInfo,
85                                   .relaxedFloat32toFloat16PerformanceTensor = kNoPerformanceInfo,
86                                   .ifPerformance = kNoPerformanceInfo,
87                                   .whilePerformance = kNoPerformanceInfo,
88                           }),
89                           InvokeWithoutArgs(makeStatusOk)));
90 
91     // These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
92     // uninteresting methods calls.
93     EXPECT_CALL(*mockDevice, getVersionString(_)).Times(testing::AnyNumber());
94     EXPECT_CALL(*mockDevice, getType(_)).Times(testing::AnyNumber());
95     EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(testing::AnyNumber());
96     EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(testing::AnyNumber());
97     EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(testing::AnyNumber());
98 
99     return mockDevice;
100 }
101 
102 constexpr auto makePreparedModelReturnImpl =
103         [](ErrorStatus launchStatus, ErrorStatus returnStatus,
104            const std::shared_ptr<MockPreparedModel>& preparedModel,
__anon40c1850f0302(ErrorStatus launchStatus, ErrorStatus returnStatus, const std::shared_ptr<MockPreparedModel>& preparedModel, const std::shared_ptr<IPreparedModelCallback>& cb) 105            const std::shared_ptr<IPreparedModelCallback>& cb) {
106             cb->notify(returnStatus, preparedModel);
107             if (launchStatus == ErrorStatus::NONE) {
108                 return ndk::ScopedAStatus::ok();
109             }
110             return ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(launchStatus));
111         };
112 
makePreparedModelReturn(ErrorStatus launchStatus,ErrorStatus returnStatus,const std::shared_ptr<MockPreparedModel> & preparedModel)113 auto makePreparedModelReturn(ErrorStatus launchStatus, ErrorStatus returnStatus,
114                              const std::shared_ptr<MockPreparedModel>& preparedModel) {
115     return [launchStatus, returnStatus, preparedModel](
116                    const Model& /*model*/, ExecutionPreference /*preference*/,
117                    Priority /*priority*/, const int64_t& /*deadline*/,
118                    const std::vector<ndk::ScopedFileDescriptor>& /*modelCache*/,
119                    const std::vector<ndk::ScopedFileDescriptor>& /*dataCache*/,
120                    const std::vector<uint8_t>& /*token*/,
121                    const std::shared_ptr<IPreparedModelCallback>& cb) -> ndk::ScopedAStatus {
122         return makePreparedModelReturnImpl(launchStatus, returnStatus, preparedModel, cb);
123     };
124 }
125 
126 const std::vector<nn::TokenValuePair> kHints = {nn::TokenValuePair{.token = 0, .value = {1}}};
127 const std::vector<nn::ExtensionNameAndPrefix> kExtensionNameToPrefix = {
128         nn::ExtensionNameAndPrefix{.name = "com.android.nn_test", .prefix = 1}};
makePreparedModelWithConfigReturn(ErrorStatus launchStatus,ErrorStatus returnStatus,const std::shared_ptr<MockPreparedModel> & preparedModel)129 auto makePreparedModelWithConfigReturn(ErrorStatus launchStatus, ErrorStatus returnStatus,
130                                        const std::shared_ptr<MockPreparedModel>& preparedModel) {
131     return [launchStatus, returnStatus, preparedModel](
132                    const Model& /*model*/, const PrepareModelConfig& /*config*/,
133                    const std::shared_ptr<IPreparedModelCallback>& cb) -> ndk::ScopedAStatus {
134         return makePreparedModelReturnImpl(launchStatus, returnStatus, preparedModel, cb);
135     };
136 }
137 
makePreparedModelFromCacheReturn(ErrorStatus launchStatus,ErrorStatus returnStatus,const std::shared_ptr<MockPreparedModel> & preparedModel)138 auto makePreparedModelFromCacheReturn(ErrorStatus launchStatus, ErrorStatus returnStatus,
139                                       const std::shared_ptr<MockPreparedModel>& preparedModel) {
140     return [launchStatus, returnStatus, preparedModel](
141                    const int64_t& /*deadline*/,
142                    const std::vector<ndk::ScopedFileDescriptor>& /*modelCache*/,
143                    const std::vector<ndk::ScopedFileDescriptor>& /*dataCache*/,
144                    const std::vector<uint8_t>& /*token*/,
145                    const std::shared_ptr<IPreparedModelCallback>& cb) {
146         return makePreparedModelReturnImpl(launchStatus, returnStatus, preparedModel, cb);
147     };
148 }
149 
__anon40c1850f0702null150 constexpr auto makeGeneralFailure = [] {
151     return ndk::ScopedAStatus::fromServiceSpecificError(
152             static_cast<int32_t>(ErrorStatus::GENERAL_FAILURE));
153 };
__anon40c1850f0802null154 constexpr auto makeGeneralTransportFailure = [] {
155     return ndk::ScopedAStatus::fromStatus(STATUS_NO_MEMORY);
156 };
__anon40c1850f0902null157 constexpr auto makeDeadObjectFailure = [] {
158     return ndk::ScopedAStatus::fromStatus(STATUS_DEAD_OBJECT);
159 };
160 
161 class DeviceTest : public VersionedAidlUtilsTestBase {};
162 
163 }  // namespace
164 
TEST_P(DeviceTest,invalidName)165 TEST_P(DeviceTest, invalidName) {
166     // run test
167     const auto device = MockDevice::create();
168     const auto result = Device::create(kInvalidName, device, kVersion);
169 
170     // verify result
171     ASSERT_FALSE(result.has_value());
172     EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
173 }
174 
TEST_P(DeviceTest,invalidDevice)175 TEST_P(DeviceTest, invalidDevice) {
176     // run test
177     const auto result = Device::create(kName, kInvalidDevice, kVersion);
178 
179     // verify result
180     ASSERT_FALSE(result.has_value());
181     EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
182 }
183 
TEST_P(DeviceTest,getVersionStringError)184 TEST_P(DeviceTest, getVersionStringError) {
185     // setup call
186     const auto mockDevice = createMockDevice();
187     EXPECT_CALL(*mockDevice, getVersionString(_))
188             .Times(1)
189             .WillOnce(InvokeWithoutArgs(makeGeneralFailure));
190 
191     // run test
192     const auto result = Device::create(kName, mockDevice, kVersion);
193 
194     // verify result
195     ASSERT_FALSE(result.has_value());
196     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
197 }
198 
TEST_P(DeviceTest,getVersionStringTransportFailure)199 TEST_P(DeviceTest, getVersionStringTransportFailure) {
200     // setup call
201     const auto mockDevice = createMockDevice();
202     EXPECT_CALL(*mockDevice, getVersionString(_))
203             .Times(1)
204             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
205 
206     // run test
207     const auto result = Device::create(kName, mockDevice, kVersion);
208 
209     // verify result
210     ASSERT_FALSE(result.has_value());
211     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
212 }
213 
TEST_P(DeviceTest,getVersionStringDeadObject)214 TEST_P(DeviceTest, getVersionStringDeadObject) {
215     // setup call
216     const auto mockDevice = createMockDevice();
217     EXPECT_CALL(*mockDevice, getVersionString(_))
218             .Times(1)
219             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
220 
221     // run test
222     const auto result = Device::create(kName, mockDevice, kVersion);
223 
224     // verify result
225     ASSERT_FALSE(result.has_value());
226     EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
227 }
228 
TEST_P(DeviceTest,getTypeError)229 TEST_P(DeviceTest, getTypeError) {
230     // setup call
231     const auto mockDevice = createMockDevice();
232     EXPECT_CALL(*mockDevice, getType(_)).Times(1).WillOnce(InvokeWithoutArgs(makeGeneralFailure));
233 
234     // run test
235     const auto result = Device::create(kName, mockDevice, kVersion);
236 
237     // verify result
238     ASSERT_FALSE(result.has_value());
239     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
240 }
241 
TEST_P(DeviceTest,getTypeTransportFailure)242 TEST_P(DeviceTest, getTypeTransportFailure) {
243     // setup call
244     const auto mockDevice = createMockDevice();
245     EXPECT_CALL(*mockDevice, getType(_))
246             .Times(1)
247             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
248 
249     // run test
250     const auto result = Device::create(kName, mockDevice, kVersion);
251 
252     // verify result
253     ASSERT_FALSE(result.has_value());
254     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
255 }
256 
TEST_P(DeviceTest,getTypeDeadObject)257 TEST_P(DeviceTest, getTypeDeadObject) {
258     // setup call
259     const auto mockDevice = createMockDevice();
260     EXPECT_CALL(*mockDevice, getType(_))
261             .Times(1)
262             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
263 
264     // run test
265     const auto result = Device::create(kName, mockDevice, kVersion);
266 
267     // verify result
268     ASSERT_FALSE(result.has_value());
269     EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
270 }
271 
TEST_P(DeviceTest,getSupportedExtensionsError)272 TEST_P(DeviceTest, getSupportedExtensionsError) {
273     // setup call
274     const auto mockDevice = createMockDevice();
275     EXPECT_CALL(*mockDevice, getSupportedExtensions(_))
276             .Times(1)
277             .WillOnce(InvokeWithoutArgs(makeGeneralFailure));
278 
279     // run test
280     const auto result = Device::create(kName, mockDevice, kVersion);
281 
282     // verify result
283     ASSERT_FALSE(result.has_value());
284     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
285 }
286 
TEST_P(DeviceTest,getSupportedExtensionsTransportFailure)287 TEST_P(DeviceTest, getSupportedExtensionsTransportFailure) {
288     // setup call
289     const auto mockDevice = createMockDevice();
290     EXPECT_CALL(*mockDevice, getSupportedExtensions(_))
291             .Times(1)
292             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
293 
294     // run test
295     const auto result = Device::create(kName, mockDevice, kVersion);
296 
297     // verify result
298     ASSERT_FALSE(result.has_value());
299     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
300 }
301 
TEST_P(DeviceTest,getSupportedExtensionsDeadObject)302 TEST_P(DeviceTest, getSupportedExtensionsDeadObject) {
303     // setup call
304     const auto mockDevice = createMockDevice();
305     EXPECT_CALL(*mockDevice, getSupportedExtensions(_))
306             .Times(1)
307             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
308 
309     // run test
310     const auto result = Device::create(kName, mockDevice, kVersion);
311 
312     // verify result
313     ASSERT_FALSE(result.has_value());
314     EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
315 }
316 
TEST_P(DeviceTest,getNumberOfCacheFilesNeeded)317 TEST_P(DeviceTest, getNumberOfCacheFilesNeeded) {
318     // setup call
319     const auto mockDevice = createMockDevice();
320     EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1);
321 
322     // run test
323     const auto result = Device::create(kName, mockDevice, kVersion);
324 
325     // verify result
326     ASSERT_TRUE(result.has_value());
327     constexpr auto kNumberOfCacheFilesPair = std::make_pair<uint32_t, uint32_t>(
328             kNumberOfCacheFiles.numModelCache, kNumberOfCacheFiles.numDataCache);
329     EXPECT_EQ(result.value()->getNumberOfCacheFilesNeeded(), kNumberOfCacheFilesPair);
330 }
331 
TEST_P(DeviceTest,getNumberOfCacheFilesNeededError)332 TEST_P(DeviceTest, getNumberOfCacheFilesNeededError) {
333     // setup call
334     const auto mockDevice = createMockDevice();
335     EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
336             .Times(1)
337             .WillOnce(InvokeWithoutArgs(makeGeneralFailure));
338 
339     // run test
340     const auto result = Device::create(kName, mockDevice, kVersion);
341 
342     // verify result
343     ASSERT_FALSE(result.has_value());
344     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
345 }
346 
TEST_P(DeviceTest,dataCacheFilesExceedsSpecifiedMax)347 TEST_P(DeviceTest, dataCacheFilesExceedsSpecifiedMax) {
348     // setup test
349     const auto mockDevice = createMockDevice();
350     EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
351             .Times(1)
352             .WillOnce(DoAll(SetArgPointee<0>(NumberOfCacheFiles{
353                                     .numModelCache = nn::kMaxNumberOfCacheFiles + 1,
354                                     .numDataCache = nn::kMaxNumberOfCacheFiles}),
355                             InvokeWithoutArgs(makeStatusOk)));
356 
357     // run test
358     const auto result = Device::create(kName, mockDevice, kVersion);
359 
360     // verify result
361     ASSERT_FALSE(result.has_value());
362     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
363 }
364 
TEST_P(DeviceTest,modelCacheFilesExceedsSpecifiedMax)365 TEST_P(DeviceTest, modelCacheFilesExceedsSpecifiedMax) {
366     // setup test
367     const auto mockDevice = createMockDevice();
368     EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
369             .Times(1)
370             .WillOnce(DoAll(SetArgPointee<0>(NumberOfCacheFiles{
371                                     .numModelCache = nn::kMaxNumberOfCacheFiles,
372                                     .numDataCache = nn::kMaxNumberOfCacheFiles + 1}),
373                             InvokeWithoutArgs(makeStatusOk)));
374 
375     // run test
376     const auto result = Device::create(kName, mockDevice, kVersion);
377 
378     // verify result
379     ASSERT_FALSE(result.has_value());
380     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
381 }
382 
TEST_P(DeviceTest,getNumberOfCacheFilesNeededTransportFailure)383 TEST_P(DeviceTest, getNumberOfCacheFilesNeededTransportFailure) {
384     // setup call
385     const auto mockDevice = createMockDevice();
386     EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
387             .Times(1)
388             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
389 
390     // run test
391     const auto result = Device::create(kName, mockDevice, kVersion);
392 
393     // verify result
394     ASSERT_FALSE(result.has_value());
395     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
396 }
397 
TEST_P(DeviceTest,getNumberOfCacheFilesNeededDeadObject)398 TEST_P(DeviceTest, getNumberOfCacheFilesNeededDeadObject) {
399     // setup call
400     const auto mockDevice = createMockDevice();
401     EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
402             .Times(1)
403             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
404 
405     // run test
406     const auto result = Device::create(kName, mockDevice, kVersion);
407 
408     // verify result
409     ASSERT_FALSE(result.has_value());
410     EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
411 }
412 
TEST_P(DeviceTest,getCapabilitiesError)413 TEST_P(DeviceTest, getCapabilitiesError) {
414     // setup call
415     const auto mockDevice = createMockDevice();
416     EXPECT_CALL(*mockDevice, getCapabilities(_))
417             .Times(1)
418             .WillOnce(InvokeWithoutArgs(makeGeneralFailure));
419 
420     // run test
421     const auto result = Device::create(kName, mockDevice, kVersion);
422 
423     // verify result
424     ASSERT_FALSE(result.has_value());
425     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
426 }
427 
TEST_P(DeviceTest,getCapabilitiesTransportFailure)428 TEST_P(DeviceTest, getCapabilitiesTransportFailure) {
429     // setup call
430     const auto mockDevice = createMockDevice();
431     EXPECT_CALL(*mockDevice, getCapabilities(_))
432             .Times(1)
433             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
434 
435     // run test
436     const auto result = Device::create(kName, mockDevice, kVersion);
437 
438     // verify result
439     ASSERT_FALSE(result.has_value());
440     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
441 }
442 
TEST_P(DeviceTest,getCapabilitiesDeadObject)443 TEST_P(DeviceTest, getCapabilitiesDeadObject) {
444     // setup call
445     const auto mockDevice = createMockDevice();
446     EXPECT_CALL(*mockDevice, getCapabilities(_))
447             .Times(1)
448             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
449 
450     // run test
451     const auto result = Device::create(kName, mockDevice, kVersion);
452 
453     // verify result
454     ASSERT_FALSE(result.has_value());
455     EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
456 }
457 
TEST_P(DeviceTest,getName)458 TEST_P(DeviceTest, getName) {
459     // setup call
460     const auto mockDevice = createMockDevice();
461     const auto device = Device::create(kName, mockDevice, kVersion).value();
462 
463     // run test
464     const auto& name = device->getName();
465 
466     // verify result
467     EXPECT_EQ(name, kName);
468 }
469 
TEST_P(DeviceTest,getFeatureLevel)470 TEST_P(DeviceTest, getFeatureLevel) {
471     // setup call
472     const auto mockDevice = createMockDevice();
473     const auto device = Device::create(kName, mockDevice, kVersion).value();
474 
475     // run test
476     const auto featureLevel = device->getFeatureLevel();
477 
478     // verify result
479     EXPECT_EQ(featureLevel, kVersion);
480 }
481 
TEST_P(DeviceTest,getCachedData)482 TEST_P(DeviceTest, getCachedData) {
483     // setup call
484     const auto mockDevice = createMockDevice();
485     EXPECT_CALL(*mockDevice, getVersionString(_)).Times(1);
486     EXPECT_CALL(*mockDevice, getType(_)).Times(1);
487     EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(1);
488     EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1);
489     EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(1);
490 
491     const auto result = Device::create(kName, mockDevice, kVersion);
492     ASSERT_TRUE(result.has_value())
493             << "Failed with " << result.error().code << ": " << result.error().message;
494     const auto& device = result.value();
495 
496     // run test and verify results
497     EXPECT_EQ(device->getVersionString(), device->getVersionString());
498     EXPECT_EQ(device->getType(), device->getType());
499     EXPECT_EQ(device->getSupportedExtensions(), device->getSupportedExtensions());
500     EXPECT_EQ(device->getNumberOfCacheFilesNeeded(), device->getNumberOfCacheFilesNeeded());
501     EXPECT_EQ(device->getCapabilities(), device->getCapabilities());
502 }
503 
TEST_P(DeviceTest,getSupportedOperations)504 TEST_P(DeviceTest, getSupportedOperations) {
505     // setup call
506     const auto mockDevice = createMockDevice();
507     const auto device = Device::create(kName, mockDevice, kVersion).value();
508     EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
509             .Times(1)
510             .WillOnce(DoAll(
511                     SetArgPointee<1>(std::vector<bool>(kSimpleModel.main.operations.size(), true)),
512                     InvokeWithoutArgs(makeStatusOk)));
513 
514     // run test
515     const auto result = device->getSupportedOperations(kSimpleModel);
516 
517     // verify result
518     ASSERT_TRUE(result.has_value())
519             << "Failed with " << result.error().code << ": " << result.error().message;
520     const auto& supportedOperations = result.value();
521     EXPECT_EQ(supportedOperations.size(), kSimpleModel.main.operations.size());
522     EXPECT_THAT(supportedOperations, Each(testing::IsTrue()));
523 }
524 
TEST_P(DeviceTest,getSupportedOperationsError)525 TEST_P(DeviceTest, getSupportedOperationsError) {
526     // setup call
527     const auto mockDevice = createMockDevice();
528     const auto device = Device::create(kName, mockDevice, kVersion).value();
529     EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
530             .Times(1)
531             .WillOnce(InvokeWithoutArgs(makeGeneralFailure));
532 
533     // run test
534     const auto result = device->getSupportedOperations(kSimpleModel);
535 
536     // verify result
537     ASSERT_FALSE(result.has_value());
538     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
539 }
540 
TEST_P(DeviceTest,getSupportedOperationsTransportFailure)541 TEST_P(DeviceTest, getSupportedOperationsTransportFailure) {
542     // setup call
543     const auto mockDevice = createMockDevice();
544     const auto device = Device::create(kName, mockDevice, kVersion).value();
545     EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
546             .Times(1)
547             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
548 
549     // run test
550     const auto result = device->getSupportedOperations(kSimpleModel);
551 
552     // verify result
553     ASSERT_FALSE(result.has_value());
554     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
555 }
556 
TEST_P(DeviceTest,getSupportedOperationsDeadObject)557 TEST_P(DeviceTest, getSupportedOperationsDeadObject) {
558     // setup call
559     const auto mockDevice = createMockDevice();
560     const auto device = Device::create(kName, mockDevice, kVersion).value();
561     EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
562             .Times(1)
563             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
564 
565     // run test
566     const auto result = device->getSupportedOperations(kSimpleModel);
567 
568     // verify result
569     ASSERT_FALSE(result.has_value());
570     EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
571 }
572 
TEST_P(DeviceTest,prepareModel)573 TEST_P(DeviceTest, prepareModel) {
574     if (kVersion.level > nn::Version::Level::FEATURE_LEVEL_7) return;
575 
576     // setup call
577     const auto mockDevice = createMockDevice();
578     const auto device = Device::create(kName, mockDevice, kVersion).value();
579     const auto mockPreparedModel = MockPreparedModel::create();
580     EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
581             .Times(1)
582             .WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::NONE, ErrorStatus::NONE,
583                                                      mockPreparedModel)));
584 
585     // run test
586     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
587                                              nn::Priority::DEFAULT, {}, {}, {}, {}, {}, {});
588 
589     // verify result
590     ASSERT_TRUE(result.has_value())
591             << "Failed with " << result.error().code << ": " << result.error().message;
592     EXPECT_NE(result.value(), nullptr);
593 }
594 
TEST_P(DeviceTest,prepareModelLaunchError)595 TEST_P(DeviceTest, prepareModelLaunchError) {
596     if (kVersion.level > nn::Version::Level::FEATURE_LEVEL_7) return;
597 
598     // setup call
599     const auto mockDevice = createMockDevice();
600     const auto device = Device::create(kName, mockDevice, kVersion).value();
601     EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
602             .Times(1)
603             .WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::GENERAL_FAILURE,
604                                                      ErrorStatus::GENERAL_FAILURE, nullptr)));
605 
606     // run test
607     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
608                                              nn::Priority::DEFAULT, {}, {}, {}, {}, {}, {});
609 
610     // verify result
611     ASSERT_FALSE(result.has_value());
612     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
613 }
614 
TEST_P(DeviceTest,prepareModelReturnError)615 TEST_P(DeviceTest, prepareModelReturnError) {
616     if (kVersion.level > nn::Version::Level::FEATURE_LEVEL_7) return;
617 
618     // setup call
619     const auto mockDevice = createMockDevice();
620     const auto device = Device::create(kName, mockDevice, kVersion).value();
621     EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
622             .Times(1)
623             .WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::NONE,
624                                                      ErrorStatus::GENERAL_FAILURE, nullptr)));
625 
626     // run test
627     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
628                                              nn::Priority::DEFAULT, {}, {}, {}, {}, {}, {});
629 
630     // verify result
631     ASSERT_FALSE(result.has_value());
632     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
633 }
634 
TEST_P(DeviceTest,prepareModelNullptrError)635 TEST_P(DeviceTest, prepareModelNullptrError) {
636     if (kVersion.level > nn::Version::Level::FEATURE_LEVEL_7) return;
637 
638     // setup call
639     const auto mockDevice = createMockDevice();
640     const auto device = Device::create(kName, mockDevice, kVersion).value();
641     EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
642             .Times(1)
643             .WillOnce(
644                     Invoke(makePreparedModelReturn(ErrorStatus::NONE, ErrorStatus::NONE, nullptr)));
645 
646     // run test
647     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
648                                              nn::Priority::DEFAULT, {}, {}, {}, {}, {}, {});
649 
650     // verify result
651     ASSERT_FALSE(result.has_value());
652     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
653 }
654 
TEST_P(DeviceTest,prepareModelTransportFailure)655 TEST_P(DeviceTest, prepareModelTransportFailure) {
656     if (kVersion.level > nn::Version::Level::FEATURE_LEVEL_7) return;
657 
658     // setup call
659     const auto mockDevice = createMockDevice();
660     const auto device = Device::create(kName, mockDevice, kVersion).value();
661     EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
662             .Times(1)
663             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
664 
665     // run test
666     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
667                                              nn::Priority::DEFAULT, {}, {}, {}, {}, {}, {});
668 
669     // verify result
670     ASSERT_FALSE(result.has_value());
671     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
672 }
673 
TEST_P(DeviceTest,prepareModelDeadObject)674 TEST_P(DeviceTest, prepareModelDeadObject) {
675     if (kVersion.level > nn::Version::Level::FEATURE_LEVEL_7) return;
676 
677     // setup call
678     const auto mockDevice = createMockDevice();
679     const auto device = Device::create(kName, mockDevice, kVersion).value();
680     EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
681             .Times(1)
682             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
683 
684     // run test
685     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
686                                              nn::Priority::DEFAULT, {}, {}, {}, {}, {}, {});
687 
688     // verify result
689     ASSERT_FALSE(result.has_value());
690     EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
691 }
692 
TEST_P(DeviceTest,prepareModelAsyncCrash)693 TEST_P(DeviceTest, prepareModelAsyncCrash) {
694     if (kVersion.level > nn::Version::Level::FEATURE_LEVEL_7) return;
695 
696     // setup test
697     const auto mockDevice = createMockDevice();
698     const auto device = Device::create(kName, mockDevice, kVersion).value();
699     const auto ret = [&device]() {
700         DeathMonitor::serviceDied(
701                 reinterpret_cast<void*>(device->getDeathMonitor()->getCookieKey()));
702         return ndk::ScopedAStatus::ok();
703     };
704     EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
705             .Times(1)
706             .WillOnce(InvokeWithoutArgs(ret));
707 
708     // run test
709     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
710                                              nn::Priority::DEFAULT, {}, {}, {}, {}, {}, {});
711 
712     // verify result
713     ASSERT_FALSE(result.has_value());
714     EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
715 }
716 
TEST_P(DeviceTest,prepareModelWithConfig)717 TEST_P(DeviceTest, prepareModelWithConfig) {
718     if (kVersion.level < nn::Version::Level::FEATURE_LEVEL_8) return;
719 
720     // setup call
721     const auto mockDevice = createMockDevice();
722     const auto device = Device::create(kName, mockDevice, kVersion).value();
723     const auto mockPreparedModel = MockPreparedModel::create();
724     EXPECT_CALL(*mockDevice, prepareModelWithConfig(_, _, _))
725             .Times(1)
726             .WillOnce(Invoke(makePreparedModelWithConfigReturn(ErrorStatus::NONE, ErrorStatus::NONE,
727                                                                mockPreparedModel)));
728 
729     // run test
730     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
731                                              nn::Priority::DEFAULT, {}, {}, {}, {}, kHints,
732                                              kExtensionNameToPrefix);
733 
734     // verify result
735     ASSERT_TRUE(result.has_value())
736             << "Failed with " << result.error().code << ": " << result.error().message;
737     EXPECT_NE(result.value(), nullptr);
738 }
739 
TEST_P(DeviceTest,prepareModelWithConfigLaunchError)740 TEST_P(DeviceTest, prepareModelWithConfigLaunchError) {
741     if (kVersion.level < nn::Version::Level::FEATURE_LEVEL_8) return;
742 
743     // setup call
744     const auto mockDevice = createMockDevice();
745     const auto device = Device::create(kName, mockDevice, kVersion).value();
746     EXPECT_CALL(*mockDevice, prepareModelWithConfig(_, _, _))
747             .Times(1)
748             .WillOnce(Invoke(makePreparedModelWithConfigReturn(
749                     ErrorStatus::GENERAL_FAILURE, ErrorStatus::GENERAL_FAILURE, nullptr)));
750 
751     // run test
752     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
753                                              nn::Priority::DEFAULT, {}, {}, {}, {}, kHints,
754                                              kExtensionNameToPrefix);
755 
756     // verify result
757     ASSERT_FALSE(result.has_value());
758     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
759 }
760 
TEST_P(DeviceTest,prepareModelWithConfigReturnError)761 TEST_P(DeviceTest, prepareModelWithConfigReturnError) {
762     if (kVersion.level < nn::Version::Level::FEATURE_LEVEL_8) return;
763 
764     // setup call
765     const auto mockDevice = createMockDevice();
766     const auto device = Device::create(kName, mockDevice, kVersion).value();
767     EXPECT_CALL(*mockDevice, prepareModelWithConfig(_, _, _))
768             .Times(1)
769             .WillOnce(Invoke(makePreparedModelWithConfigReturn(
770                     ErrorStatus::NONE, ErrorStatus::GENERAL_FAILURE, nullptr)));
771 
772     // run test
773     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
774                                              nn::Priority::DEFAULT, {}, {}, {}, {}, kHints,
775                                              kExtensionNameToPrefix);
776 
777     // verify result
778     ASSERT_FALSE(result.has_value());
779     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
780 }
781 
TEST_P(DeviceTest,prepareModelWithConfigNullptrError)782 TEST_P(DeviceTest, prepareModelWithConfigNullptrError) {
783     if (kVersion.level < nn::Version::Level::FEATURE_LEVEL_8) return;
784 
785     // setup call
786     const auto mockDevice = createMockDevice();
787     const auto device = Device::create(kName, mockDevice, kVersion).value();
788     EXPECT_CALL(*mockDevice, prepareModelWithConfig(_, _, _))
789             .Times(1)
790             .WillOnce(Invoke(makePreparedModelWithConfigReturn(ErrorStatus::NONE, ErrorStatus::NONE,
791                                                                nullptr)));
792 
793     // run test
794     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
795                                              nn::Priority::DEFAULT, {}, {}, {}, {}, kHints,
796                                              kExtensionNameToPrefix);
797 
798     // verify result
799     ASSERT_FALSE(result.has_value());
800     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
801 }
802 
TEST_P(DeviceTest,prepareModelWithConfigTransportFailure)803 TEST_P(DeviceTest, prepareModelWithConfigTransportFailure) {
804     if (kVersion.level < nn::Version::Level::FEATURE_LEVEL_8) return;
805 
806     // setup call
807     const auto mockDevice = createMockDevice();
808     const auto device = Device::create(kName, mockDevice, kVersion).value();
809     EXPECT_CALL(*mockDevice, prepareModelWithConfig(_, _, _))
810             .Times(1)
811             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
812 
813     // run test
814     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
815                                              nn::Priority::DEFAULT, {}, {}, {}, {}, kHints,
816                                              kExtensionNameToPrefix);
817 
818     // verify result
819     ASSERT_FALSE(result.has_value());
820     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
821 }
822 
TEST_P(DeviceTest,prepareModelWithConfigDeadObject)823 TEST_P(DeviceTest, prepareModelWithConfigDeadObject) {
824     if (kVersion.level < nn::Version::Level::FEATURE_LEVEL_8) return;
825 
826     // setup call
827     const auto mockDevice = createMockDevice();
828     const auto device = Device::create(kName, mockDevice, kVersion).value();
829     EXPECT_CALL(*mockDevice, prepareModelWithConfig(_, _, _))
830             .Times(1)
831             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
832 
833     // run test
834     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
835                                              nn::Priority::DEFAULT, {}, {}, {}, {}, kHints,
836                                              kExtensionNameToPrefix);
837 
838     // verify result
839     ASSERT_FALSE(result.has_value());
840     EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
841 }
842 
TEST_P(DeviceTest,prepareModelWithConfigAsyncCrash)843 TEST_P(DeviceTest, prepareModelWithConfigAsyncCrash) {
844     if (kVersion.level < nn::Version::Level::FEATURE_LEVEL_8) return;
845 
846     // setup test
847     const auto mockDevice = createMockDevice();
848     const auto device = Device::create(kName, mockDevice, kVersion).value();
849     const auto ret = [&device]() {
850         DeathMonitor::serviceDied(
851                 reinterpret_cast<void*>(device->getDeathMonitor()->getCookieKey()));
852         return ndk::ScopedAStatus::ok();
853     };
854     EXPECT_CALL(*mockDevice, prepareModelWithConfig(_, _, _))
855             .Times(1)
856             .WillOnce(InvokeWithoutArgs(ret));
857 
858     // run test
859     const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
860                                              nn::Priority::DEFAULT, {}, {}, {}, {}, kHints,
861                                              kExtensionNameToPrefix);
862 
863     // verify result
864     ASSERT_FALSE(result.has_value());
865     EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
866 }
867 
TEST_P(DeviceTest,prepareModelFromCache)868 TEST_P(DeviceTest, prepareModelFromCache) {
869     // setup call
870     const auto mockDevice = createMockDevice();
871     const auto device = Device::create(kName, mockDevice, kVersion).value();
872     const auto mockPreparedModel = MockPreparedModel::create();
873     EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
874             .Times(1)
875             .WillOnce(Invoke(makePreparedModelFromCacheReturn(ErrorStatus::NONE, ErrorStatus::NONE,
876                                                               mockPreparedModel)));
877 
878     // run test
879     const auto result = device->prepareModelFromCache({}, {}, {}, {});
880 
881     // verify result
882     ASSERT_TRUE(result.has_value())
883             << "Failed with " << result.error().code << ": " << result.error().message;
884     EXPECT_NE(result.value(), nullptr);
885 }
886 
TEST_P(DeviceTest,prepareModelFromCacheLaunchError)887 TEST_P(DeviceTest, prepareModelFromCacheLaunchError) {
888     // setup call
889     const auto mockDevice = createMockDevice();
890     const auto device = Device::create(kName, mockDevice, kVersion).value();
891     EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
892             .Times(1)
893             .WillOnce(Invoke(makePreparedModelFromCacheReturn(
894                     ErrorStatus::GENERAL_FAILURE, ErrorStatus::GENERAL_FAILURE, nullptr)));
895 
896     // run test
897     const auto result = device->prepareModelFromCache({}, {}, {}, {});
898 
899     // verify result
900     ASSERT_FALSE(result.has_value());
901     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
902 }
903 
TEST_P(DeviceTest,prepareModelFromCacheReturnError)904 TEST_P(DeviceTest, prepareModelFromCacheReturnError) {
905     // setup call
906     const auto mockDevice = createMockDevice();
907     const auto device = Device::create(kName, mockDevice, kVersion).value();
908     EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
909             .Times(1)
910             .WillOnce(Invoke(makePreparedModelFromCacheReturn(
911                     ErrorStatus::NONE, ErrorStatus::GENERAL_FAILURE, nullptr)));
912 
913     // run test
914     const auto result = device->prepareModelFromCache({}, {}, {}, {});
915 
916     // verify result
917     ASSERT_FALSE(result.has_value());
918     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
919 }
920 
TEST_P(DeviceTest,prepareModelFromCacheNullptrError)921 TEST_P(DeviceTest, prepareModelFromCacheNullptrError) {
922     // setup call
923     const auto mockDevice = createMockDevice();
924     const auto device = Device::create(kName, mockDevice, kVersion).value();
925     EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
926             .Times(1)
927             .WillOnce(Invoke(makePreparedModelFromCacheReturn(ErrorStatus::NONE, ErrorStatus::NONE,
928                                                               nullptr)));
929 
930     // run test
931     const auto result = device->prepareModelFromCache({}, {}, {}, {});
932 
933     // verify result
934     ASSERT_FALSE(result.has_value());
935     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
936 }
937 
TEST_P(DeviceTest,prepareModelFromCacheTransportFailure)938 TEST_P(DeviceTest, prepareModelFromCacheTransportFailure) {
939     // setup call
940     const auto mockDevice = createMockDevice();
941     const auto device = Device::create(kName, mockDevice, kVersion).value();
942     EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
943             .Times(1)
944             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
945 
946     // run test
947     const auto result = device->prepareModelFromCache({}, {}, {}, {});
948 
949     // verify result
950     ASSERT_FALSE(result.has_value());
951     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
952 }
953 
TEST_P(DeviceTest,prepareModelFromCacheDeadObject)954 TEST_P(DeviceTest, prepareModelFromCacheDeadObject) {
955     // setup call
956     const auto mockDevice = createMockDevice();
957     const auto device = Device::create(kName, mockDevice, kVersion).value();
958     EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
959             .Times(1)
960             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
961 
962     // run test
963     const auto result = device->prepareModelFromCache({}, {}, {}, {});
964 
965     // verify result
966     ASSERT_FALSE(result.has_value());
967     EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
968 }
969 
TEST_P(DeviceTest,prepareModelFromCacheAsyncCrash)970 TEST_P(DeviceTest, prepareModelFromCacheAsyncCrash) {
971     // setup test
972     const auto mockDevice = createMockDevice();
973     const auto device = Device::create(kName, mockDevice, kVersion).value();
974     const auto ret = [&device]() {
975         DeathMonitor::serviceDied(
976                 reinterpret_cast<void*>(device->getDeathMonitor()->getCookieKey()));
977         return ndk::ScopedAStatus::ok();
978     };
979     EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
980             .Times(1)
981             .WillOnce(InvokeWithoutArgs(ret));
982 
983     // run test
984     const auto result = device->prepareModelFromCache({}, {}, {}, {});
985 
986     // verify result
987     ASSERT_FALSE(result.has_value());
988     EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
989 }
990 
TEST_P(DeviceTest,allocate)991 TEST_P(DeviceTest, allocate) {
992     // setup call
993     const auto mockDevice = createMockDevice();
994     const auto device = Device::create(kName, mockDevice, kVersion).value();
995     const auto mockBuffer = DeviceBuffer{.buffer = MockBuffer::create(), .token = 1};
996     EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
997             .Times(1)
998             .WillOnce(DoAll(SetArgPointee<4>(mockBuffer), InvokeWithoutArgs(makeStatusOk)));
999 
1000     // run test
1001     const auto result = device->allocate({}, {}, {}, {});
1002 
1003     // verify result
1004     ASSERT_TRUE(result.has_value())
1005             << "Failed with " << result.error().code << ": " << result.error().message;
1006     EXPECT_NE(result.value(), nullptr);
1007 }
1008 
TEST_P(DeviceTest,allocateError)1009 TEST_P(DeviceTest, allocateError) {
1010     // setup call
1011     const auto mockDevice = createMockDevice();
1012     const auto device = Device::create(kName, mockDevice, kVersion).value();
1013     EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
1014             .Times(1)
1015             .WillOnce(InvokeWithoutArgs(makeGeneralFailure));
1016 
1017     // run test
1018     const auto result = device->allocate({}, {}, {}, {});
1019 
1020     // verify result
1021     ASSERT_FALSE(result.has_value());
1022     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
1023 }
1024 
TEST_P(DeviceTest,allocateTransportFailure)1025 TEST_P(DeviceTest, allocateTransportFailure) {
1026     // setup call
1027     const auto mockDevice = createMockDevice();
1028     const auto device = Device::create(kName, mockDevice, kVersion).value();
1029     EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
1030             .Times(1)
1031             .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
1032 
1033     // run test
1034     const auto result = device->allocate({}, {}, {}, {});
1035 
1036     // verify result
1037     ASSERT_FALSE(result.has_value());
1038     EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
1039 }
1040 
TEST_P(DeviceTest,allocateDeadObject)1041 TEST_P(DeviceTest, allocateDeadObject) {
1042     // setup call
1043     const auto mockDevice = createMockDevice();
1044     const auto device = Device::create(kName, mockDevice, kVersion).value();
1045     EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
1046             .Times(1)
1047             .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
1048 
1049     // run test
1050     const auto result = device->allocate({}, {}, {}, {});
1051 
1052     // verify result
1053     ASSERT_FALSE(result.has_value());
1054     EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
1055 }
1056 
1057 INSTANTIATE_VERSIONED_AIDL_UTILS_TEST(DeviceTest, kAllAidlVersions);
1058 
1059 }  // namespace aidl::android::hardware::neuralnetworks::utils
1060