WifiStateMachine: Return Succeeded when connecting to the same network

If a caller sends connection request to connect to an already connected
network, we reply with a CONNECT_NETWORK_SUCCEEDED message.

Added one unit test in WifiStateMachineTest that creates and connects to
a network, and then tries to reconnect to the same network.

Bug: 35360668
Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh
Test: frameworks/base/wifi/tests/runtests.sh
Change-Id: I00da335c50f2c98de4ea12a59e927a0af0a54728
diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java
index af845d9..fe56111 100644
--- a/service/java/com/android/server/wifi/WifiStateMachine.java
+++ b/service/java/com/android/server/wifi/WifiStateMachine.java
@@ -5542,6 +5542,7 @@
                 case WifiManager.CONNECT_NETWORK:
                     int netId = message.arg1;
                     if (mWifiInfo.getNetworkId() == netId) {
+                        replyToMessage(message, WifiManager.CONNECT_NETWORK_SUCCEEDED);
                         break;
                     }
                     return NOT_HANDLED;
diff --git a/service/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java b/service/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java
index 06e85af..669c9fd 100644
--- a/service/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java
+++ b/service/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java
@@ -836,6 +836,43 @@
         assertEquals("ConnectedState", getCurrentState().getName());
     }
 
+    /**
+     * If caller tries to connect to a network that is already connected, the connection request
+     * should succeed.
+     *
+     * Test: Create and connect to a network, then try to reconnect to the same network. Verify
+     * that connection request returns with CONNECT_NETWORK_SUCCEEDED.
+     */
+    @Test
+    public void reconnectToConnectedNetwork() throws Exception {
+        addNetworkAndVerifySuccess();
+
+        mWsm.setOperationalMode(WifiStateMachine.CONNECT_MODE);
+        mLooper.dispatchAll();
+
+        mLooper.startAutoDispatch();
+        mWsm.syncEnableNetwork(mWsmAsyncChannel, 0, true);
+        mLooper.stopAutoDispatch();
+
+        verify(mWifiConfigManager).enableNetwork(eq(0), eq(true), anyInt());
+
+        mWsm.sendMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, sBSSID);
+        mLooper.dispatchAll();
+
+        mWsm.sendMessage(WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT, 0, 0,
+                new StateChangeResult(0, sWifiSsid, sBSSID, SupplicantState.COMPLETED));
+        mLooper.dispatchAll();
+
+        assertEquals("ObtainingIpState", getCurrentState().getName());
+
+        // try to reconnect
+        mLooper.startAutoDispatch();
+        Message reply = mWsmAsyncChannel.sendMessageSynchronously(WifiManager.CONNECT_NETWORK, 0);
+        mLooper.stopAutoDispatch();
+
+        assertEquals(WifiManager.CONNECT_NETWORK_SUCCEEDED, reply.what);
+    }
+
     @Test
     public void testDhcpFailure() throws Exception {
         addNetworkAndVerifySuccess();