|
| 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 'dart:async'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:flutter/services.dart'; |
| 9 | +import 'package:flutter_test/flutter_test.dart'; |
| 10 | +import 'package:in_app_purchase_ios/in_app_purchase_ios.dart'; |
| 11 | +import 'package:in_app_purchase_ios/src/channel.dart'; |
| 12 | +import 'package:in_app_purchase_ios/store_kit_wrappers.dart'; |
| 13 | + |
| 14 | +import '../store_kit_wrappers/sk_test_stub_objects.dart'; |
| 15 | + |
| 16 | +class FakeIOSPlatform { |
| 17 | + FakeIOSPlatform() { |
| 18 | + channel.setMockMethodCallHandler(onMethodCall); |
| 19 | + } |
| 20 | + |
| 21 | + // pre-configured store informations |
| 22 | + String? receiptData; |
| 23 | + late Set<String> validProductIDs; |
| 24 | + late Map<String, SKProductWrapper> validProducts; |
| 25 | + late List<SKPaymentTransactionWrapper> transactions; |
| 26 | + late List<SKPaymentTransactionWrapper> finishedTransactions; |
| 27 | + late bool testRestoredTransactionsNull; |
| 28 | + late bool testTransactionFail; |
| 29 | + PlatformException? queryProductException; |
| 30 | + PlatformException? restoreException; |
| 31 | + SKError? testRestoredError; |
| 32 | + |
| 33 | + void reset() { |
| 34 | + transactions = []; |
| 35 | + receiptData = 'dummy base64data'; |
| 36 | + validProductIDs = ['123', '456'].toSet(); |
| 37 | + validProducts = Map(); |
| 38 | + for (String validID in validProductIDs) { |
| 39 | + Map<String, dynamic> productWrapperMap = |
| 40 | + buildProductMap(dummyProductWrapper); |
| 41 | + productWrapperMap['productIdentifier'] = validID; |
| 42 | + validProducts[validID] = SKProductWrapper.fromJson(productWrapperMap); |
| 43 | + } |
| 44 | + |
| 45 | + SKPaymentTransactionWrapper tran1 = SKPaymentTransactionWrapper( |
| 46 | + transactionIdentifier: '123', |
| 47 | + payment: dummyPayment, |
| 48 | + originalTransaction: dummyTransaction, |
| 49 | + transactionTimeStamp: 123123123.022, |
| 50 | + transactionState: SKPaymentTransactionStateWrapper.restored, |
| 51 | + error: null, |
| 52 | + ); |
| 53 | + SKPaymentTransactionWrapper tran2 = SKPaymentTransactionWrapper( |
| 54 | + transactionIdentifier: '1234', |
| 55 | + payment: dummyPayment, |
| 56 | + originalTransaction: dummyTransaction, |
| 57 | + transactionTimeStamp: 123123123.022, |
| 58 | + transactionState: SKPaymentTransactionStateWrapper.restored, |
| 59 | + error: null, |
| 60 | + ); |
| 61 | + |
| 62 | + transactions.addAll([tran1, tran2]); |
| 63 | + finishedTransactions = []; |
| 64 | + testRestoredTransactionsNull = false; |
| 65 | + testTransactionFail = false; |
| 66 | + queryProductException = null; |
| 67 | + restoreException = null; |
| 68 | + testRestoredError = null; |
| 69 | + } |
| 70 | + |
| 71 | + SKPaymentTransactionWrapper createPendingTransaction(String id) { |
| 72 | + return SKPaymentTransactionWrapper( |
| 73 | + transactionIdentifier: '', |
| 74 | + payment: SKPaymentWrapper(productIdentifier: id), |
| 75 | + transactionState: SKPaymentTransactionStateWrapper.purchasing, |
| 76 | + transactionTimeStamp: 123123.121, |
| 77 | + error: null, |
| 78 | + originalTransaction: null); |
| 79 | + } |
| 80 | + |
| 81 | + SKPaymentTransactionWrapper createPurchasedTransaction( |
| 82 | + String productId, String transactionId) { |
| 83 | + return SKPaymentTransactionWrapper( |
| 84 | + payment: SKPaymentWrapper(productIdentifier: productId), |
| 85 | + transactionState: SKPaymentTransactionStateWrapper.purchased, |
| 86 | + transactionTimeStamp: 123123.121, |
| 87 | + transactionIdentifier: transactionId, |
| 88 | + error: null, |
| 89 | + originalTransaction: null); |
| 90 | + } |
| 91 | + |
| 92 | + SKPaymentTransactionWrapper createFailedTransaction(String productId) { |
| 93 | + return SKPaymentTransactionWrapper( |
| 94 | + transactionIdentifier: '', |
| 95 | + payment: SKPaymentWrapper(productIdentifier: productId), |
| 96 | + transactionState: SKPaymentTransactionStateWrapper.failed, |
| 97 | + transactionTimeStamp: 123123.121, |
| 98 | + error: SKError( |
| 99 | + code: 0, |
| 100 | + domain: 'ios_domain', |
| 101 | + userInfo: {'message': 'an error message'}), |
| 102 | + originalTransaction: null); |
| 103 | + } |
| 104 | + |
| 105 | + Future<dynamic> onMethodCall(MethodCall call) { |
| 106 | + switch (call.method) { |
| 107 | + case '-[SKPaymentQueue canMakePayments:]': |
| 108 | + return Future<bool>.value(true); |
| 109 | + case '-[InAppPurchasePlugin startProductRequest:result:]': |
| 110 | + if (queryProductException != null) { |
| 111 | + throw queryProductException!; |
| 112 | + } |
| 113 | + List<String> productIDS = |
| 114 | + List.castFrom<dynamic, String>(call.arguments); |
| 115 | + assert(productIDS is List<String>, 'invalid argument type'); |
| 116 | + List<String> invalidFound = []; |
| 117 | + List<SKProductWrapper> products = []; |
| 118 | + for (String productID in productIDS) { |
| 119 | + if (!validProductIDs.contains(productID)) { |
| 120 | + invalidFound.add(productID); |
| 121 | + } else { |
| 122 | + products.add(validProducts[productID]!); |
| 123 | + } |
| 124 | + } |
| 125 | + SkProductResponseWrapper response = SkProductResponseWrapper( |
| 126 | + products: products, invalidProductIdentifiers: invalidFound); |
| 127 | + return Future<Map<String, dynamic>>.value( |
| 128 | + buildProductResponseMap(response)); |
| 129 | + case '-[InAppPurchasePlugin restoreTransactions:result:]': |
| 130 | + if (restoreException != null) { |
| 131 | + throw restoreException!; |
| 132 | + } |
| 133 | + if (testRestoredError != null) { |
| 134 | + InAppPurchaseIosPlatform.observer |
| 135 | + .restoreCompletedTransactionsFailed(error: testRestoredError!); |
| 136 | + return Future<void>.sync(() {}); |
| 137 | + } |
| 138 | + if (!testRestoredTransactionsNull) { |
| 139 | + InAppPurchaseIosPlatform.observer |
| 140 | + .updatedTransactions(transactions: transactions); |
| 141 | + } |
| 142 | + InAppPurchaseIosPlatform.observer |
| 143 | + .paymentQueueRestoreCompletedTransactionsFinished(); |
| 144 | + |
| 145 | + return Future<void>.sync(() {}); |
| 146 | + case '-[InAppPurchasePlugin retrieveReceiptData:result:]': |
| 147 | + if (receiptData != null) { |
| 148 | + return Future<void>.value(receiptData); |
| 149 | + } else { |
| 150 | + throw PlatformException(code: 'no_receipt_data'); |
| 151 | + } |
| 152 | + case '-[InAppPurchasePlugin refreshReceipt:result:]': |
| 153 | + receiptData = 'refreshed receipt data'; |
| 154 | + return Future<void>.sync(() {}); |
| 155 | + case '-[InAppPurchasePlugin addPayment:result:]': |
| 156 | + String id = call.arguments['productIdentifier']; |
| 157 | + SKPaymentTransactionWrapper transaction = createPendingTransaction(id); |
| 158 | + InAppPurchaseIosPlatform.observer |
| 159 | + .updatedTransactions(transactions: [transaction]); |
| 160 | + sleep(const Duration(milliseconds: 30)); |
| 161 | + if (testTransactionFail) { |
| 162 | + SKPaymentTransactionWrapper transaction_failed = |
| 163 | + createFailedTransaction(id); |
| 164 | + InAppPurchaseIosPlatform.observer |
| 165 | + .updatedTransactions(transactions: [transaction_failed]); |
| 166 | + } else { |
| 167 | + SKPaymentTransactionWrapper transaction_finished = |
| 168 | + createPurchasedTransaction( |
| 169 | + id, transaction.transactionIdentifier ?? ''); |
| 170 | + InAppPurchaseIosPlatform.observer |
| 171 | + .updatedTransactions(transactions: [transaction_finished]); |
| 172 | + } |
| 173 | + break; |
| 174 | + case '-[InAppPurchasePlugin finishTransaction:result:]': |
| 175 | + finishedTransactions.add(createPurchasedTransaction( |
| 176 | + call.arguments["productIdentifier"], |
| 177 | + call.arguments["transactionIdentifier"])); |
| 178 | + break; |
| 179 | + } |
| 180 | + return Future<void>.sync(() {}); |
| 181 | + } |
| 182 | +} |
0 commit comments