Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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 android.hardware.cts;
     18 
     19 
     20 import android.cts.util.CtsAndroidTestCase;
     21 import android.hardware.Camera;
     22 import android.hardware.Camera.Parameters;
     23 import android.hardware.cts.helpers.CameraUtils;
     24 import android.test.suitebuilder.annotation.LargeTest;
     25 import android.util.Log;
     26 
     27 import junit.framework.Test;
     28 import junit.framework.TestSuite;
     29 
     30 import java.util.Collections;
     31 import java.util.List;
     32 
     33 @LargeTest
     34 public class Camera_SizeTest extends CtsAndroidTestCase {
     35 
     36     private final int HEIGHT1 = 320;
     37     private final int WIDTH1 = 240;
     38     private final int HEIGHT2 = 480;
     39     private final int WIDTH2 = 320;
     40     private final int HEIGHT3 = 640;
     41     private final int WIDTH3 = 480;
     42 
     43     private static final float ASPECT_RATIO_TOLERANCE = 0.05f;
     44 
     45     private static final String TAG = "Camera_SizeTest";
     46 
     47     public void testConstructor() {
     48         if (Camera.getNumberOfCameras() < 1) {
     49             return;
     50         }
     51 
     52         Camera camera = Camera.open(0);
     53         Parameters parameters = camera.getParameters();
     54 
     55         checkSize(parameters, WIDTH1, HEIGHT1);
     56         checkSize(parameters, WIDTH2, HEIGHT2);
     57         checkSize(parameters, WIDTH3, HEIGHT3);
     58 
     59         camera.release();
     60     }
     61 
     62     /**
     63      * Check that the largest available preview and jpeg outputs have the same aspect ratio.  This
     64      * aspect ratio must be the same as the physical camera sensor, and the FOV for these outputs
     65      * must not be cropped.
     66      *
     67      * This is only required for backward compatibility of the Camera2 API when running in LEGACY
     68      * mode.
     69      *
     70      * @see {@link android.hardware.camera2.CameraCharacteristics#INFO_SUPPORTED_HARDWARE_LEVEL}
     71      */
     72     public void testMaxAspectRatios() throws Exception {
     73         for (int id = 0; id < Camera.getNumberOfCameras(); ++id) {
     74             if (CameraUtils.isLegacyHAL(getContext(), id)) {
     75 
     76                 Camera camera = Camera.open(id);
     77                 Parameters parameters = camera.getParameters();
     78 
     79                 List<Camera.Size> supportedJpegDimens = parameters.getSupportedPictureSizes();
     80                 List<Camera.Size> supportedPreviewDimens = parameters.getSupportedPreviewSizes();
     81 
     82                 Collections.sort(supportedJpegDimens, new CameraUtils.LegacySizeComparator());
     83                 Collections.sort(supportedPreviewDimens, new CameraUtils.LegacySizeComparator());
     84 
     85                 Camera.Size largestJpegDimen =
     86                         supportedJpegDimens.get(supportedJpegDimens.size() - 1);
     87                 Camera.Size largestPreviewDimen =
     88                         supportedPreviewDimens.get(supportedPreviewDimens.size() - 1);
     89 
     90                 float jpegAspect = largestJpegDimen.width / (float) largestJpegDimen.height;
     91                 float previewAspect =
     92                         largestPreviewDimen.width / (float) largestPreviewDimen.height;
     93 
     94                 if (Math.abs(jpegAspect - previewAspect) >= ASPECT_RATIO_TOLERANCE) {
     95                     Log.w(TAG,
     96                             "Largest preview dimension (w=" + largestPreviewDimen.width + ", h=" +
     97                             largestPreviewDimen.height + ") should have the same aspect ratio " +
     98                             "as the largest Jpeg dimension (w=" + largestJpegDimen.width +
     99                             ", h=" + largestJpegDimen.height + ")");
    100                 }
    101 
    102 
    103                 camera.release();
    104             }
    105         }
    106     }
    107 
    108     private void checkSize(Parameters parameters, int width, int height) {
    109         parameters.setPictureSize(width, height);
    110         assertEquals(width, parameters.getPictureSize().width);
    111         assertEquals(height, parameters.getPictureSize().height);
    112     }
    113 
    114     private static void addTestToSuite(TestSuite testSuite, String testName) {
    115         Camera_SizeTest test = new Camera_SizeTest();
    116         test.setName(testName);
    117         testSuite.addTest(test);
    118     }
    119 }
    120