CLAHE Python bindings
[profile/ivi/opencv.git] / modules / java / android_test / src / org / opencv / test / OpenCVTestRunner.java
1 package org.opencv.test;
2
3 import java.io.File;
4 import java.io.IOException;
5 import junit.framework.Assert;
6
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;
12
13 import android.content.Context;
14 import android.test.AndroidTestRunner;
15 import android.test.InstrumentationTestRunner;
16 import android.util.Log;
17
18 /**
19  * This only class is Android specific.
20  *
21  * @see <a href="http://opencv.itseez.com">OpenCV</a>
22  */
23
24 public class OpenCVTestRunner extends InstrumentationTestRunner {
25
26     private static final long MANAGER_TIMEOUT = 3000;
27     public static String LENA_PATH;
28     public static String CHESS_PATH;
29     public static String LBPCASCADE_FRONTALFACE_PATH;
30     public static Context context;
31
32     private AndroidTestRunner androidTestRunner;
33     private static String TAG = "opencv_test_java";
34
35     private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(getContext()) {
36
37         @Override
38         public void onManagerConnected(int status) {
39             switch (status) {
40                 case LoaderCallbackInterface.SUCCESS:
41                 {
42                     Log("OpenCV loaded successfully");
43                     synchronized (this) {
44                         notify();
45                     }
46                 } break;
47                 default:
48                 {
49                     super.onManagerConnected(status);
50                 } break;
51             }
52         }
53     };
54
55     public static String getTempFileName(String extension)
56     {
57         File cache = context.getCacheDir();
58         if (!extension.startsWith("."))
59             extension = "." + extension;
60         try {
61             File tmp = File.createTempFile("OpenCV", extension, cache);
62             String path = tmp.getAbsolutePath();
63             tmp.delete();
64             return path;
65         } catch (IOException e) {
66             Log("Failed to get temp file name. Exception is thrown: " + e);
67         }
68         return null;
69     }
70
71     static public void Log(String message) {
72         Log.e(TAG, message);
73     }
74
75     static public void Log(Mat m) {
76         Log.e(TAG, m + "\n " + m.dump());
77     }
78
79     @Override
80     public void onStart() {
81         // try to load internal libs
82         if (!OpenCVLoader.initDebug()) {
83             // There is no internal OpenCV libs
84             // Using OpenCV Manager for initialization;
85
86             Log("Internal OpenCV library not found. Using OpenCV Manager for initialization");
87             OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, getContext(), mLoaderCallback);
88
89             synchronized (this) {
90                 try {
91                     wait(MANAGER_TIMEOUT);
92                 } catch (InterruptedException e) {
93                     e.printStackTrace();
94                 }
95             }
96         } else {
97             Log("OpenCV library found inside test package. Using it!");
98         }
99
100         context = getContext();
101         Assert.assertTrue("Context can't be 'null'", context != null);
102         LENA_PATH = Utils.exportResource(context, R.drawable.lena);
103         CHESS_PATH = Utils.exportResource(context, R.drawable.chessboard);
104         LBPCASCADE_FRONTALFACE_PATH = Utils.exportResource(context, R.raw.lbpcascade_frontalface);
105
106         /*
107          * The original idea about test order randomization is from
108          * marek.defecinski blog.
109          */
110         //List<TestCase> testCases = androidTestRunner.getTestCases();
111         //Collections.shuffle(testCases); //shuffle the tests order
112
113         super.onStart();
114     }
115
116     @Override
117     protected AndroidTestRunner getAndroidTestRunner() {
118         androidTestRunner = super.getAndroidTestRunner();
119         return androidTestRunner;
120     }
121
122     public static String getOutputFileName(String name)
123     {
124         return context.getExternalFilesDir(null).getAbsolutePath() + File.separatorChar + name;
125     }
126 }