blob: ef4a9a523c89c2bf0c95864d9e5654978918ea5c [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
25#include "Camera2Device.h"
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070026#include "Camera3Device.h"
Igor Murashkine302ee32012-11-05 11:14:49 -080027#include "camera2_utils.h"
Igor Murashkineab33fc2012-11-06 17:02:54 -080028#include "TestExtensions.h"
Igor Murashkine302ee32012-11-05 11:14:49 -080029
30namespace android {
31namespace camera2 {
32namespace tests {
33
34template <bool InfoQuirk = false>
35struct CameraModuleFixture {
36
37 CameraModuleFixture(int CameraID = -1) {
Igor Murashkineab33fc2012-11-06 17:02:54 -080038 TEST_EXTENSION_FORKING_CONSTRUCTOR;
39
Igor Murashkine302ee32012-11-05 11:14:49 -080040 mCameraID = CameraID;
Igor Murashkine302ee32012-11-05 11:14:49 -080041 }
42
43 ~CameraModuleFixture() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080044 TEST_EXTENSION_FORKING_DESTRUCTOR;
Igor Murashkine302ee32012-11-05 11:14:49 -080045 }
46
Igor Murashkin02f3ac02012-12-14 16:33:46 -080047 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 Murashkine302ee32012-11-05 11:14:49 -080053 void SetUp() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080054 TEST_EXTENSION_FORKING_SET_UP;
55
Igor Murashkine302ee32012-11-05 11:14:49 -080056 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 Murashkinfb40d5d2013-03-26 18:07:31 -070063 ASSERT_LE(
Igor Murashkine302ee32012-11-05 11:14:49 -080064 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 Murashkineab33fc2012-11-06 17:02:54 -080072 TEST_EXTENSION_FORKING_TEAR_DOWN;
73
Igor Murashkine302ee32012-11-05 11:14:49 -080074 TearDownMixin();
75
76 /* important: device must be destructed before closing module,
77 since it calls back into HAL */
78 mDevice.clear();
79
Igor Murashkineab33fc2012-11-06 17:02:54 -080080 if (!TEST_EXTENSION_FORKING_ENABLED) {
81 ASSERT_EQ(0, HWModuleHelpers::closeModule(&mModule->common))
82 << "Failed to close camera HAL module";
83 }
Igor Murashkine302ee32012-11-05 11:14:49 -080084 }
85
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070086 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 Murashkin7acb21a2012-12-18 13:38:40 -0800122private:
123
Igor Murashkine302ee32012-11-05 11:14:49 -0800124 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 Talvala4c543a12013-06-25 18:12:19 -0700130 /* HALBUG (Exynos5); crashes if we skip calling get_camera_info
131 before initializing. Need info anyway now. */
Igor Murashkine302ee32012-11-05 11:14:49 -0800132
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700133 CreateCamera(mCameraID, &mDevice);
134
135 ASSERT_TRUE(mDevice != NULL) << "Failed to open device " << mCameraID;
Igor Murashkine302ee32012-11-05 11:14:49 -0800136 ASSERT_EQ(OK, mDevice->initialize(mModule))
137 << "Failed to initialize device " << mCameraID;
138 }
139 }
140
141 void TearDownMixin() {
142
143 }
144
145protected:
146 int mNumberOfCameras;
147 camera_module_t *mModule;
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700148 sp<CameraDeviceBase> mDevice;
Igor Murashkine302ee32012-11-05 11:14:49 -0800149
150private:
151 int mCameraID;
152};
153
154
155}
156}
157}
158
Igor Murashkineab33fc2012-11-06 17:02:54 -0800159#endif