• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 package com.android.messaging.ui.mediapicker;
18 
19 import android.hardware.Camera;
20 import android.hardware.Camera.CameraInfo;
21 import android.os.AsyncTask;
22 import android.test.suitebuilder.annotation.SmallTest;
23 import com.android.messaging.BugleTestCase;
24 import com.android.messaging.ui.mediapicker.CameraManager.CameraWrapper;
25 import org.mockito.InOrder;
26 import org.mockito.Mockito;
27 
28 @SmallTest
29 public class CameraManagerTest extends BugleTestCase {
30     @Override
setUp()31     protected void setUp() throws Exception {
32         super.setUp();
33         // Force each test to set up a camera wrapper to match their needs
34         CameraManager.setCameraWrapper(null);
35     }
36 
37     @Override
tearDown()38     protected void tearDown() throws Exception {
39         super.tearDown();
40         MockCameraFactory.cleanup();
41     }
42 
testNoCameraDeviceGetInfo()43     public void testNoCameraDeviceGetInfo() {
44         CameraManager.setCameraWrapper(MockCameraFactory.createCameraWrapper());
45         assertEquals(false, CameraManager.get().hasAnyCamera());
46         assertEquals(false, CameraManager.get().hasFrontAndBackCamera());
47         try {
48             CameraManager.get().selectCamera(CameraInfo.CAMERA_FACING_BACK);
49             fail("selectCamera should have thrown");
50         } catch (AssertionError e) {
51         }
52     }
53 
testFrontFacingOnlyGetInfo()54     public void testFrontFacingOnlyGetInfo() {
55         CameraManager.setCameraWrapper(MockCameraFactory.createCameraWrapper(
56                 MockCameraFactory.createCamera(CameraInfo.CAMERA_FACING_FRONT)
57         ));
58         assertEquals(true, CameraManager.get().hasAnyCamera());
59         assertEquals(false, CameraManager.get().hasFrontAndBackCamera());
60         CameraManager.get().selectCamera(CameraInfo.CAMERA_FACING_FRONT);
61         assertEquals(CameraInfo.CAMERA_FACING_FRONT, CameraManager.get().getCameraInfo().facing);
62         CameraManager.get().selectCamera(CameraInfo.CAMERA_FACING_BACK);
63         assertEquals(CameraInfo.CAMERA_FACING_FRONT, CameraManager.get().getCameraInfo().facing);
64     }
65 
testBackFacingOnlyGetInfo()66     public void testBackFacingOnlyGetInfo() {
67         CameraManager.setCameraWrapper(MockCameraFactory.createCameraWrapper(
68                 MockCameraFactory.createCamera(CameraInfo.CAMERA_FACING_BACK)
69         ));
70         assertEquals(true, CameraManager.get().hasAnyCamera());
71         assertEquals(false, CameraManager.get().hasFrontAndBackCamera());
72         CameraManager.get().selectCamera(CameraInfo.CAMERA_FACING_FRONT);
73         assertEquals(CameraInfo.CAMERA_FACING_BACK, CameraManager.get().getCameraInfo().facing);
74         CameraManager.get().selectCamera(CameraInfo.CAMERA_FACING_BACK);
75         assertEquals(CameraInfo.CAMERA_FACING_BACK, CameraManager.get().getCameraInfo().facing);
76     }
77 
testFrontAndBackGetInfo()78     public void testFrontAndBackGetInfo() {
79         CameraManager.setCameraWrapper(MockCameraFactory.createCameraWrapper(
80                 MockCameraFactory.createCamera(CameraInfo.CAMERA_FACING_FRONT),
81                 MockCameraFactory.createCamera(CameraInfo.CAMERA_FACING_BACK)
82         ));
83         assertEquals(true, CameraManager.get().hasAnyCamera());
84         assertEquals(true, CameraManager.get().hasFrontAndBackCamera());
85         CameraManager.get().selectCamera(CameraInfo.CAMERA_FACING_FRONT);
86         assertEquals(CameraInfo.CAMERA_FACING_FRONT, CameraManager.get().getCameraInfo().facing);
87         CameraManager.get().selectCamera(CameraInfo.CAMERA_FACING_BACK);
88         assertEquals(CameraInfo.CAMERA_FACING_BACK, CameraManager.get().getCameraInfo().facing);
89     }
90 
testSwapCamera()91     public void testSwapCamera() {
92         CameraManager.setCameraWrapper(MockCameraFactory.createCameraWrapper(
93                 MockCameraFactory.createCamera(CameraInfo.CAMERA_FACING_FRONT),
94                 MockCameraFactory.createCamera(CameraInfo.CAMERA_FACING_BACK)
95         ));
96         CameraManager.get().selectCamera(CameraInfo.CAMERA_FACING_FRONT);
97         assertEquals(CameraInfo.CAMERA_FACING_FRONT, CameraManager.get().getCameraInfo().facing);
98         CameraManager.get().swapCamera();
99         assertEquals(CameraInfo.CAMERA_FACING_BACK, CameraManager.get().getCameraInfo().facing);
100     }
101 
testOpenCamera()102     public void testOpenCamera() {
103         Camera backCamera = MockCameraFactory.createCamera(CameraInfo.CAMERA_FACING_BACK);
104         Camera frontCamera = MockCameraFactory.createCamera(CameraInfo.CAMERA_FACING_FRONT);
105         CameraWrapper wrapper = MockCameraFactory.createCameraWrapper(frontCamera, backCamera);
106         CameraManager.setCameraWrapper(wrapper);
107         CameraManager.get().selectCamera(CameraInfo.CAMERA_FACING_BACK);
108         CameraManager.get().openCamera();
109         CameraManager.get().openCamera();
110         CameraManager.get().openCamera();
111         waitForPendingAsyncTasks();
112         Mockito.verify(wrapper, Mockito.never()).open(0);
113         Mockito.verify(wrapper).open(1);
114         Mockito.verify(wrapper, Mockito.never()).release(frontCamera);
115         Mockito.verify(wrapper, Mockito.never()).release(backCamera);
116         CameraManager.get().swapCamera();
117         waitForPendingAsyncTasks();
118         Mockito.verify(wrapper).open(0);
119         Mockito.verify(wrapper).open(1);
120         Mockito.verify(wrapper, Mockito.never()).release(frontCamera);
121         Mockito.verify(wrapper).release(backCamera);
122         InOrder inOrder = Mockito.inOrder(wrapper);
123         inOrder.verify(wrapper).open(1);
124         inOrder.verify(wrapper).release(backCamera);
125         inOrder.verify(wrapper).open(0);
126     }
127 
waitForPendingAsyncTasks()128     private void waitForPendingAsyncTasks() {
129         try {
130             final Object lockObject = new Object();
131 
132             new AsyncTask<Void, Void, Void>() {
133                 @Override
134                 protected Void doInBackground(Void... voids) {
135                     return null;
136                 }
137 
138                 @Override
139                 protected void onPostExecute(Void aVoid) {
140                     super.onPostExecute(aVoid);
141                     synchronized (lockObject) {
142                         lockObject.notifyAll();
143                     }
144                 }
145             }.execute();
146 
147             synchronized (lockObject) {
148                 lockObject.wait(500);
149             }
150         } catch (InterruptedException e) {
151             fail();
152         }
153     }
154 }
155