Wifi: Add CTS test for WifiManager.MulticastLock

This commit adds the CTS tests for the class WifiManager.MulticastLock

Bug: 124017617
Test: atest android.net.wifi.cts
Change-Id: I6e7a6e70f350dcd23fe541ef746770e3561d49e6
diff --git a/tests/cts/net/AndroidManifest.xml b/tests/cts/net/AndroidManifest.xml
index 630de7e..dbd8fef 100644
--- a/tests/cts/net/AndroidManifest.xml
+++ b/tests/cts/net/AndroidManifest.xml
@@ -31,6 +31,7 @@
     <uses-permission android:name="android.permission.WAKE_LOCK" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
+    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
 
     <application android:usesCleartextTraffic="true">
         <uses-library android:name="android.test.runner" />
diff --git a/tests/cts/net/src/android/net/wifi/cts/MulticastLockTest.java b/tests/cts/net/src/android/net/wifi/cts/MulticastLockTest.java
new file mode 100644
index 0000000..54fe9c7
--- /dev/null
+++ b/tests/cts/net/src/android/net/wifi/cts/MulticastLockTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net.wifi.cts;
+
+import android.content.Context;
+import android.net.wifi.WifiManager;
+import android.net.wifi.WifiManager.MulticastLock;
+import android.test.AndroidTestCase;
+
+public class MulticastLockTest extends AndroidTestCase {
+
+    private static final String WIFI_TAG = "MulticastLockTest";
+
+    /**
+     * Verify acquire and release of Multicast locks
+     */
+    public void testMulticastLock() {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
+        WifiManager wm = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
+        MulticastLock mcl = wm.createMulticastLock(WIFI_TAG);
+
+        mcl.setReferenceCounted(true);
+        assertFalse(mcl.isHeld());
+        mcl.acquire();
+        assertTrue(mcl.isHeld());
+        mcl.release();
+        assertFalse(mcl.isHeld());
+        mcl.acquire();
+        mcl.acquire();
+        assertTrue(mcl.isHeld());
+        mcl.release();
+        assertTrue(mcl.isHeld());
+        mcl.release();
+        assertFalse(mcl.isHeld());
+        assertNotNull(mcl.toString());
+        try {
+            mcl.release();
+            fail("should throw out exception because release is called"
+                    +" a greater number of times than acquire");
+        } catch (RuntimeException e) {
+            // expected
+        }
+
+        mcl = wm.createMulticastLock(WIFI_TAG);
+        mcl.setReferenceCounted(false);
+        assertFalse(mcl.isHeld());
+        mcl.acquire();
+        assertTrue(mcl.isHeld());
+        mcl.release();
+        assertFalse(mcl.isHeld());
+        mcl.acquire();
+        mcl.acquire();
+        assertTrue(mcl.isHeld());
+        mcl.release();
+        assertFalse(mcl.isHeld());
+        assertNotNull(mcl.toString());
+        // releasing again after release: but ignored for non-referenced locks
+        mcl.release();
+    }
+}