CTS Tests for the Media Effects API.

Edit: Clarified introductory comment.

Change-Id: Ib64ed14f3c365288ffe918ecd978d91b57b80bfc
diff --git a/tests/tests/effect/Android.mk b/tests/tests/effect/Android.mk
new file mode 100644
index 0000000..990a86a
--- /dev/null
+++ b/tests/tests/effect/Android.mk
@@ -0,0 +1,34 @@
+# Copyright (C) 2011 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_PACKAGE_NAME := CtsEffectTestCases
+
+# Don't include this package in any target.
+LOCAL_MODULE_TAGS := optional
+
+# When built, explicitly put it in the data partition.
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+# All tests should include android.test.runner.
+LOCAL_JAVA_LIBRARIES := android.test.runner
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_SDK_VERSION := current
+
+include $(BUILD_PACKAGE)
diff --git a/tests/tests/effect/AndroidManifest.xml b/tests/tests/effect/AndroidManifest.xml
new file mode 100644
index 0000000..e410800
--- /dev/null
+++ b/tests/tests/effect/AndroidManifest.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.cts.effect">
+
+    <application>
+        <uses-library android:name="android.test.runner" />
+    </application>
+
+    <!-- This is a self-instrumenting test package. -->
+    <instrumentation android:name="android.test.InstrumentationTestRunner"
+                     android:targetPackage="com.android.cts.effect"
+                     android:label="CTS tests of android.media.effect component"/>
+
+</manifest>
+
diff --git a/tests/tests/effect/src/android/effect/cts/EffectTest.java b/tests/tests/effect/src/android/effect/cts/EffectTest.java
new file mode 100644
index 0000000..007e4f3
--- /dev/null
+++ b/tests/tests/effect/src/android/effect/cts/EffectTest.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.effect.cts;
+
+import android.graphics.Bitmap;
+import android.media.effect.Effect;
+import android.media.effect.EffectContext;
+import android.media.effect.EffectFactory;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case to assert that the EffectFactory is correctly instantiating the effects that should
+ * be available on all devices, and that they execute as expected.
+ */
+public class EffectTest extends TestCase {
+
+    /** The GL environment that our tests will run in */
+    private GLEnv mEnv;
+
+    /** This is the effect context we will run the tests in */
+    private EffectContext mEffectContext;
+
+    /** This array contains the list of effects we expect to be available and fuctioning */
+    private static String[] mExpectedEffects;
+
+    @Override
+    protected void setUp() throws Exception {
+        mEnv = new GLEnv();
+        mEnv.makeCurrent();
+        mEffectContext = EffectContext.createWithCurrentGlContext();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        if (mEffectContext != null) {
+            mEffectContext.release();
+        }
+        if (mEnv != null) {
+            mEnv.tearDown();
+        }
+    }
+
+    /** Assert an effect context can be created and attached to GL context. */
+    public void test1_createContext() {
+        assertNotNull("EffectContext attachment", mEffectContext);
+    }
+
+    /** Test that a factory can be retrieved from an EffectContext */
+    public void test2_getFactory() {
+        EffectFactory factory = getEffectContext().getFactory();
+        assertNotNull("EffectFactory retrieval", factory);
+    }
+
+    /** Assert the built-in effects are available */
+    public void test3_availableEffects() {
+        EffectFactory factory = getEffectContext().getFactory();
+        for (String effectName : getExpectedEffectList()) {
+            assertTrue(
+                "Effect '" + effectName + "' supported",
+                factory.isEffectSupported(effectName));
+        }
+    }
+
+    /** Assert that bogus effects are unavailable */
+    public void test4_effectCreate() {
+        EffectFactory factory = getEffectContext().getFactory();
+        assertFalse("Empty effect name unsupported", factory.isEffectSupported(""));
+        assertFalse("Bogus effect name unsupported", factory.isEffectSupported("!BOGUS!"));
+        //assertFalse("Non-effect name unsupported", factory.isEffectSupported("java.lang.String"));
+    }
+
+    /** Assert that we can instantiate an effect */
+    public void test5_effectCreate() {
+        EffectFactory factory = getEffectContext().getFactory();
+        for (String effectName : getExpectedEffectList()) {
+            Effect effect = factory.createEffect(effectName);
+            assertNotNull("Effect '" + effectName + "' instantiation", effect);
+            effect.release();
+        }
+    }
+
+    /** Assert that we can apply an effect */
+    public void test6_effectApply() {
+        EffectFactory factory = getEffectContext().getFactory();
+        Effect effect = factory.createEffect(EffectFactory.EFFECT_SEPIA);
+        Bitmap bitmap = createTestBitmap();
+        int inputTexture = mEnv.bitmapToTexture(bitmap);
+        int outputTexture = mEnv.generateTexture();
+        try {
+            effect.apply(inputTexture, bitmap.getWidth(), bitmap.getHeight(), outputTexture);
+        } catch(RuntimeException e) {
+            fail("Applying EFFECT_SEPIA failed: '" + e.getMessage() + "'");
+        }
+        mEnv.releaseTexture(inputTexture);
+        mEnv.releaseTexture(outputTexture);
+    }
+
+    private EffectContext getEffectContext() {
+        assertNotNull("EffectContext attachment", mEffectContext);
+        return mEffectContext;
+    }
+
+    private Bitmap createTestBitmap() {
+        Bitmap testBitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
+        testBitmap.setPixel(0, 0, 0xFF000000);
+        testBitmap.setPixel(0, 1, 0xFF0000FF);
+        testBitmap.setPixel(1, 0, 0xFF00FF00);
+        testBitmap.setPixel(1, 1, 0xFFFF0000);
+        return testBitmap;
+    }
+
+    private String[] getExpectedEffectList() {
+        if (mExpectedEffects == null) {
+            mExpectedEffects = new String[] {
+                EffectFactory.EFFECT_BRIGHTNESS,
+                EffectFactory.EFFECT_CONTRAST,
+                EffectFactory.EFFECT_FISHEYE,
+                EffectFactory.EFFECT_BACKDROPPER,
+                EffectFactory.EFFECT_AUTOFIX,
+                EffectFactory.EFFECT_BLACKWHITE,
+                EffectFactory.EFFECT_CROP,
+                EffectFactory.EFFECT_CROSSPROCESS,
+                EffectFactory.EFFECT_DOCUMENTARY,
+                EffectFactory.EFFECT_BITMAPOVERLAY,
+                EffectFactory.EFFECT_DUOTONE,
+                EffectFactory.EFFECT_FILLLIGHT,
+                EffectFactory.EFFECT_FLIP,
+                EffectFactory.EFFECT_GRAIN,
+                EffectFactory.EFFECT_GRAYSCALE,
+                EffectFactory.EFFECT_LOMOISH,
+                EffectFactory.EFFECT_NEGATIVE,
+                EffectFactory.EFFECT_POSTERIZE,
+                EffectFactory.EFFECT_REDEYE,
+                EffectFactory.EFFECT_ROTATE,
+                EffectFactory.EFFECT_SATURATE,
+                EffectFactory.EFFECT_SEPIA,
+                EffectFactory.EFFECT_SHARPEN,
+                EffectFactory.EFFECT_STRAIGHTEN,
+                EffectFactory.EFFECT_TEMPERATURE,
+                EffectFactory.EFFECT_TINT,
+                EffectFactory.EFFECT_VIGNETTE
+            };
+        }
+        return mExpectedEffects;
+    }
+}
diff --git a/tests/tests/effect/src/android/effect/cts/GLEnv.java b/tests/tests/effect/src/android/effect/cts/GLEnv.java
new file mode 100644
index 0000000..f9f55e7
--- /dev/null
+++ b/tests/tests/effect/src/android/effect/cts/GLEnv.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.effect.cts;
+
+import android.graphics.Bitmap;
+import android.opengl.GLUtils;
+import android.opengl.GLES20;
+
+import javax.microedition.khronos.egl.*;
+import javax.microedition.khronos.opengles.*;
+
+public class GLEnv {
+
+    private EGLContext mEGLContext;
+    private EGLSurface mEGLSurface;
+    private EGLDisplay mEGLDisplay;
+    private EGLConfig  mEGLConfig;
+
+    private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
+
+    public GLEnv() {
+        EGL10 egl = (EGL10)EGLContext.getEGL();
+
+        mEGLDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
+        checkForEGLErrors("eglGetDisplay");
+
+        int[] version = new int[2];
+        egl.eglInitialize(mEGLDisplay, version);
+        int[] configSpec = {
+            EGL10.EGL_SURFACE_TYPE, EGL10.EGL_PBUFFER_BIT,
+            EGL10.EGL_RED_SIZE, 8,
+            EGL10.EGL_GREEN_SIZE, 8,
+            EGL10.EGL_BLUE_SIZE, 8,
+            EGL10.EGL_NONE
+        };
+        EGLConfig[] configs = new EGLConfig[1];
+        int[] num_config = new int[1];
+        egl.eglChooseConfig(mEGLDisplay, configSpec, configs, 1, num_config);
+        checkForEGLErrors("eglChooseConfig");
+        if (num_config[0] < 1) {
+            throw new RuntimeException("Could not find a suitable config for EGL context!");
+        }
+        mEGLConfig = configs[0];
+
+        int[] attribs = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
+        mEGLContext = egl.eglCreateContext(mEGLDisplay, mEGLConfig, EGL10.EGL_NO_CONTEXT, attribs);
+        checkForEGLErrors("eglCreateContext");
+
+        int[] surfaceSize = { EGL10.EGL_WIDTH, 1, EGL10.EGL_HEIGHT, 1, EGL10.EGL_NONE };
+        mEGLSurface = egl.eglCreatePbufferSurface(mEGLDisplay, mEGLConfig, surfaceSize);
+        checkForEGLErrors("eglCreatePbufferSurface");
+    }
+
+    public void makeCurrent() {
+        EGL10 egl = (EGL10)EGLContext.getEGL();
+        egl.eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext);
+        checkForEGLErrors("eglMakeCurrent");
+    }
+
+    public int generateTexture() {
+        int textures[] = new int[1];
+        GLES20.glGenTextures(1, textures, 0);
+        return textures[0];
+    }
+
+    public int bitmapToTexture(Bitmap bitmap) {
+        int texId = generateTexture();
+        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
+        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
+        return texId;
+    }
+
+    public void releaseTexture(int texId) {
+        int[] textures = new int[1];
+        textures[0] = texId;
+        GLES20.glDeleteTextures(1, textures, 0);
+    }
+
+    public void tearDown() {
+        EGL10 egl = (EGL10)EGLContext.getEGL();
+        egl.eglDestroySurface(mEGLDisplay, mEGLSurface);
+        egl.eglDestroyContext(mEGLDisplay, mEGLContext);
+    }
+
+    public void checkForEGLErrors(String operation) {
+        EGL10 egl = (EGL10)EGLContext.getEGL();
+        int error = egl.eglGetError();
+        if (error != EGL10.EGL_SUCCESS) {
+            throw new RuntimeException("Operation '" + operation + "' caused EGL error: " + error);
+        }
+    }
+
+}