am 775eeca0: am 44bc7921: am d57ae212: am 8a37e0b3: am 48c2210b: am 4a4d6864: Merge "Fix testVpn timing issue for com.android.cts.net.HostsideNetworkTests class" into lollipop-mr1-cts-dev

* commit '775eeca0205b87008bd21b1dd95be228870f5aee':
  Fix testVpn timing issue for com.android.cts.net.HostsideNetworkTests class
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/VpnTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/VpnTest.java
old mode 100644
new mode 100755
index 8bb2a2c..5045cc2
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/VpnTest.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/VpnTest.java
@@ -87,6 +87,7 @@
     Network mNetwork;
     NetworkCallback mCallback;
     final Object mLock = new Object();
+    final Object mLockShutdown = new Object();
 
     private boolean supportedHardware() {
         final PackageManager pm = getInstrumentation().getContext().getPackageManager();
@@ -204,7 +205,9 @@
         mActivity.startService(intent);
         synchronized (mLock) {
             if (mNetwork == null) {
+                 Log.i(TAG, "bf mLock");
                  mLock.wait(TIMEOUT_MS);
+                 Log.i(TAG, "af mLock");
             }
         }
 
@@ -214,10 +217,27 @@
 
         // Unfortunately, when the available callback fires, the VPN UID ranges are not yet
         // configured. Give the system some time to do so. http://b/18436087 .
-        try { Thread.sleep(300); } catch(InterruptedException e) {}
+        try { Thread.sleep(3000); } catch(InterruptedException e) {}
     }
 
     private void stopVpn() {
+        // Register a callback so we will be notified when our VPN comes up.
+        final NetworkRequest request = new NetworkRequest.Builder()
+                .addTransportType(NetworkCapabilities.TRANSPORT_VPN)
+                .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
+                .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
+                .build();
+        mCallback = new NetworkCallback() {
+            public void onLost(Network network) {
+                synchronized (mLockShutdown) {
+                    Log.i(TAG, "Got lost callback for network=" + network + ",mNetwork = " + mNetwork);
+                    if( mNetwork == network){
+                        mLockShutdown.notify();
+                    }
+                }
+            }
+       };
+        mCM.registerNetworkCallback(request, mCallback);  // Unregistered in tearDown.
         // Simply calling mActivity.stopService() won't stop the service, because the system binds
         // to the service for the purpose of sending it a revoke command if another VPN comes up,
         // and stopping a bound service has no effect. Instead, "start" the service again with an
@@ -225,6 +245,13 @@
         Intent intent = new Intent(mActivity, MyVpnService.class)
                 .putExtra(mPackageName + ".cmd", "disconnect");
         mActivity.startService(intent);
+        synchronized (mLockShutdown) {
+            try {
+                 Log.i(TAG, "bf mLockShutdown");
+                 mLockShutdown.wait(TIMEOUT_MS);
+                 Log.i(TAG, "af mLockShutdown");
+            } catch(InterruptedException e) {}
+        }
     }
 
     private static void closeQuietly(Closeable c) {
@@ -433,7 +460,7 @@
         if (!supportedHardware()) return;
 
         startVpn(new String[] {"192.0.2.2/32", "2001:db8:1:2::ffe/128"},
-                 new String[] {"192.0.2.0/24", "2001:db8::/32"},
+                 new String[] {"0.0.0.0/0", "::/0"},
                  "", "");
 
         checkTrafficOnVpn();
@@ -443,7 +470,7 @@
         if (!supportedHardware()) return;
 
         startVpn(new String[] {"192.0.2.2/32", "2001:db8:1:2::ffe/128"},
-                 new String[] {"0.0.0.0/0", "::/0"},
+                 new String[] {"192.0.2.0/24", "2001:db8::/32"},
                  mPackageName, "");
 
         checkTrafficOnVpn();