2021-11-22 12:37:33 -03:00
|
|
|
//
|
|
|
|
|
// Copyright RevenueCat Inc. All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the MIT License (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
|
//
|
|
|
|
|
// StoreKitConfigTestCase.swift
|
|
|
|
|
//
|
|
|
|
|
// Created by Andrés Boedo on 23/9/21.
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import Nimble
|
|
|
|
|
@testable import RevenueCat
|
|
|
|
|
import StoreKitTest
|
|
|
|
|
import XCTest
|
|
|
|
|
|
|
|
|
|
@available(iOS 14.0, tvOS 14.0, macOS 11.0, watchOS 6.2, *)
|
2022-05-10 12:40:40 -07:00
|
|
|
class StoreKitConfigTestCase: TestCase {
|
2021-11-22 12:37:33 -03:00
|
|
|
|
2022-05-12 15:43:10 -07:00
|
|
|
static var requestTimeout: TimeInterval = 60
|
|
|
|
|
static var requestDispatchTimeout: DispatchTimeInterval {
|
|
|
|
|
return .seconds(Int(Self.requestTimeout))
|
|
|
|
|
}
|
2021-12-08 16:18:58 -08:00
|
|
|
|
2021-11-22 12:37:33 -03:00
|
|
|
private static var hasWaited = false
|
|
|
|
|
private static let waitLock = Lock()
|
2021-12-08 16:18:58 -08:00
|
|
|
private static let waitTimeInSeconds: Double? = {
|
|
|
|
|
ProcessInfo.processInfo.environment["CIRCLECI_STOREKIT_TESTS_DELAY_SECONDS"]
|
|
|
|
|
.flatMap(Double.init)
|
|
|
|
|
}()
|
2021-11-22 12:37:33 -03:00
|
|
|
|
|
|
|
|
var testSession: SKTestSession!
|
|
|
|
|
var userDefaults: UserDefaults!
|
|
|
|
|
|
|
|
|
|
override func setUpWithError() throws {
|
|
|
|
|
testSession = try SKTestSession(configurationFileNamed: "UnitTestsConfiguration")
|
|
|
|
|
testSession.resetToDefaultState()
|
|
|
|
|
testSession.disableDialogs = true
|
|
|
|
|
testSession.clearTransactions()
|
|
|
|
|
|
|
|
|
|
self.waitForStoreKitTestIfNeeded()
|
|
|
|
|
|
|
|
|
|
let suiteName = "StoreKitConfigTests"
|
|
|
|
|
userDefaults = UserDefaults(suiteName: suiteName)
|
|
|
|
|
userDefaults.removePersistentDomain(forName: suiteName)
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 15:02:08 -07:00
|
|
|
override func tearDown() {
|
|
|
|
|
super.tearDown()
|
|
|
|
|
|
|
|
|
|
self.clearReceiptIfExists()
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-14 08:52:11 -08:00
|
|
|
// MARK: - Transactions observation
|
|
|
|
|
|
|
|
|
|
private static var transactionsObservation: Task<Void, Never>?
|
|
|
|
|
|
|
|
|
|
override class func setUp() {
|
|
|
|
|
if #available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) {
|
|
|
|
|
Self.transactionsObservation?.cancel()
|
|
|
|
|
Self.transactionsObservation = Task {
|
|
|
|
|
// Silence warning in tests:
|
|
|
|
|
// "Making a purchase without listening for transaction updates risks missing successful purchases.
|
|
|
|
|
for await _ in Transaction.updates {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override class func tearDown() {
|
|
|
|
|
Self.transactionsObservation?.cancel()
|
|
|
|
|
Self.transactionsObservation = nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 12:37:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extension StoreKitConfigTestCase {
|
|
|
|
|
|
|
|
|
|
func waitForStoreKitTestIfNeeded() {
|
|
|
|
|
// StoreKitTest seems to take a few seconds to initialize, and running tests before that
|
|
|
|
|
// might result in failure. So we give it a few seconds to load before testing.
|
2021-12-08 16:18:58 -08:00
|
|
|
|
|
|
|
|
guard let waitTime = Self.waitTimeInSeconds else { return }
|
|
|
|
|
|
2021-11-22 12:37:33 -03:00
|
|
|
Self.waitLock.perform {
|
|
|
|
|
if !Self.hasWaited {
|
2021-12-08 16:18:58 -08:00
|
|
|
Logger.warn("Delaying tests for \(waitTime) seconds for StoreKit initialization...")
|
|
|
|
|
|
|
|
|
|
Thread.sleep(forTimeInterval: waitTime)
|
|
|
|
|
|
2021-11-22 12:37:33 -03:00
|
|
|
Self.hasWaited = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 15:02:08 -07:00
|
|
|
func clearReceiptIfExists() {
|
|
|
|
|
let manager = FileManager.default
|
|
|
|
|
|
|
|
|
|
guard let url = Bundle.main.appStoreReceiptURL, manager.fileExists(atPath: url.path) else { return }
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
try manager.removeItem(at: url)
|
|
|
|
|
} catch {
|
|
|
|
|
Logger.appleWarning("Error attempting to remove receipt URL '\(url)': \(error)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 12:37:33 -03:00
|
|
|
}
|