|
| 1 | +/** |
| 2 | + * The examples provided by Facebook are for non-commercial testing and |
| 3 | + * evaluation purposes only. |
| 4 | + * |
| 5 | + * Facebook reserves all rights not expressly granted. |
| 6 | + * |
| 7 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 8 | + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | + * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL |
| 10 | + * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 11 | + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 12 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 13 | + */ |
| 14 | + |
| 15 | + |
| 16 | +#import <UIKit/UIKit.h> |
| 17 | +#import <XCTest/XCTest.h> |
| 18 | +#import "RCTSparseArray.h" |
| 19 | +#import "RCTUIManager.h" |
| 20 | +#import "UIView+React.h" |
| 21 | + |
| 22 | +@interface RCTUIManager (Testing) |
| 23 | + |
| 24 | +- (void)_manageChildren:(NSNumber *)containerReactTag |
| 25 | + moveFromIndices:(NSArray *)moveFromIndices |
| 26 | + moveToIndices:(NSArray *)moveToIndices |
| 27 | + addChildReactTags:(NSArray *)addChildReactTags |
| 28 | + addAtIndices:(NSArray *)addAtIndices |
| 29 | + removeAtIndices:(NSArray *)removeAtIndices |
| 30 | + registry:(RCTSparseArray *)registry; |
| 31 | + |
| 32 | +@property (nonatomic, readonly) RCTSparseArray *viewRegistry; |
| 33 | + |
| 34 | +@end |
| 35 | + |
| 36 | +@interface RCTUIManagerScenarioTests : XCTestCase |
| 37 | + |
| 38 | +@property (nonatomic, readwrite, strong) RCTUIManager *uiManager; |
| 39 | + |
| 40 | +@end |
| 41 | + |
| 42 | +@implementation RCTUIManagerScenarioTests |
| 43 | + |
| 44 | +- (void)setUp |
| 45 | +{ |
| 46 | + [super setUp]; |
| 47 | + |
| 48 | + _uiManager = [[RCTUIManager alloc] init]; |
| 49 | + |
| 50 | + // Register 20 views to use in the tests |
| 51 | + for (NSInteger i = 1; i <= 20; i++) { |
| 52 | + UIView *registeredView = [[UIView alloc] init]; |
| 53 | + [registeredView setReactTag:@(i)]; |
| 54 | + _uiManager.viewRegistry[i] = registeredView; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +- (void)testManagingChildrenToAddViews |
| 59 | +{ |
| 60 | + UIView *containerView = _uiManager.viewRegistry[20]; |
| 61 | + NSMutableArray *addedViews = [NSMutableArray array]; |
| 62 | + |
| 63 | + NSArray *tagsToAdd = @[@1, @2, @3, @4, @5]; |
| 64 | + NSArray *addAtIndices = @[@0, @1, @2, @3, @4]; |
| 65 | + for (NSNumber *tag in tagsToAdd) { |
| 66 | + [addedViews addObject:_uiManager.viewRegistry[tag]]; |
| 67 | + } |
| 68 | + |
| 69 | + // Add views 1-5 to view 20 |
| 70 | + [_uiManager _manageChildren:@20 |
| 71 | + moveFromIndices:nil |
| 72 | + moveToIndices:nil |
| 73 | + addChildReactTags:tagsToAdd |
| 74 | + addAtIndices:addAtIndices |
| 75 | + removeAtIndices:nil |
| 76 | + registry:_uiManager.viewRegistry]; |
| 77 | + |
| 78 | + XCTAssertTrue([[containerView reactSubviews] count] == 5, |
| 79 | + @"Expect to have 5 react subviews after calling manage children \ |
| 80 | + with 5 tags to add, instead have %lu", (unsigned long)[[containerView reactSubviews] count]); |
| 81 | + for (UIView *view in addedViews) { |
| 82 | + XCTAssertTrue([view superview] == containerView, |
| 83 | + @"Expected to have manage children successfully add children"); |
| 84 | + [view removeFromSuperview]; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +- (void)testManagingChildrenToRemoveViews |
| 89 | +{ |
| 90 | + UIView *containerView = _uiManager.viewRegistry[20]; |
| 91 | + NSMutableArray *removedViews = [NSMutableArray array]; |
| 92 | + |
| 93 | + NSArray *removeAtIndices = @[@0, @4, @8, @12, @16]; |
| 94 | + for (NSNumber *index in removeAtIndices) { |
| 95 | + NSNumber *reactTag = @([index integerValue] + 2); |
| 96 | + [removedViews addObject:_uiManager.viewRegistry[reactTag]]; |
| 97 | + } |
| 98 | + for (NSInteger i = 2; i < 20; i++) { |
| 99 | + UIView *view = _uiManager.viewRegistry[i]; |
| 100 | + [containerView addSubview:view]; |
| 101 | + } |
| 102 | + |
| 103 | + // Remove views 1-5 from view 20 |
| 104 | + [_uiManager _manageChildren:@20 |
| 105 | + moveFromIndices:nil |
| 106 | + moveToIndices:nil |
| 107 | + addChildReactTags:nil |
| 108 | + addAtIndices:nil |
| 109 | + removeAtIndices:removeAtIndices |
| 110 | + registry:_uiManager.viewRegistry]; |
| 111 | + |
| 112 | + XCTAssertEqual(containerView.reactSubviews.count, (NSUInteger)13, |
| 113 | + @"Expect to have 13 react subviews after calling manage children\ |
| 114 | + with 5 tags to remove and 18 prior children, instead have %zd", |
| 115 | + containerView.reactSubviews.count); |
| 116 | + for (UIView *view in removedViews) { |
| 117 | + XCTAssertTrue([view superview] == nil, |
| 118 | + @"Expected to have manage children successfully remove children"); |
| 119 | + // After removing views are unregistered - we need to reregister |
| 120 | + _uiManager.viewRegistry[view.reactTag] = view; |
| 121 | + } |
| 122 | + for (NSInteger i = 2; i < 20; i++) { |
| 123 | + UIView *view = _uiManager.viewRegistry[i]; |
| 124 | + if (![removedViews containsObject:view]) { |
| 125 | + XCTAssertTrue([view superview] == containerView, |
| 126 | + @"Should not have removed view with react tag %ld during delete but did", (long)i); |
| 127 | + [view removeFromSuperview]; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// We want to start with views 1-10 added at indices 0-9 |
| 133 | +// Then we'll remove indices 2, 3, 5 and 8 |
| 134 | +// Add views 11 and 12 to indices 0 and 6 |
| 135 | +// And move indices 4 and 9 to 1 and 7 |
| 136 | +// So in total it goes from: |
| 137 | +// [1,2,3,4,5,6,7,8,9,10] |
| 138 | +// to |
| 139 | +// [11,5,1,2,7,8,12,10] |
| 140 | +- (void)testManagingChildrenToAddRemoveAndMove |
| 141 | +{ |
| 142 | + UIView *containerView = _uiManager.viewRegistry[20]; |
| 143 | + |
| 144 | + NSArray *removeAtIndices = @[@2, @3, @5, @8]; |
| 145 | + NSArray *addAtIndices = @[@0, @6]; |
| 146 | + NSArray *tagsToAdd = @[@11, @12]; |
| 147 | + NSArray *moveFromIndices = @[@4, @9]; |
| 148 | + NSArray *moveToIndices = @[@1, @7]; |
| 149 | + |
| 150 | + // We need to keep these in array to keep them around |
| 151 | + NSMutableArray *viewsToRemove = [NSMutableArray array]; |
| 152 | + for (NSUInteger i = 0; i < removeAtIndices.count; i++) { |
| 153 | + NSNumber *reactTagToRemove = @([removeAtIndices[i] integerValue] + 1); |
| 154 | + UIView *viewToRemove = _uiManager.viewRegistry[reactTagToRemove]; |
| 155 | + [viewsToRemove addObject:viewToRemove]; |
| 156 | + } |
| 157 | + |
| 158 | + for (NSInteger i = 1; i < 11; i++) { |
| 159 | + UIView *view = _uiManager.viewRegistry[i]; |
| 160 | + [containerView addSubview:view]; |
| 161 | + } |
| 162 | + |
| 163 | + [_uiManager _manageChildren:@20 |
| 164 | + moveFromIndices:moveFromIndices |
| 165 | + moveToIndices:moveToIndices |
| 166 | + addChildReactTags:tagsToAdd |
| 167 | + addAtIndices:addAtIndices |
| 168 | + removeAtIndices:removeAtIndices |
| 169 | + registry:_uiManager.viewRegistry]; |
| 170 | + |
| 171 | + XCTAssertTrue([[containerView reactSubviews] count] == 8, |
| 172 | + @"Expect to have 8 react subviews after calling manage children,\ |
| 173 | + instead have the following subviews %@", [containerView reactSubviews]); |
| 174 | + |
| 175 | + NSArray *expectedReactTags = @[@11, @5, @1, @2, @7, @8, @12, @10]; |
| 176 | + for (NSUInteger i = 0; i < containerView.subviews.count; i++) { |
| 177 | + XCTAssertEqualObjects([[containerView reactSubviews][i] reactTag], expectedReactTags[i], |
| 178 | + @"Expected subview at index %ld to have react tag #%@ but has tag #%@", |
| 179 | + (long)i, expectedReactTags[i], [[containerView reactSubviews][i] reactTag]); |
| 180 | + } |
| 181 | + |
| 182 | + // Clean up after ourselves |
| 183 | + for (NSInteger i = 1; i < 13; i++) { |
| 184 | + UIView *view = _uiManager.viewRegistry[i]; |
| 185 | + [view removeFromSuperview]; |
| 186 | + } |
| 187 | + for (UIView *view in viewsToRemove) { |
| 188 | + _uiManager.viewRegistry[view.reactTag] = view; |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +@end |
0 commit comments