Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 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 | |
| 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 | |
| 35 | namespace aidl::android::hardware::neuralnetworks::utils { |
| 36 | namespace { |
| 37 | |
| 38 | namespace nn = ::android::nn; |
| 39 | using ::testing::_; |
| 40 | using ::testing::DoAll; |
| 41 | using ::testing::Invoke; |
| 42 | using ::testing::InvokeWithoutArgs; |
| 43 | using ::testing::SetArgPointee; |
| 44 | |
| 45 | const 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 | |
| 56 | const std::string kName = "Google-MockV1"; |
| 57 | const std::string kInvalidName = ""; |
| 58 | const std::shared_ptr<BnDevice> kInvalidDevice; |
| 59 | constexpr PerformanceInfo kNoPerformanceInfo = {.execTime = std::numeric_limits<float>::max(), |
| 60 | .powerUsage = std::numeric_limits<float>::max()}; |
Lev Proleev | a31aff1 | 2021-06-28 13:10:54 +0100 | [diff] [blame] | 61 | constexpr NumberOfCacheFiles kNumberOfCacheFiles = {.numModelCache = nn::kMaxNumberOfCacheFiles - 1, |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 62 | .numDataCache = nn::kMaxNumberOfCacheFiles}; |
| 63 | |
| 64 | constexpr auto makeStatusOk = [] { return ndk::ScopedAStatus::ok(); }; |
| 65 | |
| 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, |
| 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 | |
| 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 | auto 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 | |
| 138 | constexpr auto makeGeneralFailure = [] { |
| 139 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 140 | static_cast<int32_t>(ErrorStatus::GENERAL_FAILURE)); |
| 141 | }; |
| 142 | constexpr auto makeGeneralTransportFailure = [] { |
| 143 | return ndk::ScopedAStatus::fromStatus(STATUS_NO_MEMORY); |
| 144 | }; |
| 145 | constexpr auto makeDeadObjectFailure = [] { |
| 146 | return ndk::ScopedAStatus::fromStatus(STATUS_DEAD_OBJECT); |
| 147 | }; |
| 148 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 149 | class DeviceTest : public ::testing::TestWithParam<nn::Version> { |
| 150 | protected: |
| 151 | const nn::Version kVersion = GetParam(); |
| 152 | }; |
| 153 | |
| 154 | std::string printDeviceTest(const testing::TestParamInfo<nn::Version>& info) { |
Michael Butler | 34f0a8f | 2021-11-11 20:07:46 -0800 | [diff] [blame] | 155 | const nn::Version version = info.param; |
| 156 | CHECK(!version.runtimeOnlyFeatures); |
| 157 | switch (version.level) { |
Michael Butler | 60a7b86 | 2021-11-17 16:33:35 -0800 | [diff] [blame] | 158 | case nn::Version::Level::FEATURE_LEVEL_5: |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 159 | return "v1"; |
Michael Butler | 34f0a8f | 2021-11-11 20:07:46 -0800 | [diff] [blame] | 160 | case nn::Version::Level::FEATURE_LEVEL_6: |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 161 | return "v2"; |
David Gross | 2edfc46 | 2021-11-17 15:39:18 -0800 | [diff] [blame^] | 162 | case nn::Version::Level::FEATURE_LEVEL_7: |
| 163 | return "v3"; |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 164 | default: |
Michael Butler | 34f0a8f | 2021-11-11 20:07:46 -0800 | [diff] [blame] | 165 | LOG(FATAL) << "Invalid AIDL version: " << version; |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 166 | return "invalid"; |
| 167 | } |
| 168 | } |
| 169 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 170 | } // namespace |
| 171 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 172 | TEST_P(DeviceTest, invalidName) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 173 | // run test |
| 174 | const auto device = MockDevice::create(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 175 | const auto result = Device::create(kInvalidName, device, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 176 | |
| 177 | // verify result |
| 178 | ASSERT_FALSE(result.has_value()); |
| 179 | EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT); |
| 180 | } |
| 181 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 182 | TEST_P(DeviceTest, invalidDevice) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 183 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 184 | const auto result = Device::create(kName, kInvalidDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 185 | |
| 186 | // verify result |
| 187 | ASSERT_FALSE(result.has_value()); |
| 188 | EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT); |
| 189 | } |
| 190 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 191 | TEST_P(DeviceTest, getVersionStringError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 192 | // setup call |
| 193 | const auto mockDevice = createMockDevice(); |
| 194 | EXPECT_CALL(*mockDevice, getVersionString(_)) |
| 195 | .Times(1) |
| 196 | .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); |
| 197 | |
| 198 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 199 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 200 | |
| 201 | // verify result |
| 202 | ASSERT_FALSE(result.has_value()); |
| 203 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 204 | } |
| 205 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 206 | TEST_P(DeviceTest, getVersionStringTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 207 | // setup call |
| 208 | const auto mockDevice = createMockDevice(); |
| 209 | EXPECT_CALL(*mockDevice, getVersionString(_)) |
| 210 | .Times(1) |
| 211 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 212 | |
| 213 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 214 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 215 | |
| 216 | // verify result |
| 217 | ASSERT_FALSE(result.has_value()); |
| 218 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 219 | } |
| 220 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 221 | TEST_P(DeviceTest, getVersionStringDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 222 | // setup call |
| 223 | const auto mockDevice = createMockDevice(); |
| 224 | EXPECT_CALL(*mockDevice, getVersionString(_)) |
| 225 | .Times(1) |
| 226 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 227 | |
| 228 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 229 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 230 | |
| 231 | // verify result |
| 232 | ASSERT_FALSE(result.has_value()); |
| 233 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 234 | } |
| 235 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 236 | TEST_P(DeviceTest, getTypeError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 237 | // setup call |
| 238 | const auto mockDevice = createMockDevice(); |
| 239 | EXPECT_CALL(*mockDevice, getType(_)).Times(1).WillOnce(InvokeWithoutArgs(makeGeneralFailure)); |
| 240 | |
| 241 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 242 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 243 | |
| 244 | // verify result |
| 245 | ASSERT_FALSE(result.has_value()); |
| 246 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 247 | } |
| 248 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 249 | TEST_P(DeviceTest, getTypeTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 250 | // setup call |
| 251 | const auto mockDevice = createMockDevice(); |
| 252 | EXPECT_CALL(*mockDevice, getType(_)) |
| 253 | .Times(1) |
| 254 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 255 | |
| 256 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 257 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 258 | |
| 259 | // verify result |
| 260 | ASSERT_FALSE(result.has_value()); |
| 261 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 262 | } |
| 263 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 264 | TEST_P(DeviceTest, getTypeDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 265 | // setup call |
| 266 | const auto mockDevice = createMockDevice(); |
| 267 | EXPECT_CALL(*mockDevice, getType(_)) |
| 268 | .Times(1) |
| 269 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 270 | |
| 271 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 272 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 273 | |
| 274 | // verify result |
| 275 | ASSERT_FALSE(result.has_value()); |
| 276 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 277 | } |
| 278 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 279 | TEST_P(DeviceTest, getSupportedExtensionsError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 280 | // setup call |
| 281 | const auto mockDevice = createMockDevice(); |
| 282 | EXPECT_CALL(*mockDevice, getSupportedExtensions(_)) |
| 283 | .Times(1) |
| 284 | .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); |
| 285 | |
| 286 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 287 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 288 | |
| 289 | // verify result |
| 290 | ASSERT_FALSE(result.has_value()); |
| 291 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 292 | } |
| 293 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 294 | TEST_P(DeviceTest, getSupportedExtensionsTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 295 | // setup call |
| 296 | const auto mockDevice = createMockDevice(); |
| 297 | EXPECT_CALL(*mockDevice, getSupportedExtensions(_)) |
| 298 | .Times(1) |
| 299 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 300 | |
| 301 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 302 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 303 | |
| 304 | // verify result |
| 305 | ASSERT_FALSE(result.has_value()); |
| 306 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 307 | } |
| 308 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 309 | TEST_P(DeviceTest, getSupportedExtensionsDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 310 | // setup call |
| 311 | const auto mockDevice = createMockDevice(); |
| 312 | EXPECT_CALL(*mockDevice, getSupportedExtensions(_)) |
| 313 | .Times(1) |
| 314 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 315 | |
| 316 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 317 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 318 | |
| 319 | // verify result |
| 320 | ASSERT_FALSE(result.has_value()); |
| 321 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 322 | } |
| 323 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 324 | TEST_P(DeviceTest, getNumberOfCacheFilesNeeded) { |
Lev Proleev | a31aff1 | 2021-06-28 13:10:54 +0100 | [diff] [blame] | 325 | // setup call |
| 326 | const auto mockDevice = createMockDevice(); |
| 327 | EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1); |
| 328 | |
| 329 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 330 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | a31aff1 | 2021-06-28 13:10:54 +0100 | [diff] [blame] | 331 | |
| 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 339 | TEST_P(DeviceTest, getNumberOfCacheFilesNeededError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 340 | // setup call |
| 341 | const auto mockDevice = createMockDevice(); |
| 342 | EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) |
| 343 | .Times(1) |
| 344 | .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); |
| 345 | |
| 346 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 347 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 348 | |
| 349 | // verify result |
| 350 | ASSERT_FALSE(result.has_value()); |
| 351 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 352 | } |
| 353 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 354 | TEST_P(DeviceTest, dataCacheFilesExceedsSpecifiedMax) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 355 | // 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 365 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 366 | |
| 367 | // verify result |
| 368 | ASSERT_FALSE(result.has_value()); |
| 369 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 370 | } |
| 371 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 372 | TEST_P(DeviceTest, modelCacheFilesExceedsSpecifiedMax) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 373 | // 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 383 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 384 | |
| 385 | // verify result |
| 386 | ASSERT_FALSE(result.has_value()); |
| 387 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 388 | } |
| 389 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 390 | TEST_P(DeviceTest, getNumberOfCacheFilesNeededTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 391 | // setup call |
| 392 | const auto mockDevice = createMockDevice(); |
| 393 | EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) |
| 394 | .Times(1) |
| 395 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 396 | |
| 397 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 398 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 399 | |
| 400 | // verify result |
| 401 | ASSERT_FALSE(result.has_value()); |
| 402 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 403 | } |
| 404 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 405 | TEST_P(DeviceTest, getNumberOfCacheFilesNeededDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 406 | // setup call |
| 407 | const auto mockDevice = createMockDevice(); |
| 408 | EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) |
| 409 | .Times(1) |
| 410 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 411 | |
| 412 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 413 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 414 | |
| 415 | // verify result |
| 416 | ASSERT_FALSE(result.has_value()); |
| 417 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 418 | } |
| 419 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 420 | TEST_P(DeviceTest, getCapabilitiesError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 421 | // setup call |
| 422 | const auto mockDevice = createMockDevice(); |
| 423 | EXPECT_CALL(*mockDevice, getCapabilities(_)) |
| 424 | .Times(1) |
| 425 | .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); |
| 426 | |
| 427 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 428 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 429 | |
| 430 | // verify result |
| 431 | ASSERT_FALSE(result.has_value()); |
| 432 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 433 | } |
| 434 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 435 | TEST_P(DeviceTest, getCapabilitiesTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 436 | // setup call |
| 437 | const auto mockDevice = createMockDevice(); |
| 438 | EXPECT_CALL(*mockDevice, getCapabilities(_)) |
| 439 | .Times(1) |
| 440 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 441 | |
| 442 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 443 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 444 | |
| 445 | // verify result |
| 446 | ASSERT_FALSE(result.has_value()); |
| 447 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 448 | } |
| 449 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 450 | TEST_P(DeviceTest, getCapabilitiesDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 451 | // setup call |
| 452 | const auto mockDevice = createMockDevice(); |
| 453 | EXPECT_CALL(*mockDevice, getCapabilities(_)) |
| 454 | .Times(1) |
| 455 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 456 | |
| 457 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 458 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 459 | |
| 460 | // verify result |
| 461 | ASSERT_FALSE(result.has_value()); |
| 462 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 463 | } |
| 464 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 465 | TEST_P(DeviceTest, getName) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 466 | // setup call |
| 467 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 468 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 469 | |
| 470 | // run test |
| 471 | const auto& name = device->getName(); |
| 472 | |
| 473 | // verify result |
| 474 | EXPECT_EQ(name, kName); |
| 475 | } |
| 476 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 477 | TEST_P(DeviceTest, getFeatureLevel) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 478 | // setup call |
| 479 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 480 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 481 | |
| 482 | // run test |
| 483 | const auto featureLevel = device->getFeatureLevel(); |
| 484 | |
| 485 | // verify result |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 486 | EXPECT_EQ(featureLevel, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 489 | TEST_P(DeviceTest, getCachedData) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 490 | // 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 498 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 499 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 511 | TEST_P(DeviceTest, getSupportedOperations) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 512 | // setup call |
| 513 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 514 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 515 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 532 | TEST_P(DeviceTest, getSupportedOperationsError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 533 | // setup call |
| 534 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 535 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 536 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 548 | TEST_P(DeviceTest, getSupportedOperationsTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 549 | // setup call |
| 550 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 551 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 552 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 564 | TEST_P(DeviceTest, getSupportedOperationsDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 565 | // setup call |
| 566 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 567 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 568 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 580 | TEST_P(DeviceTest, prepareModel) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 581 | // setup call |
| 582 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 583 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 584 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 600 | TEST_P(DeviceTest, prepareModelLaunchError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 601 | // setup call |
| 602 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 603 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 604 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 618 | TEST_P(DeviceTest, prepareModelReturnError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 619 | // setup call |
| 620 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 621 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 622 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 636 | TEST_P(DeviceTest, prepareModelNullptrError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 637 | // setup call |
| 638 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 639 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 640 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 654 | TEST_P(DeviceTest, prepareModelTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 655 | // setup call |
| 656 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 657 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 658 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 671 | TEST_P(DeviceTest, prepareModelDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 672 | // setup call |
| 673 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 674 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 675 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 688 | TEST_P(DeviceTest, prepareModelAsyncCrash) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 689 | // setup test |
| 690 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 691 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 692 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 709 | TEST_P(DeviceTest, prepareModelFromCache) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 710 | // setup call |
| 711 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 712 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 713 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 728 | TEST_P(DeviceTest, prepareModelFromCacheLaunchError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 729 | // setup call |
| 730 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 731 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 732 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 745 | TEST_P(DeviceTest, prepareModelFromCacheReturnError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 746 | // setup call |
| 747 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 748 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 749 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 762 | TEST_P(DeviceTest, prepareModelFromCacheNullptrError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 763 | // setup call |
| 764 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 765 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 766 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 779 | TEST_P(DeviceTest, prepareModelFromCacheTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 780 | // setup call |
| 781 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 782 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 783 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 795 | TEST_P(DeviceTest, prepareModelFromCacheDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 796 | // setup call |
| 797 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 798 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 799 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 811 | TEST_P(DeviceTest, prepareModelFromCacheAsyncCrash) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 812 | // setup test |
| 813 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 814 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 815 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 831 | TEST_P(DeviceTest, allocate) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 832 | // setup call |
| 833 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 834 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 835 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 849 | TEST_P(DeviceTest, allocateError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 850 | // setup call |
| 851 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 852 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 853 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 865 | TEST_P(DeviceTest, allocateTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 866 | // setup call |
| 867 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 868 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 869 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 881 | TEST_P(DeviceTest, allocateDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 882 | // setup call |
| 883 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 884 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 885 | 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 Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 897 | INSTANTIATE_TEST_SUITE_P(TestDevice, DeviceTest, |
David Gross | 2edfc46 | 2021-11-17 15:39:18 -0800 | [diff] [blame^] | 898 | ::testing::Values(nn::kVersionFeatureLevel5, nn::kVersionFeatureLevel6, |
| 899 | nn::kVersionFeatureLevel7), |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 900 | printDeviceTest); |
| 901 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 902 | } // namespace aidl::android::hardware::neuralnetworks::utils |