blob: 25513af84c05ee3db345d94703a8293d096c1ae8 [file] [log] [blame]
Igor Murashkine302ee32012-11-05 11:14:49 -08001/*
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 Murashkineab33fc2012-11-06 17:02:54 -080017#ifndef __ANDROID_HAL_CAMERA2_TESTS_MODULE_FIXTURE__
18#define __ANDROID_HAL_CAMERA2_TESTS_MODULE_FIXTURE__
19
Igor Murashkine302ee32012-11-05 11:14:49 -080020#include <gtest/gtest.h>
21
22#include "hardware/hardware.h"
23#include "hardware/camera2.h"
24
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -080025#include <common/CameraModule.h>
Eino-Ville Talvala48bb03f2013-07-25 17:09:14 -070026#include <device3/Camera3Device.h>
27
Igor Murashkine302ee32012-11-05 11:14:49 -080028#include "camera2_utils.h"
Igor Murashkineab33fc2012-11-06 17:02:54 -080029#include "TestExtensions.h"
Igor Murashkine302ee32012-11-05 11:14:49 -080030
31namespace android {
32namespace camera2 {
33namespace tests {
34
35template <bool InfoQuirk = false>
36struct CameraModuleFixture {
37
Chih-Hung Hsieh928e6792016-06-30 14:17:31 -070038 explicit CameraModuleFixture(int CameraID = -1) {
Igor Murashkineab33fc2012-11-06 17:02:54 -080039 TEST_EXTENSION_FORKING_CONSTRUCTOR;
40
Igor Murashkine302ee32012-11-05 11:14:49 -080041 mCameraID = CameraID;
Igor Murashkine302ee32012-11-05 11:14:49 -080042 }
43
44 ~CameraModuleFixture() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080045 TEST_EXTENSION_FORKING_DESTRUCTOR;
Igor Murashkine302ee32012-11-05 11:14:49 -080046 }
47
Igor Murashkin02f3ac02012-12-14 16:33:46 -080048 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 Murashkine302ee32012-11-05 11:14:49 -080054 void SetUp() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080055 TEST_EXTENSION_FORKING_SET_UP;
56
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -080057 camera_module_t *rawModule;
Igor Murashkine302ee32012-11-05 11:14:49 -080058 ASSERT_LE(0, hw_get_module(CAMERA_HARDWARE_MODULE_ID,
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -080059 (const hw_module_t **)&rawModule)) << "Could not load camera module";
60 ASSERT_NE((void*)0, rawModule);
61 mModule = new CameraModule(rawModule);
Igor Murashkine302ee32012-11-05 11:14:49 -080062
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -080063 mNumberOfCameras = mModule->getNumberOfCameras();
Igor Murashkine302ee32012-11-05 11:14:49 -080064 ASSERT_LE(0, mNumberOfCameras);
65
Igor Murashkinfb40d5d2013-03-26 18:07:31 -070066 ASSERT_LE(
Yin-Chia Yehc2537652015-03-24 10:48:08 -070067 CAMERA_MODULE_API_VERSION_2_0, mModule->getModuleApiVersion())
Igor Murashkine302ee32012-11-05 11:14:49 -080068 << "Wrong module API version";
69
70 /* For using this fixture in other tests only */
71 SetUpMixin();
72 }
73
74 void TearDown() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080075 TEST_EXTENSION_FORKING_TEAR_DOWN;
76
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -080077 delete mModule;
Igor Murashkine302ee32012-11-05 11:14:49 -080078 TearDownMixin();
79
80 /* important: device must be destructed before closing module,
81 since it calls back into HAL */
82 mDevice.clear();
83
Igor Murashkineab33fc2012-11-06 17:02:54 -080084 if (!TEST_EXTENSION_FORKING_ENABLED) {
Yin-Chia Yehc2537652015-03-24 10:48:08 -070085 ASSERT_EQ(0, HWModuleHelpers::closeModule(mModule->getDso()))
Igor Murashkineab33fc2012-11-06 17:02:54 -080086 << "Failed to close camera HAL module";
87 }
Igor Murashkine302ee32012-11-05 11:14:49 -080088 }
89
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070090 void CreateCamera(int cameraID, /*out*/ sp<CameraDeviceBase> *device) {
91 struct camera_info info;
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -080092 ASSERT_EQ(OK, mModule->getCameraInfo(cameraID, &info));
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070093
Eino-Ville Talvalad4392022015-11-25 13:51:51 -080094 ASSERT_GE((int)info.device_version, CAMERA_DEVICE_API_VERSION_3_0) <<
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070095 "Device version too old for camera " << cameraID << ". Version: " <<
96 info.device_version;
97 switch(info.device_version) {
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070098 case CAMERA_DEVICE_API_VERSION_3_0:
Zhijun Heb5ac45f2014-08-07 16:25:41 -070099 case CAMERA_DEVICE_API_VERSION_3_1:
100 case CAMERA_DEVICE_API_VERSION_3_2:
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700101 *device = new Camera3Device(cameraID);
102 break;
103 default:
104 device->clear();
105 FAIL() << "Device version unknown for camera " << cameraID << ". Version: " <<
106 info.device_version;
107 }
108
109 }
110
111 int getDeviceVersion() {
112 return getDeviceVersion(mCameraID);
113 }
114
115 int getDeviceVersion(int cameraId, status_t* status = NULL) {
116 camera_info info;
117 status_t res;
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -0800118 res = mModule->getCameraInfo(cameraId, &info);
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700119 if (status != NULL) *status = res;
120
121 return info.device_version;
122 }
123
Igor Murashkin7acb21a2012-12-18 13:38:40 -0800124private:
125
Igor Murashkine302ee32012-11-05 11:14:49 -0800126 void SetUpMixin() {
127 /* For using this fixture in other tests only */
128 if (mCameraID != -1) {
129 EXPECT_LE(0, mCameraID);
130 EXPECT_LT(mCameraID, mNumberOfCameras);
131
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700132 /* HALBUG (Exynos5); crashes if we skip calling get_camera_info
133 before initializing. Need info anyway now. */
Igor Murashkine302ee32012-11-05 11:14:49 -0800134
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700135 CreateCamera(mCameraID, &mDevice);
136
137 ASSERT_TRUE(mDevice != NULL) << "Failed to open device " << mCameraID;
Igor Murashkine302ee32012-11-05 11:14:49 -0800138 ASSERT_EQ(OK, mDevice->initialize(mModule))
139 << "Failed to initialize device " << mCameraID;
140 }
141 }
142
143 void TearDownMixin() {
144
145 }
146
147protected:
148 int mNumberOfCameras;
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -0800149 CameraModule *mModule;
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700150 sp<CameraDeviceBase> mDevice;
Igor Murashkine302ee32012-11-05 11:14:49 -0800151
152private:
153 int mCameraID;
154};
155
156
157}
158}
159}
160
Igor Murashkineab33fc2012-11-06 17:02:54 -0800161#endif