Commit 6e506244 authored by junweifu's avatar junweifu Committed by Commit Bot

ShapeDetection: Add instrumentation test for QR code detection

    
Move common function to a utility class which provides support for reading
files and converting Bitmaps to mojom.Bitmaps for testing.

Add instrumentation test for QR code detection on Android Platform, the
tested qr_code.png is QR code the value of it is https://chromium.org.

These new test end up being rolled up in chrome_public_test_apk.

BUG=722931

Change-Id: I1961efdb8263a26f2075fd567904bc17713270b3
Reviewed-on: https://chromium-review.googlesource.com/566541Reviewed-by: default avatarKen Rockot <rockot@chromium.org>
Reviewed-by: default avatarYuzhu Shen <yzshen@chromium.org>
Reviewed-by: default avatarMiguel Casas <mcasas@chromium.org>
Commit-Queue: Han Leon <leon.han@intel.com>
Cr-Commit-Position: refs/heads/master@{#487379}
parent bac30e6f
......@@ -137,7 +137,11 @@ if (is_android) {
android_library("service_javatests") {
testonly = true
java_files = [ "shape_detection/android/javatests/src/org/chromium/shape_detection/FaceDetectionImplTest.java" ]
java_files = [
"shape_detection/android/javatests/src/org/chromium/shape_detection/BarcodeDetectionImplTest.java",
"shape_detection/android/javatests/src/org/chromium/shape_detection/FaceDetectionImplTest.java",
"shape_detection/android/javatests/src/org/chromium/shape_detection/TestUtils.java",
]
deps = [
"$google_play_services_package:google_play_services_base_java",
"$google_play_services_package:google_play_services_basement_java",
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.shape_detection;
import android.support.test.filters.SmallTest;
import android.test.InstrumentationTestCase;
import org.chromium.base.test.util.Feature;
import org.chromium.shape_detection.mojom.BarcodeDetection;
import org.chromium.shape_detection.mojom.BarcodeDetectionResult;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
/**
* Test suite for BarcodeDetectionImpl.
*/
public class BarcodeDetectionImplTest extends InstrumentationTestCase {
public static final org.chromium.skia.mojom.Bitmap QR_CODE_BITMAP =
TestUtils.mojoBitmapFromFile("qr_code.png");
public BarcodeDetectionImplTest() {}
private static BarcodeDetectionResult[] detect(org.chromium.skia.mojom.Bitmap mojoBitmap) {
BarcodeDetection detector = new BarcodeDetectionImpl();
final ArrayBlockingQueue<BarcodeDetectionResult[]> queue = new ArrayBlockingQueue<>(1);
detector.detect(mojoBitmap, new BarcodeDetection.DetectResponse() {
public void call(BarcodeDetectionResult[] results) {
queue.add(results);
}
});
BarcodeDetectionResult[] toReturn = null;
try {
toReturn = queue.poll(5L, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail("Could not get BarcodeDetectionResult: " + e.toString());
}
assertNotNull(toReturn);
return toReturn;
}
@SmallTest
@Feature({"ShapeDetection"})
public void testDetectBase64ValidImageString() {
if (!TestUtils.IS_GMS_CORE_SUPPORTED) {
return;
}
BarcodeDetectionResult[] results = detect(QR_CODE_BITMAP);
assertEquals(1, results.length);
assertEquals("https://chromium.org", results[0].rawValue);
assertEquals(40.0, results[0].boundingBox.x, 0.0);
assertEquals(40.0, results[0].boundingBox.y, 0.0);
assertEquals(250.0, results[0].boundingBox.width, 0.0);
assertEquals(250.0, results[0].boundingBox.height, 0.0);
}
}
......@@ -5,23 +5,15 @@
package org.chromium.shape_detection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.support.test.filters.SmallTest;
import android.test.InstrumentationTestCase;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import org.chromium.base.ContextUtils;
import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.shape_detection.mojom.FaceDetection;
import org.chromium.shape_detection.mojom.FaceDetectionResult;
import org.chromium.shape_detection.mojom.FaceDetectorOptions;
import org.chromium.skia.mojom.ColorType;
import java.nio.ByteBuffer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
......@@ -30,40 +22,15 @@ import java.util.concurrent.TimeUnit;
*/
public class FaceDetectionImplTest extends InstrumentationTestCase {
public static final org.chromium.skia.mojom.Bitmap MONA_LISA_BITMAP =
mojoBitmapFromFile("mona_lisa.jpg");
TestUtils.mojoBitmapFromFile("mona_lisa.jpg");
// Different versions of Android have different implementations of FaceDetector.findFaces(), so
// we have to use a large error threshold.
public static final double BOUNDING_BOX_POSITION_ERROR = 10.0;
public static final double BOUNDING_BOX_SIZE_ERROR = 5.0;
public static enum DetectionProviderType { ANDROID, GMS_CORE }
public static final boolean IS_GMS_CORE_SUPPORTED = isGmsCoreSupported();
public FaceDetectionImplTest() {}
private static boolean isGmsCoreSupported() {
return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(
ContextUtils.getApplicationContext())
== ConnectionResult.SUCCESS;
}
private static org.chromium.skia.mojom.Bitmap mojoBitmapFromBitmap(Bitmap bitmap) {
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(buffer);
org.chromium.skia.mojom.Bitmap mojoBitmap = new org.chromium.skia.mojom.Bitmap();
mojoBitmap.width = bitmap.getWidth();
mojoBitmap.height = bitmap.getHeight();
mojoBitmap.pixelData = buffer.array();
mojoBitmap.colorType = ColorType.RGBA_8888;
return mojoBitmap;
}
private static org.chromium.skia.mojom.Bitmap mojoBitmapFromFile(String relPath) {
String path = UrlUtils.getIsolatedTestFilePath("services/test/data/" + relPath);
Bitmap bitmap = BitmapFactory.decodeFile(path);
return mojoBitmapFromBitmap(bitmap);
}
private static FaceDetectionResult[] detect(
org.chromium.skia.mojom.Bitmap mojoBitmap, DetectionProviderType api) {
FaceDetectorOptions options = new FaceDetectorOptions();
......@@ -113,7 +80,7 @@ public class FaceDetectionImplTest extends InstrumentationTestCase {
@SmallTest
@Feature({"ShapeDetection"})
public void testDetectValidImageWithGmsCore() {
if (IS_GMS_CORE_SUPPORTED) {
if (TestUtils.IS_GMS_CORE_SUPPORTED) {
detectSucceedsOnValidImage(DetectionProviderType.GMS_CORE);
}
}
......@@ -126,7 +93,7 @@ public class FaceDetectionImplTest extends InstrumentationTestCase {
MONA_LISA_BITMAP.height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(paddedBitmap);
canvas.drawBitmap(BitmapUtils.convertToBitmap(MONA_LISA_BITMAP), 0, 0, null);
org.chromium.skia.mojom.Bitmap mojoBitmap = mojoBitmapFromBitmap(paddedBitmap);
org.chromium.skia.mojom.Bitmap mojoBitmap = TestUtils.mojoBitmapFromBitmap(paddedBitmap);
assertEquals(1, mojoBitmap.width % 2);
FaceDetectionResult[] results = detect(mojoBitmap, DetectionProviderType.ANDROID);
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.shape_detection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import org.chromium.base.ContextUtils;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.skia.mojom.ColorType;
import java.nio.ByteBuffer;
/**
* Utility class for ShapeDetection instrumentation tests,
* provides support for e.g. reading files and converting
* Bitmaps to mojom.Bitmaps.
*/
public class TestUtils {
public static final boolean IS_GMS_CORE_SUPPORTED = isGmsCoreSupported();
private static boolean isGmsCoreSupported() {
return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(
ContextUtils.getApplicationContext())
== ConnectionResult.SUCCESS;
}
public static org.chromium.skia.mojom.Bitmap mojoBitmapFromBitmap(Bitmap bitmap) {
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(buffer);
org.chromium.skia.mojom.Bitmap mojoBitmap = new org.chromium.skia.mojom.Bitmap();
mojoBitmap.width = bitmap.getWidth();
mojoBitmap.height = bitmap.getHeight();
mojoBitmap.pixelData = buffer.array();
mojoBitmap.colorType = ColorType.RGBA_8888;
return mojoBitmap;
}
public static org.chromium.skia.mojom.Bitmap mojoBitmapFromFile(String relPath) {
String path = UrlUtils.getIsolatedTestFilePath("services/test/data/" + relPath);
Bitmap bitmap = BitmapFactory.decodeFile(path);
return mojoBitmapFromBitmap(bitmap);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment