-
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
da2c1b9
commit 58ca726
Showing
6 changed files
with
235 additions
and
15 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
...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,105 @@ | ||
package net.mullvad.mullvadvpn.compose.screen | ||
|
||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.test.assert | ||
import androidx.compose.ui.test.hasText | ||
import androidx.compose.ui.test.isNotEnabled | ||
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.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() | ||
|
||
private var uiState = VoucherDialogUiState.INITIAL | ||
|
||
@Before | ||
fun setup() { | ||
mockkObject(VoucherRegexHelper) | ||
} | ||
|
||
@Test | ||
fun testDismissDialog() { | ||
// Arrange | ||
val mockedClickHandler: (Boolean) -> Unit = mockk(relaxed = true) | ||
composeTestRule.setContentWithTheme { | ||
RedeemVoucherDialogScreen( | ||
uiState = uiState, | ||
onVoucherInputChange = {}, | ||
onRedeem = {}, | ||
onDismiss = mockedClickHandler | ||
) | ||
} | ||
|
||
// Act | ||
|
||
composeTestRule.onNodeWithText(CANCEL_BUTTON_TEXT).performClick() | ||
|
||
// Assert | ||
verify { mockedClickHandler.invoke(false) } | ||
} | ||
|
||
@Test | ||
fun testInsertValidVoucher() { | ||
// Arrange | ||
composeTestRule.setContentWithTheme { | ||
var voucherInput = remember { "" } | ||
RedeemVoucherDialogScreen( | ||
uiState = uiState.copy(voucherInput = voucherInput), | ||
onVoucherInputChange = { voucherInput = it }, | ||
onRedeem = {}, | ||
onDismiss = {} | ||
) | ||
} | ||
|
||
// Act | ||
composeTestRule.onNodeWithTag(VOUCHER_INPUT_TEST_TAG).performTextInput(DUMMY_VALID_VOUCHER) | ||
|
||
// Assert | ||
composeTestRule | ||
.onNodeWithTag(VOUCHER_INPUT_TEST_TAG) | ||
.assert(hasText(DUMMY_VALID_VOUCHER_FORMATED)) | ||
} | ||
|
||
@Test | ||
fun testInsertInvalidVoucher() { | ||
// Arrange | ||
composeTestRule.setContentWithTheme { | ||
var voucherInput = remember { "" } | ||
RedeemVoucherDialogScreen( | ||
uiState = uiState.copy(voucherInput = voucherInput), | ||
onVoucherInputChange = { voucherInput = it }, | ||
onRedeem = {}, | ||
onDismiss = {} | ||
) | ||
} | ||
|
||
// Act | ||
composeTestRule | ||
.onNodeWithTag(VOUCHER_INPUT_TEST_TAG) | ||
.performTextInput(DUMMY_INVALID_VOUCHER) | ||
|
||
// Assert | ||
composeTestRule.onNodeWithText(REDEEM_BUTTON_TEXT).assert(isNotEnabled()) | ||
} | ||
|
||
companion object { | ||
private const val REDEEM_BUTTON_TEXT = "Redeem" | ||
private const val CANCEL_BUTTON_TEXT = "Cancel" | ||
private const val DUMMY_VALID_VOUCHER = "DUMMYVALIDVOUCHE" | ||
private const val DUMMY_VALID_VOUCHER_FORMATED = "DUMM-YVAL-IDVO-UCHE" | ||
private const val DUMMY_INVALID_VOUCHER = "DUMM-YINV-ALID-VOUC-HER" | ||
} | ||
} |
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
...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,63 @@ | ||
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() | ||
|
||
companion object { | ||
@JvmStatic | ||
@Parameterized.Parameters | ||
fun data(): Collection<Array<Any>> = | ||
listOf( | ||
arrayOf(IS_ACCEPTED_FORMAT, acceptable_inputs_for_voucher[0]), | ||
arrayOf(IS_ACCEPTED_FORMAT, acceptable_inputs_for_voucher[1]), | ||
arrayOf(IS_ACCEPTED_FORMAT, acceptable_inputs_for_voucher[2]), | ||
arrayOf(IS_ACCEPTED_FORMAT, acceptable_inputs_for_voucher[3]), | ||
arrayOf(IS_ACCEPTED_FORMAT, acceptable_inputs_for_voucher[4]), | ||
arrayOf(IS_ACCEPTED_FORMAT, acceptable_inputs_for_voucher[5]), | ||
arrayOf(IS_ACCEPTED_FORMAT, acceptable_inputs_for_voucher[6]), | ||
arrayOf(IS_ACCEPTED_FORMAT, acceptable_inputs_for_voucher[7]), | ||
arrayOf(IS_UNACCEPTED_FORMAT, non_acceptable_inputs_for_voucher[0]), | ||
arrayOf(IS_UNACCEPTED_FORMAT, non_acceptable_inputs_for_voucher[1]), | ||
arrayOf(IS_UNACCEPTED_FORMAT, non_acceptable_inputs_for_voucher[2]) | ||
) | ||
|
||
private val acceptable_inputs_for_voucher = | ||
arrayOf( | ||
"1", | ||
"a", | ||
"A", | ||
"AAAA", | ||
"AAAABBBB11112222", | ||
"AAAA BBBB 1111 2222", | ||
"AAAA-AAAA-1111-2222\r", | ||
"AAAA-AAAA-1111-2222\n", | ||
) | ||
private val non_acceptable_inputs_for_voucher = | ||
arrayOf( | ||
"@", | ||
"AAAABBBBCCCCDDDD\t", | ||
"AAAA_BBBB_CCCC_DDDD", | ||
) | ||
} | ||
|
||
@Test | ||
fun testVoucherFormat() { | ||
assertThat(VoucherRegexHelper.validate(voucher), equalTo(isValid)) | ||
} | ||
} |
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