1 /* 2 * Copyright (C) 2016 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 #define LOG_NDEBUG 0 18 #define LOG_TAG "CameraProviderManagerTest" 19 20 #include "../common/CameraProviderManager.h" 21 #include <android/hidl/manager/1.0/IServiceManager.h> 22 #include <android/hidl/manager/1.0/IServiceNotification.h> 23 #include <android/hardware/camera/device/3.2/ICameraDeviceCallback.h> 24 #include <android/hardware/camera/device/3.2/ICameraDeviceSession.h> 25 #include <camera_metadata_hidden.h> 26 #include <gtest/gtest.h> 27 28 using namespace android; 29 using namespace android::hardware::camera; 30 using android::hardware::camera::common::V1_0::Status; 31 using android::hardware::camera::common::V1_0::VendorTag; 32 using android::hardware::camera::common::V1_0::VendorTagSection; 33 using android::hardware::camera::common::V1_0::CameraMetadataType; 34 using android::hardware::camera::device::V3_2::ICameraDeviceCallback; 35 using android::hardware::camera::device::V3_2::ICameraDeviceSession; 36 using android::hardware::camera::provider::V2_5::DeviceState; 37 38 /** 39 * Basic test implementation of a camera ver. 3.2 device interface 40 */ 41 struct TestDeviceInterface : public device::V3_2::ICameraDevice { 42 std::vector<hardware::hidl_string> mDeviceNames; 43 TestDeviceInterface(std::vector<hardware::hidl_string> deviceNames) : 44 mDeviceNames(deviceNames) {} 45 using getResourceCost_cb = std::function<void( 46 hardware::camera::common::V1_0::Status status, 47 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost)>; 48 virtual ::android::hardware::Return<void> getResourceCost( 49 getResourceCost_cb _hidl_cb) override { 50 hardware::camera::common::V1_0::CameraResourceCost resourceCost = {100, 51 mDeviceNames}; 52 _hidl_cb(Status::OK, resourceCost); 53 return hardware::Void(); 54 } 55 56 using getCameraCharacteristics_cb = std::function<void( 57 hardware::camera::common::V1_0::Status status, 58 const hardware::hidl_vec<uint8_t>& cameraCharacteristics)>; 59 hardware::Return<void> getCameraCharacteristics( 60 getCameraCharacteristics_cb _hidl_cb) override { 61 hardware::hidl_vec<uint8_t> cameraCharacteristics; 62 _hidl_cb(Status::OK, cameraCharacteristics); 63 return hardware::Void(); 64 } 65 66 hardware::Return<hardware::camera::common::V1_0::Status> setTorchMode( 67 ::android::hardware::camera::common::V1_0::TorchMode) override { 68 return Status::OK; 69 } 70 71 using open_cb = std::function<void( 72 ::android::hardware::camera::common::V1_0::Status status, 73 const ::android::sp<ICameraDeviceSession>& session)>; 74 hardware::Return<void> open( 75 const ::android::sp<ICameraDeviceCallback>&, 76 open_cb _hidl_cb) override { 77 sp<ICameraDeviceSession> deviceSession = nullptr; 78 _hidl_cb(Status::OK, deviceSession); 79 return hardware::Void(); 80 } 81 82 hardware::Return<void> dumpState( 83 const ::android::hardware::hidl_handle&) override { 84 return hardware::Void(); 85 } 86 }; 87 88 /** 89 * Basic test implementation of a camera provider 90 */ 91 struct TestICameraProvider : virtual public provider::V2_5::ICameraProvider { 92 sp<provider::V2_4::ICameraProviderCallback> mCallbacks; 93 std::vector<hardware::hidl_string> mDeviceNames; 94 sp<device::V3_2::ICameraDevice> mDeviceInterface; 95 hardware::hidl_vec<common::V1_0::VendorTagSection> mVendorTagSections; 96 97 TestICameraProvider(const std::vector<hardware::hidl_string> &devices, 98 const hardware::hidl_vec<common::V1_0::VendorTagSection> &vendorSection) : 99 mDeviceNames(devices), 100 mDeviceInterface(new TestDeviceInterface(devices)), 101 mVendorTagSections (vendorSection) {} 102 103 virtual hardware::Return<Status> setCallback( 104 const sp<provider::V2_4::ICameraProviderCallback>& callbacks) override { 105 mCalledCounter[SET_CALLBACK]++; 106 mCallbacks = callbacks; 107 return hardware::Return<Status>(Status::OK); 108 } 109 110 using getVendorTags_cb = std::function<void(Status status, 111 const hardware::hidl_vec<common::V1_0::VendorTagSection>& sections)>; 112 hardware::Return<void> getVendorTags(getVendorTags_cb _hidl_cb) override { 113 mCalledCounter[GET_VENDOR_TAGS]++; 114 _hidl_cb(Status::OK, mVendorTagSections); 115 return hardware::Void(); 116 } 117 118 using isSetTorchModeSupported_cb = std::function<void( 119 ::android::hardware::camera::common::V1_0::Status status, 120 bool support)>; 121 virtual ::hardware::Return<void> isSetTorchModeSupported( 122 isSetTorchModeSupported_cb _hidl_cb) override { 123 mCalledCounter[IS_SET_TORCH_MODE_SUPPORTED]++; 124 _hidl_cb(Status::OK, false); 125 return hardware::Void(); 126 } 127 128 using getCameraIdList_cb = std::function<void(Status status, 129 const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames)>; 130 virtual hardware::Return<void> getCameraIdList(getCameraIdList_cb _hidl_cb) override { 131 mCalledCounter[GET_CAMERA_ID_LIST]++; 132 _hidl_cb(Status::OK, mDeviceNames); 133 return hardware::Void(); 134 } 135 136 using getCameraDeviceInterface_V1_x_cb = std::function<void(Status status, 137 const sp<device::V1_0::ICameraDevice>& device)>; 138 virtual hardware::Return<void> getCameraDeviceInterface_V1_x( 139 const hardware::hidl_string& cameraDeviceName, 140 getCameraDeviceInterface_V1_x_cb _hidl_cb) override { 141 (void) cameraDeviceName; 142 _hidl_cb(Status::OK, nullptr); //TODO: impl. of ver. 1.0 device interface 143 // otherwise enumeration will fail. 144 return hardware::Void(); 145 } 146 147 using getCameraDeviceInterface_V3_x_cb = std::function<void(Status status, 148 const sp<device::V3_2::ICameraDevice>& device)>; 149 virtual hardware::Return<void> getCameraDeviceInterface_V3_x( 150 const hardware::hidl_string&, 151 getCameraDeviceInterface_V3_x_cb _hidl_cb) override { 152 _hidl_cb(Status::OK, mDeviceInterface); 153 return hardware::Void(); 154 } 155 156 virtual hardware::Return<void> notifyDeviceStateChange( 157 hardware::hidl_bitfield<DeviceState> newState) override { 158 mCalledCounter[NOTIFY_DEVICE_STATE]++; 159 mCurrentState = newState; 160 return hardware::Void(); 161 } 162 163 enum MethodNames { 164 SET_CALLBACK, 165 GET_VENDOR_TAGS, 166 IS_SET_TORCH_MODE_SUPPORTED, 167 NOTIFY_DEVICE_STATE, 168 GET_CAMERA_ID_LIST, 169 170 METHOD_NAME_COUNT 171 }; 172 int mCalledCounter[METHOD_NAME_COUNT] {0}; 173 174 hardware::hidl_bitfield<DeviceState> mCurrentState = 0xFFFFFFFF; // Unlikely to be a real state 175 }; 176 177 /** 178 * Simple test version of the interaction proxy, to use to inject onRegistered calls to the 179 * CameraProviderManager 180 */ 181 struct TestInteractionProxy : public CameraProviderManager::ServiceInteractionProxy { 182 sp<hidl::manager::V1_0::IServiceNotification> mManagerNotificationInterface; 183 sp<TestICameraProvider> mTestCameraProvider; 184 185 TestInteractionProxy() {} 186 void setProvider(sp<TestICameraProvider> provider) { 187 mTestCameraProvider = provider; 188 } 189 190 std::vector<std::string> mLastRequestedServiceNames; 191 192 virtual ~TestInteractionProxy() {} 193 194 virtual bool registerForNotifications( 195 const std::string &serviceName, 196 const sp<hidl::manager::V1_0::IServiceNotification> ¬ification) override { 197 (void) serviceName; 198 mManagerNotificationInterface = notification; 199 return true; 200 } 201 202 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService( 203 const std::string &serviceName) override { 204 mLastRequestedServiceNames.push_back(serviceName); 205 return mTestCameraProvider; 206 } 207 208 }; 209 210 struct TestStatusListener : public CameraProviderManager::StatusListener { 211 ~TestStatusListener() {} 212 213 void onDeviceStatusChanged(const String8 &, 214 hardware::camera::common::V1_0::CameraDeviceStatus) override {} 215 void onTorchStatusChanged(const String8 &, 216 hardware::camera::common::V1_0::TorchModeStatus) override {} 217 void onNewProviderRegistered() override {} 218 }; 219 220 TEST(CameraProviderManagerTest, InitializeTest) { 221 std::vector<hardware::hidl_string> deviceNames; 222 deviceNames.push_back("device (at) 3.2/test/0"); 223 deviceNames.push_back("device (at) 1.0/test/0"); 224 deviceNames.push_back("device (at) 3.2/test/1"); 225 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection; 226 status_t res; 227 sp<CameraProviderManager> providerManager = new CameraProviderManager(); 228 sp<TestStatusListener> statusListener = new TestStatusListener(); 229 TestInteractionProxy serviceProxy; 230 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames, 231 vendorSection); 232 serviceProxy.setProvider(provider); 233 234 res = providerManager->initialize(statusListener, &serviceProxy); 235 ASSERT_EQ(res, OK) << "Unable to initialize provider manager"; 236 // Check that both "legacy" and "external" providers (really the same object) are called 237 // once for all the init methods 238 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::SET_CALLBACK], 2) << 239 "Only one call to setCallback per provider expected during init"; 240 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_VENDOR_TAGS], 2) << 241 "Only one call to getVendorTags per provider expected during init"; 242 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::IS_SET_TORCH_MODE_SUPPORTED], 2) << 243 "Only one call to isSetTorchModeSupported per provider expected during init"; 244 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_CAMERA_ID_LIST], 2) << 245 "Only one call to getCameraIdList per provider expected during init"; 246 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::NOTIFY_DEVICE_STATE], 2) << 247 "Only one call to notifyDeviceState per provider expected during init"; 248 249 std::string legacyInstanceName = "legacy/0"; 250 std::string externalInstanceName = "external/0"; 251 bool gotLegacy = false; 252 bool gotExternal = false; 253 EXPECT_EQ(2u, serviceProxy.mLastRequestedServiceNames.size()) << 254 "Only two service queries expected to be seen by hardware service manager"; 255 256 for (auto& serviceName : serviceProxy.mLastRequestedServiceNames) { 257 if (serviceName == legacyInstanceName) gotLegacy = true; 258 if (serviceName == externalInstanceName) gotExternal = true; 259 } 260 ASSERT_TRUE(gotLegacy) << 261 "Legacy instance not requested from service manager"; 262 ASSERT_TRUE(gotExternal) << 263 "External instance not requested from service manager"; 264 265 hardware::hidl_string testProviderFqInterfaceName = 266 "android.hardware.camera.provider (at) 2.4::ICameraProvider"; 267 hardware::hidl_string testProviderInstanceName = "test/0"; 268 serviceProxy.mManagerNotificationInterface->onRegistration( 269 testProviderFqInterfaceName, 270 testProviderInstanceName, false); 271 272 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) << 273 "Incorrect instance requested from service manager"; 274 } 275 276 TEST(CameraProviderManagerTest, MultipleVendorTagTest) { 277 hardware::hidl_string sectionName = "VendorTestSection"; 278 hardware::hidl_string tagName = "VendorTestTag"; 279 uint32_t tagId = VENDOR_SECTION << 16; 280 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection; 281 CameraMetadataType tagType = CameraMetadataType::BYTE; 282 vendorSection.resize(1); 283 vendorSection[0].sectionName = sectionName; 284 vendorSection[0].tags.resize(1); 285 vendorSection[0].tags[0].tagId = tagId; 286 vendorSection[0].tags[0].tagName = tagName; 287 vendorSection[0].tags[0].tagType = tagType; 288 std::vector<hardware::hidl_string> deviceNames = {"device (at) 3.2/test/0"}; 289 290 sp<CameraProviderManager> providerManager = new CameraProviderManager(); 291 sp<TestStatusListener> statusListener = new TestStatusListener(); 292 TestInteractionProxy serviceProxy; 293 294 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames, 295 vendorSection); 296 serviceProxy.setProvider(provider); 297 298 auto res = providerManager->initialize(statusListener, &serviceProxy); 299 ASSERT_EQ(res, OK) << "Unable to initialize provider manager"; 300 301 hardware::hidl_string testProviderInstanceName = "test/0"; 302 hardware::hidl_string testProviderFqInterfaceName = 303 "android.hardware.camera.provider (at) 2.4::ICameraProvider"; 304 serviceProxy.mManagerNotificationInterface->onRegistration( 305 testProviderFqInterfaceName, testProviderInstanceName, false); 306 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) << 307 "Incorrect instance requested from service manager"; 308 309 hardware::hidl_string sectionNameSecond = "SecondVendorTestSection"; 310 hardware::hidl_string secondTagName = "SecondVendorTestTag"; 311 CameraMetadataType secondTagType = CameraMetadataType::DOUBLE; 312 vendorSection[0].sectionName = sectionNameSecond; 313 vendorSection[0].tags[0].tagId = tagId; 314 vendorSection[0].tags[0].tagName = secondTagName; 315 vendorSection[0].tags[0].tagType = secondTagType; 316 deviceNames = {"device (at) 3.2/test2/1"}; 317 318 sp<TestICameraProvider> secondProvider = new TestICameraProvider( 319 deviceNames, vendorSection); 320 serviceProxy.setProvider(secondProvider); 321 hardware::hidl_string testProviderSecondInstanceName = "test2/0"; 322 serviceProxy.mManagerNotificationInterface->onRegistration( 323 testProviderFqInterfaceName, testProviderSecondInstanceName, false); 324 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), 325 testProviderSecondInstanceName) << 326 "Incorrect instance requested from service manager"; 327 328 ASSERT_EQ(NO_ERROR , providerManager->setUpVendorTags()); 329 sp<VendorTagDescriptorCache> vendorCache = 330 VendorTagDescriptorCache::getGlobalVendorTagCache(); 331 ASSERT_NE(nullptr, vendorCache.get()); 332 333 metadata_vendor_id_t vendorId = std::hash<std::string> {} ( 334 testProviderInstanceName.c_str()); 335 metadata_vendor_id_t vendorIdSecond = std::hash<std::string> {} ( 336 testProviderSecondInstanceName.c_str()); 337 338 hardware::hidl_string resultTag = vendorCache->getTagName(tagId, vendorId); 339 ASSERT_EQ(resultTag, tagName); 340 341 resultTag = vendorCache->getTagName(tagId, vendorIdSecond); 342 ASSERT_EQ(resultTag, secondTagName); 343 344 // Check whether we can create two separate CameraMetadata instances 345 // using different tag vendor vendors. 346 camera_metadata *metaBuffer = allocate_camera_metadata(10, 20); 347 ASSERT_NE(nullptr, metaBuffer); 348 set_camera_metadata_vendor_id(metaBuffer, vendorId); 349 CameraMetadata metadata(metaBuffer); 350 351 uint8_t byteVal = 10; 352 ASSERT_TRUE(metadata.isEmpty()); 353 ASSERT_EQ(OK, metadata.update(tagId, &byteVal, 1)); 354 ASSERT_FALSE(metadata.isEmpty()); 355 ASSERT_TRUE(metadata.exists(tagId)); 356 357 metaBuffer = allocate_camera_metadata(10, 20); 358 ASSERT_NE(nullptr, metaBuffer); 359 set_camera_metadata_vendor_id(metaBuffer, vendorIdSecond); 360 CameraMetadata secondMetadata(metaBuffer); 361 362 ASSERT_TRUE(secondMetadata.isEmpty()); 363 double doubleVal = 1.0f; 364 ASSERT_EQ(OK, secondMetadata.update(tagId, &doubleVal, 1)); 365 ASSERT_FALSE(secondMetadata.isEmpty()); 366 ASSERT_TRUE(secondMetadata.exists(tagId)); 367 368 // Check whether CameraMetadata copying works as expected 369 CameraMetadata metadataCopy(metadata); 370 ASSERT_FALSE(metadataCopy.isEmpty()); 371 ASSERT_TRUE(metadataCopy.exists(tagId)); 372 ASSERT_EQ(OK, metadataCopy.update(tagId, &byteVal, 1)); 373 ASSERT_TRUE(metadataCopy.exists(tagId)); 374 375 // Check whether values are as expected 376 camera_metadata_entry_t entry = metadata.find(tagId); 377 ASSERT_EQ(1u, entry.count); 378 ASSERT_EQ(byteVal, entry.data.u8[0]); 379 entry = secondMetadata.find(tagId); 380 ASSERT_EQ(1u, entry.count); 381 ASSERT_EQ(doubleVal, entry.data.d[0]); 382 383 // Swap and erase 384 secondMetadata.swap(metadataCopy); 385 ASSERT_TRUE(metadataCopy.exists(tagId)); 386 ASSERT_TRUE(secondMetadata.exists(tagId)); 387 ASSERT_EQ(OK, secondMetadata.erase(tagId)); 388 ASSERT_TRUE(secondMetadata.isEmpty()); 389 doubleVal = 0.0f; 390 ASSERT_EQ(OK, metadataCopy.update(tagId, &doubleVal, 1)); 391 entry = metadataCopy.find(tagId); 392 ASSERT_EQ(1u, entry.count); 393 ASSERT_EQ(doubleVal, entry.data.d[0]); 394 395 // Append 396 uint8_t sceneMode = ANDROID_CONTROL_SCENE_MODE_ACTION; 397 secondMetadata.update(ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1); 398 // Append from two different vendor tag providers is not supported! 399 ASSERT_NE(OK, metadataCopy.append(secondMetadata)); 400 ASSERT_EQ(OK, metadataCopy.erase(tagId)); 401 metadataCopy.update(ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1); 402 // However appending from same vendor tag provider should be fine 403 ASSERT_EQ(OK, metadata.append(secondMetadata)); 404 // Append from a metadata without vendor tag provider should be supported 405 CameraMetadata regularMetadata(10, 20); 406 uint8_t controlMode = ANDROID_CONTROL_MODE_AUTO; 407 regularMetadata.update(ANDROID_CONTROL_MODE, &controlMode, 1); 408 ASSERT_EQ(OK, secondMetadata.append(regularMetadata)); 409 ASSERT_EQ(2u, secondMetadata.entryCount()); 410 ASSERT_EQ(2u, metadata.entryCount()); 411 412 // Dump 413 metadata.dump(1, 2); 414 metadataCopy.dump(1, 2); 415 secondMetadata.dump(1, 2); 416 } 417 418 TEST(CameraProviderManagerTest, NotifyStateChangeTest) { 419 std::vector<hardware::hidl_string> deviceNames { 420 "device (at) 3.2/test/0", 421 "device (at) 1.0/test/0", 422 "device (at) 3.2/test/1"}; 423 424 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection; 425 status_t res; 426 sp<CameraProviderManager> providerManager = new CameraProviderManager(); 427 sp<TestStatusListener> statusListener = new TestStatusListener(); 428 TestInteractionProxy serviceProxy; 429 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames, 430 vendorSection); 431 serviceProxy.setProvider(provider); 432 433 res = providerManager->initialize(statusListener, &serviceProxy); 434 ASSERT_EQ(res, OK) << "Unable to initialize provider manager"; 435 436 ASSERT_EQ(provider->mCurrentState, 437 static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::NORMAL)) 438 << "Initial device state not set"; 439 440 res = providerManager->notifyDeviceStateChange( 441 static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::FOLDED)); 442 443 ASSERT_EQ(res, OK) << "Unable to call notifyDeviceStateChange"; 444 ASSERT_EQ(provider->mCurrentState, 445 static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::FOLDED)) 446 << "Unable to change device state"; 447 448 } 449