From 66a1ea7604d0043343eba3feecfe9b2ccd027e13 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Tue, 20 Nov 2012 10:45:46 +0400 Subject: [PATCH] Test execution inside Eclipse fixed; OpenCV testing via OpenCV Manager implemented. --- .../src/org/opencv/test/OpenCVTestCase.java | 17 +++++- .../src/org/opencv/test/OpenCVTestRunner.java | 59 ++++++++++++++----- .../opencv/test/highgui/VideoCaptureTest.java | 31 ++++------ 3 files changed, 70 insertions(+), 37 deletions(-) diff --git a/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java b/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java index 102adf1958..b24d858ee3 100644 --- a/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java +++ b/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java @@ -23,15 +23,20 @@ import org.opencv.features2d.DMatch; import org.opencv.features2d.KeyPoint; import org.opencv.highgui.Highgui; -public class OpenCVTestCase extends TestCase { +import android.util.Log; +public class OpenCVTestCase extends TestCase { //change to 'true' to unblock fail on fail("Not yet implemented") public static final boolean passNYI = true; + protected static boolean isTestCaseEnabled = true; + protected static final int matSize = 10; protected static final double EPS = 0.001; protected static final double weakEPS = 0.5; + private static final String TAG = "OpenCVTestCase"; + protected Mat dst; protected Mat truth; @@ -173,6 +178,16 @@ public class OpenCVTestCase extends TestCase { super.tearDown(); } + @Override + protected void runTest() throws Throwable { + // Do nothing if the precondition does not hold. + if (isTestCaseEnabled) { + super.runTest(); + } else { + Log.e(TAG, "Test case \"" + this.getClass().getName() + "\" disabled!"); + } + } + protected Mat getMat(int type, double... vals) { return new Mat(matSize, matSize, type, new Scalar(vals)); diff --git a/modules/java/android_test/src/org/opencv/test/OpenCVTestRunner.java b/modules/java/android_test/src/org/opencv/test/OpenCVTestRunner.java index 3c416c5104..2dc4aead8d 100644 --- a/modules/java/android_test/src/org/opencv/test/OpenCVTestRunner.java +++ b/modules/java/android_test/src/org/opencv/test/OpenCVTestRunner.java @@ -2,11 +2,11 @@ package org.opencv.test; import java.io.File; import java.io.IOException; -import java.util.Iterator; - -import junit.framework.TestCase; import junit.framework.Assert; +import org.opencv.android.BaseLoaderCallback; +import org.opencv.android.LoaderCallbackInterface; +import org.opencv.android.OpenCVLoader; import org.opencv.android.Utils; import org.opencv.core.Mat; @@ -23,8 +23,7 @@ import android.util.Log; public class OpenCVTestRunner extends InstrumentationTestRunner { - static { System.loadLibrary("opencv_java"); } - + private static final long MANAGER_TIMEOUT = 3000; public static String LENA_PATH; public static String CHESS_PATH; public static String LBPCASCADE_FRONTALFACE_PATH; @@ -33,6 +32,26 @@ public class OpenCVTestRunner extends InstrumentationTestRunner { private AndroidTestRunner androidTestRunner; private static String TAG = "opencv_test_java"; + private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(getContext()) { + + @Override + public void onManagerConnected(int status) { + switch (status) { + case LoaderCallbackInterface.SUCCESS: + { + Log("OpenCV loaded successfully"); + synchronized (this) { + notify(); + } + } break; + default: + { + super.onManagerConnected(status); + } break; + } + } + }; + public static String getTempFileName(String extension) { File cache = context.getCacheDir(); @@ -59,6 +78,25 @@ public class OpenCVTestRunner extends InstrumentationTestRunner { @Override public void onStart() { + // try to load internal libs + if (!OpenCVLoader.initDebug()) { + // There is no internal OpenCV libs + // Using OpenCV Manager for initialization; + + Log("Internal OpenCV library not found. Using OpenCV Manager for initialization"); + OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, getContext(), mLoaderCallback); + + synchronized (this) { + try { + wait(MANAGER_TIMEOUT); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } else { + Log("OpenCV library found inside test package. Using it!"); + } + context = getContext(); Assert.assertTrue("Context can't be 'null'", context != null); LENA_PATH = Utils.exportResource(context, R.drawable.lena); @@ -72,17 +110,6 @@ public class OpenCVTestRunner extends InstrumentationTestRunner { //List testCases = androidTestRunner.getTestCases(); //Collections.shuffle(testCases); //shuffle the tests order - if(OpenCVTestCase.passNYI) { - // turn off problematic camera tests - Iterator it = androidTestRunner.getTestCases().iterator(); - while (it.hasNext()) { - String name = it.next().toString(); - if (name.contains("VideoCaptureTest")) - it.remove(); - } - } - - super.onStart(); } diff --git a/modules/java/android_test/src/org/opencv/test/highgui/VideoCaptureTest.java b/modules/java/android_test/src/org/opencv/test/highgui/VideoCaptureTest.java index c6901c5e4d..ec7211a294 100644 --- a/modules/java/android_test/src/org/opencv/test/highgui/VideoCaptureTest.java +++ b/modules/java/android_test/src/org/opencv/test/highgui/VideoCaptureTest.java @@ -19,13 +19,13 @@ public class VideoCaptureTest extends OpenCVTestCase { super.setUp(); capture = null; + isTestCaseEnabled = false; isSucceed = false; isOpened = false; } public void testGet() { - try - { + try { capture = new VideoCapture(Highgui.CV_CAP_ANDROID); double frameWidth = capture.get(Highgui.CV_CAP_PROP_FRAME_WIDTH); assertTrue(0 != frameWidth); @@ -35,8 +35,7 @@ public class VideoCaptureTest extends OpenCVTestCase { } public void testGetSupportedPreviewSizes() { - try - { + try { capture = new VideoCapture(Highgui.CV_CAP_ANDROID); List sizes = capture.getSupportedPreviewSizes(); assertNotNull(sizes); @@ -68,8 +67,7 @@ public class VideoCaptureTest extends OpenCVTestCase { } public void testIsOpenedRealCamera() { - try - { + try { capture = new VideoCapture(Highgui.CV_CAP_ANDROID); isOpened = capture.isOpened(); assertTrue(isOpened); @@ -79,8 +77,7 @@ public class VideoCaptureTest extends OpenCVTestCase { } public void testOpen() { - try - { + try { capture = new VideoCapture(); capture.open(Highgui.CV_CAP_ANDROID); isOpened = capture.isOpened(); @@ -91,8 +88,7 @@ public class VideoCaptureTest extends OpenCVTestCase { } public void testRead() { - try - { + try { capture = new VideoCapture(Highgui.CV_CAP_ANDROID); isSucceed = capture.read(dst); assertTrue(isSucceed); @@ -104,8 +100,7 @@ public class VideoCaptureTest extends OpenCVTestCase { } public void testRelease() { - try - { + try { capture = new VideoCapture(Highgui.CV_CAP_ANDROID); capture.release(); assertFalse(capture.isOpened()); @@ -116,8 +111,7 @@ public class VideoCaptureTest extends OpenCVTestCase { } public void testRetrieveMat() { - try - { + try { capture = new VideoCapture(Highgui.CV_CAP_ANDROID); capture.grab(); isSucceed = capture.retrieve(dst); @@ -130,8 +124,7 @@ public class VideoCaptureTest extends OpenCVTestCase { } public void testRetrieveMatInt() { - try - { + try { capture = new VideoCapture(Highgui.CV_CAP_ANDROID); capture.grab(); isSucceed = capture.retrieve(dst, Highgui.CV_CAP_ANDROID_GREY_FRAME); @@ -144,8 +137,7 @@ public class VideoCaptureTest extends OpenCVTestCase { } public void testSet() { - try - { + try { capture = new VideoCapture(Highgui.CV_CAP_ANDROID); capture.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 640); capture.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 480); @@ -165,8 +157,7 @@ public class VideoCaptureTest extends OpenCVTestCase { } public void testVideoCaptureInt() { - try - { + try { capture = new VideoCapture(Highgui.CV_CAP_ANDROID); assertNotNull(capture); assertTrue(capture.isOpened());