forked from KasperskyLab/Kaspresso
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISSUE-86: use native resources in kAutomator (KasperskyLab#516)
ISSUE-86: use native resources in kAutomator
- Loading branch information
Showing
10 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/KId.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,5 @@ | ||
package com.kaspersky.components.kautomator.common.resources | ||
|
||
internal object KId : ResourceNameProvider() { | ||
override val rClassName = "R\$id" | ||
} |
10 changes: 10 additions & 0 deletions
10
kautomator/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/KString.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,10 @@ | ||
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) | ||
} |
16 changes: 16 additions & 0 deletions
16
...or/src/main/kotlin/com/kaspersky/components/kautomator/common/resources/RClassProvider.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,16 @@ | ||
package com.kaspersky.components.kautomator.common.resources | ||
|
||
internal object RClassProvider { | ||
private val classNameToClass = mutableMapOf<String, Class<*>>() | ||
|
||
@Synchronized | ||
fun provideClass(className: String): Class<*> { | ||
var rClass = classNameToClass[className] | ||
if (rClass == null) { | ||
rClass = Class.forName(className) | ||
classNameToClass[className] = rClass | ||
} | ||
|
||
return rClass!! | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
.../main/kotlin/com/kaspersky/components/kautomator/common/resources/ResourceNameProvider.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,16 @@ | ||
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 | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
.../src/androidTest/java/com/kaspersky/kaspresso/kautomatorsample/screen/MainNativeScreen.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,21 @@ | ||
package com.kaspersky.kaspresso.kautomatorsample.screen | ||
|
||
import com.kaspersky.components.kautomator.component.check.UiCheckBox | ||
import com.kaspersky.components.kautomator.component.common.views.UiView | ||
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.R | ||
|
||
object MainNativeScreen : UiScreen<MainNativeScreen>() { | ||
|
||
override val packageName: String = "com.kaspersky.kaspresso.kautomatorsample" | ||
|
||
val header = UiTextView { withText(R.string.main_activity_text) } | ||
val subHeader = UiTextView { textStartsWith(R.string.main_activity_text_start) } | ||
val image = UiView { withContentDescription(R.string.main_activity_image_description) } | ||
val simpleEditText = UiEditText { withId(R.id.editText) } | ||
val simpleButton = UiButton { withId(R.id.button) } | ||
val checkBox = UiCheckBox { withId(R.id.checkBox) } | ||
} |
63 changes: 63 additions & 0 deletions
63
...le/src/androidTest/java/com/kaspersky/kaspresso/kautomatorsample/test/NativeSimpleTest.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,63 @@ | ||
package com.kaspersky.kaspresso.kautomatorsample.test | ||
|
||
import androidx.test.ext.junit.rules.activityScenarioRule | ||
import com.kaspersky.kaspresso.kautomatorsample.MainActivity | ||
import com.kaspersky.kaspresso.kautomatorsample.screen.MainNativeScreen | ||
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
/** | ||
* The sample of how to use native resources in combination with kAutomator | ||
*/ | ||
class NativeSimpleTest : TestCase() { | ||
|
||
@get:Rule | ||
val activityRule = activityScenarioRule<MainActivity>() | ||
|
||
@Test | ||
fun nativeTest() = run { | ||
step("Input text in EditText and check it") { | ||
MainNativeScreen { | ||
simpleEditText { | ||
replaceText("Kaspresso") | ||
hasText("Kaspresso") | ||
} | ||
} | ||
} | ||
step("Type more text and check it") { | ||
MainNativeScreen { | ||
simpleEditText { | ||
typeText(" is super useful") | ||
hasText("Kaspresso is super useful") | ||
} | ||
} | ||
} | ||
step("Click button") { | ||
MainNativeScreen { | ||
simpleButton { | ||
click() | ||
} | ||
} | ||
} | ||
step("Click checkbox and check it") { | ||
MainNativeScreen { | ||
checkBox { | ||
setChecked(true) | ||
isChecked() | ||
} | ||
} | ||
} | ||
step("Check headers") { | ||
MainNativeScreen { | ||
header { isDisplayed() } | ||
subHeader { isDisplayed() } | ||
} | ||
} | ||
step("Check image") { | ||
MainNativeScreen { | ||
image { isDisplayed() } | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
.../kautomator-sample/src/main/java/com/kaspersky/kaspresso/kautomatorsample/MainActivity.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 |
---|---|---|
@@ -1,12 +1,17 @@ | ||
package com.kaspersky.kaspresso.kautomatorsample | ||
|
||
import android.annotation.SuppressLint | ||
import android.os.Bundle | ||
import android.widget.TextView | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
@SuppressLint("SetTextI18n") | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
findViewById<TextView>(R.id.subHeader).text = getString(R.string.main_activity_text_start) + getString(R.string.main_activity_text_end) | ||
} | ||
} |
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
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