android - 仪器测试 WebView

标签 android unit-testing android-webview

我希望在我的 InstrumentationTest 中简单地构造一个自定义 WebView,然后检查是否已完成正确的初始化。

我的自定义 WebView:

public class InteractiveWebView extends WebView
{
    public InteractiveWebView(final Context context)
    {
        super(context);
        initialise(context);
    }

    public InteractiveWebView(final Context context, final AttributeSet attrs)
    {
        super(context, attrs);
        initialise(context);
    }

    public InteractiveWebView(final Context context, final AttributeSet attrs, final int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        initialise(context);
    }

    private void initialise(final Context context)
    {
        if (!isInEditMode())
        {
            if (Build.VERSION.SDK_INT >= 19)
            {
                setLayerType(View.LAYER_TYPE_HARDWARE, null);
            }
            else
            {
                setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            }

            setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
            setFocusable(true);
            setFocusableInTouchMode(true);
            requestFocus(View.FOCUS_DOWN);

            WebSettings settings = getSettings();

            settings.setJavaScriptEnabled(true);
            settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
            settings.setSupportMultipleWindows(false);
            settings.setDomStorageEnabled(true);
            settings.setDatabaseEnabled(true);
            settings.setSupportZoom(false);
            settings.setUseWideViewPort(false);

            String databasePath = context.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
            settings.setDatabasePath(databasePath);
        }
    }
}

InstrumentationTest:

@RunWith(AndroidJUnit4.class)
@SmallTest
public class InteractiveWebViewTest
{
    @Test
    public void constructors()
    {
        Context baseContext = InstrumentationRegistry.getContext();

        InteractiveWebView webView1 = new InteractiveWebView(baseContext);
        InteractiveWebView webView2 = new InteractiveWebView(baseContext, null);
        InteractiveWebView webView3 = new InteractiveWebView(baseContext, null, 1);
    }
}

如您所见,我目前还没有断言任何东西,

我面临的问题是,当我调用第一个 WebView 构造函数时,出现以下错误:

java.lang.NullPointerException: Attempt to read from field 'android.os.MessageQueue android.os.Looper.mQueue' on a null object reference
at android.os.Handler.<init>(Handler.java:229)
at android.os.Handler.<init>(Handler.java:137)
at org.chromium.base.ThreadUtils.setUiThread(ThreadUtils.java:39)
at com.android.webview.chromium.WebViewChromiumFactoryProvider.ensureChromiumStartedLocked(WebViewChromiumFactoryProvider.java:197)
at com.android.webview.chromium.WebViewChromiumFactoryProvider.startYourEngines(WebViewChromiumFactoryProvider.java:294)
at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:218)
at android.webkit.WebView.<init>(WebView.java:606)
at android.webkit.WebView.<init>(WebView.java:542)
at android.webkit.WebView.<init>(WebView.java:525)
at android.webkit.WebView.<init>(WebView.java:512)
at android.webkit.WebView.<init>(WebView.java:502)
at android.mobileconnect.gsma.com.library.view.InteractiveWebView.<init>(InteractiveWebView.java:17)

当我单击第 17 行的 InteractiveWebView 链接时,它会将我带到该类中的构造函数并指向 super(context); 调用。

我尝试了许多其他方法,例如将 InstrumentationTest 扩展为 ActivityInstrumentationTestCase2 类型,以防出现 Context null 但我仍然得到同样的错误。我已经多次用谷歌搜索这个问题,但似乎无法根据我的发现解决问题。

有什么地方出错了吗?

最佳答案

通过模拟 InteractiveWebView 但确保它是在主 UI 线程上运行的 Runnable 中构建的来解决它:

getActivity().runOnUiThread(new Runnable()
{
    @Override
    public void run()
    {
        // Given
        InteractiveWebView interactiveWebView = new InteractiveWebView(getActivity());
        ....
        ....
        ....
        ....
    }
}

关于android - 仪器测试 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40016827/

相关文章:

java - WebView 只显示网站的一部分/隐藏网站的一部分

java - Mockito 可以捕获实现中声明的变量的参数吗?

c# - 无法获取模拟对象的方法以返回假数据

javascript - 从 Android Webview 返回后手机屏幕变白

java - 将onClickListener与ListView中的CheckBoxes一起使用

.net - 单元测试旧版 ASP.NET Webforms 应用程序

Android DownloadManager 不保存文件

android - 如何控制 RecyclerView 中单个列表项的滑动以关闭

javascript - Android 中的 Phonegap-Jquery 兼容性问题

android - Xamarin.Forms 中是否存在等效于 html 数据列表的机制,该机制允许选择预定义值但也允许选择自由文本条目?