FPRUIViewControllerInstrumentTest.m 6.5 KB

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