-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2430bd0
commit e9386ac
Showing
6 changed files
with
277 additions
and
21 deletions.
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
...p/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/RedeemVoucherDialogTest.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,149 @@ | ||
package net.mullvad.mullvadvpn.compose.screen | ||
|
||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.compose.ui.test.onNodeWithText | ||
import androidx.compose.ui.test.performClick | ||
import androidx.compose.ui.test.performTextInput | ||
import io.mockk.mockk | ||
import io.mockk.mockkObject | ||
import io.mockk.verify | ||
import net.mullvad.mullvadvpn.compose.setContentWithTheme | ||
import net.mullvad.mullvadvpn.compose.state.VoucherDialogState | ||
import net.mullvad.mullvadvpn.compose.state.VoucherDialogUiState | ||
import net.mullvad.mullvadvpn.compose.test.VOUCHER_INPUT_TEST_TAG | ||
import net.mullvad.mullvadvpn.util.VoucherRegexHelper | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class RedeemVoucherDialogTest { | ||
@get:Rule val composeTestRule = createComposeRule() | ||
|
||
@Before | ||
fun setup() { | ||
mockkObject(VoucherRegexHelper) | ||
} | ||
|
||
@Test | ||
fun testDismissDialog() { | ||
// Arrange | ||
val mockedClickHandler: (Boolean) -> Unit = mockk(relaxed = true) | ||
composeTestRule.setContentWithTheme { | ||
RedeemVoucherDialogScreen( | ||
uiState = VoucherDialogUiState.INITIAL, | ||
onVoucherInputChange = {}, | ||
onRedeem = {}, | ||
onDismiss = mockedClickHandler | ||
) | ||
} | ||
|
||
// Act | ||
composeTestRule.onNodeWithText(CANCEL_BUTTON_TEXT).performClick() | ||
|
||
// Assert | ||
verify { mockedClickHandler.invoke(false) } | ||
} | ||
|
||
@Test | ||
fun testDismissDialogAfterSuccessfulRedeem() { | ||
// Arrange | ||
val mockedClickHandler: (Boolean) -> Unit = mockk(relaxed = true) | ||
composeTestRule.setContentWithTheme { | ||
RedeemVoucherDialogScreen( | ||
uiState = | ||
VoucherDialogUiState(voucherViewModelState = VoucherDialogState.Success(0)), | ||
onVoucherInputChange = {}, | ||
onRedeem = {}, | ||
onDismiss = mockedClickHandler | ||
) | ||
} | ||
|
||
// Act | ||
composeTestRule.onNodeWithText(GOT_IT_BUTTON_TEXT).performClick() | ||
|
||
// Assert | ||
verify { mockedClickHandler.invoke(true) } | ||
} | ||
|
||
@Test | ||
fun testInsertVoucher() { | ||
// Arrange | ||
val mockedClickHandler: (String) -> Unit = mockk(relaxed = true) | ||
composeTestRule.setContentWithTheme { | ||
RedeemVoucherDialogScreen( | ||
uiState = VoucherDialogUiState(), | ||
onVoucherInputChange = mockedClickHandler, | ||
onRedeem = {}, | ||
onDismiss = {} | ||
) | ||
} | ||
|
||
// Act | ||
composeTestRule.onNodeWithTag(VOUCHER_INPUT_TEST_TAG).performTextInput(DUMMY_VOUCHER) | ||
|
||
// Assert | ||
verify { mockedClickHandler.invoke(DUMMY_VOUCHER) } | ||
} | ||
|
||
@Test | ||
fun testVerifyingState() { | ||
// Arrange | ||
composeTestRule.setContentWithTheme { | ||
RedeemVoucherDialogScreen( | ||
uiState = | ||
VoucherDialogUiState(voucherViewModelState = VoucherDialogState.Verifying), | ||
onVoucherInputChange = {}, | ||
onRedeem = {}, | ||
onDismiss = {} | ||
) | ||
} | ||
|
||
// Assert | ||
composeTestRule.onNodeWithText("Verifying voucher…").assertExists() | ||
} | ||
|
||
@Test | ||
fun testSuccessState() { | ||
// Arrange | ||
composeTestRule.setContentWithTheme { | ||
RedeemVoucherDialogScreen( | ||
uiState = | ||
VoucherDialogUiState(voucherViewModelState = VoucherDialogState.Success(0)), | ||
onVoucherInputChange = {}, | ||
onRedeem = {}, | ||
onDismiss = {} | ||
) | ||
} | ||
|
||
// Assert | ||
composeTestRule.onNodeWithText("Voucher was successfully redeemed.").assertExists() | ||
} | ||
|
||
@Test | ||
fun testErrorState() { | ||
// Arrange | ||
composeTestRule.setContentWithTheme { | ||
RedeemVoucherDialogScreen( | ||
uiState = | ||
VoucherDialogUiState( | ||
voucherViewModelState = VoucherDialogState.Error(ERROR_MESSAGE) | ||
), | ||
onVoucherInputChange = {}, | ||
onRedeem = {}, | ||
onDismiss = {} | ||
) | ||
} | ||
|
||
// Assert | ||
composeTestRule.onNodeWithText(ERROR_MESSAGE).assertExists() | ||
} | ||
|
||
companion object { | ||
private const val REDEEM_BUTTON_TEXT = "Redeem" | ||
private const val CANCEL_BUTTON_TEXT = "Cancel" | ||
private const val GOT_IT_BUTTON_TEXT = "Got it!" | ||
private const val DUMMY_VOUCHER = "DUMMY____VOUCHER" | ||
private const val ERROR_MESSAGE = "error_message" | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
...d/app/src/test/kotlin/net/mullvad/mullvadvpn/utils/VoucherRegexHelperParameterizedTest.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,45 @@ | ||
package net.mullvad.mullvadvpn.utils | ||
|
||
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule | ||
import net.mullvad.mullvadvpn.util.VoucherRegexHelper | ||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
|
||
private const val IS_ACCEPTED_FORMAT = true | ||
private const val IS_UNACCEPTED_FORMAT = false | ||
|
||
@RunWith(Parameterized::class) | ||
class VoucherRegexHelperParameterizedTest( | ||
private val isValid: Boolean, | ||
private val voucher: String | ||
) { | ||
@get:Rule val testCoroutineRule = TestCoroutineRule() | ||
|
||
@Test | ||
fun testVoucherFormat() { | ||
assertThat(VoucherRegexHelper.validate(voucher), equalTo(isValid)) | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
@Parameterized.Parameters | ||
fun data(): Collection<Array<Any>> = | ||
listOf( | ||
arrayOf(IS_ACCEPTED_FORMAT, "1"), | ||
arrayOf(IS_ACCEPTED_FORMAT, "a"), | ||
arrayOf(IS_ACCEPTED_FORMAT, "A"), | ||
arrayOf(IS_ACCEPTED_FORMAT, "AAAA"), | ||
arrayOf(IS_ACCEPTED_FORMAT, "AAAABBBB11112222"), | ||
arrayOf(IS_ACCEPTED_FORMAT, "AAAA BBBB 1111 2222"), | ||
arrayOf(IS_ACCEPTED_FORMAT, "AAAA-AAAA-1111-2222\r"), | ||
arrayOf(IS_ACCEPTED_FORMAT, "AAAA-AAAA-1111-2222\n"), | ||
arrayOf(IS_UNACCEPTED_FORMAT, "@"), | ||
arrayOf(IS_UNACCEPTED_FORMAT, "AAAABBBBCCCCDDDD\t"), | ||
arrayOf(IS_UNACCEPTED_FORMAT, "AAAA_BBBB_CCCC_DDDD") | ||
) | ||
} | ||
} |
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