Add delay in ping connectivity test retry

Change-Id: Id6e36f42b2a2d84605185780fb7a3f2144e7a9b9
diff --git a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestBase.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestBase.java
index 80d5668..64fed7f 100644
--- a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestBase.java
+++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestBase.java
@@ -49,7 +49,8 @@
  */
 public class ConnectivityManagerTestBase extends InstrumentationTestCase {
 
-    private static final String PING_IP_ADDR = "8.8.8.8";
+    private static final String[] PING_HOST_LIST = {
+        "www.google.com", "www.yahoo.com", "www.bing.com", "www.facebook.com", "www.ask.com"};
 
     protected static final int WAIT_FOR_SCAN_RESULT = 10 * 1000; //10 seconds
     protected static final int WIFI_SCAN_TIMEOUT = 50 * 1000; // 50 seconds
@@ -281,22 +282,14 @@
     }
 
     /**
-     * @param pingServerList a list of servers that can be used for ping test, can be null
      * @return true if the ping test is successful, false otherwise.
      */
-    protected boolean pingTest(String[] pingServerList) {
-        String[] hostList = {"www.google.com", "www.yahoo.com",
-                "www.bing.com", "www.facebook.com", "www.ask.com"};
-        if (pingServerList != null) {
-            hostList = pingServerList;
-        }
-
+    protected boolean pingTest() {
         long startTime = System.currentTimeMillis();
         while ((System.currentTimeMillis() - startTime) < PING_TIMER) {
             try {
                 // assume the chance that all servers are down is very small
-                for (int i = 0; i < hostList.length; i++ ) {
-                    String host = hostList[i];
+                for (String host : PING_HOST_LIST) {
                     logv("Start ping test, ping " + host);
                     Process p = Runtime.getRuntime().exec("ping -c 10 -w 100 " + host);
                     int status = p.waitFor();
@@ -312,6 +305,7 @@
             } catch (InterruptedException e) {
                 logv("Ping test Fail: InterruptedException");
             }
+            SystemClock.sleep(SHORT_TIMEOUT);
         }
         // ping test timeout
         return false;
@@ -458,14 +452,7 @@
     // use ping request against Google public DNS to verify connectivity
     protected boolean checkNetworkConnectivity() {
         assertTrue("no active network connection", waitForActiveNetworkConnection(LONG_TIMEOUT));
-        try {
-            Process proc = Runtime.getRuntime().exec(new String[]{
-                    "/system/bin/ping", "-W", "30", "-c", "1", PING_IP_ADDR});
-            return proc.waitFor() == 0;
-        } catch (InterruptedException | IOException e) {
-            Log.e(mLogTag, "Ping failed", e);
-        }
-        return false;
+        return pingTest();
     }
 
     @Override
diff --git a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java
index d5051df..2d291ff 100644
--- a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java
+++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java
@@ -72,16 +72,6 @@
         super.tearDown();
     }
 
-    // help function to verify 3G connection
-    public void verifyCellularConnection() {
-        NetworkInfo extraNetInfo = mCm.getActiveNetworkInfo();
-        assertEquals("network type is not MOBILE", ConnectivityManager.TYPE_MOBILE,
-                extraNetInfo.getType());
-        assertTrue("not connected to cellular network", extraNetInfo.isConnected());
-    }
-
-
-
     // Test case 1: Test enabling Wifi without associating with any AP, no broadcast on network
     //              event should be expected.
     @LargeTest
@@ -336,4 +326,12 @@
         assertTrue("wifi state not disabled", waitForWifiState(
                 WifiManager.WIFI_STATE_DISABLED, LONG_TIMEOUT));
     }
+
+    // help function to verify 3G connection
+    private void verifyCellularConnection() {
+        NetworkInfo extraNetInfo = mCm.getActiveNetworkInfo();
+        assertEquals("network type is not MOBILE", ConnectivityManager.TYPE_MOBILE,
+                extraNetInfo.getType());
+        assertTrue("not connected to cellular network", extraNetInfo.isConnected());
+    }
 }
diff --git a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/stress/WifiApStress.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/stress/WifiApStress.java
index 41f01e6..de934b9 100644
--- a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/stress/WifiApStress.java
+++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/stress/WifiApStress.java
@@ -112,7 +112,7 @@
             } catch (Exception e) {
                 // ignore
             }
-            assertTrue("no uplink data connection after Wi-Fi tethering", pingTest(null));
+            assertTrue("no uplink data connection after Wi-Fi tethering", pingTest());
             // disable wifi hotspot
             assertTrue("failed to disable wifi hotspot",
                     mWifiManager.setWifiApEnabled(config, false));
diff --git a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/stress/WifiStressTest.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/stress/WifiStressTest.java
index fbd4669..f3d5c87 100644
--- a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/stress/WifiStressTest.java
+++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/stress/WifiStressTest.java
@@ -216,7 +216,7 @@
         assertTrue("wifi not connected", waitForNetworkState(ConnectivityManager.TYPE_WIFI,
                 State.CONNECTED, WIFI_CONNECTION_TIMEOUT));
         // Run ping test to verify the data connection
-        assertTrue("Wi-Fi is connected, but no data connection.", pingTest(null));
+        assertTrue("Wi-Fi is connected, but no data connection.", pingTest());
 
         long i, sum = 0, avgReconnectTime = 0;
         for (i = 1; i <= mReconnectIterations; i++) {
@@ -264,7 +264,7 @@
             } else {
                 assertEquals("mobile not connected", State.CONNECTED,
                         mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState());
-                assertTrue("no connectivity over mobile", pingTest(null));
+                assertTrue("no connectivity over mobile", pingTest());
             }
 
             // Turn screen on again
@@ -281,7 +281,7 @@
             avgReconnectTime = sum / i;
             logv("average reconnection time is: " + avgReconnectTime);
 
-            assertTrue("Reconnect to Wi-Fi network, but no data connection.", pingTest(null));
+            assertTrue("Reconnect to Wi-Fi network, but no data connection.", pingTest());
         }
         Bundle result = new Bundle();
         result.putLong("actual-iterations", i - 1);