Skip to content

Commit 15627e6

Browse files
committed
test: write tests for SystemBridgeAutoStarter.kt
1 parent d01ba3d commit 15627e6

4 files changed

Lines changed: 542 additions & 7 deletions

File tree

base/src/main/java/io/github/sds100/keymapper/base/promode/SystemBridgeAutoStarter.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package io.github.sds100.keymapper.base.promode
22

3+
import android.annotation.SuppressLint
34
import android.os.Build
4-
import android.os.SystemClock
55
import androidx.annotation.RequiresApi
66
import androidx.core.app.NotificationCompat
77
import io.github.sds100.keymapper.base.BaseMainActivity
88
import io.github.sds100.keymapper.base.R
99
import io.github.sds100.keymapper.base.system.notifications.NotificationController.Companion.CHANNEL_SETUP_ASSISTANT
1010
import io.github.sds100.keymapper.base.system.notifications.NotificationController.Companion.ID_SYSTEM_BRIDGE_STATUS
1111
import io.github.sds100.keymapper.base.utils.ui.ResourceProvider
12+
import io.github.sds100.keymapper.common.BuildConfigProvider
1213
import io.github.sds100.keymapper.common.notifications.KMNotificationAction
14+
import io.github.sds100.keymapper.common.utils.Clock
1315
import io.github.sds100.keymapper.common.utils.Constants
1416
import io.github.sds100.keymapper.data.Keys
1517
import io.github.sds100.keymapper.data.PreferenceDefaults
@@ -54,6 +56,8 @@ import timber.log.Timber
5456
@Singleton
5557
class SystemBridgeAutoStarter @Inject constructor(
5658
private val coroutineScope: CoroutineScope,
59+
private val buildConfig: BuildConfigProvider,
60+
private val clock: Clock,
5761
private val suAdapter: SuAdapter,
5862
private val shizukuAdapter: ShizukuAdapter,
5963
private val connectionManager: SystemBridgeConnectionManager,
@@ -71,6 +75,7 @@ class SystemBridgeAutoStarter @Inject constructor(
7175
}
7276

7377
// Use flatMapLatest so that any calls to ADB are only done if strictly necessary.
78+
@SuppressLint("NewApi")
7479
@OptIn(ExperimentalCoroutinesApi::class)
7580
private val autoStartTypeFlow: Flow<AutoStartType?> =
7681
suAdapter.isRootGranted.flatMapLatest { isRooted ->
@@ -88,7 +93,7 @@ class SystemBridgeAutoStarter @Inject constructor(
8893
useShizukuFlow.flatMapLatest { useShizuku ->
8994
if (useShizuku) {
9095
flowOf(AutoStartType.SHIZUKU)
91-
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
96+
} else if (buildConfig.sdkInt >= Build.VERSION_CODES.R) {
9297
val isAdbAutoStartAllowed = combine(
9398
permissionAdapter.isGrantedFlow(Permission.WRITE_SECURE_SETTINGS),
9499
networkAdapter.isWifiConnected,
@@ -150,7 +155,11 @@ class SystemBridgeAutoStarter @Inject constructor(
150155
// The Key Mapper process may not necessarily be started on boot due to the
151156
// on boot receiver so assume if it is started within a minute of boot that
152157
// it should be auto started.
153-
val isBoot = SystemClock.uptimeMillis() < 60000
158+
val isBoot = clock.elapsedRealtime() < 60000
159+
160+
Timber.i(
161+
"SystemBridgeAutoStarter init: isBoot=$isBoot",
162+
)
154163

155164
if (isBoot) {
156165
handleAutoStartOnBoot()
@@ -180,7 +189,7 @@ class SystemBridgeAutoStarter @Inject constructor(
180189
val isCleanShutdown = preferences.get(Keys.isCleanShutdown).map { it ?: false }.first()
181190

182191
Timber.i(
183-
"SystemBridgeAutoStarter init: isBoot=true, isCleanShutdown=$isCleanShutdown",
192+
"SystemBridgeAutoStarter init: isCleanShutdown=$isCleanShutdown",
184193
)
185194

186195
// Reset the value after reading it.
@@ -234,7 +243,7 @@ class SystemBridgeAutoStarter @Inject constructor(
234243
return
235244
}
236245

237-
lastAutoStartTime = SystemClock.elapsedRealtime()
246+
lastAutoStartTime = clock.elapsedRealtime()
238247

239248
when (type) {
240249
AutoStartType.ADB -> {

0 commit comments

Comments
 (0)