Cleanup test code

This commit is contained in:
topjohnwu 2024-12-25 04:50:08 -08:00 committed by John Wu
parent 9e2b59060d
commit 231a5d1853
5 changed files with 46 additions and 58 deletions

View File

@ -1,16 +1,13 @@
package com.topjohnwu.magisk.test
import android.app.UiAutomation
import android.os.ParcelFileDescriptor.AutoCloseInputStream
import androidx.annotation.Keep
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.Until
import org.junit.After
import org.junit.Assert.assertNotNull
import org.junit.Assume.assumeTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.util.concurrent.TimeUnit
@ -18,7 +15,7 @@ import java.util.regex.Pattern
@Keep
@RunWith(AndroidJUnit4::class)
class AdditionalTest {
class AdditionalTest : BaseTest {
companion object {
private const val SHELL_PKG = "com.android.shell"
@ -26,16 +23,6 @@ class AdditionalTest {
private const val LSPOSED_PKG = "org.lsposed.manager"
}
private lateinit var uiAutomation: UiAutomation
private lateinit var device: UiDevice
@Before
fun setup() {
val inst = InstrumentationRegistry.getInstrumentation()
uiAutomation = inst.uiAutomation
device = UiDevice.getInstance(inst)
}
@After
fun teardown() {
device.pressHome()
@ -47,7 +34,8 @@ class AdditionalTest {
uiAutomation.executeShellCommand(
"am start -c $LSPOSED_CATEGORY $SHELL_PKG/.BugreportWarningActivity"
)
).let { pfd -> AutoCloseInputStream(pfd).use { it.readBytes() } }
val pattern = Pattern.compile("$LSPOSED_PKG:id/.*")
assertNotNull(
"LSPosed manager launch failed",

View File

@ -0,0 +1,26 @@
package com.topjohnwu.magisk.test
import android.app.Instrumentation
import android.app.UiAutomation
import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import com.topjohnwu.magisk.core.utils.RootUtils
import com.topjohnwu.superuser.Shell
import org.junit.Assert.assertTrue
interface BaseTest {
val instrumentation: Instrumentation
get() = InstrumentationRegistry.getInstrumentation()
val context: Context get() = instrumentation.targetContext
val uiAutomation: UiAutomation get() = instrumentation.uiAutomation
val device: UiDevice get() = UiDevice.getInstance(instrumentation)
companion object {
fun prerequisite() {
assertTrue("Should have root access", Shell.getShell().isRoot)
// Make sure the root service is running
RootUtils.Connection.await()
}
}
}

View File

@ -1,12 +1,10 @@
package com.topjohnwu.magisk.test
import android.app.Notification
import android.content.Context
import android.os.Build
import androidx.annotation.Keep
import androidx.core.net.toUri
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.topjohnwu.magisk.core.BuildConfig.APP_PACKAGE_NAME
import com.topjohnwu.magisk.core.di.ServiceLocator
import com.topjohnwu.magisk.core.download.DownloadNotifier
@ -15,13 +13,10 @@ import com.topjohnwu.magisk.core.ktx.cachedFile
import com.topjohnwu.magisk.core.tasks.AppMigration
import com.topjohnwu.magisk.core.tasks.FlashZip
import com.topjohnwu.magisk.core.tasks.MagiskInstaller
import com.topjohnwu.magisk.core.utils.RootUtils
import com.topjohnwu.superuser.CallbackList
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertTrue
import org.junit.Assume.assumeTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
@ -29,16 +24,12 @@ import timber.log.Timber
@Keep
@RunWith(AndroidJUnit4::class)
class Environment {
class Environment : BaseTest {
companion object {
@BeforeClass
@JvmStatic
fun before() {
assertTrue("Should have root access", Shell.getShell().isRoot)
// Make sure the root service is running
RootUtils.Connection.await()
}
fun before() = BaseTest.prerequisite()
fun lsposed(): Boolean {
return Build.VERSION.SDK_INT >= 27 && Build.VERSION.SDK_INT <= 34
@ -54,13 +45,6 @@ class Environment {
}
}
private lateinit var mContext: Context
@Before
fun setup() {
mContext = InstrumentationRegistry.getInstrumentation().targetContext
}
@Test
fun setupMagisk() {
runBlocking {
@ -76,11 +60,11 @@ class Environment {
assumeTrue(lsposed())
val notify = object : DownloadNotifier {
override val context = mContext
override val context = this@Environment.context
override fun notifyUpdate(id: Int, editor: (Notification.Builder) -> Unit) {}
}
val processor = DownloadProcessor(notify)
val zip = mContext.cachedFile("lsposed.zip")
val zip = context.cachedFile("lsposed.zip")
runBlocking {
ServiceLocator.networkService.fetchFile(LSPOSED_URL).byteStream().use {
processor.handleModule(it, zip.toUri())
@ -98,7 +82,7 @@ class Environment {
assertTrue(
"App hiding failed",
AppMigration.patchAndHide(
context = mContext,
context = context,
label = "Settings",
pkg = "repackaged.$APP_PACKAGE_NAME"
)
@ -111,7 +95,7 @@ class Environment {
runBlocking {
assertTrue(
"App restoration failed",
AppMigration.restoreApp(mContext)
AppMigration.restoreApp(context)
)
}
}

View File

@ -1,12 +1,11 @@
package com.topjohnwu.magisk.test
import android.app.Instrumentation
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.ParcelFileDescriptor.AutoCloseInputStream
import androidx.annotation.Keep
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.di.ServiceLocator
@ -15,29 +14,19 @@ import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import java.io.FileInputStream
import java.util.concurrent.TimeUnit
@Keep
@RunWith(AndroidJUnit4::class)
class MagiskAppTest {
class MagiskAppTest : BaseTest {
companion object {
@BeforeClass
@JvmStatic
fun before() = Environment.before()
}
private lateinit var inst: Instrumentation
private val uiAutomation get() = inst.uiAutomation
@Before
fun setup() {
inst = InstrumentationRegistry.getInstrumentation()
fun before() = BaseTest.prerequisite()
}
@Test
@ -64,7 +53,7 @@ class MagiskAppTest {
val filter = IntentFilter(Intent.ACTION_VIEW)
filter.addCategory(Intent.CATEGORY_DEFAULT)
val monitor = inst.addMonitor(filter, null, false)
val monitor = instrumentation.addMonitor(filter, null, false)
// Try to call su from ADB shell
val cmd = if (Build.VERSION.SDK_INT < 24) {
@ -80,10 +69,10 @@ class MagiskAppTest {
assertNotNull("SuRequestActivity is not launched", suRequest)
// Check that the request went through
FileInputStream(pfd.fileDescriptor).use {
AutoCloseInputStream(pfd).reader().use {
assertTrue(
"Cannot grant root permission from shell",
it.reader().readText().contains("uid=0")
it.readText().contains("uid=0")
)
}

View File

@ -59,9 +59,10 @@ run_setup() {
}
run_tests() {
local self='com.topjohnwu.magisk.test/com.topjohnwu.magisk.test.TestRunner'
local app='com.topjohnwu.magisk.test/com.topjohnwu.magisk.test.AppTestRunner'
local stub='repackaged.com.topjohnwu.magisk.test/com.topjohnwu.magisk.test.AppTestRunner'
local pkg='com.topjohnwu.magisk.test'
local self="$pkg/$pkg.TestRunner"
local app="$pkg/$pkg.AppTestRunner"
local stub="repackaged.$pkg/$pkg.AppTestRunner"
# Run app tests
am_instrument '.MagiskAppTest,.AdditionalTest' $app