react-native-navigation的迁移库

RNNCommandsHandlerTest.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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)testSetStackRoot_callRenderTreeAndWaitOnce {
  224. id vc1Mock = OCMPartialMock(_vc1);
  225. id vc2Mock = OCMPartialMock(_vc2);
  226. NSArray* newViewControllers = @[vc1Mock, vc2Mock];
  227. id classMock = OCMClassMock([RNNLayoutManager class]);
  228. OCMStub(ClassMethod([classMock findComponentForId:@"vc1"])).andReturn(_nvc);
  229. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(newViewControllers);
  230. [self.uut setReadyToReceiveCommands:true];
  231. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  232. } rejection:^(NSString *code, NSString *message, NSError *error) {
  233. }];
  234. [[vc1Mock expect] renderTreeAndWait:NO perform:[OCMArg any]];
  235. [[vc2Mock expect] renderTreeAndWait:NO perform:[OCMArg any]];
  236. }
  237. - (void)testSetStackRoot_waitForRender {
  238. _vc2.options.animations.setStackRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  239. id vc1Mock = OCMPartialMock(_vc1);
  240. id vc2Mock = OCMPartialMock(_vc2);
  241. OCMStub([vc2Mock renderTreeAndWait:YES perform:[OCMArg any]]);
  242. NSArray* newViewControllers = @[vc1Mock, vc2Mock];
  243. id classMock = OCMClassMock([RNNLayoutManager class]);
  244. OCMStub(ClassMethod([classMock findComponentForId:@"vc1"])).andReturn(_nvc);
  245. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(newViewControllers);
  246. [self.uut setReadyToReceiveCommands:true];
  247. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  248. } rejection:^(NSString *code, NSString *message, NSError *error) {
  249. }];
  250. [[vc1Mock expect] renderTreeAndWait:NO perform:[OCMArg any]];
  251. [[vc2Mock expect] renderTreeAndWait:YES perform:[OCMArg any]];
  252. }
  253. - (void)testSetRoot_waitForRenderTrue {
  254. [self.uut setReadyToReceiveCommands:true];
  255. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  256. self.vc1.options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  257. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  258. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  259. [[mockedVC expect] renderTreeAndWait:YES perform:[OCMArg any]];
  260. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  261. [mockedVC verify];
  262. }
  263. - (void)testSetRoot_waitForRenderFalse {
  264. [self.uut setReadyToReceiveCommands:true];
  265. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  266. self.vc1.options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:NO];
  267. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  268. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  269. [[mockedVC expect] renderTreeAndWait:NO perform:[OCMArg any]];
  270. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  271. [mockedVC verify];
  272. }
  273. - (void)testShowModal_shouldShowAnimated {
  274. [self.uut setReadyToReceiveCommands:true];
  275. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  276. self.vc1.options.animations.showModal.enable = [[Bool alloc] initWithBOOL:YES];
  277. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  278. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  279. [[self.modalManager expect] showModal:mockedVC animated:YES hasCustomAnimation:NO completion:[OCMArg any]];
  280. [self.uut showModal:@{} commandId:@"showModal" completion:^(NSString *componentId) {
  281. }];
  282. [self.modalManager verify];
  283. }
  284. @end