Skip to content

Commit

Permalink
[in_app_purchase_storekit] Migrate InAppPurchasePluginTests.m to. Swi…
Browse files Browse the repository at this point in the history
…ft (flutter#7105)

Part of flutter/flutter#151624
## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] 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 [linked to 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], or this PR is [exempt from
CHANGELOG changes].
- [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.
  • Loading branch information
LouiseHsu authored Jul 17, 2024
1 parent 8cf4d96 commit 0375259
Show file tree
Hide file tree
Showing 12 changed files with 894 additions and 1,023 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.17+1

* Converts main plugin tests to swift.

## 0.3.17

* Removes OCMock from tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ public class InAppPurchasePlugin: NSObject, FlutterPlugin, InAppPurchaseAPI {
// TODO(louisehsu): Change it back to a set when removing obj-c dependancies from this file via type erasure
private var requestHandlers = NSHashTable<FLTRequestHandlerProtocol>()
private var handlerFactory: ((SKRequest) -> FLTRequestHandlerProtocol)
// TODO(louisehsu): Once tests are migrated to swift, we can use @testable import, and make theses vars private again and remove all instances of @objc
@objc
private var transactionObserverCallbackChannel: FLTMethodChannelProtocol?
public var registrar: FlutterPluginRegistrar?
// This property is optional, as it requires self to exist to be initialized.
@objc
public var paymentQueueHandler: FLTPaymentQueueHandlerProtocol?
// This property is optional, as it needs to be set during plugin registration, and can't be directly initialized.
@objc
public var transactionObserverCallbackChannel: FLTMethodChannelProtocol?

public static func register(with registrar: FlutterPluginRegistrar) {
#if os(iOS)
Expand All @@ -48,16 +43,17 @@ public class InAppPurchasePlugin: NSObject, FlutterPlugin, InAppPurchaseAPI {
SetUpInAppPurchaseAPI(messenger, instance)
}

@objc
// This init is used for tests
public init(
receiptManager: FIAPReceiptManager,
handlerFactory: @escaping (SKRequest) -> FLTRequestHandlerProtocol = {
DefaultRequestHandler(requestHandler: FIAPRequestHandler(request: $0))
}
},
transactionCallbackChannel: FLTMethodChannelProtocol? = nil
) {
self.receiptManager = receiptManager
self.handlerFactory = handlerFactory
self.transactionObserverCallbackChannel = transactionCallbackChannel
super.init()
}

Expand Down Expand Up @@ -93,11 +89,7 @@ public class InAppPurchasePlugin: NSObject, FlutterPlugin, InAppPurchaseAPI {
#if os(macOS)
let messenger = registrar.messenger
#endif
transactionObserverCallbackChannel = DefaultMethodChannel(
channel: FlutterMethodChannel(
name: "plugins.flutter.io/in_app_purchase",
binaryMessenger: messenger)
)
setupTransactionObserverChannelIfNeeded(withMessenger: messenger)
}

// MARK: - Pigeon Functions
Expand Down Expand Up @@ -359,7 +351,6 @@ public class InAppPurchasePlugin: NSObject, FlutterPlugin, InAppPurchaseAPI {
#endif
}

@objc
public func handleTransactionsUpdated(_ transactions: [SKPaymentTransaction]) {
let translatedTransactions = transactions.map {
FIAObjectTranslator.getMapFrom($0)
Expand All @@ -368,7 +359,6 @@ public class InAppPurchasePlugin: NSObject, FlutterPlugin, InAppPurchaseAPI {
"updatedTransactions", arguments: translatedTransactions)
}

@objc
public func handleTransactionsRemoved(_ transactions: [SKPaymentTransaction]) {
let translatedTransactions = transactions.map {
FIAObjectTranslator.getMapFrom($0)
Expand All @@ -377,19 +367,16 @@ public class InAppPurchasePlugin: NSObject, FlutterPlugin, InAppPurchaseAPI {
"removedTransactions", arguments: translatedTransactions)
}

@objc
public func handleTransactionRestoreFailed(_ error: NSError) {
transactionObserverCallbackChannel?.invokeMethod(
"restoreCompletedTransactionsFailed", arguments: FIAObjectTranslator.getMapFrom(error))
}

@objc
public func restoreCompletedTransactionsFinished() {
transactionObserverCallbackChannel?.invokeMethod(
"paymentQueueRestoreCompletedTransactionsFinished", arguments: nil)
}

@objc
public func shouldAddStorePayment(payment: SKPayment, product: SKProduct) -> Bool {
productsCache[product.productIdentifier] = product
transactionObserverCallbackChannel?.invokeMethod(
Expand Down Expand Up @@ -432,4 +419,18 @@ public class InAppPurchasePlugin: NSObject, FlutterPlugin, InAppPurchaseAPI {
}
return paymentQueueHandler
}

private func setupTransactionObserverChannelIfNeeded(
withMessenger messenger: FlutterBinaryMessenger
) {
// If the channel is already set (e.g., injected in tests), don't overwrite it.
guard self.transactionObserverCallbackChannel == nil else { return }

self.transactionObserverCallbackChannel = DefaultMethodChannel(
channel: FlutterMethodChannel(
name: "plugins.flutter.io/in_app_purchase",
binaryMessenger: messenger
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
C4667AA10A6BC70CE9A5007C /* libPods-RunnerTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = AB9CD9DD098BDAB3D5053EE5 /* libPods-RunnerTests.a */; };
E680BD031412EB2D02C9190B /* libPods-Runner.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 21CE6E615CF661FC0E18FB0A /* libPods-Runner.a */; };
F22BF91C2BC9B40B00713878 /* SwiftStubs.swift in Sources */ = {isa = PBXBuildFile; fileRef = F22BF91B2BC9B40B00713878 /* SwiftStubs.swift */; };
F276940B2C47268700277144 /* ProductRequestHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F276940A2C47268700277144 /* ProductRequestHandlerTests.swift */; };
F24C45E22C409D42000C6C72 /* InAppPurchasePluginTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F24C45E12C409D41000C6C72 /* InAppPurchasePluginTests.swift */; };
F295AD3A2C1256DD0067C78A /* Stubs.m in Sources */ = {isa = PBXBuildFile; fileRef = F295AD392C1256DD0067C78A /* Stubs.m */; };
F295AD412C1256F50067C78A /* FIAPPaymentQueueDeleteTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F295AD3B2C1256F50067C78A /* FIAPPaymentQueueDeleteTests.m */; };
F295AD422C1256F50067C78A /* InAppPurchasePluginTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F295AD3C2C1256F50067C78A /* InAppPurchasePluginTests.m */; };
F276940B2C47268700277144 /* ProductRequestHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F276940A2C47268700277144 /* ProductRequestHandlerTests.swift */; };
F295AD442C1256F50067C78A /* FIATransactionCacheTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F295AD3E2C1256F50067C78A /* FIATransactionCacheTests.m */; };
F295AD452C1256F50067C78A /* PaymentQueueTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F295AD3F2C1256F50067C78A /* PaymentQueueTests.m */; };
F295AD462C1256F50067C78A /* TranslatorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F295AD402C1256F50067C78A /* TranslatorTests.m */; };
Expand Down Expand Up @@ -75,11 +75,11 @@
CC9E5595B2B9B9B90632DA75 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
F22BF91A2BC9B40B00713878 /* RunnerTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RunnerTests-Bridging-Header.h"; sourceTree = "<group>"; };
F22BF91B2BC9B40B00713878 /* SwiftStubs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftStubs.swift; sourceTree = "<group>"; };
F276940A2C47268700277144 /* ProductRequestHandlerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ProductRequestHandlerTests.swift; path = ../../shared/RunnerTests/ProductRequestHandlerTests.swift; sourceTree = "<group>"; };
F24C45E12C409D41000C6C72 /* InAppPurchasePluginTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InAppPurchasePluginTests.swift; path = ../../shared/RunnerTests/InAppPurchasePluginTests.swift; sourceTree = "<group>"; };
F295AD362C1251300067C78A /* Stubs.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Stubs.h; path = ../../shared/RunnerTests/Stubs.h; sourceTree = "<group>"; };
F295AD392C1256DD0067C78A /* Stubs.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Stubs.m; path = ../../shared/RunnerTests/Stubs.m; sourceTree = "<group>"; };
F295AD3B2C1256F50067C78A /* FIAPPaymentQueueDeleteTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FIAPPaymentQueueDeleteTests.m; path = ../../shared/RunnerTests/FIAPPaymentQueueDeleteTests.m; sourceTree = "<group>"; };
F295AD3C2C1256F50067C78A /* InAppPurchasePluginTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = InAppPurchasePluginTests.m; path = ../../shared/RunnerTests/InAppPurchasePluginTests.m; sourceTree = "<group>"; };
F276940A2C47268700277144 /* ProductRequestHandlerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ProductRequestHandlerTests.swift; path = ../../shared/RunnerTests/ProductRequestHandlerTests.swift; sourceTree = "<group>"; };
F295AD3E2C1256F50067C78A /* FIATransactionCacheTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FIATransactionCacheTests.m; path = ../../shared/RunnerTests/FIATransactionCacheTests.m; sourceTree = "<group>"; };
F295AD3F2C1256F50067C78A /* PaymentQueueTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PaymentQueueTests.m; path = ../../shared/RunnerTests/PaymentQueueTests.m; sourceTree = "<group>"; };
F295AD402C1256F50067C78A /* TranslatorTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TranslatorTests.m; path = ../../shared/RunnerTests/TranslatorTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -186,10 +186,10 @@
A59001A521E69658004A3E5E /* RunnerTests */ = {
isa = PBXGroup;
children = (
F24C45E12C409D41000C6C72 /* InAppPurchasePluginTests.swift */,
F276940A2C47268700277144 /* ProductRequestHandlerTests.swift */,
F295AD3B2C1256F50067C78A /* FIAPPaymentQueueDeleteTests.m */,
F295AD3E2C1256F50067C78A /* FIATransactionCacheTests.m */,
F295AD3C2C1256F50067C78A /* InAppPurchasePluginTests.m */,
F295AD3F2C1256F50067C78A /* PaymentQueueTests.m */,
F295AD402C1256F50067C78A /* TranslatorTests.m */,
F295AD392C1256DD0067C78A /* Stubs.m */,
Expand Down Expand Up @@ -433,13 +433,13 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F24C45E22C409D42000C6C72 /* InAppPurchasePluginTests.swift in Sources */,
F22BF91C2BC9B40B00713878 /* SwiftStubs.swift in Sources */,
F295AD412C1256F50067C78A /* FIAPPaymentQueueDeleteTests.m in Sources */,
F295AD452C1256F50067C78A /* PaymentQueueTests.m in Sources */,
F295AD442C1256F50067C78A /* FIATransactionCacheTests.m in Sources */,
F276940B2C47268700277144 /* ProductRequestHandlerTests.swift in Sources */,
F295AD462C1256F50067C78A /* TranslatorTests.m in Sources */,
F295AD422C1256F50067C78A /* InAppPurchasePluginTests.m in Sources */,
F295AD3A2C1256DD0067C78A /* Stubs.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
33CC10F32044A3C60003C045 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 33CC10F22044A3C60003C045 /* Assets.xcassets */; };
33CC10F62044A3C60003C045 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 33CC10F42044A3C60003C045 /* MainMenu.xib */; };
33CC11132044BFA00003C045 /* MainFlutterWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33CC11122044BFA00003C045 /* MainFlutterWindow.swift */; };
F24C45E42C409D87000C6C72 /* InAppPurchasePluginTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F24C45E32C409D87000C6C72 /* InAppPurchasePluginTests.swift */; };
F27694092C4724B200277144 /* ProductRequestHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F27694082C4724B200277144 /* ProductRequestHandlerTests.swift */; };
F2C3A7412BD9D33D000D35F2 /* Stubs.swift in Sources */ = {isa = PBXBuildFile; fileRef = F2C3A7402BD9D33D000D35F2 /* Stubs.swift */; };
F79BDC102905FBE300E3999D /* FIAPPaymentQueueDeleteTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F79BDC0F2905FBE300E3999D /* FIAPPaymentQueueDeleteTests.m */; };
F79BDC122905FBF700E3999D /* FIATransactionCacheTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F79BDC112905FBF700E3999D /* FIATransactionCacheTests.m */; };
F79BDC142905FBFE00E3999D /* InAppPurchasePluginTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F79BDC132905FBFE00E3999D /* InAppPurchasePluginTests.m */; };
F79BDC182905FC1800E3999D /* PaymentQueueTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F79BDC172905FC1800E3999D /* PaymentQueueTests.m */; };
F79BDC1C2905FC3200E3999D /* Stubs.m in Sources */ = {isa = PBXBuildFile; fileRef = F79BDC1B2905FC3200E3999D /* Stubs.m */; };
F79BDC1E2905FC3900E3999D /* TranslatorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F79BDC1D2905FC3900E3999D /* TranslatorTests.m */; };
Expand Down Expand Up @@ -89,6 +89,7 @@
46EFB01DD1BBB34F886C33A0 /* Pods-RunnerTests.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.profile.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.profile.xcconfig"; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Release.xcconfig; sourceTree = "<group>"; };
9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Debug.xcconfig; sourceTree = "<group>"; };
F24C45E32C409D87000C6C72 /* InAppPurchasePluginTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InAppPurchasePluginTests.swift; path = ../../shared/RunnerTests/InAppPurchasePluginTests.swift; sourceTree = "<group>"; };
AEB2EC182EA43F26A351EE3E /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B537BC9F2D936311267ABC65 /* Pods-RunnerTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.debug.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.debug.xcconfig"; sourceTree = "<group>"; };
F27694082C4724B200277144 /* ProductRequestHandlerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ProductRequestHandlerTests.swift; path = ../../shared/RunnerTests/ProductRequestHandlerTests.swift; sourceTree = "<group>"; };
Expand All @@ -97,7 +98,6 @@
F700DD0228E652A10004836B /* RunnerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RunnerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
F79BDC0F2905FBE300E3999D /* FIAPPaymentQueueDeleteTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FIAPPaymentQueueDeleteTests.m; path = ../../shared/RunnerTests/FIAPPaymentQueueDeleteTests.m; sourceTree = "<group>"; };
F79BDC112905FBF700E3999D /* FIATransactionCacheTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FIATransactionCacheTests.m; path = ../../shared/RunnerTests/FIATransactionCacheTests.m; sourceTree = "<group>"; };
F79BDC132905FBFE00E3999D /* InAppPurchasePluginTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = InAppPurchasePluginTests.m; path = ../../shared/RunnerTests/InAppPurchasePluginTests.m; sourceTree = "<group>"; };
F79BDC152905FC0500E3999D /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = ../../shared/RunnerTests/Info.plist; sourceTree = "<group>"; };
F79BDC172905FC1800E3999D /* PaymentQueueTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PaymentQueueTests.m; path = ../../shared/RunnerTests/PaymentQueueTests.m; sourceTree = "<group>"; };
F79BDC1B2905FC3200E3999D /* Stubs.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Stubs.m; path = ../../shared/RunnerTests/Stubs.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -217,9 +217,9 @@
F700DD0328E652A10004836B /* RunnerTests */ = {
isa = PBXGroup;
children = (
F24C45E32C409D87000C6C72 /* InAppPurchasePluginTests.swift */,
F27694082C4724B200277144 /* ProductRequestHandlerTests.swift */,
F79BDC0F2905FBE300E3999D /* FIAPPaymentQueueDeleteTests.m */,
F79BDC132905FBFE00E3999D /* InAppPurchasePluginTests.m */,
F79BDC172905FC1800E3999D /* PaymentQueueTests.m */,
F79BDC1F2906023C00E3999D /* Stubs.h */,
F79BDC152905FC0500E3999D /* Info.plist */,
Expand Down Expand Up @@ -463,12 +463,12 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F24C45E42C409D87000C6C72 /* InAppPurchasePluginTests.swift in Sources */,
F79BDC1E2905FC3900E3999D /* TranslatorTests.m in Sources */,
F79BDC182905FC1800E3999D /* PaymentQueueTests.m in Sources */,
F79BDC1C2905FC3200E3999D /* Stubs.m in Sources */,
F79BDC102905FBE300E3999D /* FIAPPaymentQueueDeleteTests.m in Sources */,
F27694092C4724B200277144 /* ProductRequestHandlerTests.swift in Sources */,
F79BDC142905FBFE00E3999D /* InAppPurchasePluginTests.m in Sources */,
F79BDC122905FBF700E3999D /* FIATransactionCacheTests.m in Sources */,
F2C3A7412BD9D33D000D35F2 /* Stubs.swift in Sources */,
);
Expand Down

This file was deleted.

Loading

0 comments on commit 0375259

Please sign in to comment.