react-native-navigation的迁移库

UIViewController+LayoutProtocolTest.m 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "RNNBottomTabsController.h"
  7. #import "RNNStackController.h"
  8. @interface UIViewController_LayoutProtocolTest : XCTestCase
  9. @property (nonatomic, retain) UIViewController * uut;
  10. @end
  11. @implementation UIViewController_LayoutProtocolTest
  12. - (void)setUp {
  13. [super setUp];
  14. self.uut = [OCMockObject partialMockForObject:[UIViewController new]];
  15. _uut.layoutInfo = [[RNNLayoutInfo alloc] init];
  16. _uut.layoutInfo.componentId = @"componentId";
  17. }
  18. - (void)testInitWithLayoutApplyDefaultOptions {
  19. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] init];
  20. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  21. RNNNavigationOptions* defaultOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  22. defaultOptions.modalPresentationStyle = [[Text alloc] initWithValue:@"default"];
  23. UIViewController* uut = [[UIViewController alloc] initWithLayoutInfo:nil creator:nil options:options defaultOptions:defaultOptions presenter:presenter eventEmitter:nil childViewControllers:nil];
  24. XCTAssertEqual(uut.modalPresentationStyle, UIModalPresentationPageSheet);
  25. }
  26. - (void)testInitWithLayoutInfoShouldSetChildViewControllers {
  27. UIViewController* child1 = [UIViewController new];
  28. UIViewController* child2 = [UIViewController new];
  29. NSArray* childViewControllers = @[child1, child2];
  30. UINavigationController* uut = [[UINavigationController alloc] initWithLayoutInfo:nil creator:nil options:nil defaultOptions:nil presenter:nil eventEmitter:nil childViewControllers:childViewControllers];
  31. XCTAssertEqual(uut.viewControllers.count, 2);
  32. }
  33. - (void)testInitBottomTabsWithLayoutInfoShouldNotSetChildViewControllers {
  34. UIViewController* child1 = [UIViewController new];
  35. UIViewController* child2 = [UIViewController new];
  36. NSArray* childViewControllers = @[child1, child2];
  37. RNNBottomTabsController* uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:nil defaultOptions:nil presenter:nil eventEmitter:nil childViewControllers:childViewControllers];
  38. XCTAssertEqual(uut.viewControllers.count, 0);
  39. }
  40. - (void)testLoadChildrenShouldSetChildViewControllers {
  41. UIViewController* child1 = [UIViewController new];
  42. UIViewController* child2 = [UIViewController new];
  43. NSArray* childViewControllers = @[child1, child2];
  44. UINavigationController* uut = [[UINavigationController alloc] initWithLayoutInfo:nil creator:nil options:nil defaultOptions:nil presenter:nil eventEmitter:nil childViewControllers:childViewControllers];
  45. [uut loadChildren:childViewControllers];
  46. XCTAssertEqual(uut.viewControllers[0], child1);
  47. XCTAssertEqual(uut.viewControllers[1], child2);
  48. }
  49. - (void)testResolveOptions {
  50. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] init];
  51. RNNNavigationOptions* childOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  52. RNNNavigationOptions* parentOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  53. parentOptions.bottomTab.text = [[Text alloc] initWithValue:@"text"];
  54. parentOptions.bottomTab.selectedIconColor = [[Color alloc] initWithValue:UIColor.redColor];
  55. RNNNavigationOptions* defaultOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  56. defaultOptions.bottomTab.text = [[Text alloc] initWithValue:@"default text"];
  57. defaultOptions.bottomTab.selectedIconColor = [[Color alloc] initWithValue:UIColor.blueColor];
  58. UIViewController* child = [[UIViewController alloc] initWithLayoutInfo:[RNNLayoutInfo new] creator:nil options:childOptions defaultOptions:defaultOptions presenter:presenter eventEmitter:nil childViewControllers:nil];
  59. RNNStackController* parent = [[RNNStackController alloc] initWithLayoutInfo:nil creator:nil options:parentOptions defaultOptions:defaultOptions presenter:presenter eventEmitter:nil childViewControllers:@[child]];
  60. XCTAssertEqual([parent getCurrentChild], child);
  61. XCTAssertEqual([[parent resolveOptions].bottomTab.text get], @"text");
  62. XCTAssertEqual([[parent resolveOptions].bottomTab.selectedIconColor get], UIColor.redColor);
  63. }
  64. - (void)testMergeOptions_invokedOnParentViewController {
  65. id parent = [OCMockObject partialMockForObject:[RNNStackController new]];
  66. RNNStackController* uut = [[RNNStackController alloc] initWithLayoutInfo:nil creator:nil options:nil defaultOptions:nil presenter:nil eventEmitter:nil childViewControllers:nil];
  67. RNNNavigationOptions * toMerge = [[RNNNavigationOptions alloc] initEmptyOptions];
  68. [(UIViewController *) [parent expect] mergeChildOptions:toMerge child:uut];
  69. [parent addChildViewController:uut];
  70. [uut mergeOptions:toMerge];
  71. [parent verify];
  72. }
  73. - (void)testMergeOptions_presenterIsInvokedWithResolvedOptions {
  74. id parent = [OCMockObject partialMockForObject:[RNNStackController new]];
  75. id presenter = [OCMockObject partialMockForObject:[RNNStackPresenter new]];
  76. RNNNavigationOptions * toMerge = [[RNNNavigationOptions alloc] initEmptyOptions];
  77. toMerge.topBar.title.color = [[Color alloc] initWithValue:[UIColor redColor]];
  78. [[presenter expect] mergeOptions:toMerge resolvedOptions:[OCMArg checkWithBlock:^(id value) {
  79. RNNNavigationOptions *options = (RNNNavigationOptions *) value;
  80. XCTAssertEqual([options.topBar.title.text get], @"Initial title");
  81. XCTAssertEqual([options.bottomTab.text get], @"Child tab text");
  82. return YES;
  83. }]];
  84. RNNNavigationOptions * childOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  85. childOptions.bottomTab.text = [[Text alloc] initWithValue:@"Child tab text"];
  86. UIViewController* child = [[UIViewController alloc] initWithLayoutInfo:[RNNLayoutInfo new] creator:nil options:childOptions defaultOptions:nil presenter:[RNNComponentPresenter new] eventEmitter:nil childViewControllers:nil];
  87. RNNNavigationOptions * initialOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  88. initialOptions.topBar.title.text = [[Text alloc] initWithValue:@"Initial title"];
  89. RNNStackController* uut = [[RNNStackController alloc] initWithLayoutInfo:[RNNLayoutInfo new] creator:nil options:initialOptions defaultOptions:nil presenter:presenter eventEmitter:nil childViewControllers:@[child]];
  90. [parent addChildViewController:uut];
  91. [uut mergeOptions:toMerge];
  92. [presenter verify];
  93. }
  94. - (void)testMergeOptions_mergedIntoCurrentOptions {
  95. UIViewController* uut = [[UIViewController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:nil presenter:nil eventEmitter:nil childViewControllers:nil];
  96. RNNNavigationOptions * toMerge = [[RNNNavigationOptions alloc] initEmptyOptions];
  97. toMerge.topBar.title.text = [[Text alloc] initWithValue:@"merged"];
  98. [uut mergeOptions:toMerge];
  99. XCTAssertEqual(uut.resolveOptions.topBar.title.text.get, @"merged");
  100. }
  101. - (void)testLayout_shouldExtendedLayoutIncludesOpaqueBars {
  102. UIViewController* component = [[UIViewController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:nil presenter:nil eventEmitter:nil childViewControllers:nil];
  103. UINavigationController* stack = [[UINavigationController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:nil presenter:nil eventEmitter:nil childViewControllers:nil];
  104. UITabBarController* tabBar = [[UITabBarController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:nil presenter:nil eventEmitter:nil childViewControllers:nil];
  105. XCTAssertTrue(component.extendedLayoutIncludesOpaqueBars);
  106. XCTAssertTrue(stack.extendedLayoutIncludesOpaqueBars);
  107. XCTAssertTrue(tabBar.extendedLayoutIncludesOpaqueBars);
  108. }
  109. @end