Skip to content

Commit 559aeed

Browse files
authored
[in_app_purchase_storekit} Fix more test flakes (flutter#13279)
Fixes flutter/flutter#194285 (hopefully) The main cause of flutter/flutter#194285 (where `Product.products(for:)` returned `[]` across StoreKit 2 tests on the iOS 27 simulator) was fixed in flutter#13213 by preventing the example app from calling StoreKit APIs before `SKTestSession` initialized. However, `testGetProductsWithStoreKitError` and `testFailedNetworkErrorPurchase` remains fragile in iOS 26.2+ This is still because product caching in `StoreKitTest` is still 1. Opaque 2. unable to be manually cleared - the product cache sticks around even after calling `resetToDefaultState()`. Thus, the state of the cache depends on the order that the tests will run. :/ So, if a test that purchases a product succeeds, if we later hit `setSimulatedError(..., forAPI: .loadProducts)`, the cached product will be returned, causing the test to fail. This is an issue that RevenueCat also ran into as shown here: RevenueCat/purchases-ios#6088, where they skip tests using `.loadProduct` for anything above ios 26. Instead of keeping dead tests around, I think its fair to just remove them and avoid just keeping dead code around till the end of time. Additionally, the code paths tested by the removed tests are already being hit by `testFailedProductUnavilablePurchase`, so we arent really losing coverage. Additionally in this PR I fix up some leaky states between tests. ## Pre-Review Checklist
1 parent 727c73e commit 559aeed

2 files changed

Lines changed: 2 additions & 60 deletions

File tree

‎packages/in_app_purchase/in_app_purchase_storekit/example/shared/RunnerTests/InAppPurchaseStoreKit2PluginTests.swift‎

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -206,37 +206,6 @@ final class InAppPurchase2PluginTests: XCTestCase {
206206
XCTAssert(jsonRepresentationString.localizedStandardContains("storefront\":\"USA"))
207207
}
208208

209-
//TODO(louisehsu): Add testing for lower versions.
210-
@available(iOS 17.0, macOS 14.0, *)
211-
func testGetProductsWithStoreKitError() async throws {
212-
let osVersion = ProcessInfo.processInfo.operatingSystemVersion
213-
try XCTSkipIf(
214-
// https://developer.apple.com/forums/thread/808030
215-
osVersion.majorVersion == 26 && osVersion.minorVersion == 2,
216-
"Known StoreKitTest bug on Xcode 26.2 with setSimulatedError() when used on .loadProducts API"
217-
)
218-
219-
try await session.setSimulatedError(
220-
.generic(.networkError(URLError(.badURL))), forAPI: .loadProducts)
221-
222-
let expectation = self.expectation(description: "products request should fail")
223-
224-
plugin.products(identifiers: ["subscription_silver"]) { result in
225-
switch result {
226-
case .success:
227-
XCTFail("This `products` call should not succeed")
228-
expectation.fulfill()
229-
case .failure(let error):
230-
expectation.fulfill()
231-
XCTAssert(
232-
error.localizedDescription
233-
== "The operation couldn’t be completed. (in_app_purchase_storekit.PigeonError error 1.)"
234-
)
235-
}
236-
}
237-
await fulfillment(of: [expectation], timeout: purchaseTimeout)
238-
}
239-
240209
func testSuccessfulPurchase() async throws {
241210
let expectation = self.expectation(description: "Purchase request should succeed")
242211
plugin.purchase(id: "consumable", options: nil) { result in
@@ -252,35 +221,6 @@ final class InAppPurchase2PluginTests: XCTestCase {
252221
await fulfillment(of: [expectation], timeout: purchaseTimeout)
253222
}
254223

255-
@available(iOS 17.0, macOS 14.0, *)
256-
func testFailedNetworkErrorPurchase() async throws {
257-
let osVersion = ProcessInfo.processInfo.operatingSystemVersion
258-
try XCTSkipIf(
259-
// https://developer.apple.com/forums/thread/808030
260-
osVersion.majorVersion == 26 && osVersion.minorVersion == 2,
261-
"Known StoreKitTest bug on Xcode 26.2 with setSimulatedError() when used on .loadProducts API"
262-
)
263-
264-
// StoreKitTest aggressively caches products and transaction, which means sometimes it bypasses a simulated error.
265-
session.clearTransactions()
266-
try await session.setSimulatedError(
267-
.generic(.networkError(URLError(.badURL))), forAPI: .loadProducts)
268-
let expectation = self.expectation(description: "products request should fail")
269-
plugin.purchase(id: "consumable", options: nil) { result in
270-
switch result {
271-
case .success:
272-
XCTFail("Purchase should NOT succeed.")
273-
expectation.fulfill()
274-
case .failure(let error):
275-
XCTAssertEqual(
276-
error.localizedDescription,
277-
"The operation couldn’t be completed. (in_app_purchase_storekit.PigeonError error 1.)")
278-
expectation.fulfill()
279-
}
280-
}
281-
await fulfillment(of: [expectation], timeout: purchaseTimeout)
282-
}
283-
284224
@available(iOS 17.0, macOS 14.0, *)
285225
func testFailedProductUnavilablePurchase() async throws {
286226
try await session.setSimulatedError(
@@ -297,6 +237,7 @@ final class InAppPurchase2PluginTests: XCTestCase {
297237
}
298238
}
299239
await fulfillment(of: [expectation], timeout: purchaseTimeout)
240+
try await session.setSimulatedError(nil, forAPI: .purchase)
300241
}
301242

302243
func testInvalidProductPurchase() async throws {

‎packages/in_app_purchase/in_app_purchase_storekit/example/shared/RunnerTests/StoreKit2TranslatorTests.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ final class StoreKit2TranslatorTests: XCTestCase {
6161
priceLocale: SK2PriceLocaleMessage(currencyCode: "USD", currencySymbol: "$"))
6262

6363
self.session = try! SKTestSession(configurationFileNamed: "Configuration")
64+
self.session.resetToDefaultState()
6465
self.session.clearTransactions()
6566
let receiptManagerStub = FIAPReceiptManagerStub()
6667
plugin = InAppPurchasePluginStub(receiptManager: receiptManagerStub) { request in

0 commit comments

Comments
 (0)