private void onShippingInfoSubmitted() { ShippingInfoWidget shippingInfoWidget = findViewById(R.id.shipping_info_widget); ShippingInformation shippingInformation = shippingInfoWidget.getShippingInformation(); if (shippingInformation != null) { mShippingInformationSubmitted = shippingInformation; setCommunicatingProgress(true); broadcastShippingInfoSubmitted(shippingInformation); } }
private void onShippingMethodSave() { SelectShippingMethodWidget selectShippingMethodWidget = findViewById(R.id.select_shipping_method_widget); ShippingMethod shippingMethod = selectShippingMethodWidget.getSelectedShippingMethod(); mPaymentSessionData.setShippingMethod(shippingMethod); Intent intent = new Intent(); intent.putExtra(PAYMENT_SESSION_DATA_KEY, mPaymentSessionData); setResult(RESULT_OK, intent); finish(); }
@Override public void onReceive(Context context, Intent intent) { boolean isShippingInfoValid = intent.getBooleanExtra( EXTRA_IS_SHIPPING_INFO_VALID, false); if (isShippingInfoValid) { onShippingInfoValidated(); mValidShippingMethods = intent.getParcelableArrayListExtra(EXTRA_VALID_SHIPPING_METHODS); mDefaultShippingMethod = intent.getParcelableExtra(EXTRA_DEFAULT_SHIPPING_METHOD); } else { setCommunicatingProgress(false); String shippingInfoError = intent.getStringExtra(EXTRA_SHIPPING_INFO_ERROR); if (shippingInfoError != null && !shippingInfoError.isEmpty()) { showError(shippingInfoError); } else { showError(getString(R.string.invalid_shipping_information)); } mShippingInformationSubmitted = null; } } };
@Test public void onShippingInfoSave_whenShippingNotPopulated_doesNotFinish() { PaymentSessionConfig paymentSessionConfig = new PaymentSessionConfig.Builder() .build(); Intent intent = new Intent() .putExtra(PAYMENT_SESSION_CONFIG, paymentSessionConfig) .putExtra(PAYMENT_SESSION_DATA_KEY, new PaymentSessionData()); PaymentFlowActivity paymentFlowActivity = createActivity(intent); mShippingInfoWidget = paymentFlowActivity.findViewById(R.id.shipping_info_widget); assertNotNull(mShippingInfoWidget); paymentFlowActivity.onActionSave(); assertFalse(paymentFlowActivity.isFinishing()); }
private void onShippingMethodsReady( @NonNull List<ShippingMethod> validShippingMethods, @Nullable ShippingMethod defaultShippingMethod) { setCommunicatingProgress(false); mPaymentFlowPagerAdapter.setShippingMethods(validShippingMethods, defaultShippingMethod); mPaymentFlowPagerAdapter.setShippingInfoSaved(true); if (hasNextPage()) { mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1); } else { mPaymentSessionData.setShippingInformation(mShippingInformationSubmitted); Intent intent = new Intent(); intent.putExtra(PAYMENT_SESSION_DATA_KEY, mPaymentSessionData); setResult(RESULT_OK, intent); finish(); } }
mViewStub.setLayoutResource(R.layout.activity_shipping_flow); mViewStub.inflate(); mViewPager = findViewById(R.id.shipping_flow_viewpager); final PaymentSessionConfig paymentSessionConfig = getIntent().getParcelableExtra(PAYMENT_SESSION_CONFIG); mPaymentSessionData = getIntent().getParcelableExtra(PAYMENT_SESSION_DATA_KEY); setTitle(mPaymentFlowPagerAdapter.getPageTitle(mViewPager.getCurrentItem()));
@Test public void onShippingInfoSave_whenShippingPopulated_sendsCorrectIntent() { Intent intent = new Intent(); PaymentSessionConfig paymentSessionConfig = new PaymentSessionConfig.Builder() .setPrepopulatedShippingInfo(getExampleShippingInfo()) .build(); intent.putExtra(PAYMENT_SESSION_CONFIG, paymentSessionConfig); PaymentSessionData paymentSessionData = new PaymentSessionData(); intent.putExtra(PAYMENT_SESSION_DATA_KEY, paymentSessionData); PaymentFlowActivity paymentFlowActivity = createActivity(intent); mShippingInfoWidget = paymentFlowActivity.findViewById(R.id.shipping_info_widget); assertNotNull(mShippingInfoWidget); paymentFlowActivity.onActionSave(); ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); verify(mBroadcastReceiver).onReceive(any(Context.class), intentArgumentCaptor.capture()); Intent captured = intentArgumentCaptor.getValue(); assertNotNull(captured); assertEquals( captured.getParcelableExtra(EXTRA_SHIPPING_INFO_DATA), getExampleShippingInfo()); }
@Override protected void onActionSave() { if (mPaymentFlowPagerAdapter.getPageAt( mViewPager.getCurrentItem()).equals(PaymentFlowPagerEnum.SHIPPING_INFO)) { onShippingInfoSubmitted(); } else { onShippingMethodSave(); } }
@Test public void launchPaymentFlowActivity_withHideShippingInfoConfig_hidesShippingInfoView() { PaymentSessionConfig paymentSessionConfig = new PaymentSessionConfig.Builder() .setShippingInfoRequired(false) .build(); Intent intent = new Intent() .putExtra(PAYMENT_SESSION_DATA_KEY, new PaymentSessionData()) .putExtra(PAYMENT_SESSION_CONFIG, paymentSessionConfig); PaymentFlowActivity paymentFlowActivity = createActivity(intent); assertNull(paymentFlowActivity.findViewById(R.id.shipping_info_widget)); assertNotNull(paymentFlowActivity.findViewById(R.id.select_shipping_method_widget)); }
@Test public void onShippingInfoSaved_whenOnlyShippingInfo_finishWithSuccess() { Intent intent = new Intent(ApplicationProvider.getApplicationContext(), PaymentFlowActivity.class); PaymentSessionConfig paymentSessionConfig = new PaymentSessionConfig.Builder() .setPrepopulatedShippingInfo(getExampleShippingInfo()) .setShippingMethodsRequired(false) .build(); intent.putExtra(PAYMENT_SESSION_CONFIG, paymentSessionConfig); PaymentSessionData paymentSessionData = new PaymentSessionData(); intent.putExtra(PAYMENT_SESSION_DATA_KEY, paymentSessionData); PaymentFlowActivity paymentFlowActivity = createActivity(intent); final ShadowActivity shadowActivity = shadowOf(paymentFlowActivity); // valid result paymentFlowActivity.onActionSave(); Intent onShippingInfoProcessedValid = new Intent(EVENT_SHIPPING_INFO_PROCESSED); onShippingInfoProcessedValid.putExtra(EXTRA_IS_SHIPPING_INFO_VALID, true); LocalBroadcastManager.getInstance(ApplicationProvider.getApplicationContext()) .sendBroadcast(onShippingInfoProcessedValid); Intent shippingInfoSaved = new Intent(EVENT_SHIPPING_INFO_SAVED); LocalBroadcastManager.getInstance(ApplicationProvider.getApplicationContext()) .sendBroadcast(shippingInfoSaved); assertTrue(paymentFlowActivity.isFinishing()); assertEquals(shadowActivity.getResultCode(), Activity.RESULT_OK); PaymentSessionData resultSessionData = shadowActivity.getResultIntent().getExtras() .getParcelable(PAYMENT_SESSION_DATA_KEY); assertEquals(resultSessionData.getShippingInformation(), getExampleShippingInfo()); }
@Test public void onShippingInfoProcessed_whenValidShippingInfoSubmitted_rendersCorrectly() { Intent intent = new Intent(ApplicationProvider.getApplicationContext(), PaymentFlowActivity.class); PaymentSessionConfig paymentSessionConfig = new PaymentSessionConfig.Builder() .setPrepopulatedShippingInfo(getExampleShippingInfo()) .build(); intent.putExtra(PAYMENT_SESSION_CONFIG, paymentSessionConfig); PaymentSessionData paymentSessionData = new PaymentSessionData(); intent.putExtra(PAYMENT_SESSION_DATA_KEY, paymentSessionData); PaymentFlowActivity paymentFlowActivity = createActivity(intent); // valid result paymentFlowActivity.onActionSave(); Intent onShippingInfoProcessedValid = new Intent(EVENT_SHIPPING_INFO_PROCESSED); onShippingInfoProcessedValid.putExtra(EXTRA_IS_SHIPPING_INFO_VALID, true); ArrayList<ShippingMethod> shippingMethods = new ArrayList<>(); shippingMethods.add(new ShippingMethod("label", "id", 0, "USD")); onShippingInfoProcessedValid.putExtra(EXTRA_VALID_SHIPPING_METHODS, shippingMethods); LocalBroadcastManager.getInstance(ApplicationProvider.getApplicationContext()) .sendBroadcast(onShippingInfoProcessedValid); assertEquals(View.VISIBLE, paymentFlowActivity.mProgressBar.getVisibility()); Intent shippingInfoSaved = new Intent(EVENT_SHIPPING_INFO_SAVED); LocalBroadcastManager.getInstance(ApplicationProvider.getApplicationContext()) .sendBroadcast(shippingInfoSaved); assertEquals(View.GONE, paymentFlowActivity.mProgressBar.getVisibility()); }
@Override public void onBackPressed() { if (hasPreviousPage()) { mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1); return; } super.onBackPressed(); } }
@Test public void onShippingInfoSave_whenShippingInfoNotPopulated_doesNotContinue() { Intent intent = new Intent(); PaymentSessionConfig paymentSessionConfig = new PaymentSessionConfig.Builder() .build(); intent.putExtra(PAYMENT_SESSION_CONFIG, paymentSessionConfig); PaymentSessionData paymentSessionData = new PaymentSessionData(); intent.putExtra(PAYMENT_SESSION_DATA_KEY, paymentSessionData); PaymentFlowActivity paymentFlowActivity = createActivity(intent); mShippingInfoWidget = paymentFlowActivity.findViewById(R.id.shipping_info_widget); assertNotNull(mShippingInfoWidget); paymentFlowActivity.onActionSave(); assertFalse(paymentFlowActivity.isFinishing()); assertNotNull(paymentFlowActivity.findViewById(R.id.shipping_info_widget)); }
@Test public void onShippingInfoProcessed_whenInvalidShippingInfoSubmitted_rendersCorrectly() { Intent intent = new Intent(ApplicationProvider.getApplicationContext(), PaymentFlowActivity.class); PaymentSessionConfig paymentSessionConfig = new PaymentSessionConfig.Builder() .setPrepopulatedShippingInfo(getExampleShippingInfo()) .build(); intent.putExtra(PAYMENT_SESSION_CONFIG, paymentSessionConfig); PaymentSessionData paymentSessionData = new PaymentSessionData(); intent.putExtra(PAYMENT_SESSION_DATA_KEY, paymentSessionData); PaymentFlowActivity paymentFlowActivity = createActivity(intent); // invalid result paymentFlowActivity.onActionSave(); assertEquals(paymentFlowActivity.mProgressBar.getVisibility(), View.VISIBLE); Intent onShippingInfoProcessedInvalid = new Intent(EVENT_SHIPPING_INFO_PROCESSED); onShippingInfoProcessedInvalid.putExtra(EXTRA_IS_SHIPPING_INFO_VALID, false); LocalBroadcastManager.getInstance(ApplicationProvider.getApplicationContext()) .sendBroadcast(onShippingInfoProcessedInvalid); assertEquals(paymentFlowActivity.mProgressBar.getVisibility(), View.GONE); }