Skip to content
This repository was archived by the owner on Sep 3, 2023. It is now read-only.

Commit

Permalink
Basic UI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
afollestad committed Sep 10, 2018
1 parent 52c6f8d commit 88c9fcc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,73 @@ import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import android.support.test.uiautomator.UiDevice
import com.afollestad.assentsample.utils.TEXT_ALLOW
import com.afollestad.assentsample.utils.allowCurrentPermission
import com.afollestad.assentsample.utils.assertViewWithTextIsVisible
import com.afollestad.assentsample.utils.denyCurrentPermission
import org.junit.Before
import org.junit.FixMethodOrder
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.MethodSorters.JVM
import org.junit.runners.MethodSorters.NAME_ASCENDING

@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 18)
@FixMethodOrder(NAME_ASCENDING)
class AssentTest {

@Rule
@JvmField
var rule = ActivityTestRule(MainActivity::class.java)
var activityRule = ActivityTestRule(MainActivity::class.java)

private lateinit var device: UiDevice

@Before fun setup() {
device = UiDevice.getInstance(getInstrumentation())
}

@Test fun test_basic() {
@Test fun a_test_grant_none() {
onView(withText(R.string.none_granted))
.check(matches(isDisplayed()))
onView(withId(R.id.requestPermissionButton))
.check(matches(isDisplayed()))
.perform(click())

device.assertViewWithTextIsVisible(TEXT_ALLOW, rule.activity)
device.assertViewWithTextIsVisible(TEXT_ALLOW, activityRule.activity)
device.denyCurrentPermission()
device.denyCurrentPermission()

onView(withText(R.string.none_granted))
.check(matches(isDisplayed()))
}

@Test fun b_test_grant_some() {
onView(withText(R.string.none_granted))
.check(matches(isDisplayed()))
onView(withId(R.id.requestPermissionButton))
.check(matches(isDisplayed()))
.perform(click())

device.assertViewWithTextIsVisible(TEXT_ALLOW, activityRule.activity)
device.allowCurrentPermission()
device.denyCurrentPermission()

onView(withText(R.string.some_granted))
.check(matches(isDisplayed()))
}

@Test fun c_test_grant_all() {
onView(withText(R.string.none_granted))
.check(matches(isDisplayed()))
onView(withId(R.id.requestPermissionButton))
.check(matches(isDisplayed()))
.perform(click())

device.assertViewWithTextIsVisible(TEXT_ALLOW, activityRule.activity)
device.allowCurrentPermission()

onView(withText(R.string.all_granted))
.check(matches(isDisplayed()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ internal const val TEXT_ALLOW = "ALLOW"
internal const val TEXT_DENY = "DENY"
internal const val TEXT_NEVER_ASK_AGAIN = "Never ask again"

private fun sleep(time: Long) {
try {
Thread.sleep(time)
} catch (_: Throwable) {
}
}

internal fun UiDevice.assertViewWithTextIsVisible(
text: String,
context: Activity,
Expand All @@ -34,7 +27,7 @@ internal fun UiDevice.assertViewWithTextIsVisible(
}
}

fun UiDevice.allowCurrentPermission(device: UiDevice) {
fun UiDevice.allowCurrentPermission() {
val allowButton = findObject(UiSelector().text(TEXT_ALLOW))
allowButton.click()
}
Expand Down

0 comments on commit 88c9fcc

Please sign in to comment.