WifiManagerTest: Add CTS tests for factory reset

Bug: 150236894
Test: atest android.net.wifi.cts.WifiManagerTest
Change-Id: I0abaf771f2016398eea104b8ff0a9e7a8ca5017e
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 25ce7be..98f3d44 100644
--- a/tests/cts/net/src/android/net/wifi/cts/WifiManagerTest.java
+++ b/tests/cts/net/src/android/net/wifi/cts/WifiManagerTest.java
@@ -1473,4 +1473,34 @@
             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 {
+        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();
+        }
+    }
 }