Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
themiswang committed Nov 22, 2023
1 parent a0fcb67 commit e95fac6
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 13 deletions.
14 changes: 11 additions & 3 deletions Crashlytics/Crashlytics/FIRCrashlytics.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#import <GoogleDataTransport/GoogleDataTransport.h>

@import FirebaseSessions;
@import FirebaseRemoteConfigInterop;

#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
Expand Down Expand Up @@ -104,7 +105,8 @@ - (instancetype)initWithApp:(FIRApp *)app
appInfo:(NSDictionary *)appInfo
installations:(FIRInstallations *)installations
analytics:(id<FIRAnalyticsInterop>)analytics
sessions:(id<FIRSessionsProvider>)sessions {
sessions:(id<FIRSessionsProvider>)sessions
remoteConfig:(id<FIRRemoteConfigInterop>)remoteConfig {
self = [super init];

if (self) {
Expand Down Expand Up @@ -206,6 +208,10 @@ + (void)load {
FIRDependency *sessionsDep =
[FIRDependency dependencyWithProtocol:@protocol(FIRSessionsProvider)];

FIRDependency *remoteConfigDep =
[FIRDependency dependencyWithProtocol:@protocol(FIRRemoteConfigInterop)];

// break point on this also with rc creatioBlock and check the order
FIRComponentCreationBlock creationBlock =
^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
if (!container.app.isDefaultApp) {
Expand All @@ -215,6 +221,7 @@ + (void)load {

id<FIRAnalyticsInterop> analytics = FIR_COMPONENT(FIRAnalyticsInterop, container);
id<FIRSessionsProvider> sessions = FIR_COMPONENT(FIRSessionsProvider, container);
id<FIRRemoteConfigInterop> remoteConfig = FIR_COMPONENT(FIRRemoteConfigInterop, container);

FIRInstallations *installations = [FIRInstallations installationsWithApp:container.app];

Expand All @@ -224,13 +231,14 @@ + (void)load {
appInfo:NSBundle.mainBundle.infoDictionary
installations:installations
analytics:analytics
sessions:sessions];
sessions:sessions
remoteConfig:remoteConfig];
};

FIRComponent *component =
[FIRComponent componentWithProtocol:@protocol(FIRCrashlyticsInstanceProvider)
instantiationTiming:FIRInstantiationTimingEagerInDefaultApp
dependencies:@[ analyticsDep, sessionsDep ]
dependencies:@[ analyticsDep, sessionsDep, remoteConfigDep ]
creationBlock:creationBlock];
return @[ component ];
}
Expand Down
14 changes: 14 additions & 0 deletions FirebaseRemoteConfig/Interop/RemoteConfigInterop.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// File.swift
//
//
// Created by Themis Wang on 2023-11-16.
//

import Foundation

@objc(FIRRemoteConfigInterop)
public protocol RemoteConfigInterop {
func registerRolloutsStateSubscriber(_ namespace: String,
subscriber: RolloutsStateSubscriber?)
}
39 changes: 39 additions & 0 deletions FirebaseRemoteConfig/Interop/RolloutAssignment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// File.swift
//
//
// Created by Themis Wang on 2023-11-16.
//

import Foundation

@objc(FIRRolloutAssignment)
public class RolloutAssignment: NSObject {
@objc public var rolloutId: String
@objc public var variantId: String
@objc public var templateVersion: String
@objc public var parameterKey: String
@objc public var parameterValue: String

public init(rolloutId: String, variantId: String, templateVersion: String, parameterKey: String,
parameterValue: String) {
self.rolloutId = rolloutId
self.variantId = variantId
self.templateVersion = templateVersion
self.parameterKey = parameterKey
self.parameterValue = parameterValue
super.init()
}
}

@objc(FIRRolloutsState)
public class RolloutsState: NSObject {
@objc public var assignments: Set<RolloutAssignment> = Set()

public init(assignmentList: [RolloutAssignment]) {
for assignment in assignmentList {
assignments.insert(assignment)
}
super.init()
}
}
13 changes: 13 additions & 0 deletions FirebaseRemoteConfig/Interop/RolloutsStateSubscriber.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// File.swift
//
//
// Created by Themis Wang on 2023-11-16.
//

import Foundation

@objc(FIRRolloutsStateSubscriber)
public protocol RolloutsStateSubscriber {
func onRolloutsStateChanged(_ rolloutsState: RolloutsState)
}
7 changes: 6 additions & 1 deletion FirebaseRemoteConfig/Sources/FIRRemoteConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#import "FirebaseRemoteConfig/Sources/RCNDevice.h"
#import "FirebaseRemoteConfig/Sources/RCNPersonalization.h"

@import FirebaseRemoteConfigInterop;

/// Remote Config Error Domain.
/// TODO: Rename according to obj-c style for constants.
NSString *const FIRRemoteConfigErrorDomain = @"com.google.remoteconfig.ErrorDomain";
Expand Down Expand Up @@ -137,7 +139,9 @@ - (instancetype)initWithAppName:(NSString *)appName
namespace:(NSString *)FIRNamespace
DBManager:(RCNConfigDBManager *)DBManager
configContent:(RCNConfigContent *)configContent
analytics:(nullable id<FIRAnalyticsInterop>)analytics {
analytics:(nullable id<FIRAnalyticsInterop>)analytics
delegate:(nullable id<FIRRemoteConfigComponentDelegate>)
remoteConfigComponentDelegate {
self = [super init];
if (self) {
_appName = appName;
Expand All @@ -164,6 +168,7 @@ - (instancetype)initWithAppName:(NSString *)appName
DBManager:_DBManager
settings:_settings
analytics:analytics
delegate:remoteConfigComponentDelegate
experiment:_configExperiment
queue:_queue
namespace:_FIRNamespace
Expand Down
14 changes: 13 additions & 1 deletion FirebaseRemoteConfig/Sources/FIRRemoteConfigComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import <Foundation/Foundation.h>

#import "FirebaseCore/Extension/FirebaseCoreInternal.h"
@import FirebaseRemoteConfigInterop;

@class FIRApp;
@class FIRRemoteConfig;
Expand All @@ -35,16 +36,27 @@ NS_ASSUME_NONNULL_BEGIN

@end

/// delegate for RC instance to send the updated rollouts state back to Component
@protocol FIRRemoteConfigComponentDelegate
- (void)rolloutsStateDidUpdate:(FIRRolloutsState *)rolloutsState;
@end

/// A concrete implementation for FIRRemoteConfigInterop to create Remote Config instances and
/// register with Core's component system.
@interface FIRRemoteConfigComponent : NSObject <FIRRemoteConfigProvider, FIRLibrary>
@interface FIRRemoteConfigComponent : NSObject <FIRRemoteConfigProvider,
FIRLibrary,
FIRRemoteConfigInterop,
FIRRemoteConfigComponentDelegate>

/// The FIRApp that instances will be set up with.
@property(nonatomic, weak, readonly) FIRApp *app;

/// Cached instances of Remote Config objects.
@property(nonatomic, strong) NSMutableDictionary<NSString *, FIRRemoteConfig *> *instances;

@property(nonatomic, strong)
NSMutableDictionary<NSString *, id<FIRRolloutsStateSubscriber>> *subscribers;

/// Default method for retrieving a Remote Config instance, or creating one if it doesn't exist.
- (FIRRemoteConfig *)remoteConfigForNamespace:(NSString *)remoteConfigNamespace;

Expand Down
23 changes: 21 additions & 2 deletions FirebaseRemoteConfig/Sources/FIRRemoteConfigComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#import "FirebaseRemoteConfig/Sources/RCNConfigDBManager.h"
#import "Interop/Analytics/Public/FIRAnalyticsInterop.h"

@import FirebaseRemoteConfigInterop;

@implementation FIRRemoteConfigComponent

/// Default method for retrieving a Remote Config instance, or creating one if it doesn't exist.
Expand Down Expand Up @@ -60,12 +62,14 @@ - (FIRRemoteConfig *)remoteConfigForNamespace:(NSString *)remoteConfigNamespace
FIRApp *app = self.app;
id<FIRAnalyticsInterop> analytics =
app.isDefaultApp ? FIR_COMPONENT(FIRAnalyticsInterop, app.container) : nil;

instance = [[FIRRemoteConfig alloc] initWithAppName:app.name
FIROptions:app.options
namespace:remoteConfigNamespace
DBManager:[RCNConfigDBManager sharedInstance]
configContent:[RCNConfigContent sharedInstance]
analytics:analytics];
analytics:analytics
delegate:self];
self.instances[remoteConfigNamespace] = instance;
}

Expand All @@ -78,6 +82,7 @@ - (instancetype)initWithApp:(FIRApp *)app {
if (self) {
_app = app;
_instances = [[NSMutableDictionary alloc] initWithCapacity:1];
_subscribers = [[NSMutableDictionary alloc] initWithCapacity:1];
}
return self;
}
Expand All @@ -95,10 +100,13 @@ + (void)load {
+ (NSArray<FIRComponent *> *)componentsToRegister {
FIRDependency *analyticsDep = [FIRDependency dependencyWithProtocol:@protocol(FIRAnalyticsInterop)
isRequired:NO];
FIRDependency *rcInteropDep =
[FIRDependency dependencyWithProtocol:@protocol(FIRRemoteConfigInterop) isRequired:NO];

FIRComponent *rcProvider = [FIRComponent
componentWithProtocol:@protocol(FIRRemoteConfigProvider)
instantiationTiming:FIRInstantiationTimingAlwaysEager
dependencies:@[ analyticsDep ]
dependencies:@[ analyticsDep, rcInteropDep ]
creationBlock:^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
// Cache the component so instances of Remote Config are cached.
*isCacheable = YES;
Expand All @@ -107,4 +115,15 @@ + (void)load {
return @[ rcProvider ];
}

- (void)rolloutsStateDidUpdate:(nonnull FIRRolloutsState *)rolloutsState {
// calling the subscriber to update the change
[self.subscribers[@"nameSpace"] onRolloutsStateChanged:rolloutsState];
}

- (void)registerRolloutsStateSubscriber:(nonnull NSString *)nameSpace
subscriber:(nullable id<FIRRolloutsStateSubscriber>)subscriber {
// adding the registered subscriber reference
self.subscribers[nameSpace] = subscriber;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
@class RCNConfigDBManager;
@class RCNConfigFetch;
@class RCNConfigRealtime;

@protocol FIRRemoteConfigComponentDelegate;
@protocol FIRAnalyticsInterop;

NS_ASSUME_NONNULL_BEGIN
Expand Down Expand Up @@ -76,7 +78,8 @@ NS_ASSUME_NONNULL_BEGIN
namespace:(NSString *)FIRNamespace
DBManager:(RCNConfigDBManager *)DBManager
configContent:(RCNConfigContent *)configContent
analytics:(nullable id<FIRAnalyticsInterop>)analytics;
analytics:(nullable id<FIRAnalyticsInterop>)analytics
delegate:(nullable id<FIRRemoteConfigComponentDelegate>)remoteConfig;

@end

Expand Down
2 changes: 2 additions & 0 deletions FirebaseRemoteConfig/Sources/Private/RCNConfigFetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@class RCNConfigSettings;
@class RCNConfigExperiment;
@class RCNConfigDBManager;
@protocol FIRRemoteConfigComponentDelegate;

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -44,6 +45,7 @@ typedef void (^RCNConfigFetchCompletion)(FIRRemoteConfigFetchStatus status,
DBManager:(RCNConfigDBManager *)DBManager
settings:(RCNConfigSettings *)settings
analytics:(nullable id<FIRAnalyticsInterop>)analytics
delegate:(nullable id<FIRRemoteConfigComponentDelegate>)delegate
experiment:(nullable RCNConfigExperiment *)experiment
queue:(dispatch_queue_t)queue
namespace:(NSString *)firebaseNamespace
Expand Down
3 changes: 3 additions & 0 deletions FirebaseRemoteConfig/Sources/RCNConfigFetch.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ @implementation RCNConfigFetch {
RCNConfigContent *_content;
RCNConfigSettings *_settings;
id<FIRAnalyticsInterop> _analytics;
id<FIRRemoteConfigComponentDelegate> _delegate;
RCNConfigExperiment *_experiment;
dispatch_queue_t _lockQueue; /// Guard the read/write operation.
NSURLSession *_fetchSession; /// Managed internally by the fetch instance.
Expand All @@ -91,6 +92,7 @@ - (instancetype)initWithContent:(RCNConfigContent *)content
DBManager:(RCNConfigDBManager *)DBManager
settings:(RCNConfigSettings *)settings
analytics:(nullable id<FIRAnalyticsInterop>)analytics
delegate:(nullable id<FIRRemoteConfigComponentDelegate>)delegate
experiment:(RCNConfigExperiment *)experiment
queue:(dispatch_queue_t)queue
namespace:(NSString *)FIRNamespace
Expand All @@ -100,6 +102,7 @@ - (instancetype)initWithContent:(RCNConfigContent *)content
_FIRNamespace = FIRNamespace;
_settings = settings;
_analytics = analytics;
_delegate = delegate;
_experiment = experiment;
_lockQueue = queue;
_content = content;
Expand Down
25 changes: 20 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,16 @@ let package = Package(
),
.target(
name: "FirebaseCrashlytics",
dependencies: ["FirebaseCore", "FirebaseInstallations", "FirebaseSessions",
.product(name: "GoogleDataTransport", package: "GoogleDataTransport"),
.product(name: "GULEnvironment", package: "GoogleUtilities"),
.product(name: "FBLPromises", package: "Promises"),
.product(name: "nanopb", package: "nanopb")],
dependencies: [
"FirebaseCore",
"FirebaseInstallations",
"FirebaseSessions",
"FirebaseRemoteConfigInterop",
.product(name: "GoogleDataTransport", package: "GoogleDataTransport"),
.product(name: "GULEnvironment", package: "GoogleUtilities"),
.product(name: "FBLPromises", package: "Promises"),
.product(name: "nanopb", package: "nanopb"),
],
path: "Crashlytics",
exclude: [
"run",
Expand Down Expand Up @@ -957,6 +962,7 @@ let package = Package(
"FirebaseCore",
"FirebaseABTesting",
"FirebaseInstallations",
"FirebaseRemoteConfigInterop",
.product(name: "GULNSData", package: "GoogleUtilities"),
],
path: "FirebaseRemoteConfig/Sources",
Expand Down Expand Up @@ -1028,6 +1034,15 @@ let package = Package(
.headerSearchPath("../../../"),
]
),
// Internal headers only for consuming from other SDK.
.target(
name: "FirebaseRemoteConfigInterop",
path: "FirebaseRemoteConfig/Interop",
publicHeadersPath: ".",
cSettings: [
.headerSearchPath("../../"),
]
),

// MARK: - Firebase Sessions

Expand Down

0 comments on commit e95fac6

Please sign in to comment.