Skip to content

Commit 2b8d98b

Browse files
committed
Use UiDevice to sleep/wakeup
1 parent 65f31a0 commit 2b8d98b

File tree

2 files changed

+16
-77
lines changed

2 files changed

+16
-77
lines changed

extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/KeyEvent.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 16 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package info.mqtt.android.extsample.activity
22

3-
import android.app.UiAutomation
4-
import android.os.Build
53
import android.view.Gravity
64
import androidx.test.core.graphics.writeToTestStorage
75
import androidx.test.espresso.Espresso.onView
@@ -18,6 +16,7 @@ import androidx.test.espresso.screenshot.captureToBitmap
1816
import androidx.test.ext.junit.rules.activityScenarioRule
1917
import androidx.test.ext.junit.runners.AndroidJUnit4
2018
import androidx.test.platform.app.InstrumentationRegistry
19+
import androidx.test.uiautomator.UiDevice
2120
import com.moka.lib.assertions.WaitingAssertion
2221
import info.mqtt.android.extsample.MainActivity
2322
import info.mqtt.android.extsample.R
@@ -27,16 +26,14 @@ import org.junit.Test
2726
import org.junit.rules.TestName
2827
import org.junit.runner.RunWith
2928
import timber.log.Timber
30-
import java.io.FileInputStream
31-
import java.io.IOException
32-
import java.io.InputStream
3329
import java.lang.Thread.sleep
34-
import java.util.Locale
3530

3631

3732
@RunWith(AndroidJUnit4::class)
3833
class LongRunningSleepMode {
3934

35+
private lateinit var device: UiDevice
36+
4037
// a handy JUnit rule that stores the method name, so it can be used to generate unique screenshot files per test method
4138
@get:Rule
4239
var nameRule = TestName()
@@ -46,6 +43,7 @@ class LongRunningSleepMode {
4643

4744
@Before
4845
fun setUp() {
46+
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
4947
}
5048

5149
@Test
@@ -73,11 +71,18 @@ class LongRunningSleepMode {
7371

7472
// Now send device to sleep
7573
Timber.i("Send device to sleep")
76-
sendKeyEvent(KeyEvent.SLEEP)
77-
Timber.i("wait ${WAIT_SECONDS}")
74+
device.sleep()
75+
sleep(1000 * 2)
76+
onView(ViewMatchers.isRoot())
77+
.captureToBitmap()
78+
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-sleep")
79+
Timber.i("wait $WAIT_SECONDS seconds")
7880
sleep(1000 * WAIT_SECONDS)
79-
sendKeyEvent(KeyEvent.AWAKE)
80-
Timber.i("Awake device")
81+
device.wakeUp()
82+
Timber.i("Wakeup device")
83+
onView(ViewMatchers.isRoot())
84+
.captureToBitmap()
85+
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-afterWakeUp")
8186

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

98-
// Source:
99-
// https://github.com/facebook/screenshot-tests-for-android/blob/main/core/src/main/java/com/facebook/testing/screenshot/internal/Registry.java
100-
private fun sendKeyEvent(event: KeyEvent) {
101-
if (Build.VERSION.SDK_INT < 23) {
102-
return
103-
}
104-
val command = String.format(Locale.ENGLISH, "adb shell input keyevent %s", event.eventKey)
105-
106-
// Timber.d("event=${event.name} cmd='$command'")
107-
// try {
108-
// val proc = Runtime.getRuntime().exec(arrayOf(command))
109-
// var line: String?
110-
//
111-
// val stderr = proc.errorStream
112-
// val esr = InputStreamReader(stderr)
113-
// val ebr = BufferedReader(esr)
114-
// while ((ebr.readLine().also { line = it }) != null) Timber.e("FXN-BOOTCLASSPATH", line!!)
115-
//
116-
// val stdout = proc.inputStream
117-
// val osr = InputStreamReader(stdout)
118-
// val obr = BufferedReader(osr)
119-
// while ((obr.readLine().also { line = it }) != null) Timber.i("FXN-BOOTCLASSPATH", line!!)
120-
//
121-
// val exitVal = proc.waitFor()
122-
// Timber.d("FXN-BOOTCLASSPATH", "getprop exitValue: $exitVal")
123-
// } catch (e: Exception) {
124-
// Timber.e(e)
125-
// }
126-
127-
Timber.d("event=${event.name} cmd='$command'")
128-
val automation: UiAutomation = InstrumentationRegistry.getInstrumentation().uiAutomation
129-
val fileDescriptor = automation.executeShellCommand(command)
130-
val stream: InputStream = FileInputStream(fileDescriptor.fileDescriptor)
131-
try {
132-
val buffer = ByteArray(1024)
133-
Timber.d("start")
134-
while (stream.read(buffer) != -1) {
135-
Timber.d("while")
136-
// Consume stdout to ensure the command completes
137-
Timber.v(buffer.toString())
138-
}
139-
Timber.d("done")
140-
} catch (e: IOException) {
141-
Timber.e(e)
142-
} finally {
143-
try {
144-
stream.close()
145-
} catch (e: IOException) {
146-
Timber.e(e)
147-
}
148-
try {
149-
fileDescriptor.close()
150-
} catch (e: IOException) {
151-
Timber.e(e)
152-
}
153-
Timber.d("finished")
154-
}
155-
}
156-
157103
companion object {
158104
private const val TOPIC = "AnotherTest"
159-
private const val WAIT_SECONDS = 60L
105+
private const val WAIT_SECONDS = 310L
160106
}
161107
}

0 commit comments

Comments
 (0)