Skip to content

[in_app_purchase_storekit] backfill native tests for more complete test coverage #6209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.13

* Added new native tests for more complete test coverage.

## 0.3.12+1

* Fixes type of error code returned from native code in SKReceiptManager.retrieveReceiptData.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <StoreKit/StoreKit.h>
#import "FIAPRequestHandler.h"
#import "InAppPurchasePlugin.h"

@interface InAppPurchasePlugin ()

// Holding strong references to FIAPRequestHandlers. Remove the handlers from the set after
// the request is finished.
@property(strong, nonatomic, readonly) NSMutableSet *requestHandlers;

// Callback channel to dart used for when a function from the transaction observer is triggered.
@property(strong, nonatomic) FlutterMethodChannel *transactionObserverCallbackChannel;

// Callback channel to dart used for when a function from the transaction observer is triggered.
@property(copy, nonatomic) FIAPRequestHandler * (^handlerFactory)(SKRequest *);

// Convenience initializer with dependancy injection
- (instancetype)initWithReceiptManager:(FIAPReceiptManager *)receiptManager
handlerFactory:(FIAPRequestHandler * (^)(SKRequest *))handlerFactory;

// Transaction observer methods
- (void)handleTransactionsUpdated:(NSArray<SKPaymentTransaction *> *)transactions;
- (void)handleTransactionsRemoved:(NSArray<SKPaymentTransaction *> *)transactions;
- (void)handleTransactionRestoreFailed:(NSError *)error;
- (void)restoreCompletedTransactionsFinished;
- (BOOL)shouldAddStorePayment:(SKPayment *)payment product:(SKProduct *)product;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@
#import "FIAPReceiptManager.h"
#import "FIAPRequestHandler.h"
#import "FIAPaymentQueueHandler.h"
#import "InAppPurchasePlugin+TestOnly.h"

@interface InAppPurchasePlugin ()

// Holding strong references to FIAPRequestHandlers. Remove the handlers from the set after
// the request is finished.
@property(strong, nonatomic, readonly) NSMutableSet *requestHandlers;

// After querying the product, the available products will be saved in the map to be used
// for purchase.
@property(strong, nonatomic, readonly) NSMutableDictionary *productsCache;

// Callback channel to dart used for when a function from the transaction observer is triggered.
@property(strong, nonatomic, readonly) FlutterMethodChannel *transactionObserverCallbackChannel;

// Callback channel to dart used for when a function from the payment queue delegate is triggered.
@property(strong, nonatomic, readonly) FlutterMethodChannel *paymentQueueDelegateCallbackChannel;
@property(strong, nonatomic, readonly) NSObject<FlutterPluginRegistrar> *registrar;
Expand Down Expand Up @@ -52,6 +46,16 @@ - (instancetype)initWithReceiptManager:(FIAPReceiptManager *)receiptManager {
_receiptManager = receiptManager;
_requestHandlers = [NSMutableSet new];
_productsCache = [NSMutableDictionary new];
_handlerFactory = ^FIAPRequestHandler *(SKRequest *request) {
return [[FIAPRequestHandler alloc] initWithRequest:request];
};
return self;
}

- (instancetype)initWithReceiptManager:(FIAPReceiptManager *)receiptManager
handlerFactory:(FIAPRequestHandler * (^)(SKRequest *))handlerFactory {
self = [self initWithReceiptManager:receiptManager];
_handlerFactory = [handlerFactory copy];
return self;
}

Expand Down Expand Up @@ -117,7 +121,7 @@ - (void)startProductRequestProductIdentifiers:(NSArray<NSString *> *)productIden
FlutterError *_Nullable))completion {
SKProductsRequest *request =
[self getProductRequestWithIdentifiers:[NSSet setWithArray:productIdentifiers]];
FIAPRequestHandler *handler = [[FIAPRequestHandler alloc] initWithRequest:request];
FIAPRequestHandler *handler = self.handlerFactory(request);
[self.requestHandlers addObject:handler];
__weak typeof(self) weakSelf = self;

Expand Down Expand Up @@ -271,7 +275,7 @@ - (void)refreshReceiptReceiptProperties:(nullable NSDictionary *)receiptProperti
request = [self getRefreshReceiptRequest:nil];
}

FIAPRequestHandler *handler = [[FIAPRequestHandler alloc] initWithRequest:request];
FIAPRequestHandler *handler = self.handlerFactory(request);
[self.requestHandlers addObject:handler];
__weak typeof(self) weakSelf = self;
[handler startProductRequestWithCompletionHandler:^(SKProductsResponse *_Nullable response,
Expand All @@ -282,6 +286,7 @@ - (void)refreshReceiptReceiptProperties:(nullable NSDictionary *)receiptProperti
message:error.localizedDescription
details:error.description];
completion(requestError);
return;
}
completion(nil);
[weakSelf.requestHandlers removeObject:handler];
Expand Down
Loading