Skip to content

Commit

Permalink
Added StoreKit2Setting.shouldOnlyUseStoreKit2 (#1881)
Browse files Browse the repository at this point in the history
A couple of helpers that can be useful, for #1882 for example.
  • Loading branch information
NachoSoto authored Sep 4, 2022
1 parent 0bff3f6 commit 348d8fa
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
4 changes: 4 additions & 0 deletions RevenueCat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
37E3578711F5FDD5DC6458A8 /* AttributionFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37E3521731D8DC16873F55F3 /* AttributionFetcher.swift */; };
37E35C8515C5E2D01B0AF5C1 /* Strings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37E3507939634ED5A9280544 /* Strings.swift */; };
42F1DF385E3C1F9903A07FBF /* ProductsFetcherSK1.swift in Sources */ = {isa = PBXBuildFile; fileRef = EFB3CBAA73855779FE828CE2 /* ProductsFetcherSK1.swift */; };
57032ABF28C13CE4004FF47A /* StoreKit2SettingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57032ABE28C13CE4004FF47A /* StoreKit2SettingTests.swift */; };
57057FF828B0048900995F21 /* TestLogHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57057FF728B0048900995F21 /* TestLogHandler.swift */; };
57057FF928B0048900995F21 /* TestLogHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57057FF728B0048900995F21 /* TestLogHandler.swift */; };
5705800328B0085200995F21 /* Box.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5705800228B0085200995F21 /* Box.swift */; };
Expand Down Expand Up @@ -703,6 +704,7 @@
37E35EEE7783629CDE41B70C /* SystemInfoTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SystemInfoTests.swift; sourceTree = "<group>"; };
37E35F783903362B65FB7AF3 /* MockProductsRequestFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MockProductsRequestFactory.swift; sourceTree = "<group>"; };
37E35FDA0A44EA03EA12DAA2 /* DateFormatter+ExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "DateFormatter+ExtensionsTests.swift"; sourceTree = "<group>"; };
57032ABE28C13CE4004FF47A /* StoreKit2SettingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoreKit2SettingTests.swift; sourceTree = "<group>"; };
57057FF728B0048900995F21 /* TestLogHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestLogHandler.swift; sourceTree = "<group>"; };
5705800228B0085200995F21 /* Box.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Box.swift; sourceTree = "<group>"; };
570814C128A308050018BAE3 /* BackendIntegrationTests.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; name = BackendIntegrationTests.xctestplan; path = Tests/TestPlans/BackendIntegrationTests.xctestplan; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1466,6 +1468,7 @@
5766AA59283D4CAB00FA6091 /* IgnoreHashableTests.swift */,
57ACB12328174B9F000DCC9F /* CustomerInfo+TestExtensions.swift */,
57FDAABD28493A29009A48F1 /* SandboxEnvironmentDetectorTests.swift */,
57032ABE28C13CE4004FF47A /* StoreKit2SettingTests.swift */,
);
path = Misc;
sourceTree = "<group>";
Expand Down Expand Up @@ -2629,6 +2632,7 @@
57CFB96C27FE0E79002A6730 /* MockCurrentUserProvider.swift in Sources */,
57ACB13728184CF1000DCC9F /* DecoderExtensionTests.swift in Sources */,
351B516126D44BEB00BD2BD7 /* IdentityManagerTests.swift in Sources */,
57032ABF28C13CE4004FF47A /* StoreKit2SettingTests.swift in Sources */,
351B51C126D450E800BD2BD7 /* OfferingsManagerTests.swift in Sources */,
5796A39927D6C1E000653165 /* BackendPostSubscriberAttributesTests.swift in Sources */,
35D8330A262FBA9A00E60AC5 /* MockUserDefaults.swift in Sources */,
Expand Down
21 changes: 21 additions & 0 deletions Sources/Misc/StoreKit2Setting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ extension StoreKit2Setting {

}

extension StoreKit2Setting {

/// - Returns: `true` if SK2 is available in this device.
static var isStoreKit2Available: Bool {
if #available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) {
return true
} else {
return false
}
}

/// - Returns: `true` iff SK2 is enabled and it's available.
var shouldOnlyUseStoreKit2: Bool {
switch self {
case .disabled, .enabledOnlyForOptimizations: return false
case .enabledForCompatibleDevices: return Self.isStoreKit2Available
}
}

}

extension StoreKit2Setting: CustomDebugStringConvertible {

var debugDescription: String {
Expand Down
13 changes: 10 additions & 3 deletions Tests/StoreKitUnitTests/TestHelpers/AvailabilityChecks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@ enum AvailabilityChecks {
}
}

static func iOS14APIAvailableOrSkipTest() throws {
guard #available(iOS 14.0, tvOS 14.0, macOS 11.0, watchOS 6.2, *) else {
throw XCTSkip("Required API is not available for this test.")
}
}

static func iOS15APIAvailableOrSkipTest() throws {
guard #available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) else {
throw XCTSkip("Required API is not available for this test.")
}
}

static func iOS14APIAvailableOrSkipTest() throws {
guard #available(iOS 14.0, tvOS 14.0, macOS 11.0, watchOS 6.2, *) else {
throw XCTSkip("Required API is not available for this test.")
/// Opposite of `iOS15APIAvailableOrSkipTest`.
static func iOS15APINotAvailableOrSkipTest() throws {
if #available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) {
throw XCTSkip("Test only for older devices")
}
}

Expand Down
53 changes: 53 additions & 0 deletions Tests/UnitTests/Misc/StoreKit2SettingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// 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
//
// StoreKit2SettingTests.swift
//
// Created by Nacho Soto on 9/1/22.

import Nimble
import XCTest

@testable import RevenueCat

class StoreKit2SettingTests: TestCase {

func testStoreKit2NotAvailableWhenDisabled() {
expect(StoreKit2Setting.disabled.shouldOnlyUseStoreKit2) == false
}

func testShouldOnlyUseStoreKit2FalseWhenOnlyEnabledForOptimizations() {
expect(StoreKit2Setting.enabledOnlyForOptimizations.shouldOnlyUseStoreKit2) == false
}

func testShouldOnlyUseStoreKitFalseIfNotAvailable() throws {
try AvailabilityChecks.iOS15APINotAvailableOrSkipTest()

expect(StoreKit2Setting.enabledForCompatibleDevices.shouldOnlyUseStoreKit2) == false
}

func testShouldOnlyUseStoreKitTrueIfAvailable() throws {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

expect(StoreKit2Setting.enabledForCompatibleDevices.shouldOnlyUseStoreKit2) == true
}

func testStoreKit2NotAvailableOnOlderDevices() throws {
try AvailabilityChecks.iOS15APINotAvailableOrSkipTest()

expect(StoreKit2Setting.isStoreKit2Available) == false
}

func testStoreKit2AvailableOnNewerDevices() throws {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

expect(StoreKit2Setting.isStoreKit2Available) == true
}

}

0 comments on commit 348d8fa

Please sign in to comment.