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