blob: 0366e7dff03d4ef3043fa6ae00da21b3815596c1 [file] [log] [blame]
Lev Proleev900c28a2021-01-26 19:40:20 +00001/*
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
21#include <aidl/android/hardware/neuralnetworks/BnDevice.h>
22#include <android/binder_auto_utils.h>
23#include <android/binder_status.h>
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
26#include <nnapi/IDevice.h>
27#include <nnapi/TypeUtils.h>
28#include <nnapi/Types.h>
29#include <nnapi/hal/aidl/Device.h>
30
31#include <functional>
32#include <memory>
33#include <string>
34
35namespace aidl::android::hardware::neuralnetworks::utils {
36namespace {
37
38namespace nn = ::android::nn;
39using ::testing::_;
40using ::testing::DoAll;
41using ::testing::Invoke;
42using ::testing::InvokeWithoutArgs;
43using ::testing::SetArgPointee;
44
45const nn::Model kSimpleModel = {
46 .main = {.operands = {{.type = nn::OperandType::TENSOR_FLOAT32,
47 .dimensions = {1},
48 .lifetime = nn::Operand::LifeTime::SUBGRAPH_INPUT},
49 {.type = nn::OperandType::TENSOR_FLOAT32,
50 .dimensions = {1},
51 .lifetime = nn::Operand::LifeTime::SUBGRAPH_OUTPUT}},
52 .operations = {{.type = nn::OperationType::RELU, .inputs = {0}, .outputs = {1}}},
53 .inputIndexes = {0},
54 .outputIndexes = {1}}};
55
56const std::string kName = "Google-MockV1";
57const std::string kInvalidName = "";
58const std::shared_ptr<BnDevice> kInvalidDevice;
59constexpr PerformanceInfo kNoPerformanceInfo = {.execTime = std::numeric_limits<float>::max(),
60 .powerUsage = std::numeric_limits<float>::max()};
Lev Proleeva31aff12021-06-28 13:10:54 +010061constexpr NumberOfCacheFiles kNumberOfCacheFiles = {.numModelCache = nn::kMaxNumberOfCacheFiles - 1,
Lev Proleev900c28a2021-01-26 19:40:20 +000062 .numDataCache = nn::kMaxNumberOfCacheFiles};
63
64constexpr auto makeStatusOk = [] { return ndk::ScopedAStatus::ok(); };
65
66std::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
102constexpr auto makePreparedModelReturnImpl =
103 [](ErrorStatus launchStatus, ErrorStatus returnStatus,
104 const std::shared_ptr<MockPreparedModel>& preparedModel,
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
113auto 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
126auto makePreparedModelFromCacheReturn(ErrorStatus launchStatus, ErrorStatus returnStatus,
127 const std::shared_ptr<MockPreparedModel>& preparedModel) {
128 return [launchStatus, returnStatus, preparedModel](
129 const int64_t& /*deadline*/,
130 const std::vector<ndk::ScopedFileDescriptor>& /*modelCache*/,
131 const std::vector<ndk::ScopedFileDescriptor>& /*dataCache*/,
132 const std::vector<uint8_t>& /*token*/,
133 const std::shared_ptr<IPreparedModelCallback>& cb) {
134 return makePreparedModelReturnImpl(launchStatus, returnStatus, preparedModel, cb);
135 };
136}
137
138constexpr auto makeGeneralFailure = [] {
139 return ndk::ScopedAStatus::fromServiceSpecificError(
140 static_cast<int32_t>(ErrorStatus::GENERAL_FAILURE));
141};
142constexpr auto makeGeneralTransportFailure = [] {
143 return ndk::ScopedAStatus::fromStatus(STATUS_NO_MEMORY);
144};
145constexpr auto makeDeadObjectFailure = [] {
146 return ndk::ScopedAStatus::fromStatus(STATUS_DEAD_OBJECT);
147};
148
Michael Butlerabc86912021-10-28 01:54:26 +0000149class DeviceTest : public ::testing::TestWithParam<nn::Version> {
150 protected:
151 const nn::Version kVersion = GetParam();
152};
153
154std::string printDeviceTest(const testing::TestParamInfo<nn::Version>& info) {
Michael Butler34f0a8f2021-11-11 20:07:46 -0800155 const nn::Version version = info.param;
156 CHECK(!version.runtimeOnlyFeatures);
157 switch (version.level) {
Michael Butler60a7b862021-11-17 16:33:35 -0800158 case nn::Version::Level::FEATURE_LEVEL_5:
Michael Butlerabc86912021-10-28 01:54:26 +0000159 return "v1";
Michael Butler34f0a8f2021-11-11 20:07:46 -0800160 case nn::Version::Level::FEATURE_LEVEL_6:
Michael Butlerabc86912021-10-28 01:54:26 +0000161 return "v2";
David Gross2edfc462021-11-17 15:39:18 -0800162 case nn::Version::Level::FEATURE_LEVEL_7:
163 return "v3";
Michael Butlerabc86912021-10-28 01:54:26 +0000164 default:
Michael Butler34f0a8f2021-11-11 20:07:46 -0800165 LOG(FATAL) << "Invalid AIDL version: " << version;
Michael Butlerabc86912021-10-28 01:54:26 +0000166 return "invalid";
167 }
168}
169
Lev Proleev900c28a2021-01-26 19:40:20 +0000170} // namespace
171
Michael Butlerabc86912021-10-28 01:54:26 +0000172TEST_P(DeviceTest, invalidName) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000173 // run test
174 const auto device = MockDevice::create();
Michael Butlerabc86912021-10-28 01:54:26 +0000175 const auto result = Device::create(kInvalidName, device, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000176
177 // verify result
178 ASSERT_FALSE(result.has_value());
179 EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
180}
181
Michael Butlerabc86912021-10-28 01:54:26 +0000182TEST_P(DeviceTest, invalidDevice) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000183 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000184 const auto result = Device::create(kName, kInvalidDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000185
186 // verify result
187 ASSERT_FALSE(result.has_value());
188 EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
189}
190
Michael Butlerabc86912021-10-28 01:54:26 +0000191TEST_P(DeviceTest, getVersionStringError) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000192 // setup call
193 const auto mockDevice = createMockDevice();
194 EXPECT_CALL(*mockDevice, getVersionString(_))
195 .Times(1)
196 .WillOnce(InvokeWithoutArgs(makeGeneralFailure));
197
198 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000199 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000200
201 // verify result
202 ASSERT_FALSE(result.has_value());
203 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
204}
205
Michael Butlerabc86912021-10-28 01:54:26 +0000206TEST_P(DeviceTest, getVersionStringTransportFailure) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000207 // setup call
208 const auto mockDevice = createMockDevice();
209 EXPECT_CALL(*mockDevice, getVersionString(_))
210 .Times(1)
211 .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
212
213 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000214 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000215
216 // verify result
217 ASSERT_FALSE(result.has_value());
218 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
219}
220
Michael Butlerabc86912021-10-28 01:54:26 +0000221TEST_P(DeviceTest, getVersionStringDeadObject) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000222 // setup call
223 const auto mockDevice = createMockDevice();
224 EXPECT_CALL(*mockDevice, getVersionString(_))
225 .Times(1)
226 .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
227
228 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000229 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000230
231 // verify result
232 ASSERT_FALSE(result.has_value());
233 EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
234}
235
Michael Butlerabc86912021-10-28 01:54:26 +0000236TEST_P(DeviceTest, getTypeError) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000237 // setup call
238 const auto mockDevice = createMockDevice();
239 EXPECT_CALL(*mockDevice, getType(_)).Times(1).WillOnce(InvokeWithoutArgs(makeGeneralFailure));
240
241 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000242 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000243
244 // verify result
245 ASSERT_FALSE(result.has_value());
246 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
247}
248
Michael Butlerabc86912021-10-28 01:54:26 +0000249TEST_P(DeviceTest, getTypeTransportFailure) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000250 // setup call
251 const auto mockDevice = createMockDevice();
252 EXPECT_CALL(*mockDevice, getType(_))
253 .Times(1)
254 .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
255
256 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000257 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000258
259 // verify result
260 ASSERT_FALSE(result.has_value());
261 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
262}
263
Michael Butlerabc86912021-10-28 01:54:26 +0000264TEST_P(DeviceTest, getTypeDeadObject) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000265 // setup call
266 const auto mockDevice = createMockDevice();
267 EXPECT_CALL(*mockDevice, getType(_))
268 .Times(1)
269 .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
270
271 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000272 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000273
274 // verify result
275 ASSERT_FALSE(result.has_value());
276 EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
277}
278
Michael Butlerabc86912021-10-28 01:54:26 +0000279TEST_P(DeviceTest, getSupportedExtensionsError) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000280 // setup call
281 const auto mockDevice = createMockDevice();
282 EXPECT_CALL(*mockDevice, getSupportedExtensions(_))
283 .Times(1)
284 .WillOnce(InvokeWithoutArgs(makeGeneralFailure));
285
286 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000287 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000288
289 // verify result
290 ASSERT_FALSE(result.has_value());
291 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
292}
293
Michael Butlerabc86912021-10-28 01:54:26 +0000294TEST_P(DeviceTest, getSupportedExtensionsTransportFailure) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000295 // setup call
296 const auto mockDevice = createMockDevice();
297 EXPECT_CALL(*mockDevice, getSupportedExtensions(_))
298 .Times(1)
299 .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
300
301 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000302 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000303
304 // verify result
305 ASSERT_FALSE(result.has_value());
306 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
307}
308
Michael Butlerabc86912021-10-28 01:54:26 +0000309TEST_P(DeviceTest, getSupportedExtensionsDeadObject) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000310 // setup call
311 const auto mockDevice = createMockDevice();
312 EXPECT_CALL(*mockDevice, getSupportedExtensions(_))
313 .Times(1)
314 .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
315
316 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000317 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000318
319 // verify result
320 ASSERT_FALSE(result.has_value());
321 EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
322}
323
Michael Butlerabc86912021-10-28 01:54:26 +0000324TEST_P(DeviceTest, getNumberOfCacheFilesNeeded) {
Lev Proleeva31aff12021-06-28 13:10:54 +0100325 // setup call
326 const auto mockDevice = createMockDevice();
327 EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1);
328
329 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000330 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleeva31aff12021-06-28 13:10:54 +0100331
332 // verify result
333 ASSERT_TRUE(result.has_value());
334 constexpr auto kNumberOfCacheFilesPair = std::make_pair<uint32_t, uint32_t>(
335 kNumberOfCacheFiles.numModelCache, kNumberOfCacheFiles.numDataCache);
336 EXPECT_EQ(result.value()->getNumberOfCacheFilesNeeded(), kNumberOfCacheFilesPair);
337}
338
Michael Butlerabc86912021-10-28 01:54:26 +0000339TEST_P(DeviceTest, getNumberOfCacheFilesNeededError) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000340 // setup call
341 const auto mockDevice = createMockDevice();
342 EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
343 .Times(1)
344 .WillOnce(InvokeWithoutArgs(makeGeneralFailure));
345
346 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000347 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000348
349 // verify result
350 ASSERT_FALSE(result.has_value());
351 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
352}
353
Michael Butlerabc86912021-10-28 01:54:26 +0000354TEST_P(DeviceTest, dataCacheFilesExceedsSpecifiedMax) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000355 // setup test
356 const auto mockDevice = createMockDevice();
357 EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
358 .Times(1)
359 .WillOnce(DoAll(SetArgPointee<0>(NumberOfCacheFiles{
360 .numModelCache = nn::kMaxNumberOfCacheFiles + 1,
361 .numDataCache = nn::kMaxNumberOfCacheFiles}),
362 InvokeWithoutArgs(makeStatusOk)));
363
364 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000365 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000366
367 // verify result
368 ASSERT_FALSE(result.has_value());
369 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
370}
371
Michael Butlerabc86912021-10-28 01:54:26 +0000372TEST_P(DeviceTest, modelCacheFilesExceedsSpecifiedMax) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000373 // setup test
374 const auto mockDevice = createMockDevice();
375 EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
376 .Times(1)
377 .WillOnce(DoAll(SetArgPointee<0>(NumberOfCacheFiles{
378 .numModelCache = nn::kMaxNumberOfCacheFiles,
379 .numDataCache = nn::kMaxNumberOfCacheFiles + 1}),
380 InvokeWithoutArgs(makeStatusOk)));
381
382 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000383 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000384
385 // verify result
386 ASSERT_FALSE(result.has_value());
387 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
388}
389
Michael Butlerabc86912021-10-28 01:54:26 +0000390TEST_P(DeviceTest, getNumberOfCacheFilesNeededTransportFailure) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000391 // setup call
392 const auto mockDevice = createMockDevice();
393 EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
394 .Times(1)
395 .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
396
397 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000398 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000399
400 // verify result
401 ASSERT_FALSE(result.has_value());
402 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
403}
404
Michael Butlerabc86912021-10-28 01:54:26 +0000405TEST_P(DeviceTest, getNumberOfCacheFilesNeededDeadObject) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000406 // setup call
407 const auto mockDevice = createMockDevice();
408 EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
409 .Times(1)
410 .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
411
412 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000413 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000414
415 // verify result
416 ASSERT_FALSE(result.has_value());
417 EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
418}
419
Michael Butlerabc86912021-10-28 01:54:26 +0000420TEST_P(DeviceTest, getCapabilitiesError) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000421 // setup call
422 const auto mockDevice = createMockDevice();
423 EXPECT_CALL(*mockDevice, getCapabilities(_))
424 .Times(1)
425 .WillOnce(InvokeWithoutArgs(makeGeneralFailure));
426
427 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000428 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000429
430 // verify result
431 ASSERT_FALSE(result.has_value());
432 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
433}
434
Michael Butlerabc86912021-10-28 01:54:26 +0000435TEST_P(DeviceTest, getCapabilitiesTransportFailure) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000436 // setup call
437 const auto mockDevice = createMockDevice();
438 EXPECT_CALL(*mockDevice, getCapabilities(_))
439 .Times(1)
440 .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
441
442 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000443 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000444
445 // verify result
446 ASSERT_FALSE(result.has_value());
447 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
448}
449
Michael Butlerabc86912021-10-28 01:54:26 +0000450TEST_P(DeviceTest, getCapabilitiesDeadObject) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000451 // setup call
452 const auto mockDevice = createMockDevice();
453 EXPECT_CALL(*mockDevice, getCapabilities(_))
454 .Times(1)
455 .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
456
457 // run test
Michael Butlerabc86912021-10-28 01:54:26 +0000458 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000459
460 // verify result
461 ASSERT_FALSE(result.has_value());
462 EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
463}
464
Michael Butlerabc86912021-10-28 01:54:26 +0000465TEST_P(DeviceTest, getName) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000466 // setup call
467 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000468 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000469
470 // run test
471 const auto& name = device->getName();
472
473 // verify result
474 EXPECT_EQ(name, kName);
475}
476
Michael Butlerabc86912021-10-28 01:54:26 +0000477TEST_P(DeviceTest, getFeatureLevel) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000478 // setup call
479 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000480 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000481
482 // run test
483 const auto featureLevel = device->getFeatureLevel();
484
485 // verify result
Michael Butlerabc86912021-10-28 01:54:26 +0000486 EXPECT_EQ(featureLevel, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000487}
488
Michael Butlerabc86912021-10-28 01:54:26 +0000489TEST_P(DeviceTest, getCachedData) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000490 // setup call
491 const auto mockDevice = createMockDevice();
492 EXPECT_CALL(*mockDevice, getVersionString(_)).Times(1);
493 EXPECT_CALL(*mockDevice, getType(_)).Times(1);
494 EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(1);
495 EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1);
496 EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(1);
497
Michael Butlerabc86912021-10-28 01:54:26 +0000498 const auto result = Device::create(kName, mockDevice, kVersion);
Lev Proleev900c28a2021-01-26 19:40:20 +0000499 ASSERT_TRUE(result.has_value())
500 << "Failed with " << result.error().code << ": " << result.error().message;
501 const auto& device = result.value();
502
503 // run test and verify results
504 EXPECT_EQ(device->getVersionString(), device->getVersionString());
505 EXPECT_EQ(device->getType(), device->getType());
506 EXPECT_EQ(device->getSupportedExtensions(), device->getSupportedExtensions());
507 EXPECT_EQ(device->getNumberOfCacheFilesNeeded(), device->getNumberOfCacheFilesNeeded());
508 EXPECT_EQ(device->getCapabilities(), device->getCapabilities());
509}
510
Michael Butlerabc86912021-10-28 01:54:26 +0000511TEST_P(DeviceTest, getSupportedOperations) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000512 // setup call
513 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000514 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000515 EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
516 .Times(1)
517 .WillOnce(DoAll(
518 SetArgPointee<1>(std::vector<bool>(kSimpleModel.main.operations.size(), true)),
519 InvokeWithoutArgs(makeStatusOk)));
520
521 // run test
522 const auto result = device->getSupportedOperations(kSimpleModel);
523
524 // verify result
525 ASSERT_TRUE(result.has_value())
526 << "Failed with " << result.error().code << ": " << result.error().message;
527 const auto& supportedOperations = result.value();
528 EXPECT_EQ(supportedOperations.size(), kSimpleModel.main.operations.size());
529 EXPECT_THAT(supportedOperations, Each(testing::IsTrue()));
530}
531
Michael Butlerabc86912021-10-28 01:54:26 +0000532TEST_P(DeviceTest, getSupportedOperationsError) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000533 // setup call
534 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000535 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000536 EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
537 .Times(1)
538 .WillOnce(InvokeWithoutArgs(makeGeneralFailure));
539
540 // run test
541 const auto result = device->getSupportedOperations(kSimpleModel);
542
543 // verify result
544 ASSERT_FALSE(result.has_value());
545 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
546}
547
Michael Butlerabc86912021-10-28 01:54:26 +0000548TEST_P(DeviceTest, getSupportedOperationsTransportFailure) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000549 // setup call
550 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000551 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000552 EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
553 .Times(1)
554 .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
555
556 // run test
557 const auto result = device->getSupportedOperations(kSimpleModel);
558
559 // verify result
560 ASSERT_FALSE(result.has_value());
561 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
562}
563
Michael Butlerabc86912021-10-28 01:54:26 +0000564TEST_P(DeviceTest, getSupportedOperationsDeadObject) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000565 // setup call
566 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000567 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000568 EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
569 .Times(1)
570 .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
571
572 // run test
573 const auto result = device->getSupportedOperations(kSimpleModel);
574
575 // verify result
576 ASSERT_FALSE(result.has_value());
577 EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
578}
579
Michael Butlerabc86912021-10-28 01:54:26 +0000580TEST_P(DeviceTest, prepareModel) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000581 // setup call
582 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000583 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000584 const auto mockPreparedModel = MockPreparedModel::create();
585 EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
586 .Times(1)
587 .WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::NONE, ErrorStatus::NONE,
588 mockPreparedModel)));
589
590 // run test
591 const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
592 nn::Priority::DEFAULT, {}, {}, {}, {});
593
594 // verify result
595 ASSERT_TRUE(result.has_value())
596 << "Failed with " << result.error().code << ": " << result.error().message;
597 EXPECT_NE(result.value(), nullptr);
598}
599
Michael Butlerabc86912021-10-28 01:54:26 +0000600TEST_P(DeviceTest, prepareModelLaunchError) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000601 // setup call
602 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000603 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000604 EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
605 .Times(1)
606 .WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::GENERAL_FAILURE,
607 ErrorStatus::GENERAL_FAILURE, nullptr)));
608
609 // run test
610 const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
611 nn::Priority::DEFAULT, {}, {}, {}, {});
612
613 // verify result
614 ASSERT_FALSE(result.has_value());
615 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
616}
617
Michael Butlerabc86912021-10-28 01:54:26 +0000618TEST_P(DeviceTest, prepareModelReturnError) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000619 // setup call
620 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000621 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000622 EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
623 .Times(1)
624 .WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::NONE,
625 ErrorStatus::GENERAL_FAILURE, nullptr)));
626
627 // run test
628 const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
629 nn::Priority::DEFAULT, {}, {}, {}, {});
630
631 // verify result
632 ASSERT_FALSE(result.has_value());
633 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
634}
635
Michael Butlerabc86912021-10-28 01:54:26 +0000636TEST_P(DeviceTest, prepareModelNullptrError) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000637 // setup call
638 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000639 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000640 EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
641 .Times(1)
642 .WillOnce(
643 Invoke(makePreparedModelReturn(ErrorStatus::NONE, ErrorStatus::NONE, nullptr)));
644
645 // run test
646 const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
647 nn::Priority::DEFAULT, {}, {}, {}, {});
648
649 // verify result
650 ASSERT_FALSE(result.has_value());
651 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
652}
653
Michael Butlerabc86912021-10-28 01:54:26 +0000654TEST_P(DeviceTest, prepareModelTransportFailure) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000655 // setup call
656 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000657 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000658 EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
659 .Times(1)
660 .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
661
662 // run test
663 const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
664 nn::Priority::DEFAULT, {}, {}, {}, {});
665
666 // verify result
667 ASSERT_FALSE(result.has_value());
668 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
669}
670
Michael Butlerabc86912021-10-28 01:54:26 +0000671TEST_P(DeviceTest, prepareModelDeadObject) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000672 // setup call
673 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000674 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000675 EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
676 .Times(1)
677 .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
678
679 // run test
680 const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
681 nn::Priority::DEFAULT, {}, {}, {}, {});
682
683 // verify result
684 ASSERT_FALSE(result.has_value());
685 EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
686}
687
Michael Butlerabc86912021-10-28 01:54:26 +0000688TEST_P(DeviceTest, prepareModelAsyncCrash) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000689 // setup test
690 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000691 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000692 const auto ret = [&device]() {
693 DeathMonitor::serviceDied(device->getDeathMonitor());
694 return ndk::ScopedAStatus::ok();
695 };
696 EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
697 .Times(1)
698 .WillOnce(InvokeWithoutArgs(ret));
699
700 // run test
701 const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
702 nn::Priority::DEFAULT, {}, {}, {}, {});
703
704 // verify result
705 ASSERT_FALSE(result.has_value());
706 EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
707}
708
Michael Butlerabc86912021-10-28 01:54:26 +0000709TEST_P(DeviceTest, prepareModelFromCache) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000710 // setup call
711 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000712 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000713 const auto mockPreparedModel = MockPreparedModel::create();
714 EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
715 .Times(1)
716 .WillOnce(Invoke(makePreparedModelFromCacheReturn(ErrorStatus::NONE, ErrorStatus::NONE,
717 mockPreparedModel)));
718
719 // run test
720 const auto result = device->prepareModelFromCache({}, {}, {}, {});
721
722 // verify result
723 ASSERT_TRUE(result.has_value())
724 << "Failed with " << result.error().code << ": " << result.error().message;
725 EXPECT_NE(result.value(), nullptr);
726}
727
Michael Butlerabc86912021-10-28 01:54:26 +0000728TEST_P(DeviceTest, prepareModelFromCacheLaunchError) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000729 // setup call
730 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000731 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000732 EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
733 .Times(1)
734 .WillOnce(Invoke(makePreparedModelFromCacheReturn(
735 ErrorStatus::GENERAL_FAILURE, ErrorStatus::GENERAL_FAILURE, nullptr)));
736
737 // run test
738 const auto result = device->prepareModelFromCache({}, {}, {}, {});
739
740 // verify result
741 ASSERT_FALSE(result.has_value());
742 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
743}
744
Michael Butlerabc86912021-10-28 01:54:26 +0000745TEST_P(DeviceTest, prepareModelFromCacheReturnError) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000746 // setup call
747 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000748 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000749 EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
750 .Times(1)
751 .WillOnce(Invoke(makePreparedModelFromCacheReturn(
752 ErrorStatus::NONE, ErrorStatus::GENERAL_FAILURE, nullptr)));
753
754 // run test
755 const auto result = device->prepareModelFromCache({}, {}, {}, {});
756
757 // verify result
758 ASSERT_FALSE(result.has_value());
759 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
760}
761
Michael Butlerabc86912021-10-28 01:54:26 +0000762TEST_P(DeviceTest, prepareModelFromCacheNullptrError) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000763 // setup call
764 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000765 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000766 EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
767 .Times(1)
768 .WillOnce(Invoke(makePreparedModelFromCacheReturn(ErrorStatus::NONE, ErrorStatus::NONE,
769 nullptr)));
770
771 // run test
772 const auto result = device->prepareModelFromCache({}, {}, {}, {});
773
774 // verify result
775 ASSERT_FALSE(result.has_value());
776 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
777}
778
Michael Butlerabc86912021-10-28 01:54:26 +0000779TEST_P(DeviceTest, prepareModelFromCacheTransportFailure) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000780 // setup call
781 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000782 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000783 EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
784 .Times(1)
785 .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
786
787 // run test
788 const auto result = device->prepareModelFromCache({}, {}, {}, {});
789
790 // verify result
791 ASSERT_FALSE(result.has_value());
792 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
793}
794
Michael Butlerabc86912021-10-28 01:54:26 +0000795TEST_P(DeviceTest, prepareModelFromCacheDeadObject) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000796 // setup call
797 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000798 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000799 EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
800 .Times(1)
801 .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
802
803 // run test
804 const auto result = device->prepareModelFromCache({}, {}, {}, {});
805
806 // verify result
807 ASSERT_FALSE(result.has_value());
808 EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
809}
810
Michael Butlerabc86912021-10-28 01:54:26 +0000811TEST_P(DeviceTest, prepareModelFromCacheAsyncCrash) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000812 // setup test
813 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000814 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000815 const auto ret = [&device]() {
816 DeathMonitor::serviceDied(device->getDeathMonitor());
817 return ndk::ScopedAStatus::ok();
818 };
819 EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
820 .Times(1)
821 .WillOnce(InvokeWithoutArgs(ret));
822
823 // run test
824 const auto result = device->prepareModelFromCache({}, {}, {}, {});
825
826 // verify result
827 ASSERT_FALSE(result.has_value());
828 EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
829}
830
Michael Butlerabc86912021-10-28 01:54:26 +0000831TEST_P(DeviceTest, allocate) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000832 // setup call
833 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000834 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000835 const auto mockBuffer = DeviceBuffer{.buffer = MockBuffer::create(), .token = 1};
836 EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
837 .Times(1)
838 .WillOnce(DoAll(SetArgPointee<4>(mockBuffer), InvokeWithoutArgs(makeStatusOk)));
839
840 // run test
841 const auto result = device->allocate({}, {}, {}, {});
842
843 // verify result
844 ASSERT_TRUE(result.has_value())
845 << "Failed with " << result.error().code << ": " << result.error().message;
846 EXPECT_NE(result.value(), nullptr);
847}
848
Michael Butlerabc86912021-10-28 01:54:26 +0000849TEST_P(DeviceTest, allocateError) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000850 // setup call
851 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000852 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000853 EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
854 .Times(1)
855 .WillOnce(InvokeWithoutArgs(makeGeneralFailure));
856
857 // run test
858 const auto result = device->allocate({}, {}, {}, {});
859
860 // verify result
861 ASSERT_FALSE(result.has_value());
862 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
863}
864
Michael Butlerabc86912021-10-28 01:54:26 +0000865TEST_P(DeviceTest, allocateTransportFailure) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000866 // setup call
867 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000868 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000869 EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
870 .Times(1)
871 .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
872
873 // run test
874 const auto result = device->allocate({}, {}, {}, {});
875
876 // verify result
877 ASSERT_FALSE(result.has_value());
878 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
879}
880
Michael Butlerabc86912021-10-28 01:54:26 +0000881TEST_P(DeviceTest, allocateDeadObject) {
Lev Proleev900c28a2021-01-26 19:40:20 +0000882 // setup call
883 const auto mockDevice = createMockDevice();
Michael Butlerabc86912021-10-28 01:54:26 +0000884 const auto device = Device::create(kName, mockDevice, kVersion).value();
Lev Proleev900c28a2021-01-26 19:40:20 +0000885 EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
886 .Times(1)
887 .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
888
889 // run test
890 const auto result = device->allocate({}, {}, {}, {});
891
892 // verify result
893 ASSERT_FALSE(result.has_value());
894 EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
895}
896
Michael Butlerabc86912021-10-28 01:54:26 +0000897INSTANTIATE_TEST_SUITE_P(TestDevice, DeviceTest,
David Gross2edfc462021-11-17 15:39:18 -0800898 ::testing::Values(nn::kVersionFeatureLevel5, nn::kVersionFeatureLevel6,
899 nn::kVersionFeatureLevel7),
Michael Butlerabc86912021-10-28 01:54:26 +0000900 printDeviceTest);
901
Lev Proleev900c28a2021-01-26 19:40:20 +0000902} // namespace aidl::android::hardware::neuralnetworks::utils