Skip to content

Commit

Permalink
Fix cancel listener being called twice when PayPal is canceled with b…
Browse files Browse the repository at this point in the history
…ack button

Fixes #112
  • Loading branch information
stevecoffey authored and lkorth committed Jul 29, 2016
1 parent 77202eb commit 4d0ff99
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.braintreepayments.api;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.test.runner.AndroidJUnit4;

import com.braintreepayments.api.models.Configuration;
import com.braintreepayments.api.models.PayPalRequest;
import com.braintreepayments.api.test.TestActivity;
import com.braintreepayments.testutils.BraintreeActivityTestRule;
import com.braintreepayments.testutils.TestConfigurationBuilder;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import static com.braintreepayments.api.BraintreeFragmentTestUtils.getMockFragment;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@RunWith(AndroidJUnit4.class)
public class PayPalTest {

@Rule
public final BraintreeActivityTestRule<TestActivity> mActivityTestRule =
new BraintreeActivityTestRule<>(TestActivity.class);

@Test
public void authorizeAccount_onBackPressed_callsCancelListenerOnlyOnce() {
Configuration configuration = new TestConfigurationBuilder()
.paypalEnabled(true)
.buildConfiguration();

final BraintreeFragment fragment = getMockFragment(mActivityTestRule.getActivity(), configuration);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
fragment.onActivityResult(PayPal.PAYPAL_REQUEST_CODE, Activity.RESULT_CANCELED, new Intent());
return null;
}
}).when(fragment).startActivity(any(Intent.class));

PayPal.authorizeAccount(fragment);

verify(fragment, times(1)).postCancelCallback(PayPal.PAYPAL_REQUEST_CODE);
}
}
6 changes: 4 additions & 2 deletions Braintree/src/main/java/com/braintreepayments/api/PayPal.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ protected static void onActivityResult(final BraintreeFragment fragment, int res
sendAnalyticsEventForSwitchResult(fragment, request, isAppSwitch, "failed");
break;
case Cancel:
fragment.postCancelCallback(PAYPAL_REQUEST_CODE);
sendAnalyticsEventForSwitchResult(fragment, request, isAppSwitch, "canceled");
fragment.postCancelCallback(PAYPAL_REQUEST_CODE);
break;
case Success:
onSuccess(fragment, data, request, result);
Expand All @@ -396,7 +396,9 @@ protected static void onActivityResult(final BraintreeFragment fragment, int res
}
fragment.sendAnalyticsEvent("paypal." + type + ".canceled");

fragment.postCancelCallback(PAYPAL_REQUEST_CODE);
if (resultCode != Activity.RESULT_CANCELED) {
fragment.postCancelCallback(PAYPAL_REQUEST_CODE);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,10 +713,4 @@ private void setField(String fieldName, Object src, Object value)

field.set(src, value);
}

private Object getField(String fieldName, Object src) throws IllegalAccessException, NoSuchFieldException {
Field field = src.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(src);
}
}

0 comments on commit 4d0ff99

Please sign in to comment.