Skip to content

Commit

Permalink
Fix avoiding when automaticallyAdjustsScrollViewInsets is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
kirpichenko committed Nov 21, 2013
1 parent 90c32a4 commit 135a885
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 194 deletions.
113 changes: 113 additions & 0 deletions EKKeyboardAvoiding-UnitTests/ContenInsetRotationTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//
// ContenInsetAutoChangeTest.m
// EKKeyboardAvoiding
//
// Created by Evgeniy Kirpichenko on 10/30/13.
// Copyright (c) 2013 Evgeniy Kirpichenko. All rights reserved.
//

#import <XCTest/XCTest.h>

#import "EKFakeKeyboard.h"
#import "EKFakeKeyboardFrameListener.h"

#import "EKKeyboardAvoidingProvider.h"

@interface ContenInsetAutoChangeTest : XCTestCase
@property (nonatomic, strong) EKKeyboardAvoidingProvider *avoidingProvider;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic) UIEdgeInsets portraitAutomaticInset;
@property (nonatomic) UIEdgeInsets landscapeAutomaticInset;

@property (nonatomic) CGRect portraitFrame;
@property (nonatomic) CGRect landscapeFrame;
@end

@implementation ContenInsetAutoChangeTest

- (void)setUp
{
[super setUp];

self.portraitAutomaticInset = UIEdgeInsetsMake(64, 0, 49, 0);
self.landscapeAutomaticInset = UIEdgeInsetsMake(52, 0, 49, 0);

self.portraitFrame = CGRectMake(0, 0, 320, 568);
self.landscapeFrame = CGRectMake(0, 0, 568, 320);

self.scrollView = [[UIScrollView alloc] init];
self.avoidingProvider = [[EKKeyboardAvoidingProvider alloc] initWithScrollView:self.scrollView];
self.avoidingProvider.keyboardListener = [EKFakeKeyboardFrameListener new];

[self.avoidingProvider startAvoiding];
[self rotateScrollToPortraitOrientation];
}

- (void)tearDown
{
[self.avoidingProvider stopAvoiding];
[super tearDown];
}

- (void)testInitialContentInset
{
UIEdgeInsets expectedInset = UIEdgeInsetsMake(64, 0, 49, 0);
XCTAssertEqual([self.scrollView contentInset], expectedInset);
}

- (void)testRotationToLandscape
{
[self rotateScrollToLandscapeOrientation];

XCTAssertEqual([self.scrollView frame], self.landscapeFrame);
XCTAssertEqual([self.scrollView contentInset], self.landscapeAutomaticInset);
}

- (void)testPortraitRotateAndShowKeyboard
{
[self rotateScrollToPortraitOrientation];
[EKFakeKeyboard showWithFrame:CGRectMake(0, 352, 320, 216)];

UIEdgeInsets expectedInset = UIEdgeInsetsMake(64, 0, 216, 0);
XCTAssertEqual([self.scrollView contentInset], expectedInset);
}

- (void)testLandscapeRotateAndShowKeyboard
{
[self rotateScrollToLandscapeOrientation];
[EKFakeKeyboard showWithFrame:CGRectMake(0, 158, 568, 162)];

UIEdgeInsets expectedInset = UIEdgeInsetsMake(52, 0, 162, 0);
XCTAssertEqual([self.scrollView contentInset], expectedInset);
}

- (void)testLandscapeShowKeyboardAndRotate
{
[EKFakeKeyboard showWithFrame:CGRectMake(0, 352, 568, 162)];
[self rotateScrollToLandscapeOrientation];
[EKFakeKeyboard showWithFrame:CGRectMake(0, 158, 568, 162)];

UIEdgeInsets expectedInset = UIEdgeInsetsMake(52, 0, 162, 0);
XCTAssertEqual([self.scrollView contentInset], expectedInset);
}

#pragma mark - helpers

- (void)updateContentInset:(UIEdgeInsets)contentInset {
[[self scrollView] setContentInset:contentInset];
}

- (void)rotateScrollToPortraitOrientation
{
[self.scrollView setFrame:self.portraitFrame];
[self.scrollView setContentInset:self.portraitAutomaticInset];
}

- (void)rotateScrollToLandscapeOrientation
{
[self.scrollView setFrame:self.landscapeFrame];
[self.scrollView setContentInset:self.landscapeAutomaticInset];
}

@end
16 changes: 8 additions & 8 deletions EKKeyboardAvoiding-UnitTests/ContentInsetsCalculationTest.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// EKAvoidingListenerTest.m
// EKKeyboardAvoidingProviderTest.m
// EKKeyboardAvoiding
//
// Created by Evgeniy Kirpichenko on 9/26/13.
Expand All @@ -8,30 +8,30 @@

#import <XCTest/XCTest.h>

#import "EKAvoidingListener.h"
#import "EKKeyboardAvoidingProvider.h"

#import "EKFakeKeyboard.h"
#import "EKFakeKeyboardFrameListener.h"

@interface EKAvoidingListenerTest : XCTestCase {
@interface EKKeyboardAvoidingProviderTest : XCTestCase {
UIScrollView *scrollView;
EKAvoidingListener *listener;
EKKeyboardAvoidingProvider *avoidingProvider;
}

@end

@implementation EKAvoidingListenerTest
@implementation EKKeyboardAvoidingProviderTest

- (void)setUp {
[super setUp];

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 160, 320, 200)];
scrollView.contentInset = UIEdgeInsetsMake(40, 0, 100, 0);

listener = [[EKAvoidingListener alloc] initWithScrollView:scrollView];
listener.keyboardListener = [EKFakeKeyboardFrameListener new];
avoidingProvider = [[EKKeyboardAvoidingProvider alloc] initWithScrollView:scrollView];
avoidingProvider.keyboardListener = [EKFakeKeyboardFrameListener new];

[listener startAvoiding];
[avoidingProvider startAvoiding];
}

- (void)tearDown {
Expand Down
72 changes: 0 additions & 72 deletions EKKeyboardAvoiding-UnitTests/ContentInsetsChangesTest.m

This file was deleted.

46 changes: 46 additions & 0 deletions EKKeyboardAvoiding-UnitTests/EKAvoidingInsetCalculatorTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// EKAvoidingInsetCalculatorTest.m
// EKKeyboardAvoiding
//
// Created by Evgeniy Kirpichenko on 11/15/13.
// Copyright (c) 2013 Evgeniy Kirpichenko. All rights reserved.
//

#import <XCTest/XCTest.h>

#import "EKAvoidingInsetCalculator.h"

@interface EKAvoidingInsetCalculatorTest : XCTestCase
@property (nonatomic, strong) EKAvoidingInsetCalculator *calculator;
@end

@implementation EKAvoidingInsetCalculatorTest

- (void)setUp
{
[super setUp];

self.calculator = [EKAvoidingInsetCalculator new];
}

- (void)testViewFrameFullyContainsKeyboardFrame
{
self.calculator.scrollViewFrame = CGRectMake(0, 0, 568, 320);
self.calculator.keyboardFrame = CGRectMake(320, 0, 216, 320);
self.calculator.scrollViewInset = UIEdgeInsetsMake(10, 0, 20, 0);

UIEdgeInsets insets = [self.calculator calculateAvoidingInset];
XCTAssertEqual(insets, UIEdgeInsetsZero, @"shouldn't calculate any extra inset");
}

- (void)testKeyboardFrameContainsViewFrame
{
self.calculator.scrollViewFrame = CGRectMake(0, 352, 320, 216);
self.calculator.keyboardFrame = CGRectMake(0, 352, 320, 216);
self.calculator.scrollViewInset = UIEdgeInsetsMake(10, 0, 20, 0);

UIEdgeInsets insets = [self.calculator calculateAvoidingInset];
XCTAssertEqual(insets, UIEdgeInsetsZero, @"shouldn't calculate any extra inset");
}

@end
Loading

0 comments on commit 135a885

Please sign in to comment.