-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/KeyEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package info.mqtt.android.extsample.activity | ||
|
||
// https://github.com/hannesa2/paho.mqtt.android/issues/604#issue-2236895469 | ||
enum class KeyEvent(eventKey: Int) { | ||
SLEEP(223), | ||
AWAKE(224) | ||
} |
125 changes: 125 additions & 0 deletions
125
...dSample/src/androidTest/java/info/mqtt/android/extsample/activity/LongRunningSleepMode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
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 | ||
import androidx.test.espresso.action.ViewActions.* | ||
import androidx.test.espresso.assertion.ViewAssertions.matches | ||
import androidx.test.espresso.contrib.DrawerActions | ||
import androidx.test.espresso.contrib.DrawerMatchers.isClosed | ||
import androidx.test.espresso.matcher.ViewMatchers | ||
import androidx.test.espresso.matcher.ViewMatchers.withId | ||
import androidx.test.espresso.matcher.ViewMatchers.withText | ||
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 com.moka.lib.assertions.WaitingAssertion | ||
import info.hannes.timber.DebugFormatTree | ||
import info.mqtt.android.extsample.MainActivity | ||
import info.mqtt.android.extsample.R | ||
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 { | ||
|
||
// 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() | ||
|
||
@get:Rule | ||
val activityScenarioRule = activityScenarioRule<MainActivity>() | ||
|
||
@Before | ||
fun setUp() { | ||
Timber.plant(DebugFormatTree()) | ||
} | ||
|
||
@Test | ||
fun connectWaitAndPublish() { | ||
onView(withId(R.id.drawer_layout)) | ||
.check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed. | ||
.perform(DrawerActions.open()) | ||
onView(withId(R.id.action_add_connection)).perform(click()) | ||
onView(withId(R.id.action_save_connection)).perform(click()) | ||
|
||
onView(ViewMatchers.isRoot()) | ||
.captureToBitmap() | ||
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-AddConnect") | ||
|
||
onView(withId(R.id.disConnectSwitch)).perform(click()) | ||
onView(withId(3)).perform(click()) | ||
// onView(withTagValue(`is`("Subscribe" as Any))).perform(click()) | ||
|
||
onView(withId(R.id.subscribe_button)).perform(click()) | ||
onView(withId(R.id.subscription_topic_edit_text)).perform(typeText(TOPIC)) | ||
onView(ViewMatchers.isRoot()) | ||
.captureToBitmap() | ||
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-Subscribe") | ||
onView(withText("OK")).perform(click()) | ||
|
||
// Now wait | ||
sendKeyEvent(KeyEvent.SLEEP) | ||
sleep(1000 * 60) | ||
sendKeyEvent(KeyEvent.AWAKE) | ||
|
||
onView(withId(2)).perform(click()) | ||
onView(withId(R.id.topic)).perform(replaceText(TOPIC)) | ||
onView(withId(R.id.message)).perform(replaceText("msg")) | ||
onView(ViewMatchers.isRoot()) | ||
.captureToBitmap() | ||
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-publish") | ||
onView(withId(R.id.publish_button)).perform(click()) | ||
|
||
onView(withId(1)).perform(click()) | ||
|
||
WaitingAssertion.checkAssertion(R.id.history_list_view, Matchers.withListSizeBigger(0), 2500) | ||
onView(ViewMatchers.isRoot()) | ||
.captureToBitmap() | ||
.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 automation: UiAutomation = InstrumentationRegistry.getInstrumentation().uiAutomation | ||
val command = String.format(Locale.ENGLISH, "adb shell input keyevent %s", event) | ||
val pfd = automation.executeShellCommand(command) | ||
val stream: InputStream = FileInputStream(pfd.fileDescriptor) | ||
try { | ||
val buffer = ByteArray(1024) | ||
while (stream.read(buffer) != -1) { | ||
// Consume stdout to ensure the command completes | ||
} | ||
} catch (ignored: IOException) { | ||
} finally { | ||
try { | ||
stream.close() | ||
} catch (ignored: IOException) { | ||
} | ||
try { | ||
pfd.close() | ||
} catch (ignored: IOException) { | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private const val TOPIC = "AnotherTest" | ||
} | ||
} |