Merge "Camera2: Update tests to match latest API" into klp-dev
diff --git a/tests/src/android/renderscript/cts/group1.rs b/tests/src/android/renderscript/cts/group1.rs
new file mode 100644
index 0000000..f1172dc
--- /dev/null
+++ b/tests/src/android/renderscript/cts/group1.rs
@@ -0,0 +1,13 @@
+#pragma version(1)
+#pragma rs java_package_name(android.renderscript.cts)
+
+#include "shared.rsh"
+
+rs_allocation aSharedInt;
+
+uint32_t  __attribute__((kernel)) setSharedInt(uint32_t x) {
+    if (x == 1) {
+        rsSetElementAt_int(aSharedInt, -5, 0);
+    }
+    return x;
+}
diff --git a/tests/src/android/renderscript/cts/group2.rs b/tests/src/android/renderscript/cts/group2.rs
new file mode 100644
index 0000000..f7b62dd
--- /dev/null
+++ b/tests/src/android/renderscript/cts/group2.rs
@@ -0,0 +1,32 @@
+#pragma version(1)
+#pragma rs java_package_name(android.renderscript.cts)
+
+#include "shared.rsh"
+
+rs_allocation aSharedInt;
+rs_allocation aFailed;
+
+static bool failed[2] = { false, false };
+
+void __attribute__((kernel)) getSharedInt(uint32_t in, uint32_t x) {
+    int v = rsGetElementAt_int(aSharedInt, 0);
+    if (in != x) {
+        rsDebug("Failed to read in on iteration: ", x);
+        rsDebug("Read: ", in);
+        failed[x] = true;
+    }
+    if (v != -5) {
+        rsDebug("Failed to read -5 on iteration: ", x);
+        rsDebug("Read: ", v);
+        failed[x] = true;
+    }
+}
+
+// Write out aFailed if either of our kernel instances read old data.
+void verify() {
+    for (int i = 0; i < 2; i++) {
+        if (failed[i]) {
+            rsSetElementAt_int(aFailed, 1, 0);
+        }
+    }
+}
diff --git a/tests/tests/renderscript/src/android/renderscript/cts/ScriptGroupTest.java b/tests/tests/renderscript/src/android/renderscript/cts/ScriptGroupTest.java
index 64496ef..c9a79c8 100644
--- a/tests/tests/renderscript/src/android/renderscript/cts/ScriptGroupTest.java
+++ b/tests/tests/renderscript/src/android/renderscript/cts/ScriptGroupTest.java
@@ -202,13 +202,13 @@
         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);
+        node1 = new ScriptC_scriptgroup(mRS);
+        node2 = new ScriptC_scriptgroup(mRS);
+        node3 = new ScriptC_scriptgroup(mRS);
+        node4 = new ScriptC_scriptgroup(mRS);
+        node5 = new ScriptC_scriptgroup(mRS);
 
-        compare = new ScriptC_scriptgroup(mRS, mRes, R.raw.scriptgroup);
+        compare = new ScriptC_scriptgroup(mRS);
 
         Allocation in1, in2, out, resultAlloc;
         in1 = Allocation.createTyped(mRS, connect);
@@ -265,4 +265,50 @@
         assertTrue(result[0] == 2);
     }
 
+    /**
+     * Tests a case where a shared global variable is updated by the first kernel in a group,
+     * but then read by a subsequent kernel.
+     *
+     * The test ensures that we don't accidentally apply any fusion optimizations to the kernel
+     * pair, since there is a potential dependency that crosses the kernel cell boundary.
+     */
+    public void testScriptGroupSharedGlobal() {
+        Type i32 = new Type.Builder(mRS, Element.I32(mRS)).setX(1).create();
+        Type u32 = new Type.Builder(mRS, Element.U32(mRS)).setX(2).create();
+
+        Allocation aFailed = Allocation.createTyped(mRS, i32);
+        Allocation aSharedInt = Allocation.createTyped(mRS, i32);
+
+        ScriptC_group1 mG1 = new ScriptC_group1(mRS);
+        ScriptC_group2 mG2 = new ScriptC_group2(mRS);
+
+        mG1.set_aSharedInt(aSharedInt);
+        mG2.set_aSharedInt(aSharedInt);
+        mG2.set_aFailed(aFailed);
+
+        int [] Failed = new int [1];
+        Failed[0] = 0;
+        aFailed.copyFrom(Failed);
+
+        ScriptGroup.Builder b = new ScriptGroup.Builder(mRS);
+
+        // Writes to aSharedInt[x] in the kernel.
+        b.addKernel(mG1.getKernelID_setSharedInt());
+        // Reads aSharedInt[1] to verify it is -5.
+        b.addKernel(mG2.getKernelID_getSharedInt());
+        // If we fuse mG1/mG2, we won't see the update to the aSharedInt[1] during mG2 for x == 0.
+        // The update is only visible if we correctly identify the dependency and execute all of
+        // mG1 before starting on mG2.
+        b.addConnection(u32, mG1.getKernelID_setSharedInt(), mG2.getKernelID_getSharedInt());
+        ScriptGroup group = b.create();
+        group.execute();
+
+        mG2.invoke_verify();
+        aFailed.copyTo(Failed);
+        if (Failed[0] != 0) {
+            FoundError = true;
+        }
+
+        checkForErrors();
+    }
 }
diff --git a/tests/tests/webkit/src/android/webkit/cts/GeolocationTest.java b/tests/tests/webkit/src/android/webkit/cts/GeolocationTest.java
index 85a616f..e2166d8 100644
--- a/tests/tests/webkit/src/android/webkit/cts/GeolocationTest.java
+++ b/tests/tests/webkit/src/android/webkit/cts/GeolocationTest.java
@@ -43,6 +43,7 @@
 import java.io.UnsupportedEncodingException;
 import java.util.concurrent.Callable;
 import java.util.Date;
+import java.util.List;
 import java.util.Random;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -65,7 +66,6 @@
     private static final String JS_INTERFACE_NAME = "Android";
     private static final int POLLING_TIMEOUT = 60 * 1000;
     private static final int LOCATION_THREAD_UPDATE_WAIT_MS = 250;
-    private static final String PROVIDER_NAME = LocationManager.NETWORK_PROVIDER;
 
     // static HTML page always injected instead of the url loaded
     private static final String RAW_HTML =
@@ -105,6 +105,7 @@
     private WebViewOnUiThread mOnUiThread;
     private Thread mLocationUpdateThread;
     private volatile boolean mLocationUpdateThreadExitRequested;
+    private List<String> mProviders;
 
     public GeolocationTest() throws Exception {
         super("com.android.cts.stub", WebViewStubActivity.class);
@@ -154,21 +155,48 @@
         mLocationManager = (LocationManager)getActivity().getApplicationContext()
                 .getSystemService(Context.LOCATION_SERVICE);
         // Add a test provider before each test to inject a location
-        addTestProvider(PROVIDER_NAME);
+        mProviders = mLocationManager.getAllProviders();
+        for (String provider : mProviders) {
+            // Can't mock passive provider.
+            if (provider.equals(LocationManager.PASSIVE_PROVIDER)) {
+                mProviders.remove(provider);
+                break;
+            }
+        }
+        addTestProviders();
     }
 
     @Override
     protected void tearDown() throws Exception {
         stopUpdateLocationThread();
         // Remove the test provider after each test
-        try {
-            mLocationManager.removeTestProvider(PROVIDER_NAME);
-        } catch (IllegalArgumentException e) {} // Not much to do about this
+        for (String provider : mProviders) {
+            try {
+                mLocationManager.removeTestProvider(provider);
+            } catch (IllegalArgumentException e) {} // Not much to do about this
+        }
         mOnUiThread.cleanUp();
         // This will null all member and static variables
         super.tearDown();
     }
 
+    private void addTestProviders() {
+        for (String providerName : mProviders) {
+            LocationProvider provider = mLocationManager.getProvider(providerName);
+            mLocationManager.addTestProvider(provider.getName(),
+                    provider.requiresNetwork(), //requiresNetwork,
+                    provider.requiresSatellite(), // requiresSatellite,
+                    provider.requiresCell(),  // requiresCell,
+                    provider.hasMonetaryCost(), // hasMonetaryCost,
+                    provider.supportsAltitude(), // supportsAltitude,
+                    provider.supportsSpeed(), // supportsSpeed,
+                    provider.supportsBearing(), // supportsBearing,
+                    provider.getPowerRequirement(), // powerRequirement
+                    provider.getAccuracy()); // accuracy
+            mLocationManager.setTestProviderEnabled(provider.getName(), true);
+        }
+    }
+
     private void startUpdateLocationThread() {
         // Only start the thread once
         if (mLocationUpdateThread == null) {
@@ -182,7 +210,7 @@
                         } catch(Exception e) {
                             // Do nothing, an extra update is no problem
                         }
-                        updateLocation(PROVIDER_NAME);
+                        updateLocation();
                     }
                 }
             };
@@ -204,14 +232,16 @@
     }
 
     // Update location with a fixed latitude and longtitude, sets the time to the current time.
-    private void updateLocation(final String providerName) {
-        Location location = new Location(providerName);
-        location.setLatitude(40);
-        location.setLongitude(40);
-        location.setAccuracy(1.0f);
-        location.setTime(java.lang.System.currentTimeMillis());
-        location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
-        mLocationManager.setTestProviderLocation(providerName, location);
+    private void updateLocation() {
+        for (int i = 0; i < mProviders.size(); i++) {
+            Location location = new Location(mProviders.get(i));
+            location.setLatitude(40);
+            location.setLongitude(40);
+            location.setAccuracy(1.0f);
+            location.setTime(java.lang.System.currentTimeMillis());
+            location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
+            mLocationManager.setTestProviderLocation(mProviders.get(i), location);
+        }
     }
 
     // Need to set the location just after loading the url. Setting it after each load instead of
@@ -244,20 +274,6 @@
         }
     }
 
-    private void addTestProvider(final String providerName) {
-        mLocationManager.addTestProvider(providerName,
-                true, //requiresNetwork,
-                false, // requiresSatellite,
-                true,  // requiresCell,
-                false, // hasMonetaryCost,
-                false, // supportsAltitude,
-                false, // supportsSpeed,
-                false, // supportsBearing,
-                Criteria.POWER_MEDIUM, // powerRequirement
-                Criteria.ACCURACY_FINE); // accuracy
-        mLocationManager.setTestProviderEnabled(providerName, true);
-    }
-
     // Test loading a page and accepting the domain for one load
     public void testSimpleGeolocationRequestAcceptOnce() throws Exception {
         final TestSimpleGeolocationRequestWebChromeClient chromeClientAcceptOnce =
diff --git a/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java b/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
index 00e2f90..827bf27 100644
--- a/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
+++ b/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
@@ -1112,27 +1112,18 @@
         }.run();
         getInstrumentation().waitForIdleSync();
 
-        int previousScrollX = mOnUiThread.getScrollX();
-        int previousScrollY = mOnUiThread.getScrollY();
+        final int previousScrollX = mOnUiThread.getScrollX();
+        final int previousScrollY = mOnUiThread.getScrollY();
 
         mOnUiThread.flingScroll(100, 100);
 
-        int timeSlice = 500;
-        Thread.sleep(timeSlice);
-        assertTrue(mOnUiThread.getScrollX() > previousScrollX);
-        assertTrue(mOnUiThread.getScrollY() > previousScrollY);
-
-        previousScrollY = mOnUiThread.getScrollY();
-        previousScrollX = mOnUiThread.getScrollX();
-        Thread.sleep(timeSlice);
-        assertTrue(mOnUiThread.getScrollX() >= previousScrollX);
-        assertTrue(mOnUiThread.getScrollY() >= previousScrollY);
-
-        previousScrollY = mOnUiThread.getScrollY();
-        previousScrollX = mOnUiThread.getScrollX();
-        Thread.sleep(timeSlice);
-        assertTrue(mOnUiThread.getScrollX() >= previousScrollX);
-        assertTrue(mOnUiThread.getScrollY() >= previousScrollY);
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return mOnUiThread.getScrollX() > previousScrollX &&
+                        mOnUiThread.getScrollY() > previousScrollY;
+            }
+        }.run();
     }
 
     public void testRequestFocusNodeHref() throws Throwable {