Skip to content

Commit

Permalink
fix some tests, protect from crash in tests (no shell)
Browse files Browse the repository at this point in the history
  • Loading branch information
hg42 committed Aug 23, 2024
1 parent 1503caa commit 12ee66e
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 126 deletions.
50 changes: 50 additions & 0 deletions src/androidTest/kotlin/research/Try_composeTest.kt
Original file line number Diff line number Diff line change
@@ -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
)
}
242 changes: 122 additions & 120 deletions src/androidTest/kotlin/tests_ui/Test_UI_FollowNonStateVariable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String>()
val queue1 = mutableListOf<String>()
val queue2 = ConcurrentLinkedQueue<String>()
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<String>()
val queue1 = mutableListOf<String>()
val queue2 = ConcurrentLinkedQueue<String>()
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<MainActivityX>()

@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<MainActivityX>()

@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 }
}
}
15 changes: 10 additions & 5 deletions src/androidTest/kotlin/tests_ui/Test_UI_SelectionSaveLoad.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<MainActivityX>()
@get:Rule
//var test = createAndroidComposeRule<MainActivityX>()
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")
Expand Down Expand Up @@ -74,5 +78,6 @@ class Test_SelectionSaveLoad {
val count = test.onAllNodesWithText(selectionName).fetchSemanticsNodes().size
assertEquals("menu entries", count, 1)
}
test.onRoot().printToLog("after")
}
}
Loading

0 comments on commit 12ee66e

Please sign in to comment.