Skip to content

Commit

Permalink
[InAppMessaging] Migrate to GoogleUtilities's storage container (#12757)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncooke3 authored Apr 12, 2024
1 parent 5fd1eb2 commit 74b8196
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 33 deletions.
3 changes: 2 additions & 1 deletion FirebaseInAppMessaging.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ See more product details at https://firebase.google.com/products/in-app-messagin
s.dependency 'FirebaseCore', '~> 10.0'
s.dependency 'FirebaseInstallations', '~> 10.0'
s.dependency 'FirebaseABTesting', '~> 10.0'
s.dependency 'GoogleUtilities/Environment', '~> 7.8'
s.dependency 'GoogleUtilities/Environment', '~> 7.13'
s.dependency 'GoogleUtilities/UserDefaults', '~> 7.13'
s.dependency 'nanopb', '>= 2.30908.0', '< 2.30911.0'

s.test_spec 'unit' do |unit_tests|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#import "FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMAnalyticsEventLogger.h"

@class FIRIAMClearcutLogger;
@class GULUserDefaults;
@protocol FIRIAMTimeFetcher;
@protocol FIRAnalyticsInterop;

Expand All @@ -34,12 +35,12 @@ NS_ASSUME_NONNULL_BEGIN
/**
*
* @param userDefaults needed for tracking upload timing info persistently.If nil, using
* NSUserDefaults standardUserDefaults. It's defined as a parameter to help with
* GULUserDefaults standardUserDefaults. It's defined as a parameter to help with
* unit testing mocking
*/
- (instancetype)initWithClearcutLogger:(FIRIAMClearcutLogger *)ctLogger
usingTimeFetcher:(id<FIRIAMTimeFetcher>)timeFetcher
usingUserDefaults:(nullable NSUserDefaults *)userDefaults
usingUserDefaults:(nullable GULUserDefaults *)userDefaults
analytics:(nullable id<FIRAnalyticsInterop>)analytics;
@end
NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#import <TargetConditionals.h>
#if TARGET_OS_IOS || TARGET_OS_TV || (defined(TARGET_OS_VISION) && TARGET_OS_VISION)

#import <GoogleUtilities/GULUserDefaults.h>

#import "FirebaseInAppMessaging/Sources/Analytics/FIRIAMAnalyticsEventLoggerImpl.h"

#import "FirebaseCore/Extension/FirebaseCoreInternal.h"
Expand All @@ -29,7 +31,7 @@
@interface FIRIAMAnalyticsEventLoggerImpl ()
@property(readonly, nonatomic) FIRIAMClearcutLogger *clearCutLogger;
@property(readonly, nonatomic) id<FIRIAMTimeFetcher> timeFetcher;
@property(nonatomic, readonly) NSUserDefaults *userDefaults;
@property(nonatomic, readonly) GULUserDefaults *userDefaults;
@end

// in these kFAXX constants, FA represents FirebaseAnalytics
Expand Down Expand Up @@ -60,13 +62,13 @@ @implementation FIRIAMAnalyticsEventLoggerImpl {

- (instancetype)initWithClearcutLogger:(FIRIAMClearcutLogger *)ctLogger
usingTimeFetcher:(id<FIRIAMTimeFetcher>)timeFetcher
usingUserDefaults:(nullable NSUserDefaults *)userDefaults
usingUserDefaults:(nullable GULUserDefaults *)userDefaults
analytics:(nullable id<FIRAnalyticsInterop>)analytics {
if (self = [super init]) {
_clearCutLogger = ctLogger;
_timeFetcher = timeFetcher;
_analytics = analytics;
_userDefaults = userDefaults ? userDefaults : [NSUserDefaults standardUserDefaults];
_userDefaults = userDefaults ? userDefaults : [GULUserDefaults standardUserDefaults];

if (!_analytics) {
FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM280002",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import <TargetConditionals.h>
#if TARGET_OS_IOS || TARGET_OS_TV || (defined(TARGET_OS_VISION) && TARGET_OS_VISION)

#import <GoogleUtilities/GULUserDefaults.h>
#import <UIKit/UIKit.h>
#import "FirebaseCore/Extension/FirebaseCoreInternal.h"

Expand Down Expand Up @@ -66,7 +67,7 @@ @interface FIRIAMClearcutUploader () {
@property(nonatomic, readonly) FIRIAMClearcutLogStorage *logStorage;

@property(nonatomic, readonly) FIRIAMClearcutStrategy *strategy;
@property(nonatomic, readonly) NSUserDefaults *userDefaults;
@property(nonatomic, readonly) GULUserDefaults *userDefaults;
@end

static NSString *FIRIAM_UserDefaultsKeyForNextValidClearcutUploadTimeInMills =
Expand All @@ -89,7 +90,7 @@ - (instancetype)initWithRequestSender:(FIRIAMClearcutHttpRequestSender *)request
timeFetcher:(id<FIRIAMTimeFetcher>)timeFetcher
logStorage:(FIRIAMClearcutLogStorage *)logStorage
usingStrategy:(FIRIAMClearcutStrategy *)strategy
usingUserDefaults:(nullable NSUserDefaults *)userDefaults {
usingUserDefaults:(nullable GULUserDefaults *)userDefaults {
if (self = [super init]) {
_nextSendScheduled = NO;
_timeFetcher = timeFetcher;
Expand All @@ -109,7 +110,7 @@ - (instancetype)initWithRequestSender:(FIRIAMClearcutHttpRequestSender *)request
object:nil];
}
#endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
_userDefaults = userDefaults ? userDefaults : [NSUserDefaults standardUserDefaults];
_userDefaults = userDefaults ? userDefaults : [GULUserDefaults standardUserDefaults];
// it would be 0 if it does not exist, which is equvilent to saying that
// you can send now
_nextValidSendTimeInMills = (int64_t)
Expand Down
6 changes: 4 additions & 2 deletions FirebaseInAppMessaging/Sources/Flows/FIRIAMBookKeeper.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#import <TargetConditionals.h>
#if TARGET_OS_IOS || TARGET_OS_TV || (defined(TARGET_OS_VISION) && TARGET_OS_VISION)

#import <GoogleUtilities/GULUserDefaults.h>

#import "FirebaseCore/Extension/FirebaseCoreInternal.h"

#import "FirebaseInAppMessaging/Sources/FIRCore+InAppMessaging.h"
Expand Down Expand Up @@ -44,7 +46,7 @@ @interface FIRIAMBookKeeperViaUserDefaults ()
@property(nonatomic) double lastDisplayTime;
@property(nonatomic) double lastFetchTime;
@property(nonatomic) double nextFetchWaitTime;
@property(nonatomic, nonnull) NSUserDefaults *defaults;
@property(nonatomic, nonnull) GULUserDefaults *defaults;
@end

@interface FIRIAMImpressionRecord ()
Expand Down Expand Up @@ -85,7 +87,7 @@ - (NSString *)description {

@implementation FIRIAMBookKeeperViaUserDefaults

- (instancetype)initWithUserDefaults:(NSUserDefaults *)userDefaults {
- (instancetype)initWithUserDefaults:(GULUserDefaults *)userDefaults {
if (self = [super init]) {
_defaults = userDefaults;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@class FIRIAMClearcutLogRecord;
@class FIRIAMClearcutHttpRequestSender;
@class FIRIAMClearcutLogStorage;
@class GULUserDefaults;

@protocol FIRIAMTimeFetcher;

Expand Down Expand Up @@ -58,14 +59,14 @@ NS_ASSUME_NONNULL_BEGIN
/**
*
* @param userDefaults needed for tracking upload timing info persistently.If nil, using
* NSUserDefaults standardUserDefaults. It's defined as a parameter to help with
* GULUserDefaults standardUserDefaults. It's defined as a parameter to help with
* unit testing mocking
*/
- (instancetype)initWithRequestSender:(FIRIAMClearcutHttpRequestSender *)requestSender
timeFetcher:(id<FIRIAMTimeFetcher>)timeFetcher
logStorage:(FIRIAMClearcutLogStorage *)retryStorage
usingStrategy:(FIRIAMClearcutStrategy *)strategy
usingUserDefaults:(nullable NSUserDefaults *)userDefaults;
usingUserDefaults:(nullable GULUserDefaults *)userDefaults;
/**
* This should return very quickly without blocking on and actual log uploading to
* clearcut server, which is done asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@class GULUserDefaults;

@interface FIRIAMImpressionRecord : NSObject
@property(nonatomic, readonly, copy) NSString *messageID;
@property(nonatomic, readonly) long impressionTimeInSeconds;
Expand Down Expand Up @@ -64,7 +67,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface FIRIAMBookKeeperViaUserDefaults : NSObject <FIRIAMBookKeeper>

- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithUserDefaults:(NSUserDefaults *)userDefaults NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithUserDefaults:(GULUserDefaults *)userDefaults NS_DESIGNATED_INITIALIZER;

// for testing, don't use them for production purpose
- (void)cleanupImpressions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

NS_ASSUME_NONNULL_BEGIN

@class GULUserDefaults;

extern NSInteger const kFIRIAMMaxFetchInNewlyInstalledMode;

/**
Expand Down Expand Up @@ -56,8 +58,8 @@ extern NSInteger const kFIRIAMMaxFetchInNewlyInstalledMode;

- (instancetype)init NS_UNAVAILABLE;

// having NSUserDefaults as passed-in to help with unit testing
- (instancetype)initWithUserDefaults:(NSUserDefaults *)userDefaults
// having GULUserDefaults as passed-in to help with unit testing
- (instancetype)initWithUserDefaults:(GULUserDefaults *)userDefaults
testingModeListener:(id<FIRIAMTestingModeListener>)testingModeListener;

// returns the current SDK mode
Expand Down
12 changes: 7 additions & 5 deletions FirebaseInAppMessaging/Sources/Runtime/FIRIAMRuntimeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#import <TargetConditionals.h>
#if TARGET_OS_IOS || TARGET_OS_TV || (defined(TARGET_OS_VISION) && TARGET_OS_VISION)

#import <GoogleUtilities/GULUserDefaults.h>

#import "FirebaseCore/Extension/FirebaseCoreInternal.h"

#import "FirebaseInAppMessaging/Sources/Analytics/FIRIAMAnalyticsEventLoggerImpl.h"
Expand Down Expand Up @@ -94,7 +96,7 @@ - (void)testingModeSwitchedOn {
}

- (FIRIAMAutoDataCollectionSetting)FIAMProgrammaticAutoDataCollectionSetting {
id settingEntry = [[NSUserDefaults standardUserDefaults]
id settingEntry = [[GULUserDefaults standardUserDefaults]
objectForKey:_userDefaultsKeyForFIAMProgammaticAutoDataCollectionSetting];

if (![settingEntry isKindOfClass:[NSNumber class]]) {
Expand Down Expand Up @@ -188,7 +190,7 @@ - (BOOL)shouldRunSDKFlowsOnStartup {

- (void)resume {
// persist the setting
[[NSUserDefaults standardUserDefaults]
[[GULUserDefaults standardUserDefaults]
setObject:@(YES)
forKey:_userDefaultsKeyForFIAMProgammaticAutoDataCollectionSetting];

Expand All @@ -209,7 +211,7 @@ - (void)resume {

- (void)pause {
// persist the setting
[[NSUserDefaults standardUserDefaults]
[[GULUserDefaults standardUserDefaults]
setObject:@(NO)
forKey:_userDefaultsKeyForFIAMProgammaticAutoDataCollectionSetting];

Expand Down Expand Up @@ -262,7 +264,7 @@ - (void)internalStartRuntimeWithSDKSettings:(FIRIAMSDKSettings *)settings {
self.responseParser = [[FIRIAMFetchResponseParser alloc] initWithTimeFetcher:timeFetcher];

self.bookKeeper = [[FIRIAMBookKeeperViaUserDefaults alloc]
initWithUserDefaults:[NSUserDefaults standardUserDefaults]];
initWithUserDefaults:[GULUserDefaults standardUserDefaults]];

self.messageCache = [[FIRIAMMessageClientCache alloc] initWithBookkeeper:self.bookKeeper
usingResponseParser:self.responseParser];
Expand Down Expand Up @@ -324,7 +326,7 @@ - (void)internalStartRuntimeWithSDKSettings:(FIRIAMSDKSettings *)settings {
analytics:[FIRInAppMessaging inAppMessaging].analytics];

FIRIAMSDKModeManager *sdkModeManager =
[[FIRIAMSDKModeManager alloc] initWithUserDefaults:NSUserDefaults.standardUserDefaults
[[FIRIAMSDKModeManager alloc] initWithUserDefaults:GULUserDefaults.standardUserDefaults
testingModeListener:self];

FIRIAMActionURLFollower *actionFollower = [FIRIAMActionURLFollower actionURLFollower];
Expand Down
6 changes: 4 additions & 2 deletions FirebaseInAppMessaging/Sources/Runtime/FIRIAMSDKModeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#import <TargetConditionals.h>
#if TARGET_OS_IOS || TARGET_OS_TV || (defined(TARGET_OS_VISION) && TARGET_OS_VISION)

#import <GoogleUtilities/GULUserDefaults.h>

#import "FirebaseCore/Extension/FirebaseCoreInternal.h"

#import "FirebaseInAppMessaging/Sources/FIRCore+InAppMessaging.h"
Expand All @@ -38,7 +40,7 @@
}

@interface FIRIAMSDKModeManager ()
@property(nonatomic, nonnull, readonly) NSUserDefaults *userDefaults;
@property(nonatomic, nonnull, readonly) GULUserDefaults *userDefaults;
// Make it weak so that we don't depend on its existence to avoid circular reference.
@property(nonatomic, readonly, weak) id<FIRIAMTestingModeListener> testingModeListener;
@end
Expand All @@ -52,7 +54,7 @@ @implementation FIRIAMSDKModeManager {
NSInteger _fetchCount;
}

- (instancetype)initWithUserDefaults:(NSUserDefaults *)userDefaults
- (instancetype)initWithUserDefaults:(GULUserDefaults *)userDefaults
testingModeListener:(id<FIRIAMTestingModeListener>)testingModeListener {
if (self = [super init]) {
_userDefaults = userDefaults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

#import <GoogleUtilities/GULUserDefaults.h>
#import <OCMock/OCMock.h>
#import <XCTest/XCTest.h>

Expand All @@ -28,7 +29,7 @@ @interface FIRIAMAnalyticsEventLoggerImplTests : XCTestCase
@property(nonatomic) FIRIAMClearcutLogger *mockClearcutLogger;
@property(nonatomic) id<FIRIAMTimeFetcher> mockTimeFetcher;
@property(nonatomic) id mockFirebaseAnalytics;
@property(nonatomic) NSUserDefaults *mockUserDefaults;
@property(nonatomic) GULUserDefaults *mockUserDefaults;

@end

Expand Down Expand Up @@ -140,7 +141,7 @@ - (void)setUp {
[super setUp];
self.mockClearcutLogger = OCMClassMock(FIRIAMClearcutLogger.class);
self.mockTimeFetcher = OCMProtocolMock(@protocol(FIRIAMTimeFetcher));
self.mockUserDefaults = OCMClassMock(NSUserDefaults.class);
self.mockUserDefaults = OCMClassMock(GULUserDefaults.class);
}

- (void)tearDown {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
* limitations under the License.
*/

#import <GoogleUtilities/GULUserDefaults.h>
#import <OCMock/OCMock.h>
#import <XCTest/XCTest.h>

#import "FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMBookKeeper.h"

@interface FIRIAMBookKeeperViaUserDefaultsTests : XCTestCase
@property(nonatomic) NSUserDefaults *userDefaultsForTesting;
@property(nonatomic) GULUserDefaults *userDefaultsForTesting;
@end

extern NSString *FIRIAM_UserDefaultsKeyForImpressions;
Expand All @@ -29,18 +30,19 @@ @interface FIRIAMBookKeeperViaUserDefaultsTests : XCTestCase
extern NSString *FIRIAM_ImpressionDictKeyForID;
extern NSString *FIRIAM_ImpressionDictKeyForTimestamp;

static NSString *const kSuiteName = @"FIRIAMBookKeeperViaUserDefaultsTests";

@implementation FIRIAMBookKeeperViaUserDefaultsTests
- (void)setUp {
[super setUp];
self.userDefaultsForTesting =
[[NSUserDefaults alloc] initWithSuiteName:@"FIRIAMBookKeeperViaUserDefaultsTests"];
self.userDefaultsForTesting = [[GULUserDefaults alloc] initWithSuiteName:kSuiteName];
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the
// class.
[super tearDown];
[self.userDefaultsForTesting removeSuiteNamed:@"FIRIAMBookKeeperViaUserDefaultsTests"];
[[[NSUserDefaults alloc] initWithSuiteName:kSuiteName] removePersistentDomainForName:kSuiteName];
}

- (void)testRecordImpressionRecords {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

#import <GoogleUtilities/GULUserDefaults.h>
#import <OCMock/OCMock.h>
#import <XCTest/XCTest.h>

Expand All @@ -27,7 +28,7 @@ @interface FIRIAMClearcutUploaderTests : XCTestCase
@property(nonatomic) FIRIAMClearcutHttpRequestSender *mockRequestSender;
@property(nonatomic) FIRIAMClearcutLogStorage *mockLogStorage;
@property(nonatomic) FIRIAMClearcutStrategy *defaultStrategy;
@property(nonatomic) NSUserDefaults *mockUserDefaults;
@property(nonatomic) GULUserDefaults *mockUserDefaults;
@property(nonatomic) NSString *cachePath;
@end

Expand Down Expand Up @@ -65,7 +66,7 @@ - (void)setUp {
failureBackoffTimeInMills:1000
batchSendSize:10];

self.mockUserDefaults = OCMClassMock(NSUserDefaults.class);
self.mockUserDefaults = OCMClassMock(GULUserDefaults.class);
self.cachePath = [self generatedCachePath];
OCMStub([self.mockUserDefaults integerForKey:[OCMArg any]]).andReturn(0);
}
Expand Down
5 changes: 3 additions & 2 deletions FirebaseInAppMessaging/Tests/Unit/FIRIAMSDKModeManagerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@
* limitations under the License.
*/

#import <GoogleUtilities/GULUserDefaults.h>
#import <OCMock/OCMock.h>
#import <XCTest/XCTest.h>

#import "FirebaseInAppMessaging/Sources/Private/Runtime/FIRIAMSDKModeManager.h"

@interface FIRIAMSDKModeManagerTests : XCTestCase
@property(nonatomic) NSUserDefaults *mockUserDefaults;
@property(nonatomic) GULUserDefaults *mockUserDefaults;
@property(nonatomic) id<FIRIAMTestingModeListener> mockTestingModeListener;
@end

@implementation FIRIAMSDKModeManagerTests

- (void)setUp {
[super setUp];
self.mockUserDefaults = OCMClassMock(NSUserDefaults.class);
self.mockUserDefaults = OCMClassMock(GULUserDefaults.class);
self.mockTestingModeListener = OCMStrictProtocolMock(@protocol(FIRIAMTestingModeListener));
}

Expand Down
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ let package = Package(
"FirebaseInstallations",
"FirebaseABTesting",
.product(name: "GULEnvironment", package: "GoogleUtilities"),
.product(name: "GULUserDefaults", package: "GoogleUtilities"),
.product(name: "nanopb", package: "nanopb"),
.target(name: "FirebaseInAppMessaging_iOS", condition: .when(platforms: [.iOS])),
],
Expand Down

0 comments on commit 74b8196

Please sign in to comment.