Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "CameraCharacteristicsPermission" |
| 19 | |
| 20 | #include <gtest/gtest.h> |
| 21 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 22 | #include <android/content/AttributionSourceState.h> |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 23 | #include <binder/ProcessState.h> |
| 24 | #include <utils/Errors.h> |
| 25 | #include <utils/Log.h> |
| 26 | #include <camera/CameraMetadata.h> |
| 27 | #include <camera/Camera.h> |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 28 | #include <camera/CameraUtils.h> |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 29 | #include <android/hardware/ICameraService.h> |
| 30 | |
| 31 | using namespace android; |
| 32 | using namespace android::hardware; |
| 33 | |
| 34 | class CameraCharacteristicsPermission : public ::testing::Test { |
| 35 | protected: |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 36 | CameraCharacteristicsPermission() : numCameras(0){} |
| 37 | //Gtest interface |
| 38 | void SetUp() override; |
| 39 | void TearDown() override; |
| 40 | |
| 41 | int32_t numCameras; |
| 42 | sp<ICameraService> mCameraService; |
| 43 | }; |
| 44 | |
| 45 | void CameraCharacteristicsPermission::SetUp() { |
| 46 | ::android::binder::Status rc; |
| 47 | ProcessState::self()->startThreadPool(); |
| 48 | sp<IServiceManager> sm = defaultServiceManager(); |
| 49 | sp<IBinder> binder = sm->getService(String16("media.camera")); |
| 50 | mCameraService = interface_cast<ICameraService>(binder); |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 51 | AttributionSourceState clientAttribution; |
| 52 | clientAttribution.deviceId = kDefaultDeviceId; |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 53 | rc = mCameraService->getNumberOfCameras( |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 54 | hardware::ICameraService::CAMERA_TYPE_ALL, clientAttribution, /*devicePolicy*/0, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 55 | &numCameras); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 56 | EXPECT_TRUE(rc.isOk()); |
| 57 | } |
| 58 | |
| 59 | void CameraCharacteristicsPermission::TearDown() { |
| 60 | mCameraService.clear(); |
| 61 | } |
| 62 | |
| 63 | // Revoking and acquiring permissions automatically might not be possible. |
| 64 | // Test the functionality for removal of camera characteristics needing |
| 65 | // a camera permission. |
| 66 | TEST_F(CameraCharacteristicsPermission, TestCameraPermission) { |
| 67 | for (int32_t cameraId = 0; cameraId < numCameras; cameraId++) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 68 | std::string cameraIdStr = std::to_string(cameraId); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 69 | bool isSupported = false; |
| 70 | auto rc = mCameraService->supportsCameraApi(cameraIdStr, |
| 71 | hardware::ICameraService::API_VERSION_2, &isSupported); |
| 72 | EXPECT_TRUE(rc.isOk()); |
| 73 | if (!isSupported) { |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | CameraMetadata metadata; |
| 78 | std::vector<int32_t> tagsNeedingPermission; |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 79 | AttributionSourceState clientAttribution; |
| 80 | clientAttribution.deviceId = kDefaultDeviceId; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 81 | rc = mCameraService->getCameraCharacteristics(cameraIdStr, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 82 | /*targetSdkVersion*/__ANDROID_API_FUTURE__, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 83 | /*overrideToPortrait*/false, clientAttribution, /*devicePolicy*/0, &metadata); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 84 | ASSERT_TRUE(rc.isOk()); |
| 85 | EXPECT_FALSE(metadata.isEmpty()); |
| 86 | EXPECT_EQ(metadata.removePermissionEntries(CAMERA_METADATA_INVALID_VENDOR_ID, |
| 87 | &tagsNeedingPermission), NO_ERROR); |
| 88 | camera_metadata_entry_t availableCharacteristics = |
| 89 | metadata.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS); |
| 90 | EXPECT_TRUE(0 < availableCharacteristics.count); |
| 91 | |
| 92 | std::vector<uint32_t> availableKeys; |
| 93 | availableKeys.reserve(availableCharacteristics.count); |
| 94 | availableKeys.insert(availableKeys.begin(), availableCharacteristics.data.i32, |
| 95 | availableCharacteristics.data.i32 + availableCharacteristics.count); |
| 96 | |
| 97 | for (const auto &key : tagsNeedingPermission) { |
| 98 | ASSERT_FALSE(metadata.exists(key)); |
| 99 | auto it = std::find(availableKeys.begin(), availableKeys.end(), key); |
| 100 | ASSERT_TRUE(it == availableKeys.end()); |
| 101 | } |
| 102 | } |
| 103 | } |