Skip to content

Commit b5fe05d

Browse files
authored
[in_app_purchase_storekit] backfill native tests for more complete test coverage (#6209)
Added more native tests for more complete test coverage. Fixes flutter/flutter#143889 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [relevant style guides] and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use `dart format`.) - [x] I signed the [CLA]. - [x] The title of the PR starts with the name of the package surrounded by square brackets, e.g. `[shared_preferences]` - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated `pubspec.yaml` with an appropriate new version according to the [pub versioning philosophy], or this PR is [exempt from version changes]. - [x] I updated `CHANGELOG.md` to add a description of the change, [following repository CHANGELOG style]. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing.
1 parent 6a4e2ff commit b5fe05d

File tree

7 files changed

+383
-13
lines changed

7 files changed

+383
-13
lines changed

packages/in_app_purchase/in_app_purchase_storekit/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.13
2+
3+
* Added new native tests for more complete test coverage.
4+
15
## 0.3.12+1
26

37
* Fixes type of error code returned from native code in SKReceiptManager.retrieveReceiptData.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import <StoreKit/StoreKit.h>
6+
#import "FIAPRequestHandler.h"
7+
#import "InAppPurchasePlugin.h"
8+
9+
@interface InAppPurchasePlugin ()
10+
11+
// Holding strong references to FIAPRequestHandlers. Remove the handlers from the set after
12+
// the request is finished.
13+
@property(strong, nonatomic, readonly) NSMutableSet *requestHandlers;
14+
15+
// Callback channel to dart used for when a function from the transaction observer is triggered.
16+
@property(strong, nonatomic) FlutterMethodChannel *transactionObserverCallbackChannel;
17+
18+
// Callback channel to dart used for when a function from the transaction observer is triggered.
19+
@property(copy, nonatomic) FIAPRequestHandler * (^handlerFactory)(SKRequest *);
20+
21+
// Convenience initializer with dependancy injection
22+
- (instancetype)initWithReceiptManager:(FIAPReceiptManager *)receiptManager
23+
handlerFactory:(FIAPRequestHandler * (^)(SKRequest *))handlerFactory;
24+
25+
// Transaction observer methods
26+
- (void)handleTransactionsUpdated:(NSArray<SKPaymentTransaction *> *)transactions;
27+
- (void)handleTransactionsRemoved:(NSArray<SKPaymentTransaction *> *)transactions;
28+
- (void)handleTransactionRestoreFailed:(NSError *)error;
29+
- (void)restoreCompletedTransactionsFinished;
30+
- (BOOL)shouldAddStorePayment:(SKPayment *)payment product:(SKProduct *)product;
31+
32+
@end

packages/in_app_purchase/in_app_purchase_storekit/darwin/Classes/InAppPurchasePlugin.m

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,14 @@
99
#import "FIAPReceiptManager.h"
1010
#import "FIAPRequestHandler.h"
1111
#import "FIAPaymentQueueHandler.h"
12+
#import "InAppPurchasePlugin+TestOnly.h"
1213

1314
@interface InAppPurchasePlugin ()
1415

15-
// Holding strong references to FIAPRequestHandlers. Remove the handlers from the set after
16-
// the request is finished.
17-
@property(strong, nonatomic, readonly) NSMutableSet *requestHandlers;
18-
1916
// After querying the product, the available products will be saved in the map to be used
2017
// for purchase.
2118
@property(strong, nonatomic, readonly) NSMutableDictionary *productsCache;
2219

23-
// Callback channel to dart used for when a function from the transaction observer is triggered.
24-
@property(strong, nonatomic, readonly) FlutterMethodChannel *transactionObserverCallbackChannel;
25-
2620
// Callback channel to dart used for when a function from the payment queue delegate is triggered.
2721
@property(strong, nonatomic, readonly) FlutterMethodChannel *paymentQueueDelegateCallbackChannel;
2822
@property(strong, nonatomic, readonly) NSObject<FlutterPluginRegistrar> *registrar;
@@ -52,6 +46,16 @@ - (instancetype)initWithReceiptManager:(FIAPReceiptManager *)receiptManager {
5246
_receiptManager = receiptManager;
5347
_requestHandlers = [NSMutableSet new];
5448
_productsCache = [NSMutableDictionary new];
49+
_handlerFactory = ^FIAPRequestHandler *(SKRequest *request) {
50+
return [[FIAPRequestHandler alloc] initWithRequest:request];
51+
};
52+
return self;
53+
}
54+
55+
- (instancetype)initWithReceiptManager:(FIAPReceiptManager *)receiptManager
56+
handlerFactory:(FIAPRequestHandler * (^)(SKRequest *))handlerFactory {
57+
self = [self initWithReceiptManager:receiptManager];
58+
_handlerFactory = [handlerFactory copy];
5559
return self;
5660
}
5761

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

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

274-
FIAPRequestHandler *handler = [[FIAPRequestHandler alloc] initWithRequest:request];
278+
FIAPRequestHandler *handler = self.handlerFactory(request);
275279
[self.requestHandlers addObject:handler];
276280
__weak typeof(self) weakSelf = self;
277281
[handler startProductRequestWithCompletionHandler:^(SKProductsResponse *_Nullable response,
@@ -282,6 +286,7 @@ - (void)refreshReceiptReceiptProperties:(nullable NSDictionary *)receiptProperti
282286
message:error.localizedDescription
283287
details:error.description];
284288
completion(requestError);
289+
return;
285290
}
286291
completion(nil);
287292
[weakSelf.requestHandlers removeObject:handler];

0 commit comments

Comments
 (0)