react-native-navigation的迁移库

RNNRootViewControllerTest.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #import <XCTest/XCTest.h>
  2. #import "RNNRootViewController.h"
  3. #import "RNNReactRootViewCreator.h"
  4. #import "RNNTestRootViewCreator.h"
  5. #import <React/RCTConvert.h>
  6. #import "RNNNavigationOptions.h"
  7. #import "RNNUIBarButtonItem.h"
  8. @interface RNNRootViewControllerTest : XCTestCase
  9. @property (nonatomic, strong) id<RNNRootViewCreator> creator;
  10. @property (nonatomic, strong) NSString* pageName;
  11. @property (nonatomic, strong) NSString* containerId;
  12. @property (nonatomic, strong) id emitter;
  13. @property (nonatomic, strong) RNNNavigationOptions* options;
  14. @property (nonatomic, strong) RNNRootViewController* uut;
  15. @end
  16. @implementation RNNRootViewControllerTest
  17. - (void)setUp {
  18. [super setUp];
  19. self.creator = [[RNNTestRootViewCreator alloc] init];
  20. self.pageName = @"somename";
  21. self.containerId = @"cntId";
  22. self.emitter = nil;
  23. self.options = [RNNNavigationOptions new];
  24. self.uut = [[RNNRootViewController alloc] initWithName:self.pageName withOptions:self.options withContainerId:self.containerId rootViewCreator:self.creator eventEmitter:self.emitter];
  25. }
  26. -(void)testTopBarBackgroundColor_validColor{
  27. NSNumber* inputColor = @(0xFFFF0000);
  28. self.options.topBarBackgroundColor = inputColor;
  29. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  30. [self.uut viewWillAppear:false];
  31. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  32. XCTAssertTrue([self.uut.navigationController.navigationBar.barTintColor isEqual:expectedColor]);
  33. }
  34. -(void)testTopBarBackgroundColorWithoutNavigationController{
  35. NSNumber* inputColor = @(0xFFFF0000);
  36. self.options.topBarBackgroundColor = inputColor;
  37. XCTAssertNoThrow([self.uut viewWillAppear:false]);
  38. }
  39. - (void)testStatusBarHidden_default {
  40. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  41. [self.uut viewWillAppear:false];
  42. XCTAssertFalse([self.uut prefersStatusBarHidden]);
  43. }
  44. - (void)testStatusBarHidden_true {
  45. self.options.statusBarHidden = @(1);
  46. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  47. [self.uut viewWillAppear:false];
  48. XCTAssertTrue([self.uut prefersStatusBarHidden]);
  49. }
  50. - (void)testStatusBarHideWithTopBar_false {
  51. self.options.statusBarHideWithTopBar = @(0);
  52. self.options.topBarHidden = @(1);
  53. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  54. [self.uut viewWillAppear:false];
  55. XCTAssertFalse([self.uut prefersStatusBarHidden]);
  56. }
  57. - (void)testStatusBarHideWithTopBar_true {
  58. self.options.statusBarHideWithTopBar = @(1);
  59. self.options.topBarHidden = @(1);
  60. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  61. [self.uut viewWillAppear:false];
  62. XCTAssertTrue([self.uut prefersStatusBarHidden]);
  63. }
  64. - (void)testStatusBarHidden_false {
  65. self.options.statusBarHidden = @(0);
  66. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  67. [self.uut viewWillAppear:false];
  68. XCTAssertFalse([self.uut prefersStatusBarHidden]);
  69. }
  70. -(void)testTitle_string{
  71. NSString* title =@"some title";
  72. self.options.title= title;
  73. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  74. [self.uut viewWillAppear:false];
  75. XCTAssertTrue([self.uut.navigationItem.title isEqual:title]);
  76. }
  77. -(void)testTitle_default{
  78. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  79. [self.uut viewWillAppear:false];
  80. XCTAssertNil(self.uut.navigationItem.title);
  81. }
  82. -(void)testTopBarTextColor_validColor{
  83. NSNumber* inputColor = @(0xFFFF0000);
  84. self.options.topBarTextColor = inputColor;
  85. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  86. [self.uut viewWillAppear:false];
  87. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  88. XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSColor"] isEqual:expectedColor]);
  89. }
  90. -(void)testScreenBackgroundColor_validColor{
  91. NSNumber* inputColor = @(0xFFFF0000);
  92. self.options.screenBackgroundColor = inputColor;
  93. [self.uut viewWillAppear:false];
  94. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  95. XCTAssertTrue([self.uut.view.backgroundColor isEqual:expectedColor]);
  96. }
  97. -(void)testTopBarTextFontFamily_validFont{
  98. NSString* inputFont = @"HelveticaNeue";
  99. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  100. self.options.topBarTextFontFamily = inputFont;
  101. [self.uut viewWillAppear:false];
  102. UIFont* expectedFont = [UIFont fontWithName:inputFont size:20];
  103. XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSFont"] isEqual:expectedFont]);
  104. }
  105. -(void)testTopBarHideOnScroll_true {
  106. NSNumber* hideOnScrollInput = @(1);
  107. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  108. self.options.topBarHideOnScroll = hideOnScrollInput;
  109. [self.uut viewWillAppear:false];
  110. XCTAssertTrue(self.uut.navigationController.hidesBarsOnSwipe);
  111. }
  112. -(void)testTopBarButtonColor {
  113. NSNumber* inputColor = @(0xFFFF0000);
  114. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  115. self.options.topBarButtonColor = inputColor;
  116. [self.uut viewWillAppear:false];
  117. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  118. XCTAssertTrue([self.uut.navigationController.navigationBar.tintColor isEqual:expectedColor]);
  119. }
  120. -(void)testTopBarTranslucent {
  121. NSNumber* topBarTranslucentInput = @(0);
  122. self.options.topBarTranslucent = topBarTranslucentInput;
  123. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  124. [self.uut viewWillAppear:false];
  125. XCTAssertFalse(self.uut.navigationController.navigationBar.translucent);
  126. }
  127. -(void)testTabBadge {
  128. NSString* tabBadgeInput = @"5";
  129. self.options.tabBadge = tabBadgeInput;
  130. __unused UITabBarController* vc = [[UITabBarController alloc] init];
  131. NSMutableArray* controllers = [NSMutableArray new];
  132. UITabBarItem* item = [[UITabBarItem alloc] initWithTitle:@"A Tab" image:nil tag:1];
  133. [self.uut setTabBarItem:item];
  134. [controllers addObject:self.uut];
  135. [vc setViewControllers:controllers];
  136. [self.uut viewWillAppear:false];
  137. XCTAssertTrue([self.uut.tabBarItem.badgeValue isEqualToString:tabBadgeInput]);
  138. }
  139. -(void)testTopBarTextFontSize_withoutTextFontFamily_withoutTextColor {
  140. NSNumber* topBarTextFontSizeInput = @(15);
  141. self.options.topBarTextFontSize = topBarTextFontSizeInput;
  142. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  143. [self.uut viewWillAppear:false];
  144. UIFont* expectedFont = [UIFont systemFontOfSize:15];
  145. XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSFont"] isEqual:expectedFont]);
  146. }
  147. -(void)testTopBarTextFontSize_withoutTextFontFamily_withTextColor {
  148. NSNumber* topBarTextFontSizeInput = @(15);
  149. NSNumber* inputColor = @(0xFFFF0000);
  150. self.options.topBarTextFontSize = topBarTextFontSizeInput;
  151. self.options.topBarTextColor = inputColor;
  152. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  153. [self.uut viewWillAppear:false];
  154. UIFont* expectedFont = [UIFont systemFontOfSize:15];
  155. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  156. XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSFont"] isEqual:expectedFont]);
  157. XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSColor"] isEqual:expectedColor]);
  158. }
  159. -(void)testTopBarTextFontSize_withTextFontFamily_withTextColor {
  160. NSNumber* topBarTextFontSizeInput = @(15);
  161. NSNumber* inputColor = @(0xFFFF0000);
  162. NSString* inputFont = @"HelveticaNeue";
  163. self.options.topBarTextFontSize = topBarTextFontSizeInput;
  164. self.options.topBarTextColor = inputColor;
  165. self.options.topBarTextFontFamily = inputFont;
  166. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  167. [self.uut viewWillAppear:false];
  168. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  169. UIFont* expectedFont = [UIFont fontWithName:inputFont size:15];
  170. XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSFont"] isEqual:expectedFont]);
  171. XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSColor"] isEqual:expectedColor]);
  172. }
  173. -(void)testTopBarTextFontSize_withTextFontFamily_withoutTextColor {
  174. NSNumber* topBarTextFontSizeInput = @(15);
  175. NSString* inputFont = @"HelveticaNeue";
  176. self.options.topBarTextFontSize = topBarTextFontSizeInput;
  177. self.options.topBarTextFontFamily = inputFont;
  178. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  179. [self.uut viewWillAppear:false];
  180. UIFont* expectedFont = [UIFont fontWithName:inputFont size:15];
  181. XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSFont"] isEqual:expectedFont]);
  182. }
  183. // TODO: Currently not passing
  184. -(void)testTopBarTextFontFamily_invalidFont{
  185. NSString* inputFont = @"HelveticaNeueeeee";
  186. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  187. self.options.topBarTextFontFamily = inputFont;
  188. // XCTAssertThrows([self.uut viewWillAppear:false]);
  189. }
  190. -(void)testRightButtonsWithTitle_withoutStyle {
  191. self.options.rightButtons = @[@{@"id": @"testId", @"title": @"test"}];
  192. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  193. [self.uut viewWillAppear:false];
  194. RNNUIBarButtonItem* button = (RNNUIBarButtonItem*)[nav.topViewController.navigationItem.rightBarButtonItems objectAtIndex:0];
  195. NSString* expectedButtonId = @"testId";
  196. NSString* expectedTitle = @"test";
  197. XCTAssertTrue([button.buttonId isEqualToString:expectedButtonId]);
  198. XCTAssertTrue([button.title isEqualToString:expectedTitle]);
  199. XCTAssertTrue(button.enabled);
  200. }
  201. -(void)testRightButtonsWithTitle_withStyle {
  202. NSNumber* inputColor = @(0xFFFF0000);
  203. self.options.rightButtons = @[@{@"id": @"testId", @"title": @"test", @"disabled": @true, @"buttonColor": inputColor, @"buttonFontSize": @22, @"buttonFontWeight": @"800"}];
  204. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  205. [self.uut viewWillAppear:false];
  206. RNNUIBarButtonItem* button = (RNNUIBarButtonItem*)[nav.topViewController.navigationItem.rightBarButtonItems objectAtIndex:0];
  207. NSString* expectedButtonId = @"testId";
  208. NSString* expectedTitle = @"test";
  209. XCTAssertTrue([button.buttonId isEqualToString:expectedButtonId]);
  210. XCTAssertTrue([button.title isEqualToString:expectedTitle]);
  211. XCTAssertFalse(button.enabled);
  212. //TODO: Determine how to tests buttonColor,buttonFontSize and buttonFontWeight?
  213. }
  214. -(void)testLeftButtonsWithTitle_withoutStyle {
  215. self.options.leftButtons = @[@{@"id": @"testId", @"title": @"test"}];
  216. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  217. [self.uut viewWillAppear:false];
  218. RNNUIBarButtonItem* button = (RNNUIBarButtonItem*)[nav.topViewController.navigationItem.leftBarButtonItems objectAtIndex:0];
  219. NSString* expectedButtonId = @"testId";
  220. NSString* expectedTitle = @"test";
  221. XCTAssertTrue([button.buttonId isEqualToString:expectedButtonId]);
  222. XCTAssertTrue([button.title isEqualToString:expectedTitle]);
  223. XCTAssertTrue(button.enabled);
  224. }
  225. -(void)testLeftButtonsWithTitle_withStyle {
  226. NSNumber* inputColor = @(0xFFFF0000);
  227. self.options.leftButtons = @[@{@"id": @"testId", @"title": @"test", @"disabled": @true, @"buttonColor": inputColor, @"buttonFontSize": @22, @"buttonFontWeight": @"800"}];
  228. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  229. [self.uut viewWillAppear:false];
  230. RNNUIBarButtonItem* button = (RNNUIBarButtonItem*)[nav.topViewController.navigationItem.leftBarButtonItems objectAtIndex:0];
  231. NSString* expectedButtonId = @"testId";
  232. NSString* expectedTitle = @"test";
  233. XCTAssertTrue([button.buttonId isEqualToString:expectedButtonId]);
  234. XCTAssertTrue([button.title isEqualToString:expectedTitle]);
  235. XCTAssertFalse(button.enabled);
  236. //TODO: Determine how to tests buttonColor,buttonFontSize and buttonFontWeight?
  237. }
  238. -(void)testTopBarNoBorderOn {
  239. NSNumber* topBarNoBorderInput = @(1);
  240. self.options.topBarNoBorder = topBarNoBorderInput;
  241. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  242. [self.uut viewWillAppear:false];
  243. XCTAssertNotNil(self.uut.navigationController.navigationBar.shadowImage);
  244. }
  245. -(void)testTopBarNoBorderOff {
  246. NSNumber* topBarNoBorderInput = @(0);
  247. self.options.topBarNoBorder = topBarNoBorderInput;
  248. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  249. [self.uut viewWillAppear:false];
  250. XCTAssertNil(self.uut.navigationController.navigationBar.shadowImage);
  251. }
  252. -(void)testStatusBarBlurOn {
  253. NSNumber* statusBarBlurInput = @(1);
  254. self.options.statusBarBlur = statusBarBlurInput;
  255. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  256. [self.uut viewWillAppear:false];
  257. XCTAssertNotNil([self.uut.view viewWithTag:BLUR_STATUS_TAG]);
  258. }
  259. -(void)testStatusBarBlurOff {
  260. NSNumber* statusBarBlurInput = @(0);
  261. self.options.statusBarBlur = statusBarBlurInput;
  262. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  263. [self.uut viewWillAppear:false];
  264. XCTAssertNil([self.uut.view viewWithTag:BLUR_STATUS_TAG]);
  265. }
  266. - (void)testTabBarHidden_default {
  267. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  268. [self.uut viewWillAppear:false];
  269. XCTAssertFalse([self.uut hidesBottomBarWhenPushed]);
  270. }
  271. - (void)testTabBarHidden_true {
  272. self.options.tabBarHidden = @(1);
  273. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  274. [self.uut viewWillAppear:false];
  275. XCTAssertTrue([self.uut hidesBottomBarWhenPushed]);
  276. }
  277. - (void)testTabBarHidden_false {
  278. self.options.tabBarHidden = @(0);
  279. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
  280. [self.uut viewWillAppear:false];
  281. XCTAssertFalse([self.uut hidesBottomBarWhenPushed]);
  282. }
  283. @end