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);
}
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());
}
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();
}
});
}
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);
}
});
}
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);
}
}
}
Aggregations