diff --git a/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/LongRunningSleepMode.kt b/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/ExtendedPublishSleepTest.kt similarity index 54% rename from extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/LongRunningSleepMode.kt rename to extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/ExtendedPublishSleepTest.kt index 72d14acc..38b02fd9 100644 --- a/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/LongRunningSleepMode.kt +++ b/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/ExtendedPublishSleepTest.kt @@ -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 @@ -18,24 +16,24 @@ 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 +import org.junit.Assert import org.junit.Before import org.junit.Rule 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 { +class ExtendedPublishSleepTest { + + 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 @@ -46,6 +44,7 @@ class LongRunningSleepMode { @Before fun setUp() { + device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) } @Test @@ -56,9 +55,10 @@ class LongRunningSleepMode { onView(withId(R.id.action_add_connection)).perform(click()) onView(withId(R.id.action_save_connection)).perform(click()) + sleep(200) onView(ViewMatchers.isRoot()) .captureToBitmap() - .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-AddConnect") + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-1AddConnect") onView(withId(R.id.disConnectSwitch)).perform(click()) onView(withId(3)).perform(click()) @@ -66,96 +66,57 @@ class LongRunningSleepMode { onView(withId(R.id.subscribe_button)).perform(click()) onView(withId(R.id.subscription_topic_edit_text)).perform(typeText(TOPIC)) + sleep(200) onView(ViewMatchers.isRoot()) .captureToBitmap() - .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-Subscribe") + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-2Subscribe") onView(withText("OK")).perform(click()) + Assert.assertTrue("Device is in sleep mode", device.isScreenOn) + // Now send device to sleep Timber.i("Send device to sleep") - sendKeyEvent(KeyEvent.SLEEP) - Timber.i("wait ${WAIT_SECONDS}") + device.sleep() + sleep(1000) + Assert.assertFalse("Device is not in sleep mode", device.isScreenOn) + +// 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() + sleep(1000) + + Timber.i("Wakeup device") + onView(ViewMatchers.isRoot()) + .captureToBitmap() + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-3afterWakeUp") onView(withId(2)).perform(click()) onView(withId(R.id.topic)).perform(replaceText(TOPIC)) - onView(withId(R.id.message)).perform(replaceText("msg")) + onView(withId(R.id.message)).perform(replaceText("Typed message")) + sleep(200) onView(ViewMatchers.isRoot()) .captureToBitmap() - .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-publish") + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-4publish") onView(withId(R.id.publish_button)).perform(click()) onView(withId(1)).perform(click()) - WaitingAssertion.checkAssertion(R.id.history_list_view, Matchers.withListSizeBigger(0), 2500) + sleep(200) onView(ViewMatchers.isRoot()) .captureToBitmap() - .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-End") - } + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-5publish") - // 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") - } + WaitingAssertion.checkAssertion(R.id.history_list_view, Matchers.withListSizeBigger(0), 2500) + onView(ViewMatchers.isRoot()) + .captureToBitmap() + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-6End") } companion object { private const val TOPIC = "AnotherTest" - private const val WAIT_SECONDS = 60L + private const val WAIT_SECONDS = 310L } } diff --git a/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/KeyEvent.kt b/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/KeyEvent.kt deleted file mode 100644 index 1403bfbc..00000000 --- a/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/KeyEvent.kt +++ /dev/null @@ -1,7 +0,0 @@ -package info.mqtt.android.extsample.activity - -// https://github.com/hannesa2/paho.mqtt.android/issues/604#issue-2236895469 -enum class KeyEvent(val eventKey: Int) { - SLEEP(223), - AWAKE(224) -} diff --git a/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-1AddConnect.png b/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-1AddConnect.png new file mode 100644 index 00000000..f65e68f1 Binary files /dev/null and b/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-1AddConnect.png differ diff --git a/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-2Subscribe.png b/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-2Subscribe.png new file mode 100644 index 00000000..cd32e620 Binary files /dev/null and b/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-2Subscribe.png differ diff --git a/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-3afterWakeUp.png b/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-3afterWakeUp.png new file mode 100644 index 00000000..688907a4 Binary files /dev/null and b/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-3afterWakeUp.png differ diff --git a/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-4publish.png b/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-4publish.png new file mode 100644 index 00000000..1c1b8654 Binary files /dev/null and b/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-4publish.png differ diff --git a/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-5publish.png b/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-5publish.png new file mode 100644 index 00000000..94ebc560 Binary files /dev/null and b/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-5publish.png differ diff --git a/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-6End.png b/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-6End.png new file mode 100644 index 00000000..94ebc560 Binary files /dev/null and b/screenshotsToCompare14/ExtendedPublishSleepTest_connectWaitAndPublish-6End.png differ