|
| 1 | +package com.example.util.simpletimetracker |
| 2 | + |
| 3 | +import androidx.test.core.app.ApplicationProvider |
| 4 | +import androidx.test.espresso.Espresso.pressBack |
| 5 | +import androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed |
| 6 | +import androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA |
| 7 | +import androidx.test.espresso.matcher.ViewMatchers.withId |
| 8 | +import androidx.test.espresso.matcher.ViewMatchers.withText |
| 9 | +import androidx.test.ext.junit.rules.ActivityScenarioRule |
| 10 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 11 | +import com.example.util.simpletimetracker.core.mapper.ColorMapper |
| 12 | +import com.example.util.simpletimetracker.core.mapper.IconMapper |
| 13 | +import com.example.util.simpletimetracker.core.utils.TestUtils |
| 14 | +import com.example.util.simpletimetracker.di.AppModule |
| 15 | +import com.example.util.simpletimetracker.di.DaggerTestAppComponent |
| 16 | +import com.example.util.simpletimetracker.ui.MainActivity |
| 17 | +import com.example.util.simpletimetracker.utils.NavUtils |
| 18 | +import com.example.util.simpletimetracker.utils.checkViewDoesNotExist |
| 19 | +import com.example.util.simpletimetracker.utils.checkViewIsDisplayed |
| 20 | +import com.example.util.simpletimetracker.utils.clickOnView |
| 21 | +import com.example.util.simpletimetracker.utils.clickOnViewWithId |
| 22 | +import org.hamcrest.CoreMatchers.allOf |
| 23 | +import org.junit.Before |
| 24 | +import org.junit.Rule |
| 25 | +import org.junit.Test |
| 26 | +import org.junit.runner.RunWith |
| 27 | +import javax.inject.Inject |
| 28 | + |
| 29 | +@RunWith(AndroidJUnit4::class) |
| 30 | +class StatisticsFilterTest { |
| 31 | + |
| 32 | + @Inject |
| 33 | + lateinit var testUtils: TestUtils |
| 34 | + |
| 35 | + @Inject |
| 36 | + lateinit var iconMapper: IconMapper |
| 37 | + |
| 38 | + @Rule |
| 39 | + @JvmField |
| 40 | + val activityScenarioRule = ActivityScenarioRule(MainActivity::class.java) |
| 41 | + |
| 42 | + @Before |
| 43 | + fun setUp() { |
| 44 | + val app = ApplicationProvider.getApplicationContext() as TimeTrackerApp |
| 45 | + DaggerTestAppComponent.builder() |
| 46 | + .appModule(AppModule(app)) |
| 47 | + .build() |
| 48 | + .inject(this) |
| 49 | + |
| 50 | + testUtils.clearDatabase() |
| 51 | + testUtils.clearPrefs() |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun test() { |
| 56 | + val name = "Test1" |
| 57 | + val newName = "Test2" |
| 58 | + val firstColor = ColorMapper.availableColors.first() |
| 59 | + val lastColor = ColorMapper.availableColors.last() |
| 60 | + val firstIcon = iconMapper.availableIconsNames.values.first() |
| 61 | + val lastIcon = iconMapper.availableIconsNames.values.last() |
| 62 | + |
| 63 | + // Add activities |
| 64 | + NavUtils.addActivity(name, firstColor, firstIcon) |
| 65 | + NavUtils.addActivity(newName, lastColor, lastIcon) |
| 66 | + |
| 67 | + // Add records |
| 68 | + NavUtils.openRecordsScreen() |
| 69 | + NavUtils.addRecord(name) |
| 70 | + NavUtils.addRecord(newName) |
| 71 | + |
| 72 | + NavUtils.openStatisticsScreen() |
| 73 | + |
| 74 | + // All records displayed |
| 75 | + checkViewIsDisplayed(allOf(withText(R.string.untracked_time_name), isCompletelyDisplayed())) |
| 76 | + checkViewIsDisplayed(allOf(withText(name), isCompletelyDisplayed())) |
| 77 | + checkViewIsDisplayed(allOf(withText(newName), isCompletelyDisplayed())) |
| 78 | + |
| 79 | + // Filter untracked |
| 80 | + clickOnViewWithId(R.id.btnStatisticsChartFilter) |
| 81 | + clickOnView( |
| 82 | + allOf( |
| 83 | + isDescendantOfA(withId(R.id.layoutRecordTypeItem)), |
| 84 | + withText(R.string.untracked_time_name) |
| 85 | + ) |
| 86 | + ) |
| 87 | + pressBack() |
| 88 | + checkViewDoesNotExist( |
| 89 | + allOf(withText(R.string.untracked_time_name), isCompletelyDisplayed()) |
| 90 | + ) |
| 91 | + checkViewIsDisplayed(allOf(withText(name), isCompletelyDisplayed())) |
| 92 | + checkViewIsDisplayed(allOf(withText(newName), isCompletelyDisplayed())) |
| 93 | + |
| 94 | + // Filter activity |
| 95 | + clickOnViewWithId(R.id.btnStatisticsChartFilter) |
| 96 | + clickOnView(allOf(isDescendantOfA(withId(R.id.layoutRecordTypeItem)), withText(name))) |
| 97 | + pressBack() |
| 98 | + checkViewDoesNotExist( |
| 99 | + allOf(withText(R.string.untracked_time_name), isCompletelyDisplayed()) |
| 100 | + ) |
| 101 | + checkViewDoesNotExist(allOf(withText(name), isCompletelyDisplayed())) |
| 102 | + checkViewIsDisplayed(allOf(withText(newName), isCompletelyDisplayed())) |
| 103 | + |
| 104 | + // Remove filters |
| 105 | + clickOnViewWithId(R.id.btnStatisticsChartFilter) |
| 106 | + clickOnView( |
| 107 | + allOf( |
| 108 | + isDescendantOfA(withId(R.id.layoutRecordTypeItem)), |
| 109 | + withText(R.string.untracked_time_name) |
| 110 | + ) |
| 111 | + ) |
| 112 | + clickOnView(allOf(isDescendantOfA(withId(R.id.layoutRecordTypeItem)), withText(name))) |
| 113 | + pressBack() |
| 114 | + checkViewIsDisplayed(allOf(withText(R.string.untracked_time_name), isCompletelyDisplayed())) |
| 115 | + checkViewIsDisplayed(allOf(withText(name), isCompletelyDisplayed())) |
| 116 | + checkViewIsDisplayed(allOf(withText(newName), isCompletelyDisplayed())) |
| 117 | + } |
| 118 | +} |
0 commit comments