Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2011 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.effect.cts;
     18 
     19 import android.graphics.Bitmap;
     20 import android.media.effect.Effect;
     21 import android.media.effect.EffectContext;
     22 import android.media.effect.EffectFactory;
     23 
     24 import junit.framework.TestCase;
     25 
     26 /**
     27  * Test case to assert that the EffectFactory is correctly instantiating the effects that should
     28  * be available on all devices, and that they execute as expected.
     29  */
     30 public class EffectTest extends TestCase {
     31 
     32     /** The GL environment that our tests will run in */
     33     private GLEnv mEnv;
     34 
     35     /** This is the effect context we will run the tests in */
     36     private EffectContext mEffectContext;
     37 
     38     /** This array contains the list of effects we expect to be available and fuctioning */
     39     private static String[] mExpectedEffects;
     40 
     41     @Override
     42     protected void setUp() throws Exception {
     43         mEnv = new GLEnv();
     44         mEnv.makeCurrent();
     45         mEffectContext = EffectContext.createWithCurrentGlContext();
     46     }
     47 
     48     @Override
     49     protected void tearDown() throws Exception {
     50         if (mEffectContext != null) {
     51             mEffectContext.release();
     52         }
     53         if (mEnv != null) {
     54             mEnv.tearDown();
     55         }
     56     }
     57 
     58     /** Assert an effect context can be created and attached to GL context. */
     59     public void test1_createContext() {
     60         assertNotNull("EffectContext attachment", mEffectContext);
     61     }
     62 
     63     /** Test that a factory can be retrieved from an EffectContext */
     64     public void test2_getFactory() {
     65         EffectFactory factory = getEffectContext().getFactory();
     66         assertNotNull("EffectFactory retrieval", factory);
     67     }
     68 
     69     /** Assert the built-in effects are available */
     70     public void test3_availableEffects() {
     71         EffectFactory factory = getEffectContext().getFactory();
     72         for (String effectName : getExpectedEffectList()) {
     73             assertTrue(
     74                 "Effect '" + effectName + "' supported",
     75                 factory.isEffectSupported(effectName));
     76         }
     77     }
     78 
     79     /** Assert that bogus effects are unavailable */
     80     public void test4_effectCreate() {
     81         EffectFactory factory = getEffectContext().getFactory();
     82         assertFalse("Empty effect name unsupported", factory.isEffectSupported(""));
     83         assertFalse("Bogus effect name unsupported", factory.isEffectSupported("!BOGUS!"));
     84         //assertFalse("Non-effect name unsupported", factory.isEffectSupported("java.lang.String"));
     85     }
     86 
     87     /** Assert that we can instantiate an effect */
     88     public void test5_effectCreate() {
     89         EffectFactory factory = getEffectContext().getFactory();
     90         for (String effectName : getExpectedEffectList()) {
     91             Effect effect = factory.createEffect(effectName);
     92             assertNotNull("Effect '" + effectName + "' instantiation", effect);
     93             effect.release();
     94         }
     95     }
     96 
     97     /** Assert that we can apply an effect */
     98     public void test6_effectApply() {
     99         EffectFactory factory = getEffectContext().getFactory();
    100         Effect effect = factory.createEffect(EffectFactory.EFFECT_SEPIA);
    101         Bitmap bitmap = createTestBitmap();
    102         int inputTexture = mEnv.bitmapToTexture(bitmap);
    103         int outputTexture = mEnv.generateTexture();
    104         try {
    105             effect.apply(inputTexture, bitmap.getWidth(), bitmap.getHeight(), outputTexture);
    106         } catch(RuntimeException e) {
    107             fail("Applying EFFECT_SEPIA failed: '" + e.getMessage() + "'");
    108         }
    109         mEnv.releaseTexture(inputTexture);
    110         mEnv.releaseTexture(outputTexture);
    111     }
    112 
    113     private EffectContext getEffectContext() {
    114         assertNotNull("EffectContext attachment", mEffectContext);
    115         return mEffectContext;
    116     }
    117 
    118     private Bitmap createTestBitmap() {
    119         Bitmap testBitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
    120         testBitmap.setPixel(0, 0, 0xFF000000);
    121         testBitmap.setPixel(0, 1, 0xFF0000FF);
    122         testBitmap.setPixel(1, 0, 0xFF00FF00);
    123         testBitmap.setPixel(1, 1, 0xFFFF0000);
    124         return testBitmap;
    125     }
    126 
    127     private String[] getExpectedEffectList() {
    128         if (mExpectedEffects == null) {
    129             mExpectedEffects = new String[] {
    130                 EffectFactory.EFFECT_BRIGHTNESS,
    131                 EffectFactory.EFFECT_CONTRAST,
    132                 EffectFactory.EFFECT_FISHEYE,
    133                 EffectFactory.EFFECT_BACKDROPPER,
    134                 EffectFactory.EFFECT_AUTOFIX,
    135                 EffectFactory.EFFECT_BLACKWHITE,
    136                 EffectFactory.EFFECT_CROP,
    137                 EffectFactory.EFFECT_CROSSPROCESS,
    138                 EffectFactory.EFFECT_DOCUMENTARY,
    139                 EffectFactory.EFFECT_BITMAPOVERLAY,
    140                 EffectFactory.EFFECT_DUOTONE,
    141                 EffectFactory.EFFECT_FILLLIGHT,
    142                 EffectFactory.EFFECT_FLIP,
    143                 EffectFactory.EFFECT_GRAIN,
    144                 EffectFactory.EFFECT_GRAYSCALE,
    145                 EffectFactory.EFFECT_LOMOISH,
    146                 EffectFactory.EFFECT_NEGATIVE,
    147                 EffectFactory.EFFECT_POSTERIZE,
    148                 EffectFactory.EFFECT_REDEYE,
    149                 EffectFactory.EFFECT_ROTATE,
    150                 EffectFactory.EFFECT_SATURATE,
    151                 EffectFactory.EFFECT_SEPIA,
    152                 EffectFactory.EFFECT_SHARPEN,
    153                 EffectFactory.EFFECT_STRAIGHTEN,
    154                 EffectFactory.EFFECT_TEMPERATURE,
    155                 EffectFactory.EFFECT_TINT,
    156                 EffectFactory.EFFECT_VIGNETTE
    157             };
    158         }
    159         return mExpectedEffects;
    160     }
    161 }
    162