FPRAppActivityTrackerTest.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/FPRAppActivityTracker.h"
  16. #import "FirebasePerformance/Sources/Public/FIRPerformance.h"
  17. #import "FirebasePerformance/Tests/Unit/FPRTestCase.h"
  18. #import "FirebaseCore/Sources/Private/FIRAppInternal.h"
  19. #import <OCMock/OCMock.h>
  20. @interface FPRAppActivityTrackerTest : FPRTestCase
  21. @end
  22. @implementation FPRAppActivityTrackerTest
  23. - (void)setUp {
  24. [super setUp];
  25. FIRPerformance *performance = [FIRPerformance sharedInstance];
  26. [performance setDataCollectionEnabled:YES];
  27. }
  28. - (void)tearDown {
  29. [super tearDown];
  30. FIRPerformance *performance = [FIRPerformance sharedInstance];
  31. [performance setDataCollectionEnabled:NO];
  32. }
  33. /** Validates if the instance was successfully created. */
  34. - (void)testInstanceCreation {
  35. XCTAssertNotNil([FPRAppActivityTracker sharedInstance]);
  36. XCTAssertEqualObjects([FPRAppActivityTracker sharedInstance],
  37. [FPRAppActivityTracker sharedInstance]);
  38. }
  39. /** Validates if an active trace is available when the app is active. */
  40. - (void)testActiveTrace {
  41. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  42. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  43. object:[UIApplication sharedApplication]];
  44. XCTAssertNotNil([FPRAppActivityTracker sharedInstance].activeTrace);
  45. }
  46. /** Validates no active trace is available when data collection is disabled. */
  47. - (void)testActiveTraceWhenDataCollectionDisabled {
  48. BOOL dataCollectionEnabled = [FIRPerformance sharedInstance].dataCollectionEnabled;
  49. [[FIRPerformance sharedInstance] setDataCollectionEnabled:NO];
  50. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  51. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  52. object:[UIApplication sharedApplication]];
  53. XCTAssertNil([FPRAppActivityTracker sharedInstance].activeTrace);
  54. [[FIRPerformance sharedInstance] setDataCollectionEnabled:dataCollectionEnabled];
  55. }
  56. /** Validates if the active trace changes across launch of application from foreground to background
  57. * and then background to foreground.
  58. */
  59. - (void)testActiveTraceChanging {
  60. FIRTrace *activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  61. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  62. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  63. object:[UIApplication sharedApplication]];
  64. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  65. object:[UIApplication sharedApplication]];
  66. FIRTrace *newActiveTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  67. XCTAssertNotEqual(activeTrace, newActiveTrace);
  68. }
  69. /** Validates if the active trace changes across launch of application from foreground to
  70. * background.
  71. */
  72. - (void)testActiveTraceWhenAppChangesStates {
  73. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  74. FIRTrace *activeTrace = nil;
  75. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  76. object:[UIApplication sharedApplication]];
  77. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  78. XCTAssertEqual(activeTrace.name, kFPRAppTraceNameBackgroundSession);
  79. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  80. object:[UIApplication sharedApplication]];
  81. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  82. XCTAssertEqual(activeTrace.name, kFPRAppTraceNameForegroundSession);
  83. }
  84. /** Validates if the active trace is nil when data collection is toggled.
  85. */
  86. - (void)testActiveTraceIsNilWhenAppChangesStatesAndDataCollectionToggledFromEnabled {
  87. BOOL dataCollectionEnabled = [FIRPerformance sharedInstance].dataCollectionEnabled;
  88. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  89. FIRTrace *activeTrace = nil;
  90. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  91. object:[UIApplication sharedApplication]];
  92. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  93. XCTAssertEqual(activeTrace.name, kFPRAppTraceNameBackgroundSession);
  94. [[FIRPerformance sharedInstance] setDataCollectionEnabled:NO];
  95. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  96. object:[UIApplication sharedApplication]];
  97. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  98. XCTAssertNil(activeTrace);
  99. [[FIRPerformance sharedInstance] setDataCollectionEnabled:YES];
  100. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  101. object:[UIApplication sharedApplication]];
  102. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  103. XCTAssertEqual(activeTrace.name, kFPRAppTraceNameBackgroundSession);
  104. [FIRPerformance sharedInstance].dataCollectionEnabled = dataCollectionEnabled;
  105. }
  106. /** Validates if the active trace is nil when data collection is toggled.
  107. */
  108. - (void)testActiveTraceIsNilWhenAppChangesStatesAndDataCollectionToggledFromDisabled {
  109. BOOL dataCollectionEnabled = [FIRPerformance sharedInstance].dataCollectionEnabled;
  110. [[FIRPerformance sharedInstance] setDataCollectionEnabled:NO];
  111. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  112. FIRTrace *activeTrace = nil;
  113. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  114. object:[UIApplication sharedApplication]];
  115. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  116. XCTAssertNil(activeTrace);
  117. [[FIRPerformance sharedInstance] setDataCollectionEnabled:YES];
  118. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  119. object:[UIApplication sharedApplication]];
  120. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  121. XCTAssertNotNil(activeTrace);
  122. [[FIRPerformance sharedInstance] setDataCollectionEnabled:NO];
  123. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  124. object:[UIApplication sharedApplication]];
  125. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  126. XCTAssertNil(activeTrace);
  127. [FIRPerformance sharedInstance].dataCollectionEnabled = dataCollectionEnabled;
  128. }
  129. /** Validates if the application state is managed correctly. */
  130. - (void)testApplicationStateManagement {
  131. FPRAppActivityTracker *appTracker = [FPRAppActivityTracker sharedInstance];
  132. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  133. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  134. object:[UIApplication sharedApplication]];
  135. XCTAssertEqual(appTracker.applicationState, FPRApplicationStateBackground);
  136. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  137. object:[UIApplication sharedApplication]];
  138. XCTAssertEqual(appTracker.applicationState, FPRApplicationStateForeground);
  139. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  140. object:[UIApplication sharedApplication]];
  141. XCTAssertEqual(appTracker.applicationState, FPRApplicationStateBackground);
  142. }
  143. @end