Skip to content

Commit

Permalink
add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Jan 25, 2025
1 parent 55e38a1 commit c43decd
Show file tree
Hide file tree
Showing 6 changed files with 435 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
package com.example.util.simpletimetracker

import androidx.test.espresso.Espresso.pressBack
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
import androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed
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.core.extension.setToStartOfDay
import com.example.util.simpletimetracker.utils.BaseUiTest
import com.example.util.simpletimetracker.utils.NavUtils
import com.example.util.simpletimetracker.utils.checkViewIsDisplayed
import com.example.util.simpletimetracker.utils.clickOnViewWithText
import com.example.util.simpletimetracker.utils.longClickOnView
import com.example.util.simpletimetracker.utils.tryAction
import com.example.util.simpletimetracker.utils.withCardColor
import com.example.util.simpletimetracker.utils.withTag
import dagger.hilt.android.testing.HiltAndroidTest
import org.hamcrest.CoreMatchers.allOf
import org.junit.Test
import org.junit.runner.RunWith
import java.util.Calendar
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

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

@Test
fun visibility() {
val name1 = "Name1"
val name2 = "Name2"

// Setup
testUtils.addActivity(name1)
testUtils.addActivity(name2)
testUtils.addRecord(name1)
calendar.timeInMillis = System.currentTimeMillis()
testUtils.addRecord(
typeName = name1,
timeStarted = calendar.timeInMillis - TimeUnit.DAYS.toMillis(1),
timeEnded = calendar.timeInMillis - TimeUnit.DAYS.toMillis(1),
)
testUtils.addRunningRecord(name2)
Thread.sleep(1000)

// Running record - shown
NavUtils.openRecordsScreen()
longClickOnView(allOf(withText(name2), isCompletelyDisplayed()))
checkViewIsDisplayed(withText(coreR.string.data_edit_change_activity))
pressBack()

// Record - shown
longClickOnView(allOf(withText(name1), isCompletelyDisplayed()))
checkViewIsDisplayed(withText(coreR.string.data_edit_change_activity))
pressBack()

// Untracked - shown
longClickOnView(allOf(withText(coreR.string.untracked_time_name), isCompletelyDisplayed()))
checkViewIsDisplayed(withText(coreR.string.data_edit_change_activity))
}

@Test
fun record() {
val name1 = "Name1"
val name2 = "Name2"
val color1 = firstColor
val color2 = lastColor
val icon1 = firstIcon
val icon2 = lastIcon
val comment = "Some_comment"
val tag = "Tag"
val tagGeneral = "TagGeneral"
val fullName1 = "$name1 - $tag, $tagGeneral"
val fullName2 = "$name2 - $tagGeneral"
val calendar = Calendar.getInstance()

// Setup
val current = calendar.timeInMillis
val difference = TimeUnit.MINUTES.toMillis(30)
val timeStartedTimestamp = current - difference
val timeStartedPreview = timeStartedTimestamp.formatTime()
val timeEndedPreview = current.formatTime()
val timeRangePreview = difference.formatInterval()

testUtils.addActivity(name = name1, color = color1, icon = icon1)
testUtils.addActivity(name = name2, color = color2, icon = icon2)
testUtils.addRecordTag(tag, typeName = name1)
testUtils.addRecordTag(tagGeneral)
testUtils.addRecord(
typeName = name1,
timeStarted = timeStartedTimestamp,
timeEnded = current,
tagNames = listOf(tag, tagGeneral),
comment = comment,
)

// Check record
NavUtils.openRecordsScreen()
checkRecord(
name = fullName1,
color = color1,
icon = icon1,
timeStartedPreview = timeStartedPreview,
timeEndedPreview = timeEndedPreview,
timeRangePreview = timeRangePreview,
comment = comment,
)

// Change
longClickOnView(allOf(withText(fullName1), isCompletelyDisplayed()))
clickOnViewWithText(R.string.data_edit_change_activity)
clickOnViewWithText(name2)

tryAction {
checkRecord(
name = fullName2,
color = color2,
icon = icon2,
timeStartedPreview = timeStartedPreview,
timeEndedPreview = timeEndedPreview,
timeRangePreview = timeRangePreview,
comment = comment,
)
}
}

@Test
fun untrackedRecord() {
val name1 = "Name1"
val color1 = firstColor
val icon1 = firstIcon
val calendar = Calendar.getInstance()

// Setup
testUtils.addActivity(name = name1, color = color1, icon = icon1)
val current = calendar.timeInMillis
val yesterday = current - TimeUnit.DAYS.toMillis(1)
testUtils.addRecord(typeName = name1, timeStarted = yesterday, timeEnded = yesterday)

val startOfDay = calendar.apply { setToStartOfDay() }.timeInMillis
val timeStartedPreview = startOfDay.formatTime()
val timeEndedPreview = current.formatTime()
val difference = current - startOfDay
val timeRangePreview = difference.formatInterval()

// Check record
NavUtils.openRecordsScreen()
checkViewIsDisplayed(
allOf(
withId(baseR.id.viewRecordItem),
hasDescendant(withText(coreR.string.untracked_time_name)),
hasDescendant(withText(timeStartedPreview)),
hasDescendant(withText(timeEndedPreview)),
hasDescendant(withText(timeRangePreview)),
isCompletelyDisplayed(),
),
)

// Change
longClickOnView(withText(coreR.string.untracked_time_name))
clickOnViewWithText(R.string.data_edit_change_activity)
clickOnViewWithText(name1)

tryAction {
checkRecord(
name = name1,
color = color1,
icon = icon1,
timeStartedPreview = timeStartedPreview,
timeEndedPreview = timeEndedPreview,
timeRangePreview = timeRangePreview,
comment = "",
)
}
}

@Test
fun runningRecord() {
val name1 = "Name1"
val name2 = "Name2"
val color1 = firstColor
val color2 = lastColor
val icon1 = firstIcon
val icon2 = lastIcon
val comment = "Some_comment"
val tag = "Tag"
val tagGeneral = "TagGeneral"
val fullName1 = "$name1 - $tag, $tagGeneral"
val fullName2 = "$name2 - $tagGeneral"
val calendar = Calendar.getInstance()

// Setup
val current = calendar.timeInMillis
val difference = TimeUnit.MINUTES.toMillis(30)
val timeStartedTimestamp = current - difference
val timeStartedPreview = timeStartedTimestamp.formatTime()

testUtils.addActivity(name = name1, color = color1, icon = icon1)
testUtils.addActivity(name = name2, color = color2, icon = icon2)
testUtils.addRecordTag(tag, typeName = name1)
testUtils.addRecordTag(tagGeneral)
testUtils.addRunningRecord(
typeName = name1,
timeStarted = timeStartedTimestamp,
tagNames = listOf(tag, tagGeneral),
comment = comment,
)

// Check record
NavUtils.openRecordsScreen()
checkRunningRecord(
name = fullName1,
color = color1,
icon = icon1,
timeStartedPreview = timeStartedPreview,
comment = comment,
)

// Change
longClickOnView(allOf(withText(fullName1), isCompletelyDisplayed()))
clickOnViewWithText(R.string.data_edit_change_activity)
clickOnViewWithText(name2)

tryAction {
checkRunningRecord(
name = fullName2,
color = color2,
icon = icon2,
timeStartedPreview = timeStartedPreview,
comment = comment,
)
}
}

@Suppress("SameParameterValue")
private fun checkRecord(
name: String,
color: Int,
icon: Int,
timeStartedPreview: String,
timeEndedPreview: String,
timeRangePreview: String,
comment: String,
) {
checkViewIsDisplayed(
allOf(
withId(baseR.id.viewRecordItem),
withCardColor(color),
hasDescendant(withText(name)),
hasDescendant(withTag(icon)),
hasDescendant(withText(timeStartedPreview)),
hasDescendant(withText(timeEndedPreview)),
hasDescendant(withText(timeRangePreview)),
hasDescendant(withText(comment)),
isCompletelyDisplayed(),
),
)
}

@Suppress("SameParameterValue")
private fun checkRunningRecord(
name: String,
color: Int,
icon: Int,
timeStartedPreview: String,
comment: String,
) {
checkViewIsDisplayed(
allOf(
withId(baseR.id.viewRunningRecordItem),
withCardColor(color),
hasDescendant(withText(name)),
hasDescendant(withTag(icon)),
hasDescendant(withText(timeStartedPreview)),
hasDescendant(withText(comment)),
isCompletelyDisplayed(),
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.example.util.simpletimetracker.utils.longClickOnView
import com.example.util.simpletimetracker.utils.tryAction
import dagger.hilt.android.testing.HiltAndroidTest
import kotlinx.coroutines.runBlocking
import org.hamcrest.CoreMatchers
import org.hamcrest.Matchers.allOf
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -26,21 +25,26 @@ class RecordQuickActionsTest : BaseUiTest() {
@Test
fun recordQuickActions() {
val name = "Test"
val tag = "Tag"

// Add data
testUtils.addActivity(name)
testUtils.addRecord(name)
testUtils.addRecordTag(tag)

// Check
NavUtils.openRecordsScreen()
longClickOnView(CoreMatchers.allOf(withText(name), isCompletelyDisplayed()))
longClickOnView(allOf(withText(name), isCompletelyDisplayed()))

checkViewIsDisplayed(withText(R.string.shortcut_navigation_statistics))
checkViewIsDisplayed(withText(R.string.archive_dialog_delete))
checkViewIsDisplayed(withText(R.string.change_record_continue))
checkViewIsDisplayed(withText(R.string.change_record_repeat))
checkViewIsDisplayed(withText(R.string.change_record_duplicate))
checkViewIsDisplayed(withText(R.string.data_edit_change_activity))
checkViewIsDisplayed(withText(R.string.data_edit_change_tag))
checkViewDoesNotExist(withText(R.string.change_record_merge))
checkViewDoesNotExist(withText(R.string.notification_record_type_stop))
}

@Test
Expand All @@ -67,15 +71,20 @@ class RecordQuickActionsTest : BaseUiTest() {
checkViewDoesNotExist(withText(R.string.change_record_continue))
checkViewDoesNotExist(withText(R.string.change_record_repeat))
checkViewDoesNotExist(withText(R.string.change_record_duplicate))
checkViewIsDisplayed(withText(R.string.data_edit_change_activity))
checkViewDoesNotExist(withText(R.string.data_edit_change_tag))
checkViewIsDisplayed(withText(R.string.change_record_merge))
checkViewDoesNotExist(withText(R.string.notification_record_type_stop))
}

@Test
fun runningRecordQuickActions() {
val name = "Test"
val tag = "Tag"

// Add data
testUtils.addActivity(name)
testUtils.addRecordTag(tag)
Thread.sleep(1000)
tryAction { clickOnViewWithText(name) }

Expand All @@ -88,6 +97,9 @@ class RecordQuickActionsTest : BaseUiTest() {
checkViewDoesNotExist(withText(R.string.change_record_continue))
checkViewDoesNotExist(withText(R.string.change_record_repeat))
checkViewDoesNotExist(withText(R.string.change_record_duplicate))
checkViewIsDisplayed(withText(R.string.data_edit_change_activity))
checkViewIsDisplayed(withText(R.string.data_edit_change_tag))
checkViewDoesNotExist(withText(R.string.change_record_merge))
checkViewIsDisplayed(withText(R.string.notification_record_type_stop))
}
}
Loading

0 comments on commit c43decd

Please sign in to comment.