Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#import <OCMock/OCMock.h>
#import <XCTest/XCTest.h>
#import "HomeView.h"
#import "HomeViewController.h"
#import "HomeViewPresenter.h"
#import "UIImageHelper.h"
@interface HomeViewPresenterTest : XCTestCase {
id mockCameraButton_;
id mockGalleryButton_;
id mockHomeView_;
id mockImageHelper_;
id mockInspireButton_;
id mockViewController_;
HomeViewPresenter *presenter_;
}
@end
@implementation HomeViewPresenterTest
- (void)setUp {
[super setUp];
mockCameraButton_ = OCMStrictClassMock([UIButton class]);
mockGalleryButton_ = OCMStrictClassMock([UIButton class]);
mockHomeView_ = OCMStrictClassMock([HomeView class]);
mockImageHelper_ = OCMStrictClassMock([UIImageHelper class]);
mockInspireButton_ = OCMStrictClassMock([UIButton class]);
mockViewController_ = OCMStrictClassMock([HomeViewController class]);
OCMStub([mockViewController_ homeView]).andReturn(mockHomeView_);
presenter_ = [[HomeViewPresenter alloc] initWithImageHelper:mockImageHelper_];
[presenter_ setViewController:mockViewController_];
}
- (void)tearDown {
OCMVerifyAll(mockCameraButton_);
OCMVerifyAll(mockGalleryButton_);
OCMVerifyAll(mockHomeView_);
OCMVerifyAll(mockImageHelper_);
OCMVerifyAll(mockInspireButton_);
OCMVerifyAll(mockViewController_);
}
- (void)testCreateViewController {
HomeViewController *viewController = [HomeViewPresenter createViewController];
XCTAssertNotNil(viewController);
HomeViewPresenter *presenter = (HomeViewPresenter *) [viewController presenter];
XCTAssertNotNil(presenter);
}
- (void)testAddTargetsToButtonsCameraButtonDisabledNoCamera {
OCMStub([mockHomeView_ cameraButton]).andReturn(mockCameraButton_);
OCMStub([mockHomeView_ galleryButton]).andReturn(mockGalleryButton_);
OCMStub([mockHomeView_ inspireButton]).andReturn(mockInspireButton_);
OCMStub([mockImageHelper_ isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
.andReturn(NO);
OCMExpect([mockCameraButton_ setEnabled:NO]);
OCMExpect([mockGalleryButton_ addTarget:[OCMArg any]
action:[OCMArg anySelector]
forControlEvents:UIControlEventTouchUpInside]);
OCMExpect([mockInspireButton_ addTarget:[OCMArg any]
action:[OCMArg anySelector]
forControlEvents:UIControlEventTouchUpInside]);
[presenter_ viewLoaded];
}
- (void)testAddTargetsToButtonsCameraButtonEnabledWithCamera {
OCMStub([mockHomeView_ cameraButton]).andReturn(mockCameraButton_);
OCMStub([mockHomeView_ galleryButton]).andReturn(mockGalleryButton_);
OCMStub([mockHomeView_ inspireButton]).andReturn(mockInspireButton_);
OCMStub([mockImageHelper_ isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
.andReturn(YES);
OCMExpect([mockCameraButton_ addTarget:[OCMArg any]
action:[OCMArg anySelector]
forControlEvents:UIControlEventTouchUpInside]);
OCMExpect([mockGalleryButton_ addTarget:[OCMArg any]
action:[OCMArg anySelector]
forControlEvents:UIControlEventTouchUpInside]);
OCMExpect([mockInspireButton_ addTarget:[OCMArg any]
action:[OCMArg anySelector]
forControlEvents:UIControlEventTouchUpInside]);
[presenter_ viewLoaded];
}
- (void)testCameraButtonSelected {
// Get the action added to a real button (not a mock).
UIButton *button = [[UIButton alloc] init];
OCMStub([mockImageHelper_ isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
.andReturn(YES);
OCMStub([mockHomeView_ cameraButton]).andReturn(button);
OCMStub([mockHomeView_ galleryButton]).andReturn(mockGalleryButton_);
OCMStub([mockHomeView_ inspireButton]).andReturn(mockInspireButton_);
OCMExpect([mockGalleryButton_ addTarget:[OCMArg any]
action:[OCMArg anySelector]
forControlEvents:UIControlEventTouchUpInside]);
OCMExpect([mockInspireButton_ addTarget:[OCMArg any]
action:[OCMArg anySelector]
forControlEvents:UIControlEventTouchUpInside]);
[presenter_ viewLoaded];
// Make sure the right thing is called.
OCMExpect([mockViewController_ showImagePickerWithType:UIImagePickerControllerSourceTypeCamera
delegate:presenter_]);
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
OCMVerify([mockViewController_ showImagePickerWithType:UIImagePickerControllerSourceTypeCamera
delegate:presenter_]);
}
- (void)testGalleryButtonSelected {
// Get the action added to a real button (not a mock).
UIButton *button = [[UIButton alloc] init];
OCMStub([mockImageHelper_ isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
.andReturn(YES);
OCMStub([mockHomeView_ cameraButton]).andReturn(mockCameraButton_);
OCMStub([mockHomeView_ galleryButton]).andReturn(button);
OCMStub([mockHomeView_ inspireButton]).andReturn(mockInspireButton_);
OCMExpect([mockCameraButton_ addTarget:[OCMArg any]
action:[OCMArg anySelector]
forControlEvents:UIControlEventTouchUpInside]);
OCMExpect([mockInspireButton_ addTarget:[OCMArg any]
action:[OCMArg anySelector]
forControlEvents:UIControlEventTouchUpInside]);
[presenter_ viewLoaded];
// Make sure the right thing is called.
OCMExpect
([mockViewController_ showImagePickerWithType:UIImagePickerControllerSourceTypePhotoLibrary
delegate:presenter_]);
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
OCMVerify(
[mockViewController_ showImagePickerWithType:UIImagePickerControllerSourceTypePhotoLibrary
delegate:presenter_]);
}
- (void)testInspireButtonSelected {
// TODO(cate): Fill this in when it does something.
// Get the action added to a real button (not a mock).
UIButton *button = [[UIButton alloc] init];
OCMStub([mockImageHelper_ isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
.andReturn(YES);
OCMStub([mockHomeView_ cameraButton]).andReturn(mockCameraButton_);
OCMStub([mockHomeView_ galleryButton]).andReturn(mockGalleryButton_);
OCMStub([mockHomeView_ inspireButton]).andReturn(button);
OCMExpect([mockCameraButton_ addTarget:[OCMArg any]
action:[OCMArg anySelector]
forControlEvents:UIControlEventTouchUpInside]);
OCMExpect([mockGalleryButton_ addTarget:[OCMArg any]
action:[OCMArg anySelector]
forControlEvents:UIControlEventTouchUpInside]);
[presenter_ viewLoaded];
// Make sure the right thing is called.
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment