From 18942b2138b7a4776ee1459463d8be3c4a6d451c Mon Sep 17 00:00:00 2001 From: Nikita Ogorodnikov Date: Fri, 9 Dec 2022 11:58:24 +0100 Subject: [PATCH] Fix flaky BroadcastReceiverSystemInfoProvider test --- .../system/BroadcastReceiverSystemInfoProvider.kt | 5 ++++- .../BroadcastReceiverSystemInfoProviderTest.kt | 15 ++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/dd-sdk-android/src/main/kotlin/com/datadog/android/core/internal/system/BroadcastReceiverSystemInfoProvider.kt b/dd-sdk-android/src/main/kotlin/com/datadog/android/core/internal/system/BroadcastReceiverSystemInfoProvider.kt index 5c7eee3995..a2dc9c7556 100644 --- a/dd-sdk-android/src/main/kotlin/com/datadog/android/core/internal/system/BroadcastReceiverSystemInfoProvider.kt +++ b/dd-sdk-android/src/main/kotlin/com/datadog/android/core/internal/system/BroadcastReceiverSystemInfoProvider.kt @@ -16,6 +16,7 @@ import android.os.PowerManager import com.datadog.android.core.internal.receiver.ThreadSafeReceiver import com.datadog.android.core.internal.utils.sdkLogger import com.datadog.android.log.internal.utils.debugWithTelemetry +import kotlin.math.roundToInt internal class BroadcastReceiverSystemInfoProvider( private val buildSdkVersionProvider: BuildSdkVersionProvider = DefaultBuildSdkVersionProvider() @@ -79,7 +80,9 @@ internal class BroadcastReceiverSystemInfoProvider( val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, DEFAULT_BATTERY_SCALE) val pluggedStatus = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, BATTERY_UNPLUGGED) val batteryStatus = SystemInfo.BatteryStatus.fromAndroidStatus(status) - val batteryLevel = (level * DEFAULT_BATTERY_SCALE) / scale + + @Suppress("UnsafeThirdPartyFunctionCall") // Not a NaN here + val batteryLevel = ((level * DEFAULT_BATTERY_SCALE.toFloat()) / scale).roundToInt() val onExternalPowerSource = pluggedStatus in PLUGGED_IN_STATUS_VALUES val batteryFullOrCharging = batteryStatus in batteryFullOrChargingStatus systemInfo = systemInfo.copy( diff --git a/dd-sdk-android/src/test/kotlin/com/datadog/android/core/internal/system/BroadcastReceiverSystemInfoProviderTest.kt b/dd-sdk-android/src/test/kotlin/com/datadog/android/core/internal/system/BroadcastReceiverSystemInfoProviderTest.kt index a981195d90..397a4de798 100644 --- a/dd-sdk-android/src/test/kotlin/com/datadog/android/core/internal/system/BroadcastReceiverSystemInfoProviderTest.kt +++ b/dd-sdk-android/src/test/kotlin/com/datadog/android/core/internal/system/BroadcastReceiverSystemInfoProviderTest.kt @@ -41,6 +41,7 @@ import org.mockito.junit.jupiter.MockitoSettings import org.mockito.quality.Strictness import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit +import kotlin.math.roundToInt @Extensions( ExtendWith(MockitoExtension::class), @@ -129,7 +130,7 @@ internal class BroadcastReceiverSystemInfoProviderTest { whenever(mockBuildSdkVersionProvider.version()) doReturn Build.VERSION_CODES.LOLLIPOP val batteryIntent: Intent = mock() - val scaledLevel = (level * scale) / 100 + val scaledLevel = ((level * scale) / 100f).roundToInt() whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_STATUS), any())) .doReturn(status.androidStatus()) whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_PLUGGED), any())) @@ -166,7 +167,7 @@ internal class BroadcastReceiverSystemInfoProviderTest { whenever(mockBuildSdkVersionProvider.version()) doReturn Build.VERSION_CODES.KITKAT val batteryIntent: Intent = mock() - val scaledLevel = (level * scale) / 100 + val scaledLevel = ((level * scale) / 100f).roundToInt() whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_STATUS), any())) .doReturn(status.androidStatus()) whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_PLUGGED), any())) @@ -221,7 +222,7 @@ internal class BroadcastReceiverSystemInfoProviderTest { @IntForgery(min = 50, max = 10000) scale: Int ) { // Given - val scaledLevel = (level * scale) / 100 + val scaledLevel = ((level * scale) / 100f).roundToInt() whenever(mockIntent.getIntExtra(eq(BatteryManager.EXTRA_STATUS), any())) .doReturn(status.androidStatus()) whenever(mockIntent.getIntExtra(eq(BatteryManager.EXTRA_LEVEL), any())) doReturn scaledLevel @@ -337,7 +338,7 @@ internal class BroadcastReceiverSystemInfoProviderTest { @IntForgery(min = 50, max = 10000) scale: Int ) { // Given - val scaledLevel = (level * scale) / 100 + val scaledLevel = ((level * scale) / 100f).roundToInt() val batteryIntent: Intent = mock() whenever( batteryIntent.getIntExtra( @@ -371,7 +372,7 @@ internal class BroadcastReceiverSystemInfoProviderTest { @IntForgery(min = 50, max = 10000) scale: Int ) { // Given - val scaledLevel = (level * scale) / 100 + val scaledLevel = ((level * scale) / 100f).roundToInt() val batteryIntent: Intent = mock() whenever( batteryIntent.getIntExtra( @@ -409,7 +410,7 @@ internal class BroadcastReceiverSystemInfoProviderTest { @IntForgery(min = 50, max = 10000) scale: Int ) { // Given - val scaledLevel = (level * scale) / 100 + val scaledLevel = ((level * scale) / 100f).roundToInt() val batteryIntent: Intent = mock() whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_SCALE), any())) doReturn scale whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_LEVEL), any())) doReturn @@ -435,7 +436,7 @@ internal class BroadcastReceiverSystemInfoProviderTest { @IntForgery(min = 50, max = 10000) scale: Int ) { // Given - val scaledLevel = (level * scale) / 100 + val scaledLevel = ((level * scale) / 100f).roundToInt() val batteryIntent: Intent = mock() whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_SCALE), any())) doReturn scale whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_LEVEL), any())) doReturn