Fix deviceState mapping in `AdblibIDeviceWrapper`

`Disconnected` device state is a synthetic state and not a device state
returned from the `adb devices` command. String value for this device
state differs between Adblib and Ddmlib, and so we should not use
it to map from one enum to the other.

Test: added
Bug: 293350579
Change-Id: I2bb065fe99b32b35b643df4c17bb4bdfc0c6650a
diff --git a/adblib-ddmlibcompatibility/src/com/android/adblib/ddmlibcompatibility/debugging/AdblibIDeviceWrapper.kt b/adblib-ddmlibcompatibility/src/com/android/adblib/ddmlibcompatibility/debugging/AdblibIDeviceWrapper.kt
index 9f7905c..6ada69a 100644
--- a/adblib-ddmlibcompatibility/src/com/android/adblib/ddmlibcompatibility/debugging/AdblibIDeviceWrapper.kt
+++ b/adblib-ddmlibcompatibility/src/com/android/adblib/ddmlibcompatibility/debugging/AdblibIDeviceWrapper.kt
@@ -111,6 +111,7 @@
     private val mAvdData = connectedDevice.scope.async { createAvdData() }
     private var mAvdName: String? = null
     private var mAvdPath: String? = null
+
     init {
         mAvdData.invokeOnCompletion { throwable ->
             if (throwable == null) {
@@ -121,6 +122,7 @@
             }
         }
     }
+
     private val mUserDataMap = UserDataMapImpl()
 
     /**
@@ -272,7 +274,14 @@
     }
 
     override fun getState(): DeviceState? {
-        return deviceState?.let { DeviceState.getState(it.state)}
+        return deviceState?.let {
+            // `DISCONNECTED` is not a device state returned from `adb devices` and so we should not
+            // use `DeviceState.getState` method which is not reliable in this case.
+            if (it == com.android.adblib.DeviceState.DISCONNECTED) {
+                return DeviceState.DISCONNECTED
+            }
+            DeviceState.getState(it.state)
+        }
     }
 
     @Deprecated("")
diff --git a/adblib-ddmlibcompatibility/test/src/com/android/adblib/ddmlibcompatibility/debugging/AdblibIDeviceWrapperTest.kt b/adblib-ddmlibcompatibility/test/src/com/android/adblib/ddmlibcompatibility/debugging/AdblibIDeviceWrapperTest.kt
index ff4e101..08da373 100644
--- a/adblib-ddmlibcompatibility/test/src/com/android/adblib/ddmlibcompatibility/debugging/AdblibIDeviceWrapperTest.kt
+++ b/adblib-ddmlibcompatibility/test/src/com/android/adblib/ddmlibcompatibility/debugging/AdblibIDeviceWrapperTest.kt
@@ -119,6 +119,19 @@
     }
 
     @Test
+    fun getState_forDisconnectedDevice() = runBlockingWithTimeout {
+        // Prepare
+        val (connectedDevice, _) = createConnectedDevice(
+            "device1", DeviceState.DeviceStatus.OFFLINE
+        )
+        val adblibIDeviceWrapper = AdblibIDeviceWrapper(connectedDevice, bridge)
+        adblibIDeviceWrapper.deviceState = com.android.adblib.DeviceState.DISCONNECTED
+
+        // Act / Assert
+        assertEquals(IDevice.DeviceState.DISCONNECTED, adblibIDeviceWrapper.state)
+    }
+
+    @Test
     fun toStringReturnsSerialNumber() = runBlockingWithTimeout {
         // Prepare
         val serialNumber = "kjdlkjsi837892"