BatteryController: Remove the /sys prefix when adding UEvent listener

UEvent DEVPATHs don't contain the /sys prefix.

Bug: 243005009
Test: atest FrameworkServicesTests
Test: manual with USI 2.0 device
Change-Id: Iec6192f1e2afaee2736e5d442937be234da1d5e4
This commit is contained in:
Prabir Pradhan
2022-10-05 19:18:19 +00:00
parent 00f7264445
commit f840dd4814
2 changed files with 11 additions and 3 deletions

View File

@@ -479,7 +479,12 @@ final class BatteryController {
handleUEventNotification(deviceId, eventTime);
}
};
mUEventManager.addListener(mUEventListener, "DEVPATH=" + batteryPath);
mUEventManager.addListener(mUEventListener, "DEVPATH=" + formatDevPath(batteryPath));
}
private String formatDevPath(String path) {
// Remove the "/sys" prefix if it has one.
return path.startsWith("/sys") ? path.substring(4) : path;
}
// This must be called when the device is no longer being monitored.

View File

@@ -260,13 +260,16 @@ class BatteryControllerTests {
@Test
fun testListenersNotifiedOnUEventNotification() {
`when`(native.getBatteryDevicePath(DEVICE_ID)).thenReturn("/test/device1")
`when`(native.getBatteryDevicePath(DEVICE_ID)).thenReturn("/sys/dev/test/device1")
`when`(native.getBatteryStatus(DEVICE_ID)).thenReturn(STATUS_CHARGING)
`when`(native.getBatteryCapacity(DEVICE_ID)).thenReturn(78)
val listener = createMockListener()
val uEventListener = ArgumentCaptor.forClass(UEventManager.UEventListener::class.java)
batteryController.registerBatteryListener(DEVICE_ID, listener, PID)
verify(uEventManager).addListener(uEventListener.capture(), eq("DEVPATH=/test/device1"))
// The device paths for UEvent notifications do not include the "/sys" prefix, so verify
// that the added listener is configured to match the path without that prefix.
verify(uEventManager)
.addListener(uEventListener.capture(), eq("DEVPATH=/dev/test/device1"))
listener.verifyNotified(DEVICE_ID, status = STATUS_CHARGING, capacity = 0.78f)
// If the battery state has changed when an UEvent is sent, the listeners are notified.