FPRClientTest.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations+Private.h"
  16. #import "FirebasePerformance/Sources/FPRClient+Private.h"
  17. #import "FirebasePerformance/Sources/FPRClient.h"
  18. #import "FirebasePerformance/Sources/Loggers/FPRGDTLogger.h"
  19. #import "FirebasePerformance/Sources/Loggers/FPRGDTLogger_Private.h"
  20. #import "FirebasePerformance/Tests/Unit/Configurations/FPRFakeRemoteConfig.h"
  21. #import "FirebasePerformance/Tests/Unit/FPRTestCase.h"
  22. #import "FirebasePerformance/Tests/Unit/FPRTestUtils.h"
  23. #import "FirebasePerformance/Tests/Unit/Fakes/FPRFakeConfigurations.h"
  24. #import "FirebasePerformance/Tests/Unit/Fakes/FPRFakeInstallations.h"
  25. #import <OCMock/OCMock.h>
  26. #import "GoogleDataTransport/GDTCORTests/Common/Fakes/GDTCORTransportFake.h"
  27. @interface FPRClientTest : FPRTestCase
  28. /** Configuration which can be assigned as a fake object for event dispatch control. */
  29. @property(nonatomic) FPRConfigurations *configurations;
  30. /** Fireperf client object which can be used for fake object injection and assertion . */
  31. @property(nonatomic) FPRClient *client;
  32. @end
  33. @implementation FPRClientTest
  34. - (void)setUp {
  35. [super setUp];
  36. // Arrange installations object.
  37. FPRFakeInstallations *installations = [FPRFakeInstallations installations];
  38. self.client = [[FPRClient alloc] init];
  39. installations.identifier = @"mockId";
  40. self.client.installations = (FIRInstallations *)installations;
  41. // Arrange remote config object.
  42. FPRFakeConfigurations *fakeConfigs =
  43. [[FPRFakeConfigurations alloc] initWithSources:FPRConfigurationSourceRemoteConfig];
  44. self.configurations = fakeConfigs;
  45. fakeConfigs.dataCollectionEnabled = YES;
  46. fakeConfigs.sdkEnabled = YES;
  47. self.client.configuration = self.configurations;
  48. // Arrange gdtLogger object for event dispatch.
  49. self.client.gdtLogger = [[FPRGDTLogger alloc] initWithLogSource:1];
  50. GDTCORTransportFake *fakeGdtTransport =
  51. [[GDTCORTransportFake alloc] initWithMappingID:@"1" transformers:nil target:kGDTCORTargetFLL];
  52. self.client.gdtLogger.gdtfllTransport = fakeGdtTransport;
  53. }
  54. /** Validates if the gdtTransport logger has received trace perfMetric. */
  55. - (void)testLogAndProcessEventsForTrace {
  56. // Trace type PerfMetric for event dispatch.
  57. FPRMSGPerfMetric *perfMetric = [FPRTestUtils createRandomPerfMetric:@"RandomTrace"];
  58. // Act on event logging call.
  59. [self.client processAndLogEvent:perfMetric];
  60. // Wait for async job to execute event logging.
  61. dispatch_group_wait(self.client.eventsQueueGroup, DISPATCH_TIME_FOREVER);
  62. // Validate the event is received by gdtTransport logger.
  63. dispatch_sync(self.client.gdtLogger.queue, ^{
  64. GDTCORTransportFake *fakeGdtTransport =
  65. (GDTCORTransportFake *)self.client.gdtLogger.gdtfllTransport;
  66. XCTAssertEqual(fakeGdtTransport.logEvents.count, 1);
  67. GDTCOREvent *event = fakeGdtTransport.logEvents.firstObject;
  68. XCTAssertEqualObjects([event.dataObject transportBytes], perfMetric.data);
  69. });
  70. }
  71. /** Validates if the gdtTransport logger has received network trace perfMetric. */
  72. - (void)testLogAndProcessEventsForNetworkTrace {
  73. // Network type PerfMetric for event dispatch.
  74. FPRMSGPerfMetric *perfMetric = [FPRTestUtils createRandomNetworkPerfMetric:@"https://abc.xyz"];
  75. // Act on event logging call.
  76. [self.client processAndLogEvent:perfMetric];
  77. // Wait for async job to execute event logging.
  78. dispatch_group_wait(self.client.eventsQueueGroup, DISPATCH_TIME_FOREVER);
  79. // Validate the event is received by gdtTransport logger.
  80. dispatch_sync(self.client.gdtLogger.queue, ^{
  81. GDTCORTransportFake *fakeGdtTransport =
  82. (GDTCORTransportFake *)self.client.gdtLogger.gdtfllTransport;
  83. XCTAssertEqual(fakeGdtTransport.logEvents.count, 1);
  84. GDTCOREvent *event = fakeGdtTransport.logEvents.firstObject;
  85. XCTAssertEqualObjects([event.dataObject transportBytes], perfMetric.data);
  86. });
  87. }
  88. /** Validates if the gdtTransport logger has received session gauge perfMetric. */
  89. - (void)testLogAndProcessEventsForGauge {
  90. // Gauge type PerfMetric for event dispatch.
  91. FPRMSGPerfMetric *perfMetric = [FPRTestUtils createRandomGaugePerfMetric];
  92. // Act on event logging call.
  93. [self.client processAndLogEvent:perfMetric];
  94. // Wait for async job to execute event logging.
  95. dispatch_group_wait(self.client.eventsQueueGroup, DISPATCH_TIME_FOREVER);
  96. // Validate the event is received by gdtTransport logger.
  97. dispatch_sync(self.client.gdtLogger.queue, ^{
  98. GDTCORTransportFake *fakeGdtTransport =
  99. (GDTCORTransportFake *)self.client.gdtLogger.gdtfllTransport;
  100. XCTAssertEqual(fakeGdtTransport.logEvents.count, 1);
  101. GDTCOREvent *event = fakeGdtTransport.logEvents.firstObject;
  102. XCTAssertEqualObjects([event.dataObject transportBytes], perfMetric.data);
  103. });
  104. }
  105. /** Validates if the gdtTransport logger will not receive event when data collection is disabled. */
  106. - (void)testLogAndProcessEventsNotDispatchWhenDisabled {
  107. // Trace type PerfMetric for event dispatch.
  108. FPRMSGPerfMetric *perfMetric = [FPRTestUtils createRandomPerfMetric:@"RandomTrace"];
  109. // Act on event logging call when data collection is disabled.
  110. self.configurations.dataCollectionEnabled = NO;
  111. [self.client processAndLogEvent:perfMetric];
  112. // Wait for async job to execute event logging.
  113. dispatch_group_wait(self.client.eventsQueueGroup, DISPATCH_TIME_FOREVER);
  114. // Validate the event is not received by gdtTransport logger.
  115. dispatch_sync(self.client.gdtLogger.queue, ^{
  116. GDTCORTransportFake *fakeGdtTransport =
  117. (GDTCORTransportFake *)self.client.gdtLogger.gdtfllTransport;
  118. XCTAssertEqual(fakeGdtTransport.logEvents.count, 0);
  119. });
  120. }
  121. /** Validates if the gdtTransport logger will resume receiving event when data collection is
  122. * re-enabled. */
  123. - (void)testLogAndProcessEventsAfterReenabled {
  124. // Trace type PerfMetric for event dispatch.
  125. FPRMSGPerfMetric *perfMetric = [FPRTestUtils createRandomPerfMetric:@"RandomTrace"];
  126. // Act on event logging call when data collection is disabled.
  127. self.configurations.dataCollectionEnabled = NO;
  128. [self.client processAndLogEvent:perfMetric];
  129. // Wait for async job to execute event logging.
  130. dispatch_group_wait(self.client.eventsQueueGroup, DISPATCH_TIME_FOREVER);
  131. // Validate the event is not received by gdtTransport logger.
  132. dispatch_sync(self.client.gdtLogger.queue, ^{
  133. GDTCORTransportFake *fakeGdtTransport =
  134. (GDTCORTransportFake *)self.client.gdtLogger.gdtfllTransport;
  135. XCTAssertEqual(fakeGdtTransport.logEvents.count, 0);
  136. });
  137. // Act on event logging call after re-enable data collection.
  138. self.configurations.dataCollectionEnabled = YES;
  139. [self.client processAndLogEvent:perfMetric];
  140. // Wait for async job to execute event logging.
  141. dispatch_group_wait(self.client.eventsQueueGroup, DISPATCH_TIME_FOREVER);
  142. // Validate the event is received by gdtTransport logger.
  143. dispatch_sync(self.client.gdtLogger.queue, ^{
  144. GDTCORTransportFake *fakeGdtTransport =
  145. (GDTCORTransportFake *)self.client.gdtLogger.gdtfllTransport;
  146. XCTAssertEqual(fakeGdtTransport.logEvents.count, 1);
  147. });
  148. }
  149. /** Validates that the Clearcut log directory removal method is called. */
  150. - (void)testClearcutLogDirectoryCleanupInitiates {
  151. id clientMock = OCMClassMock(self.client.class);
  152. [self.client startWithConfiguration:[[FPRConfiguration alloc] initWithAppID:@"RandomAppId"
  153. APIKey:nil
  154. autoPush:YES]
  155. error:nil];
  156. // Wait for async job to initiate cleanup logic.
  157. dispatch_group_wait(self.client.eventsQueueGroup, DISPATCH_TIME_FOREVER);
  158. OCMVerify([clientMock cleanupClearcutCacheDirectory]);
  159. }
  160. /**
  161. * Validates that the log directory path in the cache directory created for Clearcut logs storage
  162. * gets removed (if exist).
  163. */
  164. - (void)testValidateClearcutLogDirectoryCleanupIfExists {
  165. // Create the log directory and make sure it exists.
  166. NSString *logDirectoryPath = [FPRClient logDirectoryPath];
  167. [[NSFileManager defaultManager] createDirectoryAtPath:logDirectoryPath
  168. withIntermediateDirectories:YES
  169. attributes:nil
  170. error:nil];
  171. BOOL logDirectoryExists = [[NSFileManager defaultManager] fileExistsAtPath:logDirectoryPath];
  172. XCTAssertTrue(logDirectoryExists);
  173. [FPRClient cleanupClearcutCacheDirectory];
  174. logDirectoryExists = [[NSFileManager defaultManager] fileExistsAtPath:logDirectoryPath];
  175. XCTAssertFalse(logDirectoryExists);
  176. }
  177. /**
  178. * Validates that the Clearcut log directory path removal logic doesn't explode if directory doesn't
  179. * exist.
  180. */
  181. - (void)testValidateClearcutLogDirectoryCleanupIfNotExists {
  182. NSString *logDirectoryPath = [FPRClient logDirectoryPath];
  183. BOOL logDirectoryExists = [[NSFileManager defaultManager] fileExistsAtPath:logDirectoryPath];
  184. XCTAssertFalse(logDirectoryExists);
  185. XCTAssertNoThrow([FPRClient cleanupClearcutCacheDirectory]);
  186. }
  187. @end