11package info.mqtt.android.extsample.activity
22
3- import android.app.UiAutomation
4- import android.os.Build
53import android.view.Gravity
64import androidx.test.core.graphics.writeToTestStorage
75import androidx.test.espresso.Espresso.onView
@@ -18,24 +16,24 @@ import androidx.test.espresso.screenshot.captureToBitmap
1816import androidx.test.ext.junit.rules.activityScenarioRule
1917import androidx.test.ext.junit.runners.AndroidJUnit4
2018import androidx.test.platform.app.InstrumentationRegistry
19+ import androidx.test.uiautomator.UiDevice
2120import com.moka.lib.assertions.WaitingAssertion
2221import info.mqtt.android.extsample.MainActivity
2322import info.mqtt.android.extsample.R
23+ import org.junit.Assert
2424import org.junit.Before
2525import org.junit.Rule
2626import org.junit.Test
2727import org.junit.rules.TestName
2828import org.junit.runner.RunWith
2929import timber.log.Timber
30- import java.io.FileInputStream
31- import java.io.IOException
32- import java.io.InputStream
3330import java.lang.Thread.sleep
34- import java.util.Locale
3531
3632
3733@RunWith(AndroidJUnit4 ::class )
38- class LongRunningSleepMode {
34+ class ExtendedPublishSleepTest {
35+
36+ private lateinit var device: UiDevice
3937
4038 // a handy JUnit rule that stores the method name, so it can be used to generate unique screenshot files per test method
4139 @get:Rule
@@ -46,6 +44,7 @@ class LongRunningSleepMode {
4644
4745 @Before
4846 fun setUp () {
47+ device = UiDevice .getInstance(InstrumentationRegistry .getInstrumentation())
4948 }
5049
5150 @Test
@@ -56,106 +55,68 @@ class LongRunningSleepMode {
5655 onView(withId(R .id.action_add_connection)).perform(click())
5756 onView(withId(R .id.action_save_connection)).perform(click())
5857
58+ sleep(200 )
5959 onView(ViewMatchers .isRoot())
6060 .captureToBitmap()
61- .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} -AddConnect " )
61+ .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} -1AddConnect " )
6262
6363 onView(withId(R .id.disConnectSwitch)).perform(click())
6464 onView(withId(3 )).perform(click())
6565 // onView(withTagValue(`is`("Subscribe" as Any))).perform(click())
6666
6767 onView(withId(R .id.subscribe_button)).perform(click())
6868 onView(withId(R .id.subscription_topic_edit_text)).perform(typeText(TOPIC ))
69+ sleep(200 )
6970 onView(ViewMatchers .isRoot())
7071 .captureToBitmap()
71- .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} -Subscribe " )
72+ .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} -2Subscribe " )
7273 onView(withText(" OK" )).perform(click())
7374
75+ Assert .assertTrue(" Device is in sleep mode" , device.isScreenOn)
76+
7477 // Now send device to sleep
7578 Timber .i(" Send device to sleep" )
76- sendKeyEvent(KeyEvent .SLEEP )
77- Timber .i(" wait ${WAIT_SECONDS } " )
79+ device.sleep()
80+ sleep(1000 )
81+ Assert .assertFalse(" Device is not in sleep mode" , device.isScreenOn)
82+
83+ // onView(ViewMatchers.isRoot())
84+ // .captureToBitmap()
85+ // .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-sleep")
86+ Timber .i(" wait $WAIT_SECONDS seconds" )
7887 sleep(1000 * WAIT_SECONDS )
79- sendKeyEvent(KeyEvent .AWAKE )
80- Timber .i(" Awake device" )
88+ device.wakeUp()
89+ sleep(1000 )
90+
91+ Timber .i(" Wakeup device" )
92+ onView(ViewMatchers .isRoot())
93+ .captureToBitmap()
94+ .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} -3afterWakeUp" )
8195
8296 onView(withId(2 )).perform(click())
8397 onView(withId(R .id.topic)).perform(replaceText(TOPIC ))
84- onView(withId(R .id.message)).perform(replaceText(" msg" ))
98+ onView(withId(R .id.message)).perform(replaceText(" Typed message" ))
99+ sleep(200 )
85100 onView(ViewMatchers .isRoot())
86101 .captureToBitmap()
87- .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} -publish " )
102+ .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} -4publish " )
88103 onView(withId(R .id.publish_button)).perform(click())
89104
90105 onView(withId(1 )).perform(click())
91106
92- WaitingAssertion .checkAssertion( R .id.history_list_view, Matchers .withListSizeBigger( 0 ), 2500 )
107+ sleep( 200 )
93108 onView(ViewMatchers .isRoot())
94109 .captureToBitmap()
95- .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} -End" )
96- }
110+ .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} -5publish" )
97111
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- }
112+ WaitingAssertion .checkAssertion(R .id.history_list_view, Matchers .withListSizeBigger(0 ), 2500 )
113+ onView(ViewMatchers .isRoot())
114+ .captureToBitmap()
115+ .writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} -6End" )
155116 }
156117
157118 companion object {
158119 private const val TOPIC = " AnotherTest"
159- private const val WAIT_SECONDS = 60L
120+ private const val WAIT_SECONDS = 310L
160121 }
161122}
0 commit comments