am 799044f3: am 505ce805: Merge "Change SSLCertificateSocketFactoryTest to use googlemail.com" into jb-dev

* commit '799044f32079eea8e057afda4d07b173db5da4d8':
  Change SSLCertificateSocketFactoryTest to use googlemail.com
diff --git a/tests/cts/net/src/android/net/cts/NetworkInfo_DetailedStateTest.java b/tests/cts/net/src/android/net/cts/NetworkInfo_DetailedStateTest.java
index b8d0ea4..590ce89 100644
--- a/tests/cts/net/src/android/net/cts/NetworkInfo_DetailedStateTest.java
+++ b/tests/cts/net/src/android/net/cts/NetworkInfo_DetailedStateTest.java
@@ -37,7 +37,7 @@
 
     public void testValues() {
         DetailedState[] expected = DetailedState.values();
-        assertEquals(12, expected.length);
+        assertEquals(13, expected.length);
         assertEquals(DetailedState.IDLE, expected[0]);
         assertEquals(DetailedState.SCANNING, expected[1]);
         assertEquals(DetailedState.CONNECTING, expected[2]);
@@ -50,6 +50,7 @@
         assertEquals(DetailedState.FAILED, expected[9]);
         assertEquals(DetailedState.BLOCKED, expected[10]);
         assertEquals(DetailedState.VERIFYING_POOR_LINK, expected[11]);
+        assertEquals(DetailedState.CAPTIVE_PORTAL_CHECK, expected[12]);
     }
 
 }
diff --git a/tests/cts/net/src/android/net/wifi/cts/ScanResultTest.java b/tests/cts/net/src/android/net/wifi/cts/ScanResultTest.java
index 26cfff8..c9b82ee 100644
--- a/tests/cts/net/src/android/net/wifi/cts/ScanResultTest.java
+++ b/tests/cts/net/src/android/net/wifi/cts/ScanResultTest.java
@@ -26,6 +26,7 @@
 import android.net.wifi.WifiManager;
 import android.net.wifi.WifiManager.WifiLock;
 import android.test.AndroidTestCase;
+import android.util.Log;
 
 public class ScanResultTest extends AndroidTestCase {
     private static class MySync {
@@ -39,11 +40,14 @@
     private static final int STATE_NULL = 0;
     private static final int STATE_WIFI_CHANGING = 1;
     private static final int STATE_WIFI_CHANGED = 2;
+    private static final int STATE_START_SCAN = 3;
+    private static final int STATE_SCAN_RESULTS_AVAILABLE = 4;
 
     private static final String TAG = "WifiInfoTest";
     private static final int TIMEOUT_MSEC = 6000;
     private static final int WAIT_MSEC = 60;
-    private static final int DURATION = 10000;
+    private static final int ENABLE_WAIT_MSEC = 10000;
+    private static final int SCAN_WAIT_MSEC = 10000;
     private IntentFilter mIntentFilter;
     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
@@ -54,6 +58,11 @@
                     mMySync.expectedState = STATE_WIFI_CHANGED;
                     mMySync.notify();
                 }
+            } else if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
+                synchronized (mMySync) {
+                    mMySync.expectedState = STATE_SCAN_RESULTS_AVAILABLE;
+                    mMySync.notify();
+                }
             }
         }
     };
@@ -83,7 +92,7 @@
         mWifiLock.acquire();
         if (!mWifiManager.isWifiEnabled())
             setWifiEnabled(true);
-        Thread.sleep(DURATION);
+        Thread.sleep(ENABLE_WAIT_MSEC);
         assertTrue(mWifiManager.isWifiEnabled());
         mMySync.expectedState = STATE_NULL;
     }
@@ -99,7 +108,7 @@
         mContext.unregisterReceiver(mReceiver);
         if (!mWifiManager.isWifiEnabled())
             setWifiEnabled(true);
-        Thread.sleep(DURATION);
+        Thread.sleep(ENABLE_WAIT_MSEC);
         super.tearDown();
     }
 
@@ -107,11 +116,15 @@
         synchronized (mMySync) {
             mMySync.expectedState = STATE_WIFI_CHANGING;
             assertTrue(mWifiManager.setWifiEnabled(enable));
-            long timeout = System.currentTimeMillis() + TIMEOUT_MSEC;
-            while (System.currentTimeMillis() < timeout
-                    && mMySync.expectedState == STATE_WIFI_CHANGING)
-                mMySync.wait(WAIT_MSEC);
-        }
+            waitForBroadcast(TIMEOUT_MSEC, STATE_WIFI_CHANGED);
+       }
+    }
+
+    private void waitForBroadcast(long timeout, int expectedState) throws Exception {
+        long waitTime = System.currentTimeMillis() + timeout;
+        while (System.currentTimeMillis() < waitTime
+                && mMySync.expectedState != expectedState)
+            mMySync.wait(WAIT_MSEC);
     }
 
     public void testScanResultProperties() {
@@ -127,4 +140,49 @@
         }
     }
 
+    private void scanAndWait() throws Exception {
+        synchronized (mMySync) {
+            mMySync.expectedState = STATE_START_SCAN;
+            mWifiManager.startScan();
+            waitForBroadcast(SCAN_WAIT_MSEC, STATE_SCAN_RESULTS_AVAILABLE);
+        }
+   }
+
+    public void testScanResultTimeStamp() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
+
+        long timestamp = 0;
+        String BSSID = null;
+
+        /* Multiple scans to ensure bssid is updated */
+        scanAndWait();
+        scanAndWait();
+        scanAndWait();
+
+        List<ScanResult> scanResults = mWifiManager.getScanResults();
+        for (ScanResult result : scanResults) {
+            BSSID = result.BSSID;
+            timestamp = result.timestamp;
+            assertTrue(timestamp != 0);
+            break;
+        }
+
+        scanAndWait();
+        scanAndWait();
+        scanAndWait();
+
+        scanResults = mWifiManager.getScanResults();
+        for (ScanResult result : scanResults) {
+            if (result.BSSID.equals(BSSID)) {
+                long timeDiff = (result.timestamp - timestamp) / 1000;
+                assertTrue (timeDiff > 0);
+                assertTrue (timeDiff < 6 * SCAN_WAIT_MSEC);
+            }
+        }
+
+    }
+
 }
diff --git a/tests/cts/net/src/android/net/wifi/cts/WifiManagerTest.java b/tests/cts/net/src/android/net/wifi/cts/WifiManagerTest.java
index 5fc23e7..283f63b 100644
--- a/tests/cts/net/src/android/net/wifi/cts/WifiManagerTest.java
+++ b/tests/cts/net/src/android/net/wifi/cts/WifiManagerTest.java
@@ -21,17 +21,22 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.net.NetworkInfo;
 import android.net.wifi.ScanResult;
 import android.net.wifi.WifiConfiguration;
-import android.net.wifi.WifiManager;
 import android.net.wifi.WifiConfiguration.Status;
+import android.net.wifi.WifiManager;
+import android.net.wifi.WifiManager.TxPacketCountListener;
 import android.net.wifi.WifiManager.WifiLock;
 import android.test.AndroidTestCase;
 import android.util.Log;
 
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 
 public class WifiManagerTest extends AndroidTestCase {
     private static class MySync {
@@ -42,6 +47,7 @@
     private WifiLock mWifiLock;
     private static MySync mMySync;
     private List<ScanResult> mScanResult = null;
+    private NetworkInfo mNetworkInfo;
 
     // Please refer to WifiManager
     private static final int MIN_RSSI = -100;
@@ -49,9 +55,10 @@
 
     private static final int STATE_NULL = 0;
     private static final int STATE_WIFI_CHANGING = 1;
-    private static final int STATE_WIFI_CHANGED = 2;
-    private static final int STATE_SCANING = 3;
-    private static final int STATE_SCAN_RESULTS_AVAILABLE = 4;
+    private static final int STATE_WIFI_ENABLED = 2;
+    private static final int STATE_WIFI_DISABLED = 3;
+    private static final int STATE_SCANNING = 4;
+    private static final int STATE_SCAN_RESULTS_AVAILABLE = 5;
 
     private static final String TAG = "WifiManagerTest";
     private static final String SSID1 = "\"WifiManagerTest\"";
@@ -70,13 +77,29 @@
                         mScanResult = mWifiManager.getScanResults();
                         mMySync.expectedState = STATE_SCAN_RESULTS_AVAILABLE;
                         mScanResult = mWifiManager.getScanResults();
-                        mMySync.notify();
+                        mMySync.notifyAll();
                     }
                 }
-            } else if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
+            } else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
+                int newState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
+                        WifiManager.WIFI_STATE_UNKNOWN);
                 synchronized (mMySync) {
-                    mMySync.expectedState = STATE_WIFI_CHANGED;
-                    mMySync.notify();
+                    if (newState == WifiManager.WIFI_STATE_ENABLED) {
+                        Log.d(TAG, "*** New WiFi state is ENABLED ***");
+                        mMySync.expectedState = STATE_WIFI_ENABLED;
+                        mMySync.notifyAll();
+                    } else if (newState == WifiManager.WIFI_STATE_DISABLED) {
+                        Log.d(TAG, "*** New WiFi state is DISABLED ***");
+                        mMySync.expectedState = STATE_WIFI_DISABLED;
+                        mMySync.notifyAll();
+                    }
+                }
+            } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
+                synchronized (mMySync) {
+                    mNetworkInfo =
+                            (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
+                    if (mNetworkInfo.getState() == NetworkInfo.State.CONNECTED)
+                        mMySync.notifyAll();
                 }
             }
         }
@@ -109,7 +132,9 @@
             setWifiEnabled(true);
         Thread.sleep(DURATION);
         assertTrue(mWifiManager.isWifiEnabled());
-        mMySync.expectedState = STATE_NULL;
+        synchronized (mMySync) {
+            mMySync.expectedState = STATE_NULL;
+        }
     }
 
     @Override
@@ -119,35 +144,50 @@
             super.tearDown();
             return;
         }
-        mWifiLock.release();
-        mContext.unregisterReceiver(mReceiver);
         if (!mWifiManager.isWifiEnabled())
             setWifiEnabled(true);
+        mWifiLock.release();
+        mContext.unregisterReceiver(mReceiver);
         Thread.sleep(DURATION);
         super.tearDown();
     }
 
     private void setWifiEnabled(boolean enable) throws Exception {
         synchronized (mMySync) {
-            mMySync.expectedState = STATE_WIFI_CHANGING;
             assertTrue(mWifiManager.setWifiEnabled(enable));
-            long timeout = System.currentTimeMillis() + TIMEOUT_MSEC;
-            while (System.currentTimeMillis() < timeout
-                    && mMySync.expectedState == STATE_WIFI_CHANGING)
-                mMySync.wait(WAIT_MSEC);
+            if (mWifiManager.isWifiEnabled() != enable) {
+                mMySync.expectedState = STATE_WIFI_CHANGING;
+                long timeout = System.currentTimeMillis() + TIMEOUT_MSEC;
+                int expectedState = (enable ? STATE_WIFI_ENABLED : STATE_WIFI_DISABLED);
+                while (System.currentTimeMillis() < timeout
+                        && mMySync.expectedState != expectedState)
+                    mMySync.wait(WAIT_MSEC);
+            }
         }
     }
 
     private void startScan() throws Exception {
         synchronized (mMySync) {
-            mMySync.expectedState = STATE_SCANING;
+            mMySync.expectedState = STATE_SCANNING;
             assertTrue(mWifiManager.startScan());
             long timeout = System.currentTimeMillis() + TIMEOUT_MSEC;
-            while (System.currentTimeMillis() < timeout && mMySync.expectedState == STATE_SCANING)
+            while (System.currentTimeMillis() < timeout && mMySync.expectedState == STATE_SCANNING)
                 mMySync.wait(WAIT_MSEC);
         }
     }
 
+    private void connectWifi() throws Exception {
+        synchronized (mMySync) {
+            if (mNetworkInfo.getState() == NetworkInfo.State.CONNECTED) return;
+            assertTrue(mWifiManager.reconnect());
+            long timeout = System.currentTimeMillis() + TIMEOUT_MSEC;
+            while (System.currentTimeMillis() < timeout
+                    && mNetworkInfo.getState() != NetworkInfo.State.CONNECTED)
+                mMySync.wait(WAIT_MSEC);
+            assertTrue(mNetworkInfo.getState() == NetworkInfo.State.CONNECTED);
+        }
+    }
+
     private boolean existSSID(String ssid) {
         for (final WifiConfiguration w : mWifiManager.getConfiguredNetworks()) {
             if (w.SSID.equals(ssid))
@@ -339,4 +379,74 @@
         rssiB = 4;
         assertTrue(WifiManager.compareSignalLevel(rssiA, rssiB) > 0);
     }
+
+    private int getTxPacketCount() throws Exception {
+        final AtomicInteger ret = new AtomicInteger(-1);
+
+        mWifiManager.getTxPacketCount(new TxPacketCountListener() {
+            @Override
+            public void onSuccess(int count) {
+                ret.set(count);
+            }
+            @Override
+            public void onFailure(int reason) {
+                ret.set(0);
+            }
+        });
+
+        long timeout = System.currentTimeMillis() + TIMEOUT_MSEC;
+        while (ret.get() < 0 && System.currentTimeMillis() < timeout)
+            Thread.sleep(WAIT_MSEC);
+        assertTrue(ret.get() >= 0);
+        return ret.get();
+    }
+
+    /**
+     * The new WiFi watchdog requires kernel/driver to export some packet loss
+     * counters. This CTS tests whether those counters are correctly exported.
+     * To pass this CTS test, a connected WiFi link is required.
+     */
+    public void testWifiWatchdog() throws Exception {
+        // Make sure WiFi is enabled
+        if (!mWifiManager.isWifiEnabled()) {
+            setWifiEnabled(true);
+            Thread.sleep(DURATION);
+        }
+        assertTrue(mWifiManager.isWifiEnabled());
+
+        int i = 0;
+        for (; i < 15; i++) {
+            // Wait for a WiFi connection
+            connectWifi();
+
+            // Read TX packet counter
+            int txcount1 = getTxPacketCount();
+
+            // Do some network operations
+            HttpURLConnection connection = null;
+            try {
+                URL url = new URL("http://www.google.com/");
+                connection = (HttpURLConnection) url.openConnection();
+                connection.setInstanceFollowRedirects(false);
+                connection.setConnectTimeout(TIMEOUT_MSEC);
+                connection.setReadTimeout(TIMEOUT_MSEC);
+                connection.setUseCaches(false);
+                connection.getInputStream();
+            } catch (Exception e) {
+                // ignore
+            } finally {
+                if (connection != null) connection.disconnect();
+            }
+
+            // Read TX packet counter again and make sure it increases
+            int txcount2 = getTxPacketCount();
+
+            if (txcount2 > txcount1) {
+                break;
+            } else {
+                Thread.sleep(DURATION);
+            }
+        }
+        assertTrue(i < 15);
+    }
 }