Add rsIsObject() to support library implementation.

We only had the "element" version of rsIsObject() previously. This change also
adds the beginning of a full API test for the script-side of RS (starting with
the RS object-related routines).

Change-Id: If341b352ad671be40b860a502c345a8149bf10f3
diff --git a/driver/rsdRuntimeStubs.cpp b/driver/rsdRuntimeStubs.cpp
index ac80ba3..4788a12 100644
--- a/driver/rsdRuntimeStubs.cpp
+++ b/driver/rsdRuntimeStubs.cpp
@@ -1222,24 +1222,23 @@
 // Compatibility Library entry points
 //////////////////////////////////////////////////////////////////////////////
 
-bool rsIsObject(rs_element src) {
-    return SC_IsObject((ObjectBase*)src.p);
-}
-
-#define CLEAR_SET_OBJ(t) \
+#define IS_CLEAR_SET_OBJ(t) \
+    bool rsIsObject(t src) { \
+        return SC_IsObject((ObjectBase*)src.p); \
+    } \
     void __attribute__((overloadable)) rsClearObject(t *dst) { \
-    return SC_ClearObject((ObjectBase**) dst); \
+        return SC_ClearObject((ObjectBase**) dst); \
     } \
     void __attribute__((overloadable)) rsSetObject(t *dst, t src) { \
-    return SC_SetObject((ObjectBase**) dst, (ObjectBase*) src.p); \
+        return SC_SetObject((ObjectBase**) dst, (ObjectBase*) src.p); \
     }
 
-CLEAR_SET_OBJ(rs_element)
-CLEAR_SET_OBJ(rs_type)
-CLEAR_SET_OBJ(rs_allocation)
-CLEAR_SET_OBJ(rs_sampler)
-CLEAR_SET_OBJ(rs_script)
-#undef CLEAR_SET_OBJ
+IS_CLEAR_SET_OBJ(rs_element)
+IS_CLEAR_SET_OBJ(rs_type)
+IS_CLEAR_SET_OBJ(rs_allocation)
+IS_CLEAR_SET_OBJ(rs_sampler)
+IS_CLEAR_SET_OBJ(rs_script)
+#undef IS_CLEAR_SET_OBJ
 
 const Allocation * rsGetAllocation(const void *ptr) {
     return SC_GetAllocation(ptr);
diff --git a/java/tests/RSTest_CompatLib/src/com/android/rs/test/RSTestCore.java b/java/tests/RSTest_CompatLib/src/com/android/rs/test/RSTestCore.java
index 2ee2a33..462e4f4 100644
--- a/java/tests/RSTest_CompatLib/src/com/android/rs/test/RSTestCore.java
+++ b/java/tests/RSTest_CompatLib/src/com/android/rs/test/RSTestCore.java
@@ -57,6 +57,7 @@
 
         unitTests = new ArrayList<UnitTest>();
 
+        unitTests.add(new UT_apitest(this, mRes, mCtx));
         unitTests.add(new UT_primitives(this, mRes, mCtx));
         unitTests.add(new UT_instance(this, mRes, mCtx));
         unitTests.add(new UT_constant(this, mRes, mCtx));
diff --git a/java/tests/RSTest_CompatLib/src/com/android/rs/test/UT_apitest.java b/java/tests/RSTest_CompatLib/src/com/android/rs/test/UT_apitest.java
new file mode 100644
index 0000000..6c5a1ce
--- /dev/null
+++ b/java/tests/RSTest_CompatLib/src/com/android/rs/test/UT_apitest.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2013 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.*;
+
+public class UT_apitest extends UnitTest {
+    private Resources mRes;
+
+    protected UT_apitest(RSTestCore rstc, Resources res, Context ctx) {
+        super(rstc, "API Test", ctx);
+        mRes = res;
+    }
+
+    public void run() {
+        RenderScript pRS = RenderScript.create(mCtx);
+        ScriptC_apitest s = new ScriptC_apitest(pRS);
+        pRS.setMessageHandler(mRsMessage);
+        Element elem = Element.I8(pRS);
+        Type.Builder typeBuilder = new Type.Builder(pRS, elem);
+        typeBuilder.setX(1);
+        Type type = typeBuilder.create();
+        Allocation alloc = Allocation.createTyped(pRS, type);
+        Sampler sampler = Sampler.CLAMP_NEAREST(pRS);
+        s.set_elemNonNull(elem);
+        s.set_typeNonNull(type);
+        s.set_allocNonNull(alloc);
+        s.set_samplerNonNull(sampler);
+        s.set_scriptNonNull(s);
+
+        s.invoke_api_test();
+        pRS.finish();
+        waitForMessage();
+        pRS.destroy();
+    }
+}
diff --git a/java/tests/RSTest_CompatLib/src/com/android/rs/test/apitest.rs b/java/tests/RSTest_CompatLib/src/com/android/rs/test/apitest.rs
new file mode 100644
index 0000000..9499cbd
--- /dev/null
+++ b/java/tests/RSTest_CompatLib/src/com/android/rs/test/apitest.rs
@@ -0,0 +1,74 @@
+#include "shared.rsh"
+
+rs_element elemNull;
+rs_element elemNonNull;
+rs_type typeNull;
+rs_type typeNonNull;
+rs_allocation allocNull;
+rs_allocation allocNonNull;
+rs_sampler samplerNull;
+rs_sampler samplerNonNull;
+rs_script scriptNull;
+rs_script scriptNonNull;
+
+
+static bool test_obj_api() {
+    bool failed = false;
+
+    _RS_ASSERT(!rsIsObject(elemNull));
+    _RS_ASSERT(rsIsObject(elemNonNull));
+    rsSetObject(&elemNull, elemNonNull);
+    _RS_ASSERT(rsIsObject(elemNull));
+    rsClearObject(&elemNull);
+    _RS_ASSERT(!rsIsObject(elemNull));
+
+    _RS_ASSERT(!rsIsObject(typeNull));
+    _RS_ASSERT(rsIsObject(typeNonNull));
+    rsSetObject(&typeNull, typeNonNull);
+    _RS_ASSERT(rsIsObject(typeNull));
+    rsClearObject(&typeNull);
+    _RS_ASSERT(!rsIsObject(typeNull));
+
+    _RS_ASSERT(!rsIsObject(allocNull));
+    _RS_ASSERT(rsIsObject(allocNonNull));
+    rsSetObject(&allocNull, allocNonNull);
+    _RS_ASSERT(rsIsObject(allocNull));
+    rsClearObject(&allocNull);
+    _RS_ASSERT(!rsIsObject(allocNull));
+
+    _RS_ASSERT(!rsIsObject(samplerNull));
+    _RS_ASSERT(rsIsObject(samplerNonNull));
+    rsSetObject(&samplerNull, samplerNonNull);
+    _RS_ASSERT(rsIsObject(samplerNull));
+    rsClearObject(&samplerNull);
+    _RS_ASSERT(!rsIsObject(samplerNull));
+
+    _RS_ASSERT(!rsIsObject(scriptNull));
+    _RS_ASSERT(rsIsObject(scriptNonNull));
+    rsSetObject(&scriptNull, scriptNonNull);
+    _RS_ASSERT(rsIsObject(scriptNull));
+    rsClearObject(&scriptNull);
+    _RS_ASSERT(!rsIsObject(scriptNull));
+
+    if (failed) {
+        rsDebug("test_obj_api FAILED", -1);
+    }
+    else {
+        rsDebug("test_obj_api PASSED", 0);
+    }
+
+    return failed;
+}
+
+void api_test() {
+    bool failed = false;
+    failed |= test_obj_api();
+
+    if (failed) {
+        rsSendToClientBlocking(RS_MSG_TEST_FAILED);
+    }
+    else {
+        rsSendToClientBlocking(RS_MSG_TEST_PASSED);
+    }
+}
+