react-native-navigation的迁移库

RNNNavigationControllerTest.m 7.2KB

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