Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI Preview Mode: mock CustomerInfo #4786

Merged
merged 6 commits into from
Feb 14, 2025
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
4 changes: 4 additions & 0 deletions RevenueCat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@
57FFD2522922DBED00A9A878 /* MockStoreTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57FFD2502922DBED00A9A878 /* MockStoreTransaction.swift */; };
6E38843A0CAFD551013D0A3F /* StoreProduct.swift in Sources */ = {isa = PBXBuildFile; fileRef = FECF627761D375C8431EB866 /* StoreProduct.swift */; };
75425E0F2D5DFA9F00E25F60 /* PaywallComponentsDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75425E0E2D5DFA9F00E25F60 /* PaywallComponentsDataTests.swift */; };
75ABFDB52D5F579E00D69DF1 /* CustomerInfoManagerUIPreviewModeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75ABFDB42D5F579E00D69DF1 /* CustomerInfoManagerUIPreviewModeTests.swift */; };
7706ED3E2C6E374D0004B9F9 /* ButtonStyles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7706ED3D2C6E374D0004B9F9 /* ButtonStyles.swift */; };
7707A94C2CAD93AC006E0313 /* PaywallButtonComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7707A94B2CAD93AC006E0313 /* PaywallButtonComponent.swift */; };
7707A94E2CAD94D2006E0313 /* ButtonComponentViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7707A94D2CAD94D2006E0313 /* ButtonComponentViewModel.swift */; };
Expand Down Expand Up @@ -2099,6 +2100,7 @@
57FDAABF28493C13009A48F1 /* MockSandboxEnvironmentDetector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockSandboxEnvironmentDetector.swift; sourceTree = "<group>"; };
57FFD2502922DBED00A9A878 /* MockStoreTransaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockStoreTransaction.swift; sourceTree = "<group>"; };
75425E0E2D5DFA9F00E25F60 /* PaywallComponentsDataTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaywallComponentsDataTests.swift; sourceTree = "<group>"; };
75ABFDB42D5F579E00D69DF1 /* CustomerInfoManagerUIPreviewModeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomerInfoManagerUIPreviewModeTests.swift; sourceTree = "<group>"; };
7706ED3D2C6E374D0004B9F9 /* ButtonStyles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonStyles.swift; sourceTree = "<group>"; };
7707A93B2CAD8AA2006E0313 /* Local.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Local.xcconfig; sourceTree = "<group>"; };
7707A94B2CAD93AC006E0313 /* PaywallButtonComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaywallButtonComponent.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4116,6 +4118,7 @@
children = (
37E35E992F1916C7F3911E7B /* CustomerInfoManagerTests.swift */,
4F3D56622A1E66A10070105A /* CustomerInfoManagerPostReceiptTests.swift */,
75ABFDB42D5F579E00D69DF1 /* CustomerInfoManagerUIPreviewModeTests.swift */,
37E35294EBC1E5A879C95540 /* IdentityManagerTests.swift */,
);
path = Identity;
Expand Down Expand Up @@ -6400,6 +6403,7 @@
5733D00928CFA7A4008638D8 /* MockPaymentQueueWrapper.swift in Sources */,
2DDF41CA24F6F4C3005BC22D /* ArraySlice_UInt8+ExtensionsTests.swift in Sources */,
2DDF41E124F6F527005BC22D /* MockReceiptParser.swift in Sources */,
75ABFDB52D5F579E00D69DF1 /* CustomerInfoManagerUIPreviewModeTests.swift in Sources */,
57E415EB2846962500EA5460 /* PurchasesSyncPurchasesTests.swift in Sources */,
4FE6FEEB2AA940E300780B45 /* StoredEventSerializerTests.swift in Sources */,
5766AAC5283E843300FA6091 /* PurchasesConfiguringTests.swift in Sources */,
Expand Down
31 changes: 31 additions & 0 deletions Sources/Identity/CustomerInfoManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ class CustomerInfoManager {
}

func cachedCustomerInfo(appUserID: String) -> CustomerInfo? {
guard !self.systemInfo.dangerousSettings.uiPreviewMode else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm I guess if we don't cache it, this would always be null and this might not be needed... But I guess I'm ok keeping it just in case.

Copy link
Member Author

Choose a reason for hiding this comment

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

What you say makes total sense. The main reason why I also put this there is to always receive a nonnil CustomerInfo object, even when using CacheFetchPolicy.fromCacheOnly. Like: in UI Preview mode, you consistently get the mocked CustomerInfo every time and in any case (e.g. for any CacheFetchPolicy).

return self.createPreviewCustomerInfo()
}

let cachedCustomerInfoData = self.withData {
$0.deviceCache.cachedCustomerInfoData(appUserID: appUserID)
}
Expand All @@ -209,6 +213,10 @@ class CustomerInfoManager {
}

func cache(customerInfo: CustomerInfo, appUserID: String) {
guard !self.systemInfo.dangerousSettings.uiPreviewMode else {
return
}

if customerInfo.shouldCache {
do {
let jsonData = try JSONEncoder.default.encode(customerInfo)
Expand Down Expand Up @@ -344,6 +352,10 @@ private extension CustomerInfoManager {
func getCustomerInfo(appUserID: String,
isAppBackgrounded: Bool,
completion: @escaping CustomerAPI.CustomerInfoResponseHandler) {
guard !self.systemInfo.dangerousSettings.uiPreviewMode else {
completion(.success(self.createPreviewCustomerInfo()))
return
}
if #available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) {
_ = Task<Void, Never> {
let transactions = await self.transactionFetcher.unfinishedVerifiedTransactions
Expand Down Expand Up @@ -427,6 +439,25 @@ private extension CustomerInfoManager {
initiationSource: .queue
)

// MARK: - For UI Preview mode

/// Generates a dummy `CustomerInfo` with hardcoded information exclusively for UI Preview mode.
private func createPreviewCustomerInfo() -> CustomerInfo {
let previewSubscriber = CustomerInfoResponse.Subscriber(
originalAppUserId: IdentityManager.uiPreviewModeAppUserID,
firstSeen: Date(),
subscriptions: [:],
nonSubscriptions: [:],
entitlements: [:]
)
let previewCustomerInfoResponse = CustomerInfoResponse(subscriber: previewSubscriber,
requestDate: Date(),
rawData: [:])
let previewCustomerInfo = CustomerInfo(response: previewCustomerInfoResponse,
entitlementVerification: .verified,
sandboxEnvironmentDetector: BundleSandboxEnvironmentDetector.default)
return previewCustomerInfo
}
}

// @unchecked because:
Expand Down
114 changes: 114 additions & 0 deletions Tests/UnitTests/Identity/CustomerInfoManagerUIPreviewModeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// 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
//
// CustomerInfoManagerUIPreviewModeTests.swift
//
// Created by Antonio Pallares on 14/2/25.

import Foundation
import Nimble
import XCTest

@_spi(Internal) @testable import RevenueCat

@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *)
class CustomerInfoManagerUIPreviewModeTests: BaseCustomerInfoManagerTests {

override func setUpWithError() throws {

// Enable UI Preview Mode
self.mockSystemInfo = MockSystemInfo(
platformInfo: nil,
finishTransactions: true,
dangerousSettings: DangerousSettings(uiPreviewMode: true)
)

try super.setUpWithError()
}

func testFetchCustomerInfoDoesNotCallBackendInUIPreviewMode() async throws {
_ = try await self.customerInfoManager.fetchAndCacheCustomerInfo(
appUserID: "any_user",
isAppBackgrounded: false
)

expect(self.mockBackend.invokedGetSubscriberData) == false
}

func testCustomerInfoIsNotCachedInUIPreviewMode() async throws {
_ = try await self.customerInfoManager.fetchAndCacheCustomerInfo(
appUserID: "any_user",
isAppBackgrounded: false
)

expect(self.mockDeviceCache.cacheCustomerInfoCount) == 0
}

func testCachedCustomerInfoInUIPreviewModeReturnsMockCustomerInfo() async throws {
let info = try XCTUnwrap(self.customerInfoManager.cachedCustomerInfo(appUserID: "any_user"))

expect(info.originalAppUserId) == IdentityManager.uiPreviewModeAppUserID
}

func testUIPreviewModeCustomerInfoHasExpectedValues() async throws {
let info = try await self.customerInfoManager.fetchAndCacheCustomerInfo(
appUserID: "any_user",
isAppBackgrounded: false
)

expect(info.originalAppUserId) == IdentityManager.uiPreviewModeAppUserID
}

func testCustomerInfoReturnsMockInUIPreviewModeForAllFetchPolicies() async throws {
let policies: [CacheFetchPolicy] = [
.fromCacheOnly,
.fetchCurrent,
.cachedOrFetched,
.notStaleCachedOrFetched
]

for policy in policies {
let info = try await self.customerInfoManager.customerInfo(
appUserID: "any_user",
fetchPolicy: policy
)

expect(self.mockBackend.invokedGetSubscriberData) == false
expect(info.originalAppUserId) == IdentityManager.uiPreviewModeAppUserID
expect(self.mockDeviceCache.cacheCustomerInfoCount) == 0
}
}

func testUnfinishedTransactionsIgnoredInUIPreviewMode() async throws {
self.mockTransationFetcher.stubbedUnfinishedTransactions = [
Self.createTransaction(),
Self.createTransaction()
]

mockBackend.stubbedGetCustomerInfoResult = .success(mockCustomerInfo)

let info = try await self.customerInfoManager.fetchAndCacheCustomerInfo(
appUserID: "any_user",
isAppBackgrounded: false
)

expect(self.mockBackend.invokedGetSubscriberData) == false
expect(self.mockTransactionPoster.invokedHandlePurchasedTransaction.value) == false
expect(info.originalAppUserId) == IdentityManager.uiPreviewModeAppUserID
}
}

@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *)
private extension CustomerInfoManagerUIPreviewModeTests {

static func createTransaction() -> StoreTransaction {
return .init(sk1Transaction: MockTransaction())
}

}