FIRPerformanceTest.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/Common/FPRConstants.h"
  16. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  17. #import "FirebasePerformance/Sources/FIRPerformance+Internal.h"
  18. #import "FirebasePerformance/Sources/FIRPerformance_Private.h"
  19. #import "FirebasePerformance/Sources/FPRClient+Private.h"
  20. #import "FirebasePerformance/Sources/FPRClient.h"
  21. #import "FirebasePerformance/Sources/Public/FIRPerformance.h"
  22. #import "FirebasePerformance/Sources/Timer/FIRTrace+Internal.h"
  23. #import "FirebasePerformance/Tests/Unit/Fakes/FPRFakeClient.h"
  24. #import "FirebasePerformance/Tests/Unit/FPRTestCase.h"
  25. #import "FirebaseCore/Sources/Private/FIRAppInternal.h"
  26. @interface FIRPerformanceTest : FPRTestCase
  27. @property(nonatomic) FIRPerformance *performance;
  28. @end
  29. @implementation FIRPerformanceTest
  30. - (void)setUp {
  31. [super setUp];
  32. self.performance = [[FIRPerformance alloc] init];
  33. self.performance.fprClient = [[FPRFakeClient alloc] init];
  34. [self.performance setDataCollectionEnabled:YES];
  35. [self.performance setInstrumentationEnabled:NO];
  36. }
  37. - (void)tearDown {
  38. [super tearDown];
  39. [self.performance setDataCollectionEnabled:NO];
  40. NSArray<NSString *> *allKeys = self.performance.attributes.allKeys;
  41. [allKeys enumerateObjectsUsingBlock:^(NSString *attribute, NSUInteger idx, BOOL *stop) {
  42. [self.performance removeAttribute:attribute];
  43. }];
  44. }
  45. /** Validates that the singleton instance creation works. */
  46. - (void)testSingleton {
  47. XCTAssertEqual([FIRPerformance sharedInstance], [FIRPerformance sharedInstance]);
  48. }
  49. /** Validates that toggling of enabling/disabling performance collection works. */
  50. - (void)testToggleDisablingPerformanceCollection {
  51. self.appFake.fakeIsDataCollectionDefaultEnabled = YES;
  52. [self.performance setDataCollectionEnabled:YES];
  53. XCTAssertTrue(self.performance.dataCollectionEnabled);
  54. [self.performance setDataCollectionEnabled:NO];
  55. XCTAssertFalse(self.performance.dataCollectionEnabled);
  56. [self.performance setDataCollectionEnabled:YES];
  57. XCTAssertTrue(self.performance.dataCollectionEnabled);
  58. }
  59. /** Validates that toggling of enabling/disabling performance instrumentation works. */
  60. - (void)testToggleDisablingPerformanceInstrumentation {
  61. [self.performance setInstrumentationEnabled:YES];
  62. XCTAssertTrue(self.performance.instrumentationEnabled);
  63. [self.performance setInstrumentationEnabled:NO];
  64. XCTAssertFalse([FPRConfigurations sharedInstance].isInstrumentationEnabled);
  65. XCTAssertTrue(self.performance.isInstrumentationEnabled);
  66. [self.performance setInstrumentationEnabled:YES];
  67. XCTAssertTrue([FPRConfigurations sharedInstance].isInstrumentationEnabled);
  68. XCTAssertTrue(self.performance.instrumentationEnabled);
  69. }
  70. /** Validates that trace creation works on shared instance. */
  71. - (void)testTraceCreationOnSharedInstance {
  72. XCTAssertNotNil([self.performance traceWithName:@"random"]);
  73. XCTAssertThrows([self.performance traceWithName:@""]);
  74. [self.performance setInstrumentationEnabled:YES];
  75. }
  76. /** Validates if trace creation throws exception when SDK is not configured. */
  77. - (void)testTraceCreationThrowsExceptionWhenNotConfigured {
  78. self.performance.fprClient.configured = NO;
  79. XCTAssertThrows([self.performance traceWithName:@"random"]);
  80. self.performance.fprClient.configured = YES;
  81. }
  82. #pragma mark - Custom attributes related tests
  83. /** Validates if setting a valid attribute works. */
  84. - (void)testSettingValidAttribute {
  85. [self.performance setValue:@"bar" forAttribute:@"foo"];
  86. XCTAssertEqual([self.performance valueForAttribute:@"foo"], @"bar");
  87. }
  88. /** Validates if attributes property access works. */
  89. - (void)testReadingAttributesFromProperty {
  90. XCTAssertNotNil(self.performance.attributes);
  91. XCTAssertEqual(self.performance.attributes.count, 0);
  92. [self.performance setValue:@"bar" forAttribute:@"foo"];
  93. NSDictionary<NSString *, NSString *> *attributes = self.performance.attributes;
  94. XCTAssertEqual(attributes.allKeys.count, 1);
  95. }
  96. /** Validates if attributes property is immutable. */
  97. - (void)testImmutablityOfAttributesProperty {
  98. [self.performance setValue:@"bar" forAttribute:@"foo"];
  99. NSMutableDictionary<NSString *, NSString *> *attributes =
  100. (NSMutableDictionary<NSString *, NSString *> *)self.performance.attributes;
  101. XCTAssertThrows([attributes setValue:@"bar1" forKey:@"foo"]);
  102. }
  103. /** Validates if updating attribute value works. */
  104. - (void)testUpdatingAttributeValue {
  105. [self.performance setValue:@"bar" forAttribute:@"foo"];
  106. [self.performance setValue:@"baz" forAttribute:@"foo"];
  107. XCTAssertEqual(@"baz", [self.performance valueForAttribute:@"foo"]);
  108. [self.performance setValue:@"qux" forAttribute:@"foo"];
  109. XCTAssertEqual(@"qux", [self.performance valueForAttribute:@"foo"]);
  110. }
  111. /** Validates if removing attributes work. */
  112. - (void)testRemovingAttribute {
  113. [self.performance setValue:@"bar" forAttribute:@"foo"];
  114. [self.performance removeAttribute:@"foo"];
  115. XCTAssertNil([self.performance valueForAttribute:@"foo"]);
  116. [self.performance removeAttribute:@"foo"];
  117. XCTAssertNil([self.performance valueForAttribute:@"foo"]);
  118. }
  119. /** Validates if removing non-existing attributes works. */
  120. - (void)testRemovingNonExistingAttribute {
  121. [self.performance removeAttribute:@"foo"];
  122. XCTAssertNil([self.performance valueForAttribute:@"foo"]);
  123. [self.performance removeAttribute:@"foo"];
  124. XCTAssertNil([self.performance valueForAttribute:@"foo"]);
  125. }
  126. /** Validates if using reserved prefix in attribute prefix will drop the attribute. */
  127. - (void)testAttributeNamePrefixSrtipped {
  128. NSArray<NSString *> *reservedPrefix = @[ @"firebase_", @"google_", @"ga_" ];
  129. [reservedPrefix enumerateObjectsUsingBlock:^(NSString *prefix, NSUInteger idx, BOOL *stop) {
  130. NSString *attributeName = [NSString stringWithFormat:@"%@name", prefix];
  131. NSString *attributeValue = @"value";
  132. [self.performance setValue:attributeValue forAttribute:attributeName];
  133. XCTAssertNil([self.performance valueForAttribute:attributeName]);
  134. }];
  135. }
  136. /** Validates if long attribute names gets dropped. */
  137. - (void)testMaxLengthForAttributeName {
  138. NSString *testName = [@"abc" stringByPaddingToLength:kFPRMaxAttributeNameLength + 1
  139. withString:@"-"
  140. startingAtIndex:0];
  141. [self.performance setValue:@"bar" forAttribute:testName];
  142. XCTAssertNil([self.performance valueForAttribute:testName]);
  143. }
  144. /** Validates if attribute names with illegal characters gets dropped. */
  145. - (void)testIllegalCharactersInAttributeName {
  146. [self.performance setValue:@"bar" forAttribute:@"foo_"];
  147. XCTAssertEqual([self.performance valueForAttribute:@"foo_"], @"bar");
  148. [self.performance setValue:@"bar" forAttribute:@"foo_$"];
  149. XCTAssertNil([self.performance valueForAttribute:@"foo_$"]);
  150. [self.performance setValue:@"bar" forAttribute:@"FOO_$"];
  151. XCTAssertNil([self.performance valueForAttribute:@"FOO_$"]);
  152. [self.performance setValue:@"bar" forAttribute:@"FOO_"];
  153. XCTAssertEqual([self.performance valueForAttribute:@"FOO_"], @"bar");
  154. }
  155. /** Validates if long attribute values gets truncated. */
  156. - (void)testMaxLengthForAttributeValue {
  157. NSString *testValue = [@"abc" stringByPaddingToLength:kFPRMaxAttributeValueLength + 1
  158. withString:@"-"
  159. startingAtIndex:0];
  160. [self.performance setValue:testValue forAttribute:@"foo"];
  161. XCTAssertNil([self.performance valueForAttribute:@"foo"]);
  162. }
  163. /** Validates if empty name or value of the attributes are getting dropped. */
  164. - (void)testAttributesWithEmptyValues {
  165. [self.performance setValue:@"" forAttribute:@"foo"];
  166. XCTAssertNil([self.performance valueForAttribute:@"foo"]);
  167. [self.performance setValue:@"bar" forAttribute:@""];
  168. XCTAssertNil([self.performance valueForAttribute:@""]);
  169. }
  170. /** Validates if the limit the maximum number of attributes work. */
  171. - (void)testMaximumNumberOfAttributes {
  172. for (int i = 0; i < kFPRMaxGlobalCustomAttributesCount; i++) {
  173. NSString *attributeName = [NSString stringWithFormat:@"dim%d", i];
  174. [self.performance setValue:@"bar" forAttribute:attributeName];
  175. XCTAssertEqual([self.performance valueForAttribute:attributeName], @"bar");
  176. }
  177. [self.performance setValue:@"bar" forAttribute:@"foo"];
  178. XCTAssertNil([self.performance valueForAttribute:@"foo"]);
  179. }
  180. /** Validates if removing old attributes and adding new attributes work. */
  181. - (void)testRemovingAndAddingAttributes {
  182. for (int i = 0; i < kFPRMaxGlobalCustomAttributesCount; i++) {
  183. NSString *attributeName = [NSString stringWithFormat:@"dim%d", i];
  184. [self.performance setValue:@"bar" forAttribute:attributeName];
  185. XCTAssertEqual([self.performance valueForAttribute:attributeName], @"bar");
  186. }
  187. [self.performance removeAttribute:@"dim1"];
  188. [self.performance setValue:@"bar" forAttribute:@"foo"];
  189. XCTAssertEqual([self.performance valueForAttribute:@"foo"], @"bar");
  190. }
  191. @end