Skip to content
Permalink
Fetching contributors…
Cannot retrieve contributors at this time
163 lines (136 sloc) 4.85 KB
package com.github.johnnysc.tutor.presentation.core
import android.content.Context
import android.widget.TextView
import androidx.annotation.CallSuper
import androidx.annotation.IdRes
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onData
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.assertion.ViewAssertions.doesNotExist
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.RootMatchers.withDecorView
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiSelector
import com.github.johnnysc.tutor.R
import com.github.johnnysc.tutor.data.AllData
import com.github.johnnysc.tutor.presentation.add.AddNewPairPage
import com.github.johnnysc.tutor.presentation.list.ListPage
import com.github.johnnysc.tutor.presentation.study.StudyPage
import io.realm.Realm
import org.hamcrest.CoreMatchers.*
import org.junit.After
import org.junit.Before
import org.junit.Rule
/**
* @author Asatryan on 18.06.19
*/
abstract class BaseTest {
@get:Rule
var activityTestRule = ActivityTestRule(MainActivity::class.java)
val appName = "Tutor"
val mainPage = MainPage()
val addNewPairPage = AddNewPairPage()
val alertDialogPage = AlertDialogPage()
val listPage = ListPage()
val studyPage = StudyPage()
@CallSuper
@Before
open fun setUp() = clearDataBase()
@After
fun tearDown() = clearDataBase()
protected fun goHomeAndReturnToApp() {
val context = ApplicationProvider.getApplicationContext<Context>()
with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())) {
pressHome()
pressRecentApps()
findObject(UiSelector().text(context.getString(context.applicationInfo.labelRes))).click()
}
}
protected fun checkViewsAreVisible(ids: List<Int>) {
ids.forEach { it.checkViewIsVisible() }
}
protected fun checkViewWithTextDoesNotExist(text: String) {
onView(withText(text)).check(doesNotExist())
}
protected fun checkToast(@IdRes id: Int) {
onView(withText(id)).inRoot(
withDecorView(
not(
`is`(
activityTestRule.activity.window.decorView
)
)
)
).check(matches(isDisplayed()))
}
protected fun checkToast(text: String) {
onView(withText(text)).inRoot(
withDecorView(
not(
`is`(
activityTestRule.activity.window.decorView
)
)
)
).check(matches(isDisplayed()))
}
protected fun swipeFromRightToLeftOnViewWithText(text: String) {
onView(withText(text)).perform(swipeLeft())
}
protected fun checkTitle(text: String) {
onView(allOf(instanceOf(TextView::class.java), withParent(withId(R.id.toolbar))))
.check(matches(withText(text)))
}
private fun clearDataBase() = Realm.getDefaultInstance().use { realm ->
realm.executeTransaction {
it.deleteAll()
it.createObject(AllData::class.java)
}
}
}
fun Int.checkText(text: String) {
onView(withId(this)).check(matches(withText(text)))
}
fun Int.typeText(text: String) {
onView(withId(this)).perform(clearText(), ViewActions.typeText(text), closeSoftKeyboard())
}
fun Int.checkInputHint(hint: String) {
onView(withId(this)).check(matches(withHint(hint)))
}
fun Int.chooseTextInSpinner(text: String) {
onView(withId(this)).perform(ViewActions.click())
onData(allOf(`is`(instanceOf(String::class.java)), `is`(text))).perform(ViewActions.click())
checkSpinnerText(text)
}
fun Int.checkSpinnerText(text: String) {
onView(withId(this)).check(matches(withSpinnerText(containsString(text))))
}
fun Int.clickRecyclerViewItemAtPosition(position: Int) {
onView(RecyclerViewMatcher(this).atPosition(position)).perform(ViewActions.click())
}
fun Int.click() {
onView(withId(this)).perform(ViewActions.click())
}
fun Int.checkRadioButtonIsChecked() {
onView(withId(this)).check(matches(isChecked()))
}
fun Int.checkRadioButtonIsNotChecked() {
onView(withId(this)).check(matches(not(isChecked())))
}
fun Int.checkViewIsNotEnabled() {
onView(withId(this)).check(matches(not(isEnabled())))
}
fun Int.checkViewIsEnabled() {
onView(withId(this)).check(matches(isEnabled()))
}
fun Int.checkViewIsVisible() {
onView(withId(this)).check(matches(isDisplayed()))
}
fun Int.checkViewIsNotVisible() {
onView(withId(this)).check(matches(not(isDisplayed())))
}
You can’t perform that action at this time.