Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Purchases.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2084,10 +2084,11 @@
PRODUCT_BUNDLE_IDENTIFIER = com.revenuecat.PurchasesCoreSwiftTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos macosx appletvsimulator appletvos";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGETED_DEVICE_FAMILY = "1,2,3,6";
TVOS_DEPLOYMENT_TARGET = 9.0;
WATCHOS_DEPLOYMENT_TARGET = 6.2;
};
Expand All @@ -2113,8 +2114,9 @@
PRODUCT_BUNDLE_IDENTIFIER = com.revenuecat.PurchasesCoreSwiftTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos macosx appletvsimulator appletvos";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGETED_DEVICE_FAMILY = "1,2,3,6";
TVOS_DEPLOYMENT_TARGET = 9.0;
WATCHOS_DEPLOYMENT_TARGET = 6.2;
};
Expand Down Expand Up @@ -2426,13 +2428,13 @@
PRODUCT_BUNDLE_IDENTIFIER = com.purchases.PurchasesTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos macosx";
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos macosx appletvsimulator appletvos";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OBJC_BRIDGING_HEADER = "PurchasesTests/PurchasesTests-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_PRECOMPILE_BRIDGING_HEADER = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGETED_DEVICE_FAMILY = "1,2,3,6";
TVOS_DEPLOYMENT_TARGET = 9.0;
WATCHOS_DEPLOYMENT_TARGET = 6.2;
};
Expand All @@ -2456,13 +2458,13 @@
PRODUCT_BUNDLE_IDENTIFIER = com.purchases.PurchasesTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos macosx";
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos macosx appletvsimulator appletvos";
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OBJC_BRIDGING_HEADER = "PurchasesTests/PurchasesTests-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_PRECOMPILE_BRIDGING_HEADER = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGETED_DEVICE_FAMILY = "1,2,3,6";
TVOS_DEPLOYMENT_TARGET = 9.0;
WATCHOS_DEPLOYMENT_TARGET = 6.2;
};
Expand Down
2 changes: 1 addition & 1 deletion PurchasesCoreSwift/Misc/SystemInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private extension SystemInfo {
#elseif os(macOS)
return false
#elseif os(watchOS)
return WKExtension.shared().applicationState == WKApplicationState.background
return WKExtension.shared().applicationState == WKApplicationState.background
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//

import XCTest
import OHHTTPStubs
import Nimble

import Purchases
Expand Down
2 changes: 1 addition & 1 deletion PurchasesCoreSwiftTests/Mocks/MockProductsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MockProductsManager: ProductsManager {
let products: [SKProduct] = identifiers.map { (identifier) -> MockSKProduct in
let p = MockSKProduct(mockProductIdentifier: identifier)
p.mockSubscriptionGroupIdentifier = "1234567"
if #available(iOS 12.2, *) {
if #available(iOS 11.2, tvOS 11.2, macOS 10.13.2, *) {
let mockDiscount = MockDiscount()
mockDiscount.mockIdentifier = "discount_id"
p.mockDiscount = mockDiscount
Expand Down
4 changes: 1 addition & 3 deletions PurchasesCoreSwiftTests/Mocks/MockSKDiscount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import Foundation
import StoreKit

@available(iOS 11.2, *)
@available(iOS 11.2, tvOS 11.2, macOS 10.13.2, *)
class MockDiscount: SKProductDiscount {
var mockPaymentMode: SKProductDiscount.PaymentMode?
override var paymentMode: SKProductDiscount.PaymentMode {
Expand All @@ -23,10 +23,8 @@ class MockDiscount: SKProductDiscount {
return mockIdentifier ?? "identifier"
}

@available(iOS 11.2, *)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant availability information

lazy var mockSubscriptionPeriod: SKProductSubscriptionPeriod? = nil

@available(iOS 11.2, *)
override var subscriptionPeriod: SKProductSubscriptionPeriod {
return mockSubscriptionPeriod ?? SKProductSubscriptionPeriod(numberOfUnits: 1, unit:.month)
}
Expand Down
16 changes: 6 additions & 10 deletions PurchasesCoreSwiftTests/Mocks/MockSKProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,23 @@ class MockSKProduct: SKProduct {
return mockPrice ?? 2.99 as NSDecimalNumber
}

@available(iOS 11.2, *)
@available(iOS 11.2, tvOS 11.2, macOS 10.13.2, *)
override var introductoryPrice: SKProductDiscount? {
if #available(iOS 12.2, *) {
return mockDiscount ?? MockDiscount()
} else {
return MockDiscount()
}
mockDiscount ?? MockDiscount()
}

@available(iOS 12.2, *)
@available(iOS 11.2, tvOS 11.2, macOS 10.13.2, *)
lazy var mockDiscount: SKProductDiscount? = nil

@available(iOS 12.2, *)
@available(iOS 11.2, tvOS 11.2, macOS 10.13.2, *)
override var discounts: [SKProductDiscount] {
return (mockDiscount != nil) ? [mockDiscount!] : []
}

@available(iOS 11.2, *)
@available(iOS 11.2, tvOS 11.2, macOS 10.13.2, *)
lazy var mockSubscriptionPeriod: SKProductSubscriptionPeriod? = nil

@available(iOS 11.2, *)
@available(iOS 11.2, tvOS 11.2, macOS 10.13.2, *)
override var subscriptionPeriod: SKProductSubscriptionPeriod {
return mockSubscriptionPeriod ?? SKProductSubscriptionPeriod(numberOfUnits: 1, unit:.month)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ProductInfoExtractorTests: XCTestCase {
func testExtractInfoFromProductExtractsPaymentMode() {
let product = MockSKProduct(mockProductIdentifier: "cool_product")

if #available(iOS 12.2, *) {
if #available(iOS 11.2, tvOS 11.2, macOS 10.13.2, *) {
let mockDiscount = MockDiscount()
mockDiscount.mockPaymentMode = .freeTrial

Expand All @@ -65,7 +65,7 @@ class ProductInfoExtractorTests: XCTestCase {
func testExtractInfoFromProductExtractsIntroPrice() {
let product = MockSKProduct(mockProductIdentifier: "cool_product")

if #available(iOS 12.2, *) {
if #available(iOS 11.2, tvOS 11.2, macOS 10.13.2, *) {
let mockDiscount = MockDiscount()
mockDiscount.mockPrice = 10.99

Expand All @@ -87,7 +87,7 @@ class ProductInfoExtractorTests: XCTestCase {
func testExtractInfoFromProductExtractsNormalDuration() {
let product = MockSKProduct(mockProductIdentifier: "cool_product")

if #available(iOS 11.2, *) {
if #available(iOS 11.2, tvOS 11.2, macOS 10.13.2, *) {
product.mockSubscriptionPeriod = SKProductSubscriptionPeriod(numberOfUnits: 2, unit: .month)
let productInfoExtractor = ProductInfoExtractor()

Expand All @@ -106,7 +106,7 @@ class ProductInfoExtractorTests: XCTestCase {
func testExtractInfoFromProductDoesNotExtractNormalDurationIfSubscriptionPeriodIsZero() {
let product = MockSKProduct(mockProductIdentifier: "cool_product")

if #available(iOS 11.2, *) {
if #available(iOS 11.2, tvOS 11.2, macOS 10.13.2, *) {
product.mockSubscriptionPeriod = SKProductSubscriptionPeriod(numberOfUnits: 0, unit: .month)
let productInfoExtractor = ProductInfoExtractor()

Expand All @@ -125,7 +125,7 @@ class ProductInfoExtractorTests: XCTestCase {
func testExtractInfoFromProductExtractsIntroDuration() {
let product = MockSKProduct(mockProductIdentifier: "cool_product")

if #available(iOS 12.2, *) {
if #available(iOS 11.2, tvOS 11.2, macOS 10.13.2, *) {
let mockDiscount = MockDiscount()
mockDiscount.mockSubscriptionPeriod = SKProductSubscriptionPeriod(numberOfUnits: 3, unit: .year)

Expand All @@ -147,7 +147,7 @@ class ProductInfoExtractorTests: XCTestCase {
func testExtractInfoFromProductExtractsIntroDurationType() {
let product = MockSKProduct(mockProductIdentifier: "cool_product")

if #available(iOS 12.2, macOS 10.14.4, *) {
if #available(iOS 11.2, macOS 10.14.4, tvOS 11.2, *) {
let mockDiscount = MockDiscount()
mockDiscount.mockPaymentMode = .freeTrial

Expand All @@ -168,8 +168,7 @@ class ProductInfoExtractorTests: XCTestCase {

func testExtractInfoFromProductExtractsSubscriptionGroup() {
let product = MockSKProduct(mockProductIdentifier: "cool_product")

if #available(iOS 12.0, *) {
if #available(iOS 11.2, *) {
let group = "mock_group"
product.mockSubscriptionGroupIdentifier = group
let productInfoExtractor = ProductInfoExtractor()
Expand All @@ -189,7 +188,7 @@ class ProductInfoExtractorTests: XCTestCase {
func testExtractInfoFromProductExtractsDiscounts() {
let product = MockSKProduct(mockProductIdentifier: "cool_product")

if #available(iOS 12.2, *) {
if #available(iOS 11.2, tvOS 11.2, macOS 10.13.2, *) {
let mockDiscount = MockDiscount()
let paymentMode: SKProductDiscount.PaymentMode = .freeTrial
mockDiscount.mockPaymentMode = paymentMode
Expand Down
111 changes: 55 additions & 56 deletions PurchasesCoreSwiftTests/Purchasing/ProductInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Nimble

import PurchasesCoreSwift

@available(iOS 12.2, *)
@available(iOS 11.2, *)
class ProductInfoTests: XCTestCase {
func testAsDictionaryConvertsProductIdentifierCorrectly() {
let productIdentifier = "cool_product"
Expand Down Expand Up @@ -88,63 +88,62 @@ class ProductInfoTests: XCTestCase {
let productInfo: ProductInfo = .createMockProductInfo(subscriptionGroup: subscriptionGroup)
expect(productInfo.asDictionary()["subscription_group_id"] as? String) == subscriptionGroup
}

func testAsDictionaryConvertsDiscountsCorrectly() {
if #available(macOS 10.14.4, *) {


let discount1 = PromotionalOffer(withProductDiscount: MockProductDiscount(identifier: "offerid1",
paymentMode: .payAsYouGo,
price: 11))
let discount2 = PromotionalOffer(withProductDiscount: MockProductDiscount(identifier: "offerid2",
paymentMode: .payUpFront,
price: 12))
let discount3 = PromotionalOffer(withProductDiscount: MockProductDiscount(identifier: "offerid3",
paymentMode: .freeTrial,
price: 13))

let productInfo: ProductInfo = .createMockProductInfo(discounts: [discount1, discount2, discount3])

expect(productInfo.asDictionary()["offers"] as? [[String: NSObject]]).toNot(beNil())
guard let receivedOffers = productInfo.asDictionary()["offers"] as? [[String: NSObject]] else { fatalError() }

expect(receivedOffers[0]["offer_identifier"] as? String) == discount1.offerIdentifier
expect(receivedOffers[0]["price"] as? NSDecimalNumber) == discount1.price
expect((receivedOffers[0]["payment_mode"] as? NSNumber)?.intValue) == discount1.paymentMode.rawValue

expect(receivedOffers[1]["offer_identifier"] as? String) == discount2.offerIdentifier
expect(receivedOffers[1]["price"] as? NSDecimalNumber) == discount2.price
expect((receivedOffers[1]["payment_mode"] as? NSNumber)?.intValue) == discount2.paymentMode.rawValue

expect(receivedOffers[2]["offer_identifier"] as? String) == discount3.offerIdentifier
expect(receivedOffers[2]["price"] as? NSDecimalNumber) == discount3.price
expect((receivedOffers[2]["payment_mode"] as? NSNumber)?.intValue) == discount3.paymentMode.rawValue
}
let discount1 = PromotionalOffer(offerIdentifier: "offerid1",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test is the same, I've only changed how the discounts are initialized. The constructor with a product discount as param is not available for all OS versions, but there is another compatible one. Since the objective of this test case is not to test if the constructor works fine, I think we can use the most compatible constructor. WDYT?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, I think this was probably an oversight.

price: NSDecimalNumber(decimal: 11),
paymentMode: .payAsYouGo)

let discount2 = PromotionalOffer(offerIdentifier: "offerid2",
price: NSDecimalNumber(decimal: 12),
paymentMode: .payUpFront)

let discount3 = PromotionalOffer(offerIdentifier: "offerid3",
price: NSDecimalNumber(decimal: 13),
paymentMode: .freeTrial)

let productInfo: ProductInfo = .createMockProductInfo(discounts: [discount1, discount2, discount3])

expect(productInfo.asDictionary()["offers"] as? [[String: NSObject]]).toNot(beNil())
guard let receivedOffers = productInfo.asDictionary()["offers"] as? [[String: NSObject]] else { fatalError() }

expect(receivedOffers[0]["offer_identifier"] as? String) == discount1.offerIdentifier
expect(receivedOffers[0]["price"] as? NSDecimalNumber) == discount1.price
expect((receivedOffers[0]["payment_mode"] as? NSNumber)?.intValue) == discount1.paymentMode.rawValue

expect(receivedOffers[1]["offer_identifier"] as? String) == discount2.offerIdentifier
expect(receivedOffers[1]["price"] as? NSDecimalNumber) == discount2.price
expect((receivedOffers[1]["payment_mode"] as? NSNumber)?.intValue) == discount2.paymentMode.rawValue

expect(receivedOffers[2]["offer_identifier"] as? String) == discount3.offerIdentifier
expect(receivedOffers[2]["price"] as? NSDecimalNumber) == discount3.price
expect((receivedOffers[2]["payment_mode"] as? NSNumber)?.intValue) == discount3.paymentMode.rawValue

}

func testCacheKey() {
if #available(macOS 10.14.4, *) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here. ⬆️

let discount1 = PromotionalOffer(withProductDiscount: MockProductDiscount(identifier: "offerid1",
paymentMode: .payAsYouGo,
price: 11))
let discount2 = PromotionalOffer(withProductDiscount: MockProductDiscount(identifier: "offerid2",
paymentMode: .payUpFront,
price: 12))
let discount3 = PromotionalOffer(withProductDiscount: MockProductDiscount(identifier: "offerid3",
paymentMode: .freeTrial,
price: 13))

let productInfo: ProductInfo = .createMockProductInfo(productIdentifier: "cool_product",
paymentMode: .payUpFront,
currencyCode: "UYU",
price: 49.99,
normalDuration: "P3Y",
introDuration: "P3W",
introDurationType: .freeTrial,
introPrice: 0,
subscriptionGroup: "cool_group",
discounts: [discount1, discount2, discount3])
expect(productInfo.cacheKey) == "cool_product-49.99-UYU-1-0-cool_group-P3Y-P3W-0-offerid1-offerid2-offerid3"
}
let discount1 = PromotionalOffer(offerIdentifier: "offerid1",
price: NSDecimalNumber(decimal: 11),
paymentMode: .payAsYouGo)

let discount2 = PromotionalOffer(offerIdentifier: "offerid2",
price: NSDecimalNumber(decimal: 12),
paymentMode: .payUpFront)

let discount3 = PromotionalOffer(offerIdentifier: "offerid3",
price: NSDecimalNumber(decimal: 13),
paymentMode: .freeTrial)

let productInfo: ProductInfo = .createMockProductInfo(productIdentifier: "cool_product",
paymentMode: .payUpFront,
currencyCode: "UYU",
price: 49.99,
normalDuration: "P3Y",
introDuration: "P3W",
introDurationType: .freeTrial,
introPrice: 0,
subscriptionGroup: "cool_group",
discounts: [discount1, discount2, discount3])
expect(productInfo.cacheKey) == "cool_product-49.99-UYU-1-0-cool_group-P3Y-P3W-0-offerid1-offerid2-offerid3"
}
}
6 changes: 3 additions & 3 deletions PurchasesCoreSwiftTests/Purchasing/StoreKitWrapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import Foundation
import XCTest
import OHHTTPStubs
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless 🗑️

import Nimble

@testable import PurchasesCoreSwift
Expand Down Expand Up @@ -99,7 +98,7 @@ class StoreKitWrapperTests: XCTestCase, StoreKitWrapperDelegate {
expect(self.updatedTransactions).to(contain(transaction))
}

@available(iOS 11.0, *)
@available(iOS 11.0, tvOS 11.0, macOS 11.0, macCatalyst 14.0, *)
func testCallsDelegateWhenPromoPurchaseIsAvailable() {
let product = SKProduct.init();
let payment = SKPayment.init(product: product)
Expand All @@ -109,7 +108,7 @@ class StoreKitWrapperTests: XCTestCase, StoreKitWrapperDelegate {
expect(self.promoProduct).to(be(product))
}

@available(iOS 11.0, *)
@available(iOS 11.0, tvOS 11.0, macOS 11.0, macCatalyst 14.0, *)
func testPromoDelegateMethodPassesBackReturnValueFromOwnDelegate() {
let product = SKProduct.init();
let payment = SKPayment.init(product: product)
Expand Down Expand Up @@ -199,6 +198,7 @@ class StoreKitWrapperTests: XCTestCase, StoreKitWrapperDelegate {
expect(payment.productIdentifier) == productId
}

@available(macOS 10.14, *)
func testPaymentWithProductSetsSimulatesAskToBuyInSandbox() {
guard let wrapper = wrapper else { fatalError("wrapper is not initialized!") }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//

import XCTest
import OHHTTPStubs
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless 🗑️

import Nimble

@testable import PurchasesCoreSwift
Expand Down
Loading