Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 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 | |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | using namespace android; |
| 27 | using namespace android::hardware::camera; |
| 28 | using android::hardware::camera::common::V1_0::Status; |
| 29 | |
| 30 | /** |
| 31 | * Basic test implementation of a camera provider |
| 32 | */ |
| 33 | struct TestICameraProvider : virtual public provider::V2_4::ICameraProvider { |
| 34 | sp<provider::V2_4::ICameraProviderCallbacks> mCallbacks; |
| 35 | |
| 36 | std::vector<hardware::hidl_string> mDeviceNames; |
| 37 | |
| 38 | TestICameraProvider() { |
| 39 | mDeviceNames.push_back("device@3.2/test/0"); |
| 40 | mDeviceNames.push_back("device@1.0/test/0"); |
| 41 | mDeviceNames.push_back("device@3.2/test/1"); |
| 42 | } |
| 43 | |
| 44 | virtual hardware::Return<Status> setCallbacks( |
| 45 | const sp<provider::V2_4::ICameraProviderCallbacks>& callbacks) override { |
| 46 | mCallbacks = callbacks; |
| 47 | return hardware::Return<Status>(Status::OK); |
| 48 | } |
| 49 | |
| 50 | using getVendorTags_cb = std::function<void(Status status, |
| 51 | const hardware::hidl_vec<common::V1_0::VendorTagSection>& sections)>; |
| 52 | virtual hardware::Return<void> getVendorTags(getVendorTags_cb _hidl_cb) override { |
| 53 | hardware::hidl_vec<common::V1_0::VendorTagSection> sections; |
| 54 | _hidl_cb(Status::OK, sections); |
| 55 | return hardware::Void(); |
| 56 | } |
| 57 | |
| 58 | using getCameraIdList_cb = std::function<void(Status status, |
| 59 | const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames)>; |
| 60 | virtual hardware::Return<void> getCameraIdList(getCameraIdList_cb _hidl_cb) override { |
| 61 | _hidl_cb(Status::OK, mDeviceNames); |
| 62 | return hardware::Void(); |
| 63 | } |
| 64 | |
| 65 | using getCameraDeviceInterface_V1_x_cb = std::function<void(Status status, |
| 66 | const sp<device::V1_0::ICameraDevice>& device)>; |
| 67 | virtual hardware::Return<void> getCameraDeviceInterface_V1_x( |
| 68 | const hardware::hidl_string& cameraDeviceName, |
| 69 | getCameraDeviceInterface_V1_x_cb _hidl_cb) override { |
| 70 | (void) cameraDeviceName; |
| 71 | _hidl_cb(Status::OK, nullptr); |
| 72 | return hardware::Void(); |
| 73 | } |
| 74 | |
| 75 | using getCameraDeviceInterface_V3_x_cb = std::function<void(Status status, |
| 76 | const sp<device::V3_2::ICameraDevice>& device)>; |
| 77 | virtual hardware::Return<void> getCameraDeviceInterface_V3_x( |
| 78 | const hardware::hidl_string& cameraDeviceName, |
| 79 | getCameraDeviceInterface_V3_x_cb _hidl_cb) override { |
| 80 | (void) cameraDeviceName; |
| 81 | _hidl_cb(Status::OK, nullptr); |
| 82 | return hardware::Void(); |
| 83 | } |
| 84 | |
| 85 | }; |
| 86 | |
| 87 | /** |
| 88 | * Simple test version of the interaction proxy, to use to inject onRegistered calls to the |
| 89 | * CameraProviderManager |
| 90 | */ |
| 91 | struct TestInteractionProxy : public CameraProviderManager::ServiceInteractionProxy { |
| 92 | sp<hidl::manager::V1_0::IServiceNotification> mManagerNotificationInterface; |
| 93 | const sp<TestICameraProvider> mTestCameraProvider; |
| 94 | |
| 95 | TestInteractionProxy() : |
| 96 | mTestCameraProvider(new TestICameraProvider()) { |
| 97 | |
| 98 | } |
| 99 | std::string mLastRequestedServiceName; |
| 100 | |
| 101 | virtual ~TestInteractionProxy() {} |
| 102 | |
| 103 | virtual bool registerForNotifications( |
| 104 | const std::string &serviceName, |
| 105 | const sp<hidl::manager::V1_0::IServiceNotification> ¬ification) override { |
| 106 | (void) serviceName; |
| 107 | mManagerNotificationInterface = notification; |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService( |
| 112 | const std::string &serviceName) override { |
| 113 | mLastRequestedServiceName = serviceName; |
| 114 | return mTestCameraProvider; |
| 115 | } |
| 116 | |
| 117 | }; |
| 118 | |
| 119 | TEST(CameraProviderManagerTest, InitializeTest) { |
| 120 | |
| 121 | status_t res; |
| 122 | sp<CameraProviderManager> providerManager = new CameraProviderManager(); |
| 123 | TestInteractionProxy serviceProxy{}; |
| 124 | |
| 125 | res = providerManager->initialize(&serviceProxy); |
| 126 | ASSERT_EQ(res, OK) << "Unable to initialize provider manager"; |
| 127 | |
| 128 | hardware::hidl_string legacyInstanceName = "legacy/0"; |
| 129 | ASSERT_EQ(serviceProxy.mLastRequestedServiceName, legacyInstanceName) << |
| 130 | "Legacy instance not requested from service manager"; |
| 131 | |
| 132 | hardware::hidl_string testProviderFqInterfaceName = |
| 133 | "android.hardware.camera.provider@2.4::ICameraProvider"; |
| 134 | hardware::hidl_string testProviderInstanceName = "test/0"; |
| 135 | serviceProxy.mManagerNotificationInterface->onRegistration( |
| 136 | testProviderFqInterfaceName, |
| 137 | testProviderInstanceName, false); |
| 138 | |
| 139 | ASSERT_EQ(serviceProxy.mLastRequestedServiceName, testProviderInstanceName) << |
| 140 | "Incorrect instance requested from service manager"; |
| 141 | } |