blob: acf41e10bb0baede948f2177d821bbe07bc93f19 [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
Eino-Ville Talvala48bb03f2013-07-25 17:09:14 -070025#include <device2/Camera2Device.h>
26#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
38 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
Igor Murashkine302ee32012-11-05 11:14:49 -080057 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 Murashkinfb40d5d2013-03-26 18:07:31 -070064 ASSERT_LE(
Igor Murashkine302ee32012-11-05 11:14:49 -080065 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 Murashkineab33fc2012-11-06 17:02:54 -080073 TEST_EXTENSION_FORKING_TEAR_DOWN;
74
Igor Murashkine302ee32012-11-05 11:14:49 -080075 TearDownMixin();
76
77 /* important: device must be destructed before closing module,
78 since it calls back into HAL */
79 mDevice.clear();
80
Igor Murashkineab33fc2012-11-06 17:02:54 -080081 if (!TEST_EXTENSION_FORKING_ENABLED) {
82 ASSERT_EQ(0, HWModuleHelpers::closeModule(&mModule->common))
83 << "Failed to close camera HAL module";
84 }
Igor Murashkine302ee32012-11-05 11:14:49 -080085 }
86
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070087 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 Murashkin7acb21a2012-12-18 13:38:40 -0800123private:
124
Igor Murashkine302ee32012-11-05 11:14:49 -0800125 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 Talvala4c543a12013-06-25 18:12:19 -0700131 /* HALBUG (Exynos5); crashes if we skip calling get_camera_info
132 before initializing. Need info anyway now. */
Igor Murashkine302ee32012-11-05 11:14:49 -0800133
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700134 CreateCamera(mCameraID, &mDevice);
135
136 ASSERT_TRUE(mDevice != NULL) << "Failed to open device " << mCameraID;
Igor Murashkine302ee32012-11-05 11:14:49 -0800137 ASSERT_EQ(OK, mDevice->initialize(mModule))
138 << "Failed to initialize device " << mCameraID;
139 }
140 }
141
142 void TearDownMixin() {
143
144 }
145
146protected:
147 int mNumberOfCameras;
148 camera_module_t *mModule;
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700149 sp<CameraDeviceBase> mDevice;
Igor Murashkine302ee32012-11-05 11:14:49 -0800150
151private:
152 int mCameraID;
153};
154
155
156}
157}
158}
159
Igor Murashkineab33fc2012-11-06 17:02:54 -0800160#endif