标签: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类型,以防它的上下文出现为null但我仍然得到相同的错误.我已经多次搜索过这个问题,但似乎无法根据我发现的问题解决问题.
我出错的任何想法?
解决方法:
通过模拟InteractiveWebView解决了这个问题,但确保它是在主UI线程上运行的Runnable中构建的:
getActivity().runOnUiThread(new Runnable()
{
@Override
public void run()
{
// Given
InteractiveWebView interactiveWebView = new InteractiveWebView(getActivity());
....
....
....
....
}
}
标签:android,unit-testing,android-webview 来源: https://codeday.me/bug/20190706/1393637.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。