From 3d51dd0b855c2dced3a4e7f2d7263be05fe3eb7c Mon Sep 17 00:00:00 2001 From: Jaeyun Date: Thu, 19 Sep 2019 19:52:14 +0900 Subject: [PATCH] [Android/Test] base code to run instrumented test Add base code to run Android instrumented test on the target devices. TODO add testcases for each java class Signed-off-by: Jaeyun Jung --- api/android/api/build.gradle | 5 ++ .../java/org/nnsuite/nnstreamer/APITestCommon.java | 61 ++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 api/android/api/src/androidTest/java/org/nnsuite/nnstreamer/APITestCommon.java diff --git a/api/android/api/build.gradle b/api/android/api/build.gradle index 2116c05..c2c083b 100644 --- a/api/android/api/build.gradle +++ b/api/android/api/build.gradle @@ -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 index 0000000..a208db9 --- /dev/null +++ b/api/android/api/src/androidTest/java/org/nnsuite/nnstreamer/APITestCommon.java @@ -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 Testing documentation + */ +@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()); + } +} -- 2.7.4