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;
TestDeviceInterfaceTestDeviceInterface43 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)>;
getResourceCostTestDeviceInterface48 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)>;
getCameraCharacteristicsTestDeviceInterface59 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
setTorchModeTestDeviceInterface66 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)>;
openTestDeviceInterface74 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
dumpStateTestDeviceInterface82 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
TestICameraProviderTestICameraProvider97 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
setCallbackTestICameraProvider103 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)>;
getVendorTagsTestICameraProvider112 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)>;
isSetTorchModeSupportedTestICameraProvider121 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)>;
getCameraIdListTestICameraProvider130 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)>;
getCameraDeviceInterface_V1_xTestICameraProvider138 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)>;
getCameraDeviceInterface_V3_xTestICameraProvider149 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
notifyDeviceStateChangeTestICameraProvider156 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
TestInteractionProxyTestInteractionProxy185 TestInteractionProxy() {}
setProviderTestInteractionProxy186 void setProvider(sp<TestICameraProvider> provider) {
187 mTestCameraProvider = provider;
188 }
189
190 std::vector<std::string> mLastRequestedServiceNames;
191
~TestInteractionProxyTestInteractionProxy192 virtual ~TestInteractionProxy() {}
193
registerForNotificationsTestInteractionProxy194 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
getServiceTestInteractionProxy202 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
listServicesTestInteractionProxy208 virtual hardware::hidl_vec<hardware::hidl_string> listServices() override {
209 hardware::hidl_vec<hardware::hidl_string> ret = {"test/0"};
210 return ret;
211 }
212
213 };
214
215 struct TestStatusListener : public CameraProviderManager::StatusListener {
~TestStatusListenerTestStatusListener216 ~TestStatusListener() {}
217
onDeviceStatusChangedTestStatusListener218 void onDeviceStatusChanged(const String8 &,
219 hardware::camera::common::V1_0::CameraDeviceStatus) override {}
onTorchStatusChangedTestStatusListener220 void onTorchStatusChanged(const String8 &,
221 hardware::camera::common::V1_0::TorchModeStatus) override {}
onNewProviderRegisteredTestStatusListener222 void onNewProviderRegistered() override {}
223 };
224
TEST(CameraProviderManagerTest,InitializeTest)225 TEST(CameraProviderManagerTest, InitializeTest) {
226 std::vector<hardware::hidl_string> deviceNames;
227 deviceNames.push_back("device@3.2/test/0");
228 deviceNames.push_back("device@1.0/test/0");
229 deviceNames.push_back("device@3.2/test/1");
230 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
231 status_t res;
232 sp<CameraProviderManager> providerManager = new CameraProviderManager();
233 sp<TestStatusListener> statusListener = new TestStatusListener();
234 TestInteractionProxy serviceProxy;
235 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
236 vendorSection);
237 serviceProxy.setProvider(provider);
238
239 int numProviders = static_cast<int>(serviceProxy.listServices().size());
240
241 res = providerManager->initialize(statusListener, &serviceProxy);
242 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
243 // Check that both "legacy" and "external" providers (really the same object) are called
244 // once for all the init methods
245 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::SET_CALLBACK], numProviders) <<
246 "Only one call to setCallback per provider expected during init";
247 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_VENDOR_TAGS], numProviders) <<
248 "Only one call to getVendorTags per provider expected during init";
249 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::IS_SET_TORCH_MODE_SUPPORTED],
250 numProviders) <<
251 "Only one call to isSetTorchModeSupported per provider expected during init";
252 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_CAMERA_ID_LIST], numProviders) <<
253 "Only one call to getCameraIdList per provider expected during init";
254 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::NOTIFY_DEVICE_STATE], numProviders) <<
255 "Only one call to notifyDeviceState per provider expected during init";
256
257 hardware::hidl_string testProviderFqInterfaceName =
258 "android.hardware.camera.provider@2.4::ICameraProvider";
259 hardware::hidl_string testProviderInstanceName = "test/0";
260 serviceProxy.mManagerNotificationInterface->onRegistration(
261 testProviderFqInterfaceName,
262 testProviderInstanceName, false);
263
264 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) <<
265 "Incorrect instance requested from service manager";
266 }
267
TEST(CameraProviderManagerTest,MultipleVendorTagTest)268 TEST(CameraProviderManagerTest, MultipleVendorTagTest) {
269 hardware::hidl_string sectionName = "VendorTestSection";
270 hardware::hidl_string tagName = "VendorTestTag";
271 uint32_t tagId = VENDOR_SECTION << 16;
272 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
273 CameraMetadataType tagType = CameraMetadataType::BYTE;
274 vendorSection.resize(1);
275 vendorSection[0].sectionName = sectionName;
276 vendorSection[0].tags.resize(1);
277 vendorSection[0].tags[0].tagId = tagId;
278 vendorSection[0].tags[0].tagName = tagName;
279 vendorSection[0].tags[0].tagType = tagType;
280 std::vector<hardware::hidl_string> deviceNames = {"device@3.2/test/0"};
281
282 sp<CameraProviderManager> providerManager = new CameraProviderManager();
283 sp<TestStatusListener> statusListener = new TestStatusListener();
284 TestInteractionProxy serviceProxy;
285
286 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
287 vendorSection);
288 serviceProxy.setProvider(provider);
289
290 auto res = providerManager->initialize(statusListener, &serviceProxy);
291 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
292
293 hardware::hidl_string testProviderInstanceName = "test/0";
294 hardware::hidl_string testProviderFqInterfaceName =
295 "android.hardware.camera.provider@2.4::ICameraProvider";
296 serviceProxy.mManagerNotificationInterface->onRegistration(
297 testProviderFqInterfaceName, testProviderInstanceName, false);
298 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) <<
299 "Incorrect instance requested from service manager";
300
301 hardware::hidl_string sectionNameSecond = "SecondVendorTestSection";
302 hardware::hidl_string secondTagName = "SecondVendorTestTag";
303 CameraMetadataType secondTagType = CameraMetadataType::DOUBLE;
304 vendorSection[0].sectionName = sectionNameSecond;
305 vendorSection[0].tags[0].tagId = tagId;
306 vendorSection[0].tags[0].tagName = secondTagName;
307 vendorSection[0].tags[0].tagType = secondTagType;
308 deviceNames = {"device@3.2/test2/1"};
309
310 sp<TestICameraProvider> secondProvider = new TestICameraProvider(
311 deviceNames, vendorSection);
312 serviceProxy.setProvider(secondProvider);
313 hardware::hidl_string testProviderSecondInstanceName = "test2/0";
314 serviceProxy.mManagerNotificationInterface->onRegistration(
315 testProviderFqInterfaceName, testProviderSecondInstanceName, false);
316 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(),
317 testProviderSecondInstanceName) <<
318 "Incorrect instance requested from service manager";
319
320 ASSERT_EQ(NO_ERROR , providerManager->setUpVendorTags());
321 sp<VendorTagDescriptorCache> vendorCache =
322 VendorTagDescriptorCache::getGlobalVendorTagCache();
323 ASSERT_NE(nullptr, vendorCache.get());
324
325 metadata_vendor_id_t vendorId = std::hash<std::string> {} (
326 testProviderInstanceName.c_str());
327 metadata_vendor_id_t vendorIdSecond = std::hash<std::string> {} (
328 testProviderSecondInstanceName.c_str());
329
330 hardware::hidl_string resultTag = vendorCache->getTagName(tagId, vendorId);
331 ASSERT_EQ(resultTag, tagName);
332
333 resultTag = vendorCache->getTagName(tagId, vendorIdSecond);
334 ASSERT_EQ(resultTag, secondTagName);
335
336 // Check whether we can create two separate CameraMetadata instances
337 // using different tag vendor vendors.
338 camera_metadata *metaBuffer = allocate_camera_metadata(10, 20);
339 ASSERT_NE(nullptr, metaBuffer);
340 set_camera_metadata_vendor_id(metaBuffer, vendorId);
341 CameraMetadata metadata(metaBuffer);
342
343 uint8_t byteVal = 10;
344 ASSERT_TRUE(metadata.isEmpty());
345 ASSERT_EQ(OK, metadata.update(tagId, &byteVal, 1));
346 ASSERT_FALSE(metadata.isEmpty());
347 ASSERT_TRUE(metadata.exists(tagId));
348
349 metaBuffer = allocate_camera_metadata(10, 20);
350 ASSERT_NE(nullptr, metaBuffer);
351 set_camera_metadata_vendor_id(metaBuffer, vendorIdSecond);
352 CameraMetadata secondMetadata(metaBuffer);
353
354 ASSERT_TRUE(secondMetadata.isEmpty());
355 double doubleVal = 1.0f;
356 ASSERT_EQ(OK, secondMetadata.update(tagId, &doubleVal, 1));
357 ASSERT_FALSE(secondMetadata.isEmpty());
358 ASSERT_TRUE(secondMetadata.exists(tagId));
359
360 // Check whether CameraMetadata copying works as expected
361 CameraMetadata metadataCopy(metadata);
362 ASSERT_FALSE(metadataCopy.isEmpty());
363 ASSERT_TRUE(metadataCopy.exists(tagId));
364 ASSERT_EQ(OK, metadataCopy.update(tagId, &byteVal, 1));
365 ASSERT_TRUE(metadataCopy.exists(tagId));
366
367 // Check whether values are as expected
368 camera_metadata_entry_t entry = metadata.find(tagId);
369 ASSERT_EQ(1u, entry.count);
370 ASSERT_EQ(byteVal, entry.data.u8[0]);
371 entry = secondMetadata.find(tagId);
372 ASSERT_EQ(1u, entry.count);
373 ASSERT_EQ(doubleVal, entry.data.d[0]);
374
375 // Swap and erase
376 secondMetadata.swap(metadataCopy);
377 ASSERT_TRUE(metadataCopy.exists(tagId));
378 ASSERT_TRUE(secondMetadata.exists(tagId));
379 ASSERT_EQ(OK, secondMetadata.erase(tagId));
380 ASSERT_TRUE(secondMetadata.isEmpty());
381 doubleVal = 0.0f;
382 ASSERT_EQ(OK, metadataCopy.update(tagId, &doubleVal, 1));
383 entry = metadataCopy.find(tagId);
384 ASSERT_EQ(1u, entry.count);
385 ASSERT_EQ(doubleVal, entry.data.d[0]);
386
387 // Append
388 uint8_t sceneMode = ANDROID_CONTROL_SCENE_MODE_ACTION;
389 secondMetadata.update(ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1);
390 // Append from two different vendor tag providers is not supported!
391 ASSERT_NE(OK, metadataCopy.append(secondMetadata));
392 ASSERT_EQ(OK, metadataCopy.erase(tagId));
393 metadataCopy.update(ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1);
394 // However appending from same vendor tag provider should be fine
395 ASSERT_EQ(OK, metadata.append(secondMetadata));
396 // Append from a metadata without vendor tag provider should be supported
397 CameraMetadata regularMetadata(10, 20);
398 uint8_t controlMode = ANDROID_CONTROL_MODE_AUTO;
399 regularMetadata.update(ANDROID_CONTROL_MODE, &controlMode, 1);
400 ASSERT_EQ(OK, secondMetadata.append(regularMetadata));
401 ASSERT_EQ(2u, secondMetadata.entryCount());
402 ASSERT_EQ(2u, metadata.entryCount());
403
404 // Dump
405 metadata.dump(1, 2);
406 metadataCopy.dump(1, 2);
407 secondMetadata.dump(1, 2);
408 }
409
TEST(CameraProviderManagerTest,NotifyStateChangeTest)410 TEST(CameraProviderManagerTest, NotifyStateChangeTest) {
411 std::vector<hardware::hidl_string> deviceNames {
412 "device@3.2/test/0",
413 "device@1.0/test/0",
414 "device@3.2/test/1"};
415
416 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
417 status_t res;
418 sp<CameraProviderManager> providerManager = new CameraProviderManager();
419 sp<TestStatusListener> statusListener = new TestStatusListener();
420 TestInteractionProxy serviceProxy;
421 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
422 vendorSection);
423 serviceProxy.setProvider(provider);
424
425 res = providerManager->initialize(statusListener, &serviceProxy);
426 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
427
428 ASSERT_EQ(provider->mCurrentState,
429 static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::NORMAL))
430 << "Initial device state not set";
431
432 res = providerManager->notifyDeviceStateChange(
433 static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::FOLDED));
434
435 ASSERT_EQ(res, OK) << "Unable to call notifyDeviceStateChange";
436 ASSERT_EQ(provider->mCurrentState,
437 static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::FOLDED))
438 << "Unable to change device state";
439
440 }
441