Fixed java tests build
[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
22 public class OpenCVTestRunner extends InstrumentationTestRunner {
23
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;
29
30     private AndroidTestRunner androidTestRunner;
31     private static String TAG = "opencv_test_java";
32
33     private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(getContext()) {
34
35         @Override
36         public void onManagerConnected(int status) {
37             switch (status) {
38                 case LoaderCallbackInterface.SUCCESS:
39                 {
40                     Log("OpenCV loaded successfully");
41                     synchronized (this) {
42                         notify();
43                     }
44                 } break;
45                 default:
46                 {
47                     super.onManagerConnected(status);
48                 } break;
49             }
50         }
51     };
52
53     public static String getTempFileName(String extension)
54     {
55         File cache = context.getCacheDir();
56         if (!extension.startsWith("."))
57             extension = "." + extension;
58         try {
59             File tmp = File.createTempFile("OpenCV", extension, cache);
60             String path = tmp.getAbsolutePath();
61             tmp.delete();
62             return path;
63         } catch (IOException e) {
64             Log("Failed to get temp file name. Exception is thrown: " + e);
65         }
66         return null;
67     }
68
69     static public void Log(String message) {
70         Log.e(TAG, message);
71     }
72
73     static public void Log(Mat m) {
74         Log.e(TAG, m + "\n " + m.dump());
75     }
76
77     @Override
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;
83
84             Log("Internal OpenCV library not found. Using OpenCV Manager for initialization");
85             OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, getContext(), mLoaderCallback);
86
87             synchronized (this) {
88                 try {
89                     wait(MANAGER_TIMEOUT);
90                 } catch (InterruptedException e) {
91                     e.printStackTrace();
92                 }
93             }
94         } else {
95             Log("OpenCV library found inside test package. Using it!");
96         }
97
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);
103
104         /*
105          * The original idea about test order randomization is from
106          * marek.defecinski blog.
107          */
108         //List<TestCase> testCases = androidTestRunner.getTestCases();
109         //Collections.shuffle(testCases); //shuffle the tests order
110
111         super.onStart();
112     }
113
114     @Override
115     protected AndroidTestRunner getAndroidTestRunner() {
116         androidTestRunner = super.getAndroidTestRunner();
117         return androidTestRunner;
118     }
119
120     public static String getOutputFileName(String name)
121     {
122         return context.getExternalFilesDir(null).getAbsolutePath() + File.separatorChar + name;
123     }
124 }