1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CAMERA_SESSION_MODULETEST_H 17 #define CAMERA_SESSION_MODULETEST_H 18 19 #include "gtest/gtest.h" 20 21 #include "input/camera_device.h" 22 #include "input/camera_input.h" 23 #include "input/camera_manager.h" 24 #include "output/camera_output_capability.h" 25 #include "session/aperture_video_session.h" 26 #include "session/capture_scene_const.h" 27 #include "session/capture_session.h" 28 #include "session/fluorescence_photo_session.h" 29 #include "session/high_res_photo_session.h" 30 #include "session/light_painting_session.h" 31 #include "session/macro_photo_session.h" 32 #include "session/macro_video_session.h" 33 #include "session/night_session.h" 34 #include "session/panorama_session.h" 35 #include "session/photo_session.h" 36 #include "session/portrait_session.h" 37 #include "session/profession_session.h" 38 #include "session/quick_shot_photo_session.h" 39 #include "session/scan_session.h" 40 #include "session/secure_camera_session.h" 41 #include "session/slow_motion_session.h" 42 #include "session/time_lapse_photo_session.h" 43 #include "session/video_session.h" 44 45 #include "accesstoken_kit.h" 46 #include "camera_error_code.h" 47 #include "camera_log.h" 48 #include "camera_metadata_operator.h" 49 #include "camera_util.h" 50 #include "hap_token_info.h" 51 #include "nativetoken_kit.h" 52 #include "parameter.h" 53 #include "surface.h" 54 #include "system_ability_definition.h" 55 #include "test_common.h" 56 #include "token_setproc.h" 57 58 namespace OHOS { 59 namespace CameraStandard { 60 61 typedef struct { 62 Profile preview; 63 Profile photo; 64 VideoProfile video; 65 } SelectProfiles; 66 67 enum class CAM_PHOTO_EVENTS { 68 CAM_PHOTO_CAPTURE_START = 0, 69 CAM_PHOTO_CAPTURE_END, 70 CAM_PHOTO_CAPTURE_ERR, 71 CAM_PHOTO_FRAME_SHUTTER, 72 CAM_PHOTO_MAX_EVENT, 73 CAM_PHOTO_FRAME_SHUTTER_END, 74 CAM_PHOTO_CAPTURE_READY, 75 CAM_PHOTO_ESTIMATED_CAPTURE_DURATION, 76 CAM_PHOTO_OFFLINE_PHOTOOUTPUT 77 }; 78 79 enum class CAM_PREVIEW_EVENTS { 80 CAM_PREVIEW_FRAME_START = 0, 81 CAM_PREVIEW_FRAME_END, 82 CAM_PREVIEW_FRAME_ERR, 83 CAM_PREVIEW_SKETCH_STATUS_CHANGED, 84 CAM_PREVIEW_MAX_EVENT 85 }; 86 87 enum class CAM_VIDEO_EVENTS { 88 CAM_VIDEO_FRAME_START = 0, 89 CAM_VIDEO_FRAME_END, 90 CAM_VIDEO_FRAME_ERR, 91 CAM_VIDEO_MAX_EVENT 92 }; 93 94 enum class CAM_MACRO_DETECT_EVENTS { 95 CAM_MACRO_EVENT_IDLE = 0, 96 CAM_MACRO_EVENT_ACTIVE, 97 CAM_MACRO_EVENT_MAX_EVENT 98 }; 99 100 enum class CAM_MOON_CAPTURE_BOOST_EVENTS { 101 CAM_MOON_CAPTURE_BOOST_EVENT_IDLE = 0, 102 CAM_MOON_CAPTURE_BOOST_EVENT_ACTIVE, 103 CAM_MOON_CAPTURE_BOOST_EVENT_MAX_EVENT 104 }; 105 106 const int32_t WAIT_TIME_CALLBACK = 1; 107 const int32_t WAIT_TIME_AFTER_CLOSE = 1; 108 const int32_t WAIT_TIME_BEFORE_STOP = 1; 109 const int32_t WAIT_TIME_AFTER_CAPTURE = 1; 110 const int32_t WAIT_TIME_AFTER_START = 2; 111 const int32_t WAIT_TIME_AFTER_SET_AREA = 2; 112 const int32_t WAIT_TIME_AFTER_ATTACH_SKETCH = 2; 113 const int32_t WAIT_TIME_AFTER_DEFERRED_CAPTURE = 2; 114 const int32_t WAIT_TIME_AFTER_RECORDING = 3; 115 116 const int32_t PREVIEW_DEFAULT_WIDTH = 1280; 117 const int32_t PREVIEW_DEFAULT_HEIGHT = 720; 118 const int32_t PHOTO_DEFAULT_WIDTH = 1280; 119 const int32_t PHOTO_DEFAULT_HEIGHT = 960; 120 const int32_t VIDEO_DEFAULT_WIDTH = 1280; 121 const int32_t VIDEO_DEFAULT_HEIGHT = 720; 122 const int32_t SKETCH_DEFAULT_WIDTH = 1280; 123 const int32_t SKETCH_DEFAULT_HEIGHT = 720; 124 const int32_t SKETCH_PREVIEW_MAX_WIDTH = 3000; 125 const int32_t SKETCH_PREVIEW_MIN_HEIGHT = 720; 126 const int32_t PRVIEW_WIDTH_176 = 176; 127 const int32_t PRVIEW_WIDTH_480 = 480; 128 const int32_t PRVIEW_WIDTH_640 = 640; 129 const int32_t PRVIEW_WIDTH_4096 = 4096; 130 const int32_t PRVIEW_WIDTH_4160 = 4160; 131 const int32_t PRVIEW_WIDTH_8192 = 8192; 132 const int32_t PRVIEW_HEIGHT_144 = 144; 133 const int32_t PRVIEW_HEIGHT_480 = 480; 134 const int32_t PRVIEW_HEIGHT_640 = 640; 135 const int32_t PRVIEW_HEIGHT_3072 = 3072; 136 const int32_t PRVIEW_HEIGHT_3120 = 3120; 137 const int32_t PRVIEW_HEIGHT_6144 = 6144; 138 139 const int32_t META_VALUE_10 = 10; 140 const int32_t META_VALUE_100 = 100; 141 const int32_t DEFAULT_AE_VALUE = 1; 142 const int32_t DEFAULT_ISO_VALUE = 1; 143 const int32_t DEFAULT_APERTURE_VALUE = 1; 144 const int32_t DEFAULT_NUMERATOR_VALUE = 1; 145 const int32_t DEFAULT_MIN_FRAME_RATE = 15; 146 const int32_t DEFAULT_MAX_FRAME_RATE = 30; 147 const int32_t DEFAULT_LUMINATION_VALUE = 256; 148 const int32_t DEFAULT_DENOMINATOR_VALUE = 1000000; 149 150 const int32_t MIN_FRAME_RATE = 15; 151 const int32_t MAX_FRAME_RATE = 30; 152 const int32_t CAMERA_NUMBER = 2; 153 const int32_t ZOOM_RATIO = 100; 154 155 extern std::bitset<static_cast<int>(CAM_PHOTO_EVENTS::CAM_PHOTO_MAX_EVENT)> g_photoEvents; 156 extern std::bitset<static_cast<unsigned int>(CAM_PREVIEW_EVENTS::CAM_PREVIEW_MAX_EVENT)> g_previewEvents; 157 extern std::bitset<static_cast<unsigned int>(CAM_VIDEO_EVENTS::CAM_VIDEO_MAX_EVENT)> g_videoEvents; 158 extern std::bitset<static_cast<unsigned int>(CAM_MACRO_DETECT_EVENTS::CAM_MACRO_EVENT_MAX_EVENT)> g_macroEvents; 159 extern std::bitset<static_cast<unsigned int>(CAM_MOON_CAPTURE_BOOST_EVENTS::CAM_MOON_CAPTURE_BOOST_EVENT_MAX_EVENT)> 160 g_moonCaptureBoostEvents; 161 extern std::unordered_map<std::string, int> g_camStatusMap; 162 extern std::unordered_map<std::string, bool> g_camFlashMap; 163 extern std::shared_ptr<OHOS::Camera::CameraMetadata> g_metaResult; 164 extern std::list<int32_t> g_sketchStatus; 165 extern TorchStatusInfo g_torchInfo; 166 extern int32_t g_previewFd; 167 extern int32_t g_photoFd; 168 extern int32_t g_videoFd; 169 extern int32_t g_sketchFd; 170 extern bool g_camInputOnError; 171 extern bool g_sessionclosed; 172 extern bool g_brightnessStatusChanged; 173 extern bool g_slowMotionStatusChanged; 174 175 class AppCallback : public CameraManagerCallback, 176 public TorchListener, 177 public ErrorCallback, 178 public PhotoStateCallback, 179 public PreviewStateCallback, 180 public ResultCallback, 181 public SlowMotionStateCallback, 182 public MacroStatusCallback, 183 public FeatureDetectionStatusCallback, 184 public FoldListener, 185 public LcdFlashStatusCallback, 186 public BrightnessStatusCallback { 187 public: 188 void OnCameraStatusChanged(const CameraStatusInfo& cameraDeviceInfo) const override; 189 190 void OnFlashlightStatusChanged(const std::string& cameraID, const FlashStatus flashStatus) const override; 191 192 void OnTorchStatusChange(const TorchStatusInfo &torchStatusInfo) const override; 193 194 void OnError(const int32_t errorType, const int32_t errorMsg) const override; 195 196 void OnResult(const uint64_t timestamp, const std::shared_ptr<OHOS::Camera::CameraMetadata>& result) const override; 197 198 void OnCaptureStarted(const int32_t captureId) const override; 199 200 void OnCaptureStarted(const int32_t captureId, uint32_t exposureTime) const override; 201 202 void OnCaptureEnded(const int32_t captureId, const int32_t frameCount) const override; 203 204 void OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const override; 205 206 void OnFrameShutterEnd(const int32_t captureId, const uint64_t timestamp) const override; 207 208 void OnCaptureReady(const int32_t captureId, const uint64_t timestamp) const override; 209 210 void OnEstimatedCaptureDuration(const int32_t duration) const override; 211 212 void OnCaptureError(const int32_t captureId, const int32_t errorCode) const override; 213 214 void OnFrameStarted() const override; 215 216 void OnFrameEnded(const int32_t frameCount) const override; 217 218 void OnError(const int32_t errorCode) const override; 219 220 void OnSketchStatusDataChanged(const SketchStatusData& statusData) const override; 221 222 void OnMacroStatusChanged(MacroStatus status) override; 223 224 void OnFeatureDetectionStatusChanged(SceneFeature feature, FeatureDetectionStatus status) override; 225 226 bool IsFeatureSubscribed(SceneFeature feature) override; 227 228 void OnBrightnessStatusChanged(bool state) override; 229 230 void OnSlowMotionState(const SlowMotionState state) override; 231 232 void OnFoldStatusChanged(const FoldStatusInfo &foldStatusInfo) const override; 233 234 void OnLcdFlashStatusChanged(LcdFlashStatusInfo lcdFlashStatusInfo) override; 235 236 void OnOfflineDeliveryFinished(const int32_t captureId) const override; 237 }; 238 239 class AppVideoCallback : public VideoStateCallback { 240 public: 241 void OnFrameStarted() const override; 242 243 void OnFrameEnded(const int32_t frameCount) const override; 244 245 void OnError(const int32_t errorCode) const override; 246 247 void OnDeferredVideoEnhancementInfo(const CaptureEndedInfoExt info) const override; 248 }; 249 250 class AppMetadataCallback : public MetadataObjectCallback, public MetadataStateCallback { 251 public: 252 void OnMetadataObjectsAvailable(std::vector<sptr<MetadataObject>> metaObjects) const; 253 254 void OnError(int32_t errorCode) const; 255 }; 256 257 class AppSessionCallback : public SessionCallback { 258 public: 259 void OnError(int32_t errorCode); 260 }; 261 262 263 class TestExposureInfoCallback : public ExposureInfoCallback { 264 public: OnExposureInfoChanged(ExposureInfo info)265 void OnExposureInfoChanged(ExposureInfo info) 266 { 267 MEDIA_INFO_LOG("OnExposureInfoChanged start"); 268 } 269 }; 270 271 class TestIsoInfoCallback : public IsoInfoCallback { 272 public: OnIsoInfoChanged(IsoInfo info)273 void OnIsoInfoChanged(IsoInfo info) 274 { 275 MEDIA_INFO_LOG("OnIsoInfoChanged start"); 276 } 277 }; 278 279 class TestApertureInfoCallback : public ApertureInfoCallback { 280 public: OnApertureInfoChanged(ApertureInfo info)281 void OnApertureInfoChanged(ApertureInfo info) 282 { 283 MEDIA_INFO_LOG("OnApertureInfoChanged start"); 284 } 285 }; 286 287 class TestLuminationInfoCallback : public LuminationInfoCallback { 288 public: OnLuminationInfoChanged(LuminationInfo info)289 void OnLuminationInfoChanged(LuminationInfo info) 290 { 291 MEDIA_INFO_LOG("OnLuminationInfoChanged start"); 292 } 293 }; 294 295 class CameraSessionModuleTest : public testing::Test { 296 public: 297 /* SetUpTestCase: The preset action of the test suite is executed before the first test case */ 298 static void SetUpTestCase(void); 299 300 /* TearDownTestCase: The cleanup action of the test suite is executed after the last test case */ 301 static void TearDownTestCase(void); 302 303 /* SetUp: Execute before each test case */ 304 void SetUp(); 305 306 /* TearDown: Execute after each test case */ 307 void TearDown(); 308 309 sptr<CaptureOutput> CreatePreviewOutput(int32_t width, int32_t height); 310 sptr<CaptureOutput> CreatePreviewOutput(Profile& profile); 311 sptr<CaptureOutput> CreatePreviewOutput(); 312 sptr<CaptureOutput> CreatePhotoOutput(int32_t width, int32_t height); 313 sptr<CaptureOutput> CreatePhotoOutput(Profile profile); 314 sptr<CaptureOutput> CreatePhotoOutput(); 315 sptr<CaptureOutput> CreateVideoOutput(int32_t width, int32_t height); 316 sptr<CaptureOutput> CreateVideoOutput(VideoProfile& profile); 317 sptr<CaptureOutput> CreateVideoOutput(); 318 sptr<Surface> CreateSketchSurface(CameraFormat cameraFormat); 319 std::shared_ptr<Profile> GetSketchPreviewProfile(); 320 Profile SelectProfileByRatioAndFormat(sptr<CameraOutputCapability>& modeAbility, 321 camera_rational_t ratio, CameraFormat format); 322 std::optional<Profile> GetPreviewProfileByFormat(sptr<CameraOutputCapability>& modeAbility, 323 uint32_t width, uint32_t height, CameraFormat previewFormat); 324 std::optional<VideoProfile> GetVideoProfileByFormat(sptr<CameraOutputCapability>& modeAbility, 325 uint32_t width, uint32_t height, CameraFormat videoFormat, uint32_t maxFps); 326 void GetSupportedOutputCapability(); 327 void ConfigScanSession(sptr<CaptureOutput> &previewOutput_1, sptr<CaptureOutput> &previewOutput_2); 328 void ConfigSlowMotionSession(sptr<CaptureOutput> &previewOutput, sptr<CaptureOutput> &videoOutput); 329 void ConfigVideoSession(sptr<CaptureOutput> &previewOutput, sptr<CaptureOutput> &videoOutput); 330 bool IsSupportNow(); 331 bool IsSceneModeSupported(SceneMode mode); 332 void FilterPreviewProfiles(std::vector<Profile> &previewProfiles); 333 void ProcessPreviewProfiles(sptr<CameraOutputCapability>& outputcapability); 334 void ProcessSize(); 335 void SetUpInit(); 336 337 CameraFormat previewFormat_; 338 CameraFormat photoFormat_; 339 CameraFormat videoFormat_; 340 int32_t previewWidth_; 341 int32_t previewHeight_; 342 int32_t photoWidth_; 343 int32_t photoHeight_; 344 int32_t videoWidth_; 345 int32_t videoHeight_; 346 sptr<CameraManager> manager_; 347 sptr<CaptureSession> session_; 348 sptr<CaptureSession> scanSession_; 349 sptr<SlowMotionSession> slowMotionSession_; 350 sptr<CaptureSession> videoSession_; 351 sptr<CaptureInput> input_; 352 std::vector<sptr<CameraDevice>> cameras_; 353 std::vector<CameraFormat> previewFormats_; 354 std::vector<CameraFormat> photoFormats_; 355 std::vector<CameraFormat> videoFormats_; 356 std::vector<Size> previewSizes_; 357 std::vector<Size> photoSizes_; 358 std::vector<Size> videoSizes_; 359 std::vector<int32_t> videoFrameRates_; 360 std::vector<Profile> previewProfiles_; 361 std::vector<Profile> photoProfiles_; 362 std::vector<VideoProfile> videoProfiles_; 363 364 private: 365 static void SetNativeToken(void); 366 }; 367 } // CameraStandard 368 } // OHOS 369 #endif // CAMERA_SESSION_MODULETEST_H 370