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"; |
| 162 | default: |
Michael Butler | 34f0a8f | 2021-11-11 20:07:46 -0800 | [diff] [blame] | 163 | LOG(FATAL) << "Invalid AIDL version: " << version; |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 164 | return "invalid"; |
| 165 | } |
| 166 | } |
| 167 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 168 | } // namespace |
| 169 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 170 | TEST_P(DeviceTest, invalidName) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 171 | // run test |
| 172 | const auto device = MockDevice::create(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 173 | const auto result = Device::create(kInvalidName, device, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 174 | |
| 175 | // verify result |
| 176 | ASSERT_FALSE(result.has_value()); |
| 177 | EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT); |
| 178 | } |
| 179 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 180 | TEST_P(DeviceTest, invalidDevice) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 181 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 182 | const auto result = Device::create(kName, kInvalidDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 183 | |
| 184 | // verify result |
| 185 | ASSERT_FALSE(result.has_value()); |
| 186 | EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT); |
| 187 | } |
| 188 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 189 | TEST_P(DeviceTest, getVersionStringError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 190 | // setup call |
| 191 | const auto mockDevice = createMockDevice(); |
| 192 | EXPECT_CALL(*mockDevice, getVersionString(_)) |
| 193 | .Times(1) |
| 194 | .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); |
| 195 | |
| 196 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 197 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 198 | |
| 199 | // verify result |
| 200 | ASSERT_FALSE(result.has_value()); |
| 201 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 202 | } |
| 203 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 204 | TEST_P(DeviceTest, getVersionStringTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 205 | // setup call |
| 206 | const auto mockDevice = createMockDevice(); |
| 207 | EXPECT_CALL(*mockDevice, getVersionString(_)) |
| 208 | .Times(1) |
| 209 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 210 | |
| 211 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 212 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 213 | |
| 214 | // verify result |
| 215 | ASSERT_FALSE(result.has_value()); |
| 216 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 217 | } |
| 218 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 219 | TEST_P(DeviceTest, getVersionStringDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 220 | // setup call |
| 221 | const auto mockDevice = createMockDevice(); |
| 222 | EXPECT_CALL(*mockDevice, getVersionString(_)) |
| 223 | .Times(1) |
| 224 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 225 | |
| 226 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 227 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 228 | |
| 229 | // verify result |
| 230 | ASSERT_FALSE(result.has_value()); |
| 231 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 232 | } |
| 233 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 234 | TEST_P(DeviceTest, getTypeError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 235 | // setup call |
| 236 | const auto mockDevice = createMockDevice(); |
| 237 | EXPECT_CALL(*mockDevice, getType(_)).Times(1).WillOnce(InvokeWithoutArgs(makeGeneralFailure)); |
| 238 | |
| 239 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 240 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 241 | |
| 242 | // verify result |
| 243 | ASSERT_FALSE(result.has_value()); |
| 244 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 245 | } |
| 246 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 247 | TEST_P(DeviceTest, getTypeTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 248 | // setup call |
| 249 | const auto mockDevice = createMockDevice(); |
| 250 | EXPECT_CALL(*mockDevice, getType(_)) |
| 251 | .Times(1) |
| 252 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 253 | |
| 254 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 255 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 256 | |
| 257 | // verify result |
| 258 | ASSERT_FALSE(result.has_value()); |
| 259 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 260 | } |
| 261 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 262 | TEST_P(DeviceTest, getTypeDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 263 | // setup call |
| 264 | const auto mockDevice = createMockDevice(); |
| 265 | EXPECT_CALL(*mockDevice, getType(_)) |
| 266 | .Times(1) |
| 267 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 268 | |
| 269 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 270 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 271 | |
| 272 | // verify result |
| 273 | ASSERT_FALSE(result.has_value()); |
| 274 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 275 | } |
| 276 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 277 | TEST_P(DeviceTest, getSupportedExtensionsError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 278 | // setup call |
| 279 | const auto mockDevice = createMockDevice(); |
| 280 | EXPECT_CALL(*mockDevice, getSupportedExtensions(_)) |
| 281 | .Times(1) |
| 282 | .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); |
| 283 | |
| 284 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 285 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 286 | |
| 287 | // verify result |
| 288 | ASSERT_FALSE(result.has_value()); |
| 289 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 290 | } |
| 291 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 292 | TEST_P(DeviceTest, getSupportedExtensionsTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 293 | // setup call |
| 294 | const auto mockDevice = createMockDevice(); |
| 295 | EXPECT_CALL(*mockDevice, getSupportedExtensions(_)) |
| 296 | .Times(1) |
| 297 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 298 | |
| 299 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 300 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 301 | |
| 302 | // verify result |
| 303 | ASSERT_FALSE(result.has_value()); |
| 304 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 305 | } |
| 306 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 307 | TEST_P(DeviceTest, getSupportedExtensionsDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 308 | // setup call |
| 309 | const auto mockDevice = createMockDevice(); |
| 310 | EXPECT_CALL(*mockDevice, getSupportedExtensions(_)) |
| 311 | .Times(1) |
| 312 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 313 | |
| 314 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 315 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 316 | |
| 317 | // verify result |
| 318 | ASSERT_FALSE(result.has_value()); |
| 319 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 320 | } |
| 321 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 322 | TEST_P(DeviceTest, getNumberOfCacheFilesNeeded) { |
Lev Proleev | a31aff1 | 2021-06-28 13:10:54 +0100 | [diff] [blame] | 323 | // setup call |
| 324 | const auto mockDevice = createMockDevice(); |
| 325 | EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1); |
| 326 | |
| 327 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 328 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | a31aff1 | 2021-06-28 13:10:54 +0100 | [diff] [blame] | 329 | |
| 330 | // verify result |
| 331 | ASSERT_TRUE(result.has_value()); |
| 332 | constexpr auto kNumberOfCacheFilesPair = std::make_pair<uint32_t, uint32_t>( |
| 333 | kNumberOfCacheFiles.numModelCache, kNumberOfCacheFiles.numDataCache); |
| 334 | EXPECT_EQ(result.value()->getNumberOfCacheFilesNeeded(), kNumberOfCacheFilesPair); |
| 335 | } |
| 336 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 337 | TEST_P(DeviceTest, getNumberOfCacheFilesNeededError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 338 | // setup call |
| 339 | const auto mockDevice = createMockDevice(); |
| 340 | EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) |
| 341 | .Times(1) |
| 342 | .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); |
| 343 | |
| 344 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 345 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 346 | |
| 347 | // verify result |
| 348 | ASSERT_FALSE(result.has_value()); |
| 349 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 350 | } |
| 351 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 352 | TEST_P(DeviceTest, dataCacheFilesExceedsSpecifiedMax) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 353 | // setup test |
| 354 | const auto mockDevice = createMockDevice(); |
| 355 | EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) |
| 356 | .Times(1) |
| 357 | .WillOnce(DoAll(SetArgPointee<0>(NumberOfCacheFiles{ |
| 358 | .numModelCache = nn::kMaxNumberOfCacheFiles + 1, |
| 359 | .numDataCache = nn::kMaxNumberOfCacheFiles}), |
| 360 | InvokeWithoutArgs(makeStatusOk))); |
| 361 | |
| 362 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 363 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 364 | |
| 365 | // verify result |
| 366 | ASSERT_FALSE(result.has_value()); |
| 367 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 368 | } |
| 369 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 370 | TEST_P(DeviceTest, modelCacheFilesExceedsSpecifiedMax) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 371 | // setup test |
| 372 | const auto mockDevice = createMockDevice(); |
| 373 | EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) |
| 374 | .Times(1) |
| 375 | .WillOnce(DoAll(SetArgPointee<0>(NumberOfCacheFiles{ |
| 376 | .numModelCache = nn::kMaxNumberOfCacheFiles, |
| 377 | .numDataCache = nn::kMaxNumberOfCacheFiles + 1}), |
| 378 | InvokeWithoutArgs(makeStatusOk))); |
| 379 | |
| 380 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 381 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 382 | |
| 383 | // verify result |
| 384 | ASSERT_FALSE(result.has_value()); |
| 385 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 386 | } |
| 387 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 388 | TEST_P(DeviceTest, getNumberOfCacheFilesNeededTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 389 | // setup call |
| 390 | const auto mockDevice = createMockDevice(); |
| 391 | EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) |
| 392 | .Times(1) |
| 393 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 394 | |
| 395 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 396 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 397 | |
| 398 | // verify result |
| 399 | ASSERT_FALSE(result.has_value()); |
| 400 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 401 | } |
| 402 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 403 | TEST_P(DeviceTest, getNumberOfCacheFilesNeededDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 404 | // setup call |
| 405 | const auto mockDevice = createMockDevice(); |
| 406 | EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) |
| 407 | .Times(1) |
| 408 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 409 | |
| 410 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 411 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 412 | |
| 413 | // verify result |
| 414 | ASSERT_FALSE(result.has_value()); |
| 415 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 416 | } |
| 417 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 418 | TEST_P(DeviceTest, getCapabilitiesError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 419 | // setup call |
| 420 | const auto mockDevice = createMockDevice(); |
| 421 | EXPECT_CALL(*mockDevice, getCapabilities(_)) |
| 422 | .Times(1) |
| 423 | .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); |
| 424 | |
| 425 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 426 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 427 | |
| 428 | // verify result |
| 429 | ASSERT_FALSE(result.has_value()); |
| 430 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 431 | } |
| 432 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 433 | TEST_P(DeviceTest, getCapabilitiesTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 434 | // setup call |
| 435 | const auto mockDevice = createMockDevice(); |
| 436 | EXPECT_CALL(*mockDevice, getCapabilities(_)) |
| 437 | .Times(1) |
| 438 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 439 | |
| 440 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 441 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 442 | |
| 443 | // verify result |
| 444 | ASSERT_FALSE(result.has_value()); |
| 445 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 446 | } |
| 447 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 448 | TEST_P(DeviceTest, getCapabilitiesDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 449 | // setup call |
| 450 | const auto mockDevice = createMockDevice(); |
| 451 | EXPECT_CALL(*mockDevice, getCapabilities(_)) |
| 452 | .Times(1) |
| 453 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 454 | |
| 455 | // run test |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 456 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 457 | |
| 458 | // verify result |
| 459 | ASSERT_FALSE(result.has_value()); |
| 460 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 461 | } |
| 462 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 463 | TEST_P(DeviceTest, getName) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 464 | // setup call |
| 465 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 466 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 467 | |
| 468 | // run test |
| 469 | const auto& name = device->getName(); |
| 470 | |
| 471 | // verify result |
| 472 | EXPECT_EQ(name, kName); |
| 473 | } |
| 474 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 475 | TEST_P(DeviceTest, getFeatureLevel) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 476 | // setup call |
| 477 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 478 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 479 | |
| 480 | // run test |
| 481 | const auto featureLevel = device->getFeatureLevel(); |
| 482 | |
| 483 | // verify result |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 484 | EXPECT_EQ(featureLevel, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 487 | TEST_P(DeviceTest, getCachedData) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 488 | // setup call |
| 489 | const auto mockDevice = createMockDevice(); |
| 490 | EXPECT_CALL(*mockDevice, getVersionString(_)).Times(1); |
| 491 | EXPECT_CALL(*mockDevice, getType(_)).Times(1); |
| 492 | EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(1); |
| 493 | EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1); |
| 494 | EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(1); |
| 495 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 496 | const auto result = Device::create(kName, mockDevice, kVersion); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 497 | ASSERT_TRUE(result.has_value()) |
| 498 | << "Failed with " << result.error().code << ": " << result.error().message; |
| 499 | const auto& device = result.value(); |
| 500 | |
| 501 | // run test and verify results |
| 502 | EXPECT_EQ(device->getVersionString(), device->getVersionString()); |
| 503 | EXPECT_EQ(device->getType(), device->getType()); |
| 504 | EXPECT_EQ(device->getSupportedExtensions(), device->getSupportedExtensions()); |
| 505 | EXPECT_EQ(device->getNumberOfCacheFilesNeeded(), device->getNumberOfCacheFilesNeeded()); |
| 506 | EXPECT_EQ(device->getCapabilities(), device->getCapabilities()); |
| 507 | } |
| 508 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 509 | TEST_P(DeviceTest, getSupportedOperations) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 510 | // setup call |
| 511 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 512 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 513 | EXPECT_CALL(*mockDevice, getSupportedOperations(_, _)) |
| 514 | .Times(1) |
| 515 | .WillOnce(DoAll( |
| 516 | SetArgPointee<1>(std::vector<bool>(kSimpleModel.main.operations.size(), true)), |
| 517 | InvokeWithoutArgs(makeStatusOk))); |
| 518 | |
| 519 | // run test |
| 520 | const auto result = device->getSupportedOperations(kSimpleModel); |
| 521 | |
| 522 | // verify result |
| 523 | ASSERT_TRUE(result.has_value()) |
| 524 | << "Failed with " << result.error().code << ": " << result.error().message; |
| 525 | const auto& supportedOperations = result.value(); |
| 526 | EXPECT_EQ(supportedOperations.size(), kSimpleModel.main.operations.size()); |
| 527 | EXPECT_THAT(supportedOperations, Each(testing::IsTrue())); |
| 528 | } |
| 529 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 530 | TEST_P(DeviceTest, getSupportedOperationsError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 531 | // setup call |
| 532 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 533 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 534 | EXPECT_CALL(*mockDevice, getSupportedOperations(_, _)) |
| 535 | .Times(1) |
| 536 | .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); |
| 537 | |
| 538 | // run test |
| 539 | const auto result = device->getSupportedOperations(kSimpleModel); |
| 540 | |
| 541 | // verify result |
| 542 | ASSERT_FALSE(result.has_value()); |
| 543 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 544 | } |
| 545 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 546 | TEST_P(DeviceTest, getSupportedOperationsTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 547 | // setup call |
| 548 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 549 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 550 | EXPECT_CALL(*mockDevice, getSupportedOperations(_, _)) |
| 551 | .Times(1) |
| 552 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 553 | |
| 554 | // run test |
| 555 | const auto result = device->getSupportedOperations(kSimpleModel); |
| 556 | |
| 557 | // verify result |
| 558 | ASSERT_FALSE(result.has_value()); |
| 559 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 560 | } |
| 561 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 562 | TEST_P(DeviceTest, getSupportedOperationsDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 563 | // setup call |
| 564 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 565 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 566 | EXPECT_CALL(*mockDevice, getSupportedOperations(_, _)) |
| 567 | .Times(1) |
| 568 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 569 | |
| 570 | // run test |
| 571 | const auto result = device->getSupportedOperations(kSimpleModel); |
| 572 | |
| 573 | // verify result |
| 574 | ASSERT_FALSE(result.has_value()); |
| 575 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 576 | } |
| 577 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 578 | TEST_P(DeviceTest, prepareModel) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 579 | // setup call |
| 580 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 581 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 582 | const auto mockPreparedModel = MockPreparedModel::create(); |
| 583 | EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) |
| 584 | .Times(1) |
| 585 | .WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::NONE, ErrorStatus::NONE, |
| 586 | mockPreparedModel))); |
| 587 | |
| 588 | // run test |
| 589 | const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, |
| 590 | nn::Priority::DEFAULT, {}, {}, {}, {}); |
| 591 | |
| 592 | // verify result |
| 593 | ASSERT_TRUE(result.has_value()) |
| 594 | << "Failed with " << result.error().code << ": " << result.error().message; |
| 595 | EXPECT_NE(result.value(), nullptr); |
| 596 | } |
| 597 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 598 | TEST_P(DeviceTest, prepareModelLaunchError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 599 | // setup call |
| 600 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 601 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 602 | EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) |
| 603 | .Times(1) |
| 604 | .WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::GENERAL_FAILURE, |
| 605 | ErrorStatus::GENERAL_FAILURE, nullptr))); |
| 606 | |
| 607 | // run test |
| 608 | const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, |
| 609 | nn::Priority::DEFAULT, {}, {}, {}, {}); |
| 610 | |
| 611 | // verify result |
| 612 | ASSERT_FALSE(result.has_value()); |
| 613 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 614 | } |
| 615 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 616 | TEST_P(DeviceTest, prepareModelReturnError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 617 | // setup call |
| 618 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 619 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 620 | EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) |
| 621 | .Times(1) |
| 622 | .WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::NONE, |
| 623 | ErrorStatus::GENERAL_FAILURE, nullptr))); |
| 624 | |
| 625 | // run test |
| 626 | const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, |
| 627 | nn::Priority::DEFAULT, {}, {}, {}, {}); |
| 628 | |
| 629 | // verify result |
| 630 | ASSERT_FALSE(result.has_value()); |
| 631 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 632 | } |
| 633 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 634 | TEST_P(DeviceTest, prepareModelNullptrError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 635 | // setup call |
| 636 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 637 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 638 | EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) |
| 639 | .Times(1) |
| 640 | .WillOnce( |
| 641 | Invoke(makePreparedModelReturn(ErrorStatus::NONE, ErrorStatus::NONE, nullptr))); |
| 642 | |
| 643 | // run test |
| 644 | const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, |
| 645 | nn::Priority::DEFAULT, {}, {}, {}, {}); |
| 646 | |
| 647 | // verify result |
| 648 | ASSERT_FALSE(result.has_value()); |
| 649 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 650 | } |
| 651 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 652 | TEST_P(DeviceTest, prepareModelTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 653 | // setup call |
| 654 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 655 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 656 | EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) |
| 657 | .Times(1) |
| 658 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 659 | |
| 660 | // run test |
| 661 | const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, |
| 662 | nn::Priority::DEFAULT, {}, {}, {}, {}); |
| 663 | |
| 664 | // verify result |
| 665 | ASSERT_FALSE(result.has_value()); |
| 666 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 667 | } |
| 668 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 669 | TEST_P(DeviceTest, prepareModelDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 670 | // setup call |
| 671 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 672 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 673 | EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) |
| 674 | .Times(1) |
| 675 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 676 | |
| 677 | // run test |
| 678 | const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, |
| 679 | nn::Priority::DEFAULT, {}, {}, {}, {}); |
| 680 | |
| 681 | // verify result |
| 682 | ASSERT_FALSE(result.has_value()); |
| 683 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 684 | } |
| 685 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 686 | TEST_P(DeviceTest, prepareModelAsyncCrash) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 687 | // setup test |
| 688 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 689 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 690 | const auto ret = [&device]() { |
| 691 | DeathMonitor::serviceDied(device->getDeathMonitor()); |
| 692 | return ndk::ScopedAStatus::ok(); |
| 693 | }; |
| 694 | EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) |
| 695 | .Times(1) |
| 696 | .WillOnce(InvokeWithoutArgs(ret)); |
| 697 | |
| 698 | // run test |
| 699 | const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, |
| 700 | nn::Priority::DEFAULT, {}, {}, {}, {}); |
| 701 | |
| 702 | // verify result |
| 703 | ASSERT_FALSE(result.has_value()); |
| 704 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 705 | } |
| 706 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 707 | TEST_P(DeviceTest, prepareModelFromCache) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 708 | // setup call |
| 709 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 710 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 711 | const auto mockPreparedModel = MockPreparedModel::create(); |
| 712 | EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) |
| 713 | .Times(1) |
| 714 | .WillOnce(Invoke(makePreparedModelFromCacheReturn(ErrorStatus::NONE, ErrorStatus::NONE, |
| 715 | mockPreparedModel))); |
| 716 | |
| 717 | // run test |
| 718 | const auto result = device->prepareModelFromCache({}, {}, {}, {}); |
| 719 | |
| 720 | // verify result |
| 721 | ASSERT_TRUE(result.has_value()) |
| 722 | << "Failed with " << result.error().code << ": " << result.error().message; |
| 723 | EXPECT_NE(result.value(), nullptr); |
| 724 | } |
| 725 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 726 | TEST_P(DeviceTest, prepareModelFromCacheLaunchError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 727 | // setup call |
| 728 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 729 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 730 | EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) |
| 731 | .Times(1) |
| 732 | .WillOnce(Invoke(makePreparedModelFromCacheReturn( |
| 733 | ErrorStatus::GENERAL_FAILURE, ErrorStatus::GENERAL_FAILURE, nullptr))); |
| 734 | |
| 735 | // run test |
| 736 | const auto result = device->prepareModelFromCache({}, {}, {}, {}); |
| 737 | |
| 738 | // verify result |
| 739 | ASSERT_FALSE(result.has_value()); |
| 740 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 741 | } |
| 742 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 743 | TEST_P(DeviceTest, prepareModelFromCacheReturnError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 744 | // setup call |
| 745 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 746 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 747 | EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) |
| 748 | .Times(1) |
| 749 | .WillOnce(Invoke(makePreparedModelFromCacheReturn( |
| 750 | ErrorStatus::NONE, ErrorStatus::GENERAL_FAILURE, nullptr))); |
| 751 | |
| 752 | // run test |
| 753 | const auto result = device->prepareModelFromCache({}, {}, {}, {}); |
| 754 | |
| 755 | // verify result |
| 756 | ASSERT_FALSE(result.has_value()); |
| 757 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 758 | } |
| 759 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 760 | TEST_P(DeviceTest, prepareModelFromCacheNullptrError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 761 | // setup call |
| 762 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 763 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 764 | EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) |
| 765 | .Times(1) |
| 766 | .WillOnce(Invoke(makePreparedModelFromCacheReturn(ErrorStatus::NONE, ErrorStatus::NONE, |
| 767 | nullptr))); |
| 768 | |
| 769 | // run test |
| 770 | const auto result = device->prepareModelFromCache({}, {}, {}, {}); |
| 771 | |
| 772 | // verify result |
| 773 | ASSERT_FALSE(result.has_value()); |
| 774 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 775 | } |
| 776 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 777 | TEST_P(DeviceTest, prepareModelFromCacheTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 778 | // setup call |
| 779 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 780 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 781 | EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) |
| 782 | .Times(1) |
| 783 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 784 | |
| 785 | // run test |
| 786 | const auto result = device->prepareModelFromCache({}, {}, {}, {}); |
| 787 | |
| 788 | // verify result |
| 789 | ASSERT_FALSE(result.has_value()); |
| 790 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 791 | } |
| 792 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 793 | TEST_P(DeviceTest, prepareModelFromCacheDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 794 | // setup call |
| 795 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 796 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 797 | EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) |
| 798 | .Times(1) |
| 799 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 800 | |
| 801 | // run test |
| 802 | const auto result = device->prepareModelFromCache({}, {}, {}, {}); |
| 803 | |
| 804 | // verify result |
| 805 | ASSERT_FALSE(result.has_value()); |
| 806 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 807 | } |
| 808 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 809 | TEST_P(DeviceTest, prepareModelFromCacheAsyncCrash) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 810 | // setup test |
| 811 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 812 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 813 | const auto ret = [&device]() { |
| 814 | DeathMonitor::serviceDied(device->getDeathMonitor()); |
| 815 | return ndk::ScopedAStatus::ok(); |
| 816 | }; |
| 817 | EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) |
| 818 | .Times(1) |
| 819 | .WillOnce(InvokeWithoutArgs(ret)); |
| 820 | |
| 821 | // run test |
| 822 | const auto result = device->prepareModelFromCache({}, {}, {}, {}); |
| 823 | |
| 824 | // verify result |
| 825 | ASSERT_FALSE(result.has_value()); |
| 826 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 827 | } |
| 828 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 829 | TEST_P(DeviceTest, allocate) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 830 | // setup call |
| 831 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 832 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 833 | const auto mockBuffer = DeviceBuffer{.buffer = MockBuffer::create(), .token = 1}; |
| 834 | EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _)) |
| 835 | .Times(1) |
| 836 | .WillOnce(DoAll(SetArgPointee<4>(mockBuffer), InvokeWithoutArgs(makeStatusOk))); |
| 837 | |
| 838 | // run test |
| 839 | const auto result = device->allocate({}, {}, {}, {}); |
| 840 | |
| 841 | // verify result |
| 842 | ASSERT_TRUE(result.has_value()) |
| 843 | << "Failed with " << result.error().code << ": " << result.error().message; |
| 844 | EXPECT_NE(result.value(), nullptr); |
| 845 | } |
| 846 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 847 | TEST_P(DeviceTest, allocateError) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 848 | // setup call |
| 849 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 850 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 851 | EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _)) |
| 852 | .Times(1) |
| 853 | .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); |
| 854 | |
| 855 | // run test |
| 856 | const auto result = device->allocate({}, {}, {}, {}); |
| 857 | |
| 858 | // verify result |
| 859 | ASSERT_FALSE(result.has_value()); |
| 860 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 861 | } |
| 862 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 863 | TEST_P(DeviceTest, allocateTransportFailure) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 864 | // setup call |
| 865 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 866 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 867 | EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _)) |
| 868 | .Times(1) |
| 869 | .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); |
| 870 | |
| 871 | // run test |
| 872 | const auto result = device->allocate({}, {}, {}, {}); |
| 873 | |
| 874 | // verify result |
| 875 | ASSERT_FALSE(result.has_value()); |
| 876 | EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); |
| 877 | } |
| 878 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 879 | TEST_P(DeviceTest, allocateDeadObject) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 880 | // setup call |
| 881 | const auto mockDevice = createMockDevice(); |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 882 | const auto device = Device::create(kName, mockDevice, kVersion).value(); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 883 | EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _)) |
| 884 | .Times(1) |
| 885 | .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); |
| 886 | |
| 887 | // run test |
| 888 | const auto result = device->allocate({}, {}, {}, {}); |
| 889 | |
| 890 | // verify result |
| 891 | ASSERT_FALSE(result.has_value()); |
| 892 | EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); |
| 893 | } |
| 894 | |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 895 | INSTANTIATE_TEST_SUITE_P(TestDevice, DeviceTest, |
Michael Butler | 60a7b86 | 2021-11-17 16:33:35 -0800 | [diff] [blame] | 896 | ::testing::Values(nn::kVersionFeatureLevel5, nn::kVersionFeatureLevel6), |
Michael Butler | abc8691 | 2021-10-28 01:54:26 +0000 | [diff] [blame] | 897 | printDeviceTest); |
| 898 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 899 | } // namespace aidl::android::hardware::neuralnetworks::utils |