Home | History | Annotate | Download | only in camera2
      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 <device2/Camera2Device.h>
     27 #include <device3/Camera3Device.h>
     28 
     29 #include "camera2_utils.h"
     30 #include "TestExtensions.h"
     31 
     32 namespace android {
     33 namespace camera2 {
     34 namespace tests {
     35 
     36 template <bool InfoQuirk = false>
     37 struct CameraModuleFixture {
     38 
     39     CameraModuleFixture(int CameraID = -1) {
     40         TEST_EXTENSION_FORKING_CONSTRUCTOR;
     41 
     42         mCameraID = CameraID;
     43     }
     44 
     45     ~CameraModuleFixture() {
     46         TEST_EXTENSION_FORKING_DESTRUCTOR;
     47     }
     48 
     49     camera_metadata_ro_entry GetStaticEntry(uint32_t tag) const {
     50         const CameraMetadata& staticInfo = mDevice->info();
     51         camera_metadata_ro_entry entry = staticInfo.find(tag);
     52         return entry;
     53     }
     54 
     55     void SetUp() {
     56         TEST_EXTENSION_FORKING_SET_UP;
     57 
     58         camera_module_t *rawModule;
     59         ASSERT_LE(0, hw_get_module(CAMERA_HARDWARE_MODULE_ID,
     60             (const hw_module_t **)&rawModule)) << "Could not load camera module";
     61         ASSERT_NE((void*)0, rawModule);
     62         mModule = new CameraModule(rawModule);
     63 
     64         mNumberOfCameras = mModule->getNumberOfCameras();
     65         ASSERT_LE(0, mNumberOfCameras);
     66 
     67         ASSERT_LE(
     68             CAMERA_MODULE_API_VERSION_2_0, mModule->getModuleApiVersion())
     69             << "Wrong module API version";
     70 
     71         /* For using this fixture in other tests only */
     72         SetUpMixin();
     73     }
     74 
     75     void TearDown() {
     76         TEST_EXTENSION_FORKING_TEAR_DOWN;
     77 
     78         delete mModule;
     79         TearDownMixin();
     80 
     81         /* important: device must be destructed before closing module,
     82            since it calls back into HAL */
     83         mDevice.clear();
     84 
     85         if (!TEST_EXTENSION_FORKING_ENABLED) {
     86             ASSERT_EQ(0, HWModuleHelpers::closeModule(mModule->getDso()))
     87                 << "Failed to close camera HAL module";
     88         }
     89     }
     90 
     91     void CreateCamera(int cameraID, /*out*/ sp<CameraDeviceBase> *device) {
     92         struct camera_info info;
     93         ASSERT_EQ(OK, mModule->getCameraInfo(cameraID, &info));
     94 
     95         ASSERT_GE((int)info.device_version, CAMERA_DEVICE_API_VERSION_2_0) <<
     96                 "Device version too old for camera " << cameraID << ". Version: " <<
     97                 info.device_version;
     98         switch(info.device_version) {
     99             case CAMERA_DEVICE_API_VERSION_2_0:
    100             case CAMERA_DEVICE_API_VERSION_2_1:
    101                 *device = new Camera2Device(cameraID);
    102                 break;
    103             case CAMERA_DEVICE_API_VERSION_3_0:
    104             case CAMERA_DEVICE_API_VERSION_3_1:
    105             case CAMERA_DEVICE_API_VERSION_3_2:
    106                 *device = new Camera3Device(cameraID);
    107                 break;
    108             default:
    109                 device->clear();
    110                 FAIL() << "Device version unknown for camera " << cameraID << ". Version: " <<
    111                        info.device_version;
    112         }
    113 
    114     }
    115 
    116     int getDeviceVersion() {
    117         return getDeviceVersion(mCameraID);
    118     }
    119 
    120     int getDeviceVersion(int cameraId, status_t* status = NULL) {
    121         camera_info info;
    122         status_t res;
    123         res = mModule->getCameraInfo(cameraId, &info);
    124         if (status != NULL) *status = res;
    125 
    126         return info.device_version;
    127     }
    128 
    129 private:
    130 
    131     void SetUpMixin() {
    132         /* For using this fixture in other tests only */
    133         if (mCameraID != -1) {
    134             EXPECT_LE(0, mCameraID);
    135             EXPECT_LT(mCameraID, mNumberOfCameras);
    136 
    137             /* HALBUG (Exynos5); crashes if we skip calling get_camera_info
    138                before initializing. Need info anyway now. */
    139 
    140             CreateCamera(mCameraID, &mDevice);
    141 
    142             ASSERT_TRUE(mDevice != NULL) << "Failed to open device " << mCameraID;
    143             ASSERT_EQ(OK, mDevice->initialize(mModule))
    144                 << "Failed to initialize device " << mCameraID;
    145         }
    146     }
    147 
    148     void TearDownMixin() {
    149 
    150     }
    151 
    152 protected:
    153     int mNumberOfCameras;
    154     CameraModule *mModule;
    155     sp<CameraDeviceBase> mDevice;
    156 
    157 private:
    158     int mCameraID;
    159 };
    160 
    161 
    162 }
    163 }
    164 }
    165 
    166 #endif
    167