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 } from '../common'; 20 21const TAG = "CameraXts.createPhotoOutputTest"; 22const abnormalIndex = -1; 23let mCameraManager: camera.CameraManager; 24let mCameraDeviceArray: Array<camera.CameraDevice>; 25let testContext: common.UIAbilityContext = AppStorage.get('testContext') as common.UIAbilityContext; 26 27function getCameraManager() { 28 console.info(TAG, 'getCameraManager.'); 29 mCameraManager = camera.getCameraManager(testContext); 30 if (isEmpty(mCameraManager)) { 31 return false; 32 } 33 return true; 34} 35 36function getSupportedCameraDeviceArray() { 37 console.info(TAG, 'getSupportedCameraDeviceArray.'); 38 mCameraDeviceArray = mCameraManager.getSupportedCameras(); 39 if (isEmpty(mCameraDeviceArray)) { 40 return false; 41 } 42 console.info(TAG, 'getSupportedCameraDeviceArray length: ' + mCameraDeviceArray.length); 43 return true; 44} 45 46function createPhotoOutputAbnormal(done: Function, testName: string, profile: camera.Profile | null | undefined) { 47 console.info(TAG, testName + ' begin.'); 48 try { 49 if (isEmpty(mCameraManager)) { 50 console.info(TAG, testName + ' cameraManager is null.'); 51 expect().assertFail(); 52 } else { 53 let photoOutput = mCameraManager.createPhotoOutput(profile); 54 console.info(TAG, testName + ' photoOutput: ' + JSON.stringify(photoOutput)); 55 expect(isEmpty(photoOutput)).assertFalse(); 56 } 57 done(); 58 } catch (error) { 59 console.error(TAG, testName + ' failed. err: ' + error.code + ', msg: ' + error.message); 60 expect(error.code == cameraErrorCode.INVALID_ARGUMENT).assertTrue(); 61 done(); 62 } 63} 64 65export default function createPhotoOutputTest() { 66 describe('createPhotoOutputTest', () => { 67 beforeAll(async () => { 68 console.info(TAG, 'beforeAll case.'); 69 getCameraManager(); 70 getSupportedCameraDeviceArray(); 71 }); 72 73 beforeEach(() => { 74 console.info(TAG, 'beforeEach case.'); 75 }); 76 77 afterEach(() => { 78 console.info(TAG, 'afterEach case.'); 79 }); 80 81 afterAll(() => { 82 console.info(TAG, 'afterAll case.'); 83 }); 84 85 /** 86 * @tc.number : CAMERA_MANAGER_CREATE_PHOTO_OUTPUT_001 87 * @tc.name : createPhotoOutput_001 88 * @tc.desc : No abnormal scenarios 89 * @tc.size : MEDIUM 90 * @tc.type : Function 91 * @tc.level : Level 0 92 */ 93 it('createPhotoOutput_001', Level.LEVEL0, async (done: Function) => { 94 const testName = 'createPhotoOutput_001'; 95 console.info(TAG, testName + ' begin.'); 96 try { 97 if (isEmpty(mCameraManager) && isEmpty(mCameraDeviceArray)) { 98 console.info(TAG, testName + ' cameraManager is null.'); 99 expect().assertFail(); 100 } else { 101 for (let i = 0; i < mCameraDeviceArray.length; i++) { 102 let sceneModes = mCameraManager.getSupportedSceneModes(mCameraDeviceArray[i]); 103 if (sceneModes !== null && sceneModes.length > 0) { 104 for (const mode of sceneModes) { 105 let cameraOutputCapability = mCameraManager.getSupportedOutputCapability(mCameraDeviceArray[i], mode); 106 expect(isEmpty(cameraOutputCapability)).assertFalse(); 107 console.info(TAG, testName + ' camera : ' + i + ', mode: ' + mode + ', output cap: ' + 108 JSON.stringify(cameraOutputCapability)); 109 let photoOutput = 110 mCameraManager.createPhotoOutput(cameraOutputCapability.photoProfiles[i]); 111 console.info(TAG, testName + ' photoOutput: ' + JSON.stringify(photoOutput)); 112 expect(isEmpty(photoOutput)).assertFalse(); 113 } 114 } 115 } 116 } 117 done(); 118 } catch (error) { 119 console.error(TAG, testName + ' failed. err: ' + error.code + ', msg: ' + error.message); 120 expect(error.code == cameraErrorCode.INVALID_ARGUMENT).assertTrue(); 121 done(); 122 } 123 }) 124 125 /** 126 * @tc.number : CAMERA_MANAGER_CREATE_PHOTO_OUTPUT_002 127 * @tc.name : createPhotoOutput_002 128 * @tc.desc : No abnormal scenarios-no photoProfile 129 * @tc.size : MEDIUM 130 * @tc.type : Function 131 * @tc.level : Level 0 132 */ 133 it('createPhotoOutput_002', Level.LEVEL0, async (done: Function) => { 134 const testName = 'createPhotoOutput_002'; 135 console.info(TAG, testName + ' begin.'); 136 try { 137 if (isEmpty(mCameraManager) && isEmpty(mCameraDeviceArray)) { 138 console.info(TAG, testName + ' cameraManager is null.'); 139 expect().assertFail(); 140 } else { 141 let photoOutput = mCameraManager.createPhotoOutput(); 142 console.info(TAG, testName + ' photoOutput: ' + JSON.stringify(photoOutput)); 143 expect(isEmpty(photoOutput)).assertFalse(); 144 } 145 done(); 146 } catch (error) { 147 console.error(TAG, testName + ' failed. err: ' + error.code + ', msg: ' + error.message); 148 expect(error.code === cameraErrorCode.INVALID_ARGUMENT).assertTrue(); 149 done(); 150 } 151 }) 152 153 /** 154 * @tc.number : CAMERA_MANAGER_CREATE_PHOTO_OUTPUT_003 155 * @tc.name : createPhotoOutput_abnormal_001 156 * @tc.desc : new photoProfiles -> error_code: 7400101 157 * @tc.size : MEDIUM 158 * @tc.type : Function 159 * @tc.level : Level 2 160 */ 161 it('createPhotoOutput_abnormal_001', Level.LEVEL2, async (done: Function) => { 162 const testName = 'createPhotoOutput_abnormal_001'; 163 let newProfile: Array<camera.Profile> = new Array<camera.Profile>(); 164 createPhotoOutputAbnormal(done, testName, newProfile[0]); 165 }) 166 167 /** 168 * @tc.number : CAMERA_MANAGER_CREATE_PHOTO_OUTPUT_004 169 * @tc.name : createPhotoOutput_abnormal_002 170 * @tc.desc : abnormal photoProfiles -> error_code: 7400101 171 * @tc.size : MEDIUM 172 * @tc.type : Function 173 * @tc.level : Level 2 174 */ 175 it('createPhotoOutput_abnormal_002', Level.LEVEL2, async (done: Function) => { 176 const testName = 'createPhotoOutput_abnormal_002'; 177 let cameraOutputCapability = 178 mCameraManager.getSupportedOutputCapability(mCameraDeviceArray[0], camera.SceneMode.NORMAL_PHOTO); 179 console.info(TAG, testName + ' camera, output cap: ' + JSON.stringify(cameraOutputCapability)); 180 createPhotoOutputAbnormal(done, testName, cameraOutputCapability.photoProfiles[abnormalIndex]); 181 }) 182 183 /** 184 * @tc.number : CAMERA_MANAGER_CREATE_PHOTO_OUTPUT_005 185 * @tc.name : createPhotoOutput_abnormal_003 186 * @tc.desc : photoProfiles->undefined -> error_code: 7400101 187 * @tc.size : MEDIUM 188 * @tc.type : Function 189 * @tc.level : Level 2 190 */ 191 it('createPhotoOutput_abnormal_003', Level.LEVEL2, async (done: Function) => { 192 const testName = 'createPhotoOutput_abnormal_003'; 193 createPhotoOutputAbnormal(done, testName, undefined); 194 }) 195 196 /** 197 * @tc.number : CAMERA_MANAGER_CREATE_PHOTO_OUTPUT_006 198 * @tc.name : createPhotoOutput_abnormal_004 199 * @tc.desc : photoProfiles->null -> error_code: 7400101 200 * @tc.size : MEDIUM 201 * @tc.type : Function 202 * @tc.level : Level 2 203 */ 204 it('createPhotoOutput_abnormal_004', Level.LEVEL2, async (done: Function) => { 205 const testName = 'createPhotoOutput_abnormal_004'; 206 createPhotoOutputAbnormal(done, testName, null); 207 }) 208 209 /** 210 * @tc.number : CAMERA_MANAGER_CREATE_PHOTO_OUTPUT_007 211 * @tc.name : createPhotoOutput_abnormal_005 212 * @tc.desc : no supported sceneMode: SECURE_PHOTO, normal outputCap list -> error_code: 7400101 213 * @tc.size : MEDIUM 214 * @tc.type : Function 215 * @tc.level : Level 0 216 */ 217 it('createPhotoOutput_abnormal_005', Level.LEVEL0, async (done: Function) => { 218 const testName = 'createPhotoOutput_abnormal_005'; 219 let cameraOutputCapability = 220 mCameraManager.getSupportedOutputCapability(mCameraDeviceArray[0], camera.SceneMode.SECURE_PHOTO); 221 console.info(TAG, testName + ' camera, output cap: ' + JSON.stringify(cameraOutputCapability)); 222 createPhotoOutputAbnormal(done, testName, cameraOutputCapability.photoProfiles[0]); 223 }) 224 }) 225}