react-native-navigation的迁移库

RNNCommandsHandlerTest.m 14KB

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