react-native-navigation的迁移库

RNNCommandsHandlerTest.m 12KB

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