我在测试FirebaseVisionTextRecognizer.processImage()的结果时遇到问题
我的Android应用获取该功能的结果,并尝试在特殊的上下文中对其进行解释。不同类型的图像应使用定制的解析器进行不同处理。因此,我认为对这些类进行自动化测试将是正确的方法。
Robolectric听起来像是此用例的正确框架,但我不确定。
@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();
}
没有显示任何内容,如果您注释掉要等待任务完成的命令,则将永远等待(> 2h)。
我怀疑它与库在首次使用时必须下载的文件有关,但是我不确定这是由ml-vision套件还是Robolectric框架中的某些原因引起的。
感谢您提供有关如何解决此问题的建议,或针对不需要此设置的其他设置的建议。
[是的,textRecognizer取决于要通过Google Play服务下载的型号。在机器人测试中,很难测试涉及模型的管道。您将需要在某种程度上进行模拟,以使您的单元测试工作来测试您自己的代码。