I've got problems testing the result of FirebaseVisionTextRecognizer.processImage()
My Android app takes the outcome of that function and tries to interpret it in a special context. Different types of images should be treated differently with a customized parser. So I thought that an automated test of these classes would be the right approach.
Robolectric sounds like the right framework for this use case, but I'm not really sure.
@Rule
public TestRule rule = new InstantTaskExecutorRule();
private static int number = 0;
@Test
public void testTask() {
Bitmap testBitmap = BitmapFactory.decodeFile( "src/test/testImages/img1.jpg" );
FirebaseApp.initializeApp( ApplicationProvider.getApplicationContext() );
FirebaseVisionImage visionImage = FirebaseVisionImage.fromBitmap( testBitmap );
FirebaseVisionTextRecognizer detector =
FirebaseVision.getInstance().getCloudTextRecognizer();
Task<FirebaseVisionText> result =
detector.processImage( visionImage )
.addOnSuccessListener( visionText -> {
number = 1;
System.out.println( "Success!" );
} )
.addOnFailureListener(
e -> {
number = 2;
System.err.println( "Failed…" );
}
);
// nothing of this block works
ShadowApplication.runBackgroundTasks();
shadowOf( getMainLooper() ).idle();
shadowOf( getMainLooper() ).runToEndOfTasks();
Robolectric.flushBackgroundThreadScheduler();
Thread t = new Thread( () -> {
try {
// Comment this out to see that it runs forever
// Tasks.await( result );
} catch( Exception e ) {
e.printStackTrace();
}
} );
t.start();
try {
t.join();
} catch( InterruptedException e ) {
e.printStackTrace();
}
assertThat( number == 1 ).isTrue();
}
Nothing is printed, and if you comment out the command that says you want to wait until the task is finished, you wait forever (>2h).
I suspect it has something to do with the files the library has to download the first time it is used, but I'm not sure if it is caused by something in the ml-vision kit or in the Robolectric framework.
Thank you for any advice on how to fix this problem or for suggestions for other setups where I don't need this.
Yeah, the textRecognizer depends on the model to be downloaded via Google Play Services. It will be hard to test the pipeline involving model in robolectric test. You will need to mock at some level to make your unit test work to test your own code.