[Android/Test] base code to run instrumented test
authorJaeyun <jy1210.jung@samsung.com>
Thu, 19 Sep 2019 10:52:14 +0000 (19:52 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Mon, 23 Sep 2019 01:30:14 +0000 (10:30 +0900)
Add base code to run Android instrumented test on the target devices.

TODO add testcases for each java class

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
api/android/api/build.gradle
api/android/api/src/androidTest/java/org/nnsuite/nnstreamer/APITestCommon.java [new file with mode: 0644]

index 2116c05..c2c083b 100644 (file)
@@ -9,6 +9,7 @@ android {
         targetSdkVersion 28
         versionCode 1
         versionName "1.0"
+        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
         externalNativeBuild {
             ndkBuild {
                 def gstRoot
@@ -61,4 +62,8 @@ android {
 dependencies {
     implementation fileTree(include: ['*.jar'], dir: 'libs')
     implementation 'com.android.support:appcompat-v7:28.0.0'
+    testImplementation 'junit:junit:4.12'
+    androidTestImplementation 'com.android.support.test:rules:1.0.2'
+    androidTestImplementation 'com.android.support.test:runner:1.0.2'
+    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
 }
diff --git a/api/android/api/src/androidTest/java/org/nnsuite/nnstreamer/APITestCommon.java b/api/android/api/src/androidTest/java/org/nnsuite/nnstreamer/APITestCommon.java
new file mode 100644 (file)
index 0000000..a208db9
--- /dev/null
@@ -0,0 +1,61 @@
+package org.nnsuite.nnstreamer;
+
+import android.content.Context;
+import android.os.Environment;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.File;
+
+import static org.junit.Assert.*;
+
+/**
+ * Common definition to test NNStreamer API.
+ *
+ * Instrumented test, which will execute on an Android device.
+ *
+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
+ */
+@RunWith(AndroidJUnit4.class)
+public class APITestCommon {
+    private static boolean mInitialized = false;
+
+    /**
+     * Initializes NNStreamer API library.
+     */
+    public static void initNNStreamer() {
+        if (!mInitialized) {
+            try {
+                Context context = InstrumentationRegistry.getTargetContext();
+                mInitialized = NNStreamer.initialize(context);
+            } catch (Exception e) {
+                fail();
+            }
+        }
+    }
+
+    /**
+     * Gets the File object of tensorflow-lite image classification model.
+     * Note that, to invoke tensorflow-lite model in the storage, the permission READ_EXTERNAL_STORAGE is required.
+     */
+    public static File getTestModel() {
+        String root = Environment.getExternalStorageDirectory().getAbsolutePath();
+        File model = new File(root + "/nnstreamer/test/mobilenet_v1_1.0_224_quant.tflite");
+
+        if (!model.exists()) {
+            fail();
+        }
+
+        return model;
+    }
+
+    @Test
+    public void useAppContext() {
+        Context context = InstrumentationRegistry.getTargetContext();
+
+        assertEquals("org.nnsuite.nnstreamer.test", context.getPackageName());
+    }
+}