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 16import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, Level } from '@ohos/hypium'; 17import { camera } from '@kit.CameraKit'; 18import { common } from '@kit.AbilityKit'; 19import { cameraErrorCode, isEmpty, sceneMode } from '../common'; 20import { image } from '@kit.ImageKit'; 21import { indexSurfaceId } from '../../testability/pages/Index'; 22 23const TAG = "CameraXts.createPreviewOutputTest"; 24const abnormalIndex = -1; 25const abnormalSurfaceId = 'error_value'; 26let mPhotoSurfaceId: string; 27let mCameraManager: camera.CameraManager; 28let mCameraDeviceArray: Array<camera.CameraDevice>; 29let mCameraOutputCap: camera.CameraOutputCapability; 30let mPreviewProfile: camera.Profile; 31let testContext: common.UIAbilityContext = AppStorage.get('testContext') as common.UIAbilityContext; 32 33function getCameraManager() { 34 console.info(TAG, 'getCameraManager.'); 35 mCameraManager = camera.getCameraManager(testContext); 36 if (isEmpty(mCameraManager)) { 37 return false; 38 } 39 return true; 40} 41 42function getSupportedCameraDeviceArray() { 43 console.info(TAG, 'getSupportedCameraDeviceArray.'); 44 mCameraDeviceArray = mCameraManager.getSupportedCameras(); 45 if (isEmpty(mCameraDeviceArray)) { 46 return false; 47 } 48 console.info(TAG, 'getSupportedCameraDeviceArray length: ' + mCameraDeviceArray.length); 49 return true; 50} 51 52async function getSurfaceId() { 53 console.info(TAG, 'getSurfaceId begin.'); 54 let size: image.Size = { 55 height: 480, 56 width: 640 57 }; 58 let receiver = image.createImageReceiver(size, image.ImageFormat.JPEG, 8); 59 if (receiver !== undefined) { 60 console.info(TAG, 'receiver created successfully.'); 61 mPhotoSurfaceId = await receiver.getReceivingSurfaceId(); 62 console.info(TAG, 'receiver id: ' + mPhotoSurfaceId); 63 } else { 64 console.info(TAG, 'receiver is failed.'); 65 } 66} 67 68function getOutputCapability() { 69 console.info(TAG, 'getOutputCapability.'); 70 mCameraOutputCap = mCameraManager.getSupportedOutputCapability(mCameraDeviceArray[0], sceneMode.NORMAL_PHOTO); 71 console.info(TAG, 'camera, output cap: ' + JSON.stringify(mCameraOutputCap)); 72 mPreviewProfile = mCameraOutputCap.previewProfiles[0]; 73 expect(isEmpty(mPreviewProfile)).assertFalse(); 74} 75 76function createPreviewOutputWithProfile(done: Function, testName: string, surfaceId: string) { 77 console.info(TAG, testName + ' begin.'); 78 try { 79 if (isEmpty(mCameraManager) && isEmpty(mCameraDeviceArray)) { 80 console.info(TAG, testName + ' cameraManager is null.'); 81 expect().assertFail(); 82 } else { 83 for (let i = 0; i < mCameraDeviceArray.length; i++) { 84 let sceneModes = mCameraManager.getSupportedSceneModes(mCameraDeviceArray[i]); 85 if (sceneModes !== null && sceneModes.length > 0) { 86 for (const mode of sceneModes) { 87 let cameraOutputCapability = mCameraManager.getSupportedOutputCapability(mCameraDeviceArray[i], mode); 88 expect(isEmpty(cameraOutputCapability)).assertFalse(); 89 console.info(TAG, testName + ' camera : ' + i + ', mode: ' + mode + ', output cap: ' + 90 JSON.stringify(cameraOutputCapability)); 91 console.info(TAG, 92 testName + ' profiles: ' + cameraOutputCapability.previewProfiles[i] + ', surfaceId: ' + surfaceId); 93 let previewOutput = 94 mCameraManager.createPreviewOutput(cameraOutputCapability.previewProfiles[i], surfaceId); 95 console.info(TAG, testName + ' previewOutput: ' + JSON.stringify(previewOutput)); 96 expect(isEmpty(previewOutput)).assertFalse(); 97 } 98 } 99 } 100 } 101 done(); 102 } catch (error) { 103 console.error(TAG, testName + ' failed. err: ' + error.code + ', msg: ' + error.message); 104 expect(error.code == cameraErrorCode.INVALID_ARGUMENT).assertTrue(); 105 done(); 106 } 107} 108 109function createPreviewOutputAbnormal(done: Function, testName: string, previewProfile: camera.Profile, 110 surfaceId: string | null | undefined) { 111 console.info(TAG, testName + ' begin.'); 112 try { 113 if (isEmpty(mCameraManager) && isEmpty(mCameraDeviceArray)) { 114 console.info(TAG, testName + ' cameraManager is null.'); 115 expect().assertFail(); 116 } else { 117 let previewOutput = mCameraManager.createPreviewOutput(previewProfile, surfaceId as string); 118 console.info(TAG, testName + ' previewOutput: ' + JSON.stringify(previewOutput)); 119 expect(isEmpty(previewOutput)).assertTrue(); 120 } 121 done(); 122 } catch (error) { 123 console.error(TAG, testName + ' failed. err: ' + error.code + ', msg: ' + error.message); 124 expect(error.code == cameraErrorCode.INVALID_ARGUMENT).assertTrue(); 125 done(); 126 } 127} 128 129function createPreviewOutputNoProfile(done: Function, testName: string, surfaceId: string) { 130 console.info(TAG, testName + ' begin.'); 131 try { 132 if (isEmpty(mCameraManager)) { 133 console.info(TAG, testName + ' cameraManager is null.'); 134 expect().assertFail(); 135 } else { 136 let previewOutput = mCameraManager.createPreviewOutput(surfaceId as string); 137 console.info(TAG, testName + ' previewOutput: ' + JSON.stringify(previewOutput)); 138 expect(isEmpty(previewOutput)).assertFalse(); 139 } 140 done(); 141 } catch (error) { 142 console.error(TAG, testName + ' failed. err: ' + error.code + ', msg: ' + error.message); 143 expect(error.code == cameraErrorCode.INVALID_ARGUMENT).assertTrue(); 144 done(); 145 } 146} 147 148function createPreviewOutputNoProfileAbnormal(done: Function, testName: string, surfaceId: string | null | undefined) { 149 console.info(TAG, testName + ' begin.'); 150 try { 151 if (isEmpty(mCameraManager)) { 152 console.info(TAG, testName + ' cameraManager is null.'); 153 expect().assertFail(); 154 } else { 155 let previewOutput = mCameraManager.createPreviewOutput(surfaceId as string); 156 console.info(TAG, testName + ' previewOutput: ' + JSON.stringify(previewOutput)); 157 expect(isEmpty(previewOutput)).assertTrue(); 158 } 159 done(); 160 } catch (error) { 161 console.error(TAG, testName + ' failed. err: ' + error.code + ', msg: ' + error.message); 162 expect(error.code == cameraErrorCode.INVALID_ARGUMENT).assertTrue(); 163 done(); 164 } 165} 166 167export default function createPreviewOutputTest() { 168 describe('createPreviewOutputTest', () => { 169 beforeAll(async () => { 170 console.info(TAG, 'beforeAll case.'); 171 getCameraManager(); 172 getSupportedCameraDeviceArray(); 173 getOutputCapability(); 174 await getSurfaceId(); 175 }); 176 177 beforeEach(() => { 178 console.info(TAG, 'beforeEach case.'); 179 }); 180 181 afterEach(() => { 182 console.info(TAG, 'afterEach case.'); 183 }); 184 185 afterAll(() => { 186 console.info(TAG, 'afterAll case.'); 187 mPhotoSurfaceId = ''; 188 }); 189 190 /** 191 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_001 192 * @tc.name : createPreviewOutput_with_profile_001 193 * @tc.desc : No abnormal scenarios-with previewProfiles, image surfaceId 194 * @tc.size : MEDIUM 195 * @tc.type : Function 196 * @tc.level : Level 0 197 */ 198 it('createPreviewOutput_with_profile_001', Level.LEVEL0, async (done: Function) => { 199 const testName = 'createPreviewOutput_with_profile_001'; 200 createPreviewOutputWithProfile(done, testName, mPhotoSurfaceId); 201 }) 202 203 /** 204 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_002 205 * @tc.name : createPreviewOutput_with_profile_002 206 * @tc.desc : No abnormal scenarios-with previewProfiles, Index surfaceId 207 * @tc.size : MEDIUM 208 * @tc.type : Function 209 * @tc.level : Level 0 210 */ 211 it('createPreviewOutput_with_profile_002', Level.LEVEL0, async (done: Function) => { 212 const testName = 'createPreviewOutput_with_profile_002'; 213 createPreviewOutputWithProfile(done, testName, indexSurfaceId); 214 }) 215 216 /** 217 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_003 218 * @tc.name : createPreviewOutput_with_profile_abnormal_001 219 * @tc.desc : surfaceId->undefined -> error_code: 7400101 220 * @tc.size : MEDIUM 221 * @tc.type : Function 222 * @tc.level : Level 2 223 */ 224 it('createPreviewOutput_with_profile_abnormal_001', Level.LEVEL2, async (done: Function) => { 225 const testName = 'createPreviewOutput_with_profile_abnormal_001'; 226 createPreviewOutputAbnormal(done, testName, mPreviewProfile, undefined); 227 }) 228 229 /** 230 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_004 231 * @tc.name : createPreviewOutput_with_profile_abnormal_002 232 * @tc.desc : surfaceId->null -> error_code: 7400101 233 * @tc.size : MEDIUM 234 * @tc.type : Function 235 * @tc.level : Level 2 236 */ 237 it('createPreviewOutput_with_profile_abnormal_002', Level.LEVEL2, async (done: Function) => { 238 const testName = 'createPreviewOutput_with_profile_abnormal_002'; 239 createPreviewOutputAbnormal(done, testName, mPreviewProfile, null); 240 }) 241 242 /** 243 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_005 244 * @tc.name : createPreviewOutput_with_profile_abnormal_003 245 * @tc.desc : abnormal surfaceId -> previewOutput->null 246 * @tc.size : MEDIUM 247 * @tc.type : Function 248 * @tc.level : Level 2 249 */ 250 it('createPreviewOutput_with_profile_abnormal_003', Level.LEVEL2, async (done: Function) => { 251 const testName = 'createPreviewOutput_with_profile_abnormal_003'; 252 createPreviewOutputAbnormal(done, testName, mPreviewProfile, abnormalSurfaceId); 253 }) 254 255 /** 256 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_006 257 * @tc.name : createPreviewOutput_with_profile_abnormal_004 258 * @tc.desc : new previewProfile, with image surfaceId -> error_code: 7400101 259 * @tc.size : MEDIUM 260 * @tc.type : Function 261 * @tc.level : Level 2 262 */ 263 it('createPreviewOutput_with_profile_abnormal_004', Level.LEVEL2, async (done: Function) => { 264 const testName = 'createPreviewOutput_with_profile_abnormal_004'; 265 let newProfile: Array<camera.Profile> = new Array<camera.Profile>(); 266 createPreviewOutputAbnormal(done, testName, newProfile[0], mPhotoSurfaceId); 267 }) 268 269 /** 270 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_007 271 * @tc.name : createPreviewOutput_with_profile_abnormal_005 272 * @tc.desc : new previewProfile, with Index surfaceId -> error_code: 7400101 273 * @tc.size : MEDIUM 274 * @tc.type : Function 275 * @tc.level : Level 2 276 */ 277 it('createPreviewOutput_with_profile_abnormal_005', Level.LEVEL2, async (done: Function) => { 278 const testName = 'createPreviewOutput_with_profile_abnormal_005'; 279 let newProfile: Array<camera.Profile> = new Array<camera.Profile>(); 280 createPreviewOutputAbnormal(done, testName, newProfile[0], indexSurfaceId); 281 }) 282 283 /** 284 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_008 285 * @tc.name : createPreviewOutput_with_profile_abnormal_006 286 * @tc.desc : abnormal previewProfile, image surfaceId -> error_code: 7400101 287 * @tc.size : MEDIUM 288 * @tc.type : Function 289 * @tc.level : Level 2 290 */ 291 it('createPreviewOutput_with_profile_abnormal_006', Level.LEVEL2, async (done: Function) => { 292 const testName = 'createPreviewOutput_with_profile_abnormal_006'; 293 createPreviewOutputAbnormal(done, testName, mCameraOutputCap.previewProfiles[abnormalIndex], mPhotoSurfaceId); 294 }) 295 296 /** 297 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_009 298 * @tc.name : createPreviewOutput_with_profile_abnormal_007 299 * @tc.desc : abnormal previewProfile, image surfaceId -> error_code: 7400101 300 * @tc.size : MEDIUM 301 * @tc.type : Function 302 * @tc.level : Level 2 303 */ 304 it('createPreviewOutput_with_profile_abnormal_007', Level.LEVEL2, async (done: Function) => { 305 const testName = 'createPreviewOutput_with_profile_abnormal_007'; 306 createPreviewOutputAbnormal(done, testName, mCameraOutputCap.previewProfiles[abnormalIndex], indexSurfaceId); 307 }) 308 309 /** 310 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_010 311 * @tc.name : createPreviewOutput_001 312 * @tc.desc : No abnormal scenarios-no previewProfiles, only image surfaceId 313 * @tc.size : MEDIUM 314 * @tc.type : Function 315 * @tc.level : Level 0 316 */ 317 it('createPreviewOutput_001', Level.LEVEL0, async (done: Function) => { 318 const testName = 'createPreviewOutput_001'; 319 createPreviewOutputNoProfile(done, testName, mPhotoSurfaceId); 320 }) 321 322 /** 323 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_011 324 * @tc.name : createPreviewOutput_002 325 * @tc.desc : No abnormal scenarios-no previewProfiles, only Index surfaceId 326 * @tc.size : MEDIUM 327 * @tc.type : Function 328 * @tc.level : Level 0 329 */ 330 it('createPreviewOutput_002', Level.LEVEL0, async (done: Function) => { 331 const testName = 'createPreviewOutput_002'; 332 createPreviewOutputNoProfile(done, testName, indexSurfaceId); 333 }) 334 335 /** 336 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_012 337 * @tc.name : createPreviewOutput_abnormal_001 338 * @tc.desc : only surfaceId->null -> error_code: 7400101 339 * @tc.size : MEDIUM 340 * @tc.type : Function 341 * @tc.level : Level 2 342 */ 343 it('createPreviewOutput_abnormal_001', Level.LEVEL2, async (done: Function) => { 344 const testName = 'createPreviewOutput_abnormal_001'; 345 createPreviewOutputNoProfileAbnormal(done, testName, null); 346 }) 347 348 /** 349 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_013 350 * @tc.name : createPreviewOutput_abnormal_002 351 * @tc.desc : only surfaceId->undefined -> error_code: 7400101 352 * @tc.size : MEDIUM 353 * @tc.type : Function 354 * @tc.level : Level 2 355 */ 356 it('createPreviewOutput_abnormal_002', Level.LEVEL2, async (done: Function) => { 357 const testName = 'createPreviewOutput_abnormal_002'; 358 createPreviewOutputNoProfileAbnormal(done, testName, undefined); 359 }) 360 361 /** 362 * @tc.number : CAMERA_MANAGER_CREATE_PREVIEW_OUTPUT_014 363 * @tc.name : createPreviewOutput_abnormal_003 364 * @tc.desc : only abnormal surfaceId -> previewOutput->undefined 365 * @tc.size : MEDIUM 366 * @tc.type : Function 367 * @tc.level : Level 2 368 */ 369 it('createPreviewOutput_abnormal_003', Level.LEVEL2, async (done: Function) => { 370 const testName = 'createPreviewOutput_abnormal_003'; 371 createPreviewOutputNoProfileAbnormal(done, testName, abnormalSurfaceId); 372 }) 373 }) 374}