public class Settings {

    protected static final String ENVIRONMENT = "environment";

    private static final String VERSION = "version";

    private static final String SANDBOX_BASE_SERVER_URL = "https://braintree-sample-merchant.herokuapp.com";
    private static final String PRODUCTION_BASE_SERVER_URL = "https://executive-sample-merchant.herokuapp.com";

    private static final String SANDBOX_TOKENIZATION_KEY = "sandbox_tmxhyf7d_dcpspy2brwdjr3qn";
    private static final String PRODUCTION_TOKENIZATION_KEY = "production_t2wns2y2_dfy45jdj3dxkmz5m";

    private static SharedPreferences sSharedPreferences;

    public static SharedPreferences getPreferences(Context context) {
        if (sSharedPreferences == null) {
            sSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        }

        return sSharedPreferences;
    }

    public static int getVersion(Context context) {
        return getPreferences(context).getInt(VERSION, 0);
    }

    public static void setVersion(Context context) {
        getPreferences(context).edit().putInt(VERSION, BuildConfig.VERSION_CODE).apply();
    }

    public static int getEnvironment(Context context) {
        return getPreferences(context).getInt(ENVIRONMENT, 0);
    }

    public static void setEnvironment(Context context, int environment) {
        getPreferences(context)
                .edit()
                .putInt(ENVIRONMENT, environment)
                .apply();

        DemoApplication.resetApiClient();
    }

    public static String getSandboxUrl() {
        return SANDBOX_BASE_SERVER_URL;
    }

    public static String getEnvironmentUrl(Context context) {
        int environment = getEnvironment(context);
        if (environment == 0) {
            return SANDBOX_BASE_SERVER_URL;
        } else if (environment == 1) {
            return PRODUCTION_BASE_SERVER_URL;
        } else {
            return "";
        }
    }

    public static String getCustomerId(Context context) {
        return getPreferences(context).getString("customer", null);
    }

    public static String getMerchantAccountId(Context context) {
        return getPreferences(context).getString("merchant_account", null);
    }

    public static String getThreeDSecureMerchantAccountId(Context context) {
        if (isThreeDSecureEnabled(context) && getEnvironment(context) == 1) {
            return "test_AIB";
        } else {
            return null;
        }
    }

    public static String getUnionPayMerchantAccountId(Context context) {
        if (getEnvironment(context) == 0) {
            return "fake_switch_usd";
        } else {
            return null;
        }
    }

    public static boolean useTokenizationKey(Context context) {
        return getPreferences(context).getBoolean("tokenization_key", false);
    }

    public static String getEnvironmentTokenizationKey(Context context) {
        int environment = getEnvironment(context);
        if (environment == 0) {
            return SANDBOX_TOKENIZATION_KEY;
        } else if (environment == 1) {
            return PRODUCTION_TOKENIZATION_KEY;
        } else {
            return "";
        }
    }

    public static boolean isPayPalSignatureVerificationDisabled(Context context) {
        return getPreferences(context).getBoolean("paypal_disable_signature_verification", true);
    }

    public static boolean useHardcodedPayPalConfiguration(Context context) {
        return getPreferences(context).getBoolean("paypal_use_hardcoded_configuration", false);
    }

    public static boolean isThreeDSecureEnabled(Context context) {
        return getPreferences(context).getBoolean("enable_three_d_secure", false);
    }

    public static boolean isThreeDSecureRequired(Context context) {
        return getPreferences(context).getBoolean("require_three_d_secure", true);
    }

    public static String getThreeDSecureVersion(Context context) {
        return getPreferences(context).getString("three_d_secure_version", ThreeDSecureRequest.VERSION_2);
    }

    public static boolean isVaultManagerEnabled(Context context) {
        return getPreferences(context).getBoolean("enable_vault_manager", false);
    }

    public static int getCardholderNameStatus(Context context) {
        String status = getPreferences(context).getString("cardholder_name_status", "Disabled");

        switch (status) {
            case "Optional":
                return CardForm.FIELD_OPTIONAL;
            case "Required":
                return CardForm.FIELD_REQUIRED;
            case "Disabled":
            default:
                return CardForm.FIELD_DISABLED;
        }
    }

    static boolean isSaveCardCheckBoxVisible(Context context) {
        return getPreferences(context).getBoolean("save_card_checkbox_visible", false);
    }

    static boolean defaultVaultSetting(Context context) {
        return getPreferences(context).getBoolean("save_card_checkbox_default_value", true);
    }
} public class ExpirationDate {

    public static final String VALID_EXPIRATION_YEAR =
        String.valueOf(Calendar.getInstance().get(Calendar.YEAR) + 1);
} public class TestHelper {

    @CallSuper
    public void setup() {
        clearPreference("BraintreeApi");
        clearPreference("PayPalOTC");

        PreferenceManager.getDefaultSharedPreferences(ApplicationProvider.getApplicationContext())
                .edit()
                .clear()
                .putBoolean("paypal_use_hardcoded_configuration", true)
                .commit();
    }

    public void launchApp() {
        onDevice().onHomeScreen().launchApp("com.braintreepayments.demo");
        ensureEnvironmentIs("Sandbox");
    }

    public DeviceAutomator getNonceDetails() {
        return onDevice(withResourceId("com.braintreepayments.demo:id/nonce_details"));
    }

    public void setCustomerId(String customerId) {
        PreferenceManager.getDefaultSharedPreferences(ApplicationProvider.getApplicationContext())
                .edit()
                .putString("customer", customerId)
                .commit();
    }

    /**
     * Sets the customer ID to a value that should be randomized.
     * There should not be any saved payment methods for this customer.
     */
    public void setUniqueCustomerId() {
        setCustomerId(""+System.currentTimeMillis());
    }

    public void setMerchantAccountId(String merchantAccountId) {
        PreferenceManager.getDefaultSharedPreferences(ApplicationProvider.getApplicationContext())
                .edit()
                .putString("merchant_account", merchantAccountId)
                .commit();
    }

    public void useTokenizationKey() {
        PreferenceManager.getDefaultSharedPreferences(ApplicationProvider.getApplicationContext())
                .edit()
                .putBoolean("tokenization_key", true)
                .commit();
    }

    public void enableThreeDSecure() {
        PreferenceManager.getDefaultSharedPreferences(ApplicationProvider.getApplicationContext())
                .edit()
                .putBoolean("enable_three_d_secure", true)
                .commit();
    }

    public void enableVaultManager() {
        PreferenceManager.getDefaultSharedPreferences(ApplicationProvider.getApplicationContext())
                .edit()
                .putBoolean("enable_vault_manager", true)
                .commit();
    }

    public void setCardholderNameStatus(int cardholderNameStatus) {
        String status;

        switch(cardholderNameStatus) {
            case CardForm.FIELD_REQUIRED:
                status = "Required";
                break;
            case CardForm.FIELD_OPTIONAL:
                status = "Optional";
                break;
            default:
            case CardForm.FIELD_DISABLED:
                status = "Disabled";
                break;
        }

        PreferenceManager.getDefaultSharedPreferences(ApplicationProvider.getApplicationContext())
                .edit()
                .putString("cardholder_name_status", status)
                .commit();
    }

    public void setSaveCardCheckBox(boolean visible, boolean defaultValue) {
        PreferenceManager.getDefaultSharedPreferences(ApplicationProvider.getApplicationContext())
                .edit()
                .putBoolean("save_card_checkbox_visible", visible)
                .putBoolean("save_card_checkbox_default_value", defaultValue)
                .commit();
    }

    private void clearPreference(String preference) {
        ApplicationProvider.getApplicationContext().getSharedPreferences(preference, Context.MODE_PRIVATE)
                .edit()
                .clear()
                .commit();
    }

    private static void ensureEnvironmentIs(String environment) {
        try {
            onDevice(withText(environment)).check(text(equalTo(environment)));
        } catch (RuntimeException e) {
            onDevice(withClass(Spinner.class)).perform(click());
            onDevice(withText(environment)).perform(click());
            onDevice(withText(environment)).check(text(equalTo(environment)));
        }
    }

    protected void performCardDetailsEntry() {
        onDevice(withText("Expiration Date")).perform(setText("12" + ExpirationDate.VALID_EXPIRATION_YEAR));
        onDevice(withText("CVV")).perform(setText("123"));
        onDevice(withText("Postal Code")).perform(setText("12345"));
    }

    protected void tokenizeCard(String cardNumber) {
        onDevice(withText("Credit or Debit Card")).waitForExists().perform(click());
        onDevice(withText("Card Number")).waitForExists().perform(setText(cardNumber));
        onDevice(withText("Next")).perform(click());
        performCardDetailsEntry();
        onDevice(withTextContaining("Add Card")).perform(click());
    }
} public static final String THREE_D_SECURE_2_CHALLENGE_VERIFICATON = "4000000000001091"; public static final String THREE_D_SECURE_2_FRICTIONLESS_VERIFICATON = "4000000000001000"; public static final String THREE_D_SECURE_VERIFICATON = "4000000000000002"; public static final String UNIONPAY_CREDIT = "6212345678901232"; public static final String UNIONPAY_SMS_NOT_REQUIRED = "6212345678900085"; public static final String VISA = "4111111111111111"; public static void clickWebViewText(String text) {
    clickWebViewText(text, 1000);
}
import android.widget.Button;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.RequiresDevice;
import com.braintreepayments.demo.Settings;
import com.braintreepayments.demo.test.utilities.ExpirationDate;
import com.braintreepayments.demo.test.utilities.TestHelper;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
import static androidx.test.InstrumentationRegistry.getTargetContext;
import static com.braintreepayments.AutomatorAction.clearTextField;
import static com.braintreepayments.AutomatorAction.click;
import static com.braintreepayments.AutomatorAction.setText;
import static com.braintreepayments.AutomatorAssertion.text;
import static com.braintreepayments.DeviceAutomator.onDevice;
import static com.braintreepayments.UiObjectMatcher.withContentDescription;
import static com.braintreepayments.UiObjectMatcher.withResourceId;
import static com.braintreepayments.UiObjectMatcher.withText;
import static com.braintreepayments.UiObjectMatcher.withTextContaining;
import static com.braintreepayments.UiObjectMatcher.withTextStartingWith;
import static com.braintreepayments.demo.test.utilities.CardNumber.THREE_D_SECURE_2_CHALLENGE_VERIFICATON;
import static com.braintreepayments.demo.test.utilities.CardNumber.THREE_D_SECURE_2_FRICTIONLESS_VERIFICATON;
import static com.braintreepayments.demo.test.utilities.CardNumber.THREE_D_SECURE_VERIFICATON;
import static com.braintreepayments.demo.test.utilities.CardNumber.UNIONPAY_CREDIT;
import static com.braintreepayments.demo.test.utilities.CardNumber.UNIONPAY_SMS_NOT_REQUIRED;
import static com.braintreepayments.demo.test.utilities.CardNumber.VISA;
import static com.braintreepayments.demo.test.utilities.UiTestActions.clickWebViewText;
import static junit.framework.Assert.assertFalse;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.StringEndsWith.endsWith;
import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase;
import static org.junit.Assume.assumeFalse;
package com.braintreepayments.demo.test;







@RunWith(AndroidJUnit4.class)
public class DropInTest extends TestHelper {

    @Before
    public void setup() {
        super.setup();
    }

    @Test(timeout = 60000)
    public void tokenizesACard() {
        launchApp();
        onDevice(withText("Add Payment Method")).waitForExists().waitForEnabled().perform(click());

        tokenizeCard(VISA);