Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Aug 10, 2024
1 parent e324b0a commit d1588fd
Show file tree
Hide file tree
Showing 13 changed files with 1,001 additions and 29 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class RecordActionsMergeTest : BaseUiTest() {
// Setup
testUtils.addActivity(name)
testUtils.addRecord(name)
calendar.timeInMillis = System.currentTimeMillis()
testUtils.addRecord(
typeName = name,
timeStarted = calendar.timeInMillis - TimeUnit.DAYS.toMillis(1),
Expand Down Expand Up @@ -83,15 +84,6 @@ class RecordActionsMergeTest : BaseUiTest() {
checkViewIsDisplayed(withText(coreR.string.change_record_merge))
clickOnViewWithText(coreR.string.change_record_actions_hint)
pressBack()

// Untracked and have no prev record - not shown
clickOnViewWithId(recordsR.id.btnRecordsContainerPrevious)
clickOnView(allOf(withText(name), isCompletelyDisplayed()))
clickOnViewWithId(changeRecordR.id.btnChangeRecordDelete)
clickOnViewWithId(recordsR.id.btnRecordsContainerNext)
clickOnView(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed()))
clickOnViewWithText(coreR.string.change_record_actions_hint)
checkViewDoesNotExist(withText(coreR.string.change_record_merge))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ class RecordActionsSplitTest : BaseUiTest() {
tagNames = listOf(tag),
comment = comment,
)
Thread.sleep(1000)

// Check record
tryAction { checkRunningRecord(fullName, timeStartedPreview, comment) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package com.example.util.simpletimetracker

import androidx.test.espresso.Espresso.pressBack
import androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed
import androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA
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.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.checkViewIsNotDisplayed
import com.example.util.simpletimetracker.utils.clickOnView
import com.example.util.simpletimetracker.utils.clickOnViewWithId
import com.example.util.simpletimetracker.utils.clickOnViewWithText
import com.example.util.simpletimetracker.utils.tryAction
import dagger.hilt.android.testing.HiltAndroidTest
import kotlinx.coroutines.runBlocking
import org.hamcrest.CoreMatchers.allOf
import org.junit.Test
import org.junit.runner.RunWith
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
import com.example.util.simpletimetracker.feature_records.R as recordsR

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

@Test
fun buttonsVisibility() {
NavUtils.openRecordsScreen()

// Check not visible
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerOptions))
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerFilter))
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerShare))
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))

// Show options
clickOnViewWithId(recordsR.id.btnRecordsContainerOptions)
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerOptions))
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerFilter))
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerShare))
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))

// Switch tabs
NavUtils.openRunningRecordsScreen()
NavUtils.openRecordsScreen()

// Still shown
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerOptions))
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerFilter))
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerShare))
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))

// Hide options
clickOnViewWithId(recordsR.id.btnRecordsContainerOptions)
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerOptions))
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerFilter))
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerShare))
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))
}

@Test
fun filterType() {
val name1 = "TypeName1"
val name2 = "TypeName2"

// Add data
testUtils.addActivity(name1)
testUtils.addActivity(name2)

// Add records
testUtils.addRecord(name1)
testUtils.addRecord(name2)
val before = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1)
testUtils.addRecord(name1, timeStarted = before, timeEnded = before)
testUtils.addRecord(name2, timeStarted = before, timeEnded = before)

// All records displayed
NavUtils.openRecordsScreen()
checkViewIsDisplayed(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed()))
checkViewIsDisplayed(allOf(withText(name1), isCompletelyDisplayed()))
checkViewIsDisplayed(allOf(withText(name2), isCompletelyDisplayed()))

// Filter untracked
clickOnViewWithId(recordsR.id.btnRecordsContainerOptions)
clickOnViewWithId(recordsR.id.btnRecordsContainerFilter)
clickOnView(
allOf(isDescendantOfA(withId(baseR.id.viewRecordTypeItem)), withText(coreR.string.untracked_time_name)),
)
pressBack()
tryAction { checkViewDoesNotExist(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed())) }
checkViewIsDisplayed(allOf(withText(name1), isCompletelyDisplayed()))
checkViewIsDisplayed(allOf(withText(name2), isCompletelyDisplayed()))

// Filter activity
clickOnViewWithId(recordsR.id.btnRecordsContainerFilter)
clickOnView(allOf(isDescendantOfA(withId(baseR.id.viewRecordTypeItem)), withText(name1)))
pressBack()
tryAction { checkViewDoesNotExist(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed())) }
checkViewDoesNotExist(allOf(withText(name1), isCompletelyDisplayed()))
checkViewIsDisplayed(allOf(withText(name2), isCompletelyDisplayed()))

// Filter all
clickOnViewWithId(recordsR.id.btnRecordsContainerFilter)
clickOnView(allOf(isDescendantOfA(withId(baseR.id.viewRecordTypeItem)), withText(name2)))
pressBack()
tryAction { checkViewDoesNotExist(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed())) }
checkViewDoesNotExist(allOf(withText(name1), isCompletelyDisplayed()))
checkViewDoesNotExist(allOf(withText(name2), isCompletelyDisplayed()))
checkViewIsDisplayed(allOf(withText(coreR.string.no_data), isCompletelyDisplayed()))

// Show all
clickOnViewWithId(recordsR.id.btnRecordsContainerFilter)
clickOnViewWithText(coreR.string.types_filter_show_all)
pressBack()
tryAction { checkViewIsDisplayed(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed())) }
checkViewIsDisplayed(allOf(withText(name1), isCompletelyDisplayed()))
checkViewIsDisplayed(allOf(withText(name2), isCompletelyDisplayed()))

// Hide all
clickOnViewWithId(recordsR.id.btnRecordsContainerFilter)
clickOnViewWithText(coreR.string.types_filter_hide_all)
pressBack()
tryAction { checkViewDoesNotExist(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed())) }
checkViewDoesNotExist(allOf(withText(name1), isCompletelyDisplayed()))
checkViewDoesNotExist(allOf(withText(name2), isCompletelyDisplayed()))
checkViewIsDisplayed(allOf(withText(coreR.string.no_data), isCompletelyDisplayed()))
}

@Test
fun showRecordsCalendar() {
val name = "Test"

// Add data
runBlocking { prefsInteractor.setShowCalendarButtonOnRecordsTab(true) }
testUtils.addActivity(name = name)
testUtils.addRecord(name)

// Record is shown
NavUtils.openRecordsScreen()
checkViewDoesNotExist(allOf(withId(recordsR.id.viewRecordsCalendar), isCompletelyDisplayed()))
checkViewDoesNotExist(allOf(withId(recordsR.id.tvRecordsCalendarHint), isCompletelyDisplayed()))
checkViewIsDisplayed(allOf(withId(recordsR.id.rvRecordsList), isCompletelyDisplayed()))
checkViewIsDisplayed(allOf(withText(name), isCompletelyDisplayed()))

// Change setting
clickOnViewWithId(recordsR.id.btnRecordsContainerOptions)
clickOnViewWithId(recordsR.id.btnRecordsContainerCalendarSwitch)

// Record is not shown
checkViewIsDisplayed(allOf(withId(recordsR.id.viewRecordsCalendar), isCompletelyDisplayed()))
checkViewIsDisplayed(allOf(withId(recordsR.id.tvRecordsCalendarHint), isCompletelyDisplayed()))
checkViewDoesNotExist(allOf(withId(recordsR.id.rvRecordsList), isCompletelyDisplayed()))
checkViewDoesNotExist(allOf(withText(name), isCompletelyDisplayed()))

// Change setting
clickOnViewWithId(recordsR.id.btnRecordsContainerCalendarSwitch)

// Record is shown
checkViewDoesNotExist(allOf(withId(recordsR.id.viewRecordsCalendar), isCompletelyDisplayed()))
checkViewDoesNotExist(allOf(withId(recordsR.id.tvRecordsCalendarHint), isCompletelyDisplayed()))
checkViewIsDisplayed(allOf(withId(recordsR.id.rvRecordsList), isCompletelyDisplayed()))
checkViewIsDisplayed(allOf(withText(name), isCompletelyDisplayed()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,40 @@ class SettingsTest : BaseUiTest() {
checkViewIsDisplayed(allOf(withText(coreR.string.title_today), isCompletelyDisplayed()))
}

@Test
fun showCalendarSwitchOnTheSameTab() {
// Check setting
NavUtils.openSettingsScreen()
NavUtils.openSettingsDisplay()
scrollSettingsRecyclerToText(coreR.string.settings_show_calendar_button_on_records_tab)
checkCheckboxIsNotChecked(settingsCheckboxBesideText(coreR.string.settings_show_calendar_button_on_records_tab))

// Check not shown
NavUtils.openRecordsScreen()
clickOnViewWithId(recordsR.id.btnRecordsContainerOptions)
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))

// Change settings
NavUtils.openSettingsScreen()
scrollSettingsRecyclerToText(coreR.string.settings_show_calendar_button_on_records_tab)
clickOnSettingsCheckboxBesideText(coreR.string.settings_show_calendar_button_on_records_tab)
checkCheckboxIsChecked(settingsCheckboxBesideText(coreR.string.settings_show_calendar_button_on_records_tab))

// Check shown
NavUtils.openRecordsScreen()
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))

// Change back
NavUtils.openSettingsScreen()
scrollSettingsRecyclerToText(coreR.string.settings_show_calendar_button_on_records_tab)
clickOnSettingsCheckboxBesideText(coreR.string.settings_show_calendar_button_on_records_tab)
checkCheckboxIsNotChecked(settingsCheckboxBesideText(coreR.string.settings_show_calendar_button_on_records_tab))

// Check not shown
NavUtils.openRecordsScreen()
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))
}

@Test
fun keepStatisticsRange() {
val name = "Test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,13 @@ class StatisticsDetailTest : BaseUiTest() {
val tag1 = "TagName1"
val tag2 = "TagName2"
val color = firstColor
val lastColor = lastColor
val icon = firstIcon

// Add activity
testUtils.addActivity(name = name, color = color, icon = icon)
testUtils.addRecordTag(tag1, name)
testUtils.addRecordTag(tag2)
testUtils.addRecordTag(tag2, color = lastColor)

// Add records
var calendar = Calendar.getInstance()
Expand Down Expand Up @@ -780,7 +781,7 @@ class StatisticsDetailTest : BaseUiTest() {

// Tag split
checkTagItem(color, tag1, "1$hourString 0$minuteString", "33%")
checkTagItem(color, tag2, "2$hourString 0$minuteString", "67%")
checkTagItem(lastColor, tag2, "2$hourString 0$minuteString", "67%")
checkNoTagItem("Untagged")

// Next month
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import com.example.util.simpletimetracker.core.mapper.IconImageMapper
import com.example.util.simpletimetracker.core.mapper.TimeMapper
import com.example.util.simpletimetracker.core.utils.CountingIdlingResourceProvider
import com.example.util.simpletimetracker.core.utils.TestUtils
import com.example.util.simpletimetracker.domain.interactor.ComplexRuleInteractor
import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
import com.example.util.simpletimetracker.domain.repo.ComplexRuleRepo
import com.example.util.simpletimetracker.feature_records.view.RecordsContainerFragment
import com.example.util.simpletimetracker.feature_statistics.view.StatisticsContainerFragment
import com.example.util.simpletimetracker.feature_views.pieChart.PieChartView
Expand Down Expand Up @@ -52,6 +54,9 @@ open class BaseUiTest {
@Inject
lateinit var languageInteractor: LanguageInteractor

@Inject
lateinit var complexRuleRepo: ComplexRuleRepo

@get:Rule(order = 0)
var hiltRule = HiltAndroidRule(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ object NavUtils {
clickOnSettingsRecyclerText(coreR.string.settings_data_edit)
}

fun openComplexRules() {
scrollSettingsRecyclerToText(coreR.string.settings_complex_rules)
clickOnSettingsRecyclerText(coreR.string.settings_complex_rules)
}

fun openCardSizeScreen() {
scrollSettingsRecyclerToText(coreR.string.settings_change_card_size)
clickOnSettingsRecyclerText(coreR.string.settings_change_card_size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.example.util.simpletimetracker.domain.extension.orZero
import com.example.util.simpletimetracker.domain.interactor.ActivityFilterInteractor
import com.example.util.simpletimetracker.domain.interactor.CategoryInteractor
import com.example.util.simpletimetracker.domain.interactor.ClearDataInteractor
import com.example.util.simpletimetracker.domain.interactor.ComplexRuleInteractor
import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
import com.example.util.simpletimetracker.domain.interactor.RecordInteractor
import com.example.util.simpletimetracker.domain.interactor.RecordTagInteractor
Expand All @@ -18,6 +19,7 @@ import com.example.util.simpletimetracker.domain.interactor.RunningRecordInterac
import com.example.util.simpletimetracker.domain.model.ActivityFilter
import com.example.util.simpletimetracker.domain.model.AppColor
import com.example.util.simpletimetracker.domain.model.Category
import com.example.util.simpletimetracker.domain.model.ComplexRule
import com.example.util.simpletimetracker.domain.model.DayOfWeek
import com.example.util.simpletimetracker.domain.model.Record
import com.example.util.simpletimetracker.domain.model.RecordTag
Expand All @@ -39,6 +41,7 @@ class TestUtils @Inject constructor(
private val recordTypeToDefaultTagInteractor: RecordTypeToDefaultTagInteractor,
private val activityFilterInteractor: ActivityFilterInteractor,
private val recordTypeGoalInteractor: RecordTypeGoalInteractor,
private val complexRuleInteractor: ComplexRuleInteractor,
private val prefsInteractor: PrefsInteractor,
private val iconImageMapper: IconImageMapper,
private val clearDataInteractor: ClearDataInteractor,
Expand Down Expand Up @@ -221,22 +224,17 @@ class TestUtils @Inject constructor(
?: (0..colors.size).random()
val availableCategories = categoryInteractor.getAll()
val availableTypes = recordTypeInteractor.getAll()
val selectedIds = names
.mapNotNull { name ->
when (type) {
is ActivityFilter.Type.Activity -> {
availableTypes.firstOrNull { it.name == name }?.id
}

is ActivityFilter.Type.Category -> {
availableCategories.firstOrNull { it.name == name }?.id
}
val selectedIds = names.mapNotNull { name ->
when (type) {
is ActivityFilter.Type.Activity -> {
availableTypes.firstOrNull { it.name == name }?.id
}

is ActivityFilter.Type.Category -> {
availableCategories.firstOrNull { it.name == name }?.id
}
}
.takeUnless {
it.isEmpty()
}
.orEmpty()
}

val data = ActivityFilter(
selectedIds = selectedIds,
Expand All @@ -248,4 +246,36 @@ class TestUtils @Inject constructor(

activityFilterInteractor.add(data)
}

fun addComplexRule(
action: ComplexRule.Action,
assignTagNames: List<String> = emptyList(),
startingTypeNames: List<String> = emptyList(),
currentTypeNames: List<String> = emptyList(),
daysOfWeek: List<DayOfWeek> = emptyList(),
) = runBlocking {
val availableTypes = recordTypeInteractor.getAll()

fun getTypeIds(names: List<String>): Set<Long> {
return names.mapNotNull { name ->
availableTypes.firstOrNull { it.name == name }?.id
}.toSet()
}

val assignTagIds = recordTagInteractor.getAll()
.filter { it.name in assignTagNames }
.map { it.id }
.toSet()

val data = ComplexRule(
disabled = false,
action = action,
actionAssignTagIds = assignTagIds,
conditionStartingTypeIds = getTypeIds(startingTypeNames),
conditionCurrentTypeIds = getTypeIds(currentTypeNames),
conditionDaysOfWeek = daysOfWeek.toSet(),
)

complexRuleInteractor.add(data)
}
}
Loading

0 comments on commit d1588fd

Please sign in to comment.