Something went wrong. Try again.
A fork of Simple Time Tracker with WearOS support. (Now integrated upstream)
Something went wrong. Try again.
SimpleTimeTracker-WearOS app src androidTest java com example util simpletimetracker GoalsTestUtils.kt
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250package com.example.util.simpletimetracker
import android.view.Viewimport androidx.test.espresso.matcher.ViewMatchersimport androidx.test.espresso.matcher.ViewMatchers.hasDescendantimport androidx.test.espresso.matcher.ViewMatchers.hasSiblingimport androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayedimport androidx.test.espresso.matcher.ViewMatchers.isDescendantOfAimport androidx.test.espresso.matcher.ViewMatchers.withIdimport androidx.test.espresso.matcher.ViewMatchers.withSubstringimport androidx.test.espresso.matcher.ViewMatchers.withTextimport com.example.util.simpletimetracker.core.utils.TestUtilsimport com.example.util.simpletimetracker.domain.model.DayOfWeekimport com.example.util.simpletimetracker.domain.model.RecordTypeGoalimport com.example.util.simpletimetracker.feature_change_record.Rimport com.example.util.simpletimetracker.utils.checkViewDoesNotExistimport com.example.util.simpletimetracker.utils.checkViewIsDisplayedimport com.example.util.simpletimetracker.utils.checkViewIsNotDisplayedimport org.hamcrest.CoreMatchers.allOfimport org.hamcrest.Matcherimport java.util.Calendarimport java.util.concurrent.TimeUnitimport com.example.util.simpletimetracker.feature_base_adapter.R as baseR
object GoalsTestUtils {
val durationInSeconds = TimeUnit.MINUTES.toSeconds(10) private val durationInMillis = TimeUnit.MINUTES.toMillis(10)
fun getSessionDurationGoal(duration: Long): RecordTypeGoal = getDurationGoal(RecordTypeGoal.Range.Session, duration)
fun getSessionDurationGoalCategory(duration: Long): RecordTypeGoal = getDurationGoalCategory(RecordTypeGoal.Range.Session, duration)
fun getDailyDurationGoal(duration: Long): RecordTypeGoal = getDurationGoal(RecordTypeGoal.Range.Daily, duration)
fun getDailyDurationGoalCategory(duration: Long): RecordTypeGoal = getDurationGoalCategory(RecordTypeGoal.Range.Daily, duration)
fun getWeeklyDurationGoal(duration: Long): RecordTypeGoal = getDurationGoal(RecordTypeGoal.Range.Weekly, duration)
fun getWeeklyDurationGoalCategory(duration: Long): RecordTypeGoal = getDurationGoalCategory(RecordTypeGoal.Range.Weekly, duration)
fun getMonthlyDurationGoal(duration: Long): RecordTypeGoal = getDurationGoal(RecordTypeGoal.Range.Monthly, duration)
fun getMonthlyDurationGoalCategory(duration: Long): RecordTypeGoal = getDurationGoalCategory(RecordTypeGoal.Range.Monthly, duration)
fun getDailyCountGoal(count: Long): RecordTypeGoal = getCountGoal(RecordTypeGoal.Range.Daily, count)
fun getDailyCountGoalCategory(count: Long): RecordTypeGoal = getCountGoalCategory(RecordTypeGoal.Range.Daily, count)
fun getWeeklyCountGoal(count: Long): RecordTypeGoal = getCountGoal(RecordTypeGoal.Range.Weekly, count)
fun getWeeklyCountGoalCategory(count: Long): RecordTypeGoal = getCountGoalCategory(RecordTypeGoal.Range.Weekly, count)
fun getMonthlyCountGoal(count: Long): RecordTypeGoal = getCountGoal(RecordTypeGoal.Range.Monthly, count)
fun getMonthlyCountGoalCategory(count: Long): RecordTypeGoal = getCountGoalCategory(RecordTypeGoal.Range.Monthly, count)
fun addRecords(testUtils: TestUtils, typeName: String) { val currentTime = Calendar.getInstance().apply { set(Calendar.HOUR_OF_DAY, 15) }.timeInMillis val thisWeek = Calendar.getInstance().apply { set(Calendar.HOUR_OF_DAY, 15) val dateShift = if (get(Calendar.DAY_OF_WEEK) == firstDayOfWeek) +1 else -1 add(Calendar.DATE, dateShift) }.timeInMillis val thisMonth = Calendar.getInstance().apply { set(Calendar.HOUR_OF_DAY, 15) val dateShift = if (get(Calendar.DAY_OF_MONTH) < 15) +7 else -7 add(Calendar.DATE, dateShift) }.timeInMillis
testUtils.addRecord( typeName = typeName, timeStarted = currentTime - durationInMillis, timeEnded = currentTime, ) testUtils.addRecord( typeName = typeName, timeStarted = thisWeek - durationInMillis, timeEnded = thisWeek, ) testUtils.addRecord( typeName = typeName, timeStarted = thisMonth - durationInMillis, timeEnded = thisMonth, ) }
fun checkNoStatisticsGoal(typeName: String) { allOf( isDescendantOfA(withId(R.id.viewStatisticsGoalItem)), hasSibling(withText(typeName)), isCompletelyDisplayed(), ).let(::checkViewDoesNotExist) }
fun checkStatisticsGoal( typeName: String, current: String, goal: String, ) { allOf( isDescendantOfA(withId(baseR.id.viewStatisticsGoalItem)), hasSibling(withText(typeName)), withId(R.id.tvStatisticsGoalItemCurrent), withSubstring(current), isCompletelyDisplayed(), ).let(::checkViewIsDisplayed)
allOf( isDescendantOfA(withId(baseR.id.viewStatisticsGoalItem)), hasSibling(withText(typeName)), withId(R.id.tvStatisticsGoalItemGoal), withText(goal), isCompletelyDisplayed(), ).let(::checkViewIsDisplayed) }
fun checkStatisticsPercent( typeName: String, percent: String, ) { allOf( isDescendantOfA(withId(baseR.id.viewStatisticsGoalItem)), hasSibling(withText(typeName)), withId(R.id.tvStatisticsGoalItemPercent), withText(percent), ).let(::checkViewIsDisplayed) }
fun checkStatisticsMark(typeName: String, isVisible: Boolean) { allOf( isDescendantOfA(withId(baseR.id.viewStatisticsGoalItem)), hasSibling(withText(typeName)), withId(R.id.ivStatisticsGoalItemCheck), ).let { if (isVisible) checkViewIsDisplayed(it) else checkViewIsNotDisplayed(it) } }
fun checkTypeMark(typeName: String, isVisible: Boolean) { allOf(withId(R.id.viewRecordTypeItem), hasDescendant(withText(typeName)), isCompletelyDisplayed()) .let(::checkViewIsDisplayed) allOf(getTypeMatcher(typeName), withId(R.id.ivGoalCheckmarkItemCheckOutline)) .let(::checkViewIsDisplayed) allOf(getTypeMatcher(typeName), withId(R.id.ivGoalCheckmarkItemCheck)) .let { if (isVisible) checkViewIsDisplayed(it) else checkViewIsNotDisplayed(it) } }
fun checkNoTypeMark(typeName: String) { allOf(withId(R.id.viewRecordTypeItem), hasDescendant(withText(typeName)), isCompletelyDisplayed()) .let(::checkViewIsDisplayed) allOf(getTypeMatcher(typeName), withId(R.id.ivGoalCheckmarkItemCheckOutline)) .let(::checkViewIsNotDisplayed) allOf(getTypeMatcher(typeName), withId(R.id.ivGoalCheckmarkItemCheck)) .let(::checkViewIsNotDisplayed) }
fun checkRunningGoal(typeName: String, goal: String) { allOf( isDescendantOfA(withId(R.id.viewRunningRecordItem)), hasSibling(withText(typeName)), withId(R.id.tvRunningRecordItemGoalTime), ViewMatchers.withSubstring(goal), ).let(::checkViewIsDisplayed) }
fun checkNoRunningGoal(typeName: String) { allOf( isDescendantOfA(withId(R.id.viewRunningRecordItem)), hasSibling(withText(typeName)), withId(R.id.tvRunningRecordItemGoalTime), ).let(::checkViewIsNotDisplayed) }
fun checkRunningMark(typeName: String, isVisible: Boolean) { allOf( isDescendantOfA(withId(R.id.viewRunningRecordItem)), hasSibling(withText(typeName)), withId(R.id.ivRunningRecordItemGoalTimeCheck), ).let { if (isVisible) checkViewIsDisplayed(it) else checkViewIsNotDisplayed(it) } }
private fun getTypeMatcher(typeName: String): Matcher<View> { return isDescendantOfA( allOf( withId(R.id.viewRecordTypeItem), hasDescendant(withText(typeName)), ), ) }
private fun getDurationGoal( range: RecordTypeGoal.Range, duration: Long, ): RecordTypeGoal { return RecordTypeGoal( idData = RecordTypeGoal.IdData.Type(0), range = range, type = RecordTypeGoal.Type.Duration(duration), daysOfWeek = DayOfWeek.values().toList(), ) }
private fun getDurationGoalCategory( range: RecordTypeGoal.Range, duration: Long, ): RecordTypeGoal { return getDurationGoal(range = range, duration = duration) .copy(idData = RecordTypeGoal.IdData.Category(0)) }
private fun getCountGoal( range: RecordTypeGoal.Range, count: Long, ): RecordTypeGoal { return RecordTypeGoal( idData = RecordTypeGoal.IdData.Type(0), range = range, type = RecordTypeGoal.Type.Count(count), daysOfWeek = DayOfWeek.values().toList(), ) }
private fun getCountGoalCategory( range: RecordTypeGoal.Range, count: Long, ): RecordTypeGoal { return getCountGoal(range = range, count = count) .copy(idData = RecordTypeGoal.IdData.Category(0)) }}