• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, driveFn, getPermission, isEmpty } from '../common';
20import { indexSurfaceId } from '../../testability/pages/Index';
21import { BusinessError } from '@kit.BasicServicesKit';
22
23const TAG = "CameraXts.PreviewOutputTest";
24let mCameraManager: camera.CameraManager;
25let mCameraDeviceArray: Array<camera.CameraDevice>;
26let mSupportedModes: Array<camera.SceneMode>;
27let mCameraInput: camera.CameraInput;
28let mCameraOutputCap: camera.CameraOutputCapability;
29let mPhotoSession: camera.PhotoSession;
30let testContext: common.UIAbilityContext = AppStorage.get('testContext') as common.UIAbilityContext;
31
32function getCameraManager() {
33  console.info(TAG, 'getCameraManager.');
34  mCameraManager = camera.getCameraManager(testContext);
35  if (isEmpty(mCameraManager)) {
36    return false;
37  }
38  console.info(TAG, 'mCameraManager created successfully.');
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
52function getCameraInput() {
53  console.info(TAG, 'getCameraInput.');
54  mCameraInput = mCameraManager.createCameraInput(mCameraDeviceArray[0]);
55  if (isEmpty(mCameraInput)) {
56    return false;
57  }
58  console.info(TAG, 'cameraInput created successfully.');
59  return true;
60}
61
62function getSupportedModes() {
63  console.info(TAG, 'getSupportedModes.');
64  for (let i = 0; i < mCameraDeviceArray.length; i++) {
65    mSupportedModes = mCameraManager.getSupportedSceneModes(mCameraDeviceArray[i]);
66    if (isEmpty(mSupportedModes)) {
67      return false;
68    }
69    console.info(TAG, 'mSupportedModes: ' + JSON.stringify(mSupportedModes));
70  }
71  return true;
72}
73
74function getOutputCapability() {
75  console.info(TAG, 'getOutputCapability.');
76  mCameraOutputCap = mCameraManager.getSupportedOutputCapability(mCameraDeviceArray[0], mSupportedModes[0]);
77  console.info(TAG, 'camera, output cap: ' + JSON.stringify(mCameraOutputCap));
78}
79
80async function getPhotoSession(testName: string, previewOutput: camera.PreviewOutput, photoOutput: camera.PhotoOutput) {
81  console.info(TAG, 'getPhotoSession.');
82  mPhotoSession = mCameraManager.createSession(mSupportedModes[0]);
83  console.info(TAG, testName + ' createSession passed.');
84  mPhotoSession.beginConfig();
85  console.info(TAG, testName + ' beginConfig passed.');
86  mPhotoSession.addInput(mCameraInput);
87  console.info(TAG, testName + ' addInput passed.');
88  await mCameraInput.open();
89  console.info(TAG, testName + ' open passed.');
90  mPhotoSession.addOutput(previewOutput);
91  console.info(TAG, testName + ' addOutput previewOutput passed.');
92  mPhotoSession.addOutput(photoOutput);
93  console.info(TAG, testName + ' addOutput photoOutput passed.');
94  await mPhotoSession.commitConfig();
95  console.info(TAG, testName + ' commitConfig passed.');
96}
97
98export default function PreviewOutputTest() {
99  describe('PreviewOutputTest', () => {
100    beforeAll(async () => {
101      console.info(TAG, 'beforeAll case.');
102      getCameraManager();
103      getSupportedCameraDeviceArray();
104      getSupportedModes();
105      getOutputCapability();
106      await getPermission();
107      await driveFn();
108      getCameraInput();
109    });
110
111    beforeEach(() => {
112      console.info(TAG, 'beforeEach case.');
113    });
114
115    afterEach(() => {
116      console.info(TAG, 'afterEach case.');
117    });
118
119    afterAll(() => {
120      console.info(TAG, 'afterAll case.');
121    });
122
123    /**
124     * @tc.number    : CAMERA_OUTPUT_PREVIEW_001
125     * @tc.name      : on_error_listen_previewOutput_001
126     * @tc.desc      : No abnormal scenarios-listen on error
127     * @tc.size      : MEDIUM
128     * @tc.type      : Function
129     * @tc.level     : Level 0
130     */
131    it('on_error_listen_previewOutput_001', Level.LEVEL0, async (done: Function) => {
132      const testName = 'on_error_listen_previewOutput_001';
133      console.info(TAG, testName + ' begin.');
134      try {
135        if (isEmpty(mCameraManager) && isEmpty(mCameraDeviceArray)) {
136          console.info(TAG, testName + ' cameraManager is null.');
137          expect().assertFail();
138        } else {
139          let previewOutput = mCameraManager.createPreviewOutput(mCameraOutputCap.previewProfiles[0], indexSurfaceId);
140          previewOutput.on('error', (error) => {
141            if (error !== undefined && error.code !== 0) {
142              console.error(TAG,
143                testName + ' failed to listen error previewOutput on, err: ' + error.code + ', msg: ' + error.message);
144              expect().assertFail();
145            } else {
146              console.info(TAG, testName + ' succeed to listen error previewOutput on.');
147              expect(true).assertFalse();
148            }
149          });
150          await previewOutput.release();
151        }
152        done();
153      } catch (error) {
154        console.error(TAG, testName + ' failed. err: ' + error.code + ', msg: ' + error.message);
155        expect().assertFail();
156        done();
157      }
158    })
159
160    /**
161     * @tc.number    : CAMERA_OUTPUT_PREVIEW_002
162     * @tc.name      : off_error_listen_previewOutput_001
163     * @tc.desc      : No abnormal scenarios-listen off error
164     * @tc.size      : MEDIUM
165     * @tc.type      : Function
166     * @tc.level     : Level 0
167     */
168    it('off_error_listen_previewOutput_001', Level.LEVEL0, async (done: Function) => {
169      const testName = 'off_error_listen_previewOutput_001';
170      console.info(TAG, testName + ' begin.');
171      try {
172        if (isEmpty(mCameraManager) && isEmpty(mCameraDeviceArray)) {
173          console.info(TAG, testName + ' cameraManager is null.');
174          expect().assertFail();
175        } else {
176          let photoOutput = mCameraManager.createPhotoOutput(mCameraOutputCap.photoProfiles[0]);
177          let previewOutput = mCameraManager.createPreviewOutput(mCameraOutputCap.previewProfiles[0], indexSurfaceId);
178          await getPhotoSession(testName, previewOutput, photoOutput);
179          previewOutput.on('error', (error) => {
180            if (error !== undefined && error.code !== 0) {
181              console.error(TAG,
182                testName + ' failed to listen error previewOutput on, err: ' + error.code + ', msg: ' + error.message);
183              expect().assertFail();
184            } else {
185              console.info(TAG, testName + ' succeed to listen error previewOutput on.');
186              expect(true).assertFalse();
187            }
188          });
189          previewOutput.off('error', (error) => {
190            if (error !== undefined && error.code !== 0) {
191              console.error(TAG,
192                testName + ' failed to listen error previewOutput off, err: ' + error.code + ', msg: ' + error.message);
193              expect().assertFail();
194            } else {
195              console.info(TAG, testName + ' succeed to listen error previewOutput off.');
196              expect(true).assertFalse();
197            }
198          });
199          await mPhotoSession.release();
200        }
201        done();
202      } catch (error) {
203        console.error(TAG, testName + ' failed. err: ' + error.code + ', msg: ' + error.message);
204        expect().assertFail();
205        await mPhotoSession.release();
206        done();
207      }
208    })
209
210    /**
211     * @tc.number    : CAMERA_OUTPUT_PREVIEW_003
212     * @tc.name      : release_preview_callback_001
213     * @tc.desc      : No abnormal scenarios-preview output release-callback
214     * @tc.size      : MEDIUM
215     * @tc.type      : Function
216     * @tc.level     : Level 0
217     */
218    it('release_preview_callback_001', Level.LEVEL0, async (done: Function) => {
219      const testName = 'release_preview_callback_001';
220      console.info(TAG, testName + ' begin.');
221      try {
222        if (isEmpty(mCameraManager) && isEmpty(mCameraDeviceArray)) {
223          console.info(TAG, testName + ' cameraManager is null.');
224          expect().assertFail();
225        } else {
226          let photoOutput = mCameraManager.createPhotoOutput(mCameraOutputCap.photoProfiles[0]);
227          let previewOutput = mCameraManager.createPreviewOutput(mCameraOutputCap.previewProfiles[0], indexSurfaceId);
228          await getPhotoSession(testName, previewOutput, photoOutput);
229          console.info(TAG, testName + ' getPhotoSession passed.');
230          previewOutput.release((error) => {
231            if (error !== undefined && error.code !== 0) {
232              console.error(TAG,
233                testName + ' failed to release previewOutput, err: ' + error.code + ', msg: ' + error.message);
234              expect(error.code == cameraErrorCode.SESSION_NOT_RUNNING).assertTrue();
235            } else {
236              console.info(TAG, testName + ' release passed.');
237              expect(true).assertTrue();
238            }
239          });
240          await photoOutput.release();
241          await mPhotoSession.release();
242          console.info(TAG, testName + ' end.');
243        }
244        done();
245      } catch (error) {
246        console.error(TAG, testName + ' failed. err: ' + error.code + ', msg: ' + error.message);
247        expect().assertFail();
248        done();
249      }
250    })
251
252    /**
253     * @tc.number    : CAMERA_OUTPUT_PREVIEW_004
254     * @tc.name      : release_preview_promise_001
255     * @tc.desc      : No abnormal scenarios-preview output release-promise
256     * @tc.size      : MEDIUM
257     * @tc.type      : Function
258     * @tc.level     : Level 0
259     */
260    it('release_preview_promise_001', Level.LEVEL0, async (done: Function) => {
261      const testName = 'release_preview_promise_001';
262      console.info(TAG, testName + ' begin.');
263      try {
264        if (isEmpty(mCameraManager) && isEmpty(mCameraDeviceArray)) {
265          console.info(TAG, testName + ' cameraManager is null.');
266          expect().assertFail();
267        } else {
268          let photoOutput = mCameraManager.createPhotoOutput(mCameraOutputCap.photoProfiles[0]);
269          let previewOutput = mCameraManager.createPreviewOutput(mCameraOutputCap.previewProfiles[0], indexSurfaceId);
270          await getPhotoSession(testName, previewOutput, photoOutput);
271          console.info(TAG, testName + ' getPhotoSession passed.');
272          await previewOutput.release().then(() => {
273            console.info(TAG, testName + ' release previewOutput passed.');
274            expect(true).assertTrue();
275          }).catch((error: BusinessError) => {
276            console.error(TAG,
277              testName + ' failed to release previewOutput, err: ' + error.code + ', msg: ' + error.message);
278            expect().assertFail();
279          });
280          await photoOutput.release();
281          await mPhotoSession.release();
282          console.info(TAG, testName + ' end.');
283        }
284        done();
285      } catch (error) {
286        console.error(TAG, testName + ' failed. err: ' + error.code + ', msg: ' + error.message);
287        expect().assertFail();
288        done();
289      }
290    })
291
292    /**
293     * @tc.number    : CAMERA_OUTPUT_PREVIEW_005 //TODO 未能成功监听错误码
294     * @tc.name      : on_error_listen_previewOutput_abnormal_001
295     * @tc.desc      : No abnormal scenarios
296     * @tc.size      : MEDIUM
297     * @tc.type      : Function
298     * @tc.level     : Level 0
299     */
300/*
301    it('on_error_listen_previewOutput_abnormal_001', Level.LEVEL0, async (done: Function) => {
302      const testName = 'on_error_listen_previewOutput_abnormal_001';
303      console.info(TAG, testName + ' begin.');
304      try {
305        if (isEmpty(mCameraManager) && isEmpty(mCameraDeviceArray)) {
306          console.info(TAG, testName + ' cameraManager is null.');
307          expect().assertFail();
308        } else {
309          let previewOutput = mCameraManager.createPreviewOutput(mCameraOutputCap.previewProfiles[0], indexSurfaceId);
310          mPhotoSession = mCameraManager.createSession(mSupportedModes[0]);
311          previewOutput.on('error', (error) => {
312            if (error !== undefined && error.code !== 0) {
313              console.error(TAG,
314                testName + ' failed to listen error previewOutput on, err: ' + error.code + ', msg: ' + error.message);
315              expect().assertFail();
316            } else {
317              console.info(TAG, testName + ' succeed to listen error previewOutput on.');
318              expect(true).assertFalse();
319            }
320          });
321          await mPhotoSession.start();
322          mPhotoSession.beginConfig();
323          await mPhotoSession.release();
324        }
325        done();
326      } catch (error) {
327        console.error(TAG, testName + ' failed. err: ' + error.code + ', msg: ' + error.message);
328        expect(error.code == cameraErrorCode.SESSION_NOT_CONFIG).assertTrue();
329        await mPhotoSession.release();
330        done();
331      }
332    })
333*/
334  })
335}