i worked on android application and i have a webview that show some web content for user.
Now i try to do some integration test cases in order to ensure that all things are correct.
One of this test cases is to turn off the internet connection during loading the page and check the behaviour.
This is my code:
@Override
public void initializeWebViewCallbacks() {
if(!InternetHelper.isThereInternetConnection(AuthFragment.this.context()))
{
AuthFragment.this.showRetry();
}
else{
wv_auth.setWebViewClient(new WebViewClient());
wv_auth.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
wv_auth.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
if (Build.VERSION.SDK_INT >= 19) {
wv_auth.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
wv_auth.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
wv_auth.loadUrl("http://myurl");
wv_auth.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
wv_auth.setVisibility(View.GONE);
AuthFragment.this.hideLoading();
AuthFragment.this.showRetry();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
AuthFragment.this.showLoading();
}
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@TargetApi(android.os.Build.VERSION_CODES.M)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return shouldOverrideUrlLoading(view, request.toString());
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
AuthFragment.this.hideLoading();
// some code
new Handler().postDelayed(new Runnable() {
public void run() {
((BaseActivity)getActivity()).navigator.navigateToMainActivity(AuthFragment.this.getActivity(),false);
}
}, 200);
}
}
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view,errorCode,description,failingUrl);
// Handle the error
wv_auth.setVisibility(View.GONE);
AuthFragment.this.hideLoading();
AuthFragment.this.showRetry();
}
@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
});
}
}
When i turn off the internet connection during loding the page, The progress circle still running for about 30 minutes and break point didn't go through onReceivedError(...) so i can't handle this case, any suggestions??
I think it's an issue with loadUrl(..) method.
I try every possible solution and also follow all solutions here: SO question but with no chance.
I notice that when you create the activity or fragment that host the webview every thing works correctly so i did a workaround by navigate to another intermediate blank activity for a 1 second then return back to this activity and by this i solve the issue.