chore: try to setup CI instrumentation tests

This commit is contained in:
Akash Yadav
2023-10-08 15:22:54 +05:30
parent 8e500547b7
commit 8b7b374a68
7 changed files with 380 additions and 4 deletions
@@ -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();
}
}
@@ -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)
@@ -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()
}
}