/** * Launch the {@link PaymentMethodsActivity} to allow the user to select a payment method, * or to add a new one. */ public void presentPaymentMethodSelection() { Intent paymentMethodsIntent = PaymentMethodsActivity.newIntent(mHostActivity); paymentMethodsIntent.putExtra(EXTRA_PAYMENT_SESSION_ACTIVE, true); mHostActivity.startActivityForResult(paymentMethodsIntent, PAYMENT_METHOD_REQUEST); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_payment_methods); mProgressBar = findViewById(R.id.payment_methods_progress_bar); mRecyclerView = findViewById(R.id.payment_methods_recycler); View addCardView = findViewById(R.id.payment_methods_add_payment_container); Toolbar toolbar = findViewById(R.id.payment_methods_toolbar); setSupportActionBar(toolbar); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); boolean waitForProxy = getIntent().getBooleanExtra(EXTRA_PROXY_DELAY, false); if (!waitForProxy) { initializeCustomerSourceData(); mStartedFromPaymentSession = getIntent().hasExtra(EXTRA_PAYMENT_SESSION_ACTIVE);
private void updateAdapterWithCustomer(@NonNull Customer customer) { if (mMaskedCardAdapter == null) { createListFromCustomerSources(); if (mCustomer == null) { return; } } mMaskedCardAdapter.updateCustomer(customer); setCommunicatingProgress(false); }
@Test public void onClickAddSourceView_whenStartedFromPaymentSession_launchesActivityWithLog() { Intent intent = PaymentMethodsActivity .newIntent(ApplicationProvider.getApplicationContext()) .putExtra(EXTRA_PROXY_DELAY, true) .putExtra(EXTRA_PAYMENT_SESSION_ACTIVE, true); mPaymentMethodsActivity = createActivity(intent); mPaymentMethodsActivity.setCustomerSessionProxy(mCustomerSessionProxy); mShadowActivity = Shadows.shadowOf(mPaymentMethodsActivity); mAddCardView = mPaymentMethodsActivity.findViewById(R.id.payment_methods_add_payment_container); Customer customer = Customer.fromString(CustomerSessionTest.FIRST_TEST_CUSTOMER_OBJECT); when(mCustomerSessionProxy.getCachedCustomer()).thenReturn(customer); mPaymentMethodsActivity.initializeCustomerSourceData(); mAddCardView.performClick(); ShadowActivity.IntentForResult intentForResult = mShadowActivity.getNextStartedActivityForResult(); assertNotNull(intentForResult); assertEquals(AddSourceActivity.class.getName(), intentForResult.intent.getComponent().getClassName()); assertTrue(intentForResult.intent.hasExtra(EXTRA_PAYMENT_SESSION_ACTIVE)); }
@Before public void setup() { MockitoAnnotations.initMocks(this); Intent intent = PaymentMethodsActivity.newIntent(ApplicationProvider.getApplicationContext()) .putExtra(EXTRA_PROXY_DELAY, true); mPaymentMethodsActivity = createActivity(intent); mPaymentMethodsActivity.setCustomerSessionProxy(mCustomerSessionProxy); mShadowActivity = Shadows.shadowOf(mPaymentMethodsActivity); mProgressBar = mPaymentMethodsActivity.findViewById(R.id.payment_methods_progress_bar); mRecyclerView = mPaymentMethodsActivity.findViewById(R.id.payment_methods_recycler); mAddCardView = mPaymentMethodsActivity.findViewById(R.id.payment_methods_add_payment_container); }
@Test public void onActivityResult_withValidSource_refreshesCustomer() { Customer customer = Customer.fromString(CustomerSessionTest.FIRST_TEST_CUSTOMER_OBJECT); when(mCustomerSessionProxy.getCachedCustomer()).thenReturn(customer); mPaymentMethodsActivity.initializeCustomerSourceData(); Source source = Source.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE); assertNotNull(source); Intent resultIntent = new Intent(); resultIntent.putExtra(AddSourceActivity.EXTRA_NEW_SOURCE, source.toString()); ArgumentCaptor<CustomerSession.CustomerRetrievalListener> listenerArgumentCaptor = ArgumentCaptor.forClass(CustomerSession.CustomerRetrievalListener.class); mPaymentMethodsActivity.onActivityResult(REQUEST_CODE_ADD_CARD, RESULT_OK, resultIntent); assertEquals(View.VISIBLE, mProgressBar.getVisibility()); verify(mCustomerSessionProxy).updateCurrentCustomer(listenerArgumentCaptor.capture()); CustomerSession.CustomerRetrievalListener listener = listenerArgumentCaptor.getValue(); assertNotNull(listener); // Note - this doesn't make sense as the actual update; just testing that the customer // changes Customer updatedCustomer = Customer.fromString(TEST_CUSTOMER_OBJECT_WITH_SOURCES); assertNotNull(updatedCustomer); listener.onCustomerRetrieved(updatedCustomer); assertEquals(View.GONE, mProgressBar.getVisibility()); assertEquals(2, mRecyclerView.getAdapter().getItemCount()); }
mPaymentMethodsActivity.initializeCustomerSourceData(); ArgumentCaptor.forClass(CustomerSession.CustomerRetrievalListener.class); mPaymentMethodsActivity.onOptionsItemSelected(menuItem); assertTrue(mPaymentMethodsActivity.isFinishing()); assertEquals(RESULT_OK, mShadowActivity.getResultCode()); Intent intent = mShadowActivity.getResultIntent();
@Test public void onClickAddSourceView_withoutPaymentSessoin_launchesAddSourceActivityWithoutLog() { Customer customer = Customer.fromString(CustomerSessionTest.FIRST_TEST_CUSTOMER_OBJECT); when(mCustomerSessionProxy.getCachedCustomer()).thenReturn(customer); mPaymentMethodsActivity.initializeCustomerSourceData(); mAddCardView.performClick(); ShadowActivity.IntentForResult intentForResult = mShadowActivity.getNextStartedActivityForResult(); assertNotNull(intentForResult); assertEquals(AddSourceActivity.class.getName(), intentForResult.intent.getComponent().getClassName()); assertFalse(intentForResult.intent.hasExtra(EXTRA_PAYMENT_SESSION_ACTIVE)); }
@VisibleForTesting void initializeCustomerSourceData() { Customer cachedCustomer = mCustomerSessionProxy == null ? CustomerSession.getInstance().getCachedCustomer() : mCustomerSessionProxy.getCachedCustomer(); if (cachedCustomer != null) { mCustomer = cachedCustomer; createListFromCustomerSources(); } else { getCustomerFromSession(); } }
private void setSelectionAndFinish() { if (mMaskedCardAdapter == null || mMaskedCardAdapter.getSelectedSource() == null) { cancelAndFinish(); return; listener); setCommunicatingProgress(true);
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.add_source_menu, menu); menu.findItem(R.id.action_save).setEnabled(!mCommunicating); return true; }
Customer customer = Customer.fromString(CustomerSessionTest.FIRST_TEST_CUSTOMER_OBJECT); when(mCustomerSessionProxy.getCachedCustomer()).thenReturn(customer); mPaymentMethodsActivity.initializeCustomerSourceData(); ArgumentCaptor.forClass(CustomerSession.CustomerRetrievalListener.class); mPaymentMethodsActivity.onActivityResult(REQUEST_CODE_ADD_CARD, RESULT_OK, resultIntent); assertEquals(View.VISIBLE, mProgressBar.getVisibility()); verify(mCustomerSessionProxy).updateCurrentCustomer(listenerArgumentCaptor.capture());
@Test public void onCreate_withCachedCustomer_showsUi() { Customer customer = Customer.fromString(CustomerSessionTest.FIRST_TEST_CUSTOMER_OBJECT); when(mCustomerSessionProxy.getCachedCustomer()).thenReturn(customer); mPaymentMethodsActivity.initializeCustomerSourceData(); assertNotNull(mProgressBar); assertNotNull(mRecyclerView); assertNotNull(mAddCardView); assertEquals(View.VISIBLE, mAddCardView.getVisibility()); assertEquals(View.VISIBLE, mRecyclerView.getVisibility()); assertEquals(View.GONE, mProgressBar.getVisibility()); }
private void finishWithSelection(String selectedSourceId) { CustomerSource customerSource = mCustomer.getSourceById(selectedSourceId); if (customerSource != null) { Intent intent = new Intent(); intent.putExtra(EXTRA_SELECTED_PAYMENT, customerSource.toString()); setResult(RESULT_OK, intent); } else { setResult(RESULT_CANCELED); } finish(); }
private void launchWithCustomer() { Intent payIntent = PaymentMethodsActivity.newIntent(this); startActivityForResult(payIntent, REQUEST_CODE_SELECT_SOURCE); }
@Test public void onCreate_withoutCacheCustomer_callsApiAndDisplaysProgressBarWhileWaiting() { Customer customer = Customer.fromString(CustomerSessionTest.FIRST_TEST_CUSTOMER_OBJECT); assertNotNull(customer); when(mCustomerSessionProxy.getCachedCustomer()).thenReturn(null); ArgumentCaptor<CustomerSession.CustomerRetrievalListener> listenerArgumentCaptor = ArgumentCaptor.forClass(CustomerSession.CustomerRetrievalListener.class); assertNotNull(mProgressBar); assertNotNull(mRecyclerView); assertNotNull(mAddCardView); mPaymentMethodsActivity.initializeCustomerSourceData(); verify(mCustomerSessionProxy).retrieveCurrentCustomer(listenerArgumentCaptor.capture()); assertEquals(View.VISIBLE, mProgressBar.getVisibility()); assertEquals(View.VISIBLE, mAddCardView.getVisibility()); assertEquals(View.VISIBLE, mRecyclerView.getVisibility()); CustomerSession.CustomerRetrievalListener listener = listenerArgumentCaptor.getValue(); assertNotNull(listener); listener.onCustomerRetrieved(customer); assertEquals(View.GONE, mProgressBar.getVisibility()); }