Android:如何在 Robolectric 中测试 PDFView?

Posted

技术标签:

【中文标题】Android:如何在 Robolectric 中测试 PDFView?【英文标题】:Android: how test PDFView in Roboelectric? 【发布时间】:2018-09-19 10:07:18 【问题描述】:

我使用 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;

但没有任何改变。

请帮助或尝试给出一些想法如何实现这一点。

我将不胜感激!

【问题讨论】:

【参考方案1】:

需要 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);

【讨论】:

以上是关于Android:如何在 Robolectric 中测试 PDFView?的主要内容,如果未能解决你的问题,请参考以下文章

Android单元测试系列-Robolectric

Android单元测试系列-Robolectric

Robolectric结合Android Studio的使用

Robolectric 与 Android 测试框架

配置同时使用PowerMock和Robolectric对Android进行单元测试

配置同时使用PowerMock和Robolectric对Android进行单元测试