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