diff --git a/Tests/AppKit/CPViewControllerTest.j b/Tests/AppKit/CPViewControllerTest.j index 098be199c..7918c8732 100644 --- a/Tests/AppKit/CPViewControllerTest.j +++ b/Tests/AppKit/CPViewControllerTest.j @@ -2,6 +2,7 @@ @import var methodsCalled; +var testResponderChainActionCalled; @implementation CPViewControllerTest : OJTestCase { @@ -56,6 +57,34 @@ var methodsCalled; [self assert:expectedResult equals:methodsCalled]; } +- (void)testResponderChain +{ + testResponderChainActionCalled = NO; + + // 1. Create the controller, its view, and a superview. + var viewController = [[ResponderTestViewController alloc] init]; + var view = [viewController view]; + var superview = [[CPView alloc] init]; + + // 2. Add the view to the view hierarchy. + [superview addSubview:view]; + + // 3. Assert the responder chain is correctly wired. + // The view's next responder should be its controller. + [self assert:viewController equals:[view nextResponder] message:@"The view controller should be the next responder of its view."]; + // The controller's next responder should be its view's superview. + [self assert:superview equals:[viewController nextResponder] message:@"The view's superview should be the next responder of the view controller."]; + + // 4. Test that an action sent to the view is handled by the controller. + var wasHandled = [view tryToPerform:@selector(testAction:) with:nil]; + [self assertTrue:wasHandled message:@"The action should be handled by the responder chain."]; + [self assertTrue:testResponderChainActionCalled message:@"The view controller's action method should have been called."]; + + // 5. Test that the chain unwires correctly when the view is removed. + [view removeFromSuperview]; + [self assert:nil equals:[viewController nextResponder] message:@"The next responder should be nil after the view is removed from its superview."]; +} + @end @implementation ViewController : CPViewController @@ -87,3 +116,14 @@ var methodsCalled; [methodsCalled addObject:_cmd]; } @end + +@implementation ResponderTestViewController : CPViewController +{ +} + +- (void)testAction:(id)sender +{ + testResponderChainActionCalled = YES; +} + +@end