Skip to content

Commit

Permalink
Fix NPE in Drop-in when Braintree is not yet setup
Browse files Browse the repository at this point in the history
* Fixes #30
  • Loading branch information
lkorth committed May 19, 2015
1 parent e346c57 commit 09af7be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.braintreepayments.api.dropin;

import android.app.Activity;
import android.view.KeyEvent;

import com.braintreepayments.api.Braintree;
Expand All @@ -9,6 +10,8 @@
import com.braintreepayments.api.exceptions.ServerException;
import com.braintreepayments.testutils.TestClientTokenBuilder;

import java.util.Map;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.typeText;
Expand All @@ -19,6 +22,7 @@
import static com.braintreepayments.api.TestDependencyInjector.injectBraintree;
import static com.braintreepayments.api.utils.PaymentFormHelpers.performPayPalAdd;
import static com.braintreepayments.api.utils.PaymentFormHelpers.waitForAddPaymentFormHeader;
import static com.braintreepayments.testutils.ActivityResultHelper.getActivityResult;
import static com.braintreepayments.testutils.CardNumber.VISA;
import static com.braintreepayments.testutils.ui.Matchers.withHint;
import static com.braintreepayments.testutils.ui.Matchers.withId;
Expand Down Expand Up @@ -111,6 +115,17 @@ public void testAddsEventOnSDKExitWithUserCanceled() {
verify(mBraintree, times(1)).sendAnalyticsEvent("sdk.exit.user-canceled");
}

public void testDoesntCrashWhenUserExitsRightAfterDropInIsLaunched() {
setClientTokenExtraForTest(this, new TestClientTokenBuilder().withAnalytics().build());
mActivity = getActivity();

sendKeys(KeyEvent.KEYCODE_BACK);

waitForActivityToFinish(mActivity);
Map<String, Object> result = getActivityResult(mActivity);
assertEquals(Activity.RESULT_CANCELED, result.get("resultCode"));
}

public void testAddsEventOnSDKExitWithDeveloperError() {
setupActivity();
BraintreeTestUtils
Expand All @@ -137,6 +152,7 @@ public void testAddsEventOnSDKExitWithServerUnavailableError() {
verify(mBraintree, times(1)).sendAnalyticsEvent("sdk.exit.server-unavailable");
}

/* helpers */
private void setupActivity() {
String clientToken = new TestClientTokenBuilder().withFakePayPal().withAnalytics().build();
mBraintree = spy(injectBraintree(mContext, clientToken, clientToken));
Expand All @@ -153,5 +169,4 @@ private void fillInCreditCard() {
onView(withHint("Postal Code")).perform(typeText("12345"));
onView(withId(R.id.bt_card_form_submit_button)).perform(click());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,10 @@ public void onBackPressed() {
mAddPaymentMethodViewController.isSubmitting()) {
// noop
} else {
mBraintree.sendAnalyticsEvent("sdk.exit.user-canceled");
if (mBraintree != null) {
mBraintree.sendAnalyticsEvent("sdk.exit.user-canceled");
}

setResult(RESULT_CANCELED);
finish();
}
Expand Down

0 comments on commit 09af7be

Please sign in to comment.