Skip to content

Commit

Permalink
add checks (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksun2-stripe authored Jun 18, 2018
1 parent 196ab6c commit 6670d21
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ void setCustomerSourceList(@NonNull List<CustomerSource> sourceList) {
}

void updateCustomer(@NonNull Customer customer) {
mCustomerSourceList = customer.getSources();
mCustomerSourceList.clear();
CustomerSource[] customerSources = new CustomerSource[customer.getSources().size()];
addCustomerSourceIfSupported(customer.getSources().toArray(customerSources));
String sourceId = customer.getDefaultSource();
if (sourceId == null) {
updateSelectedIndex(NO_SELECTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ private void initializeCheckMark() {
}

private void updateBrandIcon() {
if (!TEMPLATE_RESOURCE_MAP.containsKey(mCardBrand)) {
return;
}
@DrawableRes int iconResourceId = TEMPLATE_RESOURCE_MAP.get(mCardBrand);
updateDrawable(iconResourceId, mCardIconImageView, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
import android.support.v7.widget.RecyclerView;

import com.stripe.android.BuildConfig;
import com.stripe.android.model.Card;
import com.stripe.android.model.Customer;
import com.stripe.android.model.CustomerSource;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -86,6 +89,23 @@ public void updateCustomer_removesExistingSourcesAndAddsAllCustomerSources() {
assertEquals(2, mMaskedCardAdapter.getItemCount());
assertNotNull(mMaskedCardAdapter.getSelectedSource());
assertEquals(customer.getDefaultSource(), mMaskedCardAdapter.getSelectedSource().getId());
verify(mAdapterDataObserver, times(3)).onChanged();
verify(mAdapterDataObserver, times(4)).onChanged();
}

@Test
public void updateCustomer_filtersOutNonCardSources() {
List<CustomerSource> customerSourceList = new ArrayList<CustomerSource>();
CustomerSource cardCustomerSource = Mockito.mock(CustomerSource.class);
Card card = Mockito.mock(Card.class);
Mockito.when(cardCustomerSource.asCard()).thenReturn(card);
CustomerSource nonCardCustomerSource = Mockito.mock(CustomerSource.class);
customerSourceList.add(cardCustomerSource);
customerSourceList.add(nonCardCustomerSource);
Customer customer = Mockito.mock(Customer.class);
Mockito.when(customer.getSources()).thenReturn(customerSourceList);

mMaskedCardAdapter.updateCustomer(customer);

assertEquals(1, mMaskedCardAdapter.getItemCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.support.v4.graphics.ColorUtils;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.stripe.android.BuildConfig;
Expand All @@ -15,6 +14,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.android.controller.ActivityController;
Expand Down Expand Up @@ -157,4 +157,12 @@ public void toggleSelected_switchesState() {
assertFalse(mMaskedCardView.isSelected());
assertEquals(View.INVISIBLE, mSelectedImageView.getVisibility());
}

@Test
public void whenSourceNotCard_doesNotCrash() {
SourceCardData sourceCardData = Mockito.mock(SourceCardData.class);
Mockito.when(sourceCardData.getBrand()).thenReturn("unrecognized_brand");
Mockito.when(sourceCardData.getLast4()).thenReturn("");
mMaskedCardView.setSourceCardData(sourceCardData);
}
}

0 comments on commit 6670d21

Please sign in to comment.