Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Lazy column tests #189

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ class CounterDisplayTest {
composeRule.onNodeWithText("Copy").performClick()
composeRule.onNodeWithTag("Counter Display").assertTextEquals("Invalid entry")
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.jetpack_compose_all_in_one.lessons.lesson_9

import androidx.activity.compose.setContent
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performScrollToNode
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.jetpack_compose_all_in_one.ui.views.main_ui.MainActivity
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class LazyColumnTestingExample {
@get:Rule
val composeRule = createAndroidComposeRule<MainActivity>()

@Before
fun setup() {
composeRule.activity.setContent {
LazyColumnTesting()
}
}

@Test
fun testAllItemsVisible(){
composeRule.apply {
onNodeWithText("1").assertIsDisplayed()
onNodeWithTag("lazyColumn").performScrollToNode(hasText("20"))
onNodeWithText("19").assertIsDisplayed()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringArrayResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -31,7 +34,7 @@ private fun LessonContent() {
val currentPage = rememberSaveable { mutableIntStateOf(0) }

LogicPager(
pageCount = 2,
pageCount = 3,
currentPage = currentPage
) {
Column(
Expand All @@ -50,6 +53,7 @@ private fun LessonContent() {
when (currentPage.value) {
0 -> CounterDisplay()
1 -> ArticleDemo()
2 -> LazyColumnTesting()
}
}
}
Expand Down Expand Up @@ -122,6 +126,18 @@ fun ArticleContent(content: String, onClick: () -> Unit = {}) {
}
}

@Composable
fun LazyColumnTesting(){

val myList = (1..20).toList()

LazyColumn(modifier = Modifier.testTag("lazyColumn")) {
items(myList){
androidx.compose.material3.Text(modifier = Modifier.padding(10.dp), text = it.toString())
}
}
}

@Preview
@Composable
fun PreviewArticleContent() {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<string-array name="lesson_9_ui_test">
<item>UI TEST Demo 1</item>
<item>UI TEST Demo 2</item>
<item>UI Test Demo 3</item>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<item>UI Test Demo 3</item>
<item>UI Test Demo 3rd</item>

</string-array>

<string-array name="l2c5_header_text">
Expand Down