|
| 1 | +package com.appliedrec.credentials.app; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.Instrumentation; |
| 5 | +import android.content.Intent; |
| 6 | +import android.graphics.Bitmap; |
| 7 | +import android.graphics.BitmapFactory; |
| 8 | +import android.net.Uri; |
| 9 | +import android.os.Parcel; |
| 10 | + |
| 11 | +import androidx.exifinterface.media.ExifInterface; |
| 12 | +import androidx.test.espresso.intent.rule.IntentsTestRule; |
| 13 | +import androidx.test.platform.app.InstrumentationRegistry; |
| 14 | + |
| 15 | +import com.appliedrec.uielements.facecomparison.ResultActivity; |
| 16 | +import com.appliedrec.verid.core.Bearing; |
| 17 | +import com.appliedrec.verid.core.DetectedFace; |
| 18 | +import com.appliedrec.verid.core.Face; |
| 19 | +import com.appliedrec.verid.core.FaceDetectionException; |
| 20 | +import com.appliedrec.verid.core.RecognizableFace; |
| 21 | +import com.appliedrec.verid.core.VerID; |
| 22 | +import com.appliedrec.verid.core.VerIDFactory; |
| 23 | +import com.appliedrec.verid.core.VerIDImage; |
| 24 | +import com.appliedrec.verid.core.VerIDSessionResult; |
| 25 | +import com.appliedrec.verid.ui.VerIDSessionActivity; |
| 26 | + |
| 27 | +import org.junit.After; |
| 28 | +import org.junit.Rule; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import java.io.File; |
| 32 | +import java.io.FileInputStream; |
| 33 | +import java.io.FileOutputStream; |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.InputStream; |
| 36 | +import java.util.ArrayList; |
| 37 | + |
| 38 | +import static androidx.test.espresso.Espresso.onView; |
| 39 | +import static androidx.test.espresso.action.ViewActions.click; |
| 40 | +import static androidx.test.espresso.intent.Intents.getIntents; |
| 41 | +import static androidx.test.espresso.intent.Intents.intended; |
| 42 | +import static androidx.test.espresso.intent.Intents.intending; |
| 43 | +import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent; |
| 44 | +import static androidx.test.espresso.intent.matcher.IntentMatchers.hasExtraWithKey; |
| 45 | +import static androidx.test.espresso.matcher.ViewMatchers.withId; |
| 46 | +import static junit.framework.TestCase.assertTrue; |
| 47 | +import static org.hamcrest.core.AllOf.allOf; |
| 48 | +import static org.junit.Assert.assertEquals; |
| 49 | +import static org.junit.Assert.fail; |
| 50 | + |
| 51 | +public class AppTests { |
| 52 | + |
| 53 | + private File idCardTempFile; |
| 54 | + private File selfieTempFile; |
| 55 | + private VerID verID; |
| 56 | + |
| 57 | + @After |
| 58 | + public void cleanup() { |
| 59 | + if (idCardTempFile != null) { |
| 60 | + idCardTempFile.delete(); |
| 61 | + idCardTempFile = null; |
| 62 | + } |
| 63 | + if (selfieTempFile != null) { |
| 64 | + selfieTempFile.delete(); |
| 65 | + selfieTempFile = null; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Rule |
| 70 | + public IntentsTestRule<IDCardActivity> idCardActivityIntentsTestRule = new IntentsTestRule<IDCardActivity>(IDCardActivity.class) { |
| 71 | + @Override |
| 72 | + protected Intent getActivityIntent() { |
| 73 | + Intent intent = new Intent(InstrumentationRegistry.getInstrumentation().getTargetContext(), IDCardActivity.class); |
| 74 | + try { |
| 75 | + Uri cardImageUri = getCardImageUri(); |
| 76 | + intent.putExtra(IDCardActivity.EXTRA_CARD_IMAGE_URI, cardImageUri); |
| 77 | + VerIDImage verIDImage = getImageFromUri(cardImageUri); |
| 78 | + Face[] faces = getVerID().getFaceDetection().detectFacesInImage(verIDImage, 1, 0); |
| 79 | + if (faces.length == 0) { |
| 80 | + throw new FaceDetectionException(FaceDetectionException.Type.FACE_NOT_FOUND, null); |
| 81 | + } |
| 82 | + RecognizableFace[] recognizableFaces = getVerID().getFaceRecognition().createRecognizableFacesFromFaces(faces, verIDImage); |
| 83 | + intent.putExtra(IDCardActivity.EXTRA_DETECTED_FACE, new DetectedFace(recognizableFaces[0], Bearing.STRAIGHT, cardImageUri)); |
| 84 | + } catch (Exception e) { |
| 85 | + fail(); |
| 86 | + } |
| 87 | + return intent; |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testFaceComparison_returnsExpectedScore() throws Exception { |
| 93 | + Intent data = new Intent(); |
| 94 | + VerIDSessionResult sessionResult = getSessionResult(null); |
| 95 | + data.putExtra(VerIDSessionActivity.EXTRA_RESULT, sessionResult); |
| 96 | + Instrumentation.ActivityResult activityResult = new Instrumentation.ActivityResult(Activity.RESULT_OK, data); |
| 97 | + intending(hasComponent(VerIDSessionActivity.class.getName())).respondWith(activityResult); |
| 98 | + |
| 99 | + onView(withId(R.id.button)).perform(click()); |
| 100 | + |
| 101 | + Thread.sleep(5000); |
| 102 | + |
| 103 | + intended(allOf(hasComponent(ResultActivity.class.getName()), hasExtraWithKey(ResultActivity.EXTRA_SCORE))); |
| 104 | + |
| 105 | + float score = -1f; |
| 106 | + for (Intent intent : getIntents()) { |
| 107 | + if (intent.hasExtra(ResultActivity.EXTRA_SCORE)) { |
| 108 | + score = intent.getFloatExtra(ResultActivity.EXTRA_SCORE, -1f); |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | + assertTrue(score > 3.8f); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testFaceDetection_returnsExpectedFaceCoordinates() throws Exception { |
| 117 | + Face[] faces = getVerID().getFaceDetection().detectFacesInImage(getSelfieImage("selfieImage.jpg"), 1, 0); |
| 118 | + assertEquals(1, faces.length); |
| 119 | + float delta = 0.1f; |
| 120 | + assertEquals(81f, faces[0].getLeftEye().x, delta); |
| 121 | + assertEquals(71.5f, faces[0].getLeftEye().y, delta); |
| 122 | + assertEquals(111.0f, faces[0].getRightEye().x, delta); |
| 123 | + assertEquals(68.5f, faces[0].getRightEye().y, delta); |
| 124 | + } |
| 125 | + |
| 126 | + private VerIDImage getImageFromUri(Uri uri) throws IOException { |
| 127 | + Bitmap bitmap; |
| 128 | + int orientation; |
| 129 | + try (InputStream inputStream = new FileInputStream(uri.getPath())) { |
| 130 | + bitmap = BitmapFactory.decodeStream(inputStream); |
| 131 | + } |
| 132 | + if (bitmap == null) { |
| 133 | + throw new IOException(); |
| 134 | + } |
| 135 | + try (InputStream inputStream = new FileInputStream(uri.getPath())) { |
| 136 | + orientation = new ExifInterface(inputStream).getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); |
| 137 | + } |
| 138 | + return new VerIDImage(bitmap, orientation); |
| 139 | + } |
| 140 | + |
| 141 | + private Uri getCardImageUri() throws Exception { |
| 142 | + try (InputStream inputStream = InstrumentationRegistry.getInstrumentation().getContext().getAssets().open("cardImage.png")) { |
| 143 | + idCardTempFile = File.createTempFile("cardImage_", ".png"); |
| 144 | + try (FileOutputStream outputStream = new FileOutputStream(idCardTempFile)) { |
| 145 | + int read; |
| 146 | + byte[] buffer = new byte[512]; |
| 147 | + while ((read = inputStream.read(buffer, 0, buffer.length)) > 0) { |
| 148 | + outputStream.write(buffer, 0, read); |
| 149 | + } |
| 150 | + outputStream.flush(); |
| 151 | + return Uri.fromFile(idCardTempFile); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private VerIDSessionResult getSessionResult(Exception exception) throws Exception { |
| 157 | + if (exception != null) { |
| 158 | + return new VerIDSessionResult(exception); |
| 159 | + } else { |
| 160 | + String fileName = "selfieImage.jpg"; |
| 161 | + VerIDImage image = getSelfieImage(fileName); |
| 162 | + Face[] faces = getVerID().getFaceDetection().detectFacesInImage(image, 1, 0); |
| 163 | + assertEquals(1, faces.length); |
| 164 | + RecognizableFace[] recognizableFaces = getVerID().getFaceRecognition().createRecognizableFacesFromFaces(faces, image); |
| 165 | + |
| 166 | + DetectedFace detectedFace = new DetectedFace(recognizableFaces[0], Bearing.STRAIGHT, getSelfieImageUri(fileName)); |
| 167 | + ArrayList<DetectedFace> attachments = new ArrayList<>(); |
| 168 | + attachments.add(detectedFace); |
| 169 | + |
| 170 | + Parcel parcel = Parcel.obtain(); |
| 171 | + parcel.writeTypedList(attachments); |
| 172 | + parcel.setDataPosition(0); |
| 173 | + return VerIDSessionResult.CREATOR.createFromParcel(parcel); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private VerIDImage getSelfieImage(String name) throws Exception { |
| 178 | + Bitmap bitmap; |
| 179 | + int orientation; |
| 180 | + try (InputStream inputStream = InstrumentationRegistry.getInstrumentation().getContext().getAssets().open(name)) { |
| 181 | + bitmap = BitmapFactory.decodeStream(inputStream); |
| 182 | + } |
| 183 | + if (bitmap == null) { |
| 184 | + throw new Exception(); |
| 185 | + } |
| 186 | + try (InputStream inputStream = InstrumentationRegistry.getInstrumentation().getContext().getAssets().open(name)) { |
| 187 | + ExifInterface exifInterface = new ExifInterface(inputStream); |
| 188 | + orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); |
| 189 | + } |
| 190 | + return new VerIDImage(bitmap, orientation); |
| 191 | + } |
| 192 | + |
| 193 | + private Uri getSelfieImageUri(String name) throws Exception { |
| 194 | + if (selfieTempFile == null) { |
| 195 | + try (InputStream inputStream = InstrumentationRegistry.getInstrumentation().getContext().getAssets().open(name)) { |
| 196 | + File tempFile = File.createTempFile("test_", ".jpg"); |
| 197 | + try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile)) { |
| 198 | + int read; |
| 199 | + byte[] buffer = new byte[512]; |
| 200 | + while ((read = inputStream.read(buffer, 0, buffer.length)) > 0) { |
| 201 | + fileOutputStream.write(buffer, 0, read); |
| 202 | + } |
| 203 | + fileOutputStream.flush(); |
| 204 | + selfieTempFile = tempFile; |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + return Uri.fromFile(selfieTempFile); |
| 209 | + } |
| 210 | + |
| 211 | + private VerID getVerID() throws Exception { |
| 212 | + if (verID == null) { |
| 213 | + verID = new VerIDFactory(InstrumentationRegistry.getInstrumentation().getTargetContext()).createVerIDSync(); |
| 214 | + } |
| 215 | + return verID; |
| 216 | + } |
| 217 | +} |
0 commit comments