Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 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 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 17 | #ifndef __ANDROID_HAL_CAMERA2_TESTS_MODULE_FIXTURE__ |
| 18 | #define __ANDROID_HAL_CAMERA2_TESTS_MODULE_FIXTURE__ |
| 19 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | #include "hardware/hardware.h" |
| 23 | #include "hardware/camera2.h" |
| 24 | |
Yin-Chia Yeh | 7eb3d91 | 2015-02-02 16:20:46 -0800 | [diff] [blame] | 25 | #include <common/CameraModule.h> |
Eino-Ville Talvala | 48bb03f | 2013-07-25 17:09:14 -0700 | [diff] [blame] | 26 | #include <device2/Camera2Device.h> |
| 27 | #include <device3/Camera3Device.h> |
| 28 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 29 | #include "camera2_utils.h" |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 30 | #include "TestExtensions.h" |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 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) { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 40 | TEST_EXTENSION_FORKING_CONSTRUCTOR; |
| 41 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 42 | mCameraID = CameraID; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ~CameraModuleFixture() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 46 | TEST_EXTENSION_FORKING_DESTRUCTOR; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Igor Murashkin | 02f3ac0 | 2012-12-14 16:33:46 -0800 | [diff] [blame] | 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 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 55 | void SetUp() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 56 | TEST_EXTENSION_FORKING_SET_UP; |
| 57 | |
Yin-Chia Yeh | 7eb3d91 | 2015-02-02 16:20:46 -0800 | [diff] [blame] | 58 | camera_module_t *rawModule; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 59 | ASSERT_LE(0, hw_get_module(CAMERA_HARDWARE_MODULE_ID, |
Yin-Chia Yeh | 7eb3d91 | 2015-02-02 16:20:46 -0800 | [diff] [blame] | 60 | (const hw_module_t **)&rawModule)) << "Could not load camera module"; |
| 61 | ASSERT_NE((void*)0, rawModule); |
| 62 | mModule = new CameraModule(rawModule); |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 63 | |
Yin-Chia Yeh | 7eb3d91 | 2015-02-02 16:20:46 -0800 | [diff] [blame] | 64 | mNumberOfCameras = mModule->getNumberOfCameras(); |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 65 | ASSERT_LE(0, mNumberOfCameras); |
| 66 | |
Igor Murashkin | fb40d5d | 2013-03-26 18:07:31 -0700 | [diff] [blame] | 67 | ASSERT_LE( |
Yin-Chia Yeh | c253765 | 2015-03-24 10:48:08 -0700 | [diff] [blame^] | 68 | CAMERA_MODULE_API_VERSION_2_0, mModule->getModuleApiVersion()) |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 69 | << "Wrong module API version"; |
| 70 | |
| 71 | /* For using this fixture in other tests only */ |
| 72 | SetUpMixin(); |
| 73 | } |
| 74 | |
| 75 | void TearDown() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 76 | TEST_EXTENSION_FORKING_TEAR_DOWN; |
| 77 | |
Yin-Chia Yeh | 7eb3d91 | 2015-02-02 16:20:46 -0800 | [diff] [blame] | 78 | delete mModule; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 79 | TearDownMixin(); |
| 80 | |
| 81 | /* important: device must be destructed before closing module, |
| 82 | since it calls back into HAL */ |
| 83 | mDevice.clear(); |
| 84 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 85 | if (!TEST_EXTENSION_FORKING_ENABLED) { |
Yin-Chia Yeh | c253765 | 2015-03-24 10:48:08 -0700 | [diff] [blame^] | 86 | ASSERT_EQ(0, HWModuleHelpers::closeModule(mModule->getDso())) |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 87 | << "Failed to close camera HAL module"; |
| 88 | } |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame] | 91 | void CreateCamera(int cameraID, /*out*/ sp<CameraDeviceBase> *device) { |
| 92 | struct camera_info info; |
Yin-Chia Yeh | 7eb3d91 | 2015-02-02 16:20:46 -0800 | [diff] [blame] | 93 | ASSERT_EQ(OK, mModule->getCameraInfo(cameraID, &info)); |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame] | 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: |
Zhijun He | b5ac45f | 2014-08-07 16:25:41 -0700 | [diff] [blame] | 104 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 105 | case CAMERA_DEVICE_API_VERSION_3_2: |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame] | 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; |
Yin-Chia Yeh | 7eb3d91 | 2015-02-02 16:20:46 -0800 | [diff] [blame] | 123 | res = mModule->getCameraInfo(cameraId, &info); |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame] | 124 | if (status != NULL) *status = res; |
| 125 | |
| 126 | return info.device_version; |
| 127 | } |
| 128 | |
Igor Murashkin | 7acb21a | 2012-12-18 13:38:40 -0800 | [diff] [blame] | 129 | private: |
| 130 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 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 | |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame] | 137 | /* HALBUG (Exynos5); crashes if we skip calling get_camera_info |
| 138 | before initializing. Need info anyway now. */ |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 139 | |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame] | 140 | CreateCamera(mCameraID, &mDevice); |
| 141 | |
| 142 | ASSERT_TRUE(mDevice != NULL) << "Failed to open device " << mCameraID; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 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; |
Yin-Chia Yeh | 7eb3d91 | 2015-02-02 16:20:46 -0800 | [diff] [blame] | 154 | CameraModule *mModule; |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame] | 155 | sp<CameraDeviceBase> mDevice; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 156 | |
| 157 | private: |
| 158 | int mCameraID; |
| 159 | }; |
| 160 | |
| 161 | |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 166 | #endif |