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 #include <gtest/gtest.h> 18 19 #define LOG_TAG "CameraModuleTest" 20 #define LOG_NDEBUG 0 21 #include <utils/Log.h> 22 #include <utils/StrongPointer.h> 23 #include <common/CameraDeviceBase.h> 24 25 #include "hardware/hardware.h" 26 #include "hardware/camera2.h" 27 28 #include "CameraModuleFixture.h" 29 30 namespace android { 31 namespace camera2 { 32 namespace tests { 33 34 class CameraModuleTest : public ::testing::Test, 35 public CameraModuleFixture<> { 36 37 public: 38 CameraModuleTest() { 39 CameraModuleFixture::SetUp(); 40 } 41 42 ~CameraModuleTest() { 43 CameraModuleFixture::TearDown(); 44 } 45 46 status_t initializeDevice(int cameraId) { 47 48 // ignore HAL1s. count as test pass 49 status_t stat; 50 if (isDeviceVersionHal2(cameraId, &stat) && stat == OK) { 51 stat = mDevice->initialize(mModule); 52 } 53 54 return stat; 55 } 56 57 bool isDeviceVersionHal2(int cameraId, status_t* status) { 58 return getDeviceVersion(cameraId, status) 59 >= CAMERA_DEVICE_API_VERSION_2_0; 60 } 61 }; 62 63 TEST_F(CameraModuleTest, LoadModule) { 64 65 TEST_EXTENSION_FORKING_INIT; 66 67 status_t stat; 68 for (int i = 0; i < mNumberOfCameras; ++i) { 69 if (isDeviceVersionHal2(i, &stat) && stat == OK) { 70 CreateCamera(i, &mDevice); 71 ASSERT_EQ(OK, initializeDevice(i)) 72 << "Failed to initialize device " << i; 73 mDevice.clear(); 74 } else { 75 const ::testing::TestInfo* const test_info = 76 ::testing::UnitTest::GetInstance()->current_test_info(); 77 std::cerr << "Skipping test " 78 << test_info->test_case_name() << "." 79 << test_info->name() 80 << " because HAL device version is V1" 81 << std::endl; 82 } 83 } 84 85 } 86 87 TEST_F(CameraModuleTest, LoadModuleBadIndices) { 88 89 TEST_EXTENSION_FORKING_INIT; 90 91 int idx[] = { -1, mNumberOfCameras, mNumberOfCameras + 1 }; 92 hw_device_t *device = NULL; 93 94 for (unsigned i = 0; i < sizeof(idx)/sizeof(idx[0]); ++i) { 95 String8 deviceName = String8::format("%d", idx[i]); 96 status_t res = mModule->open(deviceName, &device); 97 EXPECT_NE(OK, res); 98 EXPECT_EQ(-ENODEV, res) 99 << "Incorrect error code when trying to open camera with invalid id " 100 << deviceName; 101 } 102 } 103 104 TEST_F(CameraModuleTest, GetCameraInfo) { 105 106 TEST_EXTENSION_FORKING_INIT; 107 108 for (int i = 0; i < mNumberOfCameras; ++i) { 109 struct camera_info info; 110 ASSERT_EQ(OK, mModule->getCameraInfo(i, &info)); 111 } 112 113 } 114 115 TEST_F(CameraModuleTest, GetCameraInfoBadIndices) { 116 117 TEST_EXTENSION_FORKING_INIT; 118 119 int idx[] = { -1, mNumberOfCameras, mNumberOfCameras + 1 }; 120 for (unsigned i = 0; i < sizeof(idx)/sizeof(idx[0]); ++i) { 121 struct camera_info info; 122 EXPECT_NE(OK, mModule->getCameraInfo(idx[i], &info)); 123 EXPECT_EQ(-EINVAL, mModule->getCameraInfo(idx[i], &info)) 124 << "Incorrect error code for get_camera_info idx= " 125 << idx[i]; 126 } 127 } 128 129 /** 130 * TODO: Additional test to add: open two cameras at once. 131 * (is allowed to fail, at least for now, but should not blow up) 132 * - open same device multiple times 133 * - close same device multiple times 134 */ 135 136 137 138 139 } 140 } 141 } 142