Validates Intrinsic Blur only takes 2D Allocations

Bug: 24555166

This is for support lib.

Test: RSTest on x86_64 emulator
Change-Id: If50d28fc12d2d7a2df38b427b6d82cb89493dce6
diff --git a/support/java/src/android/support/v8/renderscript/ScriptIntrinsicBlur.java b/support/java/src/android/support/v8/renderscript/ScriptIntrinsicBlur.java
index e1a134a..926d7d3 100644
--- a/support/java/src/android/support/v8/renderscript/ScriptIntrinsicBlur.java
+++ b/support/java/src/android/support/v8/renderscript/ScriptIntrinsicBlur.java
@@ -72,6 +72,9 @@
      * @param ain The input allocation
      */
     public void setInput(Allocation ain) {
+        if (ain.getType().getY() == 0) {
+            throw new RSIllegalArgumentException("Input set to a 1D Allocation");
+        }
         mInput = ain;
         setVar(1, ain);
     }
@@ -98,6 +101,9 @@
      *             type.
      */
     public void forEach(Allocation aout) {
+        if (aout.getType().getY() == 0) {
+            throw new RSIllegalArgumentException("Output is a 1D Allocation");
+        }
         forEach(0, (Allocation) null, aout, null);
     }
 
diff --git a/tests/java_api/RSTest_CompatLib/src/com/android/rs/test/RSTestCore.java b/tests/java_api/RSTest_CompatLib/src/com/android/rs/test/RSTestCore.java
index b16eb8d..7ee559e 100644
--- a/tests/java_api/RSTest_CompatLib/src/com/android/rs/test/RSTestCore.java
+++ b/tests/java_api/RSTest_CompatLib/src/com/android/rs/test/RSTestCore.java
@@ -69,6 +69,7 @@
         unitTests.add(new UT_alloc_copyPadded(this, mRes, mCtx));
         unitTests.add(new UT_kernel(this, mRes, mCtx));
         unitTests.add(new UT_kernel_struct(this, mRes, mCtx));
+        unitTests.add(new UT_blur_validation(this, mRes, mCtx));
         unitTests.add(new UT_bug_char(this, mRes, mCtx));
         unitTests.add(new UT_clamp(this, mRes, mCtx));
         unitTests.add(new UT_clamp_relaxed(this, mRes, mCtx));
diff --git a/tests/java_api/RSTest_CompatLib/src/com/android/rs/test/UT_blur_validation.java b/tests/java_api/RSTest_CompatLib/src/com/android/rs/test/UT_blur_validation.java
new file mode 100644
index 0000000..bb79919
--- /dev/null
+++ b/tests/java_api/RSTest_CompatLib/src/com/android/rs/test/UT_blur_validation.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2017 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.rs.test_compat;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.support.v8.renderscript.Allocation;
+import android.support.v8.renderscript.Element;
+import android.support.v8.renderscript.RenderScript;
+import android.support.v8.renderscript.RSIllegalArgumentException;
+import android.support.v8.renderscript.ScriptIntrinsicBlur;
+import android.support.v8.renderscript.Type;
+import android.util.Log;
+
+// Tests that ScriptIntrinsicBlur properly throws exception if input or output
+// are set to 1D Allocations.
+public class UT_blur_validation extends UnitTest {
+    private static final String TAG = "ScriptIntrinsicBlur validation";
+    private RenderScript RS;
+    private Allocation input1D, output1D;
+    private Allocation input2D, output2D;
+    private ScriptIntrinsicBlur scriptBlur;
+
+    protected UT_blur_validation(RSTestCore rstc, Resources res, Context ctx) {
+        super(rstc, TAG, ctx);
+    }
+
+    private void cleanup() {
+        RS.finish();
+        input1D.destroy();
+        input2D.destroy();
+        output1D.destroy();
+        output2D.destroy();
+        scriptBlur.destroy();
+        RS.destroy();
+    }
+
+    public void run() {
+        RS = RenderScript.create(mCtx);
+
+        final int width  = 100;
+        final int height = 100;
+
+        input1D = Allocation.createSized(RS,
+                                         Element.U8(RS),
+                                         width * height,
+                                         Allocation.USAGE_SCRIPT);
+
+        output1D = Allocation.createTyped(RS, input1D.getType());
+
+        Type.Builder typeBuilder = new Type.Builder(RS, Element.U8(RS));
+        typeBuilder.setX(width);
+        typeBuilder.setY(height);
+        Type ty = typeBuilder.create();
+
+        input2D  = Allocation.createTyped(RS, ty);
+        output2D = Allocation.createTyped(RS, ty);
+
+        scriptBlur = ScriptIntrinsicBlur.create(RS, Element.U8(RS));
+
+        scriptBlur.setRadius(25f);
+        boolean failed = false;
+
+        try {
+            scriptBlur.setInput(input1D);
+        } catch (RSIllegalArgumentException e) {
+            scriptBlur.setInput(input2D);
+            try {
+                scriptBlur.forEach(output1D);
+            } catch (RSIllegalArgumentException e1) {
+                scriptBlur.forEach(output2D);
+                cleanup();
+                passTest();
+                return;
+            }
+            Log.e(TAG, "setting 1d output does not trigger exception");
+            cleanup();
+            failTest();
+            return;
+        }
+
+        Log.e(TAG, "setting 1d input does not trigger exception");
+        cleanup();
+        failTest();
+    }
+}