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     android::hardware::hidl_vec<uint8_t> mCharacteristics;
44 
TestDeviceInterfaceTestDeviceInterface45     TestDeviceInterface(std::vector<hardware::hidl_string> deviceNames,
46             android::hardware::hidl_vec<uint8_t> chars) :
47         mDeviceNames(deviceNames), mCharacteristics(chars) {}
48 
TestDeviceInterfaceTestDeviceInterface49     TestDeviceInterface(std::vector<hardware::hidl_string> deviceNames) :
50         mDeviceNames(deviceNames) {}
51 
52     using getResourceCost_cb = std::function<void(
53             hardware::camera::common::V1_0::Status status,
54             const hardware::camera::common::V1_0::CameraResourceCost& resourceCost)>;
getResourceCostTestDeviceInterface55     virtual ::android::hardware::Return<void> getResourceCost(
56             getResourceCost_cb _hidl_cb) override {
57         hardware::camera::common::V1_0::CameraResourceCost resourceCost = {100,
58                 mDeviceNames};
59         _hidl_cb(Status::OK, resourceCost);
60         return hardware::Void();
61     }
62 
63     using getCameraCharacteristics_cb = std::function<void(
64             hardware::camera::common::V1_0::Status status,
65             const hardware::hidl_vec<uint8_t>& cameraCharacteristics)>;
getCameraCharacteristicsTestDeviceInterface66     hardware::Return<void> getCameraCharacteristics(
67             getCameraCharacteristics_cb _hidl_cb) override {
68         _hidl_cb(Status::OK, mCharacteristics);
69         return hardware::Void();
70     }
71 
setTorchModeTestDeviceInterface72     hardware::Return<hardware::camera::common::V1_0::Status> setTorchMode(
73             ::android::hardware::camera::common::V1_0::TorchMode) override {
74         return Status::OK;
75     }
76 
77     using open_cb = std::function<void(
78             ::android::hardware::camera::common::V1_0::Status status,
79              const ::android::sp<ICameraDeviceSession>& session)>;
openTestDeviceInterface80     hardware::Return<void> open(
81             const ::android::sp<ICameraDeviceCallback>&,
82             open_cb _hidl_cb) override {
83         sp<ICameraDeviceSession> deviceSession = nullptr;
84         _hidl_cb(Status::OK, deviceSession);
85         return hardware::Void();
86     }
87 
dumpStateTestDeviceInterface88     hardware::Return<void> dumpState(
89             const ::android::hardware::hidl_handle&) override {
90         return hardware::Void();
91     }
92 };
93 
94 /**
95  * Basic test implementation of a camera provider
96  */
97 struct TestICameraProvider : virtual public provider::V2_5::ICameraProvider {
98     sp<provider::V2_4::ICameraProviderCallback> mCallbacks;
99     std::vector<hardware::hidl_string> mDeviceNames;
100     sp<device::V3_2::ICameraDevice> mDeviceInterface;
101     hardware::hidl_vec<common::V1_0::VendorTagSection> mVendorTagSections;
102 
TestICameraProviderTestICameraProvider103     TestICameraProvider(const std::vector<hardware::hidl_string> &devices,
104             const hardware::hidl_vec<common::V1_0::VendorTagSection> &vendorSection) :
105         mDeviceNames(devices),
106         mDeviceInterface(new TestDeviceInterface(devices)),
107         mVendorTagSections (vendorSection) {}
108 
TestICameraProviderTestICameraProvider109     TestICameraProvider(const std::vector<hardware::hidl_string> &devices,
110             const hardware::hidl_vec<common::V1_0::VendorTagSection> &vendorSection,
111             android::hardware::hidl_vec<uint8_t> chars) :
112         mDeviceNames(devices),
113         mDeviceInterface(new TestDeviceInterface(devices, chars)),
114         mVendorTagSections (vendorSection) {}
115 
setCallbackTestICameraProvider116     virtual hardware::Return<Status> setCallback(
117             const sp<provider::V2_4::ICameraProviderCallback>& callbacks) override {
118         mCalledCounter[SET_CALLBACK]++;
119         mCallbacks = callbacks;
120         return hardware::Return<Status>(Status::OK);
121     }
122 
123     using getVendorTags_cb = std::function<void(Status status,
124             const hardware::hidl_vec<common::V1_0::VendorTagSection>& sections)>;
getVendorTagsTestICameraProvider125     hardware::Return<void> getVendorTags(getVendorTags_cb _hidl_cb) override {
126         mCalledCounter[GET_VENDOR_TAGS]++;
127         _hidl_cb(Status::OK, mVendorTagSections);
128         return hardware::Void();
129     }
130 
131     using isSetTorchModeSupported_cb = std::function<void(
132             ::android::hardware::camera::common::V1_0::Status status,
133              bool support)>;
isSetTorchModeSupportedTestICameraProvider134     virtual ::hardware::Return<void> isSetTorchModeSupported(
135             isSetTorchModeSupported_cb _hidl_cb) override {
136         mCalledCounter[IS_SET_TORCH_MODE_SUPPORTED]++;
137         _hidl_cb(Status::OK, false);
138         return hardware::Void();
139     }
140 
141     using getCameraIdList_cb = std::function<void(Status status,
142             const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames)>;
getCameraIdListTestICameraProvider143     virtual hardware::Return<void> getCameraIdList(getCameraIdList_cb _hidl_cb) override {
144         mCalledCounter[GET_CAMERA_ID_LIST]++;
145         _hidl_cb(Status::OK, mDeviceNames);
146         return hardware::Void();
147     }
148 
149     using getCameraDeviceInterface_V1_x_cb = std::function<void(Status status,
150             const sp<device::V1_0::ICameraDevice>& device)>;
getCameraDeviceInterface_V1_xTestICameraProvider151     virtual hardware::Return<void> getCameraDeviceInterface_V1_x(
152             const hardware::hidl_string& cameraDeviceName,
153             getCameraDeviceInterface_V1_x_cb _hidl_cb) override {
154         (void) cameraDeviceName;
155         _hidl_cb(Status::OK, nullptr); //TODO: impl. of ver. 1.0 device interface
156                                        //      otherwise enumeration will fail.
157         return hardware::Void();
158     }
159 
160     using getCameraDeviceInterface_V3_x_cb = std::function<void(Status status,
161             const sp<device::V3_2::ICameraDevice>& device)>;
getCameraDeviceInterface_V3_xTestICameraProvider162     virtual hardware::Return<void> getCameraDeviceInterface_V3_x(
163             const hardware::hidl_string&,
164             getCameraDeviceInterface_V3_x_cb _hidl_cb) override {
165         _hidl_cb(Status::OK, mDeviceInterface);
166         return hardware::Void();
167     }
168 
notifyDeviceStateChangeTestICameraProvider169     virtual hardware::Return<void> notifyDeviceStateChange(
170             hardware::hidl_bitfield<DeviceState> newState) override {
171         mCalledCounter[NOTIFY_DEVICE_STATE]++;
172         mCurrentState = newState;
173         return hardware::Void();
174     }
175 
176     enum MethodNames {
177         SET_CALLBACK,
178         GET_VENDOR_TAGS,
179         IS_SET_TORCH_MODE_SUPPORTED,
180         NOTIFY_DEVICE_STATE,
181         GET_CAMERA_ID_LIST,
182 
183         METHOD_NAME_COUNT
184     };
185     int mCalledCounter[METHOD_NAME_COUNT] {0};
186 
187     hardware::hidl_bitfield<DeviceState> mCurrentState = 0xFFFFFFFF; // Unlikely to be a real state
188 };
189 
190 /**
191  * Simple test version of the interaction proxy, to use to inject onRegistered calls to the
192  * CameraProviderManager
193  */
194 struct TestInteractionProxy : public CameraProviderManager::ServiceInteractionProxy {
195     sp<hidl::manager::V1_0::IServiceNotification> mManagerNotificationInterface;
196     sp<TestICameraProvider> mTestCameraProvider;
197 
TestInteractionProxyTestInteractionProxy198     TestInteractionProxy() {}
199 
setProviderTestInteractionProxy200     void setProvider(sp<TestICameraProvider> provider) {
201         mTestCameraProvider = provider;
202     }
203 
204     std::vector<std::string> mLastRequestedServiceNames;
205 
~TestInteractionProxyTestInteractionProxy206     virtual ~TestInteractionProxy() {}
207 
registerForNotificationsTestInteractionProxy208     virtual bool registerForNotifications(
209             const std::string &serviceName,
210             const sp<hidl::manager::V1_0::IServiceNotification> &notification) override {
211         (void) serviceName;
212         mManagerNotificationInterface = notification;
213         return true;
214     }
215 
tryGetServiceTestInteractionProxy216     virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService(
217             const std::string &serviceName) override {
218         // If no provider has been given, act like the HAL isn't available and return null.
219         if (mTestCameraProvider == nullptr) return nullptr;
220         return getService(serviceName);
221     }
222 
getServiceTestInteractionProxy223     virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
224             const std::string &serviceName) override {
225         // If no provider has been given, fail; in reality, getService would
226         // block for HALs that don't start correctly, so we should never use
227         // getService when we don't have a valid HAL running
228         if (mTestCameraProvider == nullptr) {
229             ADD_FAILURE() << "getService called with no valid provider; would block indefinitely";
230             // Real getService would block, but that's bad in unit tests. So
231             // just record an error and return nullptr
232             return nullptr;
233         }
234         mLastRequestedServiceNames.push_back(serviceName);
235         return mTestCameraProvider;
236     }
237 
listServicesTestInteractionProxy238     virtual hardware::hidl_vec<hardware::hidl_string> listServices() override {
239         // Always provide a list even if there's no actual provider yet, to
240         // simulate stuck HAL situations as well
241         hardware::hidl_vec<hardware::hidl_string> ret = {"test/0"};
242         return ret;
243     }
244 
245 };
246 
247 struct TestStatusListener : public CameraProviderManager::StatusListener {
~TestStatusListenerTestStatusListener248     ~TestStatusListener() {}
249 
onDeviceStatusChangedTestStatusListener250     void onDeviceStatusChanged(const String8 &,
251             hardware::camera::common::V1_0::CameraDeviceStatus) override {}
onDeviceStatusChangedTestStatusListener252     void onDeviceStatusChanged(const String8 &, const String8 &,
253             hardware::camera::common::V1_0::CameraDeviceStatus) override {}
onTorchStatusChangedTestStatusListener254     void onTorchStatusChanged(const String8 &,
255             hardware::camera::common::V1_0::TorchModeStatus) override {}
onNewProviderRegisteredTestStatusListener256     void onNewProviderRegistered() override {}
257 };
258 
TEST(CameraProviderManagerTest,InitializeDynamicDepthTest)259 TEST(CameraProviderManagerTest, InitializeDynamicDepthTest) {
260     std::vector<hardware::hidl_string> deviceNames;
261     deviceNames.push_back("device@3.2/test/0");
262     hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
263     status_t res;
264     sp<CameraProviderManager> providerManager = new CameraProviderManager();
265     sp<TestStatusListener> statusListener = new TestStatusListener();
266     TestInteractionProxy serviceProxy;
267 
268     android::hardware::hidl_vec<uint8_t> chars;
269     CameraMetadata meta;
270     int32_t charKeys[] = { ANDROID_DEPTH_DEPTH_IS_EXCLUSIVE,
271             ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS };
272     meta.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, charKeys,
273             sizeof(charKeys) / sizeof(charKeys[0]));
274     uint8_t depthIsExclusive = ANDROID_DEPTH_DEPTH_IS_EXCLUSIVE_FALSE;
275     meta.update(ANDROID_DEPTH_DEPTH_IS_EXCLUSIVE, &depthIsExclusive, 1);
276     int32_t sizes[] = { HAL_PIXEL_FORMAT_BLOB,
277             640, 480, ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT };
278     meta.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, sizes,
279             sizeof(sizes) / sizeof(sizes[0]));
280     sizes[0] = HAL_PIXEL_FORMAT_Y16;
281     meta.update(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS, sizes,
282             sizeof(sizes) / sizeof(sizes[0]));
283     int64_t durations[] = { HAL_PIXEL_FORMAT_BLOB, 640, 480, 0 };
284     meta.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, durations,
285             sizeof(durations) / sizeof(durations[0]));
286     meta.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, durations,
287             sizeof(durations) / sizeof(durations[0]));
288     durations[0]= HAL_PIXEL_FORMAT_Y16;
289     meta.update(ANDROID_DEPTH_AVAILABLE_DEPTH_MIN_FRAME_DURATIONS, durations,
290             sizeof(durations) / sizeof(durations[0]));
291     meta.update(ANDROID_DEPTH_AVAILABLE_DEPTH_STALL_DURATIONS, durations,
292             sizeof(durations) / sizeof(durations[0]));
293     camera_metadata_t* metaBuffer = const_cast<camera_metadata_t*>(meta.getAndLock());
294     chars.setToExternal(reinterpret_cast<uint8_t*>(metaBuffer),
295             get_camera_metadata_size(metaBuffer));
296 
297     sp<TestICameraProvider> provider =  new TestICameraProvider(deviceNames,
298             vendorSection, chars);
299     serviceProxy.setProvider(provider);
300 
301     res = providerManager->initialize(statusListener, &serviceProxy);
302     ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
303 }
304 
TEST(CameraProviderManagerTest,InitializeTest)305 TEST(CameraProviderManagerTest, InitializeTest) {
306     std::vector<hardware::hidl_string> deviceNames;
307     deviceNames.push_back("device@3.2/test/0");
308     deviceNames.push_back("device@1.0/test/0");
309     deviceNames.push_back("device@3.2/test/1");
310     hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
311     status_t res;
312     sp<CameraProviderManager> providerManager = new CameraProviderManager();
313     sp<TestStatusListener> statusListener = new TestStatusListener();
314     TestInteractionProxy serviceProxy;
315     sp<TestICameraProvider> provider =  new TestICameraProvider(deviceNames,
316             vendorSection);
317     serviceProxy.setProvider(provider);
318 
319     int numProviders = static_cast<int>(serviceProxy.listServices().size());
320 
321     res = providerManager->initialize(statusListener, &serviceProxy);
322     ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
323     // Check that both "legacy" and "external" providers (really the same object) are called
324     // once for all the init methods
325     EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::SET_CALLBACK], numProviders) <<
326             "Only one call to setCallback per provider expected during init";
327     EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_VENDOR_TAGS], numProviders) <<
328             "Only one call to getVendorTags per provider expected during init";
329     EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::IS_SET_TORCH_MODE_SUPPORTED],
330             numProviders) <<
331             "Only one call to isSetTorchModeSupported per provider expected during init";
332     EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_CAMERA_ID_LIST], numProviders) <<
333             "Only one call to getCameraIdList per provider expected during init";
334     EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::NOTIFY_DEVICE_STATE], numProviders) <<
335             "Only one call to notifyDeviceState per provider expected during init";
336 
337     hardware::hidl_string testProviderFqInterfaceName =
338             "android.hardware.camera.provider@2.4::ICameraProvider";
339     hardware::hidl_string testProviderInstanceName = "test/0";
340     serviceProxy.mManagerNotificationInterface->onRegistration(
341             testProviderFqInterfaceName,
342             testProviderInstanceName, false);
343 
344     ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) <<
345             "Incorrect instance requested from service manager";
346 }
347 
TEST(CameraProviderManagerTest,MultipleVendorTagTest)348 TEST(CameraProviderManagerTest, MultipleVendorTagTest) {
349     hardware::hidl_string sectionName = "VendorTestSection";
350     hardware::hidl_string tagName = "VendorTestTag";
351     uint32_t tagId = VENDOR_SECTION << 16;
352     hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
353     CameraMetadataType tagType = CameraMetadataType::BYTE;
354     vendorSection.resize(1);
355     vendorSection[0].sectionName = sectionName;
356     vendorSection[0].tags.resize(1);
357     vendorSection[0].tags[0].tagId = tagId;
358     vendorSection[0].tags[0].tagName = tagName;
359     vendorSection[0].tags[0].tagType = tagType;
360     std::vector<hardware::hidl_string> deviceNames = {"device@3.2/test/0"};
361 
362     sp<CameraProviderManager> providerManager = new CameraProviderManager();
363     sp<TestStatusListener> statusListener = new TestStatusListener();
364     TestInteractionProxy serviceProxy;
365 
366     sp<TestICameraProvider> provider =  new TestICameraProvider(deviceNames,
367             vendorSection);
368     serviceProxy.setProvider(provider);
369 
370     auto res = providerManager->initialize(statusListener, &serviceProxy);
371     ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
372 
373     hardware::hidl_string testProviderInstanceName = "test/0";
374     hardware::hidl_string testProviderFqInterfaceName =
375             "android.hardware.camera.provider@2.4::ICameraProvider";
376     serviceProxy.mManagerNotificationInterface->onRegistration(
377             testProviderFqInterfaceName, testProviderInstanceName, false);
378     ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) <<
379             "Incorrect instance requested from service manager";
380 
381     hardware::hidl_string sectionNameSecond = "SecondVendorTestSection";
382     hardware::hidl_string secondTagName = "SecondVendorTestTag";
383     CameraMetadataType secondTagType = CameraMetadataType::DOUBLE;
384     vendorSection[0].sectionName = sectionNameSecond;
385     vendorSection[0].tags[0].tagId = tagId;
386     vendorSection[0].tags[0].tagName = secondTagName;
387     vendorSection[0].tags[0].tagType = secondTagType;
388     deviceNames = {"device@3.2/test2/1"};
389 
390     sp<TestICameraProvider> secondProvider =  new TestICameraProvider(
391             deviceNames, vendorSection);
392     serviceProxy.setProvider(secondProvider);
393     hardware::hidl_string testProviderSecondInstanceName = "test2/0";
394     serviceProxy.mManagerNotificationInterface->onRegistration(
395             testProviderFqInterfaceName, testProviderSecondInstanceName, false);
396     ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(),
397               testProviderSecondInstanceName) <<
398             "Incorrect instance requested from service manager";
399 
400     ASSERT_EQ(NO_ERROR , providerManager->setUpVendorTags());
401     sp<VendorTagDescriptorCache> vendorCache =
402             VendorTagDescriptorCache::getGlobalVendorTagCache();
403     ASSERT_NE(nullptr, vendorCache.get());
404 
405     metadata_vendor_id_t vendorId = std::hash<std::string> {} (
406             testProviderInstanceName.c_str());
407     metadata_vendor_id_t vendorIdSecond = std::hash<std::string> {} (
408             testProviderSecondInstanceName.c_str());
409 
410     hardware::hidl_string resultTag = vendorCache->getTagName(tagId, vendorId);
411     ASSERT_EQ(resultTag, tagName);
412 
413     resultTag = vendorCache->getTagName(tagId, vendorIdSecond);
414     ASSERT_EQ(resultTag, secondTagName);
415 
416     // Check whether we can create two separate CameraMetadata instances
417     // using different tag vendor vendors.
418     camera_metadata *metaBuffer = allocate_camera_metadata(10, 20);
419     ASSERT_NE(nullptr, metaBuffer);
420     set_camera_metadata_vendor_id(metaBuffer, vendorId);
421     CameraMetadata metadata(metaBuffer);
422 
423     uint8_t byteVal = 10;
424     ASSERT_TRUE(metadata.isEmpty());
425     ASSERT_EQ(OK, metadata.update(tagId, &byteVal, 1));
426     ASSERT_FALSE(metadata.isEmpty());
427     ASSERT_TRUE(metadata.exists(tagId));
428 
429     metaBuffer = allocate_camera_metadata(10, 20);
430     ASSERT_NE(nullptr, metaBuffer);
431     set_camera_metadata_vendor_id(metaBuffer, vendorIdSecond);
432     CameraMetadata secondMetadata(metaBuffer);
433 
434     ASSERT_TRUE(secondMetadata.isEmpty());
435     double doubleVal = 1.0f;
436     ASSERT_EQ(OK, secondMetadata.update(tagId, &doubleVal, 1));
437     ASSERT_FALSE(secondMetadata.isEmpty());
438     ASSERT_TRUE(secondMetadata.exists(tagId));
439 
440     // Check whether CameraMetadata copying works as expected
441     CameraMetadata metadataCopy(metadata);
442     ASSERT_FALSE(metadataCopy.isEmpty());
443     ASSERT_TRUE(metadataCopy.exists(tagId));
444     ASSERT_EQ(OK, metadataCopy.update(tagId, &byteVal, 1));
445     ASSERT_TRUE(metadataCopy.exists(tagId));
446 
447     // Check whether values are as expected
448     camera_metadata_entry_t entry = metadata.find(tagId);
449     ASSERT_EQ(1u, entry.count);
450     ASSERT_EQ(byteVal, entry.data.u8[0]);
451     entry = secondMetadata.find(tagId);
452     ASSERT_EQ(1u, entry.count);
453     ASSERT_EQ(doubleVal, entry.data.d[0]);
454 
455     // Swap and erase
456     secondMetadata.swap(metadataCopy);
457     ASSERT_TRUE(metadataCopy.exists(tagId));
458     ASSERT_TRUE(secondMetadata.exists(tagId));
459     ASSERT_EQ(OK, secondMetadata.erase(tagId));
460     ASSERT_TRUE(secondMetadata.isEmpty());
461     doubleVal = 0.0f;
462     ASSERT_EQ(OK, metadataCopy.update(tagId, &doubleVal, 1));
463     entry = metadataCopy.find(tagId);
464     ASSERT_EQ(1u, entry.count);
465     ASSERT_EQ(doubleVal, entry.data.d[0]);
466 
467     // Append
468     uint8_t sceneMode = ANDROID_CONTROL_SCENE_MODE_ACTION;
469     secondMetadata.update(ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1);
470     // Append from two different vendor tag providers is not supported!
471     ASSERT_NE(OK, metadataCopy.append(secondMetadata));
472     ASSERT_EQ(OK, metadataCopy.erase(tagId));
473     metadataCopy.update(ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1);
474     // However appending from same vendor tag provider should be fine
475     ASSERT_EQ(OK, metadata.append(secondMetadata));
476     // Append from a metadata without vendor tag provider should be supported
477     CameraMetadata regularMetadata(10, 20);
478     uint8_t controlMode = ANDROID_CONTROL_MODE_AUTO;
479     regularMetadata.update(ANDROID_CONTROL_MODE, &controlMode, 1);
480     ASSERT_EQ(OK, secondMetadata.append(regularMetadata));
481     ASSERT_EQ(2u, secondMetadata.entryCount());
482     ASSERT_EQ(2u, metadata.entryCount());
483 
484     // Dump
485     metadata.dump(1, 2);
486     metadataCopy.dump(1, 2);
487     secondMetadata.dump(1, 2);
488 }
489 
TEST(CameraProviderManagerTest,NotifyStateChangeTest)490 TEST(CameraProviderManagerTest, NotifyStateChangeTest) {
491     std::vector<hardware::hidl_string> deviceNames {
492         "device@3.2/test/0",
493         "device@1.0/test/0",
494         "device@3.2/test/1"};
495 
496     hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
497     status_t res;
498     sp<CameraProviderManager> providerManager = new CameraProviderManager();
499     sp<TestStatusListener> statusListener = new TestStatusListener();
500     TestInteractionProxy serviceProxy;
501     sp<TestICameraProvider> provider =  new TestICameraProvider(deviceNames,
502             vendorSection);
503     serviceProxy.setProvider(provider);
504 
505     res = providerManager->initialize(statusListener, &serviceProxy);
506     ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
507 
508     ASSERT_EQ(provider->mCurrentState,
509             static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::NORMAL))
510             << "Initial device state not set";
511 
512     res = providerManager->notifyDeviceStateChange(
513         static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::FOLDED));
514 
515     ASSERT_EQ(res, OK) << "Unable to call notifyDeviceStateChange";
516     ASSERT_EQ(provider->mCurrentState,
517             static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::FOLDED))
518             << "Unable to change device state";
519 
520 }
521 
522 // Test that CameraProviderManager doesn't get stuck when the camera HAL isn't really working
TEST(CameraProviderManagerTest,BadHalStartupTest)523 TEST(CameraProviderManagerTest, BadHalStartupTest) {
524 
525     std::vector<hardware::hidl_string> deviceNames;
526     deviceNames.push_back("device@3.2/test/0");
527     deviceNames.push_back("device@1.0/test/0");
528     deviceNames.push_back("device@3.2/test/1");
529     hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
530     status_t res;
531 
532     sp<CameraProviderManager> providerManager = new CameraProviderManager();
533     sp<TestStatusListener> statusListener = new TestStatusListener();
534     TestInteractionProxy serviceProxy;
535     sp<TestICameraProvider> provider =  new TestICameraProvider(deviceNames,
536             vendorSection);
537 
538     // Not setting up provider in the service proxy yet, to test cases where a
539     // HAL isn't starting right
540     res = providerManager->initialize(statusListener, &serviceProxy);
541     ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
542 
543     // Now set up provider and trigger a registration
544     serviceProxy.setProvider(provider);
545     int numProviders = static_cast<int>(serviceProxy.listServices().size());
546 
547     hardware::hidl_string testProviderFqInterfaceName =
548             "android.hardware.camera.provider@2.4::ICameraProvider";
549     hardware::hidl_string testProviderInstanceName = "test/0";
550     serviceProxy.mManagerNotificationInterface->onRegistration(
551             testProviderFqInterfaceName,
552             testProviderInstanceName, false);
553 
554     // Check that new provider is called once for all the init methods
555     EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::SET_CALLBACK], numProviders) <<
556             "Only one call to setCallback per provider expected during register";
557     EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_VENDOR_TAGS], numProviders) <<
558             "Only one call to getVendorTags per provider expected during register";
559     EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::IS_SET_TORCH_MODE_SUPPORTED],
560             numProviders) <<
561             "Only one call to isSetTorchModeSupported per provider expected during init";
562     EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_CAMERA_ID_LIST], numProviders) <<
563             "Only one call to getCameraIdList per provider expected during init";
564     EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::NOTIFY_DEVICE_STATE], numProviders) <<
565             "Only one call to notifyDeviceState per provider expected during init";
566 
567     ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) <<
568             "Incorrect instance requested from service manager";
569 }
570