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
17#ifndef __ANDROID_HAL_CAMERA2_TESTS_MODULE_FIXTURE__
18#define __ANDROID_HAL_CAMERA2_TESTS_MODULE_FIXTURE__
19
20#include <gtest/gtest.h>
21
22#include "hardware/hardware.h"
23#include "hardware/camera2.h"
24
25#include <device2/Camera2Device.h>
26#include <device3/Camera3Device.h>
27
28#include "camera2_utils.h"
29#include "TestExtensions.h"
30
31namespace android {
32namespace camera2 {
33namespace tests {
34
35template <bool InfoQuirk = false>
36struct CameraModuleFixture {
37
38    CameraModuleFixture(int CameraID = -1) {
39        TEST_EXTENSION_FORKING_CONSTRUCTOR;
40
41        mCameraID = CameraID;
42    }
43
44    ~CameraModuleFixture() {
45        TEST_EXTENSION_FORKING_DESTRUCTOR;
46    }
47
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
54    void SetUp() {
55        TEST_EXTENSION_FORKING_SET_UP;
56
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
64        ASSERT_LE(
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() {
73        TEST_EXTENSION_FORKING_TEAR_DOWN;
74
75        TearDownMixin();
76
77        /* important: device must be destructed before closing module,
78           since it calls back into HAL */
79        mDevice.clear();
80
81        if (!TEST_EXTENSION_FORKING_ENABLED) {
82            ASSERT_EQ(0, HWModuleHelpers::closeModule(&mModule->common))
83                << "Failed to close camera HAL module";
84        }
85    }
86
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
123private:
124
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
131            /* HALBUG (Exynos5); crashes if we skip calling get_camera_info
132               before initializing. Need info anyway now. */
133
134            CreateCamera(mCameraID, &mDevice);
135
136            ASSERT_TRUE(mDevice != NULL) << "Failed to open device " << mCameraID;
137            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;
149    sp<CameraDeviceBase> mDevice;
150
151private:
152    int mCameraID;
153};
154
155
156}
157}
158}
159
160#endif
161