diff --git a/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/KId.kt b/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/KId.kt deleted file mode 100644 index f41483880..000000000 --- a/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/KId.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.kaspersky.components.kautomator.common.resources - -internal object KId : ResourceNameProvider() { - override val rClassName = "R\$id" -} \ No newline at end of file diff --git a/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/KString.kt b/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/KString.kt index 0b1599afe..e49608da3 100644 --- a/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/KString.kt +++ b/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/KString.kt @@ -3,8 +3,7 @@ package com.kaspersky.components.kautomator.common.resources import androidx.annotation.StringRes import androidx.test.platform.app.InstrumentationRegistry -internal object KString : ResourceNameProvider() { - override val rClassName = "R\$string" - - fun getString(@StringRes resId: Int): String = InstrumentationRegistry.getInstrumentation().targetContext.getString(resId) -} \ No newline at end of file +internal object KString { + fun getString(@StringRes resId: Int): String = + InstrumentationRegistry.getInstrumentation().targetContext.getString(resId) +} diff --git a/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/RClassProvider.kt b/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/RClassProvider.kt deleted file mode 100644 index 3464094fa..000000000 --- a/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/RClassProvider.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.kaspersky.components.kautomator.common.resources - -internal object RClassProvider { - private val classNameToClass = mutableMapOf>() - - @Synchronized - fun provideClass(className: String): Class<*> { - var rClass = classNameToClass[className] - if (rClass == null) { - rClass = Class.forName(className) - classNameToClass[className] = rClass - } - - return rClass!! - } -} \ No newline at end of file diff --git a/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/ResourceNameProvider.kt b/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/ResourceNameProvider.kt deleted file mode 100644 index 517e2128c..000000000 --- a/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/ResourceNameProvider.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.kaspersky.components.kautomator.common.resources - -internal abstract class ResourceNameProvider { - protected abstract val rClassName: String - - fun resolveResName(packageName: String, resId: Int): String { - val rClass = RClassProvider.provideClass("$packageName.$rClassName") - return getResourceNameById(rClass, resId) - } - - private fun getResourceNameById(rClass: Class<*>, id: Int): String { - return rClass.fields - .first { it.getInt(it) == id } - .name - } -} \ No newline at end of file diff --git a/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/component/common/builders/UiViewBuilder.kt b/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/component/common/builders/UiViewBuilder.kt index f8d5207b8..47de471e0 100644 --- a/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/component/common/builders/UiViewBuilder.kt +++ b/kautomator/src/main/kotlin/com/kaspersky/components/kautomator/component/common/builders/UiViewBuilder.kt @@ -9,7 +9,6 @@ import androidx.annotation.StringRes import androidx.test.platform.app.InstrumentationRegistry import androidx.test.uiautomator.BySelector import androidx.test.uiautomator.BySelectorHack -import com.kaspersky.components.kautomator.common.resources.KId import com.kaspersky.components.kautomator.common.resources.KString import com.kaspersky.components.kautomator.component.common.KautomatorMarker import java.util.regex.Pattern @@ -50,8 +49,10 @@ class UiViewBuilder { * @param resourceId id to match */ fun withId(@IdRes resourceId: Int) { - val packageName = InstrumentationRegistry.getInstrumentation().targetContext.packageName - val resName = KId.resolveResName(packageName, resourceId) + val fullName = InstrumentationRegistry.getInstrumentation() + .targetContext.resources.getResourceName(resourceId) + val packageName = fullName.substringBefore(":") + val resName = fullName.substringAfterLast("/") return withResourceName(packageName, resName) } diff --git a/samples/kautomator-sample/kautomator-compiletime-r-sanity/build.gradle.kts b/samples/kautomator-sample/kautomator-compiletime-r-sanity/build.gradle.kts new file mode 100644 index 000000000..aa614c612 --- /dev/null +++ b/samples/kautomator-sample/kautomator-compiletime-r-sanity/build.gradle.kts @@ -0,0 +1,21 @@ +plugins { id("convention.android-app") } + +android { + namespace = "com.kaspersky.kaspresso.kautomatorsample.compiletimertest" + defaultConfig { + applicationId = "com.kaspersky.kaspresso.kautomatorsample.compiletimertest" + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + testInstrumentationRunnerArguments["clearPackageData"] = "true" + } + testOptions { execution = "ANDROIDX_TEST_ORCHESTRATOR" } +} + +dependencies { + implementation(libs.appcompat) + implementation(libs.material) + implementation(libs.constraint) + androidTestImplementation(projects.kaspresso) + androidTestImplementation(libs.androidXTestExtJunitKtx) + androidTestImplementation(libs.androidXTestExtJunit) + androidTestUtil(libs.androidXTestOrchestrator) +} diff --git a/samples/kautomator-sample/kautomator-compiletime-r-sanity/gradle.properties b/samples/kautomator-sample/kautomator-compiletime-r-sanity/gradle.properties new file mode 100644 index 000000000..2990bf14f --- /dev/null +++ b/samples/kautomator-sample/kautomator-compiletime-r-sanity/gradle.properties @@ -0,0 +1 @@ +android.enableAppCompileTimeRClass=true diff --git a/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/androidTest/kotlin/com/kaspersky/kaspresso/kautomatorsample/compiletimertest/screen/MainScreen.kt b/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/androidTest/kotlin/com/kaspersky/kaspresso/kautomatorsample/compiletimertest/screen/MainScreen.kt new file mode 100644 index 000000000..09ee1cc0b --- /dev/null +++ b/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/androidTest/kotlin/com/kaspersky/kaspresso/kautomatorsample/compiletimertest/screen/MainScreen.kt @@ -0,0 +1,15 @@ +package com.kaspersky.kaspresso.kautomatorsample.compiletimertest.screen + +import com.kaspersky.components.kautomator.component.edit.UiEditText +import com.kaspersky.components.kautomator.component.text.UiButton +import com.kaspersky.components.kautomator.component.text.UiTextView +import com.kaspersky.components.kautomator.screen.UiScreen +import com.kaspersky.kaspresso.kautomatorsample.compiletimertest.R + +object MainScreen : UiScreen() { + override val packageName = "com.kaspersky.kaspresso.kautomatorsample.compiletimertest" + + val editText = UiEditText { withId(R.id.editText) } + val button = UiButton { withId(R.id.button) } + val textView = UiTextView { withId(R.id.textView) } +} diff --git a/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/androidTest/kotlin/com/kaspersky/kaspresso/kautomatorsample/compiletimertest/test/WithIdSanityTest.kt b/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/androidTest/kotlin/com/kaspersky/kaspresso/kautomatorsample/compiletimertest/test/WithIdSanityTest.kt new file mode 100644 index 000000000..4d05d100d --- /dev/null +++ b/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/androidTest/kotlin/com/kaspersky/kaspresso/kautomatorsample/compiletimertest/test/WithIdSanityTest.kt @@ -0,0 +1,25 @@ +package com.kaspersky.kaspresso.kautomatorsample.compiletimertest.test + +import androidx.test.ext.junit.rules.activityScenarioRule +import com.kaspersky.kaspresso.kautomatorsample.compiletimertest.MainActivity +import com.kaspersky.kaspresso.kautomatorsample.compiletimertest.screen.MainScreen +import com.kaspersky.kaspresso.testcases.api.testcase.TestCase +import org.junit.Rule +import org.junit.Test + +class WithIdSanityTest : TestCase() { + + @get:Rule + val activityRule = activityScenarioRule() + + @Test + fun withIdResolvesResourcesCorrectly() = run { + step("Locate views by integer R.id references") { + MainScreen { + editText { isDisplayed() } + button { isDisplayed() } + textView { isDisplayed() } + } + } + } +} diff --git a/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/main/AndroidManifest.xml b/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/main/AndroidManifest.xml new file mode 100644 index 000000000..4d7bfd399 --- /dev/null +++ b/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/main/AndroidManifest.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + diff --git a/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/main/kotlin/com/kaspersky/kaspresso/kautomatorsample/compiletimertest/MainActivity.kt b/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/main/kotlin/com/kaspersky/kaspresso/kautomatorsample/compiletimertest/MainActivity.kt new file mode 100644 index 000000000..5c59d5e73 --- /dev/null +++ b/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/main/kotlin/com/kaspersky/kaspresso/kautomatorsample/compiletimertest/MainActivity.kt @@ -0,0 +1,11 @@ +package com.kaspersky.kaspresso.kautomatorsample.compiletimertest + +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity + +class MainActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + } +} diff --git a/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/main/res/layout/activity_main.xml b/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/main/res/layout/activity_main.xml new file mode 100644 index 000000000..7f51723d7 --- /dev/null +++ b/samples/kautomator-sample/kautomator-compiletime-r-sanity/src/main/res/layout/activity_main.xml @@ -0,0 +1,33 @@ + + + + + +