react-native-navigation的迁移库

RNNStackControllerTest.m 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #import <XCTest/XCTest.h>
  2. #import <OCMock/OCMock.h>
  3. #import <ReactNativeNavigation/RNNStackController.h>
  4. #import <ReactNativeNavigation/RNNComponentViewController.h>
  5. #import "RNNTestRootViewCreator.h"
  6. @interface RNNStackControllerTest : XCTestCase
  7. @property (nonatomic, strong) RNNStackController *uut;
  8. @end
  9. @implementation RNNStackControllerTest {
  10. RNNComponentViewController* _vc1;
  11. id _vc2Mock;
  12. RNNComponentViewController* _vc2;
  13. UIViewController* _vc3;
  14. RNNNavigationOptions* _options;
  15. RNNTestRootViewCreator* _creator;
  16. RNNEventEmitter* _eventEmitter;
  17. id _presenter;
  18. }
  19. - (void)setUp {
  20. [super setUp];
  21. _presenter = [OCMockObject partialMockForObject:[[RNNStackPresenter alloc] init]];
  22. _eventEmitter = [OCMockObject niceMockForClass:[RNNEventEmitter class]];
  23. _creator = [[RNNTestRootViewCreator alloc] init];
  24. _vc1 = [[RNNComponentViewController alloc] initWithLayoutInfo:[RNNLayoutInfo new] rootViewCreator:nil eventEmitter:nil presenter:[OCMockObject partialMockForObject:[[RNNComponentPresenter alloc] init]] options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions]];
  25. _vc2 = [[RNNComponentViewController alloc] initWithLayoutInfo:[RNNLayoutInfo new] rootViewCreator:nil eventEmitter:nil presenter:[[RNNComponentPresenter alloc] init] options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions]];
  26. _vc2Mock = [OCMockObject partialMockForObject:_vc2];
  27. _vc3 = [UIViewController new];
  28. _options = [OCMockObject partialMockForObject:[[RNNNavigationOptions alloc] initEmptyOptions]];
  29. self.uut = [[RNNStackController alloc] initWithLayoutInfo:nil creator:_creator options:_options defaultOptions:nil presenter:_presenter eventEmitter:_eventEmitter childViewControllers:@[_vc1, _vc2]];
  30. }
  31. - (void)testInitWithLayoutInfo_shouldBindPresenter {
  32. XCTAssertNotNil(self.uut.presenter);
  33. }
  34. - (void)testInitWithLayoutInfo_shouldSetMultipleViewControllers {
  35. self.uut = [[RNNStackController alloc] initWithLayoutInfo:nil creator:_creator options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNComponentPresenter alloc] init] eventEmitter:nil childViewControllers:@[_vc1, _vc2]];
  36. XCTAssertTrue(self.uut.viewControllers.count == 2);
  37. }
  38. - (void)testChildViewControllerForStatusBarStyle_shouldReturnTopViewController {
  39. XCTAssertTrue(self.uut.childViewControllerForStatusBarStyle == self.uut.topViewController);
  40. }
  41. - (void)testCurrentChild_shouldReturnTopViewController {
  42. XCTAssertTrue(self.uut.getCurrentChild == self.uut.topViewController);
  43. }
  44. - (void)testGetLeafViewController_shouldReturnTopViewController {
  45. XCTAssertTrue(self.uut.getCurrentChild == self.uut.topViewController);
  46. }
  47. - (void)testCurrentChild_shouldReturnLastChildWithLayoutInfo {
  48. [self.uut addChildViewController:[UIViewController new]];
  49. XCTAssertTrue(self.uut.getCurrentChild != self.uut.topViewController);
  50. XCTAssertTrue(self.uut.getCurrentChild == self.uut.childViewControllers[self.uut.childViewControllers.count-2]);
  51. }
  52. - (void)testPreferredStatusBarStyle_shouldReturnLeafPreferredStatusBarStyle {
  53. self.uut.getCurrentChild.resolveOptions.statusBar.style = [[Text alloc] initWithValue:@"light"];
  54. XCTAssertTrue(self.uut.preferredStatusBarStyle == self.uut.getCurrentChild.preferredStatusBarStyle);
  55. }
  56. - (void)testPopGestureEnabled_false {
  57. NSNumber* popGestureEnabled = @(0);
  58. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  59. options.popGesture = [[Bool alloc] initWithValue:popGestureEnabled];
  60. self.uut = [self createNavigationControllerWithOptions:options];
  61. [self.uut viewWillAppear:false];
  62. XCTAssertFalse(self.uut.interactivePopGestureRecognizer.enabled);
  63. }
  64. - (void)testPopGestureEnabled_true {
  65. NSNumber* popGestureEnabled = @(1);
  66. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  67. options.popGesture = [[Bool alloc] initWithValue:popGestureEnabled];
  68. self.uut = [self createNavigationControllerWithOptions:options];
  69. [self.uut onChildWillAppear];
  70. XCTAssertTrue(self.uut.interactivePopGestureRecognizer.enabled);
  71. }
  72. - (void)testRootBackgroundImage {
  73. UIImage* rootBackgroundImage = [[UIImage alloc] init];
  74. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  75. options.rootBackgroundImage = [[Image alloc] initWithValue:rootBackgroundImage];
  76. self.uut = [self createNavigationControllerWithOptions:options];
  77. [self.uut onChildWillAppear];
  78. XCTAssertTrue([[(UIImageView*)self.uut.view.subviews[0] image] isEqual:rootBackgroundImage]);
  79. }
  80. - (void)testTopBarBackgroundClipToBounds_true {
  81. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  82. options.topBar.background.clipToBounds = [[Bool alloc] initWithValue:@(1)];
  83. self.uut = [self createNavigationControllerWithOptions:options];
  84. [self.uut onChildWillAppear];
  85. XCTAssertTrue(self.uut.navigationBar.clipsToBounds);
  86. }
  87. - (void)testTopBarBackgroundClipToBounds_false {
  88. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  89. options.topBar.background.clipToBounds = [[Bool alloc] initWithValue:@(0)];
  90. self.uut = [self createNavigationControllerWithOptions:options];
  91. XCTAssertFalse(self.uut.navigationController.navigationBar.clipsToBounds);
  92. }
  93. - (void)testSupportedOrientationsShouldReturnCurrentChildSupportedOrientations {
  94. XCTAssertEqual(self.uut.supportedInterfaceOrientations, self.uut.getCurrentChild.supportedInterfaceOrientations);
  95. }
  96. - (void)testPopViewControllerReturnLastChildViewController {
  97. RNNStackController* uut = [RNNStackController new];
  98. [uut setViewControllers:@[_vc1, _vc2]];
  99. XCTAssertEqual([uut popViewControllerAnimated:NO], _vc2);
  100. }
  101. - (void)testPopViewControllerSetTopBarBackgroundForPoppingViewController {
  102. _options.topBar.background.color = [[Color alloc] initWithValue:[UIColor redColor]];
  103. [_vc1 overrideOptions:_options];
  104. [self.uut popViewControllerAnimated:NO];
  105. XCTAssertEqual(_vc1.resolveOptions.topBar.background.color.get, self.uut.navigationBar.standardAppearance.backgroundColor);
  106. }
  107. - (void)testPopViewControllerSetDefaultTopBarBackgroundForPoppingViewController {
  108. _options.topBar.background.color = [[Color alloc] initWithValue:[UIColor redColor]];
  109. [_vc1 setDefaultOptions:_options];
  110. [self.uut popViewControllerAnimated:NO];
  111. XCTAssertEqual(_vc1.resolveOptions.topBar.background.color.get, self.uut.navigationBar.barTintColor);
  112. }
  113. - (void)testPopViewControllerShouldInvokeApplyOptionsBeforePoppingForDestinationViewController {
  114. RNNStackController* uut = [RNNStackController new];
  115. [uut setViewControllers:@[_vc1, _vc2]];
  116. [[(id)uut.presenter expect] applyOptionsBeforePopping:[OCMArg any]];
  117. [uut popViewControllerAnimated:NO];
  118. [(id)uut.presenter verify];
  119. }
  120. - (void)testPopViewController_ShouldEmitScreenPoppedEvent {
  121. RNNStackController* uut = [RNNStackController new];
  122. [uut setViewControllers:@[_vc1, _vc2]];
  123. [[(id)uut.eventEmitter expect] sendScreenPoppedEvent:_vc2.layoutInfo.componentId];
  124. [uut popViewControllerAnimated:NO];
  125. [(id)uut.eventEmitter verify];
  126. }
  127. - (void)testOverrideOptionsShouldOverrideOptionsState {
  128. RNNNavigationOptions* overrideOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  129. [(RNNNavigationOptions*)[(id)self.uut.options expect] overrideOptions:overrideOptions];
  130. [self.uut overrideOptions:overrideOptions];
  131. [(id)self.uut.options verify];
  132. }
  133. - (void)testMergeChildOptionsShouldUpdatePresenterForVisibleChild {
  134. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  135. [[_presenter expect] mergeOptions:options resolvedOptions:[OCMArg any]];
  136. [self.uut mergeChildOptions:options child:self.uut.childViewControllers.lastObject];
  137. [_presenter verify];
  138. }
  139. - (void)testMergeChildOptionsShouldNotUpdatePresenterForInvisibleChild {
  140. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  141. [[_presenter reject] mergeOptions:options resolvedOptions:self.uut.resolveOptions];
  142. [self.uut mergeChildOptions:options child:self.uut.childViewControllers.firstObject];
  143. [_presenter verify];
  144. }
  145. - (RNNStackController *)createNavigationControllerWithOptions:(RNNNavigationOptions *)options {
  146. RNNStackController* nav = [[RNNStackController alloc] initWithLayoutInfo:nil creator:_creator options:options defaultOptions:nil presenter:[[RNNStackPresenter alloc] init] eventEmitter:nil childViewControllers:@[_vc1]];
  147. return nav;
  148. }
  149. @end