Search in sources :

Example 1 with QueuedCallback

use of com.braintreepayments.api.interfaces.QueuedCallback in project braintree_android by braintree.

the class BraintreeFragmentUnitTest method doesNotExecuteCallbackWhenShouldRunIsFalse.

@Test
public void doesNotExecuteCallbackWhenShouldRunIsFalse() throws InvalidArgumentException {
    BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY);
    QueuedCallback callback = new QueuedCallback() {

        @Override
        public boolean shouldRun() {
            return false;
        }

        @Override
        public void run() {
            fail("Listener was called");
        }
    };
    fragment.postOrQueueCallback(callback);
}
Also used : QueuedCallback(com.braintreepayments.api.interfaces.QueuedCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with QueuedCallback

use of com.braintreepayments.api.interfaces.QueuedCallback in project braintree_android by braintree.

the class BraintreeFragmentUnitTest method executesCallbacksOnlyWhenShouldRunIsTrue.

@Test
public void executesCallbacksOnlyWhenShouldRunIsTrue() throws InvalidArgumentException {
    final AtomicBoolean shouldRun = new AtomicBoolean(false);
    final AtomicBoolean run = new AtomicBoolean(false);
    BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY);
    QueuedCallback callback = new QueuedCallback() {

        @Override
        public boolean shouldRun() {
            return shouldRun.get();
        }

        @Override
        public void run() {
            run.set(true);
        }
    };
    fragment.postOrQueueCallback(callback);
    assertFalse(run.get());
    shouldRun.set(true);
    fragment.flushCallbacks();
    assertTrue(run.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueuedCallback(com.braintreepayments.api.interfaces.QueuedCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with QueuedCallback

use of com.braintreepayments.api.interfaces.QueuedCallback in project braintree_android by braintree.

the class BraintreeFragment method fetchConfiguration.

@VisibleForTesting
protected void fetchConfiguration() {
    if (getConfiguration() != null || ConfigurationManager.isFetchingConfiguration() || mAuthorization == null || mHttpClient == null) {
        return;
    }
    if (mConfigurationRequestAttempts >= 3) {
        postCallback(new ConfigurationException("Configuration retry limit has been exceeded. Create a new " + "BraintreeFragment and try again."));
        return;
    }
    mConfigurationRequestAttempts++;
    ConfigurationManager.getConfiguration(this, new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            setConfiguration(configuration);
            postConfigurationCallback();
            flushCallbacks();
        }
    }, new BraintreeResponseListener<Exception>() {

        @Override
        public void onResponse(final Exception e) {
            final ConfigurationException exception = new ConfigurationException("Request for configuration has failed: " + e.getMessage() + ". " + "Future requests will retry up to 3 times", e);
            postCallback(exception);
            postOrQueueCallback(new QueuedCallback() {

                @Override
                public boolean shouldRun() {
                    return mConfigurationErrorListener != null;
                }

                @Override
                public void run() {
                    mConfigurationErrorListener.onResponse(exception);
                }
            });
            flushCallbacks();
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Configuration(com.braintreepayments.api.models.Configuration) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) JSONException(org.json.JSONException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) GoogleApiClientException(com.braintreepayments.api.exceptions.GoogleApiClientException) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) QueuedCallback(com.braintreepayments.api.interfaces.QueuedCallback) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 4 with QueuedCallback

use of com.braintreepayments.api.interfaces.QueuedCallback in project braintree_android by braintree.

the class BraintreeFragment method postCallback.

protected void postCallback(final PaymentMethodNonce paymentMethodNonce) {
    if (paymentMethodNonce instanceof AndroidPayCardNonce) {
        for (PaymentMethodNonce cachedPaymentMethodNonce : new ArrayList<>(mCachedPaymentMethodNonces)) {
            if (cachedPaymentMethodNonce instanceof AndroidPayCardNonce) {
                mCachedPaymentMethodNonces.remove(cachedPaymentMethodNonce);
            }
        }
    }
    mCachedPaymentMethodNonces.add(0, paymentMethodNonce);
    postOrQueueCallback(new QueuedCallback() {

        @Override
        public boolean shouldRun() {
            return mPaymentMethodNonceCreatedListener != null;
        }

        @Override
        public void run() {
            mPaymentMethodNonceCreatedListener.onPaymentMethodNonceCreated(paymentMethodNonce);
        }
    });
}
Also used : AndroidPayCardNonce(com.braintreepayments.api.models.AndroidPayCardNonce) ArrayList(java.util.ArrayList) PaymentMethodNonce(com.braintreepayments.api.models.PaymentMethodNonce) QueuedCallback(com.braintreepayments.api.interfaces.QueuedCallback)

Example 5 with QueuedCallback

use of com.braintreepayments.api.interfaces.QueuedCallback in project braintree_android by braintree.

the class BraintreeFragment method flushCallbacks.

@VisibleForTesting
protected void flushCallbacks() {
    Queue<QueuedCallback> queue = new ArrayDeque<>();
    queue.addAll(mCallbackQueue);
    for (QueuedCallback callback : queue) {
        if (callback.shouldRun()) {
            callback.run();
            mCallbackQueue.remove(callback);
        }
    }
}
Also used : QueuedCallback(com.braintreepayments.api.interfaces.QueuedCallback) ArrayDeque(java.util.ArrayDeque) VisibleForTesting(android.support.annotation.VisibleForTesting)

Aggregations

QueuedCallback (com.braintreepayments.api.interfaces.QueuedCallback)7 VisibleForTesting (android.support.annotation.VisibleForTesting)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 BraintreeException (com.braintreepayments.api.exceptions.BraintreeException)1 ConfigurationException (com.braintreepayments.api.exceptions.ConfigurationException)1 GoogleApiClientException (com.braintreepayments.api.exceptions.GoogleApiClientException)1 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)1 ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)1 AndroidPayCardNonce (com.braintreepayments.api.models.AndroidPayCardNonce)1 Configuration (com.braintreepayments.api.models.Configuration)1 PaymentMethodNonce (com.braintreepayments.api.models.PaymentMethodNonce)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 JSONException (org.json.JSONException)1