FPRTraceBackgroundActivityTrackerTest.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/AppActivity/FPRTraceBackgroundActivityTracker.h"
  16. @interface FPRTraceBackgroundActivityTrackerTest : XCTestCase
  17. @end
  18. @implementation FPRTraceBackgroundActivityTrackerTest
  19. /** Validate instance creation. */
  20. - (void)testInstanceCreation {
  21. XCTAssertNotNil([[FPRTraceBackgroundActivityTracker alloc] init]);
  22. }
  23. /** Validates if the foreground state is captured correctly. */
  24. - (void)testForegroundTracking {
  25. FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init];
  26. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  27. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  28. object:[UIApplication sharedApplication]];
  29. XCTAssertEqual(tracker.traceBackgroundState, FPRTraceStateForegroundOnly);
  30. }
  31. /** Validates if the foreground & background state is captured correctly. */
  32. - (void)testBackgroundTracking {
  33. XCTestExpectation *expectation = [self expectationWithDescription:@"Application state change"];
  34. FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init];
  35. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  36. dispatch_async(dispatch_get_main_queue(), ^{
  37. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  38. object:[UIApplication sharedApplication]];
  39. [defaultCenter postNotificationName:UIApplicationDidEnterBackgroundNotification
  40. object:[UIApplication sharedApplication]];
  41. [expectation fulfill];
  42. });
  43. [self waitForExpectationsWithTimeout:5.0
  44. handler:^(NSError *_Nullable error) {
  45. if (error) {
  46. XCTFail(@"Expectation failed with error: %@", error);
  47. } else {
  48. XCTAssertEqual(tracker.traceBackgroundState,
  49. FPRTraceStateBackgroundAndForeground);
  50. }
  51. }];
  52. }
  53. /** Tests that synchronous observer registration works correctly and observers are immediately
  54. * available. */
  55. - (void)testObservers_synchronousRegistrationAddsObserver {
  56. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  57. FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init];
  58. XCTAssertNotNil(tracker);
  59. [notificationCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  60. object:[UIApplication sharedApplication]];
  61. XCTAssertEqual(tracker.traceBackgroundState, FPRTraceStateForegroundOnly);
  62. tracker = nil;
  63. XCTAssertNil(tracker);
  64. XCTAssertNoThrow([notificationCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  65. object:[UIApplication sharedApplication]]);
  66. XCTAssertNoThrow([notificationCenter
  67. postNotificationName:UIApplicationDidEnterBackgroundNotification
  68. object:[UIApplication sharedApplication]]);
  69. }
  70. /** Tests rapid creation and deallocation to verify race condition. */
  71. - (void)testRapidCreationAndDeallocation_noRaceCondition {
  72. for (int i = 0; i < 100; i++) {
  73. @autoreleasepool {
  74. FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init];
  75. XCTAssertNotNil(tracker);
  76. [[NSNotificationCenter defaultCenter]
  77. postNotificationName:UIApplicationDidBecomeActiveNotification
  78. object:[UIApplication sharedApplication]];
  79. }
  80. }
  81. XCTAssertNoThrow([[NSNotificationCenter defaultCenter]
  82. postNotificationName:UIApplicationDidBecomeActiveNotification
  83. object:[UIApplication sharedApplication]]);
  84. XCTAssertNoThrow([[NSNotificationCenter defaultCenter]
  85. postNotificationName:UIApplicationDidEnterBackgroundNotification
  86. object:[UIApplication sharedApplication]]);
  87. }
  88. /** Tests observer registration when created from background thread. */
  89. - (void)testObservers_registrationFromBackgroundThread {
  90. XCTestExpectation *expectation = [self expectationWithDescription:@"Background thread creation"];
  91. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  92. FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init];
  93. XCTAssertNotNil(tracker);
  94. dispatch_async(dispatch_get_main_queue(), ^{
  95. [[NSNotificationCenter defaultCenter]
  96. postNotificationName:UIApplicationDidBecomeActiveNotification
  97. object:[UIApplication sharedApplication]];
  98. XCTAssertEqual(tracker.traceBackgroundState, FPRTraceStateForegroundOnly);
  99. [expectation fulfill];
  100. });
  101. });
  102. [self waitForExpectationsWithTimeout:5.0
  103. handler:^(NSError *error) {
  104. XCTAssertNil(error, @"Test timed out");
  105. }];
  106. }
  107. @end