Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.framework.tests;
     18 
     19 import com.android.tradefed.device.DeviceNotAvailableException;
     20 import com.android.tradefed.device.ITestDevice;
     21 import com.android.tradefed.log.LogUtil.CLog;
     22 import com.android.tradefed.util.RunUtil;
     23 
     24 public class DataIdleTestHelper {
     25     private final static String[] PING_SERVER_LIST = {"www.google.com", "www.facebook.com",
     26         "www.bing.com", "www.ask.com", "www.yahoo.com"};
     27     private final static String PING_FAIL_STRING = "ping: unknown host";
     28     private ITestDevice mDevice;
     29     private final static int RETRY_ATTEMPTS = 5;
     30     private final static int PING_WAITING_TIME = 30 * 10000;  // 30 seconds
     31 
     32     DataIdleTestHelper(ITestDevice device) {
     33         mDevice = device;
     34     }
     35 
     36     /**
     37      * Ping a series of popular servers to check for Internet connection.
     38      *
     39      * @return true if the device is able to ping the test servers.
     40      * @throws DeviceNotAvailableException
     41      */
     42     public boolean pingTest() throws DeviceNotAvailableException {
     43         for (int i = 0; i < RETRY_ATTEMPTS; ++i) {
     44             for (int j = 0; j < PING_SERVER_LIST.length; ++j) {
     45                 String host = PING_SERVER_LIST[j];
     46                 CLog.d("Start ping test, ping %s", host);
     47                 String res = mDevice.executeShellCommand("ping -c 10 -w 100 " + host);
     48                 CLog.d("res: %s", res);
     49                 if (!res.contains(PING_FAIL_STRING)) {
     50                     return true;
     51                 }
     52             }
     53             RunUtil.getDefault().sleep(PING_WAITING_TIME);
     54         }
     55         return false;
     56     }
     57 
     58 
     59 }
     60