Merge "Add configChange screenSize" into jb-mr1-dev
diff --git a/CtsTestCaseList.mk b/CtsTestCaseList.mk
index c93fafb..8c2b798 100644
--- a/CtsTestCaseList.mk
+++ b/CtsTestCaseList.mk
@@ -25,7 +25,8 @@
 	CtsSimpleAppInstallDiffCert \
 	CtsTargetInstrumentationApp \
 	CtsUsePermissionDiffCert \
-	CtsWriteExternalStorageApp
+	CtsWriteExternalStorageApp \
+	CtsMultiUserStorageApp
 
 cts_support_packages := \
 	CtsAccelerationTestStubs \
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/location/LocationVerifier.java b/apps/CtsVerifier/src/com/android/cts/verifier/location/LocationVerifier.java
index 56a3b44..be0ef01 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/location/LocationVerifier.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/location/LocationVerifier.java
@@ -45,12 +45,14 @@
     private int mNumActiveUpdates = 0;
     private int mNumPassiveUpdates = 0;
     private boolean mRunning = false;
+    private boolean mActiveLocationArrive = false;
 
     private class ActiveListener implements LocationListener {
         @Override
         public void onLocationChanged(Location location) {
             if (!mRunning) return;
 
+            mActiveLocationArrive = true;
             mNumActiveUpdates++;
             scheduleTimeout();
 
@@ -103,6 +105,15 @@
             if (!mRunning) return;
             if (!location.getProvider().equals(mProvider)) return;
 
+            // When a test round start, passive listener shouldn't recevice location before active listener.
+            // If this situation occurs, we treat this location as overdue location.
+            // (The overdue location comes from previous test round, it occurs occasionally)
+            // We have to skip it to prevent wrong calculation of time interval.
+            if (!mActiveLocationArrive) {
+                mCb.log("ignoring passive " + mProvider + " update");
+                return;
+            }
+
             mNumPassiveUpdates++;
             long timestamp = location.getTime();
             long delta = timestamp - mLastPassiveTimestamp;
diff --git a/hostsidetests/appsecurity/src/com/android/cts/appsecurity/AppSecurityTests.java b/hostsidetests/appsecurity/src/com/android/cts/appsecurity/AppSecurityTests.java
index 5c28a4f4..7e65be3 100644
--- a/hostsidetests/appsecurity/src/com/android/cts/appsecurity/AppSecurityTests.java
+++ b/hostsidetests/appsecurity/src/com/android/cts/appsecurity/AppSecurityTests.java
@@ -19,15 +19,17 @@
 import com.android.cts.tradefed.build.CtsBuildHelper;
 import com.android.ddmlib.IDevice;
 import com.android.ddmlib.Log;
+import com.android.ddmlib.testrunner.InstrumentationResultParser;
 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
 import com.android.ddmlib.testrunner.TestIdentifier;
 import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.device.CollectingOutputReceiver;
 import com.android.tradefed.device.DeviceNotAvailableException;
 import com.android.tradefed.device.ITestDevice;
 import com.android.tradefed.result.CollectingTestListener;
 import com.android.tradefed.result.TestResult;
-import com.android.tradefed.result.TestRunResult;
 import com.android.tradefed.result.TestResult.TestStatus;
+import com.android.tradefed.result.TestRunResult;
 import com.android.tradefed.testtype.DeviceTestCase;
 import com.android.tradefed.testtype.IBuildReceiver;
 
@@ -94,6 +96,11 @@
 
     private static final String READ_EXTERNAL_STORAGE = "android.permission.READ_EXTERNAL_STORAGE";
 
+    private static final String MULTIUSER_STORAGE_APK = "CtsMultiUserStorageApp.apk";
+    private static final String MULTIUSER_STORAGE_PKG = "com.android.cts.multiuserstorageapp";
+    private static final String MULTIUSER_STORAGE_CLASS = MULTIUSER_STORAGE_PKG
+            + ".MultiUserStorageTest";
+
     private static final String LOG_TAG = "AppSecurityTests";
 
     private CtsBuildHelper mCtsBuild;
@@ -242,6 +249,25 @@
     }
 
     /**
+     * Verify that legacy filesystem paths continue working, and that they all
+     * point to same location.
+     */
+    public void testExternalStorageLegacyPaths() throws Exception {
+        try {
+            getDevice().uninstallPackage(WRITE_EXTERNAL_STORAGE_APP_PKG);
+            assertNull(getDevice()
+                    .installPackage(getTestAppFile(WRITE_EXTERNAL_STORAGE_APP_APK), false));
+
+            assertTrue("Failed to verify legacy filesystem paths", runDeviceTests(
+                    WRITE_EXTERNAL_STORAGE_APP_PKG, WRITE_EXTERNAL_STORAGE_APP_CLASS,
+                    "testLegacyPaths"));
+
+        } finally {
+            getDevice().uninstallPackage(WRITE_EXTERNAL_STORAGE_APP_PKG);
+        }
+    }
+
+    /**
      * Test that uninstall of an app removes its private data.
      */
     public void testUninstallRemovesData() throws Exception {
@@ -346,6 +372,73 @@
     }
 
     /**
+     * Test multi-user emulated storage environment, ensuring that each user has
+     * isolated storage minus shared OBB directory.
+     */
+    public void testMultiUserStorage() throws Exception {
+        final String PACKAGE = MULTIUSER_STORAGE_PKG;
+        final String CLAZZ = MULTIUSER_STORAGE_CLASS;
+
+        if (!isMultiUserSupportedOnDevice(getDevice())) {
+            Log.d(LOG_TAG, "Single user device; skipping isolated storage tests");
+            return;
+        }
+
+        int owner = 0;
+        int secondary = -1;
+        try {
+            // Create secondary user
+            secondary = createUserOnDevice(getDevice());
+
+            // Install our test app
+            getDevice().uninstallPackage(MULTIUSER_STORAGE_PKG);
+            final String installResult = getDevice()
+                    .installPackage(getTestAppFile(MULTIUSER_STORAGE_APK), false);
+            assertNull("Failed to install: " + installResult, installResult);
+
+            // Clear data from previous tests
+            assertDeviceTestsPass(
+                    doRunTestsAsUser(PACKAGE, CLAZZ, "cleanIsolatedStorage", owner));
+            assertDeviceTestsPass(
+                    doRunTestsAsUser(PACKAGE, CLAZZ, "cleanIsolatedStorage", secondary));
+
+            // Have both users try writing into isolated storage
+            assertDeviceTestsPass(
+                    doRunTestsAsUser(PACKAGE, CLAZZ, "writeIsolatedStorage", owner));
+            assertDeviceTestsPass(
+                    doRunTestsAsUser(PACKAGE, CLAZZ, "writeIsolatedStorage", secondary));
+
+            // Verify they both have isolated view of storage
+            assertDeviceTestsPass(
+                    doRunTestsAsUser(PACKAGE, CLAZZ, "readIsolatedStorage", owner));
+            assertDeviceTestsPass(
+                    doRunTestsAsUser(PACKAGE, CLAZZ, "readIsolatedStorage", secondary));
+
+            // Clear data from previous tests
+            assertDeviceTestsPass(
+                    doRunTestsAsUser(PACKAGE, CLAZZ, "cleanObbStorage", owner));
+            assertDeviceTestsPass(
+                    doRunTestsAsUser(PACKAGE, CLAZZ, "cleanObbStorage", secondary));
+
+            // Only write data as owner
+            assertDeviceTestsPass(
+                    doRunTestsAsUser(PACKAGE, CLAZZ, "writeObbStorage", owner));
+
+            // Verify that both users can see shared OBB data
+            assertDeviceTestsPass(
+                    doRunTestsAsUser(PACKAGE, CLAZZ, "readObbStorage", owner));
+            assertDeviceTestsPass(
+                    doRunTestsAsUser(PACKAGE, CLAZZ, "readObbStorage", secondary));
+
+        } finally {
+            getDevice().uninstallPackage(MULTIUSER_STORAGE_PKG);
+            if (secondary != -1) {
+                removeUserOnDevice(getDevice(), secondary);
+            }
+        }
+    }
+
+    /**
      * Helper method that checks that all tests in given result passed, and attempts to generate
      * a meaningful error message if they failed.
      *
@@ -415,6 +508,58 @@
         return listener.getCurrentRunResults();
     }
 
+    private static boolean isMultiUserSupportedOnDevice(ITestDevice device)
+            throws DeviceNotAvailableException {
+        // TODO: move this to ITestDevice once it supports users
+        final String output = device.executeShellCommand("pm get-max-users");
+        try {
+            return Integer.parseInt(output.substring(output.lastIndexOf(" ")).trim()) > 1;
+        } catch (NumberFormatException e) {
+            fail("Failed to parse result: " + output);
+        }
+        return false;
+    }
+
+    private static int createUserOnDevice(ITestDevice device) throws DeviceNotAvailableException {
+        // TODO: move this to ITestDevice once it supports users
+        final String name = "CTS_" + System.currentTimeMillis();
+        final String output = device.executeShellCommand("pm create-user " + name);
+        if (output.startsWith("Success")) {
+            try {
+                return Integer.parseInt(output.substring(output.lastIndexOf(" ")).trim());
+            } catch (NumberFormatException e) {
+                fail("Failed to parse result: " + output);
+            }
+        } else {
+            fail("Failed to create user: " + output);
+        }
+        throw new IllegalStateException();
+    }
+
+    private static void removeUserOnDevice(ITestDevice device, int userId)
+            throws DeviceNotAvailableException {
+        // TODO: move this to ITestDevice once it supports users
+        final String output = device.executeShellCommand("pm remove-user " + userId);
+        if (output.startsWith("Error")) {
+            fail("Failed to remove user: " + output);
+        }
+    }
+
+    private TestRunResult doRunTestsAsUser(
+            String pkgName, String testClassName, String testMethodName, int userId)
+            throws DeviceNotAvailableException {
+        // TODO: move this to RemoteAndroidTestRunner once it supports users
+        final String cmd = "am instrument --user " + userId + " -w -r -e class " + testClassName
+                + "#" + testMethodName + " " + pkgName + "/android.test.InstrumentationTestRunner";
+        Log.i(LOG_TAG, "Running " + cmd + " on " + getDevice().getSerialNumber());
+
+        CollectingTestListener listener = new CollectingTestListener();
+        InstrumentationResultParser parser = new InstrumentationResultParser(pkgName, listener);
+
+        getDevice().executeShellCommand(cmd, parser);
+        return listener.getCurrentRunResults();
+    }
+
     private static void setPermissionEnforced(
             ITestDevice device, String permission, boolean enforced)
             throws DeviceNotAvailableException {
diff --git a/hostsidetests/appsecurity/test-apps/MultiUserStorageApp/Android.mk b/hostsidetests/appsecurity/test-apps/MultiUserStorageApp/Android.mk
new file mode 100644
index 0000000..8781e8b
--- /dev/null
+++ b/hostsidetests/appsecurity/test-apps/MultiUserStorageApp/Android.mk
@@ -0,0 +1,27 @@
+# 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)
+
+LOCAL_MODULE_TAGS := tests
+LOCAL_SDK_VERSION := 16
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+LOCAL_PACKAGE_NAME := CtsMultiUserStorageApp
+
+LOCAL_DEX_PREOPT := false
+
+include $(BUILD_PACKAGE)
diff --git a/hostsidetests/appsecurity/test-apps/MultiUserStorageApp/AndroidManifest.xml b/hostsidetests/appsecurity/test-apps/MultiUserStorageApp/AndroidManifest.xml
new file mode 100644
index 0000000..6ace31b
--- /dev/null
+++ b/hostsidetests/appsecurity/test-apps/MultiUserStorageApp/AndroidManifest.xml
@@ -0,0 +1,28 @@
+<?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.multiuserstorageapp">
+
+    <application>
+        <uses-library android:name="android.test.runner" />
+    </application>
+
+    <instrumentation android:name="android.test.InstrumentationTestRunner"
+        android:targetPackage="com.android.cts.multiuserstorageapp" />
+
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+
+</manifest>
diff --git a/hostsidetests/appsecurity/test-apps/MultiUserStorageApp/src/com/android/cts/multiuserstorageapp/MultiUserStorageTest.java b/hostsidetests/appsecurity/test-apps/MultiUserStorageApp/src/com/android/cts/multiuserstorageapp/MultiUserStorageTest.java
new file mode 100644
index 0000000..7e2d3ed
--- /dev/null
+++ b/hostsidetests/appsecurity/test-apps/MultiUserStorageApp/src/com/android/cts/multiuserstorageapp/MultiUserStorageTest.java
@@ -0,0 +1,105 @@
+/*
+ * 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 com.android.cts.multiuserstorageapp;
+
+import android.os.Environment;
+import android.test.AndroidTestCase;
+import android.util.Log;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+/**
+ * Test multi-user emulated storage environment, ensuring that each user has
+ * isolated storage minus shared OBB directory.
+ */
+public class MultiUserStorageTest extends AndroidTestCase {
+    private static final String TAG = "MultiUserStorageTest";
+
+    private static final String FILE_PREFIX = "MUST_";
+
+    private static final int MAGIC_VALUE = 16785407;
+
+    private final File mTargetSame = new File(
+            Environment.getExternalStorageDirectory(), FILE_PREFIX + "same");
+    private final File mTargetUid = new File(
+            Environment.getExternalStorageDirectory(), FILE_PREFIX + android.os.Process.myUid());
+
+    private File getFileObbSame() {
+        return new File(getContext().getObbDir(), FILE_PREFIX + "obb_same");
+    }
+
+    private void wipeTestFiles(File dir) {
+        dir.mkdirs();
+        for (File file : dir.listFiles()) {
+            if (file.getName().startsWith(FILE_PREFIX)) {
+                Log.d(TAG, "Wiping " + file);
+                file.delete();
+            }
+        }
+    }
+
+    public void cleanIsolatedStorage() throws Exception {
+        wipeTestFiles(Environment.getExternalStorageDirectory());
+    }
+
+    public void writeIsolatedStorage() throws Exception {
+        writeInt(mTargetSame, android.os.Process.myUid());
+        writeInt(mTargetUid, android.os.Process.myUid());
+    }
+
+    public void readIsolatedStorage() throws Exception {
+        // Expect that the value we wrote earlier is still valid and wasn't
+        // overwritten by us running as another user.
+        assertEquals(android.os.Process.myUid(), readInt(mTargetSame));
+        assertEquals(android.os.Process.myUid(), readInt(mTargetUid));
+    }
+
+    public void cleanObbStorage() throws Exception {
+        wipeTestFiles(getContext().getObbDir());
+    }
+
+    public void writeObbStorage() throws Exception {
+        writeInt(getFileObbSame(), MAGIC_VALUE);
+    }
+
+    public void readObbStorage() throws Exception {
+        assertEquals(MAGIC_VALUE, readInt(getFileObbSame()));
+    }
+
+    private static void writeInt(File file, int value) throws IOException {
+        final DataOutputStream os = new DataOutputStream(new FileOutputStream(file));
+        try {
+            os.writeInt(value);
+        } finally {
+            os.close();
+        }
+    }
+
+    private static int readInt(File file) throws IOException {
+        final DataInputStream is = new DataInputStream(new FileInputStream(file));
+        try {
+            return is.readInt();
+        } finally {
+            is.close();
+        }
+    }
+}
diff --git a/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/src/com/android/cts/writeexternalstorageapp/WriteExternalStorageTest.java b/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/src/com/android/cts/writeexternalstorageapp/WriteExternalStorageTest.java
index b899bb0..075fe2e 100644
--- a/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/src/com/android/cts/writeexternalstorageapp/WriteExternalStorageTest.java
+++ b/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/src/com/android/cts/writeexternalstorageapp/WriteExternalStorageTest.java
@@ -19,48 +19,49 @@
 import android.os.Environment;
 import android.test.AndroidTestCase;
 
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import java.util.Random;
 
 /**
  * Test if {@link Environment#getExternalStorageDirectory()} is writable.
  */
 public class WriteExternalStorageTest extends AndroidTestCase {
 
-    private static final String TEST_FILE = "meow";
+    private static final File TEST_FILE = new File(
+            Environment.getExternalStorageDirectory(), "meow");
 
-    private void assertExternalStorageMounted() {
-        assertEquals(Environment.MEDIA_MOUNTED, Environment.getExternalStorageState());
-    }
+    /**
+     * Set of file paths that should all refer to the same location to verify
+     * support for legacy paths.
+     */
+    private static final File[] IDENTICAL_FILES = {
+            new File("/sdcard/caek"),
+            new File("/mnt/sdcard/caek"),
+            new File("/storage/sdcard0/caek"),
+            new File(Environment.getExternalStorageDirectory(), "caek"),
+    };
 
-    private void readExternalStorage() throws IOException {
-        final File file = new File(Environment.getExternalStorageDirectory(), TEST_FILE);
-        final InputStream is = new FileInputStream(file);
+    @Override
+    protected void tearDown() throws Exception {
         try {
-            is.read();
+            TEST_FILE.delete();
+            for (File file : IDENTICAL_FILES) {
+                file.delete();
+            }
         } finally {
-            is.close();
-        }
-    }
-
-    private void writeExternalStorage() throws IOException {
-        final File file = new File(Environment.getExternalStorageDirectory(), TEST_FILE);
-        final OutputStream os = new FileOutputStream(file);
-        try {
-            os.write(32);
-        } finally {
-            os.close();
+            super.tearDown();
         }
     }
 
     public void testReadExternalStorage() throws Exception {
         assertExternalStorageMounted();
         try {
-            readExternalStorage();
+            writeInt(TEST_FILE, 32);
         } catch (IOException e) {
             fail("unable to read external file");
         }
@@ -69,10 +70,54 @@
     public void testWriteExternalStorage() throws Exception {
         assertExternalStorageMounted();
         try {
-            writeExternalStorage();
+            assertEquals(readInt(TEST_FILE), 32);
         } catch (IOException e) {
             fail("unable to read external file");
         }
     }
 
+    /**
+     * Verify that legacy filesystem paths continue working, and that they all
+     * point to same location.
+     */
+    public void testLegacyPaths() throws Exception {
+        final Random r = new Random();
+        for (File target : IDENTICAL_FILES) {
+            // Ensure we're starting with clean slate
+            for (File file : IDENTICAL_FILES) {
+                file.delete();
+            }
+
+            // Write value to our current target
+            final int value = r.nextInt();
+            writeInt(target, value);
+
+            // Ensure that identical files all contain the value
+            for (File file : IDENTICAL_FILES) {
+                assertEquals(readInt(file), value);
+            }
+        }
+    }
+
+    private static void assertExternalStorageMounted() {
+        assertEquals(Environment.MEDIA_MOUNTED, Environment.getExternalStorageState());
+    }
+
+    private static void writeInt(File file, int value) throws IOException {
+        final DataOutputStream os = new DataOutputStream(new FileOutputStream(file));
+        try {
+            os.writeInt(value);
+        } finally {
+            os.close();
+        }
+    }
+
+    private static int readInt(File file) throws IOException {
+        final DataInputStream is = new DataInputStream(new FileInputStream(file));
+        try {
+            return is.readInt();
+        } finally {
+            is.close();
+        }
+    }
 }
diff --git a/tests/src/android/renderscript/cts/scriptgroup.rs b/tests/src/android/renderscript/cts/scriptgroup.rs
new file mode 100644
index 0000000..91527f8
--- /dev/null
+++ b/tests/src/android/renderscript/cts/scriptgroup.rs
@@ -0,0 +1,49 @@
+#pragma version(1)
+#pragma rs java_package_name(android.renderscript.cts)
+
+#include "shared.rsh"
+
+int memset_toValue = 0;
+
+int compare_value = 0;
+int compare_failure = 2;
+
+// 0 = +, 1 = -, 2 = *, 3 = /
+int arith_operation = 0;
+int arith_use_rs_allocation = 0;
+rs_allocation arith_rs_input;
+int arith_value = 0;
+
+void arith(const int *ain, int *aout, uint32_t x) {
+    int value = arith_value;
+
+    if (arith_use_rs_allocation)
+        value = *(int*)(rsGetElementAt(arith_rs_input, x));
+
+    if (arith_operation == 0) {
+        *aout = *ain + value;
+    } else if (arith_operation == 1) {
+        *aout = *ain - value;
+    } else if (arith_operation == 2) {
+        *aout = *ain * value;
+    } else if (arith_operation == 3) {
+        *aout = *ain / value;
+    }
+
+}
+
+void memset(int *aout) {
+    *aout = memset_toValue;
+    return;
+}
+
+void compare(const int *ain) {
+    if (*ain != compare_value) {
+        rsAtomicCas(&compare_failure, 2, -1);
+    }
+    return;
+}
+
+void getCompareResult(int* aout) {
+    *aout = compare_failure;
+}
diff --git a/tests/tests/provider/src/android/provider/cts/SettingsTest.java b/tests/tests/provider/src/android/provider/cts/SettingsTest.java
index 601efce..3ea47d4 100644
--- a/tests/tests/provider/src/android/provider/cts/SettingsTest.java
+++ b/tests/tests/provider/src/android/provider/cts/SettingsTest.java
@@ -180,8 +180,8 @@
         // Test that the secure table can be read from.
         Cursor cursor = null;
         try {
-            cursor = provider.query(Settings.Secure.CONTENT_URI, SECURE_PROJECTION,
-                    Settings.Secure.NAME + "=\"" + Settings.Secure.ADB_ENABLED + "\"",
+            cursor = provider.query(Settings.Global.CONTENT_URI, SECURE_PROJECTION,
+                    Settings.Global.NAME + "=\"" + Settings.Global.ADB_ENABLED + "\"",
                     null, null, null);
             assertNotNull(cursor);
         } finally {
diff --git a/tests/tests/renderscript/src/android/renderscript/cts/ScriptGroupTest.java b/tests/tests/renderscript/src/android/renderscript/cts/ScriptGroupTest.java
new file mode 100644
index 0000000..e73195c
--- /dev/null
+++ b/tests/tests/renderscript/src/android/renderscript/cts/ScriptGroupTest.java
@@ -0,0 +1,269 @@
+/*
+ * Copyright (C) 2011-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.renderscript.cts;
+
+import com.android.cts.stub.R;
+
+import android.graphics.Bitmap;
+import android.renderscript.Allocation;
+import android.renderscript.AllocationAdapter;
+import android.renderscript.Allocation.MipmapControl;
+import android.renderscript.Element;
+import android.renderscript.RSIllegalArgumentException;
+import android.renderscript.RSInvalidStateException;
+import android.renderscript.Type;
+import android.renderscript.Type.Builder;
+
+import android.renderscript.ScriptIntrinsicColorMatrix;
+import android.renderscript.ScriptIntrinsicConvolve3x3;
+import android.renderscript.ScriptGroup;
+import android.renderscript.Matrix4f;
+import android.util.Log;
+
+public class ScriptGroupTest extends RSBaseCompute {
+
+    static int bDimX = 48;
+    static int bDimY = 8;
+
+    public void testScriptGroupSingleKernel() {
+        ScriptGroup group;
+
+        Type connect = new Type.Builder(mRS, Element.U8_4(mRS)).setX(bDimX).setY(bDimY).create();
+
+        ScriptIntrinsicColorMatrix mColorMatrix;
+
+        mColorMatrix = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS));
+
+        Allocation a1_copy, a2_copy;
+        a1_copy = Allocation.createTyped(mRS, connect);
+        a2_copy = Allocation.createTyped(mRS, connect);
+
+        Matrix4f m = new Matrix4f();
+        m.set(1, 0, 0.2f);
+        m.set(1, 1, 0.9f);
+        m.set(1, 2, 0.2f);
+        mColorMatrix.setColorMatrix(m);
+
+        ScriptGroup.Builder b = new ScriptGroup.Builder(mRS);
+        b.addKernel(mColorMatrix.getKernelID());
+        group = b.create();
+
+        group.setInput(mColorMatrix.getKernelID(), a1_copy);
+        group.setOutput(mColorMatrix.getKernelID(), a2_copy);
+
+        group.execute();
+    }
+
+    public void testScriptGroupDisconnectedKernel() {
+        ScriptGroup group;
+
+        Type connect = new Type.Builder(mRS, Element.U8_4(mRS)).setX(bDimX).setY(bDimY).create();
+
+        ScriptIntrinsicColorMatrix mColorMatrix, mColorMatrix2;
+
+        mColorMatrix = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS));
+        mColorMatrix2 = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS));
+
+        Allocation a1_copy, a2_copy;
+
+        a1_copy = Allocation.createTyped(mRS, connect);
+        a2_copy = Allocation.createTyped(mRS, connect);
+
+        Matrix4f m = new Matrix4f();
+        m.set(1, 0, 0.2f);
+        m.set(1, 1, 0.9f);
+        m.set(1, 2, 0.2f);
+        mColorMatrix.setColorMatrix(m);
+        mColorMatrix2.setColorMatrix(m);
+
+        ScriptGroup.Builder b = new ScriptGroup.Builder(mRS);
+        b.addKernel(mColorMatrix.getKernelID());
+        b.addKernel(mColorMatrix2.getKernelID());
+        try {
+            group = b.create();
+            fail("should throw RSInvalidStateException.");
+        } catch (RSInvalidStateException e) {
+
+        }
+    }
+
+
+    public void testScriptGroupFieldConnection() {
+        ScriptGroup group;
+
+        Type connect = new Type.Builder(mRS, Element.U8_4(mRS)).setX(bDimX).setY(bDimY).create();
+
+        ScriptIntrinsicConvolve3x3 mConvolve3x3;
+        ScriptIntrinsicColorMatrix mColorMatrix;
+
+        mConvolve3x3 = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS));
+        mColorMatrix = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS));
+
+        Allocation a1_copy, a2_copy;
+        a1_copy = Allocation.createTyped(mRS, connect);
+        a2_copy = Allocation.createTyped(mRS, connect);
+
+        float f[] = new float[9];
+        f[0] =  0.f;    f[1] = -1.f;    f[2] =  0.f;
+        f[3] = -1.f;    f[4] =  5.f;    f[5] = -1.f;
+        f[6] =  0.f;    f[7] = -1.f;    f[8] =  0.f;
+
+        mConvolve3x3.setCoefficients(f);
+
+        Matrix4f m = new Matrix4f();
+        m.set(1, 0, 0.2f);
+        m.set(1, 1, 0.9f);
+        m.set(1, 2, 0.2f);
+        mColorMatrix.setColorMatrix(m);
+
+        ScriptGroup.Builder b = new ScriptGroup.Builder(mRS);
+        b.addKernel(mColorMatrix.getKernelID());
+        b.addKernel(mConvolve3x3.getKernelID());
+        b.addConnection(connect, mColorMatrix.getKernelID(), mConvolve3x3.getFieldID_Input());
+        group = b.create();
+
+        group.setInput(mColorMatrix.getKernelID(), a1_copy);
+        group.setOutput(mConvolve3x3.getKernelID(), a2_copy);
+
+        group.execute();
+
+    }
+
+    public void testScriptGroupDisconnectedDAG() {
+        ScriptGroup group;
+
+        Type connect = new Type.Builder(mRS, Element.U8_4(mRS)).setX(bDimX).setY(bDimY).create();
+
+        ScriptIntrinsicConvolve3x3 mConvolve3x3, mConvolve3x32;
+        ScriptIntrinsicColorMatrix mColorMatrix, mColorMatrix2;
+
+        mConvolve3x3 = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS));
+        mConvolve3x32 = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS));
+        mColorMatrix = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS));
+        mColorMatrix2 = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS));
+
+        Allocation a1_copy, a2_copy;
+        a1_copy = Allocation.createTyped(mRS, connect);
+        a2_copy = Allocation.createTyped(mRS, connect);
+
+        float f[] = new float[9];
+        f[0] =  0.f;    f[1] = -1.f;    f[2] =  0.f;
+        f[3] = -1.f;    f[4] =  5.f;    f[5] = -1.f;
+        f[6] =  0.f;    f[7] = -1.f;    f[8] =  0.f;
+
+        mConvolve3x3.setCoefficients(f);
+        mConvolve3x32.setCoefficients(f);
+
+        Matrix4f m = new Matrix4f();
+        m.set(1, 0, 0.2f);
+        m.set(1, 1, 0.9f);
+        m.set(1, 2, 0.2f);
+        mColorMatrix.setColorMatrix(m);
+        mColorMatrix2.setColorMatrix(m);
+
+        ScriptGroup.Builder b = new ScriptGroup.Builder(mRS);
+        b.addKernel(mColorMatrix.getKernelID());
+        b.addKernel(mColorMatrix2.getKernelID());
+        b.addKernel(mConvolve3x3.getKernelID());
+        b.addKernel(mConvolve3x32.getKernelID());
+        b.addConnection(connect, mColorMatrix.getKernelID(), mConvolve3x3.getFieldID_Input());
+        b.addConnection(connect, mColorMatrix2.getKernelID(), mConvolve3x32.getFieldID_Input());
+        try {
+            group = b.create();
+            fail("RSInvalidStateException expected");
+        } catch (RSInvalidStateException e) {
+
+        }
+
+    }
+
+    public void testScriptGroupTorture() {
+        ScriptGroup group;
+
+        int[] result = new int[1];
+
+        bDimX = 1;
+
+        Type connect = new Type.Builder(mRS, Element.I32(mRS)).setX(bDimX).create();
+        Type compareType = new Type.Builder(mRS, Element.I32(mRS)).create();
+
+        ScriptC_scriptgroup node1, node2, node3, node4, node5, compare;
+        node1 = new ScriptC_scriptgroup(mRS, mRes, R.raw.scriptgroup);
+        node2 = new ScriptC_scriptgroup(mRS, mRes, R.raw.scriptgroup);
+        node3 = new ScriptC_scriptgroup(mRS, mRes, R.raw.scriptgroup);
+        node4 = new ScriptC_scriptgroup(mRS, mRes, R.raw.scriptgroup);
+        node5 = new ScriptC_scriptgroup(mRS, mRes, R.raw.scriptgroup);
+
+        compare = new ScriptC_scriptgroup(mRS, mRes, R.raw.scriptgroup);
+
+        Allocation in1, in2, out, resultAlloc;
+        in1 = Allocation.createTyped(mRS, connect);
+        in2 = Allocation.createTyped(mRS, connect);
+
+        out = Allocation.createTyped(mRS, connect);
+        resultAlloc = Allocation.createTyped(mRS, compareType);
+
+        node1.set_memset_toValue(1);
+        node1.forEach_memset(in1);
+        node1.set_memset_toValue(2);
+        node1.forEach_memset(in2);
+
+        node1.set_arith_operation(2);
+        node2.set_arith_operation(1);
+        node3.set_arith_operation(0);
+        node4.set_arith_operation(0);
+        node5.set_arith_operation(1);
+
+        node3.set_arith_use_rs_allocation(1);
+        node4.set_arith_use_rs_allocation(1);
+
+        node1.set_arith_value(5);
+        node2.set_arith_value(3);
+        node5.set_arith_value(7);
+
+        ScriptGroup.Builder b = new ScriptGroup.Builder(mRS);
+        b.addKernel(node1.getKernelID_arith());
+        b.addKernel(node2.getKernelID_arith());
+        b.addKernel(node3.getKernelID_arith());
+        b.addKernel(node4.getKernelID_arith());
+        b.addKernel(node5.getKernelID_arith());
+
+        b.addConnection(connect, node1.getKernelID_arith(), node2.getKernelID_arith());
+        b.addConnection(connect, node1.getKernelID_arith(), node3.getFieldID_arith_rs_input());
+        b.addConnection(connect, node2.getKernelID_arith(), node4.getFieldID_arith_rs_input());
+        b.addConnection(connect, node3.getKernelID_arith(), node4.getKernelID_arith());
+        b.addConnection(connect, node4.getKernelID_arith(), node5.getKernelID_arith());
+
+        group = b.create();
+        group.setInput(node1.getKernelID_arith(), in1);
+        group.setInput(node3.getKernelID_arith(), in2);
+
+        group.setOutput(node5.getKernelID_arith(), out);
+
+        group.execute();
+
+        mRS.finish();
+
+        compare.set_compare_value(2);
+        compare.forEach_compare(out);
+        compare.forEach_getCompareResult(resultAlloc);
+        resultAlloc.copyTo(result);
+        Log.e("ARGH", "result = " + result[0]);
+        assertTrue(result[0] == 2);
+    }
+
+}