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