-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PaymentQueueWrapper
: also conform to SKPaymentTransactionObserver
…
… to fix promoted purchases (#1962) Follow up to #1901. That fix was incomplete. I missed it because the protocols were mixed up (fixed in #1961). Now `PaymentQueueWrapper`, when set up as the delegate (only when SK2 is enabled and SK1 is disabled) will be added as a transaction observer.
- Loading branch information
Showing
6 changed files
with
162 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// 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 | ||
// | ||
// MockPaymentQueue.swift | ||
// | ||
// Created by Nacho Soto on 9/30/22. | ||
|
||
import StoreKit | ||
|
||
final class MockPaymentQueue: SKPaymentQueue { | ||
|
||
var addedPayments: [SKPayment] = [] | ||
override func add(_ payment: SKPayment) { | ||
addedPayments.append(payment) | ||
} | ||
|
||
var observers: [SKPaymentTransactionObserver] = [] | ||
override func add(_ observer: SKPaymentTransactionObserver) { | ||
observers.append(observer) | ||
} | ||
|
||
override func remove(_ observer: SKPaymentTransactionObserver) { | ||
let index = observers.firstIndex { $0 === observer } | ||
observers.remove(at: index!) | ||
} | ||
|
||
var finishedTransactions: [SKPaymentTransaction] = [] | ||
override func finishTransaction(_ transaction: SKPaymentTransaction) { | ||
finishedTransactions.append(transaction) | ||
} | ||
|
||
#if os(iOS) || targetEnvironment(macCatalyst) | ||
@available(iOS 13.4, macCatalyst 13.4, *) | ||
func simulatePaymentQueueShouldShowPriceConsent() -> [Bool] { | ||
return self.observers | ||
.compactMap { $0 as? SKPaymentQueueDelegate } | ||
.compactMap { $0.paymentQueueShouldShowPriceConsent?(self) } | ||
} | ||
#endif | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// 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 | ||
// | ||
// PaymentQuueWrapperTests.swift | ||
// | ||
// Created by Nacho Soto on 9/30/22. | ||
|
||
import Foundation | ||
import Nimble | ||
import StoreKit | ||
import XCTest | ||
|
||
@testable import RevenueCat | ||
|
||
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *) | ||
class PaymentQueueWrapperTests: TestCase { | ||
|
||
private var paymentQueue: MockPaymentQueue! | ||
private var wrapper: PaymentQueueWrapper! | ||
private var delegate: WrapperDelegate! | ||
|
||
override func setUpWithError() throws { | ||
try super.setUpWithError() | ||
|
||
try AvailabilityChecks.iOS13APIAvailableOrSkipTest() | ||
|
||
self.paymentQueue = MockPaymentQueue() | ||
self.wrapper = .init(paymentQueue: self.paymentQueue) | ||
self.delegate = WrapperDelegate() | ||
} | ||
|
||
func testNoDelegateIsSetByDefault() { | ||
expect(self.paymentQueue.delegate).to(beNil()) | ||
} | ||
|
||
func testNoObserverIsAddedByDefault() { | ||
expect(self.paymentQueue.observers).to(beEmpty()) | ||
} | ||
|
||
func testSettingDelegateSetsPaymentQueueDelegate() { | ||
self.wrapper.delegate = self.delegate | ||
|
||
expect(self.paymentQueue.delegate) === self.wrapper | ||
} | ||
|
||
func testSettingDelegateAddsTransactionObserver() { | ||
self.wrapper.delegate = self.delegate | ||
|
||
expect(self.paymentQueue.observers).to(haveCount(1)) | ||
expect(self.paymentQueue.observers.onlyElement) === self.wrapper | ||
} | ||
|
||
func testResettingDelegateClearsPaymentQueueDelegate() { | ||
self.wrapper.delegate = self.delegate | ||
self.wrapper.delegate = nil | ||
|
||
expect(self.paymentQueue.delegate).to(beNil()) | ||
} | ||
|
||
func testResettingDelegateRemovesTransactionObserver() { | ||
self.wrapper.delegate = self.delegate | ||
self.wrapper.delegate = nil | ||
|
||
expect(self.paymentQueue.observers).to(beEmpty()) | ||
} | ||
|
||
} | ||
|
||
private final class WrapperDelegate: NSObject, PaymentQueueWrapperDelegate { | ||
|
||
override init() {} | ||
|
||
func paymentQueueWrapper( | ||
_ wrapper: RevenueCat.PaymentQueueWrapper, | ||
shouldAddStorePayment payment: SKPayment, | ||
for product: RevenueCat.SK1Product | ||
) -> Bool { | ||
fail("Unexpected call") | ||
return false | ||
} | ||
|
||
var paymentQueueWrapperShouldShowPriceConsent: Bool { | ||
fail("Unexpected call") | ||
return false | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters