66 lines
2.5 KiB
Kotlin
66 lines
2.5 KiB
Kotlin
package com.example.retroha
|
|
import androidx.test.espresso.Espresso.onView
|
|
import androidx.test.espresso.action.ViewActions.click
|
|
import androidx.test.espresso.matcher.ViewMatchers.withId
|
|
import androidx.test.espresso.matcher.ViewMatchers.withText
|
|
import androidx.test.ext.junit.rules.ActivityScenarioRule
|
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
|
import org.junit.Rule
|
|
import org.junit.Test
|
|
import org.junit.runner.RunWith
|
|
import java.util.Random
|
|
import androidx.test.espresso.ViewInteraction
|
|
import android.view.View
|
|
import org.hamcrest.Matcher
|
|
import androidx.test.espresso.UiController
|
|
import androidx.test.espresso.ViewAction
|
|
import android.view.ViewGroup
|
|
import android.widget.GridView
|
|
import org.hamcrest.Description
|
|
import org.hamcrest.TypeSafeMatcher
|
|
@RunWith(AndroidJUnit4::class)
|
|
class MonkeyStressTest {
|
|
@get:Rule
|
|
val activityRule = ActivityScenarioRule(MainActivity::class.java)
|
|
private val random = Random()
|
|
@Test
|
|
fun runMonkeyTest() {
|
|
val iterations = 50
|
|
val tabs = listOf("WSZYSTKO", "OŚWIETLENIE", "GNIAZDKA", "MOC", "POGODA")
|
|
for (i in 1..iterations) {
|
|
val actionType = random.nextInt(3)
|
|
try {
|
|
when (actionType) {
|
|
0 -> {
|
|
val tab = tabs[random.nextInt(tabs.size)]
|
|
onView(withText(tab)).perform(click())
|
|
}
|
|
1 -> {
|
|
onView(withId(R.id.gridView)).perform(clickRandomItem())
|
|
}
|
|
2 -> {
|
|
onView(withId(R.id.btnSettings)).perform(click())
|
|
Thread.sleep(500)
|
|
androidx.test.espresso.Espresso.pressBack()
|
|
}
|
|
}
|
|
} catch (e: Exception) {
|
|
}
|
|
Thread.sleep(200)
|
|
}
|
|
}
|
|
private fun clickRandomItem(): ViewAction {
|
|
return object : ViewAction {
|
|
override fun getConstraints(): Matcher<View> = withId(R.id.gridView)
|
|
override fun getDescription(): String = "Kliknięcie losowego elementu w GridView"
|
|
override fun perform(uiController: UiController, view: View) {
|
|
val gridView = view as GridView
|
|
if (gridView.childCount > 0) {
|
|
val randomIndex = random.nextInt(gridView.childCount)
|
|
gridView.getChildAt(randomIndex).performClick()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|