FIRIAMClearcutLoggerTests.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "FIRIAMClearcutHttpRequestSender.h"
  19. #import "FIRIAMClearcutLogStorage.h"
  20. #import "FIRIAMClearcutLogger.h"
  21. #import "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. fetchFirebaseIIDDataWithProjectNumber:[OCMArg any]
  51. withCompletion:([OCMArg invokeBlockWithArgs:iid, @"token",
  52. [NSNull null], nil])]);
  53. OCMStub([self.mockClientInfoFetcher getIAMSDKVersion]).andReturn(sdkVersion);
  54. OCMStub([self.mockClientInfoFetcher getOSVersion]).andReturn(osVersion);
  55. }
  56. - (void)tearDown {
  57. // Put teardown code here. This method is called after the invocation of each test method in the
  58. // class.
  59. [super tearDown];
  60. }
  61. // verify that the produced FIRIAMClearcutLogRecord record has expected content for
  62. // the event extension json string
  63. - (void)testEventLogBodyContent_Expected {
  64. NSString *fbProjectNumber = @"clearcutserver";
  65. NSString *fbAppId = @"test Firebase app";
  66. FIRIAMClearcutLogger *logger =
  67. [[FIRIAMClearcutLogger alloc] initWithFBProjectNumber:fbProjectNumber
  68. fbAppId:fbAppId
  69. clientInfoFetcher:self.mockClientInfoFetcher
  70. usingTimeFetcher:self.mockTimeFetcher
  71. usingUploader:self.mockCtUploader];
  72. NSTimeInterval eventMoment = 10000;
  73. __block NSDictionary *capturedEventDict;
  74. OCMExpect([self.mockCtUploader
  75. addNewLogRecord:[OCMArg checkWithBlock:^BOOL(FIRIAMClearcutLogRecord *newLogRecord) {
  76. NSString *jsonString = newLogRecord.eventExtensionJsonString;
  77. capturedEventDict = [NSJSONSerialization
  78. JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
  79. options:kNilOptions
  80. error:nil];
  81. return (int)newLogRecord.eventTimestampInSeconds == (int)eventMoment;
  82. }]]);
  83. NSString *campaignID = @"test campaign";
  84. [logger logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
  85. forCampaignID:campaignID
  86. withCampaignName:@"name"
  87. eventTimeInMs:[NSNumber numberWithInteger:eventMoment * 1000]
  88. completion:^(BOOL success){
  89. }];
  90. OCMVerifyAll((id)self.mockCtUploader);
  91. XCTAssertEqualObjects(@"CLICK_EVENT_TYPE", capturedEventDict[@"event_type"]);
  92. XCTAssertEqualObjects(fbProjectNumber, capturedEventDict[@"project_number"]);
  93. XCTAssertEqualObjects(campaignID, capturedEventDict[@"campaign_id"]);
  94. XCTAssertEqualObjects(fbAppId, capturedEventDict[@"client_app"][@"google_app_id"]);
  95. XCTAssertEqualObjects(iid, capturedEventDict[@"client_app"][@"firebase_instance_id"]);
  96. XCTAssertEqualObjects(sdkVersion, capturedEventDict[@"fiam_sdk_version"]);
  97. }
  98. // calling logAnalyticsEventForType with event time set to nil
  99. - (void)testNilEventTimestamp {
  100. FIRIAMClearcutLogger *logger =
  101. [[FIRIAMClearcutLogger alloc] initWithFBProjectNumber:@"clearcutserver"
  102. fbAppId:@"test Firebase app"
  103. clientInfoFetcher:self.mockClientInfoFetcher
  104. usingTimeFetcher:self.mockTimeFetcher
  105. usingUploader:self.mockCtUploader];
  106. NSTimeInterval currentMoment = 10000;
  107. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
  108. OCMExpect([self.mockCtUploader
  109. addNewLogRecord:[OCMArg checkWithBlock:^BOOL(FIRIAMClearcutLogRecord *newLogRecord) {
  110. return (int)newLogRecord.eventTimestampInSeconds == (int)currentMoment;
  111. }]]);
  112. [logger logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
  113. forCampaignID:@"test campaign"
  114. withCampaignName:@"name"
  115. eventTimeInMs:nil
  116. completion:^(BOOL success){
  117. }];
  118. OCMVerifyAll((id)self.mockCtUploader);
  119. }
  120. @end