PurchaseManager { @NonNull @VisibleForTesting Observable<Set<AvailablePurchase>> getAvailableSubscriptions() { return getAvailablePurchases(InAppPurchase.getSubscriptionSkus(), Subscription.GOOGLE_PRODUCT_TYPE); } @Inject  PurchaseManager(@NonNull Context context,
                           @NonNull RemoteSubscriptionManager remoteSubscriptionManager,
                           @NonNull PurchaseWallet purchaseWallet,
                           @NonNull Analytics analytics); @VisibleForTesting  PurchaseManager(@NonNull Context context,
                    @NonNull RemoteSubscriptionManager remoteSubscriptionManager,
                    @NonNull PurchaseWallet purchaseWallet,
                    @NonNull Analytics analytics,
                    @NonNull Scheduler subscribeOnScheduler,
                    @NonNull Scheduler observeOnScheduler); void addEventListener(@NonNull PurchaseEventsListener listener); void removeEventListener(@NonNull PurchaseEventsListener listener); @SuppressLint("CheckResult") @SuppressWarnings("ResultOfMethodCallIgnored") void initialize(@NonNull Application application); void onActivityResumed(@NonNull Activity activity); void onActivityPaused(); void initiatePurchase(@NonNull final InAppPurchase inAppPurchase, @NonNull final PurchaseSource purchaseSource); @VisibleForTesting void sendMockPurchaseRequest(@NonNull InAppPurchase inAppPurchase); boolean onActivityResult(int requestCode, int resultCode, @Nullable Intent data); @NonNull Observable<Set<ManagedProduct>> getAllOwnedPurchases(); @NonNull Observable<Set<AvailablePurchase>> getAllAvailablePurchases(); @NonNull Observable<Set<InAppPurchase>> getAllAvailablePurchaseSkus(); @NonNull Completable consumePurchase(@NonNull final ConsumablePurchase consumablePurchase); static final int REQUEST_CODE; }
@Test public void getAvailableSubscriptionsThrowsRemoteException() throws Exception { final Bundle getSkuDetailsResponse = new Bundle(); getSkuDetailsResponse.putInt("RESPONSE_CODE", RESULT_ERROR); when(inAppBillingService.getSkuDetails(eq(3), eq(packageName), eq("subs"), bundleCaptor.capture())).thenThrow(new RemoteException()); purchaseManager.getAvailableSubscriptions().test() .assertNoValues() .assertNotComplete() .assertError(RemoteException.class); verifyZeroInteractions(purchaseWallet); assertEquals(bundleCaptor.getValue().getStringArrayList("ITEM_ID_LIST"), InAppPurchase.getSubscriptionSkus()); }
@Test public void getAvailableSubscriptionsResponseError() throws Exception { final Bundle getSkuDetailsResponse = new Bundle(); getSkuDetailsResponse.putInt("RESPONSE_CODE", RESULT_ERROR); when(inAppBillingService.getSkuDetails(eq(3), eq(packageName), eq("subs"), bundleCaptor.capture())).thenReturn(getSkuDetailsResponse); purchaseManager.getAvailableSubscriptions().test() .assertNoValues() .assertNotComplete() .assertError(Exception.class); verifyZeroInteractions(purchaseWallet); assertEquals(bundleCaptor.getValue().getStringArrayList("ITEM_ID_LIST"), InAppPurchase.getSubscriptionSkus()); }
@Test public void getAvailableSubscriptionsWithNoneAvailable() throws Exception { final Bundle getSkuDetailsResponse = new Bundle(); getSkuDetailsResponse.putInt("RESPONSE_CODE", RESULT_OK); getSkuDetailsResponse.putStringArrayList("DETAILS_LIST", new ArrayList<>()); when(inAppBillingService.getSkuDetails(eq(3), eq(packageName), eq("subs"), bundleCaptor.capture())).thenReturn(getSkuDetailsResponse); purchaseManager.getAvailableSubscriptions().test() .assertValue(Collections.<AvailablePurchase>emptySet()) .assertComplete() .assertNoErrors(); verify(purchaseWallet, never()).addLocalInAppPurchaseToWallet(any(ManagedProduct.class)); verify(purchaseWallet, never()).updateLocalInAppPurchasesInWallet(anySetOf(ManagedProduct.class)); assertEquals(bundleCaptor.getValue().getStringArrayList("ITEM_ID_LIST"), InAppPurchase.getSubscriptionSkus()); }
@Test public void getAvailableSubscriptionsWithSomeAvailable() throws Exception { final Bundle getSkuDetailsResponse = new Bundle(); getSkuDetailsResponse.putInt("RESPONSE_CODE", RESULT_OK); getSkuDetailsResponse.putStringArrayList("DETAILS_LIST", new ArrayList<>(Collections.singletonList(getSkuDetails(InAppPurchase.SmartReceiptsPlus)))); when(inAppBillingService.getSkuDetails(eq(3), eq(packageName), eq("subs"), bundleCaptor.capture())).thenReturn(getSkuDetailsResponse); purchaseManager.getAvailableSubscriptions().test() .assertValue(Collections.singleton(getSkuDetailsAsAvailablePurchase(InAppPurchase.SmartReceiptsPlus))) .assertComplete() .assertNoErrors(); verify(purchaseWallet, never()).addLocalInAppPurchaseToWallet(any(ManagedProduct.class)); verify(purchaseWallet, never()).updateLocalInAppPurchasesInWallet(anySetOf(ManagedProduct.class)); assertEquals(bundleCaptor.getValue().getStringArrayList("ITEM_ID_LIST"), InAppPurchase.getSubscriptionSkus()); }