• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2024 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 } from '@ohos/hypium';
17import cameras from 'libentry.so'
18import image from '@ohos.multimedia.image';
19import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
20import bundleManager from '@ohos.bundle.bundleManager';
21import account from '@ohos.account.osAccount';
22
23const TAG = "CAMERA_LOGTAG_JS";
24const CAMERA_OK = 0;
25const CAMERA_ERROR_7400101 = 7400101; //参数错误
26let receiver: image.ImageReceiver;
27let mPhotoSurface: string;
28
29function getCameraManagerInstance() {
30  console.info(TAG + 'mSurfaceId : ' + mPhotoSurface);
31  let ret = cameras.initCamera(mPhotoSurface, 0);
32  console.info(TAG + 'initCamera : ' + ret);
33  if (ret != 0) {
34    console.info(TAG + "getCameraManager FAILED");
35    expect().assertFail();
36  }
37}
38
39async function getPhotoReceiverSurface() {
40  console.log(TAG + '通过宽、高、图片格式、容量创建ImageReceiver实例。');
41  receiver = image.createImageReceiver({ width: 640, height: 480 }, 4, 8);
42  if (receiver !== undefined) {
43    console.log(TAG + '创建ImageReceiver实例 successfully');
44    mPhotoSurface = await receiver.getReceivingSurfaceId();
45    console.log(TAG + 'Photo received id: ' + JSON.stringify(mPhotoSurface));
46  } else {
47    console.log(TAG + '创建ImageReceiver实例 failed');
48  }
49}
50
51function getSupportedCamerasInstance() {
52  let cameraInfo = cameras.oHCameraManagerGetSupportedCameras(0);
53  if (isEmpty(cameraInfo)) {
54    console.info(TAG + "getSupportedCamerasInstance FAILED");
55    expect().assertFail();
56  }
57}
58
59function createCameraInput() {
60  let ret = cameras.oHCameraManagerCreateCameraInput(0);
61  if (ret != CAMERA_OK) {
62    expect().assertFail();
63  }
64}
65
66function isEmpty(data: cameras.cameraInfo | number) {
67  if (data == null || data == undefined) {
68    return true;
69  }
70  return false;
71}
72
73async function getPermissions() {
74  console.info('getPermission start');
75  type MyPermissions = "ohos.permission.CAMERA" | "ohos.permission.READ_MEDIA" | "ohos.permission.WRITE_MEDIA";
76  let list: MyPermissions[] = ['ohos.permission.CAMERA', 'ohos.permission.READ_MEDIA', 'ohos.permission.WRITE_MEDIA'];
77  try {
78    let userId: number = await account.getAccountManager().getOsAccountLocalId();
79    let applicationFlags: number = bundleManager.ApplicationFlag.GET_APPLICATION_INFO_DEFAULT;
80    let applicationInfo: bundleManager.ApplicationInfo = await bundleManager.getApplicationInfoSync('com.example.ohcamerandk', applicationFlags, userId);
81    let tokenId: number = applicationInfo.accessTokenId;
82    for (let index = 0; index < list.length; index++) {
83      await abilityAccessCtrl.createAtManager().grantUserGrantedPermission(tokenId, list[index], 1);
84    }
85  } catch (err) {
86    console.info(`getPermission error ${JSON.stringify(err)}`);
87  }
88  console.info('getPermission end');
89};
90
91export default function CameraInputTest() {
92  describe('CameraInputTest', () => {
93    // Defines a test suite. Two parameters are supported: test suite name and test suite function.
94    beforeAll(async () => {
95      await getPermissions();
96      await getPhotoReceiverSurface();
97      getCameraManagerInstance();
98      getSupportedCamerasInstance();
99    })
100    beforeEach(() => {
101      // Presets an action, which is performed before each unit test case starts.
102      // The number of execution times is the same as the number of test cases defined by **it**.
103      // This API supports only one parameter: preset action function.
104    })
105    afterEach(() => {
106      // Presets a clear action, which is performed after each unit test case ends.
107      // The number of execution times is the same as the number of test cases defined by **it**.
108      // This API supports only one parameter: clear action function.
109    })
110    afterAll(() => {
111      // Presets a clear action, which is performed after all test cases of the test suite end.
112      // This API supports only one parameter: clear action function.
113    })
114    /**
115     * @tc.number     : SUB_MULTIMEIDA_OHCAMWEANDK_OHCameraInputOpen0100
116     * @tc.name       : testOHCameraInputOpen0100
117     * @tc.size       : MediumTest
118     * @tc.desc       : The total duration of obtaining media files, accurate to milliseconds.
119     * @tc.type       : Function
120     * @tc.level      : Level 3
121     */
122    it('testOHCameraInputOpen0100', 0, () => {
123      createCameraInput()
124      let ret = cameras.oHCameraInputOpen(0);
125      console.log(TAG+"testOHCameraInputOpen0100:"+ret);
126      expect(ret).assertEqual(CAMERA_OK);
127    })
128    /**
129     * @tc.number     : SUB_MULTIMEIDA_OHCAMWEANDK_OHCameraInputOpen0200
130     * @tc.name       : testOHCameraInputOpen0200
131     * @tc.size       : MediumTest
132     * @tc.desc       : The total duration of obtaining media files, accurate to milliseconds.
133     * @tc.type       : Function
134     * @tc.level      : Level 3
135     */
136    it('testOHCameraInputOpen0200', 0, () => {
137      createCameraInput()
138      let ret = cameras.oHCameraInputOpen(1);
139      console.log(TAG+"testOHCameraInputOpen0200:"+ret);
140      expect(ret).assertEqual(CAMERA_ERROR_7400101);
141    })
142    /**
143     * @tc.number     : SUB_MULTIMEIDA_OHCAMWEANDK_OHCameraInputClose0100
144     * @tc.name       : testOHCameraInputClose0100
145     * @tc.size       : MediumTest
146     * @tc.desc       : The total duration of obtaining media files, accurate to milliseconds.
147     * @tc.type       : Function
148     * @tc.level      : Level 3
149     */
150    it('testOHCameraInputClose0100', 0, () => {
151      createCameraInput()
152      let ret = cameras.oHCameraInputClose(0);
153      console.log(TAG+"testOHCameraInputClose0100:"+ret);
154      expect(ret).assertEqual(CAMERA_OK);
155    })
156    /**
157     * @tc.number     : SUB_MULTIMEIDA_OHCAMWEANDK_OHCameraInputClose0200
158     * @tc.name       : testOHCameraInputClose0200
159     * @tc.size       : MediumTest
160     * @tc.desc       : The total duration of obtaining media files, accurate to milliseconds.
161     * @tc.type       : Function
162     * @tc.level      : Level 3
163     */
164    it('testOHCameraInputClose0200', 0, () => {
165      createCameraInput()
166      let ret = cameras.oHCameraInputClose(1);
167      console.log(TAG+"testOHCameraInputClose0200:"+ret);
168      expect(ret).assertEqual(CAMERA_ERROR_7400101);
169    })
170    /**
171     * @tc.number     : SUB_MULTIMEIDA_OHCAMWEANDK_OHCameraInputRelease0100
172     * @tc.name       : testOHCameraInputRelease0100
173     * @tc.size       : MediumTest
174     * @tc.desc       : The total duration of obtaining media files, accurate to milliseconds.
175     * @tc.type       : Function
176     * @tc.level      : Level 3
177     */
178    it('testOHCameraInputRelease0100', 0, () => {
179      createCameraInput()
180      let ret = cameras.oHCameraInputRelease(0);
181      console.log(TAG+"testOHCameraInputRelease0100:"+ret);
182      expect(ret).assertEqual(CAMERA_OK);
183    })
184    /**
185     * @tc.number     : SUB_MULTIMEIDA_OHCAMWEANDK_OHCameraInputRelease0200
186     * @tc.name       : testOHCameraInputRelease0200
187     * @tc.size       : MediumTest
188     * @tc.desc       : The total duration of obtaining media files, accurate to milliseconds.
189     * @tc.type       : Function
190     * @tc.level      : Level 3
191     */
192    it('testOHCameraInputRelease0200', 0, () => {
193      createCameraInput()
194      let ret = cameras.oHCameraInputRelease(1);
195      console.log(TAG+"testOHCameraInputRelease0200:"+ret);
196      expect(ret).assertEqual(CAMERA_ERROR_7400101);
197    })
198    /**
199     * @tc.number     : SUB_MULTIMEIDA_OHCAMWEANDK_OHCameraInputRegisterCallback0100
200     * @tc.name       : testOHCameraInputRegisterCallback0100
201     * @tc.size       : MediumTest
202     * @tc.desc       : The total duration of obtaining media files, accurate to milliseconds.
203     * @tc.type       : Function
204     * @tc.level      : Level 3
205     */
206    it('testOHCameraInputRegisterCallback0100', 0, () => {
207      createCameraInput()
208      let ret = cameras.oHCameraInputRegisterCallback(0);
209      console.log(TAG+"testOHCameraInputRegisterCallback0100:"+ret);
210      expect(ret).assertEqual(CAMERA_OK);
211    })
212    /**
213     * @tc.number     : SUB_MULTIMEIDA_OHCAMWEANDK_OHCameraInputRegisterCallback0200
214     * @tc.name       : testOHCameraInputRegisterCallback0200
215     * @tc.size       : MediumTest
216     * @tc.desc       : The total duration of obtaining media files, accurate to milliseconds.
217     * @tc.type       : Function
218     * @tc.level      : Level 3
219     */
220    it('testOHCameraInputRegisterCallback0200', 0, () => {
221      createCameraInput()
222      let ret = cameras.oHCameraInputRegisterCallback(1);
223      console.log(TAG+"testOHCameraInputRegisterCallback0200:"+ret);
224      expect(ret).assertEqual(CAMERA_ERROR_7400101);
225    })
226    /**
227     * @tc.number     : SUB_MULTIMEIDA_OHCAMWEANDK_OHCameraInputRegisterCallback0300
228     * @tc.name       : testOHCameraInputRegisterCallback0300
229     * @tc.size       : MediumTest
230     * @tc.desc       : The total duration of obtaining media files, accurate to milliseconds.
231     * @tc.type       : Function
232     * @tc.level      : Level 3
233     */
234    it('testOHCameraInputRegisterCallback0300', 0, () => {
235      createCameraInput()
236      let ret = cameras.oHCameraInputRegisterCallback(2);
237      console.log(TAG+"testOHCameraInputRegisterCallback0300:"+ret);
238      expect(ret).assertEqual(CAMERA_ERROR_7400101);
239    })
240    /**
241     * @tc.number     : SUB_MULTIMEIDA_OHCAMWEANDK_OHCameraInputUnregisterCallback0100
242     * @tc.name       : testOHCameraInputUnregisterCallback0100
243     * @tc.size       : MediumTest
244     * @tc.desc       : The total duration of obtaining media files, accurate to milliseconds.
245     * @tc.type       : Function
246     * @tc.level      : Level 3
247     */
248    it('testOHCameraInputUnregisterCallback0100', 0, () => {
249      createCameraInput()
250      let ret = cameras.oHCameraInputUnregisterCallback(0);
251      console.log(TAG+"testOHCameraInputUnregisterCallback0100:"+ret);
252      expect(ret).assertEqual(CAMERA_OK);
253    })
254    /**
255     * @tc.number     : SUB_MULTIMEIDA_OHCAMWEANDK_OHCameraInputUnregisterCallback0200
256     * @tc.name       : testOHCameraInputUnregisterCallback0200
257     * @tc.size       : MediumTest
258     * @tc.desc       : The total duration of obtaining media files, accurate to milliseconds.
259     * @tc.type       : Function
260     * @tc.level      : Level 3
261     */
262    it('testOHCameraInputUnregisterCallback0200', 0, () => {
263      createCameraInput()
264      let ret = cameras.oHCameraInputUnregisterCallback(1);
265      console.log(TAG+"testOHCameraInputUnregisterCallback0200:"+ret);
266      expect(ret).assertEqual(CAMERA_ERROR_7400101);
267    })
268    /**
269     * @tc.number     : SUB_MULTIMEIDA_OHCAMWEANDK_OHCameraInputUnregisterCallback0300
270     * @tc.name       : testOHCameraInputUnregisterCallback0300
271     * @tc.size       : MediumTest
272     * @tc.desc       : The total duration of obtaining media files, accurate to milliseconds.
273     * @tc.type       : Function
274     * @tc.level      : Level 3
275     */
276    it('testOHCameraInputUnregisterCallback0300', 0, () => {
277      createCameraInput()
278      let ret = cameras.oHCameraInputUnregisterCallback(2);
279      console.log(TAG+"testOHCameraInputUnregisterCallback0300:"+ret);
280      expect(ret).assertEqual(CAMERA_ERROR_7400101);
281    })
282
283  })
284}