react-native-navigation的迁移库

UIViewController+LayoutProtocolTest.m 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #import <XCTest/XCTest.h>
  2. #import <OCMock/OCMock.h>
  3. #import "UIViewController+LayoutProtocol.h"
  4. #import "UIViewController+RNNOptions.h"
  5. #import "RNNComponentPresenter.h"
  6. #import "RCTConvert+Modal.h"
  7. #import "RNNBottomTabsController.h"
  8. #import "RNNStackController.h"
  9. @interface UIViewController_LayoutProtocolTest : XCTestCase
  10. @property (nonatomic, retain) UIViewController * uut;
  11. @end
  12. @implementation UIViewController_LayoutProtocolTest
  13. - (void)setUp {
  14. [super setUp];
  15. self.uut = [OCMockObject partialMockForObject:[UIViewController new]];
  16. _uut.layoutInfo = [[RNNLayoutInfo alloc] init];
  17. _uut.layoutInfo.componentId = @"componentId";
  18. }
  19. - (void)testInitWithLayoutApplyDefaultOptions {
  20. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] init];
  21. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  22. RNNNavigationOptions* defaultOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  23. defaultOptions.modalPresentationStyle = [[Text alloc] initWithValue:@"fullScreen"];
  24. UIViewController* uut = [[UIViewController alloc] initWithLayoutInfo:nil creator:nil options:options defaultOptions:defaultOptions presenter:presenter eventEmitter:nil childViewControllers:nil];
  25. XCTAssertEqual(uut.modalPresentationStyle, [RCTConvert UIModalPresentationStyle:@"fullScreen"]);
  26. }
  27. - (void)testInitWithLayoutInfoShouldSetChildViewControllers {
  28. UIViewController* child1 = [UIViewController new];
  29. UIViewController* child2 = [UIViewController new];
  30. NSArray* childViewControllers = @[child1, child2];
  31. UINavigationController* uut = [[UINavigationController alloc] initWithLayoutInfo:nil creator:nil options:nil defaultOptions:nil presenter:nil eventEmitter:nil childViewControllers:childViewControllers];
  32. XCTAssertEqual(uut.viewControllers[0], child1);
  33. XCTAssertEqual(uut.viewControllers[1], child2);
  34. }
  35. - (void)testSetBackButtonIcon_withColor_shouldSetColor {
  36. UIViewController* uut = [UIViewController new];
  37. [[UINavigationController alloc] initWithRootViewController:uut];
  38. UIColor* color = [UIColor blackColor];
  39. [uut setBackButtonIcon:nil withColor:color title:nil];
  40. XCTAssertEqual(color, uut.navigationItem.backBarButtonItem.tintColor);
  41. }
  42. - (void)testSetBackButtonIcon_withColor_shouldSetTitle {
  43. UIViewController* uut = [UIViewController new];
  44. UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:uut];
  45. NSString* title = @"Title";
  46. [uut setBackButtonIcon:nil withColor:nil title:title];
  47. XCTAssertEqual(title, uut.navigationItem.backBarButtonItem.title);
  48. }
  49. - (void)testSetBackButtonIcon_withColor_shouldSetIcon {
  50. UIViewController* uut = [UIViewController new];
  51. UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:uut];
  52. UIImage* icon = [UIImage new];
  53. [uut setBackButtonIcon:icon withColor:nil title:nil];
  54. XCTAssertEqual(icon, uut.navigationItem.backBarButtonItem.image);
  55. }
  56. - (void)testSetBackButtonIcon_shouldSetTitleOnPreviousViewControllerIfExists {
  57. UIViewController* uut = [UIViewController new];
  58. UIViewController* viewController2 = [UIViewController new];
  59. UINavigationController* nav = [[UINavigationController alloc] init];
  60. [nav setViewControllers:@[uut, viewController2]];
  61. NSString* title = @"Title";
  62. [uut setBackButtonIcon:nil withColor:nil title:title];
  63. XCTAssertEqual(title, uut.navigationItem.backBarButtonItem.title);
  64. }
  65. - (void)testResolveOptions {
  66. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] init];
  67. RNNNavigationOptions* childOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  68. RNNNavigationOptions* parentOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  69. parentOptions.bottomTab.text = [[Text alloc] initWithValue:@"text"];
  70. parentOptions.bottomTab.selectedIconColor = [[Color alloc] initWithValue:UIColor.redColor];
  71. RNNNavigationOptions* defaultOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  72. defaultOptions.bottomTab.text = [[Text alloc] initWithValue:@"default text"];
  73. defaultOptions.bottomTab.selectedIconColor = [[Color alloc] initWithValue:UIColor.blueColor];
  74. UIViewController* child = [[UIViewController alloc] initWithLayoutInfo:nil creator:nil options:childOptions defaultOptions:defaultOptions presenter:presenter eventEmitter:nil childViewControllers:nil];
  75. RNNStackController* parent = [[RNNStackController alloc] initWithLayoutInfo:nil creator:nil options:parentOptions defaultOptions:defaultOptions presenter:presenter eventEmitter:nil childViewControllers:@[child]];
  76. XCTAssertEqual([parent getCurrentChild], child);
  77. XCTAssertEqual([[parent resolveOptions].bottomTab.text get], @"text");
  78. XCTAssertEqual([[parent resolveOptions].bottomTab.selectedIconColor get], UIColor.redColor);
  79. }
  80. - (void)testMergeOptions_invokedOnParentViewController {
  81. id parent = [OCMockObject partialMockForObject:[RNNStackController new]];
  82. RNNNavigationOptions * toMerge = [[RNNNavigationOptions alloc] initEmptyOptions];
  83. [(UIViewController *) [parent expect] mergeChildOptions:toMerge];
  84. RNNStackController* uut = [[RNNStackController alloc] initWithLayoutInfo:nil creator:nil options:nil defaultOptions:nil presenter:nil eventEmitter:nil childViewControllers:nil];
  85. [parent addChildViewController:uut];
  86. [uut mergeOptions:toMerge];
  87. [parent verify];
  88. }
  89. - (void)testMergeOptions_presenterIsInvokedWithResolvedOptions {
  90. id parent = [OCMockObject partialMockForObject:[RNNStackController new]];
  91. id presenter = [OCMockObject partialMockForObject:[RNNStackPresenter new]];
  92. RNNNavigationOptions * toMerge = [[RNNNavigationOptions alloc] initEmptyOptions];
  93. toMerge.topBar.title.color = [[Color alloc] initWithValue:[UIColor redColor]];
  94. [[presenter expect] mergeOptions:toMerge resolvedOptions:[OCMArg checkWithBlock:^(id value) {
  95. RNNNavigationOptions *options = (RNNNavigationOptions *) value;
  96. XCTAssertEqual([options.topBar.title.text get], @"Initial title");
  97. XCTAssertEqual([options.bottomTab.text get], @"Child tab text");
  98. return YES;
  99. }]];
  100. RNNNavigationOptions * childOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  101. childOptions.bottomTab.text = [[Text alloc] initWithValue:@"Child tab text"];
  102. UIViewController* child = [[UIViewController alloc] initWithLayoutInfo:nil creator:nil options:childOptions defaultOptions:nil presenter:presenter eventEmitter:nil childViewControllers:nil];
  103. RNNNavigationOptions * initialOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  104. initialOptions.topBar.title.text = [[Text alloc] initWithValue:@"Initial title"];
  105. RNNStackController* uut = [[RNNStackController alloc] initWithLayoutInfo:nil creator:nil options:initialOptions defaultOptions:nil presenter:presenter eventEmitter:nil childViewControllers:@[child]];
  106. [parent addChildViewController:uut];
  107. [uut mergeOptions:toMerge];
  108. [presenter verify];
  109. }
  110. - (void)testMergeOptions_mergedIntoCurrentOptions {
  111. UIViewController* uut = [[UIViewController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:nil presenter:nil eventEmitter:nil childViewControllers:nil];
  112. RNNNavigationOptions * toMerge = [[RNNNavigationOptions alloc] initEmptyOptions];
  113. toMerge.topBar.title.text = [[Text alloc] initWithValue:@"merged"];
  114. [uut mergeOptions:toMerge];
  115. XCTAssertEqual(uut.resolveOptions.topBar.title.text.get, @"merged");
  116. }
  117. @end