Merge changes Ifb098057,I0abaf771,I8dd60499,I2fbbb79d,Ib3cb0ffa into rvc-dev

* changes:
  CtsNetTestCases: Add wifi feature check in new tests
  WifiManagerTest: Add CTS tests for factory reset
  WifiManagerTest: Add CTS tests for WifiMigration builders
  WifiManagerTest: Add CTS tests for various settings
  WifiManagerTest: Add CTS test for forget API
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 7e9e97d..c836af5 100644
--- a/tests/cts/net/src/android/net/wifi/cts/WifiManagerTest.java
+++ b/tests/cts/net/src/android/net/wifi/cts/WifiManagerTest.java
@@ -16,11 +16,12 @@
 
 package android.net.wifi.cts;
 
-import static com.google.common.truth.Truth.assertWithMessage;
-
 import static android.net.NetworkCapabilitiesProto.TRANSPORT_WIFI;
+import static android.net.wifi.WifiConfiguration.INVALID_NETWORK_ID;
 import static android.net.wifi.WifiManager.TrafficStateCallback.DATA_ACTIVITY_INOUT;
 
+import static com.google.common.truth.Truth.assertWithMessage;
+
 import static org.junit.Assert.assertNotEquals;
 
 import android.app.UiAutomation;
@@ -56,6 +57,7 @@
 
 import androidx.test.platform.app.InstrumentationRegistry;
 
+import com.android.compatibility.common.util.PollingCheck;
 import com.android.compatibility.common.util.ShellIdentityUtils;
 import com.android.compatibility.common.util.SystemUtil;
 
@@ -105,6 +107,7 @@
     private static final int WAIT_MSEC = 60;
     private static final int DURATION = 10_000;
     private static final int DURATION_SCREEN_TOGGLE = 2000;
+    private static final int DURATION_SETTINGS_TOGGLE = 1_000;
     private static final int WIFI_SCAN_TEST_INTERVAL_MILLIS = 60 * 1000;
     private static final int WIFI_SCAN_TEST_CACHE_DELAY_MILLIS = 3 * 60 * 1000;
     private static final int WIFI_SCAN_TEST_ITERATIONS = 5;
@@ -605,9 +608,9 @@
         wifiConfiguration.SSID = SSID1;
         wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
 
-        assertEquals(WifiConfiguration.INVALID_NETWORK_ID,
+        assertEquals(INVALID_NETWORK_ID,
                 mWifiManager.addNetwork(wifiConfiguration));
-        assertEquals(WifiConfiguration.INVALID_NETWORK_ID,
+        assertEquals(INVALID_NETWORK_ID,
                 mWifiManager.updateNetwork(wifiConfiguration));
         assertFalse(mWifiManager.enableNetwork(0, true));
         assertFalse(mWifiManager.disableNetwork(0));
@@ -989,6 +992,12 @@
         Thread.sleep(DURATION_SCREEN_TOGGLE);
     }
 
+    private void assertWifiScanningIsOn() {
+        if (!mWifiManager.isScanAlwaysAvailable()) {
+            fail("Wi-Fi scanning should be on.");
+        }
+    }
+
     /**
      * Verify that Wi-Fi scanning is not turned off when the screen turns off while wifi is disabled
      * but location is on.
@@ -1151,6 +1160,10 @@
      * network.
      */
     public void testConnectWithNetworkId() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
         testConnect(true);
     }
 
@@ -1159,6 +1172,10 @@
      * existing saved network.
      */
     public void testConnectWithWifiConfiguration() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
         testConnect(false);
 
     }
@@ -1208,6 +1225,10 @@
      * an existing saved network metered.
      */
     public void testSave() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
         TestActionListener actionListener = new TestActionListener(mLock);
         UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
         List<WifiConfiguration> savedNetworks = null;
@@ -1256,6 +1277,52 @@
         }
     }
 
+    /**
+     * Tests {@link WifiManager#forget(int, WifiManager.ActionListener)} by adding/removing a new
+     * network.
+     */
+    public void testForget() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
+        TestActionListener actionListener = new TestActionListener(mLock);
+        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
+        int newNetworkId = INVALID_NETWORK_ID;
+        try {
+            uiAutomation.adoptShellPermissionIdentity();
+            // These below API's only work with privileged permissions (obtained via shell identity
+            // for test)
+            List<WifiConfiguration> savedNetworks = mWifiManager.getConfiguredNetworks();
+
+            WifiConfiguration newOpenNetwork = new WifiConfiguration();
+            newOpenNetwork.SSID = "\"" + TEST_SSID_UNQUOTED + "\"";
+            newNetworkId = mWifiManager.addNetwork(newOpenNetwork);
+            assertNotEquals(INVALID_NETWORK_ID, newNetworkId);
+
+            assertEquals(savedNetworks.size() + 1, mWifiManager.getConfiguredNetworks().size());
+
+            // Now remove the network
+            synchronized (mLock) {
+                try {
+                    mWifiManager.forget(newNetworkId, actionListener);
+                    // now wait for callback
+                    mLock.wait(DURATION);
+                } catch (InterruptedException e) {
+                }
+            }
+            // check if we got the success callback
+            assertTrue(actionListener.onSuccessCalled);
+
+            // Ensure that the new network has been successfully removed.
+            assertEquals(savedNetworks.size(), mWifiManager.getConfiguredNetworks().size());
+        } finally {
+            // For whatever reason, if the forget fails, try removing using the public remove API.
+            if (newNetworkId != INVALID_NETWORK_ID) mWifiManager.removeNetwork(newNetworkId);
+            uiAutomation.dropShellPermissionIdentity();
+        }
+    }
+
     private static class TestTrafficStateCallback implements WifiManager.TrafficStateCallback {
         private final Object mLock;
         public boolean onStateChangedCalled = false;
@@ -1300,6 +1367,10 @@
      * WifiManager.TrafficStateCallback)} by sending some traffic.
      */
     public void testTrafficStateCallback() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
         TestTrafficStateCallback trafficStateCallback = new TestTrafficStateCallback(mLock);
         UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
         try {
@@ -1331,9 +1402,145 @@
         }
     }
 
-    private void assertWifiScanningIsOn() {
-        if(!mWifiManager.isScanAlwaysAvailable()) {
-            fail("Wi-Fi scanning should be on.");
+    /**
+     * Tests {@link WifiManager#setScanAlwaysAvailable(boolean)} &
+     * {@link WifiManager#isScanAlwaysAvailable()}.
+     */
+    public void testScanAlwaysAvailable() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
+        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
+        Boolean currState = null;
+        try {
+            uiAutomation.adoptShellPermissionIdentity();
+            currState = mWifiManager.isScanAlwaysAvailable();
+            boolean newState = !currState;
+            mWifiManager.setScanAlwaysAvailable(newState);
+            PollingCheck.check(
+                    "Wifi settings toggle failed!",
+                    DURATION_SETTINGS_TOGGLE,
+                    () -> mWifiManager.isScanAlwaysAvailable() == newState);
+            assertEquals(newState, mWifiManager.isScanAlwaysAvailable());
+        } finally {
+            if (currState != null) mWifiManager.setScanAlwaysAvailable(currState);
+            uiAutomation.dropShellPermissionIdentity();
+        }
+    }
+
+    /**
+     * Tests {@link WifiManager#setScanThrottleEnabled(boolean)} &
+     * {@link WifiManager#isScanThrottleEnabled()}.
+     */
+    public void testScanThrottleEnabled() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
+        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
+        Boolean currState = null;
+        try {
+            uiAutomation.adoptShellPermissionIdentity();
+            currState = mWifiManager.isScanThrottleEnabled();
+            boolean newState = !currState;
+            mWifiManager.setScanThrottleEnabled(newState);
+            PollingCheck.check(
+                    "Wifi settings toggle failed!",
+                    DURATION_SETTINGS_TOGGLE,
+                    () -> mWifiManager.isScanThrottleEnabled() == newState);
+            assertEquals(newState, mWifiManager.isScanThrottleEnabled());
+        } finally {
+            if (currState != null) mWifiManager.setScanThrottleEnabled(currState);
+            uiAutomation.dropShellPermissionIdentity();
+        }
+    }
+
+    /**
+     * Tests {@link WifiManager#setAutoWakeupEnabled(boolean)} &
+     * {@link WifiManager#isAutoWakeupEnabled()}.
+     */
+    public void testAutoWakeUpEnabled() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
+        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
+        Boolean currState = null;
+        try {
+            uiAutomation.adoptShellPermissionIdentity();
+            currState = mWifiManager.isAutoWakeupEnabled();
+            boolean newState = !currState;
+            mWifiManager.setAutoWakeupEnabled(newState);
+            PollingCheck.check(
+                    "Wifi settings toggle failed!",
+                    DURATION_SETTINGS_TOGGLE,
+                    () -> mWifiManager.isAutoWakeupEnabled() == newState);
+            assertEquals(newState, mWifiManager.isAutoWakeupEnabled());
+        } finally {
+            if (currState != null) mWifiManager.setAutoWakeupEnabled(currState);
+            uiAutomation.dropShellPermissionIdentity();
+        }
+    }
+
+    /**
+     * Tests {@link WifiManager#setVerboseLoggingEnabled(boolean)} &
+     * {@link WifiManager#isVerboseLoggingEnabled()}.
+     */
+    public void testVerboseLoggingEnabled() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
+        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
+        Boolean currState = null;
+        try {
+            uiAutomation.adoptShellPermissionIdentity();
+            currState = mWifiManager.isVerboseLoggingEnabled();
+            boolean newState = !currState;
+            mWifiManager.setVerboseLoggingEnabled(newState);
+            PollingCheck.check(
+                    "Wifi settings toggle failed!",
+                    DURATION_SETTINGS_TOGGLE,
+                    () -> mWifiManager.isVerboseLoggingEnabled() == newState);
+            assertEquals(newState, mWifiManager.isVerboseLoggingEnabled());
+        } finally {
+            if (currState != null) mWifiManager.setVerboseLoggingEnabled(currState);
+            uiAutomation.dropShellPermissionIdentity();
+        }
+    }
+
+    /**
+     * Tests {@link WifiManager#factoryReset()}.
+     *
+     * Note: This test assumes that the device only has 1 or more saved networks before the test.
+     * The test will restore those when the test exits. But, it does not restore the softap
+     * configuration, suggestions, etc which will also have been lost on factory reset.
+     */
+    public void testFactoryReset() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
+        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
+        List<WifiConfiguration> savedNetworks = null;
+        try {
+            uiAutomation.adoptShellPermissionIdentity();
+            // These below API's only work with privileged permissions (obtained via shell identity
+            // for test)
+            savedNetworks = mWifiManager.getConfiguredNetworks();
+
+            mWifiManager.factoryReset();
+            // Ensure all the saved networks are removed.
+            assertEquals(0, mWifiManager.getConfiguredNetworks().size());
+        } finally {
+            // Restore the original saved networks.
+            if (savedNetworks != null) {
+                for (WifiConfiguration network : savedNetworks) {
+                    mWifiManager.save(network, null);
+                }
+            }
+            uiAutomation.dropShellPermissionIdentity();
         }
     }
 }
diff --git a/tests/cts/net/src/android/net/wifi/cts/WifiMigrationTest.java b/tests/cts/net/src/android/net/wifi/cts/WifiMigrationTest.java
new file mode 100644
index 0000000..6e19a21
--- /dev/null
+++ b/tests/cts/net/src/android/net/wifi/cts/WifiMigrationTest.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2020 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 static org.junit.Assert.assertNotNull;
+
+import android.net.wifi.SoftApConfiguration;
+import android.net.wifi.WifiConfiguration;
+import android.net.wifi.WifiMigration;
+import android.test.AndroidTestCase;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class WifiMigrationTest extends AndroidTestCase {
+    private static final String TEST_SSID_UNQUOTED = "testSsid1";
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            super.tearDown();
+            return;
+        }
+        super.tearDown();
+    }
+
+    /**
+     * Tests {@link android.net.wifi.WifiMigration.ConfigStoreMigrationData} class.
+     */
+    public void testWifiMigrationConfigStoreDataBuilder() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
+        WifiConfiguration savedNetwork1 = new WifiConfiguration();
+        savedNetwork1.SSID = "\"test1\"";
+        WifiConfiguration savedNetwork2 = new WifiConfiguration();
+        savedNetwork1.SSID = "\"test2\"";
+        List<WifiConfiguration> savedNetworks = Arrays.asList(savedNetwork1, savedNetwork2);
+
+        SoftApConfiguration softApConfiguration = new SoftApConfiguration.Builder()
+                .setSsid("\"test3\"")
+                .build();
+
+        WifiMigration.ConfigStoreMigrationData migrationData =
+                new WifiMigration.ConfigStoreMigrationData.Builder()
+                        .setUserSavedNetworkConfigurations(savedNetworks)
+                        .setUserSoftApConfiguration(softApConfiguration)
+                        .build();
+
+        assertNotNull(migrationData);
+        assertEquals(savedNetworks.size(),
+                migrationData.getUserSavedNetworkConfigurations().size());
+        assertEquals(savedNetwork1.SSID,
+                migrationData.getUserSavedNetworkConfigurations().get(0).SSID);
+        assertEquals(savedNetwork2.SSID,
+                migrationData.getUserSavedNetworkConfigurations().get(1).SSID);
+        assertEquals(softApConfiguration.getSsid(),
+                migrationData.getUserSoftApConfiguration().getSsid());
+    }
+
+    /**
+     * Tests {@link android.net.wifi.WifiMigration.ConfigStoreMigrationData} class.
+     */
+    public void testWifiMigrationSettingsDataBuilder() throws Exception {
+        if (!WifiFeature.isWifiSupported(getContext())) {
+            // skip the test if WiFi is not supported
+            return;
+        }
+        WifiMigration.SettingsMigrationData migrationData =
+                new WifiMigration.SettingsMigrationData.Builder()
+                        .setScanAlwaysAvailable(true)
+                        .setP2pFactoryResetPending(true)
+                        .setScanThrottleEnabled(true)
+                        .setSoftApTimeoutEnabled(true)
+                        .setWakeUpEnabled(true)
+                        .setVerboseLoggingEnabled(true)
+                        .setP2pDeviceName(TEST_SSID_UNQUOTED)
+                        .build();
+
+        assertNotNull(migrationData);
+        assertTrue(migrationData.isScanAlwaysAvailable());
+        assertTrue(migrationData.isP2pFactoryResetPending());
+        assertTrue(migrationData.isScanThrottleEnabled());
+        assertTrue(migrationData.isSoftApTimeoutEnabled());
+        assertTrue(migrationData.isWakeUpEnabled());
+        assertTrue(migrationData.isVerboseLoggingEnabled());
+        assertEquals(TEST_SSID_UNQUOTED, migrationData.getP2pDeviceName());
+    }
+}