Bug 1622816 - Ensure network is always available in TestRunnerActivity r=geckoview-reviewers,agi

When running mochitests on real hardware, we sometimes lose the network,
causing strange timeouts. It's better if we crash immediately in those
situations to avoid confusion.

Differential Revision: https://phabricator.services.mozilla.com/D67383

--HG--
extra : moz-landing-system : lando
This commit is contained in:
James Willcox 2020-03-18 20:38:15 +00:00
Родитель 3315f2c93c
Коммит 4ccf12cecf
1 изменённых файлов: 21 добавлений и 0 удалений

Просмотреть файл

@ -19,8 +19,12 @@ import org.mozilla.geckoview.WebExtensionController;
import org.mozilla.geckoview.WebRequestError;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.SurfaceTexture;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
@ -257,12 +261,29 @@ public class TestRunnerActivity extends Activity {
}
}
private void ensureNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
cm.registerDefaultNetworkCallback(new ConnectivityManager.NetworkCallback() {
@Override
public void onLost(Network network) {
throw new RuntimeException("Network has been lost");
}
});
NetworkInfo network = cm.getActiveNetworkInfo();
if (network == null || !network.isConnected()) {
throw new RuntimeException("No network available");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
ensureNetworkAvailable();
if (sRuntime == null) {
final GeckoRuntimeSettings.Builder runtimeSettingsBuilder =
new GeckoRuntimeSettings.Builder();