|
| 1 | +package com.wei.picquest.feature.contactme.contactme |
| 2 | + |
| 3 | +import androidx.activity.ComponentActivity |
| 4 | +import androidx.annotation.StringRes |
| 5 | +import androidx.compose.ui.test.assertIsDisplayed |
| 6 | +import androidx.compose.ui.test.junit4.AndroidComposeTestRule |
| 7 | +import androidx.compose.ui.test.onNodeWithContentDescription |
| 8 | +import androidx.compose.ui.test.performClick |
| 9 | +import androidx.test.ext.junit.rules.ActivityScenarioRule |
| 10 | +import androidx.window.layout.DisplayFeature |
| 11 | +import com.google.common.truth.Truth |
| 12 | +import com.wei.picquest.core.designsystem.theme.PqTheme |
| 13 | +import com.wei.picquest.core.designsystem.ui.PqContentType |
| 14 | +import com.wei.picquest.feature.contactme.R |
| 15 | +import com.wei.picquest.feature.contactme.contactme.utilities.EMAIL |
| 16 | +import com.wei.picquest.feature.contactme.contactme.utilities.LINKEDIN_URL |
| 17 | +import com.wei.picquest.feature.contactme.contactme.utilities.NAME_ENG |
| 18 | +import com.wei.picquest.feature.contactme.contactme.utilities.NAME_TW |
| 19 | +import com.wei.picquest.feature.contactme.contactme.utilities.PHONE |
| 20 | +import com.wei.picquest.feature.contactme.contactme.utilities.POSITION |
| 21 | +import com.wei.picquest.feature.contactme.contactme.utilities.TIME_ZONE |
| 22 | +import kotlin.properties.ReadOnlyProperty |
| 23 | + |
| 24 | +/** |
| 25 | + * Screen Robot for [ContactMeScreenTest]. |
| 26 | + * |
| 27 | + * 遵循此模型,找到測試使用者介面元素、檢查其屬性、和透過測試規則執行動作: |
| 28 | + * composeTestRule{.finder}{.assertion}{.action} |
| 29 | + * |
| 30 | + * Testing cheatsheet: |
| 31 | + * https://developer.android.com/jetpack/compose/testing-cheatsheet |
| 32 | + */ |
| 33 | +internal fun contactMeScreenRobot( |
| 34 | + composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<ComponentActivity>, ComponentActivity>, |
| 35 | + func: ContactMeScreenRobot.() -> Unit, |
| 36 | +) = ContactMeScreenRobot(composeTestRule).apply(func) |
| 37 | + |
| 38 | +internal open class ContactMeScreenRobot( |
| 39 | + private val composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<ComponentActivity>, ComponentActivity>, |
| 40 | +) { |
| 41 | + private fun AndroidComposeTestRule<*, *>.stringResource(@StringRes resId: Int) = |
| 42 | + ReadOnlyProperty<Any?, String> { _, _ -> activity.getString(resId) } |
| 43 | + |
| 44 | + private val profilePictureDescription by composeTestRule.stringResource(R.string.profile_picture) |
| 45 | + private val linkedinString by composeTestRule.stringResource(R.string.linkedin) |
| 46 | + private val emailString by composeTestRule.stringResource(R.string.email) |
| 47 | + private val timezoneString by composeTestRule.stringResource(R.string.timezone) |
| 48 | + private val callDescription by composeTestRule.stringResource(R.string.call) |
| 49 | + |
| 50 | + private val profilePicture by lazy { |
| 51 | + composeTestRule.onNodeWithContentDescription( |
| 52 | + profilePictureDescription.format(testUiState.nameEng), |
| 53 | + useUnmergedTree = true, |
| 54 | + ) |
| 55 | + } |
| 56 | + private val name by lazy { |
| 57 | + composeTestRule.onNodeWithContentDescription( |
| 58 | + testUiState.nameEng, |
| 59 | + useUnmergedTree = true, |
| 60 | + ) |
| 61 | + } |
| 62 | + private val position by lazy { |
| 63 | + composeTestRule.onNodeWithContentDescription( |
| 64 | + testUiState.position, |
| 65 | + useUnmergedTree = true, |
| 66 | + ) |
| 67 | + } |
| 68 | + private val linkedin by lazy { |
| 69 | + composeTestRule.onNodeWithContentDescription( |
| 70 | + linkedinString, |
| 71 | + useUnmergedTree = true, |
| 72 | + ) |
| 73 | + } |
| 74 | + private val linkedinValue by lazy { |
| 75 | + composeTestRule.onNodeWithContentDescription( |
| 76 | + testUiState.linkedinUrl, |
| 77 | + useUnmergedTree = true, |
| 78 | + ) |
| 79 | + } |
| 80 | + private val email by lazy { |
| 81 | + composeTestRule.onNodeWithContentDescription( |
| 82 | + emailString, |
| 83 | + useUnmergedTree = true, |
| 84 | + ) |
| 85 | + } |
| 86 | + private val emailValue by lazy { |
| 87 | + composeTestRule.onNodeWithContentDescription( |
| 88 | + testUiState.email, |
| 89 | + useUnmergedTree = true, |
| 90 | + ) |
| 91 | + } |
| 92 | + private val timeZone by lazy { |
| 93 | + composeTestRule.onNodeWithContentDescription( |
| 94 | + timezoneString, |
| 95 | + useUnmergedTree = true, |
| 96 | + ) |
| 97 | + } |
| 98 | + private val timeZoneValue by lazy { |
| 99 | + composeTestRule.onNodeWithContentDescription( |
| 100 | + testUiState.timeZone, |
| 101 | + useUnmergedTree = true, |
| 102 | + ) |
| 103 | + } |
| 104 | + private val call by lazy { |
| 105 | + composeTestRule.onNodeWithContentDescription( |
| 106 | + callDescription.format(testUiState.nameEng), |
| 107 | + useUnmergedTree = true, |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + private var callClicked: Boolean = false |
| 112 | + |
| 113 | + fun setContactMeScreenContent( |
| 114 | + contentType: PqContentType, |
| 115 | + displayFeature: List<DisplayFeature>, |
| 116 | + ) { |
| 117 | + composeTestRule.setContent { |
| 118 | + PqTheme { |
| 119 | + ContactMeScreen( |
| 120 | + uiStates = testUiState, |
| 121 | + contentType = contentType, |
| 122 | + displayFeatures = displayFeature, |
| 123 | + onPhoneClick = { callClicked = true }, |
| 124 | + ) |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + fun verifyProfilePictureDisplayed() { |
| 130 | + profilePicture.assertExists().assertIsDisplayed() |
| 131 | + } |
| 132 | + |
| 133 | + fun verifyNameDisplayed() { |
| 134 | + name.assertExists().assertIsDisplayed() |
| 135 | + } |
| 136 | + |
| 137 | + fun verifyPositionDisplayed() { |
| 138 | + position.assertExists().assertIsDisplayed() |
| 139 | + } |
| 140 | + |
| 141 | + fun verifyCallDisplayed() { |
| 142 | + call.assertExists().assertIsDisplayed() |
| 143 | + } |
| 144 | + |
| 145 | + fun verifyLinkedinExists() { |
| 146 | + linkedin.assertExists() |
| 147 | + } |
| 148 | + |
| 149 | + fun verifyLinkedinValueExists() { |
| 150 | + linkedinValue.assertExists() |
| 151 | + } |
| 152 | + |
| 153 | + fun verifyEmailExists() { |
| 154 | + email.assertExists() |
| 155 | + } |
| 156 | + |
| 157 | + fun verifyEmailValueExists() { |
| 158 | + emailValue.assertExists() |
| 159 | + } |
| 160 | + |
| 161 | + fun verifyTimeZoneExists() { |
| 162 | + timeZone.assertExists() |
| 163 | + } |
| 164 | + |
| 165 | + fun verifyTimeZoneValueExists() { |
| 166 | + timeZoneValue.assertExists() |
| 167 | + } |
| 168 | + |
| 169 | + infix fun call(func: ContactMeScreenCallRobot.() -> Unit): ContactMeScreenCallRobot { |
| 170 | + call.assertExists().performClick() |
| 171 | + return contactMeScreenCallRobot(composeTestRule) { |
| 172 | + setIsCallClicked(callClicked) |
| 173 | + func() |
| 174 | + } |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +internal fun contactMeScreenCallRobot( |
| 179 | + composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<ComponentActivity>, ComponentActivity>, |
| 180 | + func: ContactMeScreenCallRobot.() -> Unit, |
| 181 | +) = ContactMeScreenCallRobot(composeTestRule).apply(func) |
| 182 | + |
| 183 | +internal open class ContactMeScreenCallRobot( |
| 184 | + private val composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<ComponentActivity>, ComponentActivity>, |
| 185 | +) { |
| 186 | + |
| 187 | + private var isCallClicked: Boolean = false |
| 188 | + |
| 189 | + fun setIsCallClicked(backClicked: Boolean) { |
| 190 | + isCallClicked = backClicked |
| 191 | + } |
| 192 | + |
| 193 | + fun isCall() { |
| 194 | + Truth.assertThat(isCallClicked).isTrue() |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +val testUiState = ContactMeViewState( |
| 199 | + nameTw = NAME_TW, |
| 200 | + nameEng = NAME_ENG, |
| 201 | + position = POSITION, |
| 202 | + phone = PHONE, |
| 203 | + linkedinUrl = LINKEDIN_URL, |
| 204 | + email = EMAIL, |
| 205 | + timeZone = TIME_ZONE, |
| 206 | +) |
0 commit comments