FPRUIViewControllerInstrumentTest.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #pragma mark - Unswizzle based tests
  15. #import "FirebasePerformance/Sources/Instrumentation/UIKit/FPRUIViewControllerInstrument.h"
  16. #import <XCTest/XCTest.h>
  17. #import <OCMock/OCMock.h>
  18. #import "FirebasePerformance/Sources/AppActivity/FPRScreenTraceTracker+Private.h"
  19. #import "FirebasePerformance/Sources/AppActivity/FPRScreenTraceTracker.h"
  20. #import "FirebasePerformance/Sources/Public/FirebasePerformance/FIRPerformance.h"
  21. #import <GoogleUtilities/GULSwizzler.h>
  22. static BOOL originalViewDidAppearInvoked = NO;
  23. static BOOL originalViewDidDisappearInvoked = NO;
  24. @interface FPRUIViewControllerInstrumentTest : XCTestCase
  25. @end
  26. @implementation FPRUIViewControllerInstrumentTest
  27. + (void)setUp {
  28. [super setUp];
  29. FIRPerformance *performance = [FIRPerformance sharedInstance];
  30. [performance setDataCollectionEnabled:YES];
  31. }
  32. + (void)tearDown {
  33. [super tearDown];
  34. FIRPerformance *performance = [FIRPerformance sharedInstance];
  35. [performance setDataCollectionEnabled:NO];
  36. }
  37. - (void)setUp {
  38. [super setUp];
  39. originalViewDidAppearInvoked = NO;
  40. originalViewDidDisappearInvoked = NO;
  41. }
  42. #if !SWIFT_PACKAGE
  43. /** Tests that the viewControllerDidAppear: of the FPRScreenTraceTracker sharedInstance is invoked
  44. * when a UIViewController's viewDidAppear: is invoked.
  45. */
  46. - (void)testViewDidAppearInvokesViewControllerDidAppearOnScreenTraceTracker {
  47. UIViewController *testViewController = [[UIViewController alloc] init];
  48. [[UIApplication sharedApplication].keyWindow addSubview:[testViewController view]];
  49. FPRUIViewControllerInstrument *instrument = [[FPRUIViewControllerInstrument alloc] init];
  50. [instrument registerInstrumentors];
  51. // Partial mock isa swizzles the object and we can listen to whether it received certain messages.
  52. id screenTraceTrackerListener = OCMPartialMock([FPRScreenTraceTracker sharedInstance]);
  53. OCMExpect([screenTraceTrackerListener viewControllerDidAppear:testViewController]);
  54. [testViewController viewDidAppear:YES];
  55. OCMVerifyAll(screenTraceTrackerListener);
  56. [screenTraceTrackerListener stopMocking];
  57. [instrument deregisterInstrumentors];
  58. [[testViewController view] removeFromSuperview];
  59. }
  60. #endif // SWIFT_PACKAGE
  61. /** Tests that the viewControllerDidAppear: of the FPRScreenTraceTracker sharedInstance is invoked
  62. * when a UIViewController's viewDidAppear: is invoked.
  63. */
  64. - (void)testViewDidAppearDoesntInvokeViewControllerDidAppearOnNonKeyWindowView {
  65. UIViewController *testViewController = [[UIViewController alloc] init];
  66. FPRUIViewControllerInstrument *instrument = [[FPRUIViewControllerInstrument alloc] init];
  67. [instrument registerInstrumentors];
  68. // Partial mock isa swizzles the object and we can listen to whether it received certain messages.
  69. id screenTraceTrackerListener = OCMPartialMock([FPRScreenTraceTracker sharedInstance]);
  70. OCMReject([screenTraceTrackerListener viewControllerDidAppear:testViewController]);
  71. [testViewController viewDidAppear:YES];
  72. OCMVerifyAll(screenTraceTrackerListener);
  73. [screenTraceTrackerListener stopMocking];
  74. [instrument deregisterInstrumentors];
  75. }
  76. /** Tests that the viewControllerDidDisappear: of the FPRScreenTraceTracker sharedInstance is
  77. * invoked when a UIViewController's viewDidDisappear: is invoked.
  78. */
  79. - (void)testViewDidDisappearInvokesViewControllerDidDisappearOnScreenTraceTracker {
  80. UIViewController *testViewController = [[UIViewController alloc] init];
  81. FPRUIViewControllerInstrument *instrument = [[FPRUIViewControllerInstrument alloc] init];
  82. [instrument registerInstrumentors];
  83. // Partial mock isa swizzles the object and we can listen to whether it received certain messages.
  84. id screenTraceTrackerListener = OCMPartialMock([FPRScreenTraceTracker sharedInstance]);
  85. OCMExpect([screenTraceTrackerListener viewControllerDidDisappear:testViewController]);
  86. [testViewController viewDidDisappear:YES];
  87. OCMVerifyAll(screenTraceTrackerListener);
  88. [screenTraceTrackerListener stopMocking];
  89. [instrument deregisterInstrumentors];
  90. }
  91. /** Tests that the instrument invokes the IMP that was previously in place for
  92. * [uiViewControllerInstance viewDidAppear:].
  93. */
  94. - (void)testViewDidAppearInvokesPreviousViewDidAppear {
  95. __block BOOL previousViewDidAppearCalled = NO;
  96. Class viewControllerClass = [UIViewController class];
  97. SEL viewDidAppearSelector = @selector(viewDidAppear:);
  98. [GULSwizzler swizzleClass:viewControllerClass
  99. selector:viewDidAppearSelector
  100. isClassSelector:NO
  101. withBlock:^void(id _self, BOOL animated) {
  102. previousViewDidAppearCalled = YES;
  103. }];
  104. UIViewController *testViewController = [[UIViewController alloc] init];
  105. FPRUIViewControllerInstrument *instrument = [[FPRUIViewControllerInstrument alloc] init];
  106. [instrument registerInstrumentors];
  107. XCTAssertFalse(previousViewDidAppearCalled);
  108. [testViewController viewDidAppear:YES];
  109. XCTAssertTrue(previousViewDidAppearCalled);
  110. // This should revert the first IMP that was swizzled as well.
  111. [instrument deregisterInstrumentors];
  112. }
  113. /** Tests that the instrument invokes the IMP that was previously in place for
  114. * [uiViewControllerInstance viewDidDisappear:].
  115. */
  116. - (void)testViewDidAppearInvokesPreviousViewDidDisappear {
  117. __block BOOL previousViewDidDisappearCalled = NO;
  118. Class viewControllerClass = [UIViewController class];
  119. SEL viewDidDisappearSelector = @selector(viewDidDisappear:);
  120. [GULSwizzler swizzleClass:viewControllerClass
  121. selector:viewDidDisappearSelector
  122. isClassSelector:NO
  123. withBlock:^void(id _self, BOOL animated) {
  124. previousViewDidDisappearCalled = YES;
  125. }];
  126. UIViewController *testViewController = [[UIViewController alloc] init];
  127. FPRUIViewControllerInstrument *instrument = [[FPRUIViewControllerInstrument alloc] init];
  128. [instrument registerInstrumentors];
  129. XCTAssertFalse(previousViewDidDisappearCalled);
  130. [testViewController viewDidDisappear:YES];
  131. XCTAssertTrue(previousViewDidDisappearCalled);
  132. // This should revert the first IMP that was swizzled as well.
  133. [instrument deregisterInstrumentors];
  134. }
  135. @end