FIRIAMClearcutLoggerTests.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FirebaseInAppMessaging/Sources/Analytics/FIRIAMClearcutHttpRequestSender.h"
  19. #import "FirebaseInAppMessaging/Sources/Analytics/FIRIAMClearcutLogStorage.h"
  20. #import "FirebaseInAppMessaging/Sources/Private/Analytics/FIRIAMClearcutLogger.h"
  21. #import "FirebaseInAppMessaging/Sources/Private/Analytics/FIRIAMClearcutUploader.h"
  22. @interface FIRIAMClearcutLoggerTests : XCTestCase
  23. @property(nonatomic) FIRIAMClientInfoFetcher *mockClientInfoFetcher;
  24. @property(nonatomic) id<FIRIAMTimeFetcher> mockTimeFetcher;
  25. @property(nonatomic) FIRIAMClearcutHttpRequestSender *mockRequestSender;
  26. @property(nonatomic) FIRIAMClearcutUploader *mockCtUploader;
  27. @end
  28. NSString *iid = @"my iid";
  29. NSString *osVersion = @"iOS version";
  30. NSString *sdkVersion = @"SDK version";
  31. // we need to access the some internal things in FIRIAMClearcutLogger in our unit tests
  32. // verifications
  33. @interface FIRIAMClearcutLogger (UnitTestAccess)
  34. @property(readonly, nonatomic) FIRIAMClearcutLogStorage *retryStorage;
  35. @property(nonatomic) FIRIAMClearcutHttpRequestSender *requestSender;
  36. @property(nonatomic) id<FIRIAMTimeFetcher> timeFetcher;
  37. - (void)checkAndRetryClearcutLogs;
  38. @end
  39. @interface FIRIAMClearcutLogStorage (UnitTestAccess)
  40. @property(nonatomic) NSMutableArray<FIRIAMClearcutLogRecord *> *records;
  41. @end
  42. @implementation FIRIAMClearcutLoggerTests
  43. - (void)setUp {
  44. [super setUp];
  45. self.mockTimeFetcher = OCMProtocolMock(@protocol(FIRIAMTimeFetcher));
  46. self.mockClientInfoFetcher = OCMClassMock(FIRIAMClientInfoFetcher.class);
  47. self.mockRequestSender = OCMClassMock(FIRIAMClearcutHttpRequestSender.class);
  48. self.mockCtUploader = OCMClassMock(FIRIAMClearcutUploader.class);
  49. OCMStub([self.mockClientInfoFetcher
  50. fetchFirebaseInstallationDataWithProjectNumber:[OCMArg any]
  51. withCompletion:([OCMArg invokeBlockWithArgs:iid, @"token",
  52. [NSNull null],
  53. nil])]);
  54. OCMStub([self.mockClientInfoFetcher getIAMSDKVersion]).andReturn(sdkVersion);
  55. OCMStub([self.mockClientInfoFetcher getOSVersion]).andReturn(osVersion);
  56. }
  57. - (void)tearDown {
  58. // Put teardown code here. This method is called after the invocation of each test method in the
  59. // class.
  60. [super tearDown];
  61. }
  62. // verify that the produced FIRIAMClearcutLogRecord record has expected content for
  63. // the event extension json string
  64. - (void)testEventLogBodyContent_Expected {
  65. NSString *fbProjectNumber = @"clearcutserver";
  66. NSString *fbAppId = @"test Firebase app";
  67. FIRIAMClearcutLogger *logger =
  68. [[FIRIAMClearcutLogger alloc] initWithFBProjectNumber:fbProjectNumber
  69. fbAppId:fbAppId
  70. clientInfoFetcher:self.mockClientInfoFetcher
  71. usingTimeFetcher:self.mockTimeFetcher
  72. usingUploader:self.mockCtUploader];
  73. NSTimeInterval eventMoment = 10000;
  74. __block NSDictionary *capturedEventDict;
  75. OCMExpect([self.mockCtUploader
  76. addNewLogRecord:[OCMArg checkWithBlock:^BOOL(FIRIAMClearcutLogRecord *newLogRecord) {
  77. NSString *jsonString = newLogRecord.eventExtensionJsonString;
  78. capturedEventDict = [NSJSONSerialization
  79. JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
  80. options:kNilOptions
  81. error:nil];
  82. return (int)newLogRecord.eventTimestampInSeconds == (int)eventMoment;
  83. }]]);
  84. NSString *campaignID = @"test campaign";
  85. [logger logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
  86. forCampaignID:campaignID
  87. withCampaignName:@"name"
  88. eventTimeInMs:[NSNumber numberWithInteger:eventMoment * 1000]
  89. completion:^(BOOL success){
  90. }];
  91. OCMVerifyAll((id)self.mockCtUploader);
  92. XCTAssertEqualObjects(@"CLICK_EVENT_TYPE", capturedEventDict[@"event_type"]);
  93. XCTAssertEqualObjects(fbProjectNumber, capturedEventDict[@"project_number"]);
  94. XCTAssertEqualObjects(campaignID, capturedEventDict[@"campaign_id"]);
  95. XCTAssertEqualObjects(fbAppId, capturedEventDict[@"client_app"][@"google_app_id"]);
  96. XCTAssertEqualObjects(iid, capturedEventDict[@"client_app"][@"firebase_instance_id"]);
  97. XCTAssertEqualObjects(sdkVersion, capturedEventDict[@"fiam_sdk_version"]);
  98. }
  99. // calling logAnalyticsEventForType with event time set to nil
  100. - (void)testNilEventTimestamp {
  101. FIRIAMClearcutLogger *logger =
  102. [[FIRIAMClearcutLogger alloc] initWithFBProjectNumber:@"clearcutserver"
  103. fbAppId:@"test Firebase app"
  104. clientInfoFetcher:self.mockClientInfoFetcher
  105. usingTimeFetcher:self.mockTimeFetcher
  106. usingUploader:self.mockCtUploader];
  107. NSTimeInterval currentMoment = 10000;
  108. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
  109. OCMExpect([self.mockCtUploader
  110. addNewLogRecord:[OCMArg checkWithBlock:^BOOL(FIRIAMClearcutLogRecord *newLogRecord) {
  111. return (int)newLogRecord.eventTimestampInSeconds == (int)currentMoment;
  112. }]]);
  113. [logger logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
  114. forCampaignID:@"test campaign"
  115. withCampaignName:@"name"
  116. eventTimeInMs:nil
  117. completion:^(BOOL success){
  118. }];
  119. OCMVerifyAll((id)self.mockCtUploader);
  120. }
  121. @end