|
| 1 | +package info.mqtt.android.extsample.activity |
| 2 | + |
| 3 | +import android.app.UiAutomation |
| 4 | +import android.os.Build |
| 5 | +import android.view.Gravity |
| 6 | +import androidx.test.core.graphics.writeToTestStorage |
| 7 | +import androidx.test.espresso.Espresso.onView |
| 8 | +import androidx.test.espresso.action.ViewActions.click |
| 9 | +import androidx.test.espresso.action.ViewActions.replaceText |
| 10 | +import androidx.test.espresso.action.ViewActions.typeText |
| 11 | +import androidx.test.espresso.assertion.ViewAssertions.matches |
| 12 | +import androidx.test.espresso.contrib.DrawerActions |
| 13 | +import androidx.test.espresso.contrib.DrawerMatchers.isClosed |
| 14 | +import androidx.test.espresso.matcher.ViewMatchers |
| 15 | +import androidx.test.espresso.matcher.ViewMatchers.withId |
| 16 | +import androidx.test.espresso.matcher.ViewMatchers.withText |
| 17 | +import androidx.test.espresso.screenshot.captureToBitmap |
| 18 | +import androidx.test.ext.junit.rules.activityScenarioRule |
| 19 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 20 | +import androidx.test.platform.app.InstrumentationRegistry |
| 21 | +import com.moka.lib.assertions.WaitingAssertion |
| 22 | +import info.hannes.timber.DebugFormatTree |
| 23 | +import info.mqtt.android.extsample.MainActivity |
| 24 | +import info.mqtt.android.extsample.R |
| 25 | +import org.junit.Before |
| 26 | +import org.junit.Rule |
| 27 | +import org.junit.Test |
| 28 | +import org.junit.rules.TestName |
| 29 | +import org.junit.runner.RunWith |
| 30 | +import timber.log.Timber |
| 31 | +import java.io.BufferedReader |
| 32 | +import java.io.FileInputStream |
| 33 | +import java.io.IOException |
| 34 | +import java.io.InputStream |
| 35 | +import java.io.InputStreamReader |
| 36 | +import java.lang.Thread.sleep |
| 37 | +import java.util.Locale |
| 38 | + |
| 39 | + |
| 40 | +@RunWith(AndroidJUnit4::class) |
| 41 | +class LongRunningSleepMode { |
| 42 | + |
| 43 | + // a handy JUnit rule that stores the method name, so it can be used to generate unique screenshot files per test method |
| 44 | + @get:Rule |
| 45 | + var nameRule = TestName() |
| 46 | + |
| 47 | + @get:Rule |
| 48 | + val activityScenarioRule = activityScenarioRule<MainActivity>() |
| 49 | + |
| 50 | + @Before |
| 51 | + fun setUp() { |
| 52 | + Timber.plant(DebugFormatTree()) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun connectWaitAndPublish() { |
| 57 | + onView(withId(R.id.drawer_layout)) |
| 58 | + .check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed. |
| 59 | + .perform(DrawerActions.open()) |
| 60 | + onView(withId(R.id.action_add_connection)).perform(click()) |
| 61 | + onView(withId(R.id.action_save_connection)).perform(click()) |
| 62 | + |
| 63 | + onView(ViewMatchers.isRoot()) |
| 64 | + .captureToBitmap() |
| 65 | + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-AddConnect") |
| 66 | + |
| 67 | + onView(withId(R.id.disConnectSwitch)).perform(click()) |
| 68 | + onView(withId(3)).perform(click()) |
| 69 | + // onView(withTagValue(`is`("Subscribe" as Any))).perform(click()) |
| 70 | + |
| 71 | + onView(withId(R.id.subscribe_button)).perform(click()) |
| 72 | + onView(withId(R.id.subscription_topic_edit_text)).perform(typeText(TOPIC)) |
| 73 | + onView(ViewMatchers.isRoot()) |
| 74 | + .captureToBitmap() |
| 75 | + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-Subscribe") |
| 76 | + onView(withText("OK")).perform(click()) |
| 77 | + |
| 78 | + // Now send device to sleep |
| 79 | + Timber.i("Send device to sleep") |
| 80 | + sendKeyEvent(KeyEvent.SLEEP) |
| 81 | + Timber.i("wait ${WAIT_SECONDS}") |
| 82 | + sleep(1000 * WAIT_SECONDS) |
| 83 | + sendKeyEvent(KeyEvent.AWAKE) |
| 84 | + Timber.i("Awake device") |
| 85 | + |
| 86 | + onView(withId(2)).perform(click()) |
| 87 | + onView(withId(R.id.topic)).perform(replaceText(TOPIC)) |
| 88 | + onView(withId(R.id.message)).perform(replaceText("msg")) |
| 89 | + onView(ViewMatchers.isRoot()) |
| 90 | + .captureToBitmap() |
| 91 | + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-publish") |
| 92 | + onView(withId(R.id.publish_button)).perform(click()) |
| 93 | + |
| 94 | + onView(withId(1)).perform(click()) |
| 95 | + |
| 96 | + WaitingAssertion.checkAssertion(R.id.history_list_view, Matchers.withListSizeBigger(0), 2500) |
| 97 | + onView(ViewMatchers.isRoot()) |
| 98 | + .captureToBitmap() |
| 99 | + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-End") |
| 100 | + } |
| 101 | + |
| 102 | + // Source: |
| 103 | + // https://github.com/facebook/screenshot-tests-for-android/blob/main/core/src/main/java/com/facebook/testing/screenshot/internal/Registry.java |
| 104 | + private fun sendKeyEvent(event: KeyEvent) { |
| 105 | + if (Build.VERSION.SDK_INT < 23) { |
| 106 | + return |
| 107 | + } |
| 108 | + val command = String.format(Locale.ENGLISH, "adb shell input keyevent %s", event.eventKey) |
| 109 | + |
| 110 | +// Timber.d("event=${event.name} cmd='$command'") |
| 111 | +// try { |
| 112 | +// val proc = Runtime.getRuntime().exec(arrayOf(command)) |
| 113 | +// var line: String? |
| 114 | +// |
| 115 | +// val stderr = proc.errorStream |
| 116 | +// val esr = InputStreamReader(stderr) |
| 117 | +// val ebr = BufferedReader(esr) |
| 118 | +// while ((ebr.readLine().also { line = it }) != null) Timber.e("FXN-BOOTCLASSPATH", line!!) |
| 119 | +// |
| 120 | +// val stdout = proc.inputStream |
| 121 | +// val osr = InputStreamReader(stdout) |
| 122 | +// val obr = BufferedReader(osr) |
| 123 | +// while ((obr.readLine().also { line = it }) != null) Timber.i("FXN-BOOTCLASSPATH", line!!) |
| 124 | +// |
| 125 | +// val exitVal = proc.waitFor() |
| 126 | +// Timber.d("FXN-BOOTCLASSPATH", "getprop exitValue: $exitVal") |
| 127 | +// } catch (e: Exception) { |
| 128 | +// Timber.e(e) |
| 129 | +// } |
| 130 | + |
| 131 | + Timber.d("event=${event.name} cmd='$command'") |
| 132 | + val automation: UiAutomation = InstrumentationRegistry.getInstrumentation().uiAutomation |
| 133 | + val fileDescriptor = automation.executeShellCommand(command) |
| 134 | + val stream: InputStream = FileInputStream(fileDescriptor.fileDescriptor) |
| 135 | + try { |
| 136 | + val buffer = ByteArray(1024) |
| 137 | + Timber.d("start") |
| 138 | + while (stream.read(buffer) != -1) { |
| 139 | + Timber.d("while") |
| 140 | + // Consume stdout to ensure the command completes |
| 141 | + Timber.v(buffer.toString()) |
| 142 | + } |
| 143 | + Timber.d("done") |
| 144 | + } catch (e: IOException) { |
| 145 | + Timber.e(e) |
| 146 | + } finally { |
| 147 | + try { |
| 148 | + stream.close() |
| 149 | + } catch (e: IOException) { |
| 150 | + Timber.e(e) |
| 151 | + } |
| 152 | + try { |
| 153 | + fileDescriptor.close() |
| 154 | + } catch (e: IOException) { |
| 155 | + Timber.e(e) |
| 156 | + } |
| 157 | + Timber.d("finished") |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + companion object { |
| 162 | + private const val TOPIC = "AnotherTest" |
| 163 | + private const val WAIT_SECONDS = 60L |
| 164 | + } |
| 165 | +} |
0 commit comments