blob: bae17fa727416ce70a64289fb3d366cdc9ff2f7e [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.test.suitebuilder.annotation.SmallTest;
20import android.hardware.photography.utils.*;
21import android.hardware.photography.utils.Decorator.DecoratorListener;
22
23import junit.framework.Assert;
24
25import java.lang.reflect.Method;
26
27/**
28 * adb shell am instrument -e class 'com.android.mediaframeworktest.unit.CameraUtilsDecoratorTest' \
29 * -w com.android.mediaframeworktest/.MediaFrameworkUnitTestRunner
30 */
31public class CameraUtilsDecoratorTest extends junit.framework.TestCase {
32 private DummyListener mDummyListener;
33 private DummyInterface mIface;
34
35 @Override
36 public void setUp() {
37 mDummyListener = new DummyListener();
38 mIface = Decorator.newInstance(new DummyImpl(), mDummyListener);
39 }
40
41 interface DummyInterface {
42 int addValues(int x, int y, int z);
43
44 void raiseException() throws Exception;
45
46 void raiseUnsupportedOperationException() throws UnsupportedOperationException;
47 }
48
49 class DummyImpl implements DummyInterface {
50 @Override
51 public int addValues(int x, int y, int z) {
52 return x + y + z;
53 }
54
55 @Override
56 public void raiseException() throws Exception {
57 throw new Exception("Test exception");
58 }
59
60 @Override
61 public void raiseUnsupportedOperationException() throws UnsupportedOperationException {
62 throw new UnsupportedOperationException("Test exception");
63 }
64 }
65
66 class DummyListener implements DecoratorListener {
67
68 public boolean beforeCalled = false;
69 public boolean afterCalled = false;
70 public boolean catchCalled = false;
71 public boolean finallyCalled = false;
72 public Object resultValue = null;
73
74 public boolean raiseException = false;
75
76 @Override
77 public void onBeforeInvocation(Method m, Object[] args) {
78 beforeCalled = true;
79 }
80
81 @Override
82 public void onAfterInvocation(Method m, Object[] args, Object result) {
83 afterCalled = true;
84 resultValue = result;
85
86 if (raiseException) {
87 throw new UnsupportedOperationException("Test exception");
88 }
89 }
90
91 @Override
92 public boolean onCatchException(Method m, Object[] args, Throwable t) {
93 catchCalled = true;
94 return false;
95 }
96
97 @Override
98 public void onFinally(Method m, Object[] args) {
99 finallyCalled = true;
100 }
101
102 };
103
104 @SmallTest
105 public void testDecorator() {
106
107 // TODO rewrite this using mocks
108
109 assertTrue(mIface.addValues(1, 2, 3) == 6);
110 assertTrue(mDummyListener.beforeCalled);
111 assertTrue(mDummyListener.afterCalled);
112
113 int resultValue = (Integer)mDummyListener.resultValue;
114 assertTrue(resultValue == 6);
115 assertTrue(mDummyListener.finallyCalled);
116 assertFalse(mDummyListener.catchCalled);
117 }
118
119 @SmallTest
120 public void testDecoratorExceptions() {
121
122 boolean gotExceptions = false;
123 try {
124 mIface.raiseException();
125 } catch (Exception e) {
126 gotExceptions = true;
127 assertTrue(e.getMessage() == "Test exception");
128 }
129 assertTrue(gotExceptions);
130 assertTrue(mDummyListener.beforeCalled);
131 assertFalse(mDummyListener.afterCalled);
132 assertTrue(mDummyListener.catchCalled);
133 assertTrue(mDummyListener.finallyCalled);
134 }
135
136 @SmallTest
137 public void testDecoratorUnsupportedOperationException() {
138
139 boolean gotExceptions = false;
140 try {
141 mIface.raiseUnsupportedOperationException();
142 } catch (UnsupportedOperationException e) {
143 gotExceptions = true;
144 assertTrue(e.getMessage() == "Test exception");
145 }
146 assertTrue(gotExceptions);
147 assertTrue(mDummyListener.beforeCalled);
148 assertFalse(mDummyListener.afterCalled);
149 assertTrue(mDummyListener.catchCalled);
150 assertTrue(mDummyListener.finallyCalled);
151 }
152
153 @SmallTest
154 public void testDecoratorRaisesException() {
155
156 boolean gotExceptions = false;
157 try {
158 mDummyListener.raiseException = true;
159 mIface.addValues(1, 2, 3);
160 Assert.fail("unreachable");
161 } catch (UnsupportedOperationException e) {
162 gotExceptions = true;
163 assertTrue(e.getMessage() == "Test exception");
164 }
165 assertTrue(gotExceptions);
166 assertTrue(mDummyListener.beforeCalled);
167 assertTrue(mDummyListener.afterCalled);
168 assertFalse(mDummyListener.catchCalled);
169 assertTrue(mDummyListener.finallyCalled);
170 }
171}