Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
EOL- Test Menu - allow logger to work after EOL (EXPOSUREAPP-14849) (#…
Browse files Browse the repository at this point in the history
…5861)

* EOL- allow logger to work after EOL

* Fix test
  • Loading branch information
mtwalli authored Feb 23, 2023
1 parent 7c70a4d commit 41acc60
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class DebugOptionsFragment : Fragment(R.layout.fragment_test_debugoptions), Auto
vm.environmentStateChange.observe(viewLifecycleOwner) {
showSnackBar("Environment changed to: $it\nForce stop & restart the app!")
}

vm.isLoggerAllowed.observe(viewLifecycleOwner) { binding.allowLoggerEol.isChecked = it == true }
binding.allowLoggerEol.setOnCheckedChangeListener { _, checked -> vm.setAllowedFlag(checked) }
}

private infix fun String.styleTo(value: String) = buildSpannedString {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import de.rki.coronawarnapp.environment.EnvironmentSetup
import de.rki.coronawarnapp.environment.EnvironmentSetup.Type.Companion.toEnvironmentType
import de.rki.coronawarnapp.eol.EolSetting
import de.rki.coronawarnapp.test.debugoptions.ui.EnvironmentState.Companion.toEnvironmentState
import de.rki.coronawarnapp.util.coroutine.AppScope
import de.rki.coronawarnapp.util.coroutine.DispatcherProvider
Expand All @@ -17,6 +18,7 @@ import kotlinx.coroutines.launch

class DebugOptionsFragmentViewModel @AssistedInject constructor(
private val envSetup: EnvironmentSetup,
private val eolSetting: EolSetting,
dispatcherProvider: DispatcherProvider,
private val environmentSunset: EnvironmentSunset,
@AppScope private val appScope: CoroutineScope,
Expand All @@ -25,6 +27,7 @@ class DebugOptionsFragmentViewModel @AssistedInject constructor(
private val environmentStateFlow = MutableStateFlow(envSetup.toEnvironmentState())
val environmentState = environmentStateFlow.asLiveData(context = dispatcherProvider.Default)
val environmentStateChange = SingleLiveEvent<EnvironmentState>()
val isLoggerAllowed = eolSetting.isLoggerAllowed.asLiveData2()

fun clearLaunchEnvironment() {
envSetup.launchEnvironment = null
Expand All @@ -44,6 +47,10 @@ class DebugOptionsFragmentViewModel @AssistedInject constructor(
cleanCachedData()
}

fun setAllowedFlag(flag: Boolean) = launch {
eolSetting.setLoggerAllowed(flag)
}

private fun cleanCachedData() = appScope.launch {
environmentSunset.reset()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@
android:text="Open new debug log screen"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/new_debuglog_screen_explanation" />

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/allow_logger_eol"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Allow logger after EOL"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/show_debug_log_screen" />
</androidx.constraintlayout.widget.ConstraintLayout>

<LinearLayout
Expand Down
11 changes: 8 additions & 3 deletions Corona-Warn-App/src/main/java/de/rki/coronawarnapp/eol/AppEol.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.onEach
import timber.log.Timber
import javax.inject.Inject
Expand All @@ -32,7 +33,7 @@ import javax.inject.Singleton
@Singleton
@Suppress("LongParameterList")
class AppEol @Inject constructor(
eolSetting: EolSetting,
private val eolSetting: EolSetting,
private val enfClient: ENFClient,
private val workManager: WorkManager,
private val timeStamper: TimeStamper,
Expand Down Expand Up @@ -67,8 +68,12 @@ class AppEol @Inject constructor(
)
alarmManager.cancel(intentFactory.createIntent())

Timber.tag(TAG).d("Stop logger")
debugLogger.stop()
if (!eolSetting.isLoggerAllowed.first()) {
Timber.tag(TAG).d("Stop logger")
debugLogger.stop()
} else {
Timber.tag(TAG).d("Logger is allowed after EOL")
}

runCatching {
Timber.tag(TAG).d("Disable ENF")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package de.rki.coronawarnapp.eol

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import kotlinx.coroutines.flow.map
Expand All @@ -27,7 +28,18 @@ class EolSetting @Inject constructor(
}
}

val isLoggerAllowed = dataStore.data.map { prefs ->
runCatching { prefs[EOL_LOGGER_FLAG] ?: false }.getOrElse { false }
}

suspend fun setLoggerAllowed(flag: Boolean) {
dataStore.edit { prefs ->
prefs[EOL_LOGGER_FLAG] = flag
}
}

companion object {
private val EOL_DATE_TIME = stringPreferencesKey("EolSetting.eolDateTime")
private val EOL_LOGGER_FLAG = booleanPreferencesKey("EolSetting.eolLoggerAllowed")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class AppEolTest : BaseTest() {
coEvery { debugLogger.stop() } just Runs

every { eolSetting.eolDateTime } returns flowOf(ZonedDateTime.parse("2023-06-01T00:00:00+02:00"))
every { eolSetting.isLoggerAllowed } returns flowOf(false)
every { alarmManager.cancel(any<PendingIntent>()) } just Runs
every { workManager.cancelAllWork() } returns mockk()
every { appShortcutsHelper.initShortcuts(any()) } returns mockk()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package de.rki.coronawarnapp.test.debugoptions.ui

import de.rki.coronawarnapp.environment.EnvironmentSetup
import de.rki.coronawarnapp.eol.EolSetting
import io.kotest.matchers.shouldBe
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.TestScope
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand All @@ -18,6 +20,7 @@ class DebugOptionsFragmentViewModelTest : testhelpers.BaseTest() {

@MockK private lateinit var environmentSetup: EnvironmentSetup
@MockK private lateinit var environmentSunset: EnvironmentSunset
@MockK private lateinit var eolSetting: EolSetting

private var currentEnvironment = EnvironmentSetup.Type.DEV

Expand All @@ -40,13 +43,15 @@ class DebugOptionsFragmentViewModelTest : testhelpers.BaseTest() {
every { environmentSetup.currentEnvironment = any() } answers { currentEnvironment = arg(0) }
every { environmentSetup.currentEnvironment } answers { currentEnvironment }
every { environmentSetup.launchEnvironment } returns null
every { eolSetting.isLoggerAllowed } returns flowOf(false)
}

private fun createViewModel(): DebugOptionsFragmentViewModel = DebugOptionsFragmentViewModel(
envSetup = environmentSetup,
dispatcherProvider = TestDispatcherProvider(),
environmentSunset = environmentSunset,
appScope = TestScope()
appScope = TestScope(),
eolSetting = eolSetting,
)

@Test
Expand Down

0 comments on commit 41acc60

Please sign in to comment.