void init() { inflate(getContext(), R.layout.masked_card_view, this); setOrientation(HORIZONTAL); setMinimumWidth(getResources().getDimensionPixelSize(R.dimen.card_widget_min_width)); int paddingPixels = getContext() .getResources().getDimensionPixelSize(R.dimen.masked_card_vertical_padding); setPadding(0, paddingPixels, 0, paddingPixels); mCardIconImageView = findViewById(R.id.masked_icon_view); mCardInformationTextView = findViewById(R.id.masked_card_info_view); mCheckMarkImageView = findViewById(R.id.masked_check_icon); mSelectedColorInt = getThemeAccentColor(getContext()).data; mUnselectedColorInt = getThemeColorControlNormal(getContext()).data; mUnselectedTextColorInt = getThemeTextColorSecondary(getContext()).data; useDefaultColorsIfThemeColorsAreInvisible(); setLightTextColorValues(); initializeCheckMark(); updateCheckMark(); }
@Override public void onClick(View view) { if (!maskedCardView.isSelected()) { maskedCardView.toggleSelected(); updateSelectedIndex(index); } } });
void setMaskedCardData(@NonNull CustomerSource customerSource) { maskedCardView.setCustomerSource(customerSource); }
@Test public void setCustomerSource_withNonCardSource_setsNoData() { CustomerSource customerSource = CustomerSource.fromString(EXAMPLE_JSON_SOURCE_BITCOIN); assertNotNull(customerSource); assertNotNull(customerSource.asSource()); mMaskedCardView.setCustomerSource(customerSource); assertNull(mMaskedCardView.getCardBrand()); assertNull(mMaskedCardView.getLast4()); assertFalse(mMaskedCardView.isSelected()); }
@Test public void setCard_setsCorrectData() { Card card = new Card(CardInputTestActivity.VALID_AMEX_NO_SPACES, 12, 50, "1234"); mMaskedCardView.setCard(card); assertEquals("0005", mMaskedCardView.getLast4()); assertEquals(Card.AMERICAN_EXPRESS, mMaskedCardView.getCardBrand()); assertFalse(mMaskedCardView.isSelected()); }
@Test public void toggleSelected_switchesState() { CustomerSource customerSource = CustomerSource.fromString(JSON_CARD); assertNotNull(customerSource); mMaskedCardView.setCustomerSource(customerSource); assertFalse(mMaskedCardView.isSelected()); mMaskedCardView.toggleSelected(); assertTrue(mMaskedCardView.isSelected()); assertEquals(View.VISIBLE, mSelectedImageView.getVisibility()); mMaskedCardView.toggleSelected(); assertFalse(mMaskedCardView.isSelected()); assertEquals(View.INVISIBLE, mSelectedImageView.getVisibility()); }
@Test public void setSelected_changesCheckMarkVisibility() { CustomerSource customerSource = CustomerSource.fromString(JSON_CARD); assertNotNull(customerSource); mMaskedCardView.setCustomerSource(customerSource); assertFalse(mMaskedCardView.isSelected()); assertEquals(View.INVISIBLE, mSelectedImageView.getVisibility()); mMaskedCardView.setSelected(true); assertTrue(mMaskedCardView.isSelected()); assertEquals(View.VISIBLE, mSelectedImageView.getVisibility()); }
@Test public void setSourceCardData_withCardSource_setsCorrectData() { Source source = Source.fromString(EXAMPLE_JSON_CARD_SOURCE); assertNotNull(source); assertTrue(source.getSourceTypeModel() instanceof SourceCardData); SourceCardData sourceCardData = (SourceCardData) source.getSourceTypeModel(); assertNotNull(sourceCardData); mMaskedCardView.setSourceCardData(sourceCardData); assertEquals(Card.VISA, mMaskedCardView.getCardBrand()); assertEquals("4242", mMaskedCardView.getLast4()); assertFalse(mMaskedCardView.isSelected()); }
/** * Set the card data displayed using a {@link CustomerSource} object. If the object * is not a {@link Source} object that represents a card and the object returns {@code null} * from its {@link CustomerSource#asCard()} method, then no values will be set on this control. * * @param customerSource the {@link CustomerSource} to be partially displayed */ void setCustomerSource(@NonNull CustomerSource customerSource) { Source source = customerSource.asSource(); if (source != null && source.getSourceTypeModel() != null && Source.CARD.equals(source.getType()) && source.getSourceTypeModel() instanceof SourceCardData) { SourceCardData sourceCardData = (SourceCardData) source.getSourceTypeModel(); setSourceCardData(sourceCardData); return; } Card card = customerSource.asCard(); if (card != null) { setCard(card); } }
/** * Toggle the view from selected to unselected or vice-versa. */ void toggleSelected() { setSelected(!mIsSelected); }
@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); } }
@Before public void setup() { ActivityController<CardInputTestActivity> activityController = Robolectric.buildActivity(CardInputTestActivity.class).create().start().resume(); mMaskedCardView = activityController.get().getMaskedCardView(); mSelectedImageView = mMaskedCardView.findViewById(R.id.masked_check_icon); Calendar expirationCalendar = Calendar.getInstance(); expirationCalendar.set(Calendar.MONTH, Calendar.DECEMBER); expirationCalendar.set(Calendar.YEAR, 2050); Calendar nowCalendar = Calendar.getInstance(); assertTrue("These tests assume that an expiry date of December 2050 is valid.", expirationCalendar.after(nowCalendar)); }
private void useDefaultColorsIfThemeColorsAreInvisible() { mSelectedColorInt = ViewUtils.isColorTransparent(mSelectedColorInt) ? ContextCompat.getColor(getContext(), R.color.accent_color_default) : mSelectedColorInt; mUnselectedColorInt = ViewUtils.isColorTransparent(mUnselectedColorInt) ? ContextCompat.getColor(getContext(), R.color.control_normal_color_default) : mUnselectedColorInt; mUnselectedTextColorInt = ViewUtils.isColorTransparent(mUnselectedTextColorInt) ? ContextCompat.getColor(getContext(), R.color.color_text_secondary_default) : mUnselectedTextColorInt; }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(R.style.StripeDefaultTheme); mCardInputWidget = new CardInputWidget(this); mCardMultilineWidget = new CardMultilineWidget(this, true); mNoZipCardMulitlineWidget = new CardMultilineWidget(this, false); mMaskedCardView = new MaskedCardView(this); LinearLayout linearLayout = new LinearLayout(this); linearLayout.addView(mCardInputWidget); linearLayout.addView(mCardMultilineWidget); linearLayout.addView(mNoZipCardMulitlineWidget); linearLayout.addView(mMaskedCardView); setContentView(linearLayout); }
@Test public void setCustomerSource_withCardSource_setsCorrectData() { CustomerSource customerSource = CustomerSource.fromString(EXAMPLE_JSON_CARD_SOURCE); assertNotNull(customerSource); assertNotNull(customerSource.asSource()); mMaskedCardView.setCustomerSource(customerSource); assertEquals(Card.VISA, mMaskedCardView.getCardBrand()); assertEquals("4242", mMaskedCardView.getLast4()); assertFalse(mMaskedCardView.isSelected()); }
void setSelected(boolean selected) { maskedCardView.setSelected(selected); } }
private void updateDrawable( @DrawableRes int resourceId, @NonNull ImageView imageView, boolean isCheckMark) { final Drawable icon = ContextCompat.getDrawable(getContext(), resourceId); @ColorInt int tintColor = mIsSelected || isCheckMark ? mSelectedColorInt : mUnselectedColorInt; Drawable compatIcon = DrawableCompat.wrap(icon); DrawableCompat.setTint(compatIcon.mutate(), tintColor); imageView.setImageDrawable(compatIcon); }
@Test public void setCustomerSource_withCardObject_setsCorrectData() { CustomerSource customerSource = CustomerSource.fromString(JSON_CARD); assertNotNull(customerSource); assertNotNull(customerSource.asCard()); assertNull(customerSource.asSource()); mMaskedCardView.setCustomerSource(customerSource); assertEquals(Card.MASTERCARD, mMaskedCardView.getCardBrand()); assertEquals("5555", mMaskedCardView.getLast4()); assertFalse(mMaskedCardView.isSelected()); }