-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(test): Resolve navigation test case issues caused by altering sta…
…rt destination
- Loading branch information
1 parent
f3f2067
commit 07da8c6
Showing
8 changed files
with
84 additions
and
15 deletions.
There are no files selected for viewing
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
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
63 changes: 63 additions & 0 deletions
63
app/src/androidTest/java/com/wei/amazingtalker/ui/robot/HomeEndToEndRobot.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.wei.amazingtalker.ui.robot | ||
|
||
import androidx.annotation.StringRes | ||
import androidx.compose.ui.semantics.Role | ||
import androidx.compose.ui.semantics.SemanticsProperties | ||
import androidx.compose.ui.semantics.getOrNull | ||
import androidx.compose.ui.test.SemanticsMatcher | ||
import androidx.compose.ui.test.assertIsDisplayed | ||
import androidx.compose.ui.test.hasContentDescription | ||
import androidx.compose.ui.test.junit4.AndroidComposeTestRule | ||
import androidx.test.ext.junit.rules.ActivityScenarioRule | ||
import com.wei.amazingtalker.MainActivity | ||
import kotlin.properties.ReadOnlyProperty | ||
import com.wei.amazingtalker.feature.home.R as FeatureHomeR | ||
|
||
/** | ||
* Screen Robot for End To End Test. | ||
* | ||
* 遵循此模型,找到測試使用者介面元素、檢查其屬性、和透過測試規則執行動作: | ||
* composeTestRule{.finder}{.assertion}{.action} | ||
* | ||
* Testing cheatsheet: | ||
* https://developer.android.com/jetpack/compose/testing-cheatsheet | ||
*/ | ||
internal fun homeEndToEndRobot( | ||
composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<MainActivity>, MainActivity>, | ||
func: HomeEndToEndRobot.() -> Unit, | ||
) = HomeEndToEndRobot(composeTestRule).apply(func) | ||
|
||
internal open class HomeEndToEndRobot( | ||
private val composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<MainActivity>, MainActivity>, | ||
) { | ||
private fun AndroidComposeTestRule<*, *>.stringResource(@StringRes resId: Int) = | ||
ReadOnlyProperty<Any?, String> { _, _ -> activity.getString(resId) } | ||
|
||
private fun withRole(role: Role) = SemanticsMatcher("${SemanticsProperties.Role.name} contains '$role'") { | ||
val roleProperty = it.config.getOrNull(SemanticsProperties.Role) ?: false | ||
roleProperty == role | ||
} | ||
|
||
// The strings used for matching in these tests | ||
private val menuDescription by composeTestRule.stringResource(FeatureHomeR.string.menu) | ||
|
||
private val menuButton by lazy { | ||
composeTestRule.onNode( | ||
withRole(Role.Button) | ||
.and(hasContentDescription(menuDescription)), | ||
) | ||
} | ||
|
||
fun verifyMenuButtonDisplayed() { | ||
menuButton.assertExists().assertIsDisplayed() | ||
} | ||
|
||
fun isMenuButtonDisplayed(): Boolean { | ||
return try { | ||
verifyMenuButtonDisplayed() | ||
true | ||
} catch (e: AssertionError) { | ||
false | ||
} | ||
} | ||
} |
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
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,4 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<!-- 首頁 --> | ||
<string name="profile_picture">%s 的大頭貼</string> | ||
<string name="menu">選單</string> | ||
</resources> |
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,4 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<!-- 首頁 --> | ||
<string name="profile_picture">%s\'s profile picture</string> | ||
<string name="menu">Menu</string> | ||
</resources> |