Skip to content

Commit d1588fd

Browse files
committed
add more tests
1 parent e324b0a commit d1588fd

File tree

13 files changed

+1001
-29
lines changed

13 files changed

+1001
-29
lines changed

app/src/androidTest/java/com/example/util/simpletimetracker/ComplexRulesTest.kt

Lines changed: 732 additions & 0 deletions
Large diffs are not rendered by default.

app/src/androidTest/java/com/example/util/simpletimetracker/RecordActionsMergeTest.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class RecordActionsMergeTest : BaseUiTest() {
3939
// Setup
4040
testUtils.addActivity(name)
4141
testUtils.addRecord(name)
42+
calendar.timeInMillis = System.currentTimeMillis()
4243
testUtils.addRecord(
4344
typeName = name,
4445
timeStarted = calendar.timeInMillis - TimeUnit.DAYS.toMillis(1),
@@ -83,15 +84,6 @@ class RecordActionsMergeTest : BaseUiTest() {
8384
checkViewIsDisplayed(withText(coreR.string.change_record_merge))
8485
clickOnViewWithText(coreR.string.change_record_actions_hint)
8586
pressBack()
86-
87-
// Untracked and have no prev record - not shown
88-
clickOnViewWithId(recordsR.id.btnRecordsContainerPrevious)
89-
clickOnView(allOf(withText(name), isCompletelyDisplayed()))
90-
clickOnViewWithId(changeRecordR.id.btnChangeRecordDelete)
91-
clickOnViewWithId(recordsR.id.btnRecordsContainerNext)
92-
clickOnView(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed()))
93-
clickOnViewWithText(coreR.string.change_record_actions_hint)
94-
checkViewDoesNotExist(withText(coreR.string.change_record_merge))
9587
}
9688

9789
@Test

app/src/androidTest/java/com/example/util/simpletimetracker/RecordActionsSplitTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ class RecordActionsSplitTest : BaseUiTest() {
276276
tagNames = listOf(tag),
277277
comment = comment,
278278
)
279+
Thread.sleep(1000)
279280

280281
// Check record
281282
tryAction { checkRunningRecord(fullName, timeStartedPreview, comment) }
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package com.example.util.simpletimetracker
2+
3+
import androidx.test.espresso.Espresso.pressBack
4+
import androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed
5+
import androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA
6+
import androidx.test.espresso.matcher.ViewMatchers.withId
7+
import androidx.test.espresso.matcher.ViewMatchers.withText
8+
import androidx.test.ext.junit.runners.AndroidJUnit4
9+
import com.example.util.simpletimetracker.utils.BaseUiTest
10+
import com.example.util.simpletimetracker.utils.NavUtils
11+
import com.example.util.simpletimetracker.utils.checkViewDoesNotExist
12+
import com.example.util.simpletimetracker.utils.checkViewIsDisplayed
13+
import com.example.util.simpletimetracker.utils.checkViewIsNotDisplayed
14+
import com.example.util.simpletimetracker.utils.clickOnView
15+
import com.example.util.simpletimetracker.utils.clickOnViewWithId
16+
import com.example.util.simpletimetracker.utils.clickOnViewWithText
17+
import com.example.util.simpletimetracker.utils.tryAction
18+
import dagger.hilt.android.testing.HiltAndroidTest
19+
import kotlinx.coroutines.runBlocking
20+
import org.hamcrest.CoreMatchers.allOf
21+
import org.junit.Test
22+
import org.junit.runner.RunWith
23+
import java.util.concurrent.TimeUnit
24+
import com.example.util.simpletimetracker.core.R as coreR
25+
import com.example.util.simpletimetracker.feature_base_adapter.R as baseR
26+
import com.example.util.simpletimetracker.feature_records.R as recordsR
27+
28+
@HiltAndroidTest
29+
@RunWith(AndroidJUnit4::class)
30+
class RecordsOptionsTest : BaseUiTest() {
31+
32+
@Test
33+
fun buttonsVisibility() {
34+
NavUtils.openRecordsScreen()
35+
36+
// Check not visible
37+
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerOptions))
38+
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerFilter))
39+
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerShare))
40+
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))
41+
42+
// Show options
43+
clickOnViewWithId(recordsR.id.btnRecordsContainerOptions)
44+
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerOptions))
45+
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerFilter))
46+
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerShare))
47+
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))
48+
49+
// Switch tabs
50+
NavUtils.openRunningRecordsScreen()
51+
NavUtils.openRecordsScreen()
52+
53+
// Still shown
54+
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerOptions))
55+
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerFilter))
56+
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerShare))
57+
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))
58+
59+
// Hide options
60+
clickOnViewWithId(recordsR.id.btnRecordsContainerOptions)
61+
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerOptions))
62+
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerFilter))
63+
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerShare))
64+
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))
65+
}
66+
67+
@Test
68+
fun filterType() {
69+
val name1 = "TypeName1"
70+
val name2 = "TypeName2"
71+
72+
// Add data
73+
testUtils.addActivity(name1)
74+
testUtils.addActivity(name2)
75+
76+
// Add records
77+
testUtils.addRecord(name1)
78+
testUtils.addRecord(name2)
79+
val before = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1)
80+
testUtils.addRecord(name1, timeStarted = before, timeEnded = before)
81+
testUtils.addRecord(name2, timeStarted = before, timeEnded = before)
82+
83+
// All records displayed
84+
NavUtils.openRecordsScreen()
85+
checkViewIsDisplayed(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed()))
86+
checkViewIsDisplayed(allOf(withText(name1), isCompletelyDisplayed()))
87+
checkViewIsDisplayed(allOf(withText(name2), isCompletelyDisplayed()))
88+
89+
// Filter untracked
90+
clickOnViewWithId(recordsR.id.btnRecordsContainerOptions)
91+
clickOnViewWithId(recordsR.id.btnRecordsContainerFilter)
92+
clickOnView(
93+
allOf(isDescendantOfA(withId(baseR.id.viewRecordTypeItem)), withText(coreR.string.untracked_time_name)),
94+
)
95+
pressBack()
96+
tryAction { checkViewDoesNotExist(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed())) }
97+
checkViewIsDisplayed(allOf(withText(name1), isCompletelyDisplayed()))
98+
checkViewIsDisplayed(allOf(withText(name2), isCompletelyDisplayed()))
99+
100+
// Filter activity
101+
clickOnViewWithId(recordsR.id.btnRecordsContainerFilter)
102+
clickOnView(allOf(isDescendantOfA(withId(baseR.id.viewRecordTypeItem)), withText(name1)))
103+
pressBack()
104+
tryAction { checkViewDoesNotExist(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed())) }
105+
checkViewDoesNotExist(allOf(withText(name1), isCompletelyDisplayed()))
106+
checkViewIsDisplayed(allOf(withText(name2), isCompletelyDisplayed()))
107+
108+
// Filter all
109+
clickOnViewWithId(recordsR.id.btnRecordsContainerFilter)
110+
clickOnView(allOf(isDescendantOfA(withId(baseR.id.viewRecordTypeItem)), withText(name2)))
111+
pressBack()
112+
tryAction { checkViewDoesNotExist(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed())) }
113+
checkViewDoesNotExist(allOf(withText(name1), isCompletelyDisplayed()))
114+
checkViewDoesNotExist(allOf(withText(name2), isCompletelyDisplayed()))
115+
checkViewIsDisplayed(allOf(withText(coreR.string.no_data), isCompletelyDisplayed()))
116+
117+
// Show all
118+
clickOnViewWithId(recordsR.id.btnRecordsContainerFilter)
119+
clickOnViewWithText(coreR.string.types_filter_show_all)
120+
pressBack()
121+
tryAction { checkViewIsDisplayed(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed())) }
122+
checkViewIsDisplayed(allOf(withText(name1), isCompletelyDisplayed()))
123+
checkViewIsDisplayed(allOf(withText(name2), isCompletelyDisplayed()))
124+
125+
// Hide all
126+
clickOnViewWithId(recordsR.id.btnRecordsContainerFilter)
127+
clickOnViewWithText(coreR.string.types_filter_hide_all)
128+
pressBack()
129+
tryAction { checkViewDoesNotExist(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed())) }
130+
checkViewDoesNotExist(allOf(withText(name1), isCompletelyDisplayed()))
131+
checkViewDoesNotExist(allOf(withText(name2), isCompletelyDisplayed()))
132+
checkViewIsDisplayed(allOf(withText(coreR.string.no_data), isCompletelyDisplayed()))
133+
}
134+
135+
@Test
136+
fun showRecordsCalendar() {
137+
val name = "Test"
138+
139+
// Add data
140+
runBlocking { prefsInteractor.setShowCalendarButtonOnRecordsTab(true) }
141+
testUtils.addActivity(name = name)
142+
testUtils.addRecord(name)
143+
144+
// Record is shown
145+
NavUtils.openRecordsScreen()
146+
checkViewDoesNotExist(allOf(withId(recordsR.id.viewRecordsCalendar), isCompletelyDisplayed()))
147+
checkViewDoesNotExist(allOf(withId(recordsR.id.tvRecordsCalendarHint), isCompletelyDisplayed()))
148+
checkViewIsDisplayed(allOf(withId(recordsR.id.rvRecordsList), isCompletelyDisplayed()))
149+
checkViewIsDisplayed(allOf(withText(name), isCompletelyDisplayed()))
150+
151+
// Change setting
152+
clickOnViewWithId(recordsR.id.btnRecordsContainerOptions)
153+
clickOnViewWithId(recordsR.id.btnRecordsContainerCalendarSwitch)
154+
155+
// Record is not shown
156+
checkViewIsDisplayed(allOf(withId(recordsR.id.viewRecordsCalendar), isCompletelyDisplayed()))
157+
checkViewIsDisplayed(allOf(withId(recordsR.id.tvRecordsCalendarHint), isCompletelyDisplayed()))
158+
checkViewDoesNotExist(allOf(withId(recordsR.id.rvRecordsList), isCompletelyDisplayed()))
159+
checkViewDoesNotExist(allOf(withText(name), isCompletelyDisplayed()))
160+
161+
// Change setting
162+
clickOnViewWithId(recordsR.id.btnRecordsContainerCalendarSwitch)
163+
164+
// Record is shown
165+
checkViewDoesNotExist(allOf(withId(recordsR.id.viewRecordsCalendar), isCompletelyDisplayed()))
166+
checkViewDoesNotExist(allOf(withId(recordsR.id.tvRecordsCalendarHint), isCompletelyDisplayed()))
167+
checkViewIsDisplayed(allOf(withId(recordsR.id.rvRecordsList), isCompletelyDisplayed()))
168+
checkViewIsDisplayed(allOf(withText(name), isCompletelyDisplayed()))
169+
}
170+
}

app/src/androidTest/java/com/example/util/simpletimetracker/SettingsTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,40 @@ class SettingsTest : BaseUiTest() {
18001800
checkViewIsDisplayed(allOf(withText(coreR.string.title_today), isCompletelyDisplayed()))
18011801
}
18021802

1803+
@Test
1804+
fun showCalendarSwitchOnTheSameTab() {
1805+
// Check setting
1806+
NavUtils.openSettingsScreen()
1807+
NavUtils.openSettingsDisplay()
1808+
scrollSettingsRecyclerToText(coreR.string.settings_show_calendar_button_on_records_tab)
1809+
checkCheckboxIsNotChecked(settingsCheckboxBesideText(coreR.string.settings_show_calendar_button_on_records_tab))
1810+
1811+
// Check not shown
1812+
NavUtils.openRecordsScreen()
1813+
clickOnViewWithId(recordsR.id.btnRecordsContainerOptions)
1814+
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))
1815+
1816+
// Change settings
1817+
NavUtils.openSettingsScreen()
1818+
scrollSettingsRecyclerToText(coreR.string.settings_show_calendar_button_on_records_tab)
1819+
clickOnSettingsCheckboxBesideText(coreR.string.settings_show_calendar_button_on_records_tab)
1820+
checkCheckboxIsChecked(settingsCheckboxBesideText(coreR.string.settings_show_calendar_button_on_records_tab))
1821+
1822+
// Check shown
1823+
NavUtils.openRecordsScreen()
1824+
checkViewIsDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))
1825+
1826+
// Change back
1827+
NavUtils.openSettingsScreen()
1828+
scrollSettingsRecyclerToText(coreR.string.settings_show_calendar_button_on_records_tab)
1829+
clickOnSettingsCheckboxBesideText(coreR.string.settings_show_calendar_button_on_records_tab)
1830+
checkCheckboxIsNotChecked(settingsCheckboxBesideText(coreR.string.settings_show_calendar_button_on_records_tab))
1831+
1832+
// Check not shown
1833+
NavUtils.openRecordsScreen()
1834+
checkViewIsNotDisplayed(withId(recordsR.id.btnRecordsContainerCalendarSwitch))
1835+
}
1836+
18031837
@Test
18041838
fun keepStatisticsRange() {
18051839
val name = "Test"

app/src/androidTest/java/com/example/util/simpletimetracker/StatisticsDetailTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,12 +669,13 @@ class StatisticsDetailTest : BaseUiTest() {
669669
val tag1 = "TagName1"
670670
val tag2 = "TagName2"
671671
val color = firstColor
672+
val lastColor = lastColor
672673
val icon = firstIcon
673674

674675
// Add activity
675676
testUtils.addActivity(name = name, color = color, icon = icon)
676677
testUtils.addRecordTag(tag1, name)
677-
testUtils.addRecordTag(tag2)
678+
testUtils.addRecordTag(tag2, color = lastColor)
678679

679680
// Add records
680681
var calendar = Calendar.getInstance()
@@ -780,7 +781,7 @@ class StatisticsDetailTest : BaseUiTest() {
780781

781782
// Tag split
782783
checkTagItem(color, tag1, "1$hourString 0$minuteString", "33%")
783-
checkTagItem(color, tag2, "2$hourString 0$minuteString", "67%")
784+
checkTagItem(lastColor, tag2, "2$hourString 0$minuteString", "67%")
784785
checkNoTagItem("Untagged")
785786

786787
// Next month

app/src/androidTest/java/com/example/util/simpletimetracker/utils/BaseUiTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import com.example.util.simpletimetracker.core.mapper.IconImageMapper
1616
import com.example.util.simpletimetracker.core.mapper.TimeMapper
1717
import com.example.util.simpletimetracker.core.utils.CountingIdlingResourceProvider
1818
import com.example.util.simpletimetracker.core.utils.TestUtils
19+
import com.example.util.simpletimetracker.domain.interactor.ComplexRuleInteractor
1920
import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
21+
import com.example.util.simpletimetracker.domain.repo.ComplexRuleRepo
2022
import com.example.util.simpletimetracker.feature_records.view.RecordsContainerFragment
2123
import com.example.util.simpletimetracker.feature_statistics.view.StatisticsContainerFragment
2224
import com.example.util.simpletimetracker.feature_views.pieChart.PieChartView
@@ -52,6 +54,9 @@ open class BaseUiTest {
5254
@Inject
5355
lateinit var languageInteractor: LanguageInteractor
5456

57+
@Inject
58+
lateinit var complexRuleRepo: ComplexRuleRepo
59+
5560
@get:Rule(order = 0)
5661
var hiltRule = HiltAndroidRule(this)
5762

app/src/androidTest/java/com/example/util/simpletimetracker/utils/NavUtils.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ object NavUtils {
9898
clickOnSettingsRecyclerText(coreR.string.settings_data_edit)
9999
}
100100

101+
fun openComplexRules() {
102+
scrollSettingsRecyclerToText(coreR.string.settings_complex_rules)
103+
clickOnSettingsRecyclerText(coreR.string.settings_complex_rules)
104+
}
105+
101106
fun openCardSizeScreen() {
102107
scrollSettingsRecyclerToText(coreR.string.settings_change_card_size)
103108
clickOnSettingsRecyclerText(coreR.string.settings_change_card_size)

core/src/main/java/com/example/util/simpletimetracker/core/utils/TestUtils.kt

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.example.util.simpletimetracker.domain.extension.orZero
66
import com.example.util.simpletimetracker.domain.interactor.ActivityFilterInteractor
77
import com.example.util.simpletimetracker.domain.interactor.CategoryInteractor
88
import com.example.util.simpletimetracker.domain.interactor.ClearDataInteractor
9+
import com.example.util.simpletimetracker.domain.interactor.ComplexRuleInteractor
910
import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
1011
import com.example.util.simpletimetracker.domain.interactor.RecordInteractor
1112
import com.example.util.simpletimetracker.domain.interactor.RecordTagInteractor
@@ -18,6 +19,7 @@ import com.example.util.simpletimetracker.domain.interactor.RunningRecordInterac
1819
import com.example.util.simpletimetracker.domain.model.ActivityFilter
1920
import com.example.util.simpletimetracker.domain.model.AppColor
2021
import com.example.util.simpletimetracker.domain.model.Category
22+
import com.example.util.simpletimetracker.domain.model.ComplexRule
2123
import com.example.util.simpletimetracker.domain.model.DayOfWeek
2224
import com.example.util.simpletimetracker.domain.model.Record
2325
import com.example.util.simpletimetracker.domain.model.RecordTag
@@ -39,6 +41,7 @@ class TestUtils @Inject constructor(
3941
private val recordTypeToDefaultTagInteractor: RecordTypeToDefaultTagInteractor,
4042
private val activityFilterInteractor: ActivityFilterInteractor,
4143
private val recordTypeGoalInteractor: RecordTypeGoalInteractor,
44+
private val complexRuleInteractor: ComplexRuleInteractor,
4245
private val prefsInteractor: PrefsInteractor,
4346
private val iconImageMapper: IconImageMapper,
4447
private val clearDataInteractor: ClearDataInteractor,
@@ -221,22 +224,17 @@ class TestUtils @Inject constructor(
221224
?: (0..colors.size).random()
222225
val availableCategories = categoryInteractor.getAll()
223226
val availableTypes = recordTypeInteractor.getAll()
224-
val selectedIds = names
225-
.mapNotNull { name ->
226-
when (type) {
227-
is ActivityFilter.Type.Activity -> {
228-
availableTypes.firstOrNull { it.name == name }?.id
229-
}
230-
231-
is ActivityFilter.Type.Category -> {
232-
availableCategories.firstOrNull { it.name == name }?.id
233-
}
227+
val selectedIds = names.mapNotNull { name ->
228+
when (type) {
229+
is ActivityFilter.Type.Activity -> {
230+
availableTypes.firstOrNull { it.name == name }?.id
231+
}
232+
233+
is ActivityFilter.Type.Category -> {
234+
availableCategories.firstOrNull { it.name == name }?.id
234235
}
235236
}
236-
.takeUnless {
237-
it.isEmpty()
238-
}
239-
.orEmpty()
237+
}
240238

241239
val data = ActivityFilter(
242240
selectedIds = selectedIds,
@@ -248,4 +246,36 @@ class TestUtils @Inject constructor(
248246

249247
activityFilterInteractor.add(data)
250248
}
249+
250+
fun addComplexRule(
251+
action: ComplexRule.Action,
252+
assignTagNames: List<String> = emptyList(),
253+
startingTypeNames: List<String> = emptyList(),
254+
currentTypeNames: List<String> = emptyList(),
255+
daysOfWeek: List<DayOfWeek> = emptyList(),
256+
) = runBlocking {
257+
val availableTypes = recordTypeInteractor.getAll()
258+
259+
fun getTypeIds(names: List<String>): Set<Long> {
260+
return names.mapNotNull { name ->
261+
availableTypes.firstOrNull { it.name == name }?.id
262+
}.toSet()
263+
}
264+
265+
val assignTagIds = recordTagInteractor.getAll()
266+
.filter { it.name in assignTagNames }
267+
.map { it.id }
268+
.toSet()
269+
270+
val data = ComplexRule(
271+
disabled = false,
272+
action = action,
273+
actionAssignTagIds = assignTagIds,
274+
conditionStartingTypeIds = getTypeIds(startingTypeNames),
275+
conditionCurrentTypeIds = getTypeIds(currentTypeNames),
276+
conditionDaysOfWeek = daysOfWeek.toSet(),
277+
)
278+
279+
complexRuleInteractor.add(data)
280+
}
251281
}

0 commit comments

Comments
 (0)