FPRAppActivityTrackerTest.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/FirebasePerformance/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:UIWindowDidBecomeVisibleNotification
  43. object:[UIApplication sharedApplication]];
  44. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  45. object:[UIApplication sharedApplication]];
  46. XCTAssertNotNil([FPRAppActivityTracker sharedInstance].activeTrace);
  47. }
  48. /** Validates no active trace is available when data collection is disabled. */
  49. - (void)testActiveTraceWhenDataCollectionDisabled {
  50. BOOL dataCollectionEnabled = [FIRPerformance sharedInstance].dataCollectionEnabled;
  51. [[FIRPerformance sharedInstance] setDataCollectionEnabled:NO];
  52. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  53. [defaultCenter postNotificationName:UIWindowDidBecomeVisibleNotification
  54. object:[UIApplication sharedApplication]];
  55. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  56. object:[UIApplication sharedApplication]];
  57. XCTAssertNil([FPRAppActivityTracker sharedInstance].activeTrace);
  58. [[FIRPerformance sharedInstance] setDataCollectionEnabled:dataCollectionEnabled];
  59. }
  60. /** Validates if the active trace changes across launch of application from foreground to background
  61. * and then background to foreground.
  62. */
  63. - (void)testActiveTraceChanging {
  64. FIRTrace *activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  65. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  66. [defaultCenter postNotificationName:UIWindowDidBecomeVisibleNotification
  67. object:[UIApplication sharedApplication]];
  68. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  69. object:[UIApplication sharedApplication]];
  70. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  71. object:[UIApplication sharedApplication]];
  72. FIRTrace *newActiveTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  73. XCTAssertNotEqual(activeTrace, newActiveTrace);
  74. }
  75. /** Validates if the active trace changes across launch of application from foreground to
  76. * background.
  77. */
  78. - (void)testActiveTraceWhenAppChangesStates {
  79. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  80. FIRTrace *activeTrace = nil;
  81. [defaultCenter postNotificationName:UIWindowDidBecomeVisibleNotification
  82. object:[UIApplication sharedApplication]];
  83. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  84. object:[UIApplication sharedApplication]];
  85. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  86. XCTAssertEqual(activeTrace.name, kFPRAppTraceNameBackgroundSession);
  87. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  88. object:[UIApplication sharedApplication]];
  89. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  90. XCTAssertEqual(activeTrace.name, kFPRAppTraceNameForegroundSession);
  91. }
  92. /** Validates if the active trace is nil when data collection is toggled.
  93. */
  94. - (void)testActiveTraceIsNilWhenAppChangesStatesAndDataCollectionToggledFromEnabled {
  95. BOOL dataCollectionEnabled = [FIRPerformance sharedInstance].dataCollectionEnabled;
  96. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  97. FIRTrace *activeTrace = nil;
  98. [defaultCenter postNotificationName:UIWindowDidBecomeVisibleNotification
  99. object:[UIApplication sharedApplication]];
  100. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  101. object:[UIApplication sharedApplication]];
  102. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  103. XCTAssertEqual(activeTrace.name, kFPRAppTraceNameBackgroundSession);
  104. [[FIRPerformance sharedInstance] setDataCollectionEnabled:NO];
  105. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  106. object:[UIApplication sharedApplication]];
  107. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  108. XCTAssertNil(activeTrace);
  109. [[FIRPerformance sharedInstance] setDataCollectionEnabled:YES];
  110. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  111. object:[UIApplication sharedApplication]];
  112. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  113. XCTAssertEqual(activeTrace.name, kFPRAppTraceNameBackgroundSession);
  114. [FIRPerformance sharedInstance].dataCollectionEnabled = dataCollectionEnabled;
  115. }
  116. /** Validates if the active trace is nil when data collection is toggled.
  117. */
  118. - (void)testActiveTraceIsNilWhenAppChangesStatesAndDataCollectionToggledFromDisabled {
  119. BOOL dataCollectionEnabled = [FIRPerformance sharedInstance].dataCollectionEnabled;
  120. [[FIRPerformance sharedInstance] setDataCollectionEnabled:NO];
  121. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  122. FIRTrace *activeTrace = nil;
  123. [defaultCenter postNotificationName:UIWindowDidBecomeVisibleNotification
  124. object:[UIApplication sharedApplication]];
  125. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  126. object:[UIApplication sharedApplication]];
  127. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  128. XCTAssertNil(activeTrace);
  129. [[FIRPerformance sharedInstance] setDataCollectionEnabled:YES];
  130. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  131. object:[UIApplication sharedApplication]];
  132. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  133. XCTAssertNotNil(activeTrace);
  134. [[FIRPerformance sharedInstance] setDataCollectionEnabled:NO];
  135. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  136. object:[UIApplication sharedApplication]];
  137. activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  138. XCTAssertNil(activeTrace);
  139. [FIRPerformance sharedInstance].dataCollectionEnabled = dataCollectionEnabled;
  140. }
  141. /** Validates if the application state is managed correctly. */
  142. - (void)testApplicationStateManagement {
  143. FPRAppActivityTracker *appTracker = [FPRAppActivityTracker sharedInstance];
  144. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  145. [defaultCenter postNotificationName:UIWindowDidBecomeVisibleNotification
  146. object:[UIApplication sharedApplication]];
  147. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  148. object:[UIApplication sharedApplication]];
  149. XCTAssertEqual(appTracker.applicationState, FPRApplicationStateBackground);
  150. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  151. object:[UIApplication sharedApplication]];
  152. XCTAssertEqual(appTracker.applicationState, FPRApplicationStateForeground);
  153. [defaultCenter postNotificationName:UIApplicationWillResignActiveNotification
  154. object:[UIApplication sharedApplication]];
  155. XCTAssertEqual(appTracker.applicationState, FPRApplicationStateBackground);
  156. }
  157. @end