|
| 1 | +package com.example.util.simpletimetracker |
| 2 | + |
| 3 | +import android.view.View |
| 4 | +import androidx.test.espresso.matcher.ViewMatchers.hasDescendant |
| 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.feature_change_record_tag.R |
| 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.clickOnRecyclerItem |
| 15 | +import com.example.util.simpletimetracker.utils.clickOnView |
| 16 | +import com.example.util.simpletimetracker.utils.clickOnViewWithText |
| 17 | +import com.example.util.simpletimetracker.utils.longClickOnView |
| 18 | +import com.example.util.simpletimetracker.utils.scrollRecyclerInPagerToView |
| 19 | +import com.example.util.simpletimetracker.utils.tryAction |
| 20 | +import com.example.util.simpletimetracker.utils.typeTextIntoView |
| 21 | +import dagger.hilt.android.testing.HiltAndroidTest |
| 22 | +import kotlinx.coroutines.runBlocking |
| 23 | +import org.hamcrest.CoreMatchers.allOf |
| 24 | +import org.hamcrest.Matcher |
| 25 | +import org.junit.Test |
| 26 | +import org.junit.runner.RunWith |
| 27 | +import com.example.util.simpletimetracker.core.R as coreR |
| 28 | +import com.example.util.simpletimetracker.feature_change_record.R as changeRecordR |
| 29 | +import com.example.util.simpletimetracker.feature_change_running_record.R as changeRunningRecordR |
| 30 | +import com.example.util.simpletimetracker.feature_records.R as recordsR |
| 31 | + |
| 32 | +@HiltAndroidTest |
| 33 | +@RunWith(AndroidJUnit4::class) |
| 34 | +class RecordTagValueTest : BaseUiTest() { |
| 35 | + |
| 36 | + @Test |
| 37 | + fun start() { |
| 38 | + val typeName1 = "TypeName1" |
| 39 | + val typeName2 = "TypeName2" |
| 40 | + val typeName3 = "TypeName3" |
| 41 | + val tagNoValue = "TagNoValue" |
| 42 | + val tagWithValue = "TagValue" |
| 43 | + val tagWithValueWithSuffix = "TagValueWithSuffix" |
| 44 | + val suffix = "kg" |
| 45 | + val value1 = "1" |
| 46 | + val value2 = "2" |
| 47 | + val fullName = "$typeName1 - $tagNoValue" |
| 48 | + val fullNameWithValue = "$typeName2 - $tagWithValue ($value1)" |
| 49 | + val fullNameWithValueAndSuffix = "$typeName3 - $tagWithValueWithSuffix ($value2 $suffix)" |
| 50 | + |
| 51 | + fun checkRunningRecord(name: String) { |
| 52 | + checkViewIsDisplayed( |
| 53 | + allOf( |
| 54 | + isDescendantOfA(withId(R.id.viewRunningRecordItem)), |
| 55 | + withText(fullName), |
| 56 | + ), |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + // Add data |
| 61 | + runBlocking { prefsInteractor.setShowRecordTagSelection(true) } |
| 62 | + testUtils.addActivity(typeName1) |
| 63 | + testUtils.addActivity(typeName2) |
| 64 | + testUtils.addActivity(typeName3) |
| 65 | + testUtils.addRecordTag(tagNoValue, typeName1) |
| 66 | + testUtils.addRecordTag(tagWithValue, typeName2, hasTagValue = true) |
| 67 | + testUtils.addRecordTag(tagWithValueWithSuffix, typeName3, hasTagValue = true, tagValueSuffix = suffix) |
| 68 | + Thread.sleep(1000) |
| 69 | + |
| 70 | + // Add no value |
| 71 | + tryAction { clickOnViewWithText(typeName1) } |
| 72 | + tryAction { clickOnViewWithText(tagNoValue) } |
| 73 | + clickOnViewWithText(R.string.duration_dialog_save) |
| 74 | + tryAction { checkRunningRecord(fullName) } |
| 75 | + |
| 76 | + // Add with value |
| 77 | + clickOnViewWithText(typeName2) |
| 78 | + tryAction { clickOnViewWithText(tagWithValue) } |
| 79 | + typeTextIntoView(R.id.etCommentItemField, value1) |
| 80 | + clickOnViewWithText(R.string.duration_dialog_save) |
| 81 | + checkViewIsDisplayed(withText("$tagWithValue ($value1)")) |
| 82 | + clickOnViewWithText(R.string.duration_dialog_save) |
| 83 | + tryAction { checkRunningRecord(fullNameWithValue) } |
| 84 | + |
| 85 | + // Add with value and suffix |
| 86 | + clickOnViewWithText(typeName3) |
| 87 | + tryAction { clickOnViewWithText(tagWithValueWithSuffix) } |
| 88 | + typeTextIntoView(R.id.etCommentItemField, value2) |
| 89 | + clickOnViewWithText(R.string.duration_dialog_save) |
| 90 | + checkViewIsDisplayed(withText("$tagWithValueWithSuffix ($value2 $suffix)")) |
| 91 | + clickOnViewWithText(R.string.duration_dialog_save) |
| 92 | + tryAction { checkRunningRecord(fullNameWithValueAndSuffix) } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun changeRecord() { |
| 97 | + val name = "Test1" |
| 98 | + val tag1 = "tag1" |
| 99 | + val tag2 = "tag2" |
| 100 | + val fullName1 = "$name - $tag1" |
| 101 | + |
| 102 | + // Add activities |
| 103 | + testUtils.addActivity(name) |
| 104 | + testUtils.addRecordTag(tag1, name) |
| 105 | + testUtils.addRecordTag(tag2, name, hasTagValue = true) |
| 106 | + testUtils.addRecord(name, tagNames = listOf(tag1)) |
| 107 | + |
| 108 | + // Open record |
| 109 | + NavUtils.openRecordsScreen() |
| 110 | + clickOnView(withText(fullName1)) |
| 111 | + checkPreviewUpdated(hasDescendant(withText(fullName1))) |
| 112 | + |
| 113 | + // Change value |
| 114 | + clickOnViewWithText(coreR.string.change_record_tag_field) |
| 115 | + clickOnRecyclerItem(changeRecordR.id.rvChangeRecordCategories, withText(tag1)) |
| 116 | + clickOnRecyclerItem(changeRecordR.id.rvChangeRecordCategories, withText(tag2)) |
| 117 | + typeTextIntoView(R.id.etCommentItemField, "1") |
| 118 | + clickOnViewWithText(coreR.string.change_record_type_save) |
| 119 | + checkViewIsDisplayed(allOf(withId(R.id.viewCategoryItem), hasDescendant(withText("$tag2 (1)")))) |
| 120 | + checkPreviewUpdated(hasDescendant(withText("$name - $tag2 (1)"))) |
| 121 | + |
| 122 | + // Change value |
| 123 | + clickOnRecyclerItem(changeRecordR.id.rvChangeRecordCategories, withText("$tag2 (1)")) |
| 124 | + clickOnRecyclerItem(changeRecordR.id.rvChangeRecordCategories, withText(tag2)) |
| 125 | + typeTextIntoView(R.id.etCommentItemField, "2") |
| 126 | + clickOnViewWithText(coreR.string.change_record_type_save) |
| 127 | + checkViewIsDisplayed(allOf(withId(R.id.viewCategoryItem), hasDescendant(withText("$tag2 (2)")))) |
| 128 | + checkPreviewUpdated(hasDescendant(withText("$name - $tag2 (2)"))) |
| 129 | + |
| 130 | + // Record updated |
| 131 | + clickOnViewWithText(coreR.string.change_record_type_save) |
| 132 | + checkViewDoesNotExist(withText(fullName1)) |
| 133 | + checkViewIsDisplayed(withText("$name - $tag2 (2)")) |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + fun changeRunningRecord() { |
| 138 | + val name = "Test1" |
| 139 | + val tag1 = "tag1" |
| 140 | + val tag2 = "tag2" |
| 141 | + val fullName1 = "$name - $tag1" |
| 142 | + |
| 143 | + // Add activities |
| 144 | + testUtils.addActivity(name) |
| 145 | + testUtils.addRecordTag(tag1, name) |
| 146 | + testUtils.addRecordTag(tag2, name, hasTagValue = true) |
| 147 | + testUtils.addRunningRecord(name, tagNames = listOf(tag1)) |
| 148 | + Thread.sleep(1000) |
| 149 | + |
| 150 | + // Open record |
| 151 | + tryAction { longClickOnView(withText(fullName1)) } |
| 152 | + checkRunningPreviewUpdated(hasDescendant(withText(fullName1))) |
| 153 | + |
| 154 | + // Change value |
| 155 | + clickOnViewWithText(coreR.string.change_record_tag_field) |
| 156 | + clickOnRecyclerItem(changeRecordR.id.rvChangeRecordCategories, withText(tag1)) |
| 157 | + clickOnRecyclerItem(changeRecordR.id.rvChangeRecordCategories, withText(tag2)) |
| 158 | + typeTextIntoView(R.id.etCommentItemField, "1") |
| 159 | + clickOnViewWithText(coreR.string.change_record_type_save) |
| 160 | + checkViewIsDisplayed(allOf(withId(R.id.viewCategoryItem), hasDescendant(withText("$tag2 (1)")))) |
| 161 | + checkRunningPreviewUpdated(hasDescendant(withText("$name - $tag2 (1)"))) |
| 162 | + |
| 163 | + // Record updated |
| 164 | + clickOnViewWithText(coreR.string.change_record_type_save) |
| 165 | + checkViewDoesNotExist(withText(fullName1)) |
| 166 | + checkViewIsDisplayed(withText("$name - $tag2 (1)")) |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + fun valueFormatting() { |
| 171 | + val typeName = "typeName" |
| 172 | + val tag1 = "tag1" |
| 173 | + val tag2 = "tag2" |
| 174 | + val suffix = "kg" |
| 175 | + |
| 176 | + val data = listOf( |
| 177 | + tag1 to 1.0 to "$typeName - $tag1 (1)", |
| 178 | + tag2 to 1.0 to "$typeName - $tag2 (1 $suffix)", |
| 179 | + tag1 to 2.3 to "$typeName - $tag1 (2.3)", |
| 180 | + tag2 to 2.3 to "$typeName - $tag2 (2.3 $suffix)", |
| 181 | + tag1 to 4.0000005 to "$typeName - $tag1 (4.0000005)", |
| 182 | + tag2 to 4.0000005 to "$typeName - $tag2 (4.0000005 $suffix)", |
| 183 | + tag1 to -1.0 to "$typeName - $tag1 (-1)", |
| 184 | + tag2 to -1.0 to "$typeName - $tag2 (-1 $suffix)", |
| 185 | + tag1 to -2.3 to "$typeName - $tag1 (-2.3)", |
| 186 | + tag2 to -2.3 to "$typeName - $tag2 (-2.3 $suffix)", |
| 187 | + tag1 to -4.0000005 to "$typeName - $tag1 (-4.0000005)", |
| 188 | + tag2 to -4.0000005 to "$typeName - $tag2 (-4.0000005 $suffix)", |
| 189 | + ) |
| 190 | + |
| 191 | + // Add data |
| 192 | + testUtils.addActivity(typeName) |
| 193 | + testUtils.addRecordTag(tag1, typeName, hasTagValue = true) |
| 194 | + testUtils.addRecordTag(tag2, typeName, hasTagValue = true, tagValueSuffix = suffix) |
| 195 | + data.forEach { (tag, _) -> |
| 196 | + testUtils.addRecord(typeName, tagNamesWithValues = listOf(tag)) |
| 197 | + } |
| 198 | + |
| 199 | + // Check |
| 200 | + NavUtils.openRecordsScreen() |
| 201 | + data.forEach { (_, name) -> |
| 202 | + scrollRecyclerInPagerToView( |
| 203 | + recordsR.id.rvRecordsList, |
| 204 | + allOf(withId(R.id.viewRecordItem), hasDescendant(withText(name))), |
| 205 | + ) |
| 206 | + checkViewIsDisplayed(withText(name)) |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + private fun checkPreviewUpdated(matcher: Matcher<View>) = |
| 211 | + checkViewIsDisplayed(allOf(withId(changeRecordR.id.previewChangeRecord), matcher)) |
| 212 | + |
| 213 | + private fun checkRunningPreviewUpdated(matcher: Matcher<View>) = |
| 214 | + checkViewIsDisplayed(allOf(withId(changeRunningRecordR.id.previewChangeRunningRecord), matcher)) |
| 215 | +} |
0 commit comments