react-native-navigation的迁移库

RNNCommandsHandlerTest.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #import <XCTest/XCTest.h>
  2. #import <objc/runtime.h>
  3. #import "RNNCommandsHandler.h"
  4. #import "RNNNavigationOptions.h"
  5. #import "RNNTestRootViewCreator.h"
  6. #import "RNNComponentViewController.h"
  7. #import "RNNStackController.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 : RNNStackController
  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) RNNComponentViewController* vc1;
  34. @property (nonatomic, strong) RNNComponentViewController* vc2;
  35. @property (nonatomic, strong) RNNComponentViewController* 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 = [RNNComponentViewController new];
  53. self.vc2 = [RNNComponentViewController new];
  54. self.vc3 = [RNNComponentViewController 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. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] init];
  98. RNNComponentViewController* vc = [[RNNComponentViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:creator eventEmitter:nil presenter:presenter options:initialOptions defaultOptions:nil];
  99. RNNStackController* nav = [[RNNStackController alloc] initWithLayoutInfo:nil creator:creator options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:nil presenter:[[RNNStackPresenter 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. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] init];
  114. RNNComponentViewController* vc = [[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:[[RNNTestRootViewCreator alloc] init] eventEmitter:nil presenter:presenter options:initialOptions defaultOptions:nil];
  115. __unused RNNStackController* nav = [[RNNStackController 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 = [RNNComponentViewController 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 = [OCMockObject partialMockForObject:_vc1];
  225. id vc2Mock = [OCMockObject partialMockForObject:_vc2];
  226. NSArray* newViewControllers = @[vc1Mock, vc2Mock];
  227. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(newViewControllers);
  228. [self.uut setReadyToReceiveCommands:true];
  229. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  230. } rejection:^(NSString *code, NSString *message, NSError *error) {
  231. }];
  232. [[vc1Mock expect] renderTreeAndWait:NO perform:[OCMArg any]];
  233. [[vc2Mock expect] renderTreeAndWait:NO perform:[OCMArg any]];
  234. }
  235. - (void)testSetStackRoot_waitForRender {
  236. _vc2.options.animations.setStackRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  237. id vc1Mock = OCMPartialMock(_vc1);
  238. id vc2Mock = OCMPartialMock(_vc2);
  239. NSArray* newViewControllers = @[vc1Mock, vc2Mock];
  240. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(newViewControllers);
  241. [self.uut setReadyToReceiveCommands:true];
  242. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  243. } rejection:^(NSString *code, NSString *message, NSError *error) {
  244. }];
  245. [[vc1Mock expect] renderTreeAndWait:NO perform:[OCMArg any]];
  246. [[vc2Mock expect] renderTreeAndWait:YES perform:[OCMArg any]];
  247. }
  248. - (void)testSetRoot_waitForRenderTrue {
  249. [self.uut setReadyToReceiveCommands:true];
  250. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  251. self.vc1.options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  252. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  253. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  254. [[mockedVC expect] renderTreeAndWait:YES perform:[OCMArg any]];
  255. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  256. [mockedVC verify];
  257. }
  258. - (void)testSetRoot_waitForRenderFalse {
  259. [self.uut setReadyToReceiveCommands:true];
  260. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  261. self.vc1.options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:NO];
  262. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  263. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  264. [[mockedVC expect] renderTreeAndWait:NO perform:[OCMArg any]];
  265. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  266. [mockedVC verify];
  267. }
  268. - (void)testShowModal_shouldShowAnimated {
  269. [self.uut setReadyToReceiveCommands:true];
  270. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  271. self.vc1.options.animations.showModal.enable = [[Bool alloc] initWithBOOL:YES];
  272. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  273. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  274. [[self.modalManager expect] showModal:mockedVC animated:YES hasCustomAnimation:NO completion:[OCMArg any]];
  275. [self.uut showModal:@{} commandId:@"showModal" completion:^(NSString *componentId) {
  276. }];
  277. [self.modalManager verify];
  278. }
  279. @end