1 package org.opencv.test;
4 import java.io.IOException;
5 import junit.framework.Assert;
7 import org.opencv.android.BaseLoaderCallback;
8 import org.opencv.android.LoaderCallbackInterface;
9 import org.opencv.android.OpenCVLoader;
10 import org.opencv.android.Utils;
11 import org.opencv.core.Mat;
13 import android.content.Context;
14 import android.test.AndroidTestRunner;
15 import android.test.InstrumentationTestRunner;
16 import android.util.Log;
19 * This only class is Android specific.
22 public class OpenCVTestRunner extends InstrumentationTestRunner {
24 private static final long MANAGER_TIMEOUT = 3000;
25 public static String LENA_PATH;
26 public static String CHESS_PATH;
27 public static String LBPCASCADE_FRONTALFACE_PATH;
28 public static Context context;
30 private AndroidTestRunner androidTestRunner;
31 private static String TAG = "opencv_test_java";
33 private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(getContext()) {
36 public void onManagerConnected(int status) {
38 case LoaderCallbackInterface.SUCCESS:
40 Log("OpenCV loaded successfully");
47 super.onManagerConnected(status);
53 public static String getTempFileName(String extension)
55 File cache = context.getCacheDir();
56 if (!extension.startsWith("."))
57 extension = "." + extension;
59 File tmp = File.createTempFile("OpenCV", extension, cache);
60 String path = tmp.getAbsolutePath();
63 } catch (IOException e) {
64 Log("Failed to get temp file name. Exception is thrown: " + e);
69 static public void Log(String message) {
73 static public void Log(Mat m) {
74 Log.e(TAG, m + "\n " + m.dump());
78 public void onStart() {
79 // try to load internal libs
80 if (!OpenCVLoader.initDebug()) {
81 // There is no internal OpenCV libs
82 // Using OpenCV Manager for initialization;
84 Log("Internal OpenCV library not found. Using OpenCV Manager for initialization");
85 OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, getContext(), mLoaderCallback);
89 wait(MANAGER_TIMEOUT);
90 } catch (InterruptedException e) {
95 Log("OpenCV library found inside test package. Using it!");
98 context = getContext();
99 Assert.assertTrue("Context can't be 'null'", context != null);
100 LENA_PATH = Utils.exportResource(context, R.drawable.lena);
101 CHESS_PATH = Utils.exportResource(context, R.drawable.chessboard);
102 LBPCASCADE_FRONTALFACE_PATH = Utils.exportResource(context, R.raw.lbpcascade_frontalface);
105 * The original idea about test order randomization is from
106 * marek.defecinski blog.
108 //List<TestCase> testCases = androidTestRunner.getTestCases();
109 //Collections.shuffle(testCases); //shuffle the tests order
115 protected AndroidTestRunner getAndroidTestRunner() {
116 androidTestRunner = super.getAndroidTestRunner();
117 return androidTestRunner;
120 public static String getOutputFileName(String name)
122 return context.getExternalFilesDir(null).getAbsolutePath() + File.separatorChar + name;