summaryrefslogtreecommitdiffstats
path: root/media/tests/MediaFrameworkTest/src
diff options
context:
space:
mode:
authorIgor Murashkin <iam@google.com>2013-06-25 20:26:06 +0000
committerIgor Murashkin <iam@google.com>2013-06-26 13:19:44 -0700
commite363fbb2647aeb5ef4c87160d84c6b9ae8d45598 (patch)
treeff832d9b46118be173d254ad269c93627ce9e6d1 /media/tests/MediaFrameworkTest/src
parent73d5fe9f2e6edc11327b4a211f7d077e1e52cbb2 (diff)
downloadframeworks_base-e363fbb2647aeb5ef4c87160d84c6b9ae8d45598.zip
frameworks_base-e363fbb2647aeb5ef4c87160d84c6b9ae8d45598.tar.gz
frameworks_base-e363fbb2647aeb5ef4c87160d84c6b9ae8d45598.tar.bz2
Partial CameraManager implementation
Bug: 9213377 Change-Id: I8f89fb94d7081a71b38e5cd0ad89116d219b4c33
Diffstat (limited to 'media/tests/MediaFrameworkTest/src')
-rw-r--r--media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaFrameworkUnitTestRunner.java8
-rw-r--r--media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsBinderDecoratorTest.java172
-rw-r--r--media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsDecoratorTest.java171
-rw-r--r--media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsRuntimeExceptionTest.java77
-rw-r--r--media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsUncheckedThrowTest.java40
5 files changed, 468 insertions, 0 deletions
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaFrameworkUnitTestRunner.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaFrameworkUnitTestRunner.java
index 62af3f3..e9fbca7 100644
--- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaFrameworkUnitTestRunner.java
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaFrameworkUnitTestRunner.java
@@ -48,6 +48,7 @@ public class MediaFrameworkUnitTestRunner extends InstrumentationTestRunner {
addMediaRecorderStateUnitTests(suite);
addMediaPlayerStateUnitTests(suite);
addMediaScannerUnitTests(suite);
+ addCameraUnitTests(suite);
return suite;
}
@@ -56,6 +57,13 @@ public class MediaFrameworkUnitTestRunner extends InstrumentationTestRunner {
return MediaFrameworkUnitTestRunner.class.getClassLoader();
}
+ private void addCameraUnitTests(TestSuite suite) {
+ suite.addTestSuite(CameraUtilsDecoratorTest.class);
+ suite.addTestSuite(CameraUtilsRuntimeExceptionTest.class);
+ suite.addTestSuite(CameraUtilsUncheckedThrowTest.class);
+ suite.addTestSuite(CameraUtilsBinderDecoratorTest.class);
+ }
+
// Running all unit tests checking the state machine may be time-consuming.
private void addMediaMetadataRetrieverStateUnitTests(TestSuite suite) {
suite.addTestSuite(MediaMetadataRetrieverTest.class);
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsBinderDecoratorTest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsBinderDecoratorTest.java
new file mode 100644
index 0000000..7c48992
--- /dev/null
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsBinderDecoratorTest.java
@@ -0,0 +1,172 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.mediaframeworktest.unit;
+
+import android.hardware.photography.CameraAccessException;
+import android.hardware.photography.utils.CameraBinderDecorator;
+import android.hardware.photography.utils.CameraRuntimeException;
+import android.os.DeadObjectException;
+import android.os.RemoteException;
+import android.os.TransactionTooLargeException;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import static org.mockito.Mockito.*;
+import static android.hardware.photography.utils.CameraBinderDecorator.*;
+import static android.hardware.photography.CameraAccessException.*;
+
+import junit.framework.Assert;
+
+public class CameraUtilsBinderDecoratorTest extends junit.framework.TestCase {
+
+ private interface ICameraBinderStereotype {
+
+ double doNothing();
+
+ // int is a 'status_t'
+ int doSomethingPositive();
+
+ int doSomethingNoError();
+
+ int doSomethingPermissionDenied();
+
+ int doSomethingAlreadyExists();
+
+ int doSomethingBadValue();
+
+ int doSomethingDeadObject() throws CameraRuntimeException;
+
+ int doSomethingBadPolicy() throws CameraRuntimeException;
+
+ int doSomethingDeviceBusy() throws CameraRuntimeException;
+
+ int doSomethingNoSuchDevice() throws CameraRuntimeException;
+
+ int doSomethingUnknownErrorCode();
+
+ int doSomethingThrowDeadObjectException() throws RemoteException;
+
+ int doSomethingThrowTransactionTooLargeException() throws RemoteException;
+ }
+
+ private static final double SOME_ARBITRARY_DOUBLE = 1.0;
+ private static final int SOME_ARBITRARY_POSITIVE_INT = 5;
+ private static final int SOME_ARBITRARY_NEGATIVE_INT = -0xC0FFEE;
+
+ @SmallTest
+ public void testStereotypes() {
+
+ ICameraBinderStereotype mock = mock(ICameraBinderStereotype.class);
+ try {
+ when(mock.doNothing()).thenReturn(SOME_ARBITRARY_DOUBLE);
+ when(mock.doSomethingPositive()).thenReturn(SOME_ARBITRARY_POSITIVE_INT);
+ when(mock.doSomethingNoError()).thenReturn(NO_ERROR);
+ when(mock.doSomethingPermissionDenied()).thenReturn(PERMISSION_DENIED);
+ when(mock.doSomethingAlreadyExists()).thenReturn(ALREADY_EXISTS);
+ when(mock.doSomethingBadValue()).thenReturn(BAD_VALUE);
+ when(mock.doSomethingDeadObject()).thenReturn(DEAD_OBJECT);
+ when(mock.doSomethingBadPolicy()).thenReturn(EACCES);
+ when(mock.doSomethingDeviceBusy()).thenReturn(EBUSY);
+ when(mock.doSomethingNoSuchDevice()).thenReturn(ENODEV);
+ when(mock.doSomethingUnknownErrorCode()).thenReturn(SOME_ARBITRARY_NEGATIVE_INT);
+ when(mock.doSomethingThrowDeadObjectException()).thenThrow(new DeadObjectException());
+ when(mock.doSomethingThrowTransactionTooLargeException()).thenThrow(
+ new TransactionTooLargeException());
+ } catch (RemoteException e) {
+ Assert.fail("Unreachable");
+ }
+
+ ICameraBinderStereotype decoratedMock = CameraBinderDecorator.newInstance(mock);
+
+ // ignored by decorator because return type is double, not int
+ assertEquals(SOME_ARBITRARY_DOUBLE, decoratedMock.doNothing());
+
+ // pass through for positive values
+ assertEquals(SOME_ARBITRARY_POSITIVE_INT, decoratedMock.doSomethingPositive());
+
+ // pass through NO_ERROR
+ assertEquals(NO_ERROR, decoratedMock.doSomethingNoError());
+
+ try {
+ decoratedMock.doSomethingPermissionDenied();
+ Assert.fail("Should've thrown SecurityException");
+ } catch (SecurityException e) {
+ }
+
+ assertEquals(ALREADY_EXISTS, decoratedMock.doSomethingAlreadyExists());
+
+ try {
+ decoratedMock.doSomethingBadValue();
+ Assert.fail("Should've thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ }
+
+ try {
+ decoratedMock.doSomethingDeadObject();
+ Assert.fail("Should've thrown CameraRuntimeException");
+ } catch (CameraRuntimeException e) {
+ assertEquals(CAMERA_DISCONNECTED, e.getReason());
+ }
+
+ try {
+ decoratedMock.doSomethingBadPolicy();
+ Assert.fail("Should've thrown CameraRuntimeException");
+ } catch (CameraRuntimeException e) {
+ assertEquals(CAMERA_DISABLED, e.getReason());
+ }
+
+ try {
+ decoratedMock.doSomethingDeviceBusy();
+ Assert.fail("Should've thrown CameraRuntimeException");
+ } catch (CameraRuntimeException e) {
+ assertEquals(CAMERA_IN_USE, e.getReason());
+ }
+
+ try {
+ decoratedMock.doSomethingNoSuchDevice();
+ Assert.fail("Should've thrown CameraRuntimeException");
+ } catch (CameraRuntimeException e) {
+ assertEquals(CAMERA_DISCONNECTED, e.getReason());
+ }
+
+ try {
+ decoratedMock.doSomethingUnknownErrorCode();
+ Assert.fail("Should've thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ assertEquals(String.format("Unknown error %d",
+ SOME_ARBITRARY_NEGATIVE_INT), e.getMessage());
+ }
+
+ try {
+ decoratedMock.doSomethingThrowDeadObjectException();
+ Assert.fail("Should've thrown CameraRuntimeException");
+ } catch (CameraRuntimeException e) {
+ assertEquals(CAMERA_DISCONNECTED, e.getReason());
+ } catch (RemoteException e) {
+ Assert.fail("Should not throw a DeadObjectException directly, but rethrow");
+ }
+
+ try {
+ decoratedMock.doSomethingThrowTransactionTooLargeException();
+ Assert.fail("Should've thrown UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ assertTrue(e.getCause() instanceof TransactionTooLargeException);
+ } catch (RemoteException e) {
+ Assert.fail("Should not throw a TransactionTooLargeException directly, but rethrow");
+ }
+ }
+
+}
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsDecoratorTest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsDecoratorTest.java
new file mode 100644
index 0000000..bae17fa
--- /dev/null
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsDecoratorTest.java
@@ -0,0 +1,171 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.mediaframeworktest.unit;
+
+import android.test.suitebuilder.annotation.SmallTest;
+import android.hardware.photography.utils.*;
+import android.hardware.photography.utils.Decorator.DecoratorListener;
+
+import junit.framework.Assert;
+
+import java.lang.reflect.Method;
+
+/**
+ * adb shell am instrument -e class 'com.android.mediaframeworktest.unit.CameraUtilsDecoratorTest' \
+ * -w com.android.mediaframeworktest/.MediaFrameworkUnitTestRunner
+ */
+public class CameraUtilsDecoratorTest extends junit.framework.TestCase {
+ private DummyListener mDummyListener;
+ private DummyInterface mIface;
+
+ @Override
+ public void setUp() {
+ mDummyListener = new DummyListener();
+ mIface = Decorator.newInstance(new DummyImpl(), mDummyListener);
+ }
+
+ interface DummyInterface {
+ int addValues(int x, int y, int z);
+
+ void raiseException() throws Exception;
+
+ void raiseUnsupportedOperationException() throws UnsupportedOperationException;
+ }
+
+ class DummyImpl implements DummyInterface {
+ @Override
+ public int addValues(int x, int y, int z) {
+ return x + y + z;
+ }
+
+ @Override
+ public void raiseException() throws Exception {
+ throw new Exception("Test exception");
+ }
+
+ @Override
+ public void raiseUnsupportedOperationException() throws UnsupportedOperationException {
+ throw new UnsupportedOperationException("Test exception");
+ }
+ }
+
+ class DummyListener implements DecoratorListener {
+
+ public boolean beforeCalled = false;
+ public boolean afterCalled = false;
+ public boolean catchCalled = false;
+ public boolean finallyCalled = false;
+ public Object resultValue = null;
+
+ public boolean raiseException = false;
+
+ @Override
+ public void onBeforeInvocation(Method m, Object[] args) {
+ beforeCalled = true;
+ }
+
+ @Override
+ public void onAfterInvocation(Method m, Object[] args, Object result) {
+ afterCalled = true;
+ resultValue = result;
+
+ if (raiseException) {
+ throw new UnsupportedOperationException("Test exception");
+ }
+ }
+
+ @Override
+ public boolean onCatchException(Method m, Object[] args, Throwable t) {
+ catchCalled = true;
+ return false;
+ }
+
+ @Override
+ public void onFinally(Method m, Object[] args) {
+ finallyCalled = true;
+ }
+
+ };
+
+ @SmallTest
+ public void testDecorator() {
+
+ // TODO rewrite this using mocks
+
+ assertTrue(mIface.addValues(1, 2, 3) == 6);
+ assertTrue(mDummyListener.beforeCalled);
+ assertTrue(mDummyListener.afterCalled);
+
+ int resultValue = (Integer)mDummyListener.resultValue;
+ assertTrue(resultValue == 6);
+ assertTrue(mDummyListener.finallyCalled);
+ assertFalse(mDummyListener.catchCalled);
+ }
+
+ @SmallTest
+ public void testDecoratorExceptions() {
+
+ boolean gotExceptions = false;
+ try {
+ mIface.raiseException();
+ } catch (Exception e) {
+ gotExceptions = true;
+ assertTrue(e.getMessage() == "Test exception");
+ }
+ assertTrue(gotExceptions);
+ assertTrue(mDummyListener.beforeCalled);
+ assertFalse(mDummyListener.afterCalled);
+ assertTrue(mDummyListener.catchCalled);
+ assertTrue(mDummyListener.finallyCalled);
+ }
+
+ @SmallTest
+ public void testDecoratorUnsupportedOperationException() {
+
+ boolean gotExceptions = false;
+ try {
+ mIface.raiseUnsupportedOperationException();
+ } catch (UnsupportedOperationException e) {
+ gotExceptions = true;
+ assertTrue(e.getMessage() == "Test exception");
+ }
+ assertTrue(gotExceptions);
+ assertTrue(mDummyListener.beforeCalled);
+ assertFalse(mDummyListener.afterCalled);
+ assertTrue(mDummyListener.catchCalled);
+ assertTrue(mDummyListener.finallyCalled);
+ }
+
+ @SmallTest
+ public void testDecoratorRaisesException() {
+
+ boolean gotExceptions = false;
+ try {
+ mDummyListener.raiseException = true;
+ mIface.addValues(1, 2, 3);
+ Assert.fail("unreachable");
+ } catch (UnsupportedOperationException e) {
+ gotExceptions = true;
+ assertTrue(e.getMessage() == "Test exception");
+ }
+ assertTrue(gotExceptions);
+ assertTrue(mDummyListener.beforeCalled);
+ assertTrue(mDummyListener.afterCalled);
+ assertFalse(mDummyListener.catchCalled);
+ assertTrue(mDummyListener.finallyCalled);
+ }
+}
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsRuntimeExceptionTest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsRuntimeExceptionTest.java
new file mode 100644
index 0000000..8c2dd4d
--- /dev/null
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsRuntimeExceptionTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.mediaframeworktest.unit;
+
+import android.hardware.photography.CameraAccessException;
+import android.hardware.photography.utils.CameraRuntimeException;
+import android.hardware.photography.utils.UncheckedThrow;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import junit.framework.Assert;
+
+public class CameraUtilsRuntimeExceptionTest extends junit.framework.TestCase {
+
+ @SmallTest
+ public void testCameraRuntimeException1() {
+ try {
+ CameraRuntimeException runtimeExc = new CameraRuntimeException(12345);
+ throw runtimeExc.asChecked();
+ } catch (CameraAccessException e) {
+ assertEquals(12345, e.getReason());
+ assertNull(e.getMessage());
+ assertNull(e.getCause());
+ }
+ }
+
+ @SmallTest
+ public void testCameraRuntimeException2() {
+ try {
+ CameraRuntimeException runtimeExc = new CameraRuntimeException(12345, "Hello");
+ throw runtimeExc.asChecked();
+ } catch (CameraAccessException e) {
+ assertEquals(12345, e.getReason());
+ assertEquals("Hello", e.getMessage());
+ assertNull(e.getCause());
+ }
+ }
+
+ @SmallTest
+ public void testCameraRuntimeException3() {
+ Throwable cause = new IllegalStateException("For great justice");
+ try {
+ CameraRuntimeException runtimeExc = new CameraRuntimeException(12345, cause);
+ throw runtimeExc.asChecked();
+ } catch (CameraAccessException e) {
+ assertEquals(12345, e.getReason());
+ assertNull(e.getMessage());
+ assertEquals(cause, e.getCause());
+ }
+ }
+
+ @SmallTest
+ public void testCameraRuntimeException4() {
+ Throwable cause = new IllegalStateException("For great justice");
+ try {
+ CameraRuntimeException runtimeExc = new CameraRuntimeException(12345, "Hello", cause);
+ throw runtimeExc.asChecked();
+ } catch (CameraAccessException e) {
+ assertEquals(12345, e.getReason());
+ assertEquals("Hello", e.getMessage());
+ assertEquals(cause, e.getCause());
+ }
+ }
+}
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsUncheckedThrowTest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsUncheckedThrowTest.java
new file mode 100644
index 0000000..cbe123c
--- /dev/null
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/CameraUtilsUncheckedThrowTest.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.mediaframeworktest.unit;
+
+import android.hardware.photography.CameraAccessException;
+import android.hardware.photography.utils.UncheckedThrow;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import junit.framework.Assert;
+
+public class CameraUtilsUncheckedThrowTest extends junit.framework.TestCase {
+
+ private void fakeNeverThrowsCameraAccess() throws CameraAccessException {
+ }
+
+ @SmallTest
+ public void testUncheckedThrow() {
+ try {
+ UncheckedThrow.throwAnyException(new CameraAccessException(
+ CameraAccessException.CAMERA_DISCONNECTED));
+ Assert.fail("unreachable");
+ fakeNeverThrowsCameraAccess();
+ } catch (CameraAccessException e) {
+ }
+ }
+}