iOS unit test: How to set/update/examine firstResponder?

无人久伴 提交于 2019-11-30 04:51:34

I guess that managing/changing the first responder chain is somehow accomplished in the main loop, when the UI is updated preparing for the next event handling. If this hypothesis is correct, I would simply do the following:

-(void)assertIfNotFirstResponder:(UITextField*)field {
    STAssertTrue([field isFirstResponder], nil);
}

- (void)testFirstResponder
{
     [controller view];
     [[controller firstTextField] becomeFirstResponder];
     [self performSelector:@selector(@"assertIfNotFirstResponder:") withObject:[controller firstTextField] afterDelay:0.0];
 }

Note: I have used a 0.0 delay because I simply want that the message is put on the event queue and dispatched as soon as possible. I need just a way to get back to the main loop, for its housekeeping. This should produce no actual delay in your case. If you are executing several tests of the same kind, i.e. by repeatedly changing the control that is the first responder, this technique should guarantee that all of those events correctly ordered with the ones generated by performSelector.

If you are running your tests from a different thread, you could use – performSelectorOnMainThread:withObject:waitUntilDone:

Using Xcode 5.1 and XCTestCase, this seems to work okay:

- (void)testFirstResponder
{      
  // Make sure the controller's view has a window
  UIWindow *window = [[UIWindow alloc] init];
  [window addSubview:controller.view];

  // Call whatever method you're testing
  [controller.textView becomeFirstResponder];

  // Assert that the desired subview is the first responder
  XCTAssertTrue([sut.textView isFirstResponder]);
}

In order for a view/subview to become first responder, it must be part of a view hierarchy, meaning that its root view's window property must be set.

Jon and Sergio mention that you may need to call [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]] after calling becomeFirstResponderon your desired subview, but I found that this wasn't required in our instance.

However, your mileage may vary (even depending on the version of Xcode you're using), so you may or may not need to include such call.

You need to ensure the textField is installed in the view hierarchy.

If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy.

Hopefully this helps....

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!