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