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            case CAMERA_DEVICE_API_VERSION_3_1:
101            case CAMERA_DEVICE_API_VERSION_3_2:
102                *device = new Camera3Device(cameraID);
103                break;
104            default:
105                device->clear();
106                FAIL() << "Device version unknown for camera " << cameraID << ". Version: " <<
107                       info.device_version;
108        }
109
110    }
111
112    int getDeviceVersion() {
113        return getDeviceVersion(mCameraID);
114    }
115
116    int getDeviceVersion(int cameraId, status_t* status = NULL) {
117        camera_info info;
118        status_t res;
119        res = mModule->get_camera_info(cameraId, &info);
120        if (status != NULL) *status = res;
121
122        return info.device_version;
123    }
124
125private:
126
127    void SetUpMixin() {
128        /* For using this fixture in other tests only */
129        if (mCameraID != -1) {
130            EXPECT_LE(0, mCameraID);
131            EXPECT_LT(mCameraID, mNumberOfCameras);
132
133            /* HALBUG (Exynos5); crashes if we skip calling get_camera_info
134               before initializing. Need info anyway now. */
135
136            CreateCamera(mCameraID, &mDevice);
137
138            ASSERT_TRUE(mDevice != NULL) << "Failed to open device " << mCameraID;
139            ASSERT_EQ(OK, mDevice->initialize(mModule))
140                << "Failed to initialize device " << mCameraID;
141        }
142    }
143
144    void TearDownMixin() {
145
146    }
147
148protected:
149    int mNumberOfCameras;
150    camera_module_t *mModule;
151    sp<CameraDeviceBase> mDevice;
152
153private:
154    int mCameraID;
155};
156
157
158}
159}
160}
161
162#endif
163