Add graphics.TextureView test
- create a new graphics2 package to have its own activity

Change-Id: I370965c376dbf8f9cbc34ce9a283cf013468c31a
diff --git a/CtsTestCaseList.mk b/CtsTestCaseList.mk
index 0004a44..1c60a45 100644
--- a/CtsTestCaseList.mk
+++ b/CtsTestCaseList.mk
@@ -44,6 +44,7 @@
 	CtsExampleTestCases \
 	CtsGestureTestCases \
 	CtsGraphicsTestCases \
+	CtsGraphics2TestCases \
 	CtsHardwareTestCases \
 	CtsHoloTestCases \
 	CtsJniTestCases \
diff --git a/development/ide/eclipse/.classpath b/development/ide/eclipse/.classpath
index 30c63d8..706a601 100644
--- a/development/ide/eclipse/.classpath
+++ b/development/ide/eclipse/.classpath
@@ -32,6 +32,7 @@
     <classpathentry kind="src" path="cts/tests/tests/example/src"/>
     <classpathentry kind="src" path="cts/tests/tests/gesture/src"/>
     <classpathentry kind="src" path="cts/tests/tests/graphics/src"/>
+    <classpathentry kind="src" path="cts/tests/tests/graphics2/src"/>
     <classpathentry kind="src" path="cts/tests/tests/hardware/src"/>
     <classpathentry kind="src" path="cts/tests/tests/holo/src"/>
     <classpathentry kind="src" path="cts/tests/tests/jni/src"/>
diff --git a/tests/tests/graphics2/Android.mk b/tests/tests/graphics2/Android.mk
new file mode 100644
index 0000000..d977ee8
--- /dev/null
+++ b/tests/tests/graphics2/Android.mk
@@ -0,0 +1,34 @@
+# Copyright (C) 2012 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)
+
+# don't include this package in any target
+LOCAL_MODULE_TAGS := optional
+# and when built explicitly put it in the data partition
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_JAVA_LIBRARIES := android.test.runner
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_PACKAGE_NAME := CtsGraphics2TestCases
+
+LOCAL_INSTRUMENTATION_FOR :=
+
+LOCAL_SDK_VERSION := current
+
+include $(BUILD_PACKAGE)
diff --git a/tests/tests/graphics2/AndroidManifest.xml b/tests/tests/graphics2/AndroidManifest.xml
new file mode 100644
index 0000000..e26b962
--- /dev/null
+++ b/tests/tests/graphics2/AndroidManifest.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2012 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.graphics2"
+    android:versionCode="1"
+    android:versionName="1.0" >
+
+    <uses-permission android:name="android.permission.CAMERA" />
+    <uses-feature android:name="android.hardware.camera" />
+
+    <instrumentation
+        android:targetPackage="com.android.cts.graphics2"
+        android:name="android.test.InstrumentationTestRunner" />
+
+    <application>
+        <uses-library android:name="android.test.runner" />
+        <activity android:name="android.graphics2.cts.TextureViewCameraActivity">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>
diff --git a/tests/tests/graphics2/src/android/graphics2/cts/TextureViewCameraActivity.java b/tests/tests/graphics2/src/android/graphics2/cts/TextureViewCameraActivity.java
new file mode 100644
index 0000000..d947565
--- /dev/null
+++ b/tests/tests/graphics2/src/android/graphics2/cts/TextureViewCameraActivity.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2012 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.graphics2.cts;
+
+import android.app.Activity;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Matrix;
+import android.graphics.SurfaceTexture;
+import android.hardware.Camera;
+import android.os.Bundle;
+import android.view.TextureView;
+import android.view.View;
+
+import java.io.IOException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import junit.framework.Assert;
+
+
+public class TextureViewCameraActivity extends Activity implements
+        TextureView.SurfaceTextureListener {
+    private static final int CAPTURE_SCREEN_INTERVAL = 10;
+    private static final float SCREEN_ROTATION_RATE = 1.0f;
+    private static final int MAX_FRAME_UPDATE = 40;
+    private Camera mCamera;
+    private TextureView mTextureView;
+    private int mUpdateCounter = 0;
+    private int mWidth;
+    private int mHeight;
+    private float mRotation = 0f;
+    private final CountDownLatch mLatch = new CountDownLatch(1);
+
+    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
+        Assert.assertTrue(mTextureView.getLayerType() == View.LAYER_TYPE_HARDWARE);
+        Assert.assertTrue(mTextureView.isAvailable());
+        Assert.assertNotNull(mTextureView.getSurfaceTexture());
+        Assert.assertTrue(mTextureView.getSurfaceTextureListener() == this);
+        Assert.assertTrue(mTextureView.isOpaque());
+        mWidth = width;
+        mHeight = height;
+        mCamera = Camera.open();
+
+        try {
+            mCamera.setPreviewTexture(surface);
+            mCamera.startPreview();
+        } catch (IOException ioe) {
+            // Something bad happened
+            Assert.fail();
+        }
+    }
+
+    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
+        mWidth = width;
+        mHeight = height;
+    }
+
+    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
+        mCamera.stopPreview();
+        mCamera.release();
+        return true;
+    }
+
+    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
+        mUpdateCounter++;
+        if (mUpdateCounter % CAPTURE_SCREEN_INTERVAL == 0) {
+            Canvas canvas = mTextureView.lockCanvas();
+            canvas.drawColor(0xffff0000);
+            mTextureView.unlockCanvasAndPost(canvas);
+            Bitmap bitmap = mTextureView.getBitmap();
+            Assert.assertEquals(mHeight, bitmap.getHeight());
+            Assert.assertEquals(mWidth, bitmap.getWidth());
+            bitmap.recycle();
+            if (mUpdateCounter >= MAX_FRAME_UPDATE) {
+                mLatch.countDown();
+            }
+        }
+        Matrix transformMatrix =  mTextureView.getTransform(null);
+        mRotation += SCREEN_ROTATION_RATE;
+        transformMatrix.setRotate(mRotation, mWidth/2, mHeight/2);
+        mTextureView.setTransform(transformMatrix);
+    }
+
+    public boolean waitForCompletion(long timeoutInSecs) throws InterruptedException {
+        return mLatch.await(timeoutInSecs, TimeUnit.SECONDS);
+    }
+
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        mTextureView = new TextureView(this);
+        mTextureView.setSurfaceTextureListener(this);
+        mTextureView.setOpaque(true);
+        setContentView(mTextureView);
+    }
+}
diff --git a/tests/tests/graphics2/src/android/graphics2/cts/TextureViewTest.java b/tests/tests/graphics2/src/android/graphics2/cts/TextureViewTest.java
new file mode 100644
index 0000000..932a5d5
--- /dev/null
+++ b/tests/tests/graphics2/src/android/graphics2/cts/TextureViewTest.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2012 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.graphics2.cts;
+
+import android.graphics2.cts.TextureViewCameraActivity;
+import android.test.ActivityInstrumentationTestCase2;
+
+
+public class TextureViewTest extends ActivityInstrumentationTestCase2<TextureViewCameraActivity> {
+    private static final long WAIT_TIMEOUT_IN_SECS = 10;
+    private TextureViewCameraActivity mActivity;
+    public TextureViewTest() {
+        super(TextureViewCameraActivity.class);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mActivity = getActivity();
+    }
+
+    public void testTextureViewActivity() throws InterruptedException {
+        assertTrue(mActivity.waitForCompletion(WAIT_TIMEOUT_IN_SECS));
+    }
+
+}
+