-
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
35d2ab7
commit aaa4a60
Showing
5 changed files
with
213 additions
and
10 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
...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,99 @@ | ||
package net.mullvad.mullvadvpn.compose.screen | ||
|
||
import android.content.res.Resources | ||
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.MockKAnnotations | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.mockkObject | ||
import io.mockk.verify | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
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.ui.serviceconnection.ServiceConnectionManager | ||
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionState | ||
import net.mullvad.mullvadvpn.util.VoucherRegexHelper | ||
import net.mullvad.mullvadvpn.viewmodel.VoucherDialogViewModel | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class RedeemVoucherDialogTest { | ||
@get:Rule val composeTestRule = createComposeRule() | ||
|
||
private val mockServiceConnectionManager: ServiceConnectionManager = mockk() | ||
private val mockResources: Resources = mockk() | ||
|
||
private val _connectionState = | ||
MutableStateFlow<ServiceConnectionState>(ServiceConnectionState.Disconnected) | ||
|
||
val connectionState = _connectionState.asStateFlow() | ||
|
||
@Before | ||
fun setup() { | ||
MockKAnnotations.init(this) | ||
mockkObject(VoucherRegexHelper) | ||
} | ||
|
||
@Test | ||
fun testInsertInvalidVoucher() { | ||
// Arrange | ||
every { VoucherRegexHelper.validate(any()) } returns false | ||
every { mockServiceConnectionManager.connectionState } returns connectionState | ||
|
||
// Act | ||
val vm = VoucherDialogViewModel(mockServiceConnectionManager, mockResources) | ||
val uiState = vm.uiState.value | ||
composeTestRule.setContentWithTheme { | ||
RedeemVoucherDialogScreen( | ||
uiState = uiState, | ||
onVoucherInputChange = { vm.onVoucherInputChange(it) }, | ||
onRedeem = {}, | ||
onDismiss = {} | ||
) | ||
} | ||
|
||
// Sets the TextField value | ||
composeTestRule.onNodeWithTag(VOUCHER_INPUT_TEST_TAG).performTextInput(DUMMY_VALID_VOUCHER) | ||
composeTestRule.onNodeWithText(REDEEM_BUTTON_TEXT).performClick() | ||
|
||
// Assert | ||
composeTestRule.onNodeWithText(DUMMY_INVALID_VOUCHER).assertDoesNotExist() | ||
} | ||
|
||
@Test | ||
fun testDismissDialog() { | ||
// Arrange | ||
val mockedClickHandler: (Boolean) -> Unit = mockk(relaxed = true) | ||
|
||
// Act | ||
composeTestRule.setContentWithTheme { | ||
RedeemVoucherDialogScreen( | ||
uiState = VoucherDialogUiState.INITIAL, | ||
onVoucherInputChange = {}, | ||
onRedeem = {}, | ||
onDismiss = mockedClickHandler | ||
) | ||
} | ||
|
||
composeTestRule.onNodeWithText(CANCEL_BUTTON_TEXT).performClick() | ||
|
||
// Assert | ||
verify { mockedClickHandler.invoke(false) } | ||
} | ||
|
||
companion object { | ||
private const val REDEEM_BUTTON_TEXT = "Redeem" | ||
private const val CANCEL_BUTTON_TEXT = "Cancel" | ||
// private const val DUMMY_VALID_VOUCHER = "DUMM-YVAL-IDVO-UCHE-R" | ||
private const val DUMMY_VALID_VOUCHER = "DUMMYVALIDVOUCHER" | ||
private const val DUMMY_USED_VOUCHER = "DUMM-YUSE-DVOU-CHER" | ||
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
57 changes: 57 additions & 0 deletions
57
android/app/src/test/kotlin/net/mullvad/mullvadvpn/utils/RedeemVoucherHelperTest.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,57 @@ | ||
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 | ||
|
||
@RunWith(Parameterized::class) | ||
class RedeemVoucherHelperTest(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(true, acceptable_inputs_for_voucher[0]), | ||
arrayOf(true, acceptable_inputs_for_voucher[1]), | ||
arrayOf(true, acceptable_inputs_for_voucher[2]), | ||
arrayOf(true, acceptable_inputs_for_voucher[3]), | ||
arrayOf(true, acceptable_inputs_for_voucher[4]), | ||
arrayOf(true, acceptable_inputs_for_voucher[5]), | ||
arrayOf(true, acceptable_inputs_for_voucher[6]), | ||
arrayOf(true, acceptable_inputs_for_voucher[7]), | ||
arrayOf(false, non_acceptable_inputs_for_voucher[0]), | ||
arrayOf(false, non_acceptable_inputs_for_voucher[1]), | ||
arrayOf(false, 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 shouldReturnExpectedResultForVoucherCheck() { | ||
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