react-native-navigation的迁移库

RNNNavigationControllerTest.m 8.2KB

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