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 | |
| 25 | #include "Camera2Device.h" |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame] | 26 | #include "Camera3Device.h" |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 27 | #include "camera2_utils.h" |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 28 | #include "TestExtensions.h" |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 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) { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 38 | TEST_EXTENSION_FORKING_CONSTRUCTOR; |
| 39 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 40 | mCameraID = CameraID; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | ~CameraModuleFixture() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 44 | TEST_EXTENSION_FORKING_DESTRUCTOR; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Igor Murashkin | 02f3ac0 | 2012-12-14 16:33:46 -0800 | [diff] [blame] | 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 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 53 | void SetUp() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 54 | TEST_EXTENSION_FORKING_SET_UP; |
| 55 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 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 | |
Igor Murashkin | fb40d5d | 2013-03-26 18:07:31 -0700 | [diff] [blame] | 63 | ASSERT_LE( |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 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() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 72 | TEST_EXTENSION_FORKING_TEAR_DOWN; |
| 73 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 74 | TearDownMixin(); |
| 75 | |
| 76 | /* important: device must be destructed before closing module, |
| 77 | since it calls back into HAL */ |
| 78 | mDevice.clear(); |
| 79 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 80 | if (!TEST_EXTENSION_FORKING_ENABLED) { |
| 81 | ASSERT_EQ(0, HWModuleHelpers::closeModule(&mModule->common)) |
| 82 | << "Failed to close camera HAL module"; |
| 83 | } |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame] | 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 | |
Igor Murashkin | 7acb21a | 2012-12-18 13:38:40 -0800 | [diff] [blame] | 122 | private: |
| 123 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 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 | |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame] | 130 | /* HALBUG (Exynos5); crashes if we skip calling get_camera_info |
| 131 | before initializing. Need info anyway now. */ |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 132 | |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame] | 133 | CreateCamera(mCameraID, &mDevice); |
| 134 | |
| 135 | ASSERT_TRUE(mDevice != NULL) << "Failed to open device " << mCameraID; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 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; |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame] | 148 | sp<CameraDeviceBase> mDevice; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 149 | |
| 150 | private: |
| 151 | int mCameraID; |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 159 | #endif |