Skip to content

Commit

Permalink
Merge pull request #94 from keepalivedev/UpdateTests
Browse files Browse the repository at this point in the history
add webhook config page to screenshots and move shared test functiona…
  • Loading branch information
keepalivedev authored Sep 9, 2024
2 parents ca8df3f + 63da05d commit af1cb45
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.keepalive.android

import android.Manifest
import android.content.Context
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.RecyclerView
Expand Down Expand Up @@ -74,6 +73,8 @@ class AppScreenshotsInstrumentedTest {
onView(withId(R.id.restPeriodRow)).perform(click())
Thread.sleep(2000)
Screengrab.screenshot("3-ConfigureRestPeriodScreen")

// click the negative button to cancel
onView(withId(android.R.id.button2)).perform(click())
Thread.sleep(2000)

Expand All @@ -90,6 +91,12 @@ class AppScreenshotsInstrumentedTest {
Thread.sleep(2000)
Screengrab.screenshot("5-AddSMSPhoneNumberScreen")
onView(withId(android.R.id.button2)).perform(click())

// open the configure webhook dialog and take a screenshot
onView(withId(R.id.alertWebhookRow)).perform(click())
Thread.sleep(2000)
Screengrab.screenshot("6-ConfigureWebhookScreen")
onView(withId(android.R.id.button2)).perform(click())
}

// we can't do this with the other shared prefs because those are loaded before
Expand Down Expand Up @@ -142,18 +149,6 @@ class AppScreenshotsInstrumentedTest {

companion object {

// the permissions we need for the app to show up normally
private val permissions = listOf(

// to test manually: adb shell pm grant io.keepalive.android android.permission.SEND_SMS
Manifest.permission.SEND_SMS,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.CALL_PHONE,
Manifest.permission.POST_NOTIFICATIONS,
Manifest.permission.ACCESS_BACKGROUND_LOCATION,
Manifest.permission.SYSTEM_ALERT_WINDOW
)

// this runs before the app is launched, though after it is installed?
// can't do any locale-specific stuff here, because the locale is set after this runs
@JvmStatic
Expand All @@ -169,35 +164,14 @@ class AppScreenshotsInstrumentedTest {

Screengrab.setDefaultScreenshotStrategy(UiAutomatorScreenshotStrategy())

// this just sets the time to 12:30 and removes the notification icons...
// CleanStatusBar.enableWithDefaults()

val targetContext = InstrumentationRegistry.getInstrumentation().targetContext

// to test manually
// adb shell appops set io.keepalive.android android:get_usage_stats allow
// adb shell appops set io.keepalive.android android:schedule_exact_alarm allow
// adb shell appops set io.keepalive.android AUTO_REVOKE_PERMISSIONS_IF_UNUSED ignore

// grant usage stats permissions
cmdExec("appops set ${targetContext.packageName} android:get_usage_stats allow")

// grant schedule exact alarm permissions
cmdExec("appops set ${targetContext.packageName} android:schedule_exact_alarm allow")

// disable app hibernation restrictions
cmdExec("appops set ${targetContext.packageName} AUTO_REVOKE_PERMISSIONS_IF_UNUSED ignore")
TestSetupUtil.setupTestEnvironment()

// preload some settings
loadTestSharedPrefs(targetContext)

// grant all the permissions
permissions.forEach { permission ->
InstrumentationRegistry.getInstrumentation().uiAutomation.grantRuntimePermission(
targetContext.packageName,
permission
)
}
// this just sets the time to 12:30 and removes the notification icons...
// CleanStatusBar.enableWithDefaults()

// brief sleep after doing setup before the app is launched
Thread.sleep(1000)
Expand Down
91 changes: 91 additions & 0 deletions app/src/androidTest/java/io/keepalive/android/TestSetupUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.keepalive.android

import android.Manifest
import android.content.Context
import android.os.Build
import androidx.preference.PreferenceManager
import androidx.test.platform.app.InstrumentationRegistry
import com.google.gson.Gson
import io.keepalive.android.AppScreenshotsInstrumentedTest.Companion
import java.io.BufferedReader
import java.io.FileInputStream
import java.io.InputStreamReader

object TestSetupUtil {

private var permissions = listOf(
Manifest.permission.SEND_SMS,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.CALL_PHONE,
Manifest.permission.SYSTEM_ALERT_WINDOW
)

fun setupTestEnvironment() {
println("Setting up test environment...")

val targetContext = InstrumentationRegistry.getInstrumentation().targetContext

cmdExec("appops set ${targetContext.packageName} android:get_usage_stats allow")
cmdExec("appops set ${targetContext.packageName} android:schedule_exact_alarm allow")
cmdExec("appops set ${targetContext.packageName} AUTO_REVOKE_PERMISSIONS_IF_UNUSED ignore")

// grant usage stats permissions
cmdExec("appops set ${targetContext.packageName} android:get_usage_stats allow")

// grant schedule exact alarm permissions
cmdExec("appops set ${targetContext.packageName} android:schedule_exact_alarm allow")

// disable app hibernation restrictions
cmdExec("appops set ${targetContext.packageName} AUTO_REVOKE_PERMISSIONS_IF_UNUSED ignore")

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
permissions += Manifest.permission.ACCESS_BACKGROUND_LOCATION
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
permissions += Manifest.permission.POST_NOTIFICATIONS
}

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
permissions += Manifest.permission.READ_PHONE_STATE
}

permissions.forEach { permission ->
println("Granting permission: $permission")

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// For API 28 and above
InstrumentationRegistry.getInstrumentation().uiAutomation.grantRuntimePermission(
targetContext.packageName,
permission
)
} else {
// For API 27 and below
cmdExec("pm grant ${targetContext.packageName} $permission")
}
}

println("Test environment setup complete.")
}

private fun cmdExec(cmd: String): String {
println("Executing command: $cmd")
val parcelFileDescriptor = InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand(cmd)
val inputStream = FileInputStream(parcelFileDescriptor.fileDescriptor)
val reader = BufferedReader(InputStreamReader(inputStream))
val stringBuilder = StringBuilder()

var line: String? = reader.readLine()
while (line != null) {
stringBuilder.append(line)
stringBuilder.append("\n")
line = reader.readLine()
}

reader.close()
inputStream.close()
parcelFileDescriptor.close()

return stringBuilder.toString()
}
}

0 comments on commit af1cb45

Please sign in to comment.