1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __ANDROID_HAL_CAMERA2_TESTS_MODULE_FIXTURE__
18 #define __ANDROID_HAL_CAMERA2_TESTS_MODULE_FIXTURE__
19 
20 #include <gtest/gtest.h>
21 
22 #include "hardware/hardware.h"
23 #include "hardware/camera2.h"
24 
25 #include <common/CameraModule.h>
26 #include <device3/Camera3Device.h>
27 
28 #include "camera2_utils.h"
29 #include "TestExtensions.h"
30 
31 namespace android {
32 namespace camera2 {
33 namespace tests {
34 
35 template <bool InfoQuirk = false>
36 struct CameraModuleFixture {
37 
38     CameraModuleFixture(int CameraID = -1) {
39         TEST_EXTENSION_FORKING_CONSTRUCTOR;
40 
41         mCameraID = CameraID;
42     }
43 
~CameraModuleFixtureCameraModuleFixture44     ~CameraModuleFixture() {
45         TEST_EXTENSION_FORKING_DESTRUCTOR;
46     }
47 
GetStaticEntryCameraModuleFixture48     camera_metadata_ro_entry GetStaticEntry(uint32_t tag) const {
49         const CameraMetadata& staticInfo = mDevice->info();
50         camera_metadata_ro_entry entry = staticInfo.find(tag);
51         return entry;
52     }
53 
SetUpCameraModuleFixture54     void SetUp() {
55         TEST_EXTENSION_FORKING_SET_UP;
56 
57         camera_module_t *rawModule;
58         ASSERT_LE(0, hw_get_module(CAMERA_HARDWARE_MODULE_ID,
59             (const hw_module_t **)&rawModule)) << "Could not load camera module";
60         ASSERT_NE((void*)0, rawModule);
61         mModule = new CameraModule(rawModule);
62 
63         mNumberOfCameras = mModule->getNumberOfCameras();
64         ASSERT_LE(0, mNumberOfCameras);
65 
66         ASSERT_LE(
67             CAMERA_MODULE_API_VERSION_2_0, mModule->getModuleApiVersion())
68             << "Wrong module API version";
69 
70         /* For using this fixture in other tests only */
71         SetUpMixin();
72     }
73 
TearDownCameraModuleFixture74     void TearDown() {
75         TEST_EXTENSION_FORKING_TEAR_DOWN;
76 
77         delete mModule;
78         TearDownMixin();
79 
80         /* important: device must be destructed before closing module,
81            since it calls back into HAL */
82         mDevice.clear();
83 
84         if (!TEST_EXTENSION_FORKING_ENABLED) {
85             ASSERT_EQ(0, HWModuleHelpers::closeModule(mModule->getDso()))
86                 << "Failed to close camera HAL module";
87         }
88     }
89 
CreateCameraCameraModuleFixture90     void CreateCamera(int cameraID, /*out*/ sp<CameraDeviceBase> *device) {
91         struct camera_info info;
92         ASSERT_EQ(OK, mModule->getCameraInfo(cameraID, &info));
93 
94         ASSERT_GE((int)info.device_version, CAMERA_DEVICE_API_VERSION_3_0) <<
95                 "Device version too old for camera " << cameraID << ". Version: " <<
96                 info.device_version;
97         switch(info.device_version) {
98             case CAMERA_DEVICE_API_VERSION_3_0:
99             case CAMERA_DEVICE_API_VERSION_3_1:
100             case CAMERA_DEVICE_API_VERSION_3_2:
101                 *device = new Camera3Device(cameraID);
102                 break;
103             default:
104                 device->clear();
105                 FAIL() << "Device version unknown for camera " << cameraID << ". Version: " <<
106                        info.device_version;
107         }
108 
109     }
110 
getDeviceVersionCameraModuleFixture111     int getDeviceVersion() {
112         return getDeviceVersion(mCameraID);
113     }
114 
115     int getDeviceVersion(int cameraId, status_t* status = NULL) {
116         camera_info info;
117         status_t res;
118         res = mModule->getCameraInfo(cameraId, &info);
119         if (status != NULL) *status = res;
120 
121         return info.device_version;
122     }
123 
124 private:
125 
SetUpMixinCameraModuleFixture126     void SetUpMixin() {
127         /* For using this fixture in other tests only */
128         if (mCameraID != -1) {
129             EXPECT_LE(0, mCameraID);
130             EXPECT_LT(mCameraID, mNumberOfCameras);
131 
132             /* HALBUG (Exynos5); crashes if we skip calling get_camera_info
133                before initializing. Need info anyway now. */
134 
135             CreateCamera(mCameraID, &mDevice);
136 
137             ASSERT_TRUE(mDevice != NULL) << "Failed to open device " << mCameraID;
138             ASSERT_EQ(OK, mDevice->initialize(mModule))
139                 << "Failed to initialize device " << mCameraID;
140         }
141     }
142 
TearDownMixinCameraModuleFixture143     void TearDownMixin() {
144 
145     }
146 
147 protected:
148     int mNumberOfCameras;
149     CameraModule *mModule;
150     sp<CameraDeviceBase> mDevice;
151 
152 private:
153     int mCameraID;
154 };
155 
156 
157 }
158 }
159 }
160 
161 #endif
162