1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file expected 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#include "camera_ability_test.h"
16
17using namespace testing::ext;
18
19void CameraAbilityTest::SetUpTestCase(void) {}
20
21void CameraAbilityTest::TearDownTestCase(void) {}
22
23void CameraAbilityTest::SetUp(void)
24{
25    if (cameraBase_ == nullptr) {
26        cameraBase_ = std::make_shared<TestCameraBase>();
27    }
28    cameraBase_->Init();
29}
30
31OHOS::Camera::RetCode CameraAbilityTest::GetSensorOrientation(std::shared_ptr<CameraAbility> &ability)
32{
33    common_metadata_header_t *data = cameraBase_->ability->get();
34    int32_t sensorOrientation;
35    camera_metadata_item_t entry;
36    int ret = OHOS::Camera::FindCameraMetadataItem(data, OHOS_SENSOR_ORIENTATION, &entry);
37    if (ret != 0) {
38        CAMERA_LOGE("get OHOS_SENSOR_ORIENTATION error.");
39        return OHOS::Camera::RC_ERROR;
40    }
41    sensorOrientation = *(entry.data.i32);
42    CAMERA_LOGD("get sensorOrientation = %{public}d", sensorOrientation);
43    return OHOS::Camera::RC_OK;
44}
45
46OHOS::Camera::RetCode CameraAbilityTest::GetFlashAvailable(std::shared_ptr<CameraAbility> &ability)
47{
48    common_metadata_header_t *data = cameraBase_->ability->get();
49    uint8_t flashAvailable;
50    camera_metadata_item_t entry;
51    int ret = OHOS::Camera::FindCameraMetadataItem(data, OHOS_ABILITY_FLASH_AVAILABLE, &entry);
52    if (ret != 0) {
53        CAMERA_LOGE("get OHOS_ABILITY_FLASH_AVAILABLE error.");
54        return OHOS::Camera::RC_ERROR;
55    }
56    flashAvailable = *(entry.data.u8);
57    CAMERA_LOGD("get flashAvailable = %{public}d", static_cast<int>(flashAvailable));
58    return OHOS::Camera::RC_OK;
59}
60
61OHOS::Camera::RetCode CameraAbilityTest::GetAfAvailable(std::shared_ptr<CameraAbility> &ability)
62{
63    common_metadata_header_t *data = cameraBase_->ability->get();
64    std::vector<uint8_t> afAvailable;
65    camera_metadata_item_t entry;
66    int ret = OHOS::Camera::FindCameraMetadataItem(data, OHOS_CONTROL_AF_AVAILABLE_MODES, &entry);
67    if (ret != 0) {
68        CAMERA_LOGE("get OHOS_CONTROL_AF_AVAILABLE_MODES error.");
69        return OHOS::Camera::RC_ERROR;
70    }
71    uint32_t count = entry.count;
72    CAMERA_LOGD("count = %{public}u", count);
73
74    for (int i = 0; i < count; i++) {
75        afAvailable.push_back(*(entry.data.u8 + i));
76    }
77
78    for (auto it = afAvailable.begin(); it != afAvailable.end(); it++) {
79        CAMERA_LOGI("afAvailable = %{public}d", static_cast<int>(*it));
80    }
81    return OHOS::Camera::RC_OK;
82}
83
84OHOS::Camera::RetCode CameraAbilityTest::GetZoomRatioRange(std::shared_ptr<CameraAbility> &ability)
85{
86    common_metadata_header_t *data = cameraBase_->ability->get();
87    std::vector<float> zoomRatioRange;
88    camera_metadata_item_t entry;
89    int ret = OHOS::Camera::FindCameraMetadataItem(data, OHOS_ABILITY_ZOOM_RATIO_RANGE, &entry);
90    if (ret != 0) {
91        CAMERA_LOGE("get OHOS_ABILITY_ZOOM_RATIO_RANGE error.");
92        return OHOS::Camera::RC_ERROR;
93    }
94    uint32_t count = entry.count;
95    CAMERA_LOGD("count = %{public}u", count);
96
97    for (int i = 0; i < count; i++) {
98        zoomRatioRange.push_back(*(entry.data.f + i));
99    }
100
101    for (auto it = zoomRatioRange.begin(); it != zoomRatioRange.end(); it++) {
102        CAMERA_LOGI("zoomRatioRange = %{public}f", *it);
103    }
104    return OHOS::Camera::RC_OK;
105}
106
107OHOS::Camera::RetCode CameraAbilityTest::GetJpegOrientation(std::shared_ptr<CameraAbility> &ability)
108{
109    common_metadata_header_t *data = cameraBase_->ability->get();
110    int32_t jpegOrientation;
111    camera_metadata_item_t entry;
112    int ret = OHOS::Camera::FindCameraMetadataItem(data, OHOS_JPEG_ORIENTATION, &entry);
113    if (ret != 0) {
114        CAMERA_LOGE("get OHOS_JPEG_ORIENTATION error.");
115        return OHOS::Camera::RC_ERROR;
116    }
117    jpegOrientation = *(entry.data.i32);
118    CAMERA_LOGD("get jpegOrientation = %{public}d", jpegOrientation);
119    return OHOS::Camera::RC_OK;
120}
121
122void CameraAbilityTest::TearDown(void)
123{
124    cameraBase_->Close();
125}
126
127/**
128 * @tc.name: get camera ability
129 * @tc.desc: get camera ability
130 * @tc.level: Level1
131 * @tc.size: MediumTest
132 * @tc.type: Function
133 */
134static HWTEST_F(CameraAbilityTest, camera_ability_001, TestSize.Level1)
135{
136    EXPECT_EQ(true, cameraBase_->ability != nullptr);
137    if (cameraBase_->ability == nullptr) {
138        CAMERA_LOGE("ability is null.");
139        return;
140    }
141    GetSensorOrientation(cameraBase_->ability);
142    GetFlashAvailable(cameraBase_->ability);
143    GetAfAvailable(cameraBase_->ability);
144    GetZoomRatioRange(cameraBase_->ability);
145    GetJpegOrientation(cameraBase_->ability);
146}
147