Skip to content

Commit

Permalink
Use UiDevice to sleep/wakeup
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesa2 committed May 22, 2024
1 parent a03a5fc commit c4a765e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 77 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package info.mqtt.android.extsample.activity

import android.app.UiAutomation
import android.os.Build
import android.view.Gravity
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.espresso.Espresso.onView
Expand All @@ -18,6 +16,7 @@ import androidx.test.espresso.screenshot.captureToBitmap
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import com.moka.lib.assertions.WaitingAssertion
import info.mqtt.android.extsample.MainActivity
import info.mqtt.android.extsample.R
Expand All @@ -27,16 +26,14 @@ import org.junit.Test
import org.junit.rules.TestName
import org.junit.runner.RunWith
import timber.log.Timber
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStream
import java.lang.Thread.sleep
import java.util.Locale


@RunWith(AndroidJUnit4::class)
class LongRunningSleepMode {

private lateinit var device: UiDevice

// a handy JUnit rule that stores the method name, so it can be used to generate unique screenshot files per test method
@get:Rule
var nameRule = TestName()
Expand All @@ -46,6 +43,7 @@ class LongRunningSleepMode {

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

@Test
Expand Down Expand Up @@ -73,11 +71,18 @@ class LongRunningSleepMode {

// Now send device to sleep
Timber.i("Send device to sleep")
sendKeyEvent(KeyEvent.SLEEP)
Timber.i("wait ${WAIT_SECONDS}")
device.sleep()
sleep(1000 * 2)
onView(ViewMatchers.isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-sleep")
Timber.i("wait $WAIT_SECONDS seconds")
sleep(1000 * WAIT_SECONDS)
sendKeyEvent(KeyEvent.AWAKE)
Timber.i("Awake device")
device.wakeUp()
Timber.i("Wakeup device")
onView(ViewMatchers.isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-afterWakeUp")

onView(withId(2)).perform(click())
onView(withId(R.id.topic)).perform(replaceText(TOPIC))
Expand All @@ -95,67 +100,8 @@ class LongRunningSleepMode {
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-End")
}

// Source:
// https://github.com/facebook/screenshot-tests-for-android/blob/main/core/src/main/java/com/facebook/testing/screenshot/internal/Registry.java
private fun sendKeyEvent(event: KeyEvent) {
if (Build.VERSION.SDK_INT < 23) {
return
}
val command = String.format(Locale.ENGLISH, "adb shell input keyevent %s", event.eventKey)

// Timber.d("event=${event.name} cmd='$command'")
// try {
// val proc = Runtime.getRuntime().exec(arrayOf(command))
// var line: String?
//
// val stderr = proc.errorStream
// val esr = InputStreamReader(stderr)
// val ebr = BufferedReader(esr)
// while ((ebr.readLine().also { line = it }) != null) Timber.e("FXN-BOOTCLASSPATH", line!!)
//
// val stdout = proc.inputStream
// val osr = InputStreamReader(stdout)
// val obr = BufferedReader(osr)
// while ((obr.readLine().also { line = it }) != null) Timber.i("FXN-BOOTCLASSPATH", line!!)
//
// val exitVal = proc.waitFor()
// Timber.d("FXN-BOOTCLASSPATH", "getprop exitValue: $exitVal")
// } catch (e: Exception) {
// Timber.e(e)
// }

Timber.d("event=${event.name} cmd='$command'")
val automation: UiAutomation = InstrumentationRegistry.getInstrumentation().uiAutomation
val fileDescriptor = automation.executeShellCommand(command)
val stream: InputStream = FileInputStream(fileDescriptor.fileDescriptor)
try {
val buffer = ByteArray(1024)
Timber.d("start")
while (stream.read(buffer) != -1) {
Timber.d("while")
// Consume stdout to ensure the command completes
Timber.v(buffer.toString())
}
Timber.d("done")
} catch (e: IOException) {
Timber.e(e)
} finally {
try {
stream.close()
} catch (e: IOException) {
Timber.e(e)
}
try {
fileDescriptor.close()
} catch (e: IOException) {
Timber.e(e)
}
Timber.d("finished")
}
}

companion object {
private const val TOPIC = "AnotherTest"
private const val WAIT_SECONDS = 60L
private const val WAIT_SECONDS = 310L
}
}

0 comments on commit c4a765e

Please sign in to comment.