1 package com.xtremelabs.robolectric.shadows; 2 3 4 import android.graphics.ImageFormat; 5 import android.hardware.Camera; 6 import com.xtremelabs.robolectric.Robolectric; 7 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 8 import org.junit.Before; 9 import org.junit.Test; 10 import org.junit.runner.RunWith; 11 12 import java.util.List; 13 14 import static org.hamcrest.CoreMatchers.equalTo; 15 import static org.hamcrest.CoreMatchers.not; 16 import static org.hamcrest.CoreMatchers.notNullValue; 17 import static org.hamcrest.core.IsCollectionContaining.hasItem; 18 import static org.junit.Assert.assertThat; 19 20 @RunWith(WithTestDefaultsRunner.class) 21 public class CameraParametersTest { 22 23 private Camera.Parameters parameters; 24 private ShadowCameraParameters shadowParameters; 25 26 @Before setUp()27 public void setUp() throws Exception { 28 parameters = Robolectric.newInstanceOf(Camera.Parameters.class); 29 shadowParameters = Robolectric.shadowOf(parameters); 30 } 31 32 @Test testPictureSize()33 public void testPictureSize() throws Exception { 34 assertThat(shadowParameters.getPictureHeight(), not(equalTo(600))); 35 assertThat(shadowParameters.getPictureWidth(), not(equalTo(800))); 36 parameters.setPictureSize(800, 600); 37 Camera.Size pictureSize = parameters.getPictureSize(); 38 assertThat(pictureSize.width, equalTo(800)); 39 assertThat(pictureSize.height, equalTo(600)); 40 assertThat(shadowParameters.getPictureHeight(), equalTo(600)); 41 assertThat(shadowParameters.getPictureWidth(), equalTo(800)); 42 } 43 44 @Test testPreviewFpsRange()45 public void testPreviewFpsRange() throws Exception { 46 int[] fpsRange = new int[2]; 47 parameters.getPreviewFpsRange(fpsRange); 48 assertThat(fpsRange[1], not(equalTo(15))); 49 assertThat(fpsRange[0], not(equalTo(25))); 50 parameters.setPreviewFpsRange(15, 25); 51 parameters.getPreviewFpsRange(fpsRange); 52 assertThat(fpsRange[1], equalTo(25)); 53 assertThat(fpsRange[0], equalTo(15)); 54 } 55 56 @Test testPreviewFrameRate()57 public void testPreviewFrameRate() throws Exception { 58 assertThat(parameters.getPreviewFrameRate(), not(equalTo(15))); 59 parameters.setPreviewFrameRate(15); 60 assertThat(parameters.getPreviewFrameRate(), equalTo(15)); 61 } 62 63 @Test testPreviewSize()64 public void testPreviewSize() throws Exception { 65 assertThat(shadowParameters.getPreviewWidth(), not(equalTo(320))); 66 assertThat(shadowParameters.getPreviewHeight(), not(equalTo(240))); 67 parameters.setPreviewSize(320, 240); 68 Camera.Size size = parameters.getPreviewSize(); 69 assertThat(size.width, equalTo(320)); 70 assertThat(size.height, equalTo(240)); 71 assertThat(shadowParameters.getPreviewWidth(), equalTo(320)); 72 assertThat(shadowParameters.getPreviewHeight(), equalTo(240)); 73 } 74 75 @Test testPreviewFormat()76 public void testPreviewFormat() throws Exception { 77 assertThat(shadowParameters.getPreviewFormat(), equalTo(ImageFormat.NV21)); 78 parameters.setPreviewFormat(ImageFormat.JPEG); 79 assertThat(shadowParameters.getPreviewFormat(), equalTo(ImageFormat.JPEG)); 80 } 81 82 @Test testGetSupportedPreviewFormats()83 public void testGetSupportedPreviewFormats() throws Exception { 84 List<Integer> supportedFormats = parameters.getSupportedPreviewFormats(); 85 assertThat(supportedFormats, notNullValue()); 86 assertThat(supportedFormats.size(), not(equalTo(0))); 87 assertThat(supportedFormats, hasItem(ImageFormat.NV21)); 88 } 89 90 @Test testGetSupportedPictureFormats()91 public void testGetSupportedPictureFormats() throws Exception { 92 List<Integer> supportedFormats = parameters.getSupportedPictureFormats(); 93 assertThat(supportedFormats, notNullValue()); 94 assertThat(supportedFormats.size(), equalTo(2)); 95 assertThat(supportedFormats, hasItem(new Integer(ImageFormat.NV21))); 96 } 97 98 @Test testGetSupportedPictureSizes()99 public void testGetSupportedPictureSizes() throws Exception { 100 List<Camera.Size> supportedSizes = parameters.getSupportedPictureSizes(); 101 assertThat(supportedSizes, notNullValue()); 102 assertThat(supportedSizes.size(), equalTo(3)); 103 assertThat(supportedSizes.get(0).width, equalTo(320)); 104 assertThat(supportedSizes.get(0).height, equalTo(240)); 105 } 106 107 @Test testGetSupportedPreviewSizes()108 public void testGetSupportedPreviewSizes() throws Exception { 109 List<Camera.Size> supportedSizes = parameters.getSupportedPreviewSizes(); 110 assertThat(supportedSizes, notNullValue()); 111 assertThat(supportedSizes.size(), equalTo(2)); 112 assertThat(supportedSizes.get(0).width, equalTo(320)); 113 assertThat(supportedSizes.get(0).height, equalTo(240)); 114 } 115 116 @Test testGetSupportedPreviewFpsRange()117 public void testGetSupportedPreviewFpsRange() throws Exception { 118 List<int[]> supportedRanges = parameters.getSupportedPreviewFpsRange(); 119 assertThat(supportedRanges, notNullValue()); 120 assertThat(supportedRanges.size(), equalTo(2)); 121 assertThat(supportedRanges.get(0)[0], equalTo(15000)); 122 assertThat(supportedRanges.get(0)[1], equalTo(15000)); 123 assertThat(supportedRanges.get(1)[0], equalTo(10000)); 124 assertThat(supportedRanges.get(1)[1], equalTo(30000)); 125 } 126 127 @Test testGetSupportedPreviewFrameRates()128 public void testGetSupportedPreviewFrameRates() throws Exception { 129 List<Integer> supportedRates = parameters.getSupportedPreviewFrameRates(); 130 assertThat(supportedRates, notNullValue()); 131 assertThat(supportedRates.size(), equalTo(3)); 132 assertThat(supportedRates.get(0), equalTo(10)); 133 } 134 135 } 136