From 12ee66e7f82711f00fec0895685a910bcf8627ad Mon Sep 17 00:00:00 2001 From: hg42 Date: Tue, 13 Aug 2024 02:51:29 +0200 Subject: [PATCH] fix some tests, protect from crash in tests (no shell) --- .../kotlin/research/Try_composeTest.kt | 50 ++++ .../Test_UI_FollowNonStateVariable.kt | 242 +++++++++--------- .../tests_ui/Test_UI_SelectionSaveLoad.kt | 15 +- .../backup/handler/ShellHandler.kt | 4 +- 4 files changed, 185 insertions(+), 126 deletions(-) create mode 100644 src/androidTest/kotlin/research/Try_composeTest.kt diff --git a/src/androidTest/kotlin/research/Try_composeTest.kt b/src/androidTest/kotlin/research/Try_composeTest.kt new file mode 100644 index 000000000..20ccbffdf --- /dev/null +++ b/src/androidTest/kotlin/research/Try_composeTest.kt @@ -0,0 +1,50 @@ +package tests.research + +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.junit4.createComposeRule +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.tooling.preview.Preview +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class Try_composeTest { + + @get:Rule + val test = createComposeRule() + + @Test + fun testComposableDisplaysCorrectText() { + // Arrange + val testName = "TestUser" + + // Act + test.setContent { + TestComposable(name = testName) + } + + // Assert + test.onNodeWithText("Hello $testName!") + .assertIsDisplayed() + } +} + +@Preview(showBackground = true) +@Composable +fun TestComposablePreview() { + TestComposable("Preview") +} + +@Composable +fun TestComposable(name: String, modifier: Modifier = Modifier) { + + Text( + text = "Hello $name!", + modifier = modifier + ) +} diff --git a/src/androidTest/kotlin/tests_ui/Test_UI_FollowNonStateVariable.kt b/src/androidTest/kotlin/tests_ui/Test_UI_FollowNonStateVariable.kt index aacdcda66..e6e969290 100644 --- a/src/androidTest/kotlin/tests_ui/Test_UI_FollowNonStateVariable.kt +++ b/src/androidTest/kotlin/tests_ui/Test_UI_FollowNonStateVariable.kt @@ -20,12 +20,13 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color -import androidx.compose.ui.test.junit4.ComposeContentTestRule -import androidx.compose.ui.test.junit4.createComposeRule +import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.machiav3lli.backup.activities.MainActivityX import com.machiav3lli.backup.preferences.TerminalText import com.machiav3lli.backup.ui.compose.isAtBottom import kotlinx.coroutines.GlobalScope @@ -34,157 +35,158 @@ import kotlinx.coroutines.launch import org.junit.Before import org.junit.Rule import org.junit.Test +import org.junit.runner.RunWith import timber.log.Timber import java.util.concurrent.ConcurrentLinkedQueue +//val queue1 = mutableStateListOf() +val queue1 = mutableListOf() +val queue2 = ConcurrentLinkedQueue() +var counter = 0 + +val init = run { + queue1.add("aaa") + queue2.add("aaa") + GlobalScope.launch { + repeat(20) { + counter += 1 + delay(50) + queue1.add("some added text $counter") + queue2.add("some added text $counter") + } + } +} + + +@RunWith(AndroidJUnit4::class) class Test_UI_FollowNonStateVariable { - //val queue1 = mutableStateListOf() - val queue1 = mutableListOf() - val queue2 = ConcurrentLinkedQueue() - var counter = 0 - - val init = run { - queue1.add("aaa") - queue2.add("aaa") - GlobalScope.launch { - repeat(20) { - counter += 1 - delay(50) - queue1.add("some added text $counter") - queue2.add("some added text $counter") + @get:Rule + val test = createAndroidComposeRule() + + @Before + fun setUp() { + test.setContent { + Column { + TestPreview() + //TerminalTextPreview() } } + //test.onRoot().printToLog("root") } - @Preview - @Composable - fun TerminalTextPreview() { + @Test + fun test_findList() { - Timber.d("recompose ${queue2.size}") + test.waitForIdle() + test.waitUntil(10000) { test.onAllNodesWithText("some added text 20").fetchSemanticsNodes().count() >= 2 } + } +} - val lines = queue2 +@Preview +@Composable +fun TerminalTextPreview() { - var recompose by remember { mutableStateOf(0) } + Timber.d("recompose ${queue2.size}") - LaunchedEffect(Unit) { - launch { - while (true) { - delay(100) - recompose++ - } + val lines = queue2 + + var recompose by remember { mutableStateOf(0) } + + LaunchedEffect(Unit) { + launch { + while (true) { + delay(100) + recompose++ } } + } - Box( - modifier = Modifier - .height(300.dp) - .width(500.dp) - .padding(0.dp) - .background(color = Color(0.2f, 0.2f, 0.3f)) - ) { - TerminalText(lines.toList()) - } + Box( + modifier = Modifier + .height(300.dp) + .width(500.dp) + .padding(0.dp) + .background(color = Color(0.2f, 0.2f, 0.3f)) + ) { + TerminalText(lines.toList()) } +} +@Preview +@Composable +fun TestPreview() { - @Preview - @Composable - fun TestPreview() { + Timber.d("recompose ${queue1.size} ${queue2.size}") - Timber.d("recompose ${queue1.size} ${queue2.size}") + val lines1 = queue1 + val lines2 = queue2 - val lines1 = queue1 - val lines2 = queue2 + val listState = rememberLazyListState() - val listState = rememberLazyListState() + var recompose by remember { mutableStateOf(0) } - var recompose by remember { mutableStateOf(0) } + LaunchedEffect(Unit) { + launch { + while (true) { + delay(100) + recompose++ + } + } + } - LaunchedEffect(Unit) { + if (listState.isAtBottom()) + LaunchedEffect(recompose) { launch { - while (true) { - delay(100) - recompose++ - } + listState.scrollToItem(Int.MAX_VALUE) } } - if (listState.isAtBottom()) - LaunchedEffect(recompose) { - launch { - listState.scrollToItem(Int.MAX_VALUE) + Box( + modifier = Modifier + .height(300.dp) + .width(500.dp) + .padding(0.dp) + .background(color = Color(0.2f, 0.2f, 0.3f)) + ) { + Row { + LazyColumn( + modifier = Modifier.weight(0.5f), + state = listState + ) { + item { + Text( + "${queue1::class.simpleName}", + color = Color.White, fontSize = 10.sp + ) + } + items(lines1) { + Text( + it, + modifier = Modifier.fillMaxWidth(), + color = Color.White + ) } } - - Box( - modifier = Modifier - .height(300.dp) - .width(500.dp) - .padding(0.dp) - .background(color = Color(0.2f, 0.2f, 0.3f)) - ) { - Row { - LazyColumn( - modifier = Modifier.weight(0.5f), - state = listState - ) { - item { - Text( - "${queue1::class.simpleName}", - color = Color.White, fontSize = 10.sp - ) - } - items(lines1) { - Text( - it, - modifier = Modifier.fillMaxWidth(), - color = Color.White - ) - } + LazyColumn( + modifier = Modifier.weight(0.5f), + state = listState + ) { + item { + Text( + "${queue2::class.simpleName}", + color = Color.White, fontSize = 10.sp + ) } - LazyColumn( - modifier = Modifier.weight(0.5f), - state = listState - ) { - item { - Text( - "${queue2::class.simpleName}", - color = Color.White, fontSize = 10.sp - ) - } - items(lines2.toList()) { - Text( - it, - modifier = Modifier.fillMaxWidth(), - color = Color.White - ) - } + items(lines2.toList()) { + Text( + it, + modifier = Modifier.fillMaxWidth(), + color = Color.White + ) } } } } +} - @Rule - @JvmField - var test: ComposeContentTestRule = createComposeRule() - //var test: ComposeContentTestRule = createAndroidComposeRule() - - @Before - fun setUp() { - test.setContent { - Column { - TestPreview() - //TerminalTextPreview() - } - } - //test.onRoot().printToLog("root") - } - - - @Test - fun test_findList() { - test.waitForIdle() - test.waitUntil(10000) { test.onAllNodesWithText("some added text 20").fetchSemanticsNodes().count() >= 2 } - } -} \ No newline at end of file diff --git a/src/androidTest/kotlin/tests_ui/Test_UI_SelectionSaveLoad.kt b/src/androidTest/kotlin/tests_ui/Test_UI_SelectionSaveLoad.kt index 86b136b4c..ba2bcc749 100644 --- a/src/androidTest/kotlin/tests_ui/Test_UI_SelectionSaveLoad.kt +++ b/src/androidTest/kotlin/tests_ui/Test_UI_SelectionSaveLoad.kt @@ -4,7 +4,6 @@ import androidx.compose.ui.test.assertIsFocused import androidx.compose.ui.test.click import androidx.compose.ui.test.hasTestTag import androidx.compose.ui.test.hasText -import androidx.compose.ui.test.junit4.ComposeContentTestRule import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.longClick import androidx.compose.ui.test.onAllNodesWithText @@ -14,29 +13,34 @@ import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.performTextInput import androidx.compose.ui.test.performTouchInput import androidx.compose.ui.test.printToLog +import androidx.test.ext.junit.runners.AndroidJUnit4 import com.machiav3lli.backup.activities.MainActivityX import junit.framework.TestCase.assertEquals import org.junit.Before import org.junit.Rule import org.junit.Test +import org.junit.runner.RunWith import tests.onNodeWait import tests.onNodeWaitOrAssert import timber.log.Timber +@RunWith(AndroidJUnit4::class) class Test_SelectionSaveLoad { - @Rule - @JvmField - var test: ComposeContentTestRule = createAndroidComposeRule() + @get:Rule + //var test = createAndroidComposeRule() + var test = createAndroidComposeRule(MainActivityX::class.java) + //val test = createComposeRule() @Before fun setUp() { //test.setContent { } - test.onRoot().printToLog("root") + //test.onRoot().printToLog("root") } @Test fun test_findList() { + test.onRoot().printToLog("before") test.waitForIdle() val column = test.onNodeWait(hasTestTag("VerticalItemList.Column"), 10000) column?.printToLog("column") ?: Timber.d("----------", "ERROR") @@ -74,5 +78,6 @@ class Test_SelectionSaveLoad { val count = test.onAllNodesWithText(selectionName).fetchSemanticsNodes().size assertEquals("menu entries", count, 1) } + test.onRoot().printToLog("after") } } \ No newline at end of file diff --git a/src/main/java/com/machiav3lli/backup/handler/ShellHandler.kt b/src/main/java/com/machiav3lli/backup/handler/ShellHandler.kt index 61eb3d42c..da327d425 100644 --- a/src/main/java/com/machiav3lli/backup/handler/ShellHandler.kt +++ b/src/main/java/com/machiav3lli/backup/handler/ShellHandler.kt @@ -173,7 +173,9 @@ class ShellHandler { Shell.enableVerboseLogging = isDebug initPrivilegedShell() - baseInfo().forEach { Timber.i(it) } + runCatching { + baseInfo().forEach { Timber.i(it) } + } utilBoxes = mutableListOf() try {