Skip to content

Commit

Permalink
πŸ› Fixed bugs in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzovngl committed Oct 23, 2023
1 parent 3db1a1d commit b190710
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.rule.GrantPermissionRule
import com.lorenzovainigli.foodexpirationdates.R
import com.lorenzovainigli.foodexpirationdates.view.activity.MainActivity
import com.lorenzovainigli.foodexpirationdates.view.composable.DateFormatDialog
Expand All @@ -24,19 +24,25 @@ internal class SettingsActivityTest {
@get:Rule
val testRule = createAndroidComposeRule(MainActivity::class.java)

@Rule
@JvmField
var mGrantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(
"android.permission.POST_NOTIFICATIONS"
)

@Test
fun changeDateFormat() {
testRule.onNodeWithContentDescription(testRule.activity.getString(R.string.menu))
.performClick()
testRule.onNodeWithText(testRule.activity.getString(R.string.settings)).performClick()
testRule.onNodeWithTag(testRule.activity.getString(R.string.date_format)).performClick()
testRule.onNodeWithTag(DateFormatDialog).assertIsDisplayed()
val dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault())
val firstRow = testRule.onAllNodesWithTag(DateFormatRow)[0]
val currentDate = dateFormat.format(Calendar.getInstance().time)
firstRow.assertTextContains(currentDate)
firstRow.performClick()
testRule.onNodeWithTag(testRule.activity.getString(R.string.date_format)).assertTextContains(currentDate)
testRule.run {
onNodeWithText(testRule.activity.getString(R.string.settings)).performClick()
onNodeWithTag(testRule.activity.getString(R.string.date_format)).performClick()
onNodeWithTag(DateFormatDialog).assertIsDisplayed()
val dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault())
val firstRow = testRule.onAllNodesWithTag(DateFormatRow)[0]
val currentDate = dateFormat.format(Calendar.getInstance().time)
firstRow.assertTextContains(currentDate)
firstRow.performClick()
onNodeWithTag(testRule.activity.getString(R.string.date_format)).assertTextContains(currentDate)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

@LargeTest
@RunWith(AndroidJUnit4::class)
Expand Down Expand Up @@ -74,20 +76,27 @@ class MainActivityTest {
private fun clear(){
composeTestRule.run {
onAllNodesWithTag("Delete item").apply {
fetchSemanticsNodes().forEachIndexed { i, _ ->
fetchSemanticsNodes().forEachIndexed { _, _ ->
get(0).performClick()
}
}
onNodeWithTag(FOOD_CARD).assertDoesNotExist()
}
}

fun formatDateForDatePicker(
calendar: Calendar = Calendar.getInstance()
): String {
val dateFormat = SimpleDateFormat("MMMM d", Locale.getDefault())
return dateFormat.format(calendar.time)
}

@Test
fun insertItemTest() {
val name = "Test"
insertItem(
name = name,
date = Calendar.getInstance().get(Calendar.DAY_OF_MONTH).toString()
date = formatDateForDatePicker()
)
composeTestRule.run {
arrayOf(
Expand All @@ -105,7 +114,7 @@ class MainActivityTest {
val updatedName = "Test updated"
insertItem(
name = name,
date = Calendar.getInstance().get(Calendar.DAY_OF_MONTH).toString()
date = formatDateForDatePicker()
)
composeTestRule.run {
onNodeWithText(name).performClick()
Expand All @@ -127,7 +136,7 @@ class MainActivityTest {
val name = "Test"
insertItem(
name = name,
date = Calendar.getInstance().get(Calendar.DAY_OF_MONTH).toString()
date = formatDateForDatePicker()
)
clear()
}
Expand All @@ -137,9 +146,9 @@ class MainActivityTest {
for (i in 0 until 3) {
insertItem(
name = "Test item $i",
date = Calendar.getInstance().apply {
date = formatDateForDatePicker(Calendar.getInstance().apply {
add(Calendar.DAY_OF_MONTH, i-1)
}.get(Calendar.DAY_OF_MONTH).toString()
})
)
}
composeTestRule.run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import androidx.test.uiautomator.UiDevice
import com.lorenzovainigli.foodexpirationdates.view.activity.MainActivity
import junit.framework.TestCase.assertFalse
Expand All @@ -15,6 +16,12 @@ class FirebaseTest {
@get:Rule
val testRule = createAndroidComposeRule(MainActivity::class.java)

@Rule
@JvmField
var mGrantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(
"android.permission.POST_NOTIFICATIONS"
)

@Test
fun firebaseNotEnabledOnFossBuild(){
testRule.onNodeWithText("Settings").assertIsDisplayed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import androidx.test.uiautomator.UiDevice
import com.lorenzovainigli.foodexpirationdates.view.activity.MainActivity
import junit.framework.TestCase.assertTrue
Expand All @@ -15,6 +16,12 @@ class FirebaseTest {
@get:Rule
val testRule = createAndroidComposeRule(MainActivity::class.java)

@Rule
@JvmField
var mGrantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(
"android.permission.POST_NOTIFICATIONS"
)

@Test
fun firebaseEnabledOnFullBuild(){
testRule.onNodeWithText("Settings").assertIsDisplayed()
Expand Down

0 comments on commit b190710

Please sign in to comment.