mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
chore: try to setup CI instrumentation tests
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* This file is part of AndroidIDE.
|
||||
*
|
||||
* AndroidIDE is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* AndroidIDE is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package androidx.test.uiautomator;
|
||||
|
||||
import android.app.UiAutomation;
|
||||
|
||||
/**
|
||||
* @author Akash Yadav
|
||||
*/
|
||||
public class UiDeviceAccessor {
|
||||
|
||||
/**
|
||||
* Get the {@link UiAutomation} instance from the given {@link UiDevice}.
|
||||
*/
|
||||
public static UiAutomation getUiAutomation(UiDevice device) {
|
||||
return device.getUiAutomation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link InteractionController} instance from the given {@link UiDevice}.
|
||||
*/
|
||||
public static InteractionController getInteractionController(UiDevice device) {
|
||||
return device.getInteractionController();
|
||||
}
|
||||
}
|
||||
+9
-1
@@ -18,6 +18,7 @@
|
||||
package com.itsaky.androidide.testing.android
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.UiAutomation
|
||||
import android.content.Intent
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
@@ -25,6 +26,7 @@ import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry
|
||||
import androidx.test.runner.lifecycle.Stage
|
||||
import androidx.test.uiautomator.By
|
||||
import androidx.test.uiautomator.UiDevice
|
||||
import androidx.test.uiautomator.UiDeviceAccessor
|
||||
import androidx.test.uiautomator.Until
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import com.itsaky.androidide.app.BaseApplication
|
||||
@@ -90,4 +92,10 @@ fun <T : Activity> getActivityInStage(stage: Stage): T? {
|
||||
}
|
||||
|
||||
return activity
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see UiDeviceAccessor.getUiAutomation
|
||||
*/
|
||||
val UiDevice.uiAutomation: UiAutomation
|
||||
get() = UiDeviceAccessor.getUiAutomation(this)
|
||||
+20
-1
@@ -17,12 +17,18 @@
|
||||
|
||||
package com.itsaky.androidide.testing.android
|
||||
|
||||
import android.view.KeyCharacterMap
|
||||
import android.view.KeyEvent
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.test.uiautomator.By
|
||||
import androidx.test.uiautomator.UiDevice
|
||||
import androidx.test.uiautomator.UiObject2
|
||||
import com.itsaky.androidide.buildinfo.BuildInfo
|
||||
|
||||
private val keyCharMap by lazy {
|
||||
KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD)
|
||||
}
|
||||
|
||||
/**
|
||||
* @see UiDevice.waitForWindowUpdate
|
||||
*/
|
||||
@@ -51,4 +57,17 @@ fun UiDevice.findObjectWithText(text: String): UiObject2? =
|
||||
* @see UiDevice.findObjectWithText
|
||||
*/
|
||||
fun UiDevice.findObjectWithText(@StringRes text: Int): UiObject2? =
|
||||
findObject(By.text(stringRes(text)))
|
||||
findObject(By.text(stringRes(text)))
|
||||
|
||||
/**
|
||||
* Sends key events to this [UiDevice] to type the given [text].
|
||||
*/
|
||||
fun UiDevice.sendKeyEvents(text: String) {
|
||||
waitForIdle()
|
||||
val keyEvents = keyCharMap.getEvents(text.toCharArray())
|
||||
for (keyEvent in keyEvents) {
|
||||
uiAutomation.injectInputEvent(keyEvent, true)
|
||||
// pressKeyCode(keyEvent.keyCode, keyEvent.metaState)
|
||||
waitForIdle()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user