|
| 1 | +package com.example.util.simpletimetracker |
| 2 | + |
| 3 | +import android.view.View |
| 4 | +import androidx.test.espresso.matcher.ViewMatchers.hasDescendant |
| 5 | +import androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed |
| 6 | +import androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA |
| 7 | +import androidx.test.espresso.matcher.ViewMatchers.withId |
| 8 | +import androidx.test.espresso.matcher.ViewMatchers.withText |
| 9 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 10 | +import com.example.util.simpletimetracker.feature_change_record.R |
| 11 | +import com.example.util.simpletimetracker.utils.BaseUiTest |
| 12 | +import com.example.util.simpletimetracker.utils.NavUtils |
| 13 | +import com.example.util.simpletimetracker.utils.checkViewIsDisplayed |
| 14 | +import com.example.util.simpletimetracker.utils.checkViewIsNotDisplayed |
| 15 | +import dagger.hilt.android.testing.HiltAndroidTest |
| 16 | +import org.hamcrest.CoreMatchers.allOf |
| 17 | +import org.hamcrest.Matcher |
| 18 | +import org.junit.Test |
| 19 | +import org.junit.runner.RunWith |
| 20 | +import java.util.concurrent.TimeUnit |
| 21 | + |
| 22 | +@HiltAndroidTest |
| 23 | +@RunWith(AndroidJUnit4::class) |
| 24 | +class GoalsOnCardsTest : BaseUiTest() { |
| 25 | + |
| 26 | + @Test |
| 27 | + fun noGoals() { |
| 28 | + val noGoals = "noGoals" |
| 29 | + val otherGoals = "otherGoals" |
| 30 | + |
| 31 | + // Add data |
| 32 | + testUtils.addActivity(noGoals) |
| 33 | + testUtils.addActivity( |
| 34 | + name = otherGoals, |
| 35 | + goals = listOf( |
| 36 | + GoalsTestUtils.getSessionDurationGoal(1), |
| 37 | + GoalsTestUtils.getWeeklyDurationGoal(1), |
| 38 | + GoalsTestUtils.getMonthlyDurationGoal(1), |
| 39 | + GoalsTestUtils.getWeeklyCountGoal(1), |
| 40 | + GoalsTestUtils.getMonthlyCountGoal(1), |
| 41 | + ), |
| 42 | + ) |
| 43 | + Thread.sleep(1000) |
| 44 | + |
| 45 | + // Check |
| 46 | + checkNoGoal(noGoals) |
| 47 | + checkNoGoal(otherGoals) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun checkmarks() { |
| 52 | + val durationGoal = "durationGoal" |
| 53 | + val countGoal = "countGoal" |
| 54 | + |
| 55 | + // Add data |
| 56 | + testUtils.addActivity( |
| 57 | + name = durationGoal, |
| 58 | + goals = listOf(GoalsTestUtils.getDailyDurationGoal(2)), |
| 59 | + ) |
| 60 | + testUtils.addActivity( |
| 61 | + name = countGoal, |
| 62 | + goals = listOf(GoalsTestUtils.getDailyCountGoal(2)), |
| 63 | + ) |
| 64 | + Thread.sleep(1000) |
| 65 | + |
| 66 | + // Not reached |
| 67 | + checkCheckmark(durationGoal, isVisible = false) |
| 68 | + checkCheckmark(countGoal, isVisible = false) |
| 69 | + |
| 70 | + // Add records |
| 71 | + NavUtils.openRecordsScreen() |
| 72 | + val current = System.currentTimeMillis() |
| 73 | + testUtils.addRecord( |
| 74 | + typeName = durationGoal, |
| 75 | + timeStarted = current - TimeUnit.SECONDS.toMillis(1), |
| 76 | + timeEnded = current, |
| 77 | + ) |
| 78 | + testUtils.addRecord( |
| 79 | + typeName = countGoal, |
| 80 | + ) |
| 81 | + |
| 82 | + // Not reached |
| 83 | + NavUtils.openRunningRecordsScreen() |
| 84 | + checkCheckmark(durationGoal, isVisible = false) |
| 85 | + checkCheckmark(countGoal, isVisible = false) |
| 86 | + |
| 87 | + // Add more records |
| 88 | + NavUtils.openRecordsScreen() |
| 89 | + testUtils.addRecord( |
| 90 | + typeName = durationGoal, |
| 91 | + timeStarted = current - TimeUnit.SECONDS.toMillis(1), |
| 92 | + timeEnded = current, |
| 93 | + ) |
| 94 | + testUtils.addRecord( |
| 95 | + typeName = countGoal, |
| 96 | + ) |
| 97 | + |
| 98 | + // Reached |
| 99 | + NavUtils.openRunningRecordsScreen() |
| 100 | + checkCheckmark(durationGoal, isVisible = true) |
| 101 | + checkCheckmark(countGoal, isVisible = true) |
| 102 | + } |
| 103 | + |
| 104 | + private fun checkNoGoal(typeName: String) { |
| 105 | + allOf(withId(R.id.viewRecordTypeItem), hasDescendant(withText(typeName)), isCompletelyDisplayed()) |
| 106 | + .let(::checkViewIsDisplayed) |
| 107 | + allOf(getTypeMatcher(typeName), withId(R.id.ivGoalCheckmarkItemCheckOutline)) |
| 108 | + .let(::checkViewIsNotDisplayed) |
| 109 | + allOf(getTypeMatcher(typeName), withId(R.id.ivGoalCheckmarkItemCheck)) |
| 110 | + .let(::checkViewIsNotDisplayed) |
| 111 | + } |
| 112 | + |
| 113 | + private fun checkCheckmark(typeName: String, isVisible: Boolean) { |
| 114 | + allOf(withId(R.id.viewRecordTypeItem), hasDescendant(withText(typeName)), isCompletelyDisplayed()) |
| 115 | + .let(::checkViewIsDisplayed) |
| 116 | + allOf(getTypeMatcher(typeName), withId(R.id.ivGoalCheckmarkItemCheckOutline)) |
| 117 | + .let(::checkViewIsDisplayed) |
| 118 | + allOf(getTypeMatcher(typeName), withId(R.id.ivGoalCheckmarkItemCheck)) |
| 119 | + .let { if (isVisible) checkViewIsDisplayed(it) else checkViewIsNotDisplayed(it) } |
| 120 | + } |
| 121 | + |
| 122 | + private fun getTypeMatcher(typeName: String): Matcher<View> { |
| 123 | + return isDescendantOfA( |
| 124 | + allOf( |
| 125 | + withId(R.id.viewRecordTypeItem), |
| 126 | + hasDescendant(withText(typeName)), |
| 127 | + ), |
| 128 | + ) |
| 129 | + } |
| 130 | +} |
0 commit comments