react-native-navigation的迁移库

RNNDotIndicatorPresenterTest.m 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #import <XCTest/XCTest.h>
  2. #import <OCMock/OCMock.h>
  3. #import "RNNDotIndicatorPresenter.h"
  4. #import "DotIndicatorOptions.h"
  5. #import "RNNBottomTabsController.h"
  6. #import "RNNComponentViewController.h"
  7. #import "RNNTestBase.h"
  8. #import "UITabBarController+RNNUtils.h"
  9. @interface RNNDotIndicatorPresenterTest : RNNTestBase
  10. @property(nonatomic, strong) id uut;
  11. @property(nonatomic, strong) RNNComponentViewController *child;
  12. @property(nonatomic, strong) id bottomTabs;
  13. @end
  14. @implementation RNNDotIndicatorPresenterTest
  15. - (void)setUp {
  16. [super setUp];
  17. self.uut = [OCMockObject partialMockForObject:[RNNDotIndicatorPresenter new]];
  18. self.bottomTabs = [OCMockObject partialMockForObject:[RNNBottomTabsController new]];
  19. self.child = [self createChild];
  20. [self.bottomTabs addChildViewController:self.child];
  21. [self setupTopLevelUI:self.bottomTabs];
  22. }
  23. - (void)tearDown {
  24. [self tearDownTopLevelUI:_bottomTabs];
  25. [super tearDown];
  26. }
  27. - (void)testApply_doesNothingIfDoesNotHaveValue {
  28. DotIndicatorOptions *empty = [DotIndicatorOptions new];
  29. [[self uut] apply:self.child :empty];
  30. XCTAssertFalse([self tabHasIndicator]);
  31. }
  32. - (void)testApply_indicatorIsAddedToTabView {
  33. [self applyIndicator];
  34. XCTAssertTrue([self tabHasIndicator]);
  35. }
  36. - (void)testApply_indicatorIsRemovedIfNotVisible {
  37. [self applyIndicator];
  38. XCTAssertTrue([self tabHasIndicator]);
  39. DotIndicatorOptions *options = [DotIndicatorOptions new];
  40. options.visible = [[Bool alloc] initWithBOOL:NO];
  41. [[self uut] apply:self.child :options];
  42. XCTAssertFalse([self tabHasIndicator]);
  43. }
  44. - (void)testApply_invisibleIndicatorIsNotAdded {
  45. DotIndicatorOptions *options = [DotIndicatorOptions new];
  46. options.visible = [[Bool alloc] initWithBOOL:NO];
  47. [[self uut] apply:self.child :options];
  48. XCTAssertFalse([self tabHasIndicator]);
  49. }
  50. - (void)testApply_itDoesNotRecreateIfEqualToCurrentlyVisibleIndicator {
  51. [self applyIndicator];
  52. UIView *indicator1 = [self getIndicator];
  53. [self applyIndicator];
  54. UIView *indicator2 = [self getIndicator];
  55. XCTAssertEqualObjects(indicator1, indicator2);
  56. }
  57. - (void)testApply_itAddsIndicatorToCorrectTabView {
  58. [self applyIndicator];
  59. UIView *indicator1 = [self getIndicator];
  60. XCTAssertEqualObjects([indicator1 superview], [_bottomTabs getTabView:0]);
  61. }
  62. - (void)testApply_itRemovesPreviousDotIndicator {
  63. NSUInteger childCountBeforeApplyingIndicator = [[_bottomTabs getTabView:0] subviews].count;
  64. [self applyIndicator];
  65. NSUInteger childCountAfterApplyingIndicatorOnce = [[_bottomTabs getTabView:0] subviews].count;
  66. XCTAssertEqual(childCountBeforeApplyingIndicator + 1, childCountAfterApplyingIndicatorOnce);
  67. [self applyIndicator:[UIColor greenColor]];
  68. NSUInteger childCountAfterApplyingIndicatorTwice = [[_bottomTabs getTabView:0] subviews].count;
  69. XCTAssertEqual([[self getIndicator] backgroundColor], [UIColor greenColor]);
  70. XCTAssertEqual(childCountAfterApplyingIndicatorOnce, childCountAfterApplyingIndicatorTwice);
  71. }
  72. - (void)testApply_itRemovesPreviousIndicator {
  73. DotIndicatorOptions *options = [DotIndicatorOptions new];
  74. options.visible = [[Bool alloc] initWithBOOL:YES];
  75. options.color = [[Color alloc] initWithValue:[UIColor redColor]];
  76. options.size = [[Number alloc] initWithValue:[[NSNumber alloc] initWithInt:8]];
  77. [[self uut] apply:self.child :options];
  78. XCTAssertTrue([self tabHasIndicator]);
  79. options.visible = [[Bool alloc] initWithBOOL:NO];
  80. [[self uut] apply:self.child :options];
  81. XCTAssertFalse([self tabHasIndicator]);
  82. }
  83. - (void)testApply_indicatorIsAlignedToTopRightOfIcon {
  84. DotIndicatorOptions *options = [DotIndicatorOptions new];
  85. options.visible = [[Bool alloc] initWithBOOL:YES];
  86. options.size = [[Number alloc] initWithValue:[[NSNumber alloc] initWithInt:8]];
  87. [[self uut] apply:self.child :options];
  88. UIView *indicator = [self getIndicator];
  89. UIView * icon = [_bottomTabs getTabIcon:0];
  90. NSArray<NSLayoutConstraint *> *alignmentConstraints = [_bottomTabs getTabView:0].constraints;
  91. XCTAssertEqual([alignmentConstraints count], 2);
  92. XCTAssertEqual([alignmentConstraints[0] constant], -4);
  93. XCTAssertEqual([alignmentConstraints[0] firstItem], indicator);
  94. XCTAssertEqual([alignmentConstraints[0] secondItem], icon);
  95. XCTAssertEqual([alignmentConstraints[0] firstAttribute], NSLayoutAttributeLeft);
  96. XCTAssertEqual([alignmentConstraints[0] secondAttribute], NSLayoutAttributeRight);
  97. XCTAssertEqual([alignmentConstraints[1] constant], -4);
  98. XCTAssertEqual([alignmentConstraints[1] firstItem], indicator);
  99. XCTAssertEqual([alignmentConstraints[1] secondItem], icon);
  100. XCTAssertEqual([alignmentConstraints[1] firstAttribute], NSLayoutAttributeTop);
  101. XCTAssertEqual([alignmentConstraints[1] secondAttribute], NSLayoutAttributeTop);
  102. NSArray *sizeConstraints = indicator.constraints;
  103. XCTAssertEqual([sizeConstraints count], 2);
  104. XCTAssertEqual([sizeConstraints[0] constant], 8);
  105. XCTAssertEqual([sizeConstraints[1] constant], 8);
  106. }
  107. - (void)applyIndicator {
  108. [self applyIndicator:[UIColor redColor]];
  109. }
  110. - (void)applyIndicator:(UIColor *) color {
  111. DotIndicatorOptions *options = [DotIndicatorOptions new];
  112. options.visible = [[Bool alloc] initWithBOOL:YES];
  113. options.color = [[Color alloc] initWithValue:color];
  114. [[self uut] apply:self.child :options];
  115. }
  116. - (RNNComponentViewController *)createChild {
  117. RNNNavigationOptions *options = [RNNNavigationOptions new];
  118. options.bottomTab = [RNNBottomTabOptions new];
  119. id img = [OCMockObject partialMockForObject:[UIImage new]];
  120. options.bottomTab.icon = [[Image alloc] initWithValue:img];
  121. return [[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:nil eventEmitter:nil presenter:[RNNComponentPresenter new] options:options defaultOptions:nil];
  122. }
  123. - (BOOL)tabHasIndicator {
  124. return [self.child tabBarItem].tag > 0;
  125. }
  126. - (UIView *)getIndicator {
  127. return [self tabHasIndicator] ? [[((UITabBarController *) _bottomTabs) tabBar] viewWithTag:_child.tabBarItem.tag] : nil;
  128. }
  129. @end