From 4ccf12cecfcdfa7798f25f253d75031dbc325f77 Mon Sep 17 00:00:00 2001 From: James Willcox Date: Wed, 18 Mar 2020 20:38:15 +0000 Subject: [PATCH] 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 --- .../geckoview/test/TestRunnerActivity.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/TestRunnerActivity.java b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/TestRunnerActivity.java index 00b197260f87..203a3d9efc8e 100644 --- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/TestRunnerActivity.java +++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/TestRunnerActivity.java @@ -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();