blob: 7c489921812d4a10bb0fd7c9710dedda84599df1 [file] [log] [blame]
Igor Murashkine363fbb2013-06-25 20:26:06 +00001/*
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.unit;
18
19import android.hardware.photography.CameraAccessException;
20import android.hardware.photography.utils.CameraBinderDecorator;
21import android.hardware.photography.utils.CameraRuntimeException;
22import android.os.DeadObjectException;
23import android.os.RemoteException;
24import android.os.TransactionTooLargeException;
25import android.test.suitebuilder.annotation.SmallTest;
26
27import static org.mockito.Mockito.*;
28import static android.hardware.photography.utils.CameraBinderDecorator.*;
29import static android.hardware.photography.CameraAccessException.*;
30
31import junit.framework.Assert;
32
33public class CameraUtilsBinderDecoratorTest extends junit.framework.TestCase {
34
35 private interface ICameraBinderStereotype {
36
37 double doNothing();
38
39 // int is a 'status_t'
40 int doSomethingPositive();
41
42 int doSomethingNoError();
43
44 int doSomethingPermissionDenied();
45
46 int doSomethingAlreadyExists();
47
48 int doSomethingBadValue();
49
50 int doSomethingDeadObject() throws CameraRuntimeException;
51
52 int doSomethingBadPolicy() throws CameraRuntimeException;
53
54 int doSomethingDeviceBusy() throws CameraRuntimeException;
55
56 int doSomethingNoSuchDevice() throws CameraRuntimeException;
57
58 int doSomethingUnknownErrorCode();
59
60 int doSomethingThrowDeadObjectException() throws RemoteException;
61
62 int doSomethingThrowTransactionTooLargeException() throws RemoteException;
63 }
64
65 private static final double SOME_ARBITRARY_DOUBLE = 1.0;
66 private static final int SOME_ARBITRARY_POSITIVE_INT = 5;
67 private static final int SOME_ARBITRARY_NEGATIVE_INT = -0xC0FFEE;
68
69 @SmallTest
70 public void testStereotypes() {
71
72 ICameraBinderStereotype mock = mock(ICameraBinderStereotype.class);
73 try {
74 when(mock.doNothing()).thenReturn(SOME_ARBITRARY_DOUBLE);
75 when(mock.doSomethingPositive()).thenReturn(SOME_ARBITRARY_POSITIVE_INT);
76 when(mock.doSomethingNoError()).thenReturn(NO_ERROR);
77 when(mock.doSomethingPermissionDenied()).thenReturn(PERMISSION_DENIED);
78 when(mock.doSomethingAlreadyExists()).thenReturn(ALREADY_EXISTS);
79 when(mock.doSomethingBadValue()).thenReturn(BAD_VALUE);
80 when(mock.doSomethingDeadObject()).thenReturn(DEAD_OBJECT);
81 when(mock.doSomethingBadPolicy()).thenReturn(EACCES);
82 when(mock.doSomethingDeviceBusy()).thenReturn(EBUSY);
83 when(mock.doSomethingNoSuchDevice()).thenReturn(ENODEV);
84 when(mock.doSomethingUnknownErrorCode()).thenReturn(SOME_ARBITRARY_NEGATIVE_INT);
85 when(mock.doSomethingThrowDeadObjectException()).thenThrow(new DeadObjectException());
86 when(mock.doSomethingThrowTransactionTooLargeException()).thenThrow(
87 new TransactionTooLargeException());
88 } catch (RemoteException e) {
89 Assert.fail("Unreachable");
90 }
91
92 ICameraBinderStereotype decoratedMock = CameraBinderDecorator.newInstance(mock);
93
94 // ignored by decorator because return type is double, not int
95 assertEquals(SOME_ARBITRARY_DOUBLE, decoratedMock.doNothing());
96
97 // pass through for positive values
98 assertEquals(SOME_ARBITRARY_POSITIVE_INT, decoratedMock.doSomethingPositive());
99
100 // pass through NO_ERROR
101 assertEquals(NO_ERROR, decoratedMock.doSomethingNoError());
102
103 try {
104 decoratedMock.doSomethingPermissionDenied();
105 Assert.fail("Should've thrown SecurityException");
106 } catch (SecurityException e) {
107 }
108
109 assertEquals(ALREADY_EXISTS, decoratedMock.doSomethingAlreadyExists());
110
111 try {
112 decoratedMock.doSomethingBadValue();
113 Assert.fail("Should've thrown IllegalArgumentException");
114 } catch (IllegalArgumentException e) {
115 }
116
117 try {
118 decoratedMock.doSomethingDeadObject();
119 Assert.fail("Should've thrown CameraRuntimeException");
120 } catch (CameraRuntimeException e) {
121 assertEquals(CAMERA_DISCONNECTED, e.getReason());
122 }
123
124 try {
125 decoratedMock.doSomethingBadPolicy();
126 Assert.fail("Should've thrown CameraRuntimeException");
127 } catch (CameraRuntimeException e) {
128 assertEquals(CAMERA_DISABLED, e.getReason());
129 }
130
131 try {
132 decoratedMock.doSomethingDeviceBusy();
133 Assert.fail("Should've thrown CameraRuntimeException");
134 } catch (CameraRuntimeException e) {
135 assertEquals(CAMERA_IN_USE, e.getReason());
136 }
137
138 try {
139 decoratedMock.doSomethingNoSuchDevice();
140 Assert.fail("Should've thrown CameraRuntimeException");
141 } catch (CameraRuntimeException e) {
142 assertEquals(CAMERA_DISCONNECTED, e.getReason());
143 }
144
145 try {
146 decoratedMock.doSomethingUnknownErrorCode();
147 Assert.fail("Should've thrown UnsupportedOperationException");
148 } catch (UnsupportedOperationException e) {
149 assertEquals(String.format("Unknown error %d",
150 SOME_ARBITRARY_NEGATIVE_INT), e.getMessage());
151 }
152
153 try {
154 decoratedMock.doSomethingThrowDeadObjectException();
155 Assert.fail("Should've thrown CameraRuntimeException");
156 } catch (CameraRuntimeException e) {
157 assertEquals(CAMERA_DISCONNECTED, e.getReason());
158 } catch (RemoteException e) {
159 Assert.fail("Should not throw a DeadObjectException directly, but rethrow");
160 }
161
162 try {
163 decoratedMock.doSomethingThrowTransactionTooLargeException();
164 Assert.fail("Should've thrown UnsupportedOperationException");
165 } catch (UnsupportedOperationException e) {
166 assertTrue(e.getCause() instanceof TransactionTooLargeException);
167 } catch (RemoteException e) {
168 Assert.fail("Should not throw a TransactionTooLargeException directly, but rethrow");
169 }
170 }
171
172}