Search in sources :

Example 1 with FormatResult

use of com.globalcollect.gateway.sdk.client.android.sdk.model.FormatResult in project connect-sdk-client-android by Ingenico-ePayments.

the class MaskTest method testMaskingSingleCharacterInclCursor.

@Test
public void testMaskingSingleCharacterInclCursor() {
    StringFormatter formatter = new StringFormatter();
    FormatResult maskedValue = formatter.applyMask(maskExpiryDate, maskTestString1, emptyString, 0);
    assertEquals("1", maskedValue.getFormattedResult());
    assertTrue(maskedValue.getCursorIndex() == 1);
}
Also used : StringFormatter(com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter) FormatResult(com.globalcollect.gateway.sdk.client.android.sdk.model.FormatResult) Test(org.junit.Test)

Example 2 with FormatResult

use of com.globalcollect.gateway.sdk.client.android.sdk.model.FormatResult in project connect-sdk-client-android by Ingenico-ePayments.

the class MaskTest method testInsertCharacterInStringInclCursor.

@Test
public void testInsertCharacterInStringInclCursor() {
    StringFormatter formatter = new StringFormatter();
    FormatResult maskedValue = formatter.applyMask(maskExpiryDate, "11-22", maskTestString2, 1);
    assertEquals("11-22", maskedValue.getFormattedResult());
    assertTrue(maskedValue.getCursorIndex() == 4);
}
Also used : StringFormatter(com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter) FormatResult(com.globalcollect.gateway.sdk.client.android.sdk.model.FormatResult) Test(org.junit.Test)

Example 3 with FormatResult

use of com.globalcollect.gateway.sdk.client.android.sdk.model.FormatResult in project connect-sdk-client-android by Ingenico-ePayments.

the class StringFormatter method determineFormatResultADDED.

private FormatResult determineFormatResultADDED(String mask, String value, String oldValue, int numAddedCharacters, int previousCursorIndex) {
    String formattedValue = "";
    int isInMask = 0;
    int valueIndex = 0;
    int oldValueIndex = 0;
    boolean cursorHasMoved = false;
    int newCursorIndex = previousCursorIndex;
    int numNewCharactersAdded = 0;
    boolean cursorMoving = false;
    // loop over the mask characters
    for (Integer maskIndex = 0; maskIndex < mask.length(); maskIndex++) {
        Character maskCharacter = mask.charAt(maskIndex);
        if (maskCharacter.equals('}')) {
            isInMask--;
        } else if (maskCharacter.equals('{')) {
            isInMask++;
        } else if (isInMask == 2) {
            if (valueIndex >= value.length()) {
                break;
            }
            // so it will move along with the characters that are being added.
            if (valueIndex == previousCursorIndex) {
                cursorMoving = true;
            }
            // added by the user, the cursor should stop moving
            if (numNewCharactersAdded == numAddedCharacters) {
                cursorMoving = false;
            }
            // relevant indices.
            if ((Character.toString(value.charAt(valueIndex)).matches(convertMaskCharacterToRegex(maskCharacter.toString())))) {
                formattedValue += value.charAt(valueIndex);
                if (cursorMoving) {
                    newCursorIndex++;
                    // If the cursor is moving, this implicitly means that we are adding new
                    // charachters that were not in the oldValue, so we have to update the
                    // numNewCharactersAdded counter
                    numNewCharactersAdded++;
                }
            } else {
                // An invalid character had been inserted here; ignore it, but do not move
                // further through the mask
                maskIndex--;
            }
            // old value and the new value
            if (!cursorMoving) {
                oldValueIndex++;
            }
            valueIndex++;
        } else if (isInMask == 0) {
            // Add the mask character that belongs here
            formattedValue += maskCharacter;
            // and we can break the loop.
            if (valueIndex >= value.length()) {
                if (cursorMoving) {
                    newCursorIndex++;
                }
                break;
            }
            // so it will move along with the characters that are being added.
            if (valueIndex == previousCursorIndex) {
                cursorMoving = true;
            }
            // If the cursor is moving, update its location
            if (cursorMoving) {
                newCursorIndex++;
            }
            // added by the user, the cursor should stop moving
            if (numNewCharactersAdded == numAddedCharacters) {
                cursorMoving = false;
            }
            // here, this means that the indices can be updated.
            if (valueIndex == oldValueIndex && value.charAt(valueIndex) == maskCharacter && oldValue.charAt(oldValueIndex) == maskCharacter) {
                valueIndex++;
                oldValueIndex++;
            }
        } else {
            throw new InvalidParameterException("Error while masking inputText; there seems to be an invalid mask.");
        }
    }
    return new FormatResult(formattedValue, newCursorIndex);
}
Also used : InvalidParameterException(java.security.InvalidParameterException) FormatResult(com.globalcollect.gateway.sdk.client.android.sdk.model.FormatResult)

Example 4 with FormatResult

use of com.globalcollect.gateway.sdk.client.android.sdk.model.FormatResult in project connect-sdk-client-android by Ingenico-ePayments.

the class MaskTest method testApplyMaskWithTextWatcherBeforeTextChangedInformationCreditCardNumberADDED.

@Test
public void testApplyMaskWithTextWatcherBeforeTextChangedInformationCreditCardNumberADDED() {
    StringFormatter formatter = new StringFormatter();
    FormatResult maskResult;
    // *** Simple add-to-the-end-tests ***
    // Test typing a single digit in the EditText
    maskResult = formatter.applyMask(maskCardNumber, "1", "", 0, 0, 1);
    validateFormatResult(maskResult, "1", 1);
    // Test pasting 2 digits in the EditText
    maskResult = formatter.applyMask(maskCardNumber, "12", "", 0, 0, 2);
    validateFormatResult(maskResult, "12", 2);
    // Test pasting 4 digits in the EditText
    maskResult = formatter.applyMask(maskCardNumber, "1234", "", 0, 0, 4);
    validateFormatResult(maskResult, "1234 ", 5);
    // Test pasting a full number in the EditText
    maskResult = formatter.applyMask(maskCardNumber, "1234567890123456789", "", 0, 0, 19);
    validateFormatResult(maskResult, "1234 5678 9012 3456 789", 23);
    // Test typing a single digit after two digits in the old value
    maskResult = formatter.applyMask(maskCardNumber, "123", "12", 2, 0, 1);
    validateFormatResult(maskResult, "123", 3);
    // Test pasting 2 digits after two digits in the old value
    maskResult = formatter.applyMask(maskCardNumber, "1234", "12", 2, 0, 2);
    validateFormatResult(maskResult, "1234 ", 5);
    // Test pasting 8 digits after two digits in the old value
    maskResult = formatter.applyMask(maskCardNumber, "1234567890", "12", 2, 0, 8);
    validateFormatResult(maskResult, "1234 5678 90", 12);
    // Test typing a single digit after an already masked old value of "1234 "
    maskResult = formatter.applyMask(maskCardNumber, "1234 5", "1234 ", 5, 0, 1);
    validateFormatResult(maskResult, "1234 5", 6);
    // Test typing a single digit right before a mask character
    maskResult = formatter.applyMask(maskCardNumber, "12345 ", "1234 ", 4, 0, 1);
    validateFormatResult(maskResult, "1234 5", 6);
    // Test pasting two digits right before a mask character
    maskResult = formatter.applyMask(maskCardNumber, "123456 ", "1234 ", 4, 0, 2);
    validateFormatResult(maskResult, "1234 56", 7);
    // Test pasting four digits right before a mask character in a String containing multiple mask characters
    maskResult = formatter.applyMask(maskCardNumber, "12340123 5678 9", "1234 5678 9", 4, 0, 4);
    validateFormatResult(maskResult, "1234 0123 5678 9", 10);
    // Test typing a single digit after an already masked old value of "1234 567"
    maskResult = formatter.applyMask(maskCardNumber, "1234 5678", "1234 567", 8, 0, 1);
    validateFormatResult(maskResult, "1234 5678 ", 10);
    // Test pasting 8 digits after an already masked old value of "1234 5678 901"
    maskResult = formatter.applyMask(maskCardNumber, "1234 5678 90123456789", "1234 5678 901", 13, 0, 8);
    validateFormatResult(maskResult, "1234 5678 9012 3456 789", 23);
    // *** Adding in between other characters tests ***
    // Test adding a single digit between two digits
    maskResult = formatter.applyMask(maskCardNumber, "132", "12", 1, 0, 1);
    validateFormatResult(maskResult, "132", 2);
    // Test adding a single digit between three digits
    maskResult = formatter.applyMask(maskCardNumber, "1423", "123", 1, 0, 1);
    validateFormatResult(maskResult, "1423 ", 2);
    // Test adding two digits between two other digits
    maskResult = formatter.applyMask(maskCardNumber, "1342", "12", 1, 0, 2);
    validateFormatResult(maskResult, "1342 ", 3);
    // Test adding 8 digits between two other digits
    maskResult = formatter.applyMask(maskCardNumber, "1345678902", "12", 1, 0, 8);
    validateFormatResult(maskResult, "1345 6789 02", 11);
    // Test adding non-numerical characters
    maskResult = formatter.applyMask(maskCardNumber, "1a2bc3d4e5", "", 0, 0, 10);
    validateFormatResult(maskResult, "1234 5", 6);
    // Test adding a trailing space
    maskResult = formatter.applyMask(maskCardNumber, "1234 ", "12", 2, 0, 3);
    validateFormatResult(maskResult, "1234 ", 5);
    // Test adding a full credit card number that is already masked
    maskResult = formatter.applyMask(maskCardNumber, "1234 5678 9012 3456 789", "0", 0, 0, 23);
    validateFormatResult(maskResult, "1234 5678 9012 3456 789", 23);
    // Test adding a single digit in front of four other digits
    maskResult = formatter.applyMask(maskCardNumber, "01234 ", "1234 ", 0, 0, 1);
    validateFormatResult(maskResult, "0123 4", 1);
    // Test adding a single digit between two mask characters; test added due to find during monkey testing
    maskResult = formatter.applyMask(maskCardNumber, "1234 56278 901", "1234 5678 901", 7, 0, 1);
    validateFormatResult(maskResult, "1234 5627 8901 ", 8);
}
Also used : StringFormatter(com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter) FormatResult(com.globalcollect.gateway.sdk.client.android.sdk.model.FormatResult) Test(org.junit.Test)

Example 5 with FormatResult

use of com.globalcollect.gateway.sdk.client.android.sdk.model.FormatResult in project connect-sdk-client-android by Ingenico-ePayments.

the class MaskTest method testApplyMaskWithTextWatcherBeforeTextChangedInformationCreditCardNumberREPLACED.

@Test
public void testApplyMaskWithTextWatcherBeforeTextChangedInformationCreditCardNumberREPLACED() {
    StringFormatter formatter = new StringFormatter();
    FormatResult maskResult;
    // *** Test simple replacements that do not involve masking ***
    // Test replace a character by another
    maskResult = formatter.applyMask(maskCardNumber, "2", "1", 0, 1, 1);
    validateFormatResult(maskResult, "2", 1);
    // Test replace two characters by two others
    maskResult = formatter.applyMask(maskCardNumber, "34", "12", 0, 2, 2);
    validateFormatResult(maskResult, "34", 2);
    // Test replace one character by two others
    maskResult = formatter.applyMask(maskCardNumber, "23", "1", 0, 1, 2);
    validateFormatResult(maskResult, "23", 2);
    // Test replace three characters by one other
    maskResult = formatter.applyMask(maskCardNumber, "4", "123", 0, 3, 1);
    validateFormatResult(maskResult, "4", 1);
    // *** Test more complex replacements that touch mask characters ***
    // Test replace the character before a mask character
    maskResult = formatter.applyMask(maskCardNumber, "1235 ", "1234 ", 3, 1, 1);
    validateFormatResult(maskResult, "1235 ", 5);
    // Test replace the two characters before a mask character by a single character
    maskResult = formatter.applyMask(maskCardNumber, "125 ", "1234 ", 2, 2, 1);
    validateFormatResult(maskResult, "125", 3);
    // Test replace the character before a mask character along with the mask character
    maskResult = formatter.applyMask(maskCardNumber, "1235", "1234 ", 3, 2, 1);
    validateFormatResult(maskResult, "1235 ", 5);
    // Test replace the two characters before a mask character, along with the mask character, by a single character
    maskResult = formatter.applyMask(maskCardNumber, "125", "1234 ", 2, 3, 1);
    validateFormatResult(maskResult, "125", 3);
    // Test replace the two characters before a mask character by two characters
    maskResult = formatter.applyMask(maskCardNumber, "1256 ", "1234 ", 2, 2, 2);
    validateFormatResult(maskResult, "1256 ", 5);
    // Test replace the two characters before a mask character, along with the mask character, by two characters
    maskResult = formatter.applyMask(maskCardNumber, "1256", "1234 ", 2, 3, 2);
    validateFormatResult(maskResult, "1256 ", 5);
    // Test replace all characters before a mask character by four other characters
    maskResult = formatter.applyMask(maskCardNumber, "5678 ", "1234 ", 0, 4, 4);
    validateFormatResult(maskResult, "5678 ", 5);
    // Test replace all characters, including the mask character by four other characters
    maskResult = formatter.applyMask(maskCardNumber, "5678", "1234 ", 0, 5, 4);
    validateFormatResult(maskResult, "5678 ", 5);
    // Test replace the mask character by another character
    maskResult = formatter.applyMask(maskCardNumber, "12345", "1234 ", 4, 1, 1);
    validateFormatResult(maskResult, "1234 5", 6);
    // Test replace the character before the mask character and the mask character by two other characters
    maskResult = formatter.applyMask(maskCardNumber, "12356", "1234 ", 3, 2, 2);
    validateFormatResult(maskResult, "1235 6", 6);
    // *** Test replacements with selections that span across mask characters ***
    // Test replace a String with mask character by a single character
    maskResult = formatter.applyMask(maskCardNumber, "6", "1234 5", 0, 6, 1);
    validateFormatResult(maskResult, "6", 1);
    // Test replace a String with mask character by four characters
    maskResult = formatter.applyMask(maskCardNumber, "6789", "1234 5", 0, 6, 4);
    validateFormatResult(maskResult, "6789 ", 5);
    // Test replace a subString "across" a mask character
    maskResult = formatter.applyMask(maskCardNumber, "1237896", "1234 56", 3, 3, 3);
    validateFormatResult(maskResult, "1237 896", 7);
    // Test replace a subString "across" a mask character, where the cursor ends near a mask character
    maskResult = formatter.applyMask(maskCardNumber, "123789016", "1234 56", 3, 3, 5);
    validateFormatResult(maskResult, "1237 8901 6", 10);
    // Test replace a subString "across" a mask character, after another mask character which remains untouched
    maskResult = formatter.applyMask(maskCardNumber, "1234 56734501", "1234 5678 901", 8, 3, 3);
    validateFormatResult(maskResult, "1234 5673 4501 ", 12);
    // Test replace a full credit card number, including masks, by another full credit card number
    maskResult = formatter.applyMask(maskCardNumber, "9876543210987654321", "1234 5678 9012 3456 789", 0, 23, 19);
    validateFormatResult(maskResult, "9876 5432 1098 7654 321", 23);
}
Also used : StringFormatter(com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter) FormatResult(com.globalcollect.gateway.sdk.client.android.sdk.model.FormatResult) Test(org.junit.Test)

Aggregations

FormatResult (com.globalcollect.gateway.sdk.client.android.sdk.model.FormatResult)9 StringFormatter (com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter)6 Test (org.junit.Test)6 InvalidParameterException (java.security.InvalidParameterException)1