* [#167] Increase brightness when displaying QR Code * Use collectWith(lifecycleScope) * Fix copy-paste errors * manual test Co-authored-by: Carter Jernigan <git@carterjernigan.com>
This commit is contained in:
parent
54f23f9ec3
commit
8c7176aa22
|
@ -0,0 +1,8 @@
|
|||
# Testing screens with brightness flag
|
||||
1. install the app on the physical device
|
||||
1. go to screens with qr code:
|
||||
1. profile
|
||||
1. verify that the screen brightness is set to max
|
||||
1. leave the screen
|
||||
1. verify that the screen brightness returned to normal
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
package co.electriccoin.zcash.ui.common
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.test.junit4.ComposeContentTestRule
|
||||
import androidx.compose.ui.test.junit4.createComposeRule
|
||||
import androidx.test.filters.MediumTest
|
||||
import co.electriccoin.zcash.test.UiTestPrerequisites
|
||||
import co.electriccoin.zcash.ui.design.theme.ZcashTheme
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class ScreenBrightnessTest : UiTestPrerequisites() {
|
||||
@get:Rule
|
||||
val composeTestRule = createComposeRule()
|
||||
|
||||
@Test
|
||||
@MediumTest
|
||||
fun acquireAndReleaseScreenBrightness() = runTest {
|
||||
val testSetup = TestSetup(composeTestRule)
|
||||
|
||||
assertEquals(1, testSetup.getSecureBrightnessCount())
|
||||
|
||||
testSetup.mutableScreenBrightnessFlag.update { false }
|
||||
composeTestRule.awaitIdle()
|
||||
assertEquals(0, testSetup.getSecureBrightnessCount())
|
||||
}
|
||||
|
||||
private class TestSetup(composeTestRule: ComposeContentTestRule) {
|
||||
|
||||
val mutableScreenBrightnessFlag = MutableStateFlow(true)
|
||||
|
||||
private val screenBrightness = ScreenBrightness()
|
||||
|
||||
fun getSecureBrightnessCount() = screenBrightness.referenceCount.value
|
||||
|
||||
init {
|
||||
runTest {
|
||||
composeTestRule.setContent {
|
||||
CompositionLocalProvider(LocalScreenBrightness provides screenBrightness) {
|
||||
ZcashTheme {
|
||||
val secureScreen by mutableScreenBrightnessFlag.collectAsState()
|
||||
|
||||
TestView(secureScreen)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun TestView(brightenScreen: Boolean) {
|
||||
if (brightenScreen) {
|
||||
BrightenScreen()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package co.electriccoin.zcash.ui.screen.profile.view
|
||||
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.ui.test.junit4.ComposeContentTestRule
|
||||
import androidx.compose.ui.test.junit4.createComposeRule
|
||||
import androidx.test.filters.MediumTest
|
||||
import cash.z.ecc.sdk.fixture.WalletAddressFixture
|
||||
import cash.z.ecc.sdk.model.WalletAddress
|
||||
import co.electriccoin.zcash.test.UiTestPrerequisites
|
||||
import co.electriccoin.zcash.ui.common.LocalScreenBrightness
|
||||
import co.electriccoin.zcash.ui.common.ScreenBrightness
|
||||
import co.electriccoin.zcash.ui.design.theme.ZcashTheme
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class ProfileViewScreenBrightnessTest : UiTestPrerequisites() {
|
||||
@get:Rule
|
||||
val composeTestRule = createComposeRule()
|
||||
|
||||
@Test
|
||||
@MediumTest
|
||||
fun testFullBrightness() = runTest {
|
||||
val testSetup = newTestSetup(WalletAddressFixture.unified())
|
||||
|
||||
assertEquals(1, testSetup.getSecureBrightnessCount())
|
||||
}
|
||||
|
||||
private fun newTestSetup(walletAddress: WalletAddress) = TestSetup(composeTestRule, walletAddress)
|
||||
|
||||
private class TestSetup(private val composeTestRule: ComposeContentTestRule, walletAddress: WalletAddress) {
|
||||
private val screenBrightness = ScreenBrightness()
|
||||
|
||||
fun getSecureBrightnessCount() = screenBrightness.referenceCount.value
|
||||
|
||||
init {
|
||||
composeTestRule.setContent {
|
||||
CompositionLocalProvider(LocalScreenBrightness provides screenBrightness) {
|
||||
ZcashTheme {
|
||||
ZcashTheme {
|
||||
Profile(
|
||||
walletAddress,
|
||||
onBack = { },
|
||||
onAddressDetails = { },
|
||||
onAddressBook = { },
|
||||
onSettings = { },
|
||||
onCoinholderVote = {},
|
||||
onSupport = { },
|
||||
onAbout = { }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -15,9 +15,12 @@ import androidx.compose.ui.Modifier
|
|||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.NavHostController
|
||||
import cash.z.ecc.android.sdk.ext.collectWith
|
||||
import co.electriccoin.zcash.spackle.EmulatorWtfUtil
|
||||
import co.electriccoin.zcash.spackle.FirebaseTestLabUtil
|
||||
import co.electriccoin.zcash.ui.common.LocalScreenBrightness
|
||||
import co.electriccoin.zcash.ui.common.LocalScreenSecurity
|
||||
import co.electriccoin.zcash.ui.common.ScreenBrightness
|
||||
import co.electriccoin.zcash.ui.common.ScreenSecurity
|
||||
import co.electriccoin.zcash.ui.design.compat.FontCompat
|
||||
import co.electriccoin.zcash.ui.design.component.ConfigurationOverride
|
||||
|
@ -81,21 +84,9 @@ class MainActivity : ComponentActivity() {
|
|||
|
||||
private fun setupUiContent() {
|
||||
val screenSecurity = ScreenSecurity()
|
||||
lifecycleScope.launch {
|
||||
screenSecurity.referenceCount.map { it > 0 }.collect { isSecure ->
|
||||
val isTest = FirebaseTestLabUtil.isFirebaseTestLab(applicationContext) ||
|
||||
EmulatorWtfUtil.isEmulatorWtf(applicationContext)
|
||||
|
||||
if (isSecure && !isTest) {
|
||||
window.setFlags(
|
||||
WindowManager.LayoutParams.FLAG_SECURE,
|
||||
WindowManager.LayoutParams.FLAG_SECURE
|
||||
)
|
||||
} else {
|
||||
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
|
||||
}
|
||||
}
|
||||
}
|
||||
val screenBrightness = ScreenBrightness()
|
||||
observeScreenSecurityFlag(screenSecurity)
|
||||
observeScreenBrightnessFlag(screenBrightness)
|
||||
|
||||
setContent {
|
||||
Override(configurationOverrideFlow) {
|
||||
|
@ -105,7 +96,10 @@ class MainActivity : ComponentActivity() {
|
|||
.fillMaxWidth()
|
||||
.fillMaxHeight()
|
||||
) {
|
||||
CompositionLocalProvider(LocalScreenSecurity provides screenSecurity) {
|
||||
CompositionLocalProvider(
|
||||
LocalScreenSecurity provides screenSecurity,
|
||||
LocalScreenBrightness provides screenBrightness
|
||||
) {
|
||||
when (val secretState = walletViewModel.secretState.collectAsState().value) {
|
||||
SecretState.Loading -> {
|
||||
// For now, keep displaying splash screen using condition above.
|
||||
|
@ -133,8 +127,36 @@ class MainActivity : ComponentActivity() {
|
|||
// Force collection to improve performance; sync can start happening while
|
||||
// the user is going through the backup flow. Don't use eager collection in the view model,
|
||||
// so that the collection is still tied to UI lifecycle.
|
||||
lifecycleScope.launch {
|
||||
walletViewModel.synchronizer.collect {
|
||||
walletViewModel.synchronizer.collectWith(lifecycleScope) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun observeScreenSecurityFlag(screenSecurity: ScreenSecurity) {
|
||||
screenSecurity.referenceCount.map { it > 0 }.collectWith(lifecycleScope) { isSecure ->
|
||||
val isTest = FirebaseTestLabUtil.isFirebaseTestLab(applicationContext) ||
|
||||
EmulatorWtfUtil.isEmulatorWtf(applicationContext)
|
||||
|
||||
if (isSecure && !isTest) {
|
||||
window.setFlags(
|
||||
WindowManager.LayoutParams.FLAG_SECURE,
|
||||
WindowManager.LayoutParams.FLAG_SECURE
|
||||
)
|
||||
} else {
|
||||
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun observeScreenBrightnessFlag(screenBrightness: ScreenBrightness) {
|
||||
screenBrightness.referenceCount.map { it > 0 }.collectWith(lifecycleScope) { maxBrightness ->
|
||||
if (maxBrightness) {
|
||||
window.attributes = window.attributes.apply {
|
||||
this.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL
|
||||
}
|
||||
} else {
|
||||
window.attributes = window.attributes.apply {
|
||||
this.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package co.electriccoin.zcash.ui.common
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.compositionLocalOf
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.flow.updateAndGet
|
||||
|
||||
class ScreenBrightness {
|
||||
private val mutableReferenceCount: MutableStateFlow<Int> = MutableStateFlow(0)
|
||||
|
||||
val referenceCount = mutableReferenceCount.asStateFlow()
|
||||
|
||||
fun fullBrightness() {
|
||||
mutableReferenceCount.update { it + 1 }
|
||||
}
|
||||
|
||||
fun restore() {
|
||||
val after = mutableReferenceCount.updateAndGet { it - 1 }
|
||||
|
||||
if (after < 0) {
|
||||
error("Released brightness reference count too many times")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val LocalScreenBrightness = compositionLocalOf { ScreenBrightness() }
|
||||
|
||||
@Composable
|
||||
fun BrightenScreen() {
|
||||
val screenBrightness = LocalScreenBrightness.current
|
||||
DisposableEffect(screenBrightness) {
|
||||
screenBrightness.fullBrightness()
|
||||
onDispose { screenBrightness.restore() }
|
||||
}
|
||||
}
|
|
@ -25,6 +25,7 @@ import androidx.compose.ui.unit.dp
|
|||
import cash.z.ecc.sdk.fixture.WalletAddressFixture
|
||||
import cash.z.ecc.sdk.model.WalletAddress
|
||||
import co.electriccoin.zcash.ui.R
|
||||
import co.electriccoin.zcash.ui.common.BrightenScreen
|
||||
import co.electriccoin.zcash.ui.design.component.Body
|
||||
import co.electriccoin.zcash.ui.design.component.GradientSurface
|
||||
import co.electriccoin.zcash.ui.design.component.PrimaryButton
|
||||
|
@ -143,6 +144,7 @@ private fun ProfileContents(
|
|||
|
||||
@Composable
|
||||
private fun QrCode(data: String, size: Dp, modifier: Modifier) {
|
||||
BrightenScreen()
|
||||
val sizePixels = with(LocalDensity.current) { size.toPx() }.roundToInt()
|
||||
|
||||
// In the future, use actual/expect to switch QR code generator implementations for multiplatform
|
||||
|
|
Loading…
Reference in New Issue