| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- /*
- * Copyright 2018 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #import <OCMock/OCMock.h>
- #import <XCTest/XCTest.h>
- #import "FIRIAMAnalyticsEventLoggerImpl.h"
- #import "FIRIAMClearcutLogger.h"
- #import <FirebaseAnalyticsInterop/FIRAnalyticsInterop.h>
- #import <FirebaseAnalyticsInterop/FIRInteropEventNames.h>
- #import <FirebaseAnalyticsInterop/FIRInteropParameterNames.h>
- @interface FIRIAMAnalyticsEventLoggerImplTests : XCTestCase
- @property(nonatomic) FIRIAMClearcutLogger *mockClearcutLogger;
- @property(nonatomic) id<FIRIAMTimeFetcher> mockTimeFetcher;
- @property(nonatomic) id mockFirebaseAnalytics;
- @property(nonatomic) NSUserDefaults *mockUserDefaults;
- @end
- static NSString *campaignID = @"campaign id";
- static NSString *campaignName = @"campaign name";
- typedef void (^FIRAUserPropertiesCallback)(NSDictionary *userProperties);
- typedef void (^FakeAnalyticsLogEventHandler)(NSString *origin,
- NSString *name,
- NSDictionary *parameters);
- typedef void (^FakeAnalyticsUserPropertyHandler)(NSString *origin, NSString *name, id value);
- typedef void (^LastNotificationCallback)(NSString *);
- typedef void (^FakeAnalyticsLastNotificationHandler)(NSString *origin, LastNotificationCallback);
- @interface FakeAnalytics : NSObject <FIRAnalyticsInterop>
- @property FakeAnalyticsLogEventHandler eventHandler;
- @property FakeAnalyticsLogEventHandler userPropertyHandler;
- @property FakeAnalyticsLastNotificationHandler lastNotificationHandler;
- - (instancetype)initWithEventHandler:(FakeAnalyticsLogEventHandler)eventHandler;
- - (instancetype)initWithUserPropertyHandler:(FakeAnalyticsUserPropertyHandler)userPropertyHandler;
- @end
- @implementation FakeAnalytics
- - (instancetype)initWithEventHandler:(FakeAnalyticsLogEventHandler)eventHandler {
- self = [super init];
- if (self) {
- _eventHandler = eventHandler;
- }
- return self;
- }
- - (instancetype)initWithUserPropertyHandler:(FakeAnalyticsUserPropertyHandler)userPropertyHandler {
- self = [super init];
- if (self) {
- _userPropertyHandler = userPropertyHandler;
- }
- return self;
- }
- - (void)logEventWithOrigin:(nonnull NSString *)origin
- name:(nonnull NSString *)name
- parameters:(nullable NSDictionary<NSString *, id> *)parameters {
- if (_eventHandler) {
- _eventHandler(origin, name, parameters);
- }
- }
- - (void)setUserPropertyWithOrigin:(nonnull NSString *)origin
- name:(nonnull NSString *)name
- value:(nonnull id)value {
- if (_userPropertyHandler) {
- _userPropertyHandler(origin, name, value);
- }
- }
- - (void)checkLastNotificationForOrigin:(nonnull NSString *)origin
- queue:(nonnull dispatch_queue_t)queue
- callback:(nonnull void (^)(NSString *_Nullable))
- currentLastNotificationProperty {
- if (_lastNotificationHandler) {
- _lastNotificationHandler(origin, currentLastNotificationProperty);
- }
- }
- // Stubs
- - (void)clearConditionalUserProperty:(nonnull NSString *)userPropertyName
- clearEventName:(nonnull NSString *)clearEventName
- clearEventParameters:(nonnull NSDictionary *)clearEventParameters {
- }
- - (NSInteger)maxUserProperties:(nonnull NSString *)origin {
- return -1;
- }
- - (void)registerAnalyticsListener:(nonnull id<FIRAnalyticsInteropListener>)listener
- withOrigin:(nonnull NSString *)origin {
- }
- - (void)unregisterAnalyticsListenerWithOrigin:(nonnull NSString *)origin {
- }
- - (void)clearConditionalUserProperty:(nonnull NSString *)userPropertyName
- forOrigin:(nonnull NSString *)origin
- clearEventName:(nonnull NSString *)clearEventName
- clearEventParameters:
- (nonnull NSDictionary<NSString *, NSString *> *)clearEventParameters {
- }
- - (nonnull NSArray<NSDictionary<NSString *, NSString *> *> *)
- conditionalUserProperties:(nonnull NSString *)origin
- propertyNamePrefix:(nonnull NSString *)propertyNamePrefix {
- return nil;
- }
- - (void)setConditionalUserProperty:(nonnull NSDictionary<NSString *, id> *)conditionalUserProperty {
- }
- - (void)getUserPropertiesWithCallback:(nonnull FIRAInteropUserPropertiesCallback)callback {
- }
- @end
- @implementation FIRIAMAnalyticsEventLoggerImplTests
- - (void)setUp {
- [super setUp];
- self.mockClearcutLogger = OCMClassMock(FIRIAMClearcutLogger.class);
- self.mockTimeFetcher = OCMProtocolMock(@protocol(FIRIAMTimeFetcher));
- self.mockUserDefaults = OCMClassMock(NSUserDefaults.class);
- }
- - (void)tearDown {
- [super tearDown];
- }
- - (void)testLogImpressionEvent {
- XCTestExpectation *expectation1 = [self expectationWithDescription:@"Log to Analytics"];
- FakeAnalytics *analytics = [[FakeAnalytics alloc]
- initWithEventHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
- XCTAssertEqualObjects(origin, @"fiam");
- XCTAssertEqualObjects(name, @"firebase_in_app_message_impression");
- XCTAssertEqual([parameters count], 3);
- XCTAssertNotNil(parameters);
- XCTAssertEqual(parameters[@"_nmid"], campaignID);
- XCTAssertEqual(parameters[@"_nmn"], campaignName);
- [expectation1 fulfill];
- }];
- FIRIAMAnalyticsEventLoggerImpl *logger =
- [[FIRIAMAnalyticsEventLoggerImpl alloc] initWithClearcutLogger:self.mockClearcutLogger
- usingTimeFetcher:self.mockTimeFetcher
- usingUserDefaults:nil
- analytics:analytics];
- NSTimeInterval currentMoment = 10000;
- OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
- OCMExpect([self.mockClearcutLogger
- logAnalyticsEventForType:FIRIAMAnalyticsEventMessageImpression
- forCampaignID:[OCMArg isEqual:campaignID]
- withCampaignName:[OCMArg isEqual:campaignName]
- eventTimeInMs:[OCMArg isNil]
- completion:([OCMArg invokeBlockWithArgs:@YES, nil])]);
- XCTestExpectation *expectation2 =
- [self expectationWithDescription:@"Completion Callback Triggered"];
- [logger logAnalyticsEventForType:FIRIAMAnalyticsEventMessageImpression
- forCampaignID:campaignID
- withCampaignName:campaignName
- eventTimeInMs:nil
- completion:^(BOOL success) {
- [expectation2 fulfill];
- }];
- [self waitForExpectationsWithTimeout:1.0 handler:nil];
- }
- - (void)testLogActionEvent {
- XCTestExpectation *expectation1 = [self expectationWithDescription:@"Log to Analytics"];
- FakeAnalytics *analytics = [[FakeAnalytics alloc]
- initWithEventHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
- XCTAssertEqualObjects(origin, @"fiam");
- XCTAssertEqualObjects(name, @"firebase_in_app_message_action");
- XCTAssertEqual([parameters count], 3);
- XCTAssertNotNil(parameters);
- XCTAssertEqual(parameters[@"_nmid"], campaignID);
- XCTAssertEqual(parameters[@"_nmn"], campaignName);
- [expectation1 fulfill];
- }];
- FIRIAMAnalyticsEventLoggerImpl *logger =
- [[FIRIAMAnalyticsEventLoggerImpl alloc] initWithClearcutLogger:self.mockClearcutLogger
- usingTimeFetcher:self.mockTimeFetcher
- usingUserDefaults:self.mockUserDefaults
- analytics:analytics];
- NSTimeInterval currentMoment = 10000;
- OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
- OCMExpect([self.mockClearcutLogger
- logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
- forCampaignID:[OCMArg isEqual:campaignID]
- withCampaignName:[OCMArg isEqual:campaignName]
- eventTimeInMs:[OCMArg isNil]
- completion:([OCMArg invokeBlockWithArgs:@YES, nil])]);
- XCTestExpectation *expectation2 =
- [self expectationWithDescription:@"Completion Callback Triggered"];
- [logger logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
- forCampaignID:campaignID
- withCampaignName:campaignName
- eventTimeInMs:nil
- completion:^(BOOL success) {
- [expectation2 fulfill];
- }];
- [self waitForExpectationsWithTimeout:2.0 handler:nil];
- OCMVerifyAll((id)self.mockClearcutLogger);
- }
- @end
|