views:

37

answers:

1

My class has logic for inserting views in certain levels in the hierarchy.

containerView = [[UIView alloc] init];
shadowView = [[UIView alloc] init];
contentView = [[UIView alloc init];
[containerView addSubview:shadowView];
[containerView insertSubview:contentView belowSubview:shadowView];

Later down the line, it flips them so the shadow view is below the content view instead of above it.

// "Flipping" their positions.
[containerView insertSubview:contentView aboveSubview:shadowView];

UIView has a subviews property returning an NSArray, unfortunately the array does not reflect the view stack ordering.

I want to unit test the placement of the view compared to it's siblings. How can I do that?

Here's an example of a test.

- (void)testViewHierarchyFlipping {
   STAssertEquals(containerView, shadowView.superview, nil);
   STAssertEquals(containerView, contentView.superview, nil);
   // Test that shadowView is ABOVE contentView.
}
A: 

You can inspect the containerView.subviews property to determine the Z position of each subview.

Items at the beginning of the array are deeper than items at the end of the array.

I believe this should work for your test, although I don't have access to Xcode to verify that it's correct at the moment.

- (void)testViewHierarchyFlipping {
   STAssertEquals(containerView, shadowView.superview, nil);
   STAssertEquals(containerView, contentView.superview, nil);

   // Test that shadowView is ABOVE contentView.
   UIView *topView = (UIView *)[[containerView subviews] lastObject];
   UIView *bottomView = (UIView *)[[containerView subviews] objectAtIndex:0];
   STAssertEquals(shadowView, topView, nil);
   STAssertEquals(contentView, bottomView, nil);
}
GregInYEG