react-native-navigation的迁移库

RNNCommandsHandlerTest.m 13KB

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