• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #include "utest_camera_hdi_base.h"
17 
18 #define HDF_LOG_TAG camera_service_test
19 
20 constexpr const char *TEST_SERVICE_NAME = "camera_service";
21 
SetUpTestCase(void)22 void CameraHdiBaseTest::SetUpTestCase(void)
23 {
24 }
25 
TearDownTestCase(void)26 void CameraHdiBaseTest::TearDownTestCase(void)
27 {
28 }
29 
SetUp(void)30 void CameraHdiBaseTest::SetUp(void)
31 {
32 }
33 
TearDown(void)34 void CameraHdiBaseTest::TearDown(void)
35 {
36 }
37 
InitCameraHost()38 bool CameraHdiBaseTest::InitCameraHost()
39 {
40     if (cameraHost_ != nullptr) {
41         return true;
42     }
43 
44     cameraHost_ = ICameraHost::Get(TEST_SERVICE_NAME);
45     if (cameraHost_ == nullptr) {
46         return false;
47     }
48     return true;
49 }
50 
GetCameraDevice()51 bool CameraHdiBaseTest::GetCameraDevice()
52 {
53     if (cameraDevice_ != nullptr) {
54         return true;
55     }
56 
57     if (cameraIds_.empty()) {
58         return false;
59     }
60 
61     std::string cameraId = cameraIds_.front();
62     OHOS::sptr<CameraDeviceCallback> deviceCallback = new CameraDeviceCallback();
63     CamRetCode rc = cameraHost_->OpenCamera(cameraId, deviceCallback, cameraDevice_);
64     if (cameraDevice_ == nullptr) {
65         return false;
66     }
67     return true;
68 }
69 
GetStreamOperator()70 bool CameraHdiBaseTest::GetStreamOperator()
71 {
72     if (streamOperator_ != nullptr) {
73         return true;
74     }
75 
76     if (cameraDevice_ == nullptr) {
77         return false;
78     }
79 
80     OHOS::sptr<StreamOperatorCallback> streamOperatorCallback = new StreamOperatorCallback();
81     (void)cameraDevice_->GetStreamOperator(streamOperatorCallback, streamOperator_);
82     if (streamOperator_ == nullptr) {
83         return false;
84     }
85     return true;
86 }
87 
GetCameraIds()88 bool CameraHdiBaseTest::GetCameraIds()
89 {
90     if (InitCameraHost()) {
91         (void)cameraHost_->GetCameraIds(cameraIds_);
92     }
93     if (cameraIds_.empty()) {
94         return false;
95     }
96     return true;
97 }
98 
SaveToFile(const std::string path,const void * buffer,int32_t size)99 int32_t CameraHdiBaseTest::SaveToFile(const std::string path, const void* buffer, int32_t size)
100 {
101     int imgFd = open(path.c_str(), O_RDWR | O_CREAT, 00766);
102     if (imgFd == -1) {
103         std::cout << "open file failed." << std::endl;
104         return -1;
105     }
106 
107     int ret = write(imgFd, buffer, size);
108     if (ret == -1) {
109         std::cout << "write failed." << std::endl;
110         close(imgFd);
111         return -1;
112     }
113     close(imgFd);
114     return 0;
115 }
116 
GetCurrentLocalTimeStamp()117 uint64_t CameraHdiBaseTest::GetCurrentLocalTimeStamp()
118 {
119     std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp =
120         std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
121     auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
122     return tmp.count();
123 }