Skip to content

Commit

Permalink
add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Jan 25, 2025
1 parent 1f1c12f commit 6943bea
Show file tree
Hide file tree
Showing 14 changed files with 276 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class ChangeRecordTest : BaseUiTest() {
testUtils.addActivity(name = newName, color = lastColor, text = lastEmoji)
testUtils.addRecordTag(tag1, name)
testUtils.addRecordTag(tag2, newName)
Thread.sleep(1000)

// Add record
NavUtils.openRecordsScreen()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class ChangeRunningRecordTest : BaseUiTest() {
)
testUtils.addActivity(name = name2, color = lastColor, text = lastEmoji)
testUtils.addRecordTag(tag2, name2)
Thread.sleep(1000)

// Start timer
tryAction { clickOnViewWithText(name1) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
package com.example.util.simpletimetracker

import androidx.test.espresso.Espresso.pressBack
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
import androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.util.simpletimetracker.core.extension.setToStartOfDay
import com.example.util.simpletimetracker.utils.BaseUiTest
import com.example.util.simpletimetracker.utils.NavUtils
import com.example.util.simpletimetracker.utils.checkViewDoesNotExist
import com.example.util.simpletimetracker.utils.checkViewIsDisplayed
import com.example.util.simpletimetracker.utils.clickOnViewWithText
import com.example.util.simpletimetracker.utils.longClickOnView
import com.example.util.simpletimetracker.utils.tryAction
import com.example.util.simpletimetracker.utils.withCardColor
import com.example.util.simpletimetracker.utils.withTag
import dagger.hilt.android.testing.HiltAndroidTest
import org.hamcrest.CoreMatchers.allOf
import org.junit.Test
import org.junit.runner.RunWith
import java.util.Calendar
import java.util.concurrent.TimeUnit
import com.example.util.simpletimetracker.core.R as coreR
import com.example.util.simpletimetracker.feature_base_adapter.R as baseR

@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class RecordActionsChangeTagTest : BaseUiTest() {

@Test
fun visibility() {
val recordWithTags = "recordWithTags"
val recordWithNoTags = "recordWithNoTags"
val runningWithTags = "runningWithTags"
val runningWithNoTags = "runningWithNoTags"
val recordTag = "recordTag"
val runningTag = "runningTag"

// Setup
testUtils.addActivity(recordWithTags)
testUtils.addActivity(recordWithNoTags)
testUtils.addActivity(runningWithTags)
testUtils.addActivity(runningWithNoTags)
testUtils.addRecordTag(recordTag, typeName = recordWithTags)
testUtils.addRecordTag(runningTag, typeName = runningWithTags)
testUtils.addRunningRecord(runningWithTags)
testUtils.addRunningRecord(runningWithNoTags)
testUtils.addRecord(recordWithTags)
testUtils.addRecord(recordWithNoTags)
calendar.timeInMillis = System.currentTimeMillis()
testUtils.addRecord(
typeName = recordWithTags,
timeStarted = calendar.timeInMillis - TimeUnit.DAYS.toMillis(1),
timeEnded = calendar.timeInMillis - TimeUnit.DAYS.toMillis(1),
)
Thread.sleep(1000)

// Running record with tags - shown
NavUtils.openRecordsScreen()
longClickOnView(allOf(withText(runningWithTags), isCompletelyDisplayed()))
checkViewIsDisplayed(withText(coreR.string.data_edit_change_tag))
pressBack()

// Running records with no tags - not shown
NavUtils.openRecordsScreen()
longClickOnView(allOf(withText(runningWithNoTags), isCompletelyDisplayed()))
checkViewDoesNotExist(withText(coreR.string.data_edit_change_tag))
pressBack()

// Record with tags - shown
longClickOnView(allOf(withText(recordWithTags), isCompletelyDisplayed()))
checkViewIsDisplayed(withText(coreR.string.data_edit_change_tag))
pressBack()

// Record with no tags - not shown
longClickOnView(allOf(withText(recordWithNoTags), isCompletelyDisplayed()))
checkViewDoesNotExist(withText(coreR.string.data_edit_change_tag))
pressBack()

// Untracked - not shown
longClickOnView(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed()))
checkViewDoesNotExist(withText(coreR.string.data_edit_change_tag))
}

@Test
fun record() {
val name1 = "Name1"
val color1 = firstColor
val icon1 = firstIcon
val comment = "Some_comment"
val tag = "Tag"
val tagGeneral = "TagGeneral"
val fullName1 = "$name1 - $tag, $tagGeneral"
val fullName2 = "$name1 - $tagGeneral"
val calendar = Calendar.getInstance()

// Setup
val current = calendar.timeInMillis
val difference = TimeUnit.MINUTES.toMillis(30)
val timeStartedTimestamp = current - difference
val timeStartedPreview = timeStartedTimestamp.formatTime()
val timeEndedPreview = current.formatTime()
val timeRangePreview = difference.formatInterval()

testUtils.addActivity(name = name1, color = color1, icon = icon1)
testUtils.addRecordTag(tag, typeName = name1)
testUtils.addRecordTag(tagGeneral)
testUtils.addRecord(
typeName = name1,
timeStarted = timeStartedTimestamp,
timeEnded = current,
tagNames = listOf(tag, tagGeneral),
comment = comment,
)

// Check record
NavUtils.openRecordsScreen()
checkRecord(
name = fullName1,
color = color1,
icon = icon1,
timeStartedPreview = timeStartedPreview,
timeEndedPreview = timeEndedPreview,
timeRangePreview = timeRangePreview,
comment = comment,
)

// Change
longClickOnView(allOf(withText(fullName1), isCompletelyDisplayed()))
clickOnViewWithText(R.string.data_edit_change_tag)
clickOnViewWithText(tag)
clickOnViewWithText(R.string.change_record_save)

tryAction {
checkRecord(
name = fullName2,
color = color1,
icon = icon1,
timeStartedPreview = timeStartedPreview,
timeEndedPreview = timeEndedPreview,
timeRangePreview = timeRangePreview,
comment = comment,
)
}
}

@Test
fun runningRecord() {
val name1 = "Name1"
val color1 = firstColor
val icon1 = firstIcon
val comment = "Some_comment"
val tag = "Tag"
val tagGeneral = "TagGeneral"
val fullName1 = "$name1 - $tag, $tagGeneral"
val fullName2 = "$name1 - $tagGeneral"
val calendar = Calendar.getInstance()

// Setup
val current = calendar.timeInMillis
val difference = TimeUnit.MINUTES.toMillis(30)
val timeStartedTimestamp = current - difference
val timeStartedPreview = timeStartedTimestamp.formatTime()

testUtils.addActivity(name = name1, color = color1, icon = icon1)
testUtils.addRecordTag(tag, typeName = name1)
testUtils.addRecordTag(tagGeneral)
testUtils.addRunningRecord(
typeName = name1,
timeStarted = timeStartedTimestamp,
tagNames = listOf(tag, tagGeneral),
comment = comment,
)

// Check record
NavUtils.openRecordsScreen()
checkRunningRecord(
name = fullName1,
color = color1,
icon = icon1,
timeStartedPreview = timeStartedPreview,
comment = comment,
)

// Change
longClickOnView(allOf(withText(fullName1), isCompletelyDisplayed()))
clickOnViewWithText(R.string.data_edit_change_tag)
clickOnViewWithText(tag)
clickOnViewWithText(R.string.change_record_save)

tryAction {
checkRunningRecord(
name = fullName2,
color = color1,
icon = icon1,
timeStartedPreview = timeStartedPreview,
comment = comment,
)
}
}

@Suppress("SameParameterValue")
private fun checkRecord(
name: String,
color: Int,
icon: Int,
timeStartedPreview: String,
timeEndedPreview: String,
timeRangePreview: String,
comment: String,
) {
checkViewIsDisplayed(
allOf(
withId(baseR.id.viewRecordItem),
withCardColor(color),
hasDescendant(withText(name)),
hasDescendant(withTag(icon)),
hasDescendant(withText(timeStartedPreview)),
hasDescendant(withText(timeEndedPreview)),
hasDescendant(withText(timeRangePreview)),
hasDescendant(withText(comment)),
isCompletelyDisplayed(),
),
)
}

@Suppress("SameParameterValue")
private fun checkRunningRecord(
name: String,
color: Int,
icon: Int,
timeStartedPreview: String,
comment: String,
) {
checkViewIsDisplayed(
allOf(
withId(baseR.id.viewRunningRecordItem),
withCardColor(color),
hasDescendant(withText(name)),
hasDescendant(withTag(icon)),
hasDescendant(withText(timeStartedPreview)),
hasDescendant(withText(comment)),
isCompletelyDisplayed(),
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ fun Range.toParams(): RangeParams {
fun ChangeRunningRecordParams.Preview.GoalTimeParams.toViewData(): GoalTimeViewData {
return GoalTimeViewData(
text = this.text,
complete = this.complete,
state = this.state.toViewData(),
)
}

fun GoalTimeViewData.toParams(): ChangeRunningRecordParams.Preview.GoalTimeParams {
return ChangeRunningRecordParams.Preview.GoalTimeParams(
text = this.text,
complete = this.complete,
state = this.state.toParams(),
)
}

fun ChangeRunningRecordParams.Preview.GoalSubtypeParams.toViewData(): GoalTimeViewData.Subtype {
return when (this) {
is ChangeRunningRecordParams.Preview.GoalSubtypeParams.Hidden -> GoalTimeViewData.Subtype.Hidden
is ChangeRunningRecordParams.Preview.GoalSubtypeParams.Goal -> GoalTimeViewData.Subtype.Goal
is ChangeRunningRecordParams.Preview.GoalSubtypeParams.Limit -> GoalTimeViewData.Subtype.Limit
}
}

fun GoalTimeViewData.Subtype.toParams(): ChangeRunningRecordParams.Preview.GoalSubtypeParams {
return when (this) {
is GoalTimeViewData.Subtype.Hidden -> ChangeRunningRecordParams.Preview.GoalSubtypeParams.Hidden
is GoalTimeViewData.Subtype.Goal -> ChangeRunningRecordParams.Preview.GoalSubtypeParams.Goal
is GoalTimeViewData.Subtype.Limit -> ChangeRunningRecordParams.Preview.GoalSubtypeParams.Limit
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import com.example.util.simpletimetracker.core.viewData.StatisticsDataHolder
import com.example.util.simpletimetracker.domain.extension.orFalse
import com.example.util.simpletimetracker.domain.extension.orZero
import com.example.util.simpletimetracker.domain.recordType.extension.value
import com.example.util.simpletimetracker.domain.statistics.model.ChartFilterType
import com.example.util.simpletimetracker.domain.statistics.model.RangeLength
import com.example.util.simpletimetracker.domain.recordType.model.RecordType
import com.example.util.simpletimetracker.domain.recordType.model.RecordTypeGoal
import com.example.util.simpletimetracker.domain.statistics.model.ChartFilterType
import com.example.util.simpletimetracker.domain.statistics.model.RangeLength
import com.example.util.simpletimetracker.domain.statistics.model.Statistics
import com.example.util.simpletimetracker.feature_base_adapter.runningRecord.GoalTimeViewData
import com.example.util.simpletimetracker.feature_base_adapter.statisticsGoal.StatisticsGoalViewData
Expand All @@ -33,8 +33,7 @@ class GoalViewDataMapper @Inject constructor(
): GoalTimeViewData {
val noGoal = GoalTimeViewData(
text = "",
complete = false,
state = GoalTimeViewData.Subtype.Goal,
state = GoalTimeViewData.Subtype.Hidden,
)
if (goal == null || goal.value <= 0L || !goalsVisible) {
return noGoal
Expand Down Expand Up @@ -80,14 +79,14 @@ class GoalViewDataMapper @Inject constructor(
"$typeString $formatted"
}

val state = when (goal.subtype) {
is RecordTypeGoal.Subtype.Goal -> GoalTimeViewData.Subtype.Goal
is RecordTypeGoal.Subtype.Limit -> GoalTimeViewData.Subtype.Limit
val state = when {
complete && goal.subtype is RecordTypeGoal.Subtype.Goal -> GoalTimeViewData.Subtype.Goal
complete && goal.subtype is RecordTypeGoal.Subtype.Limit -> GoalTimeViewData.Subtype.Limit
else -> GoalTimeViewData.Subtype.Hidden
}

return GoalTimeViewData(
text = durationLeftString,
complete = complete,
state = state,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ fun updateRunningRecordPreview(
// Update if goal was shown and need update.
if (it.itemGoalTime.isNotEmpty() && update.goalText.isNotEmpty()) {
it.itemGoalTime = update.goalText
it.itemGoalTimeComplete = update.goalComplete
it.itemGoalTimeCheck = when (update.goalState) {
is GoalState.Hidden -> CheckState.HIDDEN
is GoalState.Goal -> CheckState.GOAL_REACHED
is GoalState.Limit -> CheckState.LIMIT_REACHED
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class UpdateRunningRecordFromChangeScreenInteractor @Inject constructor() {
val timer: String,
val timerTotal: String,
val goalText: String,
val goalComplete: Boolean,
val goalState: GoalState,
val additionalData: AdditionalData?,
)
Expand All @@ -38,7 +37,8 @@ class UpdateRunningRecordFromChangeScreenInteractor @Inject constructor() {
)

sealed interface GoalState {
object Goal : GoalState
object Limit : GoalState
data object Hidden : GoalState
data object Goal : GoalState
data object Limit : GoalState
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.example.util.simpletimetracker.feature_base_adapter.runningRecord

data class GoalTimeViewData(
val text: String,
val complete: Boolean,
val state: Subtype,
) {

sealed interface Subtype {
data object Hidden : Subtype
data object Goal : Subtype
data object Limit : Subtype
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.util.simpletimetracker.feature_base_adapter.runningRecord

import androidx.core.view.ViewCompat
import com.example.util.simpletimetracker.domain.extension.orFalse
import com.example.util.simpletimetracker.domain.record.interactor.UpdateRunningRecordFromChangeScreenInteractor.GoalState
import com.example.util.simpletimetracker.feature_base_adapter.createRecyclerBindingAdapterDelegate
import com.example.util.simpletimetracker.feature_base_adapter.runningRecord.GoalTimeViewData.Subtype
import com.example.util.simpletimetracker.feature_views.GoalCheckmarkView.CheckState
Expand Down Expand Up @@ -42,8 +43,8 @@ fun createRunningRecordAdapterDelegate(
}
if (rebind || updates.contains(ViewData.UPDATE_GOAL_TIME).orFalse()) {
itemGoalTime = item.goalTime.text
itemGoalTimeComplete = item.goalTime.complete
itemGoalTimeCheck = when (item.goalTime.state) {
is Subtype.Hidden -> CheckState.HIDDEN
is Subtype.Goal -> CheckState.GOAL_REACHED
is Subtype.Limit -> CheckState.LIMIT_REACHED
}
Expand Down
Loading

0 comments on commit 6943bea

Please sign in to comment.