void setCustomerSourceList(@NonNull List<CustomerSource> sourceList) { mCustomerSourceList.clear(); CustomerSource[] customerSources = new CustomerSource[sourceList.size()]; addCustomerSourceIfSupported(sourceList.toArray(customerSources)); }
void updateCustomer(@NonNull Customer customer) { mCustomerSourceList.clear(); CustomerSource[] customerSources = new CustomerSource[customer.getSources().size()]; addCustomerSourceIfSupported(customer.getSources().toArray(customerSources)); String sourceId = customer.getDefaultSource(); if (sourceId == null) { updateSelectedIndex(NO_SELECTION); } else { setSelectedSource(sourceId); } notifyDataSetChanged(); }
@Test public void addSources_onlyDisplaysCards() { CustomerSource cardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE); CustomerSource bitcoinSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_SOURCE_BITCOIN); mMaskedCardAdapter.addCustomerSourceIfSupported(cardSource, bitcoinSource); assertEquals(1, mMaskedCardAdapter.getItemCount()); verify(mAdapterDataObserver).onChanged(); }
@Test public void setSelection_changesSelection() { CustomerSource cardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE); CustomerSource secondCardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE_SECOND); mMaskedCardAdapter.addCustomerSourceIfSupported(cardSource, secondCardSource); assertEquals(2, mMaskedCardAdapter.getItemCount()); verify(mAdapterDataObserver, times(1)).onChanged(); assertNull(mMaskedCardAdapter.getSelectedSource()); assertNotNull(secondCardSource); assertNotNull(secondCardSource.getId()); mMaskedCardAdapter.setSelectedSource(secondCardSource.getId()); verify(mAdapterDataObserver, times(2)).onChanged(); assertNotNull(mMaskedCardAdapter.getSelectedSource()); assertEquals(secondCardSource.getId(), mMaskedCardAdapter.getSelectedSource().getId()); }
@Test public void updateCustomer_removesExistingSourcesAndAddsAllCustomerSources() { CustomerSource cardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE_SECOND); mMaskedCardAdapter.addCustomerSourceIfSupported(cardSource); assertEquals(1, mMaskedCardAdapter.getItemCount()); assertNull(mMaskedCardAdapter.getSelectedSource()); Customer customer = Customer.fromString(PaymentMethodsActivityTest.TEST_CUSTOMER_OBJECT_WITH_SOURCES); assertNotNull(customer); mMaskedCardAdapter.updateCustomer(customer); assertEquals(2, mMaskedCardAdapter.getItemCount()); assertNotNull(mMaskedCardAdapter.getSelectedSource()); assertEquals(customer.getDefaultSource(), mMaskedCardAdapter.getSelectedSource().getId()); verify(mAdapterDataObserver, times(4)).onChanged(); }