|
| 1 | +package com.example.util.simpletimetracker |
| 2 | + |
| 3 | +import androidx.test.espresso.Espresso.pressBack |
| 4 | +import androidx.test.espresso.matcher.ViewMatchers.hasDescendant |
| 5 | +import androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed |
| 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.core.extension.setToStartOfDay |
| 10 | +import com.example.util.simpletimetracker.utils.BaseUiTest |
| 11 | +import com.example.util.simpletimetracker.utils.NavUtils |
| 12 | +import com.example.util.simpletimetracker.utils.checkViewDoesNotExist |
| 13 | +import com.example.util.simpletimetracker.utils.checkViewIsDisplayed |
| 14 | +import com.example.util.simpletimetracker.utils.clickOnViewWithText |
| 15 | +import com.example.util.simpletimetracker.utils.longClickOnView |
| 16 | +import com.example.util.simpletimetracker.utils.tryAction |
| 17 | +import com.example.util.simpletimetracker.utils.withCardColor |
| 18 | +import com.example.util.simpletimetracker.utils.withTag |
| 19 | +import dagger.hilt.android.testing.HiltAndroidTest |
| 20 | +import org.hamcrest.CoreMatchers.allOf |
| 21 | +import org.junit.Test |
| 22 | +import org.junit.runner.RunWith |
| 23 | +import java.util.Calendar |
| 24 | +import java.util.concurrent.TimeUnit |
| 25 | +import com.example.util.simpletimetracker.core.R as coreR |
| 26 | +import com.example.util.simpletimetracker.feature_base_adapter.R as baseR |
| 27 | + |
| 28 | +@HiltAndroidTest |
| 29 | +@RunWith(AndroidJUnit4::class) |
| 30 | +class RecordActionsChangeTagTest : BaseUiTest() { |
| 31 | + |
| 32 | + @Test |
| 33 | + fun visibility() { |
| 34 | + val recordWithTags = "recordWithTags" |
| 35 | + val recordWithNoTags = "recordWithNoTags" |
| 36 | + val runningWithTags = "runningWithTags" |
| 37 | + val runningWithNoTags = "runningWithNoTags" |
| 38 | + val recordTag = "recordTag" |
| 39 | + val runningTag = "runningTag" |
| 40 | + |
| 41 | + // Setup |
| 42 | + testUtils.addActivity(recordWithTags) |
| 43 | + testUtils.addActivity(recordWithNoTags) |
| 44 | + testUtils.addActivity(runningWithTags) |
| 45 | + testUtils.addActivity(runningWithNoTags) |
| 46 | + testUtils.addRecordTag(recordTag, typeName = recordWithTags) |
| 47 | + testUtils.addRecordTag(runningTag, typeName = runningWithTags) |
| 48 | + testUtils.addRunningRecord(runningWithTags) |
| 49 | + testUtils.addRunningRecord(runningWithNoTags) |
| 50 | + testUtils.addRecord(recordWithTags) |
| 51 | + testUtils.addRecord(recordWithNoTags) |
| 52 | + calendar.timeInMillis = System.currentTimeMillis() |
| 53 | + testUtils.addRecord( |
| 54 | + typeName = recordWithTags, |
| 55 | + timeStarted = calendar.timeInMillis - TimeUnit.DAYS.toMillis(1), |
| 56 | + timeEnded = calendar.timeInMillis - TimeUnit.DAYS.toMillis(1), |
| 57 | + ) |
| 58 | + Thread.sleep(1000) |
| 59 | + |
| 60 | + // Running record with tags - shown |
| 61 | + NavUtils.openRecordsScreen() |
| 62 | + longClickOnView(allOf(withText(runningWithTags), isCompletelyDisplayed())) |
| 63 | + checkViewIsDisplayed(withText(coreR.string.data_edit_change_tag)) |
| 64 | + pressBack() |
| 65 | + |
| 66 | + // Running records with no tags - not shown |
| 67 | + NavUtils.openRecordsScreen() |
| 68 | + longClickOnView(allOf(withText(runningWithNoTags), isCompletelyDisplayed())) |
| 69 | + checkViewDoesNotExist(withText(coreR.string.data_edit_change_tag)) |
| 70 | + pressBack() |
| 71 | + |
| 72 | + // Record with tags - shown |
| 73 | + longClickOnView(allOf(withText(recordWithTags), isCompletelyDisplayed())) |
| 74 | + checkViewIsDisplayed(withText(coreR.string.data_edit_change_tag)) |
| 75 | + pressBack() |
| 76 | + |
| 77 | + // Record with no tags - not shown |
| 78 | + longClickOnView(allOf(withText(recordWithNoTags), isCompletelyDisplayed())) |
| 79 | + checkViewDoesNotExist(withText(coreR.string.data_edit_change_tag)) |
| 80 | + pressBack() |
| 81 | + |
| 82 | + // Untracked - not shown |
| 83 | + longClickOnView(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed())) |
| 84 | + checkViewDoesNotExist(withText(coreR.string.data_edit_change_tag)) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun record() { |
| 89 | + val name1 = "Name1" |
| 90 | + val color1 = firstColor |
| 91 | + val icon1 = firstIcon |
| 92 | + val comment = "Some_comment" |
| 93 | + val tag = "Tag" |
| 94 | + val tagGeneral = "TagGeneral" |
| 95 | + val fullName1 = "$name1 - $tag, $tagGeneral" |
| 96 | + val fullName2 = "$name1 - $tagGeneral" |
| 97 | + val calendar = Calendar.getInstance() |
| 98 | + |
| 99 | + // Setup |
| 100 | + val current = calendar.timeInMillis |
| 101 | + val difference = TimeUnit.MINUTES.toMillis(30) |
| 102 | + val timeStartedTimestamp = current - difference |
| 103 | + val timeStartedPreview = timeStartedTimestamp.formatTime() |
| 104 | + val timeEndedPreview = current.formatTime() |
| 105 | + val timeRangePreview = difference.formatInterval() |
| 106 | + |
| 107 | + testUtils.addActivity(name = name1, color = color1, icon = icon1) |
| 108 | + testUtils.addRecordTag(tag, typeName = name1) |
| 109 | + testUtils.addRecordTag(tagGeneral) |
| 110 | + testUtils.addRecord( |
| 111 | + typeName = name1, |
| 112 | + timeStarted = timeStartedTimestamp, |
| 113 | + timeEnded = current, |
| 114 | + tagNames = listOf(tag, tagGeneral), |
| 115 | + comment = comment, |
| 116 | + ) |
| 117 | + |
| 118 | + // Check record |
| 119 | + NavUtils.openRecordsScreen() |
| 120 | + checkRecord( |
| 121 | + name = fullName1, |
| 122 | + color = color1, |
| 123 | + icon = icon1, |
| 124 | + timeStartedPreview = timeStartedPreview, |
| 125 | + timeEndedPreview = timeEndedPreview, |
| 126 | + timeRangePreview = timeRangePreview, |
| 127 | + comment = comment, |
| 128 | + ) |
| 129 | + |
| 130 | + // Change |
| 131 | + longClickOnView(allOf(withText(fullName1), isCompletelyDisplayed())) |
| 132 | + clickOnViewWithText(R.string.data_edit_change_tag) |
| 133 | + clickOnViewWithText(tag) |
| 134 | + clickOnViewWithText(R.string.change_record_save) |
| 135 | + |
| 136 | + tryAction { |
| 137 | + checkRecord( |
| 138 | + name = fullName2, |
| 139 | + color = color1, |
| 140 | + icon = icon1, |
| 141 | + timeStartedPreview = timeStartedPreview, |
| 142 | + timeEndedPreview = timeEndedPreview, |
| 143 | + timeRangePreview = timeRangePreview, |
| 144 | + comment = comment, |
| 145 | + ) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + fun runningRecord() { |
| 151 | + val name1 = "Name1" |
| 152 | + val color1 = firstColor |
| 153 | + val icon1 = firstIcon |
| 154 | + val comment = "Some_comment" |
| 155 | + val tag = "Tag" |
| 156 | + val tagGeneral = "TagGeneral" |
| 157 | + val fullName1 = "$name1 - $tag, $tagGeneral" |
| 158 | + val fullName2 = "$name1 - $tagGeneral" |
| 159 | + val calendar = Calendar.getInstance() |
| 160 | + |
| 161 | + // Setup |
| 162 | + val current = calendar.timeInMillis |
| 163 | + val difference = TimeUnit.MINUTES.toMillis(30) |
| 164 | + val timeStartedTimestamp = current - difference |
| 165 | + val timeStartedPreview = timeStartedTimestamp.formatTime() |
| 166 | + |
| 167 | + testUtils.addActivity(name = name1, color = color1, icon = icon1) |
| 168 | + testUtils.addRecordTag(tag, typeName = name1) |
| 169 | + testUtils.addRecordTag(tagGeneral) |
| 170 | + testUtils.addRunningRecord( |
| 171 | + typeName = name1, |
| 172 | + timeStarted = timeStartedTimestamp, |
| 173 | + tagNames = listOf(tag, tagGeneral), |
| 174 | + comment = comment, |
| 175 | + ) |
| 176 | + |
| 177 | + // Check record |
| 178 | + NavUtils.openRecordsScreen() |
| 179 | + checkRunningRecord( |
| 180 | + name = fullName1, |
| 181 | + color = color1, |
| 182 | + icon = icon1, |
| 183 | + timeStartedPreview = timeStartedPreview, |
| 184 | + comment = comment, |
| 185 | + ) |
| 186 | + |
| 187 | + // Change |
| 188 | + longClickOnView(allOf(withText(fullName1), isCompletelyDisplayed())) |
| 189 | + clickOnViewWithText(R.string.data_edit_change_tag) |
| 190 | + clickOnViewWithText(tag) |
| 191 | + clickOnViewWithText(R.string.change_record_save) |
| 192 | + |
| 193 | + tryAction { |
| 194 | + checkRunningRecord( |
| 195 | + name = fullName2, |
| 196 | + color = color1, |
| 197 | + icon = icon1, |
| 198 | + timeStartedPreview = timeStartedPreview, |
| 199 | + comment = comment, |
| 200 | + ) |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + @Suppress("SameParameterValue") |
| 205 | + private fun checkRecord( |
| 206 | + name: String, |
| 207 | + color: Int, |
| 208 | + icon: Int, |
| 209 | + timeStartedPreview: String, |
| 210 | + timeEndedPreview: String, |
| 211 | + timeRangePreview: String, |
| 212 | + comment: String, |
| 213 | + ) { |
| 214 | + checkViewIsDisplayed( |
| 215 | + allOf( |
| 216 | + withId(baseR.id.viewRecordItem), |
| 217 | + withCardColor(color), |
| 218 | + hasDescendant(withText(name)), |
| 219 | + hasDescendant(withTag(icon)), |
| 220 | + hasDescendant(withText(timeStartedPreview)), |
| 221 | + hasDescendant(withText(timeEndedPreview)), |
| 222 | + hasDescendant(withText(timeRangePreview)), |
| 223 | + hasDescendant(withText(comment)), |
| 224 | + isCompletelyDisplayed(), |
| 225 | + ), |
| 226 | + ) |
| 227 | + } |
| 228 | + |
| 229 | + @Suppress("SameParameterValue") |
| 230 | + private fun checkRunningRecord( |
| 231 | + name: String, |
| 232 | + color: Int, |
| 233 | + icon: Int, |
| 234 | + timeStartedPreview: String, |
| 235 | + comment: String, |
| 236 | + ) { |
| 237 | + checkViewIsDisplayed( |
| 238 | + allOf( |
| 239 | + withId(baseR.id.viewRunningRecordItem), |
| 240 | + withCardColor(color), |
| 241 | + hasDescendant(withText(name)), |
| 242 | + hasDescendant(withTag(icon)), |
| 243 | + hasDescendant(withText(timeStartedPreview)), |
| 244 | + hasDescendant(withText(comment)), |
| 245 | + isCompletelyDisplayed(), |
| 246 | + ), |
| 247 | + ) |
| 248 | + } |
| 249 | +} |
0 commit comments