@Test public void getAllAvailablePurchases() throws Exception { final Bundle getSubscriptionsResponse = new Bundle(); getSubscriptionsResponse.putInt("RESPONSE_CODE", RESULT_OK); getSubscriptionsResponse.putStringArrayList("DETAILS_LIST", new ArrayList<>(Collections.singletonList(getSkuDetails(InAppPurchase.SmartReceiptsPlus)))); when(inAppBillingService.getSkuDetails(eq(3), eq(packageName), eq("subs"), bundleCaptor.capture())).thenReturn(getSubscriptionsResponse); final Bundle getConsumablePurchasesResponse = new Bundle(); getConsumablePurchasesResponse.putInt("RESPONSE_CODE", RESULT_OK); getConsumablePurchasesResponse.putStringArrayList("DETAILS_LIST", new ArrayList<>(Collections.singletonList(getSkuDetails(InAppPurchase.OcrScans50)))); when(inAppBillingService.getSkuDetails(eq(3), eq(packageName), eq("inapp"), bundleCaptor.capture())).thenReturn(getConsumablePurchasesResponse); purchaseManager.getAllAvailablePurchases().test() .assertValue(new HashSet<>(Arrays.asList(getSkuDetailsAsAvailablePurchase(InAppPurchase.SmartReceiptsPlus), getSkuDetailsAsAvailablePurchase(InAppPurchase.OcrScans50)))) .assertComplete() .assertNoErrors(); verify(purchaseWallet, never()).addLocalInAppPurchaseToWallet(any(ManagedProduct.class)); verify(purchaseWallet, never()).updateLocalInAppPurchasesInWallet(anySetOf(ManagedProduct.class)); }
@NonNull public Observable<Set<AvailablePurchase>> getAllAvailablePurchases() { return Observable.combineLatest(getAvailableConsumablePurchases(), getAvailableSubscriptions(), (BiFunction<Set<AvailablePurchase>, Set<AvailablePurchase>, Set<AvailablePurchase>>) (consumablePurchases, subscriptions) -> { final HashSet<AvailablePurchase> combinedSet = new HashSet<>(); combinedSet.addAll(consumablePurchases); combinedSet.addAll(subscriptions); return combinedSet; }) .map(inAppPurchases -> { final Set<AvailablePurchase> trimmedInAppPurchases = new HashSet<>(); for (final AvailablePurchase availablePurchase : inAppPurchases) { if (availablePurchase.getInAppPurchase() != null) { trimmedInAppPurchases.add(availablePurchase); } else { Logger.debug(PurchaseManager.this, "Omitting {} from available purchases as we're tracking it as owned.", availablePurchase.getInAppPurchase()); } } return trimmedInAppPurchases; }) .subscribeOn(subscribeOnScheduler); }
PurchaseManager { @NonNull public Observable<Set<AvailablePurchase>> getAllAvailablePurchases() { return Observable.combineLatest(getAvailableConsumablePurchases(), getAvailableSubscriptions(), (BiFunction<Set<AvailablePurchase>, Set<AvailablePurchase>, Set<AvailablePurchase>>) (consumablePurchases, subscriptions) -> { final HashSet<AvailablePurchase> combinedSet = new HashSet<>(); combinedSet.addAll(consumablePurchases); combinedSet.addAll(subscriptions); return combinedSet; }) .map(inAppPurchases -> { final Set<AvailablePurchase> trimmedInAppPurchases = new HashSet<>(); for (final AvailablePurchase availablePurchase : inAppPurchases) { if (availablePurchase.getInAppPurchase() != null) { trimmedInAppPurchases.add(availablePurchase); } else { Logger.debug(PurchaseManager.this, "Omitting {} from available purchases as we're tracking it as owned.", availablePurchase.getInAppPurchase()); } } return trimmedInAppPurchases; }) .subscribeOn(subscribeOnScheduler); } }
PurchaseManager { @NonNull public Observable<Set<AvailablePurchase>> getAllAvailablePurchases() { return Observable.combineLatest(getAvailableConsumablePurchases(), getAvailableSubscriptions(), (BiFunction<Set<AvailablePurchase>, Set<AvailablePurchase>, Set<AvailablePurchase>>) (consumablePurchases, subscriptions) -> { final HashSet<AvailablePurchase> combinedSet = new HashSet<>(); combinedSet.addAll(consumablePurchases); combinedSet.addAll(subscriptions); return combinedSet; }) .map(inAppPurchases -> { final Set<AvailablePurchase> trimmedInAppPurchases = new HashSet<>(); for (final AvailablePurchase availablePurchase : inAppPurchases) { if (availablePurchase.getInAppPurchase() != null) { trimmedInAppPurchases.add(availablePurchase); } else { Logger.debug(PurchaseManager.this, "Omitting {} from available purchases as we're tracking it as owned.", availablePurchase.getInAppPurchase()); } } return trimmedInAppPurchases; }) .subscribeOn(subscribeOnScheduler); } @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); }
PurchaseManager { @NonNull public Observable<Set<AvailablePurchase>> getAllAvailablePurchases() { return Observable.combineLatest(getAvailableConsumablePurchases(), getAvailableSubscriptions(), (BiFunction<Set<AvailablePurchase>, Set<AvailablePurchase>, Set<AvailablePurchase>>) (consumablePurchases, subscriptions) -> { final HashSet<AvailablePurchase> combinedSet = new HashSet<>(); combinedSet.addAll(consumablePurchases); combinedSet.addAll(subscriptions); return combinedSet; }) .map(inAppPurchases -> { final Set<AvailablePurchase> trimmedInAppPurchases = new HashSet<>(); for (final AvailablePurchase availablePurchase : inAppPurchases) { if (availablePurchase.getInAppPurchase() != null) { trimmedInAppPurchases.add(availablePurchase); } else { Logger.debug(PurchaseManager.this, "Omitting {} from available purchases as we're tracking it as owned.", availablePurchase.getInAppPurchase()); } } return trimmedInAppPurchases; }) .subscribeOn(subscribeOnScheduler); } @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); }
PurchaseManager { @NonNull public Observable<Set<AvailablePurchase>> getAllAvailablePurchases() { return Observable.combineLatest(getAvailableConsumablePurchases(), getAvailableSubscriptions(), (BiFunction<Set<AvailablePurchase>, Set<AvailablePurchase>, Set<AvailablePurchase>>) (consumablePurchases, subscriptions) -> { final HashSet<AvailablePurchase> combinedSet = new HashSet<>(); combinedSet.addAll(consumablePurchases); combinedSet.addAll(subscriptions); return combinedSet; }) .map(inAppPurchases -> { final Set<AvailablePurchase> trimmedInAppPurchases = new HashSet<>(); for (final AvailablePurchase availablePurchase : inAppPurchases) { if (availablePurchase.getInAppPurchase() != null) { trimmedInAppPurchases.add(availablePurchase); } else { Logger.debug(PurchaseManager.this, "Omitting {} from available purchases as we're tracking it as owned.", availablePurchase.getInAppPurchase()); } } return trimmedInAppPurchases; }) .subscribeOn(subscribeOnScheduler); } @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; }