blob: bc3adc92a2e14a95e71669162598c259b89c11c8 [file] [log] [blame]
Igor Murashkin4491d682013-05-31 16:43:48 -07001/*
2 * Copyright (C) 2013 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
17package com.android.mediaframeworktest.integration;
18
Igor Murashkin4491d682013-05-31 16:43:48 -070019import android.hardware.CameraInfo;
20import android.hardware.ICamera;
21import android.hardware.ICameraClient;
Igor Murashkin4491d682013-05-31 16:43:48 -070022import android.hardware.ICameraServiceListener;
23import android.hardware.IProCameraCallbacks;
24import android.hardware.IProCameraUser;
Igor Murashkin70725502013-06-25 20:27:06 +000025import android.hardware.photography.CameraMetadata;
26import android.hardware.photography.ICameraDeviceCallbacks;
27import android.hardware.photography.ICameraDeviceUser;
Igor Murashkin4491d682013-05-31 16:43:48 -070028import android.os.Binder;
29import android.os.IBinder;
30import android.os.RemoteException;
Igor Murashkin4491d682013-05-31 16:43:48 -070031import android.test.AndroidTestCase;
32import android.test.suitebuilder.annotation.SmallTest;
33import android.util.Log;
34
35/**
Igor Murashkin70725502013-06-25 20:27:06 +000036 * <p>
Igor Murashkin4491d682013-05-31 16:43:48 -070037 * Junit / Instrumentation test case for the camera2 api
Igor Murashkin70725502013-06-25 20:27:06 +000038 * </p>
39 * <p>
40 * To run only tests in this class:
41 * </p>
Igor Murashkin4491d682013-05-31 16:43:48 -070042 *
Igor Murashkin70725502013-06-25 20:27:06 +000043 * <pre>
Igor Murashkin4491d682013-05-31 16:43:48 -070044 * adb shell am instrument \
Igor Murashkin70725502013-06-25 20:27:06 +000045 * -e class com.android.mediaframeworktest.integration.CameraBinderTest \
46 * -w com.android.mediaframeworktest/.MediaFrameworkIntegrationTestRunner
47 * </pre>
Igor Murashkin4491d682013-05-31 16:43:48 -070048 */
49public class CameraBinderTest extends AndroidTestCase {
Igor Murashkin70725502013-06-25 20:27:06 +000050 static String TAG = "CameraBinderTest";
Igor Murashkin4491d682013-05-31 16:43:48 -070051
Igor Murashkin70725502013-06-25 20:27:06 +000052 protected CameraBinderTestUtils mUtils;
Igor Murashkin4491d682013-05-31 16:43:48 -070053
54 public CameraBinderTest() {
55 }
56
Igor Murashkin70725502013-06-25 20:27:06 +000057 @Override
Igor Murashkin4491d682013-05-31 16:43:48 -070058 protected void setUp() throws Exception {
59 super.setUp();
60
Igor Murashkin70725502013-06-25 20:27:06 +000061 mUtils = new CameraBinderTestUtils(getContext());
Igor Murashkin4491d682013-05-31 16:43:48 -070062 }
63
64 @SmallTest
65 public void testNumberOfCameras() throws Exception {
Igor Murashkin70725502013-06-25 20:27:06 +000066
67 int numCameras = mUtils.getCameraService().getNumberOfCameras();
68 assertTrue("At least this many cameras: " + mUtils.getGuessedNumCameras(),
69 numCameras >= mUtils.getGuessedNumCameras());
Igor Murashkin4491d682013-05-31 16:43:48 -070070 Log.v(TAG, "Number of cameras " + numCameras);
71 }
72
73 @SmallTest
74 public void testCameraInfo() throws Exception {
Igor Murashkin70725502013-06-25 20:27:06 +000075 for (int cameraId = 0; cameraId < mUtils.getGuessedNumCameras(); ++cameraId) {
Igor Murashkin4491d682013-05-31 16:43:48 -070076
77 CameraInfo info = new CameraInfo();
78 info.info.facing = -1;
79 info.info.orientation = -1;
80
Igor Murashkin70725502013-06-25 20:27:06 +000081 assertTrue(
82 "Camera service returned info for camera " + cameraId,
83 mUtils.getCameraService().getCameraInfo(cameraId, info) ==
84 CameraBinderTestUtils.NO_ERROR);
Igor Murashkin4491d682013-05-31 16:43:48 -070085 assertTrue("Facing was not set for camera " + cameraId, info.info.facing != -1);
86 assertTrue("Orientation was not set for camera " + cameraId,
87 info.info.orientation != -1);
88
89 Log.v(TAG, "Camera " + cameraId + " info: facing " + info.info.facing
90 + ", orientation " + info.info.orientation);
91 }
92 }
93
94 static abstract class DummyBase extends Binder implements android.os.IInterface {
95 @Override
96 public IBinder asBinder() {
97 return this;
98 }
99 }
100
101 static class DummyCameraClient extends DummyBase implements ICameraClient {
102 }
103
104 @SmallTest
105 public void testConnect() throws Exception {
Igor Murashkin70725502013-06-25 20:27:06 +0000106 for (int cameraId = 0; cameraId < mUtils.getGuessedNumCameras(); ++cameraId) {
Igor Murashkin4491d682013-05-31 16:43:48 -0700107
108 ICameraClient dummyCallbacks = new DummyCameraClient();
109
110 String clientPackageName = getContext().getPackageName();
111
Igor Murashkin70725502013-06-25 20:27:06 +0000112 ICamera cameraUser = mUtils.getCameraService().connect(dummyCallbacks, cameraId,
113 clientPackageName,
114 CameraBinderTestUtils.USE_CALLING_UID);
Igor Murashkin4491d682013-05-31 16:43:48 -0700115 assertNotNull(String.format("Camera %s was null", cameraId), cameraUser);
116
117 Log.v(TAG, String.format("Camera %s connected", cameraId));
118
119 cameraUser.disconnect();
120 }
121 }
122
123 static class DummyProCameraCallbacks extends DummyBase implements IProCameraCallbacks {
124 }
125
126 @SmallTest
127 public void testConnectPro() throws Exception {
Igor Murashkin70725502013-06-25 20:27:06 +0000128 for (int cameraId = 0; cameraId < mUtils.getGuessedNumCameras(); ++cameraId) {
Igor Murashkin4491d682013-05-31 16:43:48 -0700129
130 IProCameraCallbacks dummyCallbacks = new DummyProCameraCallbacks();
131
132 String clientPackageName = getContext().getPackageName();
133
Igor Murashkin70725502013-06-25 20:27:06 +0000134 IProCameraUser cameraUser = mUtils.getCameraService().connectPro(dummyCallbacks,
135 cameraId,
136 clientPackageName, CameraBinderTestUtils.USE_CALLING_UID);
137 assertNotNull(String.format("Camera %s was null", cameraId), cameraUser);
138
139 Log.v(TAG, String.format("Camera %s connected", cameraId));
140
141 cameraUser.disconnect();
142 }
143 }
144
145 static class DummyCameraDeviceCallbacks extends DummyBase implements ICameraDeviceCallbacks {
146
147 @Override
148 public void notifyCallback(int msgType, int ext1, int ext2) throws RemoteException {
149 }
150
151 @Override
152 public void onResultReceived(int frameId, CameraMetadata result) throws RemoteException {
153 }
154 }
155
156 @SmallTest
157 public void testConnectDevice() throws Exception {
158 for (int cameraId = 0; cameraId < mUtils.getGuessedNumCameras(); ++cameraId) {
159
160 ICameraDeviceCallbacks dummyCallbacks = new DummyCameraDeviceCallbacks();
161
162 String clientPackageName = getContext().getPackageName();
163
164 ICameraDeviceUser cameraUser = mUtils.getCameraService().connectDevice(dummyCallbacks,
165 cameraId,
166 clientPackageName, CameraBinderTestUtils.USE_CALLING_UID);
Igor Murashkin4491d682013-05-31 16:43:48 -0700167 assertNotNull(String.format("Camera %s was null", cameraId), cameraUser);
168
169 Log.v(TAG, String.format("Camera %s connected", cameraId));
170
171 cameraUser.disconnect();
172 }
173 }
174
175 static class DummyCameraServiceListener extends DummyBase implements ICameraServiceListener {
176 @Override
177 public void onStatusChanged(int status, int cameraId)
178 throws RemoteException {
179 Log.v(TAG, String.format("Camera %d has status changed to 0x%x", cameraId, status));
180 }
181 }
182
183 /**
Igor Murashkin70725502013-06-25 20:27:06 +0000184 * <pre>
Igor Murashkin4491d682013-05-31 16:43:48 -0700185 * adb shell am instrument \
Igor Murashkin70725502013-06-25 20:27:06 +0000186 * -e class 'com.android.mediaframeworktest.integration.CameraBinderTest#testAddRemoveListeners' \
187 * -w com.android.mediaframeworktest/.MediaFrameworkIntegrationTestRunner
188 * </pre>
Igor Murashkin4491d682013-05-31 16:43:48 -0700189 */
190 @SmallTest
191 public void testAddRemoveListeners() throws Exception {
Igor Murashkin70725502013-06-25 20:27:06 +0000192 for (int cameraId = 0; cameraId < mUtils.getGuessedNumCameras(); ++cameraId) {
Igor Murashkin4491d682013-05-31 16:43:48 -0700193
194 ICameraServiceListener listener = new DummyCameraServiceListener();
195
Igor Murashkin70725502013-06-25 20:27:06 +0000196 assertTrue(
197 "Listener was removed before added",
198 mUtils.getCameraService().removeListener(listener) ==
199 CameraBinderTestUtils.BAD_VALUE);
Igor Murashkin4491d682013-05-31 16:43:48 -0700200
Igor Murashkin70725502013-06-25 20:27:06 +0000201 assertTrue("Listener was not added",
202 mUtils.getCameraService().addListener(listener) ==
203 CameraBinderTestUtils.NO_ERROR);
204 assertTrue(
205 "Listener was wrongly added again",
206 mUtils.getCameraService().addListener(listener) ==
207 CameraBinderTestUtils.ALREADY_EXISTS);
Igor Murashkin4491d682013-05-31 16:43:48 -0700208
Igor Murashkin70725502013-06-25 20:27:06 +0000209 assertTrue(
210 "Listener was not removed",
211 mUtils.getCameraService().removeListener(listener) ==
212 CameraBinderTestUtils.NO_ERROR);
213 assertTrue(
214 "Listener was wrongly removed again",
215 mUtils.getCameraService().removeListener(listener) ==
216 CameraBinderTestUtils.BAD_VALUE);
Igor Murashkin4491d682013-05-31 16:43:48 -0700217 }
218 }
219}