我使用PDFView来显示PDF文件,这是成功的。但是我不知道如何在单元测试中测试这种方法。
com.github.barteksc.pdfviewer.PDFView pdfview = mainView.findViewById(R.id.pdfview);
public void showDocument(final InputStream inputStream){
pdfview.fromStream(inputStream)
.enableSwipe(true)
.swipeHorizontal(false)
.enableDoubletap(true)
.onPageChange(new OnPageChangeListener() {
@Override
public void onPageChanged(int page, int pageCount) {
}
})
.onLoad(new OnLoadCompleteListener() {
@Override
public void loadComplete(int nbPages) {
closeProgress();
totalPages = nbPages;
System.out.println("nbPages"+nbPages);
}
})
.onError(new OnErrorListener() {
@Override
public void onError(Throwable t) {
System.out.println("onError"+t.getMessage());
}
})
.load();
}
我的测试方法:
@Test
public void testFetchDataFromNet() {
DocumentActivity activity = Robolectric.setupActivity(DocumentActivity.class);
activity.showDocument(PDFData.iputStreamPDF);
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
}
但是onLoad()和onError()都不在控制台中打印任何数据。 我尝试了一个自定义阴影:
@Implements(PDFView.class)
public class ShadowPDFView extends ShadowRelativeLayout {
Context context;
AttributeSet attrs;
@Implementation
public void __constructor__(Context context)
{
}
@Implementation
public void __constructor__(Context context, AttributeSet attrs)
{
this.context = context;
this.attrs = attrs;
}
@Implementation
public void __constructor__(Context context, AttributeSet attrs, int defStyleAttr)
{
}
PDFView.Configurator configurator;
@Implementation
public PDFView.Configurator fromStream(InputStream stream) {
DocumentSource documentSource = new InputStreamSource(stream);
Class clazz = null;
try {
Class c2 = Class.forName("com.github.barteksc.pdfviewer.PDFView$Configurator");
Constructor constructor = c2.getDeclaredConstructor(new Class[] {PDFView.class, DocumentSource.class });
constructor.setAccessible(true);
if (constructor != null) {
configurator = (PDFView.Configurator) constructor.newInstance(new PDFView(context,attrs), documentSource);
}
} catch (Exception e) {
e.printStackTrace();
}
return configurator;
}
}
但是什么都没有改变。
请帮助或尝试提供一些实现方法。
我将非常感谢!
答案 0 :(得分:0)
需要ShadowPDFView而不是ShadowPDFConfigurator,就像这样:
@SuppressWarnings({"unused", "EmptyMethod"})
@Implements(PDFView.Configurator.class)
public class ShadowPDFConfigurator{
OnLoadCompleteListener onLoadCompleteListener;
@Implementation
public void __constructor__(DocumentSource documentSource)
{
}
@RealObject
PDFView.Configurator configurator;
@Implementation
public Object enableSwipe(boolean enableSwipe) {
DebugUtils.getInstance().system("enableSwipe","");
return configurator;
}
@Implementation
public Object swipeHorizontal(boolean swipeHorizontal) {
DebugUtils.getInstance().system("swipeHorizontal","");
return configurator;
}
@Implementation
public Object enableDoubletap(boolean enableDoubletap) {
DebugUtils.getInstance().system("enableDoubletap","");
return configurator;
}
@Implementation
public Object onPageChange(OnPageChangeListener onPageChangeListener) {
DebugUtils.getInstance().system("onPageChangeListener","");
return configurator;
}
@Implementation
public Object onLoad(OnLoadCompleteListener onLoadCompleteListener) {
DebugUtils.getInstance().system("onLoadCompleteListener","");
this.onLoadCompleteListener = onLoadCompleteListener;
return configurator;
}
@Implementation
public Object onError(OnErrorListener onErrorListener) {
DebugUtils.getInstance().system("onErrorListener","");
return configurator;
}
@Implementation
public void load() {
DebugUtils.getInstance().system("load","");
this.onLoadCompleteListener.loadComplete(1);
}
}