Unit testing button click is not working - ios

I have added a button programmatically and I have to run unit test for automation process. We dont have much UI components so we dont use UI testing bundle.
Added button in code
self.proceedButton.frame = CGRectMake(0.0, 0.0, (buttonContainerWidth * 0.75), (buttonContainerHeight * 0.3));
self.proceedButton.center = CGPointMake(self.buttonContainer.center.x, CGRectGetHeight(self.proceedButton.frame));
self.proceedButton.layer.cornerRadius = CGRectGetHeight(self.proceedButton.frame) / 2;
self.proceedButton.layer.masksToBounds = YES;
self.proceedButton.titleLabel.font = proceedFont;
[self.proceedButton addTarget:self action:#selector(onAcceptClicked) forControlEvents:UIControlEventTouchUpInside];
Test:
[vc viewDidLoad];
NSString *selector = [[vc.proceedButton actionsForTarget:vc forControlEvent:UIControlEventTouchUpInside] firstObject];
XCTAssert([selector isEqualToString:#"onAcceptClicked"]);
[vc.proceedButton sendActionsForControlEvents: UIControlEventTouchUpInside];
This fails if I comment out the [self.proceedButton addTarget:self action:#selector(onAcceptClicked) forControlEvents:UIControlEventTouchUpInside]; line so it seems the test is written correctly.
However sendActionsForControlEvents: is not entering onAcceptClicked method in the test.
Why this is happening? Is it possible that unit test is finishing before onAcceptClicked getting actually called?

This could be because you are calling [vc viewDidLoad] directly, which Apple advises you not to do, presumably because a bunch of setup is performed before viewDidLoad is eventually invoked.
You should never call this method directly. The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property.
Instead, try using XCTAssertNotNil(vc.view); at the beginning. This will try to load the view, eventually calling viewDidLoad for you and probably setting up your button so that the actions are correctly added to it.
Edit So if it helps, I've just quickly run this test and I can verify that the method onAcceptClicked is called in the view controller when the test runs.
Button setup in viewDidLoad
CGRect frame = CGRectMake(50, 40, 30, 30);
self.proceedButton = [[UIButton alloc] initWithFrame:frame];
self.proceedButton.titleLabel.text = #"button!!!!";
[self.proceedButton addTarget:self action:#selector(onAcceptClicked) forControlEvents:UIControlEventTouchUpInside];
self.proceedButton.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.proceedButton];
Test
-(void)testButton {
self.myVC = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"MyVC"];
XCTAssertNotNil(self.myVC.view);
NSString *selector = [[self.myVC.proceedButton actionsForTarget:self.myVC forControlEvent:UIControlEventTouchUpInside] firstObject];
XCTAssert([selector isEqualToString:#"onAcceptClicked"]);
[self.myVC.proceedButton sendActionsForControlEvents: UIControlEventTouchUpInside];
}

Related

iOS unit testing button has action assigned

I've got the following button written programmatically:
self.button = [[UIButton alloc] init];
[self.button setTitle:#"Reverse String!" forState:UIControlStateNormal];
self.button.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
self.button.layer.cornerRadius = 5.0;
[self.button addTarget:self action:#selector(reverseString:) forControlEvents:UIControlEventTouchUpInside];
However, my unit test fails when I try to test:
-(void)testButtonActionAssigned
{
XCTAssert([self.vc.button respondsToSelector:#selector(reverseString:)]);
}
I get a failed message saying:
test failure: -[ViewControllerTests testButtonActionAssigned] failed:
(([self.vc.button respondsToSelector:#selector(reverseString:)]) is
true) failed
My setup method is called:
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
self.vc = [[ViewController alloc] init];
NSLog(#"THIS METHOD IS CALLED");
}
Is it to do with the simulator timing and startup ?
[self.button addTarget:self action:#selector(reverseString:) forControlEvents:UIControlEventTouchUpInside];
First line means that reverseString: method should be in your ViewController (which is specified by target: parameter and you pass self there). So you need to change test with just removing .button
-(void)testButtonActionAssigned
{
XCTAssert([self.vc respondsToSelector:#selector(reverseString:)]);
}
Here is small example which can be changed:
UIButton *button = [UIButton new];
[button addTarget:self action:#selector(doThat:) forControlEvents:UIControlEventTouchUpInside];
NSString *selectorString = [[button actionsForTarget:self forControlEvent:UIControlEventTouchUpInside] firstObject];
SEL sel = NSSelectorFromString(selectorString);
// so now you can check if target responds to selector
// BOOL responds = [self respondsToSelector:sel];
NSLog(#"%#", selectorString);
I got it. I was testing the wrong thing.
The code in the question isn't really testing the button has an action assigned. It's basically saying "does the view controller responds to this method" or "does the view controller have this method".
// this is saying "does the ViewController know about this method"
// rather than "is ViewController's button assigned method reverseString:"
XCTAssert([self.vc respondsToSelector:...]);
This means regardless of whether button has a selector assigned or not, as long as the method is defined, this assert is always true.
I verified it by commenting out the line of code:
[self.button addTarget:self action:#selector(reverseString:) forControlEvents:UIControlEventTouchUpInside];
and the test still passes, which isn't what I was trying to achieve.
The proper test should be:
-(void)testButtonActionAssigned
{
[self.vc viewDidLoad];
NSArray *arrSelectors = [self.vc.button actionsForTarget:self.vc forControlEvent:UIControlEventTouchUpInside];
NSString *selector = nil;
if(arrSelectors.count > 0)
{
selector = arrSelectors[0];
}
XCTAssert([selector isEqualToString:#"reverseString:"]);
}
Now after commenting out the line:
[self.button addTarget:self action:#selector(reverseString:) forControlEvents:UIControlEventTouchUpInside];
The test fails as expected :D
This proper test code fetches the selector from the button and checks to see if it indeed is the method called reverseString:
EDIT
After B.S. answer, and confirming with Apple's documentation, a shorter version can be written as:
-(void)testButtonActionAssigned
{
[self.vc viewDidLoad];
NSString *selector = [[self.vc.button actionsForTarget:self.vc forControlEvent:UIControlEventTouchUpInside] firstObject];
XCTAssert([selector isEqualToString:#"reverseString:"]);
}
I was originally worried that calling firstObject on an empty array would cause an app to crash but Apple's documentation has confirmed to me that firstObject is a property that returns nil if array is empty.
The first object in the array. (read-only)
Declaration #property(nonatomic, readonly) ObjectType firstObject
Discussion If the array is empty, returns nil.
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/#//apple_ref/occ/instp/NSArray/firstObject

Reusing variables in other method file

I have the following code to generate a UIBUtton from a method file called FirstViewController, since the location of the button will change in different secondviewController or thirdViewController, is it possible to set a variable for the location (CGRect) of the unbutton in the FirstViewController and change the CGRect Value in the second or third viewController?
In FirstViewController.m
-(void)method:(UIView *)_view {
UIButton*Touch1= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[Touch1 addTarget:self action:#selector(TouchButton1:) forControlEvents:UIControlEventTouchUpInside];
[Touch1 setFrame:CGRectMake(50,50, 100, 100)];
**//I want to set a variable for the CGRectMake**
Touch1.translatesAutoresizingMaskIntoConstraints = YES;
[Touch1 setBackgroundImage:[UIImage imageNamed:#"1.png"] forState:UIControlStateNormal];
[Touch1 setExclusiveTouch:YES];
[_view addSubview:Touch1];
NSLog(#"test ");
}
In SecondViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
ViewController * ViewCon = [[ViewController alloc]init];
**//Change variable here by defining the new CGRect here.**
[ViewCon method:self.view];
}
This pattern is incorrect:
ViewController * ViewCon = [[ViewController alloc]init];
[ViewCon method:self.view];
as you are allocating a new view controller just to use one of its the method and then you are throwing the view controller instance away. It's extremely inefficient and inconvenient.
Either move the method to a utility class, as a class method:
[Utils method:self.view rect:rect];
or subclass UIViewController and implement the method in that base class and then derive all similar view controllers from that base class, passing any variables into it.
[self method:rect]; // method implemented in MyBaseViewController
You also asked this question before and accepted an answer that promotes the use of this bad pattern. That will mislead others into using this bad pattern.

moving between 2 UIViewControllers each with 1 UIView - XCode5 osx

I started writing a project without really using the storyboard (as i was following a tutorial about creating an options menu for a game).
So i am trying to retroactively implement a way of moving between 2 UIViewControllers (MainGame and MatchScreen) that each has a UIView (MainMenu and VSComputer respectively).
Now in the MainMenu UIView I have created 4 buttons, with all their positions and settings, two of which are subviews - options and stats - that so appear and disappear when clicked on the current ViewController.
Now, the problem is, I need to somehow move from the main menu to the match screen using one of the other buttons named 'matchScreen'. I feel there must be something that i'm missing as no answer found in my research has worked for me yet.
I have embedded my main UIViewController with a navController (as suggested in other questions that i've seen) and then added a push segue to the 2nd ViewController. But i don't know how to make this apparent to my code. My code recognises nothing that i do in the storyboard it seems.
#interface JDTHMainMenu(){
JDTHOptionsScreen *theOptionScreen;
JDTHPlayerStats *thePlayerStatsScreen;
UIButton *optionScreenButton;
UIButton *matchScreenButton;
UIButton *deckScreenButton;
UIButton *statsScreenButton;
bool isPhone;
bool isOptionScreenOpen;
bool isPlayerStatsScreenOpen;
bool matchScreenButtonPressed;
UIView *tint;
int screenWidth;
int screenHeight;
AppData *appData;
}
So to be clear, JDTHMainMenu is a UIView in the JDTHViewController - the original, JDTHMatchScreen is the second ViewController and there is a UIView called JDTHVersusScreen. Im trying to go from one to the other. Also the JDTHAppDelegate only has return YES in the didFinishLaunching method. I feel i'm getting closer....but perhaps not...
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
appData = [AppData sharedData];
NSLog(#"Main Menu has loaded");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
isPhone = YES;
} else {
isPhone = NO;
}
screenWidth = self.frame.size.width;
screenHeight = self.frame.size.height;
isOptionScreenOpen = NO;
isPlayerStatsScreenOpen = NO;
matchScreenButtonPressed = NO;
[self showOptionScreenButton];
[self showPlayerStatsScreenButton];
[self matchViewControllerButton:matchScreenButton];
[self deckViewerControllerButton:deckScreenButton];
}
return self;
}
-(IBAction)matchViewControllerButton:(UIButton *)sender{
CGRect rect;
UIFont *theFont;
if (isPhone == YES) {
rect = CGRectMake(38, 150, 134, 116);
theFont = [UIFont fontWithName:#"Zapfino" size:22];
} else {
rect = CGRectMake(275, 600, 200, 150);
theFont = [UIFont fontWithName:#"Zapfino" size:28];
}
matchScreenButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[matchScreenButton addTarget:self action:#selector(goToMatchView:sender:) forControlEvents:UIControlEventTouchDown];
[matchScreenButton.titleLabel setFont:theFont];
[matchScreenButton setTitle:#"VS Comp" forState:UIControlStateNormal];
matchScreenButton.frame = rect;
[matchScreenButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[matchScreenButton setBackgroundImage:[UIImage imageNamed:#"MainMenuOptionButton"] forState:UIControlStateNormal];
[self addSubview:matchScreenButton];
}
-(void)goToMatchView:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"pushToMatchView"]) {
JDTHMatchScreen *vc2 = (JDTHMatchScreen *)segue.destinationViewController;
vc2.name = self.textField.text;
}
}
A storyboard app has an entry in the info.plist file that tells the app that it's a storyboard app, and what the name of the storyboard is ("Main storyboard file base name" is the name of the key, and the value is the file name, without extension, of your storyboard). You would need to add this to your project to get it to recognize the storyboard. Also, non-storyboard apps usually have code in the app delegate to set up the initial controllers. This should be deleted for a storyboard app (the only code in the default template in didFinishLaunchingWithOptions: is "return YES"). It might be easier to just start a new storyboard based single view project and copy your files to it.

Adding views to the current viewcontroller from another class

I have a UIViewController named MainViewController. I have an another class named ViewMaker.
In the ViewMaker class I have a function as follows.
-(UIView *)GetContentView
{
UIView *return_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
UIButton *done_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
done_button.frame = CGRectMake(300, 300, 100, 50);
[done_button setTitle:#"Done" forState:UIControlStateNormal];
[return_view addSubview:done_button];
return return_view;
}
In the above function actually I will add more views like textviews, labels etc.
In the MainViewController class I am calling the above method as follows.
-(void)CreateViews
{
UIView *content_view = [[[ViewMaker alloc] init] GetContentView];
[self.view addSubview:content_view];
}
Now I have added a view with done button(and other components like textviews, labels etc) from the MainViewController class.
My problem is about adding a target function for the done button. When I click the done button I want to perform some actions(like add some views) in the MainViewController according to the datas in the view that was returned from the ViewMaker class.
How can I do this?
Thanks in advance.
I assume your ViewMaker class is same as PopupContentMaker,
add this to your done button:
[done_button addTarget:self action:#selector(doSomething) forControlEvents:UIControlEventTouchUpInside];
in PopupContenMaker:
- (void)doSomething{
if ([self.delegate respondsToSelector:#selector(handleDoneButtonTap)]) {
[self.delegate performSelector:#selector(handleDoneButtonTap) withObject:self];
}
}
declare a delegate property in your PopupContentMaker, and make MainViewController the delegate,
-(void)GetContentView
{
PopContentMaker *pcm = [[PopupContentMaker alloc] init];
pcm.delegate = self;
[self.view addSubview:[pcm GetContentView]];
}
-(void)handleDoneButtonTap{
//Do action after done button
}
You can try:
[done_button addTarget: self
action: #selector(buttonClickedMethod)
forControlEvents: UIControlEventTouchUpInside];
In this way you can call a custom method when button is pressed.
You can pass the target and action as parameters to the getContentView method or you can use the delegate pattern.

UIView and UIViewController

I know this is really basic stuff but i need to understand whether my understanding of this is correct.
So what i want to do is this. I want an view with a label on which when double tapped flips and loads another view. On the second view i want a UIPickerView and above i have a button saying back. Both views will be of same size as an UIPickerView which is 320px x 216px.
What i am thinking of to do is create two UIViewclasses named labelView and pickerView. I would then create a viewController which on loadView loads labelView then when user double taps the labelView i get an event in labelView class which is sent to my viewController that then can unload loadView and load the pickerView.
Does this sound as the best way to do this ? Is there a simpler way ? I am also unsure how i route the event from the labelView class to the viewControllerclass.
I dont exactly know the most efficient way to do it(as i am also now to this language),but it is for sure that i have solved ur problem. I made a simple program for that.Three classes involved here in my eg are BaseViewController (which will show two views),LabelView and PickerView (according to ur requirement).
In LabelView.h
#protocol LabelViewDelegate
-(void)didTapTwiceLabelView;
#end
#interface LabelView : UIView {
id <LabelViewDelegate> delegate;
}
#property(nonatomic,retain)id <LabelViewDelegate> delegate;
-(void)didTouch;
#end
In LabelView.m
#synthesize delegate;
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self)
{
UILabel* labl = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, frame.size.width-20,20)];
labl.text = #"Some Text";
[self addSubview:labl];
[labl release]; labl = nil;
self.backgroundColor = [UIColor grayColor];
UITapGestureRecognizer* ges = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTouch)] autorelease];
ges.numberOfTapsRequired = 2;
[self addGestureRecognizer:ges];
}
return self;
}
-(void)didTouch
{
[delegate didTapTwiceLabelView];
}
//=============================================================
In Pickerview.h
#protocol PickerViewDelegate
-(void)didTapBackButton;
#end
#interface PickerView : UIView <UIPickerViewDelegate,UIPickerViewDataSource>{
id <PickerViewDelegate> delegate;
}
#property(nonatomic,retain)id <PickerViewDelegate> delegate;
#end
In Pickerview.m
#implementation PickerView
#synthesize delegate;
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self)
{
UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 30, 320, 216)];
picker.delegate = self;
picker.dataSource = self;
[self addSubview:picker];
[picker release]; picker = nil;
self.frame = CGRectMake(frame.origin.x, frame.origin.y, 320, 250);
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10, 1, 50, 27)];
[btn setTitle:#"Back" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(backButton) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
return self;
}
-(void)backButton
{
[delegate didTapBackButton];
}
//====================================================================
in BaseViewController.h
#import "LabelView.h"
#import "PickerView.h"
#interface VarticalLabel : UIViewController<UITextFieldDelegate,PickerViewDelegate,LabelViewDelegate> {
PickerView* myPickerView;
LabelView* myLabelView;
}
#end
In BaseViewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
myPickerView= [[PickerView alloc] initWithFrame:CGRectMake(0, 50, 320, 250)];
[self.view addSubview:myPickerView];
myPickerView.delegate = self;
myLabelView= [[LabelView alloc] initWithFrame:CGRectMake(0, 50, 320, 250)];
[self.view addSubview:myLabelView];
myLabelView.delegate = self;
myPickerView.hidden = YES;
}
#pragma mark PickerViewDelgate
-(void)didTapBackButton
{
myPickerView.hidden = YES;
myLabelView.hidden = NO;
}
#pragma mark LabelViewDelegate
-(void)didTapTwiceLabelView
{
myPickerView.hidden = NO;
myLabelView.hidden = YES;
}
To get events from a button to the view controller, just hook up the button's event, e.g. touch up inside, to a method in the view controller, using interface builder. (Double tapping is probably more complicated though.)
When you say 'flips', do you mean it actually shows an animation of flipping over a view to show a 'reverse' side? Like in the weather app when you hit the 'i' button? I'm assuming this is what you mean.
Perhaps check TheElements sample example on the iPhone Reference Library, it has an example of flip animation.
Btw, it's not strictly necessary to unload the loadView that is being 'hidden' when you flip -- it saves you having to construct it again when you flip back -- but it may be pertinent if you have memory use concerns, and/or the system warns you about memory being low.
Also, what do you mean by "create a UIView"? Do you mean subclass UIView, or just instantiate a UIVIew and add children view objects to it? The latter is the usual strategy. Don't subclass UIView just because you want to add some things to a UIView.
If you've got one screen of information that gives way to another screen of information, you'd normally make them separate view controllers. So in your case you'd have one view controller with the label and upon receiving the input you want, you'd switch to the view controller composed of the UIPickerView and the button.
Supposing you use Interface Builder, you would probably have a top level XIB (which the normal project templates will have provided) that defines the app delegate and contains a reference to the initial view controller in a separate XIB (also supplied). In the separate XIB you'd probably want to add another view controller by reference (so, put it in, give it the class name but indicate that its description is contained in another file) and in that view controller put in the picker view and the button.
The point of loadView, as separate from the normal class init, is to facilitate naming and linking to an instance in one XIB while having the layout defined in another. View controllers are alloced and inited when something that has a reference to them is alloced and inited. But the view is only loaded when it is going to be presented, and may be unloaded and reloaded while the app is running (though not while it is showing). Generally speaking, views will be loaded when needed and unnecessary views will be unloaded upon a low memory warning. That's all automatic, even if you don't put anything in the XIBs and just create a view programmatically within loadView or as a result of viewDidLoad.
I've made that all sound more complicated than your solution, but it's actually simpler because of the amount you can do in Interface Builder, once you're past the curve of learning it. It may actually be worth jumping straight to the Xcode 4 beta, as it shakes things up quite a lot in this area and sites have reported that a gold master was seeded at one point, so is likely to become the official thing very soon.
With respect to catching the double tap, the easiest thing is a UITapGestureRecognizer (see here). You'd do something like:
// create a tap gesture recogniser, tell it to send events to this instance
// of this class, and to send them via the 'handleGesture:' message, which
// we'll implement below...
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleGesture:)];
// we want double taps
tapGestureRecognizer.numberOfTapsRequired = 2;
// attach the gesture recogniser to the view we want to catch taps on
[labelView addGestureRecognizer:tapGestureRecognizer];
// we have an owning reference to the recogniser but have now given it to
// the label. We don't intend to talk to it again without being prompted,
// so should relinquish ownership
[tapGestureRecognizer release];
/* ... elsewhere ... */
// the method we've nominated to receive gesture events
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
// could check 'gestureRecognizer' against tapGestureRecognizer above if
// we set the same message for multiple recognisers
// just make sure we're getting this because the gesture occurred
if(gestureRecognizer.state == UIGestureRecognizerStateRecognized)
{
// do something to present the other view
}
}
Gesture recognisers are available as of iOS 3.2 (which was for iPad only; so iOS 4.0 on iPhone and iPod Touch).

Resources