react-native-navigation的迁移库

RNNCommandsHandlerTest.m 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #import <XCTest/XCTest.h>
  2. #import <objc/runtime.h>
  3. #import <ReactNativeNavigation/RNNCommandsHandler.h>
  4. #import <ReactNativeNavigation/RNNNavigationOptions.h>
  5. #import "RNNTestRootViewCreator.h"
  6. #import <ReactNativeNavigation/RNNComponentViewController.h>
  7. #import <ReactNativeNavigation/RNNStackController.h>
  8. #import <ReactNativeNavigation/RNNErrorHandler.h>
  9. #import <OCMock/OCMock.h>
  10. #import "RNNLayoutManager.h"
  11. #import "RNNBottomTabsController.h"
  12. #import "BottomTabsAttachModeFactory.h"
  13. @interface MockUIApplication : NSObject
  14. -(UIWindow *)keyWindow;
  15. @end
  16. @implementation MockUIApplication
  17. - (UIWindow *)keyWindow {
  18. return [UIWindow new];
  19. }
  20. @end
  21. @interface MockUINavigationController : RNNStackController
  22. @property (nonatomic, strong) NSArray* willReturnVCs;
  23. @end
  24. @implementation MockUINavigationController
  25. -(NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
  26. return self.willReturnVCs;
  27. }
  28. -(NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
  29. return self.willReturnVCs;
  30. }
  31. @end
  32. @interface RNNCommandsHandlerTest : XCTestCase
  33. @property (nonatomic, strong) RNNCommandsHandler* uut;
  34. @property (nonatomic, strong) id modalManager;
  35. @property (nonatomic, strong) RNNComponentViewController* vc1;
  36. @property (nonatomic, strong) RNNComponentViewController* vc2;
  37. @property (nonatomic, strong) RNNComponentViewController* vc3;
  38. @property (nonatomic, strong) MockUINavigationController* nvc;
  39. @property (nonatomic, strong) id mainWindow;
  40. @property (nonatomic, strong) id sharedApplication;
  41. @property (nonatomic, strong) id controllerFactory;
  42. @property (nonatomic, strong) id overlayManager;
  43. @property (nonatomic, strong) id eventEmmiter;
  44. @end
  45. @implementation RNNCommandsHandlerTest
  46. - (void)setUp {
  47. [super setUp];
  48. self.mainWindow = [OCMockObject partialMockForObject:[UIWindow new]];
  49. self.eventEmmiter = [OCMockObject partialMockForObject:[RNNEventEmitter new]];
  50. self.overlayManager = [OCMockObject partialMockForObject:[RNNOverlayManager new]];
  51. self.modalManager = [OCMockObject partialMockForObject:[RNNModalManager new]];
  52. self.controllerFactory = [OCMockObject partialMockForObject:[[RNNControllerFactory alloc] initWithRootViewCreator:nil eventEmitter:self.eventEmmiter store:nil componentRegistry:nil andBridge:nil bottomTabsAttachModeFactory:[BottomTabsAttachModeFactory new]]];
  53. self.uut = [[RNNCommandsHandler alloc] initWithControllerFactory:self.controllerFactory eventEmitter:self.eventEmmiter stackManager:[RNNNavigationStackManager new] modalManager:self.modalManager overlayManager:self.overlayManager mainWindow:_mainWindow];
  54. self.vc1 = self.generateComponent;
  55. self.vc2 = self.generateComponent;
  56. self.vc3 = self.generateComponent;
  57. _nvc = [[MockUINavigationController alloc] init];
  58. [_nvc setViewControllers:@[self.vc1, self.vc2, self.vc3]];
  59. OCMStub([self.sharedApplication keyWindow]).andReturn(self.mainWindow);
  60. }
  61. - (RNNComponentViewController *)generateComponent {
  62. return [[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:[[RNNTestRootViewCreator alloc] init] eventEmitter:nil presenter:[RNNComponentPresenter new] options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil];
  63. }
  64. - (void)testAssertReadyForEachMethodThrowsExceptoins {
  65. NSArray* methods = [self getPublicMethodNamesForObject:self.uut];
  66. [self.uut setReadyToReceiveCommands:false];
  67. for (NSString* methodName in methods) {
  68. SEL s = NSSelectorFromString(methodName);
  69. IMP imp = [self.uut methodForSelector:s];
  70. void (*func)(id, SEL, id, id, id, id, id) = (void *)imp;
  71. XCTAssertThrowsSpecificNamed(func(self.uut,s, nil, nil, nil, nil, nil), NSException, @"BridgeNotLoadedError");
  72. }
  73. }
  74. -(NSArray*) getPublicMethodNamesForObject:(NSObject*)obj{
  75. NSMutableArray* skipMethods = [NSMutableArray new];
  76. [skipMethods addObject:@"initWithControllerFactory:eventEmitter:stackManager:modalManager:overlayManager:mainWindow:"];
  77. [skipMethods addObject:@"assertReady"];
  78. [skipMethods addObject:@"setReadyToReceiveCommands:"];
  79. [skipMethods addObject:@"readyToReceiveCommands"];
  80. [skipMethods addObject:@".cxx_destruct"];
  81. [skipMethods addObject:@"dismissedModal:"];
  82. [skipMethods addObject:@"dismissedMultipleModals:"];
  83. NSMutableArray* result = [NSMutableArray new];
  84. // count and names:
  85. int i=0;
  86. unsigned int mc = 0;
  87. Method * mlist = class_copyMethodList(object_getClass(obj), &mc);
  88. for(i=0; i<mc; i++) {
  89. NSString *methodName = [NSString stringWithUTF8String:sel_getName(method_getName(mlist[i]))];
  90. // filter skippedMethods
  91. if (methodName && ![skipMethods containsObject:methodName]) {
  92. [result addObject:methodName];
  93. }
  94. }
  95. return result;
  96. }
  97. -(void)testDynamicStylesMergeWithStaticStyles {
  98. RNNNavigationOptions* initialOptions = [[RNNNavigationOptions alloc] initWithDict:@{}];
  99. initialOptions.topBar.title.text = [[Text alloc] initWithValue:@"the title"];
  100. RNNLayoutInfo* layoutInfo = [RNNLayoutInfo new];
  101. RNNTestRootViewCreator* creator = [[RNNTestRootViewCreator alloc] init];
  102. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] init];
  103. RNNComponentViewController* vc = [[RNNComponentViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:creator eventEmitter:nil presenter:presenter options:initialOptions defaultOptions:nil];
  104. RNNStackController* nav = [[RNNStackController alloc] initWithLayoutInfo:nil creator:creator options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:nil presenter:[[RNNStackPresenter alloc] init] eventEmitter:nil childViewControllers:@[vc]];
  105. [vc viewWillAppear:false];
  106. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  107. [self.uut setReadyToReceiveCommands:true];
  108. NSDictionary* dictFromJs = @{@"topBar": @{@"background" : @{@"color" : @(0xFFFF0000)}}};
  109. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  110. [self.uut mergeOptions:@"componentId" options:dictFromJs completion:^{
  111. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  112. XCTAssertTrue([nav.navigationBar.barTintColor isEqual:expectedColor]);
  113. }];
  114. }
  115. - (void)testMergeOptions_shouldOverrideOptions {
  116. RNNNavigationOptions* initialOptions = [[RNNNavigationOptions alloc] initWithDict:@{}];
  117. initialOptions.topBar.title.text = [[Text alloc] initWithValue:@"the title"];
  118. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] init];
  119. RNNComponentViewController* vc = [[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:[[RNNTestRootViewCreator alloc] init] eventEmitter:nil presenter:presenter options:initialOptions defaultOptions:nil];
  120. __unused RNNStackController* nav = [[RNNStackController alloc] initWithRootViewController:vc];
  121. [vc viewWillAppear:false];
  122. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  123. [self.uut setReadyToReceiveCommands:true];
  124. NSDictionary* dictFromJs = @{@"topBar": @{@"title" : @{@"text" : @"new title"}}};
  125. [self.uut mergeOptions:@"componentId" options:dictFromJs completion:^{
  126. XCTAssertTrue([vc.navigationItem.title isEqual:@"new title"]);
  127. }];
  128. }
  129. - (void)testShowOverlay_createLayout {
  130. [self.uut setReadyToReceiveCommands:true];
  131. OCMStub([self.overlayManager showOverlayWindow:[OCMArg any]]);
  132. NSDictionary* layout = @{};
  133. [[self.controllerFactory expect] createLayout:layout];
  134. [self.uut showOverlay:layout commandId:@"" completion:^{}];
  135. [self.controllerFactory verify];
  136. }
  137. - (void)testShowOverlay_saveToStore {
  138. [self.uut setReadyToReceiveCommands:true];
  139. OCMStub([self.overlayManager showOverlayWindow:[OCMArg any]]);
  140. OCMStub([self.controllerFactory createLayout:[OCMArg any]]);
  141. [[self.controllerFactory expect] createLayout:[OCMArg any]];
  142. [self.uut showOverlay:@{} commandId:@"" completion:^{}];
  143. [self.overlayManager verify];
  144. }
  145. - (void)testShowOverlay_withCreatedLayout {
  146. [self.uut setReadyToReceiveCommands:true];
  147. UIViewController* layoutVC = self.generateComponent;
  148. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(layoutVC);
  149. [[self.overlayManager expect] showOverlayWindow:[OCMArg any]];
  150. [self.uut showOverlay:@{} commandId:@"" completion:^{}];
  151. [self.overlayManager verify];
  152. }
  153. - (void)testShowOverlay_invokeNavigationCommandEventWithLayout {
  154. [self.uut setReadyToReceiveCommands:true];
  155. OCMStub([self.overlayManager showOverlayWindow:[OCMArg any]]);
  156. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  157. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  158. NSDictionary* layout = @{};
  159. [[self.eventEmmiter expect] sendOnNavigationCommandCompletion:@"showOverlay" commandId:[OCMArg any] params:[OCMArg any]];
  160. [self.uut showOverlay:layout commandId:@"" completion:^{}];
  161. [self.eventEmmiter verify];
  162. }
  163. - (void)testDismissOverlay_findComponentFromLayoutManager {
  164. [self.uut setReadyToReceiveCommands:true];
  165. NSString* componentId = @"componentId";
  166. id classMock = OCMClassMock([RNNLayoutManager class]);
  167. [[classMock expect] findComponentForId:componentId];
  168. [self.uut dismissOverlay:componentId commandId:@"" completion:^{} rejection:^(NSString *code, NSString *message, NSError *error) {}];
  169. [classMock verify];
  170. }
  171. - (void)testDismissOverlay_dismissReturnedViewController {
  172. [self.uut setReadyToReceiveCommands:true];
  173. NSString* componentId = @"componentId";
  174. UIViewController* returnedView = [UIViewController new];
  175. id classMock = OCMClassMock([RNNLayoutManager class]);
  176. OCMStub(ClassMethod([classMock findComponentForId:componentId])).andReturn(returnedView);
  177. [[self.overlayManager expect] dismissOverlay:returnedView];
  178. [self.uut dismissOverlay:componentId commandId:@"" completion:^{} rejection:^(NSString *code, NSString *message, NSError *error) {}];
  179. [self.overlayManager verify];
  180. }
  181. - (void)testDismissOverlay_handleErrorIfNoOverlayExists {
  182. [self.uut setReadyToReceiveCommands:true];
  183. NSString* componentId = @"componentId";
  184. id errorHandlerMockClass = [OCMockObject mockForClass:[RNNErrorHandler class]];
  185. [[errorHandlerMockClass expect] reject:[OCMArg any] withErrorCode:1010 errorDescription:[OCMArg any]];
  186. [self.uut dismissOverlay:componentId commandId:@"" completion:[OCMArg any] rejection:[OCMArg any]];
  187. [errorHandlerMockClass verify];
  188. }
  189. - (void)testDismissOverlay_invokeNavigationCommandEvent {
  190. [self.uut setReadyToReceiveCommands:true];
  191. NSString* componentId = @"componentId";
  192. id classMock = OCMClassMock([RNNLayoutManager class]);
  193. OCMStub(ClassMethod([classMock findComponentForId:componentId])).andReturn([UIViewController new]);
  194. [[self.eventEmmiter expect] sendOnNavigationCommandCompletion:@"dismissOverlay" commandId:[OCMArg any] params:[OCMArg any]];
  195. [self.uut dismissOverlay:componentId commandId:@"" completion:^{
  196. } rejection:^(NSString *code, NSString *message, NSError *error) {}];
  197. [self.eventEmmiter verify];
  198. }
  199. - (void)testSetRoot_setRootViewControllerOnMainWindow {
  200. [self.uut setReadyToReceiveCommands:true];
  201. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(self.vc1);
  202. [[self.mainWindow expect] setRootViewController:self.vc1];
  203. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  204. [self.mainWindow verify];
  205. }
  206. - (void)testSetStackRoot_resetStackWithSingleComponent {
  207. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(@[self.vc2]);
  208. [self.uut setReadyToReceiveCommands:true];
  209. id classMock = OCMClassMock([RNNLayoutManager class]);
  210. OCMStub(ClassMethod([classMock findComponentForId:@"vc1"])).andReturn(_nvc);
  211. self.vc2.options.animations.setStackRoot.enable = [[Bool alloc] initWithBOOL:NO];
  212. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  213. } rejection:^(NSString *code, NSString *message, NSError *error) {
  214. }];
  215. XCTAssertEqual(_nvc.viewControllers.firstObject, self.vc2);
  216. XCTAssertEqual(_nvc.viewControllers.count, 1);
  217. }
  218. - (void)testSetStackRoot_setMultipleChildren {
  219. NSArray* newViewControllers = @[_vc1, _vc3];
  220. id classMock = OCMClassMock([RNNLayoutManager class]);
  221. OCMStub(ClassMethod([classMock findComponentForId:@"vc1"])).andReturn(_nvc);
  222. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(newViewControllers);
  223. [self.uut setReadyToReceiveCommands:true];
  224. _vc3.options.animations.setStackRoot.enable = [[Bool alloc] initWithBOOL:NO];
  225. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  226. } rejection:^(NSString *code, NSString *message, NSError *error) {
  227. }];
  228. XCTAssertTrue([_nvc.viewControllers isEqual:newViewControllers]);
  229. }
  230. - (void)testSetStackRoot_callRenderTreeAndWaitOnce {
  231. id vc1Mock = [OCMockObject partialMockForObject:_vc1];
  232. id vc2Mock = [OCMockObject partialMockForObject:_vc2];
  233. NSArray* newViewControllers = @[vc1Mock, vc2Mock];
  234. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(newViewControllers);
  235. [self.uut setReadyToReceiveCommands:true];
  236. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  237. } rejection:^(NSString *code, NSString *message, NSError *error) {
  238. }];
  239. [[vc1Mock expect] render];
  240. [[vc2Mock expect] render];
  241. }
  242. - (void)testSetStackRoot_waitForRender {
  243. _vc2.options.animations.setStackRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  244. id vc1Mock = OCMPartialMock(_vc1);
  245. id vc2Mock = OCMPartialMock(_vc2);
  246. NSArray* newViewControllers = @[vc1Mock, vc2Mock];
  247. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(newViewControllers);
  248. [self.uut setReadyToReceiveCommands:true];
  249. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  250. } rejection:^(NSString *code, NSString *message, NSError *error) {
  251. }];
  252. [[vc1Mock expect] render];
  253. [[vc2Mock expect] render];
  254. }
  255. - (void)testSetRoot_waitForRenderTrue {
  256. [self.uut setReadyToReceiveCommands:true];
  257. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  258. self.vc1.options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  259. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  260. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  261. [[mockedVC expect] render];
  262. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  263. [mockedVC verify];
  264. }
  265. - (void)testSetRoot_waitForRenderFalse {
  266. [self.uut setReadyToReceiveCommands:true];
  267. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  268. self.vc1.options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:NO];
  269. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  270. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  271. [[mockedVC expect] render];
  272. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  273. [mockedVC verify];
  274. }
  275. - (void)testSetRoot_withBottomTabsAttachModeTogether {
  276. [self.uut setReadyToReceiveCommands:true];
  277. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  278. options.bottomTabs.tabsAttachMode = [[BottomTabsAttachMode alloc] initWithValue:@"together"];
  279. options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  280. BottomTabsBaseAttacher* attacher = [[[BottomTabsAttachModeFactory alloc] initWithDefaultOptions:nil] fromOptions:options];
  281. RNNBottomTabsController* tabBarController = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:options defaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions] presenter:[RNNBasePresenter new] eventEmitter:_eventEmmiter childViewControllers:@[_vc1, _vc2] bottomTabsAttacher:attacher];
  282. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(tabBarController);
  283. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  284. XCTAssertTrue(_vc1.isViewLoaded);
  285. XCTAssertTrue(_vc2.isViewLoaded);
  286. }
  287. - (void)testSetRoot_withBottomTabsAttachModeOnSwitchToTab {
  288. [self.uut setReadyToReceiveCommands:true];
  289. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  290. options.bottomTabs.tabsAttachMode = [[BottomTabsAttachMode alloc] initWithValue:@"onSwitchToTab"];
  291. options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  292. BottomTabsBaseAttacher* attacher = [[[BottomTabsAttachModeFactory alloc] initWithDefaultOptions:nil] fromOptions:options];
  293. RNNBottomTabsController* tabBarController = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:options defaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions] presenter:[RNNBasePresenter new] eventEmitter:_eventEmmiter childViewControllers:@[_vc1, _vc2] bottomTabsAttacher:attacher];
  294. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(tabBarController);
  295. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  296. XCTAssertTrue(_vc1.isViewLoaded);
  297. XCTAssertFalse(_vc2.isViewLoaded);
  298. [tabBarController setSelectedIndex:1];
  299. XCTAssertTrue(_vc2.isViewLoaded);
  300. }
  301. - (void)testSetRoot_withBottomTabsAttachModeAfterInitialTab {
  302. [self.uut setReadyToReceiveCommands:true];
  303. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  304. options.bottomTabs.tabsAttachMode = [[BottomTabsAttachMode alloc] initWithValue:@"afterInitialTab"];
  305. options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  306. BottomTabsBaseAttacher* attacher = [[[BottomTabsAttachModeFactory alloc] initWithDefaultOptions:nil] fromOptions:options];
  307. RNNBottomTabsController* tabBarController = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:options defaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions] presenter:[RNNBasePresenter new] eventEmitter:_eventEmmiter childViewControllers:@[_vc1, _vc2] bottomTabsAttacher:attacher];
  308. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(tabBarController);
  309. [self.uut setRoot:@{} commandId:@"" completion:^{
  310. XCTAssertFalse(self->_vc2.isViewLoaded);
  311. }];
  312. XCTAssertTrue(_vc1.isViewLoaded);
  313. XCTAssertTrue(_vc2.isViewLoaded);
  314. }
  315. - (void)testShowModal_shouldShowAnimated {
  316. [self.uut setReadyToReceiveCommands:true];
  317. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  318. self.vc1.options.animations.showModal.enable = [[Bool alloc] initWithBOOL:YES];
  319. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  320. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  321. [[self.modalManager expect] showModal:mockedVC animated:YES hasCustomAnimation:NO completion:[OCMArg any]];
  322. [self.uut showModal:@{} commandId:@"showModal" completion:^(NSString *componentId) {
  323. }];
  324. [self.modalManager verify];
  325. }
  326. @end