social-aggregator/app/src/androidTest/java/pl/edu/amu/wmi/socialaggregator/TestUtils.kt

109 lines
3.9 KiB
Kotlin

package pl.edu.amu.wmi.socialaggregator
import android.app.Activity
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.NoMatchingViewException
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.ViewAssertion
import androidx.test.espresso.intent.rule.IntentsTestRule
import androidx.test.espresso.matcher.BoundedMatcher
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry
import androidx.test.runner.lifecycle.Stage
import org.hamcrest.CoreMatchers
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.core.AllOf
import org.junit.runner.RunWith
import java.io.File
object TestUtils {
fun cleanLocalStorage(activity: IntentsTestRule<out Activity>) {
deleteRecursive(activity.activity.filesDir)
}
private fun deleteRecursive(fileOrDirectory: File) {
if (fileOrDirectory.isDirectory())
for (child in fileOrDirectory.listFiles())
deleteRecursive(child)
fileOrDirectory.delete()
}
fun actionOnChild(action: ViewAction, childId: Int): ViewAction? {
return object : ViewAction {
override fun getDescription(): String {
return "Action on child"
}
override fun getConstraints(): Matcher<View> {
return AllOf.allOf(
ViewMatchers.isDisplayed(),
ViewMatchers.isAssignableFrom(View::class.java)
)
}
override fun perform(uiController: UiController?, view: View?) {
view?.let {
val child = it.findViewById<View>(childId)
action.perform(uiController, child)
}
}
}
}
fun atPosition(position: Int, itemMatcher: Matcher<View>): Matcher<View> {
checkNotNull(itemMatcher)
return object : BoundedMatcher<View, RecyclerView>(RecyclerView::class.java) {
override fun describeTo(description: Description) {
description.appendText("has item at position $position: ")
itemMatcher.describeTo(description)
}
override fun matchesSafely(view: RecyclerView): Boolean {
val viewHolder = view.findViewHolderForAdapterPosition(position)
?: // has no item on such position
return false
return itemMatcher.matches(viewHolder.itemView)
}
}
}
fun atPosition(position: Int, id: Int, itemMatcher: Matcher<View>): Matcher<View> {
checkNotNull(itemMatcher)
return object : BoundedMatcher<View, RecyclerView>(RecyclerView::class.java) {
override fun describeTo(description: Description) {
description.appendText("has item at position $position: ")
itemMatcher.describeTo(description)
}
override fun matchesSafely(view: RecyclerView): Boolean {
val viewHolder = view.findViewHolderForAdapterPosition(position)
?: // has no item on such position
return false
val view = viewHolder.itemView.findViewById<View>(id)
return itemMatcher.matches(view)
}
}
}
fun getCurrentActivity(): Activity? {
var currentActivity: Activity? = null
getInstrumentation().runOnMainSync {
run {
currentActivity =
ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(
Stage.RESUMED
).elementAtOrNull(0)
}
}
return currentActivity
}
}