FPRCounterListTest.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/FIRPerformance+Internal.h"
  16. #import "FirebasePerformance/Sources/FPRClient.h"
  17. #import "FirebasePerformance/Sources/Public/FirebasePerformance/FIRPerformance.h"
  18. #import "FirebasePerformance/Sources/Timer/FPRCounterList.h"
  19. @interface FPRCounterListTest : XCTestCase
  20. @end
  21. @implementation FPRCounterListTest
  22. + (void)setUp {
  23. [super setUp];
  24. FIRPerformance *performance = [FIRPerformance sharedInstance];
  25. [performance setDataCollectionEnabled:YES];
  26. #ifdef UNSWIZZLE_AVAILABLE
  27. [[FPRClient sharedInstance] disableInstrumentation];
  28. #endif // UNSWIZZLE_AVAILABLE
  29. }
  30. + (void)tearDown {
  31. [super tearDown];
  32. FIRPerformance *performance = [FIRPerformance sharedInstance];
  33. [performance setDataCollectionEnabled:NO];
  34. #ifdef UNSWIZZLE_AVAILABLE
  35. [[FPRClient sharedInstance] disableInstrumentation];
  36. #endif // UNSWIZZLE_AVAILABLE
  37. }
  38. /** Validates counterlist object creation. */
  39. - (void)testInit {
  40. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  41. XCTAssertNotNil(counterList);
  42. }
  43. /** Validates the initial state of counter list. */
  44. - (void)testInitialState {
  45. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  46. XCTAssertTrue(counterList.counters.count == 0);
  47. }
  48. /** Validates that the counter values are incremented correctly. */
  49. - (void)testCounters {
  50. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  51. [counterList incrementCounterNamed:@"testing" by:1];
  52. [counterList incrementCounterNamed:@"testing" by:5];
  53. [counterList incrementCounterNamed:@"testing 2" by:1];
  54. NSUInteger counterValue1 = [[counterList.counters objectForKey:@"testing"] integerValue];
  55. XCTAssertEqual(counterValue1, 6);
  56. NSUInteger counterValue2 = [[counterList.counters objectForKey:@"testing 2"] integerValue];
  57. XCTAssertEqual(counterValue2, 1);
  58. NSUInteger counterValue3 = [[counterList.counters objectForKey:@"Random"] integerValue];
  59. XCTAssertEqual(counterValue3, 0);
  60. }
  61. /** Tests that metrics are set correctly. */
  62. - (void)testSetMetric {
  63. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  64. [counterList setIntValue:10 forMetric:@"testing"];
  65. [counterList setIntValue:12 forMetric:@"testing 2"];
  66. int64_t metricValue1 = [[counterList.counters objectForKey:@"testing"] longLongValue];
  67. XCTAssertEqual(metricValue1, 10);
  68. int64_t metricValue2 = [[counterList.counters objectForKey:@"testing 2"] longLongValue];
  69. XCTAssertEqual(metricValue2, 12);
  70. }
  71. /** Tests that metrics are incremented correctly. */
  72. - (void)testIncrementMetric {
  73. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  74. [counterList setIntValue:10 forMetric:@"testing"];
  75. [counterList incrementMetric:@"testing" byInt:10];
  76. int64_t metricValue1 = [[counterList.counters objectForKey:@"testing"] longLongValue];
  77. XCTAssertEqual(metricValue1, 20);
  78. }
  79. /** Tests getting metric after having set it. */
  80. - (void)testGetMetric {
  81. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  82. [counterList setIntValue:10 forMetric:@"testing"];
  83. [counterList incrementMetric:@"testing" byInt:10];
  84. XCTAssertEqual([counterList valueForIntMetric:@"testing"], 20);
  85. }
  86. /** Validates deleting a non existent metric doesnt affect other metrics. */
  87. - (void)testDeleteNonExistentMetricDoesntAffectOtherMetrics {
  88. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  89. [counterList setIntValue:10 forMetric:@"testing"];
  90. [counterList deleteMetric:@"testing2"];
  91. XCTAssertEqual([counterList valueForIntMetric:@"testing"], 10);
  92. }
  93. /** Validates deleteMetric deletes a metric. */
  94. - (void)testDeleteExistingMetric {
  95. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  96. [counterList setIntValue:10 forMetric:@"testing"];
  97. [counterList deleteMetric:@"testing"];
  98. XCTAssertEqual([counterList valueForIntMetric:@"testing"], 0);
  99. XCTAssertNil(counterList.counters[@"testing"]);
  100. }
  101. /** Validates deleting existing metric only deletes that metric. */
  102. - (void)testDeleteExistingMetricDoesntDeleteOtherMetric {
  103. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  104. [counterList setIntValue:10 forMetric:@"testing"];
  105. [counterList setIntValue:10 forMetric:@"testing2"];
  106. [counterList deleteMetric:@"testing"];
  107. XCTAssertNil(counterList.counters[@"testing"]);
  108. XCTAssertEqual([counterList valueForIntMetric:@"testing2"], 10);
  109. }
  110. /** Validates passing nil to metricName doesn't do anything. */
  111. - (void)testDeleteMetricWithNilNameDoesntDoAnything {
  112. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  113. [counterList setIntValue:10 forMetric:@"testing"];
  114. #pragma clang diagnostic push
  115. #pragma clang diagnostic ignored "-Wnonnull"
  116. [counterList deleteMetric:nil];
  117. #pragma clang diagnostic pop
  118. XCTAssertEqual([counterList valueForIntMetric:@"testing"], 10);
  119. }
  120. /** Validates that the counters are valid when then have valid data. */
  121. - (void)testCounterValidity {
  122. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  123. [counterList incrementCounterNamed:@"testing" by:1];
  124. [counterList incrementCounterNamed:@"testing" by:1];
  125. [counterList incrementCounterNamed:@"testing 2" by:1];
  126. [counterList setIntValue:-44 forMetric:@"testing"];
  127. XCTAssertTrue([counterList isValid]);
  128. }
  129. /** Validates if the counter increment with negative value reduces the value. */
  130. - (void)testCounterIncrementWithNegativeValue {
  131. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  132. [counterList incrementCounterNamed:@"testing" by:5];
  133. [counterList incrementCounterNamed:@"testing" by:-1];
  134. NSUInteger counterValue1 = [[counterList.counters objectForKey:@"testing"] integerValue];
  135. XCTAssertEqual(counterValue1, 4);
  136. }
  137. /** Validates if the counter initialize with negative value works. */
  138. - (void)testCounterInitializeWithNegativeValue {
  139. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  140. [counterList incrementCounterNamed:@"testing" by:-1];
  141. NSUInteger counterValue1 = [[counterList.counters objectForKey:@"testing"] integerValue];
  142. XCTAssertEqual(counterValue1, -1);
  143. }
  144. @end